blob: 1b95451fb43f2dd5e6857ebf20c86e31fd5c157e [file] [log] [blame]
/* GStreamer Matroska muxer/demuxer
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
* (c) 2006 Tim-Philipp Müller <tim centricular net>
* (c) 2008 Sebastian Dröge <slomo@circular-chaos.org>
* (c) 2011 Debarshi Ray <rishi@gnu.org>
*
* matroska-demux.c: matroska file/stream demuxer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/* TODO: check CRC32 if present
* TODO: there can be a segment after the first segment. Handle like
* chained oggs. Fixes #334082
* TODO: Test samples: http://www.matroska.org/samples/matrix/index.html
* http://samples.mplayerhq.hu/Matroska/
* TODO: check if demuxing is done correct for all codecs according to spec
* TODO: seeking with incomplete or without CUE
*/
/**
* SECTION:element-matroskademux
*
* matroskademux demuxes a Matroska file into the different contained streams.
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch-1.0 -v filesrc location=/path/to/mkv ! matroskademux ! vorbisdec ! audioconvert ! audioresample ! autoaudiosink
* ]| This pipeline demuxes a Matroska file and outputs the contained Vorbis audio.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <string.h>
#include <glib/gprintf.h>
/* For AVI compatibility mode
and for fourcc stuff */
#include <gst/riff/riff-read.h>
#include <gst/riff/riff-ids.h>
#include <gst/riff/riff-media.h>
#include <gst/audio/audio.h>
#include <gst/tag/tag.h>
#include <gst/pbutils/pbutils.h>
#include <gst/video/video.h>
#include "matroska-demux.h"
#include "matroska-ids.h"
GST_DEBUG_CATEGORY_STATIC (matroskademux_debug);
#define GST_CAT_DEFAULT matroskademux_debug
#define DEBUG_ELEMENT_START(demux, ebml, element) \
GST_DEBUG_OBJECT (demux, "Parsing " element " element at offset %" \
G_GUINT64_FORMAT, gst_ebml_read_get_pos (ebml))
#define DEBUG_ELEMENT_STOP(demux, ebml, element, ret) \
GST_DEBUG_OBJECT (demux, "Parsing " element " element " \
" finished with '%s'", gst_flow_get_name (ret))
enum
{
PROP_0,
PROP_METADATA,
PROP_STREAMINFO,
PROP_MAX_GAP_TIME
};
#define DEFAULT_MAX_GAP_TIME (2 * GST_SECOND)
#define INVALID_DATA_THRESHOLD (2 * 1024 * 1024)
static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-matroska; video/x-matroska; "
"video/x-matroska-3d; audio/webm; video/webm")
);
/* TODO: fill in caps! */
static GstStaticPadTemplate audio_src_templ =
GST_STATIC_PAD_TEMPLATE ("audio_%u",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("ANY")
);
static GstStaticPadTemplate video_src_templ =
GST_STATIC_PAD_TEMPLATE ("video_%u",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("ANY")
);
static GstStaticPadTemplate subtitle_src_templ =
GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("text/x-raw, format=pango-markup; application/x-ssa; "
"application/x-ass;application/x-usf; subpicture/x-dvd; "
"subpicture/x-pgs; subtitle/x-kate; " "application/x-subtitle-unknown")
);
static GstFlowReturn gst_matroska_demux_parse_id (GstMatroskaDemux * demux,
guint32 id, guint64 length, guint needed);
/* element functions */
static void gst_matroska_demux_loop (GstPad * pad);
static gboolean gst_matroska_demux_element_send_event (GstElement * element,
GstEvent * event);
static gboolean gst_matroska_demux_element_query (GstElement * element,
GstQuery * query);
/* pad functions */
static gboolean gst_matroska_demux_sink_activate (GstPad * sinkpad,
GstObject * parent);
static gboolean gst_matroska_demux_sink_activate_mode (GstPad * sinkpad,
GstObject * parent, GstPadMode mode, gboolean active);
static gboolean gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
GstPad * pad, GstEvent * event);
static gboolean gst_matroska_demux_handle_src_event (GstPad * pad,
GstObject * parent, GstEvent * event);
static gboolean gst_matroska_demux_handle_src_query (GstPad * pad,
GstObject * parent, GstQuery * query);
static gboolean gst_matroska_demux_handle_sink_event (GstPad * pad,
GstObject * parent, GstEvent * event);
static GstFlowReturn gst_matroska_demux_chain (GstPad * pad,
GstObject * object, GstBuffer * buffer);
static GstStateChangeReturn
gst_matroska_demux_change_state (GstElement * element,
GstStateChange transition);
#if 0
static void
gst_matroska_demux_set_index (GstElement * element, GstIndex * index);
static GstIndex *gst_matroska_demux_get_index (GstElement * element);
#endif
/* caps functions */
static GstCaps *gst_matroska_demux_video_caps (GstMatroskaTrackVideoContext
* videocontext, const gchar * codec_id, guint8 * data, guint size,
gchar ** codec_name, guint32 * riff_fourcc);
static GstCaps *gst_matroska_demux_audio_caps (GstMatroskaTrackAudioContext
* audiocontext, const gchar * codec_id, guint8 * data, guint size,
gchar ** codec_name, guint16 * riff_audio_fmt);
static GstCaps
* gst_matroska_demux_subtitle_caps (GstMatroskaTrackSubtitleContext *
subtitlecontext, const gchar * codec_id, gpointer data, guint size);
/* stream methods */
static void gst_matroska_demux_reset (GstElement * element);
static gboolean perform_seek_to_offset (GstMatroskaDemux * demux,
gdouble rate, guint64 offset, guint32 seqnum, GstSeekFlags flags);
/* gobject functions */
static void gst_matroska_demux_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_matroska_demux_get_property (GObject * object,
guint prop_id, GValue * value, GParamSpec * pspec);
GType gst_matroska_demux_get_type (void);
#define parent_class gst_matroska_demux_parent_class
G_DEFINE_TYPE (GstMatroskaDemux, gst_matroska_demux, GST_TYPE_ELEMENT);
static void
gst_matroska_demux_finalize (GObject * object)
{
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (object);
gst_matroska_read_common_finalize (&demux->common);
gst_flow_combiner_free (demux->flowcombiner);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_matroska_demux_class_init (GstMatroskaDemuxClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstElementClass *gstelement_class = (GstElementClass *) klass;
GST_DEBUG_CATEGORY_INIT (matroskademux_debug, "matroskademux", 0,
"Matroska demuxer");
gobject_class->finalize = gst_matroska_demux_finalize;
gobject_class->get_property = gst_matroska_demux_get_property;
gobject_class->set_property = gst_matroska_demux_set_property;
g_object_class_install_property (gobject_class, PROP_MAX_GAP_TIME,
g_param_spec_uint64 ("max-gap-time", "Maximum gap time",
"The demuxer sends out segment events for skipping "
"gaps longer than this (0 = disabled).", 0, G_MAXUINT64,
DEFAULT_MAX_GAP_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_matroska_demux_change_state);
gstelement_class->send_event =
GST_DEBUG_FUNCPTR (gst_matroska_demux_element_send_event);
gstelement_class->query =
GST_DEBUG_FUNCPTR (gst_matroska_demux_element_query);
#if 0
gstelement_class->set_index =
GST_DEBUG_FUNCPTR (gst_matroska_demux_set_index);
gstelement_class->get_index =
GST_DEBUG_FUNCPTR (gst_matroska_demux_get_index);
#endif
gst_element_class_add_static_pad_template (gstelement_class,
&video_src_templ);
gst_element_class_add_static_pad_template (gstelement_class,
&audio_src_templ);
gst_element_class_add_static_pad_template (gstelement_class,
&subtitle_src_templ);
gst_element_class_add_static_pad_template (gstelement_class, &sink_templ);
gst_element_class_set_static_metadata (gstelement_class, "Matroska demuxer",
"Codec/Demuxer",
"Demuxes Matroska/WebM streams into video/audio/subtitles",
"GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
}
static void
gst_matroska_demux_init (GstMatroskaDemux * demux)
{
demux->common.sinkpad = gst_pad_new_from_static_template (&sink_templ,
"sink");
gst_pad_set_activate_function (demux->common.sinkpad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate));
gst_pad_set_activatemode_function (demux->common.sinkpad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_sink_activate_mode));
gst_pad_set_chain_function (demux->common.sinkpad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_chain));
gst_pad_set_event_function (demux->common.sinkpad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_sink_event));
gst_element_add_pad (GST_ELEMENT (demux), demux->common.sinkpad);
/* init defaults for common read context */
gst_matroska_read_common_init (&demux->common);
/* property defaults */
demux->max_gap_time = DEFAULT_MAX_GAP_TIME;
GST_OBJECT_FLAG_SET (demux, GST_ELEMENT_FLAG_INDEXABLE);
demux->flowcombiner = gst_flow_combiner_new ();
/* finish off */
gst_matroska_demux_reset (GST_ELEMENT (demux));
}
static void
gst_matroska_demux_reset (GstElement * element)
{
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
GST_DEBUG_OBJECT (demux, "Resetting state");
gst_matroska_read_common_reset (GST_ELEMENT (demux), &demux->common);
demux->num_a_streams = 0;
demux->num_t_streams = 0;
demux->num_v_streams = 0;
demux->have_group_id = FALSE;
demux->group_id = G_MAXUINT;
demux->clock = NULL;
demux->tracks_parsed = FALSE;
if (demux->clusters) {
g_array_free (demux->clusters, TRUE);
demux->clusters = NULL;
}
g_list_foreach (demux->seek_parsed,
(GFunc) gst_matroska_read_common_free_parsed_el, NULL);
g_list_free (demux->seek_parsed);
demux->seek_parsed = NULL;
demux->last_stop_end = GST_CLOCK_TIME_NONE;
demux->seek_block = 0;
demux->stream_start_time = GST_CLOCK_TIME_NONE;
demux->to_time = GST_CLOCK_TIME_NONE;
demux->cluster_time = GST_CLOCK_TIME_NONE;
demux->cluster_offset = 0;
demux->next_cluster_offset = 0;
demux->stream_last_time = GST_CLOCK_TIME_NONE;
demux->last_cluster_offset = 0;
demux->index_offset = 0;
demux->seekable = FALSE;
demux->need_segment = FALSE;
demux->segment_seqnum = 0;
demux->requested_seek_time = GST_CLOCK_TIME_NONE;
demux->seek_offset = -1;
demux->building_index = FALSE;
if (demux->seek_event) {
gst_event_unref (demux->seek_event);
demux->seek_event = NULL;
}
demux->seek_index = NULL;
demux->seek_entry = 0;
if (demux->new_segment) {
gst_event_unref (demux->new_segment);
demux->new_segment = NULL;
}
demux->invalid_duration = FALSE;
demux->cached_length = G_MAXUINT64;
gst_flow_combiner_clear (demux->flowcombiner);
}
static GstBuffer *
gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf)
{
GstMapInfo map;
gpointer data;
gsize size;
g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
GST_DEBUG ("decoding buffer %p", buf);
gst_buffer_map (buf, &map, GST_MAP_READ);
data = map.data;
size = map.size;
g_return_val_if_fail (size > 0, buf);
if (gst_matroska_decode_data (context->encodings, &data, &size,
GST_MATROSKA_TRACK_ENCODING_SCOPE_FRAME, FALSE)) {
gst_buffer_unmap (buf, &map);
gst_buffer_unref (buf);
return gst_buffer_new_wrapped (data, size);
} else {
GST_DEBUG ("decode data failed");
gst_buffer_unmap (buf, &map);
gst_buffer_unref (buf);
return NULL;
}
}
static void
gst_matroska_demux_add_stream_headers_to_caps (GstMatroskaDemux * demux,
GstBufferList * list, GstCaps * caps)
{
GstStructure *s;
GValue arr_val = G_VALUE_INIT;
GValue buf_val = G_VALUE_INIT;
gint i, num;
g_assert (gst_caps_is_writable (caps));
g_value_init (&arr_val, GST_TYPE_ARRAY);
g_value_init (&buf_val, GST_TYPE_BUFFER);
num = gst_buffer_list_length (list);
for (i = 0; i < num; ++i) {
g_value_set_boxed (&buf_val, gst_buffer_list_get (list, i));
gst_value_array_append_value (&arr_val, &buf_val);
}
s = gst_caps_get_structure (caps, 0);
gst_structure_take_value (s, "streamheader", &arr_val);
g_value_unset (&buf_val);
}
static GstFlowReturn
gst_matroska_demux_parse_colour (GstMatroskaDemux * demux, GstEbmlRead * ebml,
GstMatroskaTrackVideoContext * video_context)
{
GstFlowReturn ret;
GstVideoColorimetry colorimetry;
guint32 id;
guint64 num;
colorimetry.range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
DEBUG_ELEMENT_START (demux, ebml, "TrackVideoColour");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK)
goto beach;
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
goto beach;
switch (id) {
case GST_MATROSKA_ID_VIDEOMATRIXCOEFFICIENTS:{
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
goto beach;
switch (num) {
case 0:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
break;
case 1:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT709;
break;
case 2:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
break;
case 4:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_FCC;
break;
/* FIXME: "5: BT470BG" is undefined in GstVideoColorMatrix
* but it's functionally same as "6: BT601" */
case 5:
case 6:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT601;
break;
case 7:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
break;
case 9:
colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
break;
default:
GST_FIXME_OBJECT (demux, "Unsupported color matrix coefficients %"
G_GUINT64_FORMAT, num);
break;
}
break;
}
case GST_MATROSKA_ID_VIDEORANGE:{
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
goto beach;
switch (num) {
case 0:
colorimetry.range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
break;
case 1:
colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
break;
case 2:
colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
break;
default:
GST_FIXME_OBJECT (demux, "Unsupported color range %"
G_GUINT64_FORMAT, num);
break;
}
break;
}
case GST_MATROSKA_ID_VIDEOTRANSFERCHARACTERISTICS:{
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
goto beach;
switch (num) {
/* FIXME: "6: BT601" and "14: BT2020_10" are undefined in
* GstVideoTransferFunction, but functionally same as "1: BT709" */
case 1:
case 6:
case 14:
colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
break;
case 2:
colorimetry.transfer = GST_VIDEO_TRANSFER_UNKNOWN;
break;
case 4:
colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA22;
break;
case 5:
colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA28;
break;
case 7:
colorimetry.transfer = GST_VIDEO_TRANSFER_SMPTE240M;
break;
case 8:
colorimetry.transfer = GST_VIDEO_TRANSFER_GAMMA10;
break;
case 9:
colorimetry.transfer = GST_VIDEO_TRANSFER_LOG100;
break;
case 10:
colorimetry.transfer = GST_VIDEO_TRANSFER_LOG316;
break;
case 13:
colorimetry.transfer = GST_VIDEO_TRANSFER_SRGB;
break;
case 15:
colorimetry.transfer = GST_VIDEO_TRANSFER_BT2020_12;
break;
default:
GST_FIXME_OBJECT (demux,
"Unsupported color transfer characteristics %"
G_GUINT64_FORMAT, num);
break;
}
break;
}
case GST_MATROSKA_ID_VIDEOPRIMARIES:{
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
goto beach;
switch (num) {
case 1:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
break;
case 2:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
break;
case 4:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
break;
case 5:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
break;
case 6:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
break;
case 7:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
break;
case 8:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_FILM;
break;
case 9:
colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
break;
default:
GST_FIXME_OBJECT (demux, "Unsupported color primaries %"
G_GUINT64_FORMAT, num);
break;
}
break;
}
default:
GST_FIXME_OBJECT (demux, "Unsupported subelement 0x%x in Colour", id);
ret = gst_ebml_read_skip (ebml);
break;
}
}
memcpy (&video_context->colorimetry, &colorimetry,
sizeof (GstVideoColorimetry));
beach:
DEBUG_ELEMENT_STOP (demux, ebml, "TrackVideoColour", ret);
return ret;
}
static GstFlowReturn
gst_matroska_demux_parse_stream (GstMatroskaDemux * demux, GstEbmlRead * ebml,
GstMatroskaTrackContext ** dest_context)
{
GstMatroskaTrackContext *context;
GstCaps *caps = NULL;
GstTagList *cached_taglist;
GstFlowReturn ret;
guint32 id, riff_fourcc = 0;
guint16 riff_audio_fmt = 0;
gchar *codec = NULL;
DEBUG_ELEMENT_START (demux, ebml, "TrackEntry");
/* start with the master */
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
return ret;
}
/* allocate generic... if we know the type, we'll g_renew()
* with the precise type */
context = g_new0 (GstMatroskaTrackContext, 1);
context->index_writer_id = -1;
context->type = 0; /* no type yet */
context->default_duration = 0;
context->pos = 0;
context->set_discont = TRUE;
context->timecodescale = 1.0;
context->flags =
GST_MATROSKA_TRACK_ENABLED | GST_MATROSKA_TRACK_DEFAULT |
GST_MATROSKA_TRACK_LACING;
context->from_time = GST_CLOCK_TIME_NONE;
context->from_offset = -1;
context->to_offset = G_MAXINT64;
context->alignment = 1;
context->dts_only = FALSE;
context->intra_only = FALSE;
context->tags = gst_tag_list_new_empty ();
GST_DEBUG_OBJECT (demux, "Parsing a TrackEntry (%d tracks parsed so far)",
demux->common.num_streams);
/* try reading the trackentry headers */
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* track number (unique stream ID) */
case GST_MATROSKA_ID_TRACKNUMBER:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_ERROR_OBJECT (demux, "Invalid TrackNumber 0");
ret = GST_FLOW_ERROR;
break;
}
GST_DEBUG_OBJECT (demux, "TrackNumber: %" G_GUINT64_FORMAT, num);
context->num = num;
break;
}
/* track UID (unique identifier) */
case GST_MATROSKA_ID_TRACKUID:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_ERROR_OBJECT (demux, "Invalid TrackUID 0");
ret = GST_FLOW_ERROR;
break;
}
GST_DEBUG_OBJECT (demux, "TrackUID: %" G_GUINT64_FORMAT, num);
context->uid = num;
break;
}
/* track type (video, audio, combined, subtitle, etc.) */
case GST_MATROSKA_ID_TRACKTYPE:{
guint64 track_type;
if ((ret = gst_ebml_read_uint (ebml, &id, &track_type)) != GST_FLOW_OK) {
break;
}
if (context->type != 0 && context->type != track_type) {
GST_WARNING_OBJECT (demux,
"More than one tracktype defined in a TrackEntry - skipping");
break;
} else if (track_type < 1 || track_type > 254) {
GST_WARNING_OBJECT (demux, "Invalid TrackType %" G_GUINT64_FORMAT,
track_type);
break;
}
GST_DEBUG_OBJECT (demux, "TrackType: %" G_GUINT64_FORMAT, track_type);
/* ok, so we're actually going to reallocate this thing */
switch (track_type) {
case GST_MATROSKA_TRACK_TYPE_VIDEO:
gst_matroska_track_init_video_context (&context);
break;
case GST_MATROSKA_TRACK_TYPE_AUDIO:
gst_matroska_track_init_audio_context (&context);
break;
case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
gst_matroska_track_init_subtitle_context (&context);
break;
case GST_MATROSKA_TRACK_TYPE_COMPLEX:
case GST_MATROSKA_TRACK_TYPE_LOGO:
case GST_MATROSKA_TRACK_TYPE_BUTTONS:
case GST_MATROSKA_TRACK_TYPE_CONTROL:
default:
GST_WARNING_OBJECT (demux,
"Unknown or unsupported TrackType %" G_GUINT64_FORMAT,
track_type);
context->type = 0;
break;
}
break;
}
/* tracktype specific stuff for video */
case GST_MATROSKA_ID_TRACKVIDEO:{
GstMatroskaTrackVideoContext *videocontext;
DEBUG_ELEMENT_START (demux, ebml, "TrackVideo");
if (!gst_matroska_track_init_video_context (&context)) {
GST_WARNING_OBJECT (demux,
"TrackVideo element in non-video track - ignoring track");
ret = GST_FLOW_ERROR;
break;
} else if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
break;
}
videocontext = (GstMatroskaTrackVideoContext *) context;
while (ret == GST_FLOW_OK &&
gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* Should be one level up but some broken muxers write it here. */
case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
break;
}
GST_DEBUG_OBJECT (demux,
"TrackDefaultDuration: %" G_GUINT64_FORMAT, num);
context->default_duration = num;
break;
}
/* video framerate */
/* NOTE: This one is here only for backward compatibility.
* Use _TRACKDEFAULDURATION one level up. */
case GST_MATROSKA_ID_VIDEOFRAMERATE:{
gdouble num;
if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num <= 0.0) {
GST_WARNING_OBJECT (demux, "Invalid TrackVideoFPS %lf", num);
break;
}
GST_DEBUG_OBJECT (demux, "TrackVideoFrameRate: %lf", num);
if (context->default_duration == 0)
context->default_duration =
gst_gdouble_to_guint64 ((gdouble) GST_SECOND * (1.0 / num));
videocontext->default_fps = num;
break;
}
/* width of the size to display the video at */
case GST_MATROSKA_ID_VIDEODISPLAYWIDTH:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayWidth 0");
break;
}
GST_DEBUG_OBJECT (demux,
"TrackVideoDisplayWidth: %" G_GUINT64_FORMAT, num);
videocontext->display_width = num;
break;
}
/* height of the size to display the video at */
case GST_MATROSKA_ID_VIDEODISPLAYHEIGHT:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackVideoDisplayHeight 0");
break;
}
GST_DEBUG_OBJECT (demux,
"TrackVideoDisplayHeight: %" G_GUINT64_FORMAT, num);
videocontext->display_height = num;
break;
}
/* width of the video in the file */
case GST_MATROSKA_ID_VIDEOPIXELWIDTH:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelWidth 0");
break;
}
GST_DEBUG_OBJECT (demux,
"TrackVideoPixelWidth: %" G_GUINT64_FORMAT, num);
videocontext->pixel_width = num;
break;
}
/* height of the video in the file */
case GST_MATROSKA_ID_VIDEOPIXELHEIGHT:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackVideoPixelHeight 0");
break;
}
GST_DEBUG_OBJECT (demux,
"TrackVideoPixelHeight: %" G_GUINT64_FORMAT, num);
videocontext->pixel_height = num;
break;
}
/* whether the video is interlaced */
case GST_MATROSKA_ID_VIDEOFLAGINTERLACED:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num)
context->flags |= GST_MATROSKA_VIDEOTRACK_INTERLACED;
else
context->flags &= ~GST_MATROSKA_VIDEOTRACK_INTERLACED;
GST_DEBUG_OBJECT (demux, "TrackVideoInterlaced: %d",
(context->flags & GST_MATROSKA_VIDEOTRACK_INTERLACED) ? 1 :
0);
break;
}
/* aspect ratio behaviour */
case GST_MATROSKA_ID_VIDEOASPECTRATIOTYPE:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num != GST_MATROSKA_ASPECT_RATIO_MODE_FREE &&
num != GST_MATROSKA_ASPECT_RATIO_MODE_KEEP &&
num != GST_MATROSKA_ASPECT_RATIO_MODE_FIXED) {
GST_WARNING_OBJECT (demux,
"Unknown TrackVideoAspectRatioType 0x%x", (guint) num);
break;
}
GST_DEBUG_OBJECT (demux,
"TrackVideoAspectRatioType: %" G_GUINT64_FORMAT, num);
videocontext->asr_mode = num;
break;
}
/* colourspace (only matters for raw video) fourcc */
case GST_MATROSKA_ID_VIDEOCOLOURSPACE:{
guint8 *data;
guint64 datalen;
if ((ret =
gst_ebml_read_binary (ebml, &id, &data,
&datalen)) != GST_FLOW_OK)
break;
if (datalen != 4) {
g_free (data);
GST_WARNING_OBJECT (demux,
"Invalid TrackVideoColourSpace length %" G_GUINT64_FORMAT,
datalen);
break;
}
memcpy (&videocontext->fourcc, data, 4);
GST_DEBUG_OBJECT (demux,
"TrackVideoColourSpace: %" GST_FOURCC_FORMAT,
GST_FOURCC_ARGS (videocontext->fourcc));
g_free (data);
break;
}
/* color info */
case GST_MATROSKA_ID_VIDEOCOLOUR:{
ret = gst_matroska_demux_parse_colour (demux, ebml, videocontext);
break;
}
case GST_MATROSKA_ID_VIDEOSTEREOMODE:
{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
GST_DEBUG_OBJECT (demux, "StereoMode: %" G_GUINT64_FORMAT, num);
switch (num) {
case GST_MATROSKA_STEREO_MODE_SBS_RL:
videocontext->multiview_flags =
GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
/* fall through */
case GST_MATROSKA_STEREO_MODE_SBS_LR:
videocontext->multiview_mode =
GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE;
break;
case GST_MATROSKA_STEREO_MODE_TB_RL:
videocontext->multiview_flags =
GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
/* fall through */
case GST_MATROSKA_STEREO_MODE_TB_LR:
videocontext->multiview_mode =
GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM;
break;
case GST_MATROSKA_STEREO_MODE_CHECKER_RL:
videocontext->multiview_flags =
GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
/* fall through */
case GST_MATROSKA_STEREO_MODE_CHECKER_LR:
videocontext->multiview_mode =
GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD;
break;
case GST_MATROSKA_STEREO_MODE_FBF_RL:
videocontext->multiview_flags =
GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST;
/* fall through */
case GST_MATROSKA_STEREO_MODE_FBF_LR:
videocontext->multiview_mode =
GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME;
/* FIXME: In frame-by-frame mode, left/right frame buffers are
* laced within one block, and we'll need to apply FIRST_IN_BUNDLE
* accordingly. See http://www.matroska.org/technical/specs/index.html#StereoMode */
GST_FIXME_OBJECT (demux,
"Frame-by-frame stereoscopic mode not fully implemented");
break;
}
break;
}
default:
GST_WARNING_OBJECT (demux,
"Unknown TrackVideo subelement 0x%x - ignoring", id);
/* fall through */
case GST_MATROSKA_ID_VIDEODISPLAYUNIT:
case GST_MATROSKA_ID_VIDEOPIXELCROPBOTTOM:
case GST_MATROSKA_ID_VIDEOPIXELCROPTOP:
case GST_MATROSKA_ID_VIDEOPIXELCROPLEFT:
case GST_MATROSKA_ID_VIDEOPIXELCROPRIGHT:
case GST_MATROSKA_ID_VIDEOGAMMAVALUE:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (demux, ebml, "TrackVideo", ret);
break;
}
/* tracktype specific stuff for audio */
case GST_MATROSKA_ID_TRACKAUDIO:{
GstMatroskaTrackAudioContext *audiocontext;
DEBUG_ELEMENT_START (demux, ebml, "TrackAudio");
if (!gst_matroska_track_init_audio_context (&context)) {
GST_WARNING_OBJECT (demux,
"TrackAudio element in non-audio track - ignoring track");
ret = GST_FLOW_ERROR;
break;
}
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK)
break;
audiocontext = (GstMatroskaTrackAudioContext *) context;
while (ret == GST_FLOW_OK &&
gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* samplerate */
case GST_MATROSKA_ID_AUDIOSAMPLINGFREQ:{
gdouble num;
if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num <= 0.0) {
GST_WARNING_OBJECT (demux,
"Invalid TrackAudioSamplingFrequency %lf", num);
break;
}
GST_DEBUG_OBJECT (demux, "TrackAudioSamplingFrequency: %lf", num);
audiocontext->samplerate = num;
break;
}
/* bitdepth */
case GST_MATROSKA_ID_AUDIOBITDEPTH:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackAudioBitDepth 0");
break;
}
GST_DEBUG_OBJECT (demux, "TrackAudioBitDepth: %" G_GUINT64_FORMAT,
num);
audiocontext->bitdepth = num;
break;
}
/* channels */
case GST_MATROSKA_ID_AUDIOCHANNELS:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackAudioChannels 0");
break;
}
GST_DEBUG_OBJECT (demux, "TrackAudioChannels: %" G_GUINT64_FORMAT,
num);
audiocontext->channels = num;
break;
}
default:
GST_WARNING_OBJECT (demux,
"Unknown TrackAudio subelement 0x%x - ignoring", id);
/* fall through */
case GST_MATROSKA_ID_AUDIOCHANNELPOSITIONS:
case GST_MATROSKA_ID_AUDIOOUTPUTSAMPLINGFREQ:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (demux, ebml, "TrackAudio", ret);
break;
}
/* codec identifier */
case GST_MATROSKA_ID_CODECID:{
gchar *text;
if ((ret = gst_ebml_read_ascii (ebml, &id, &text)) != GST_FLOW_OK)
break;
GST_DEBUG_OBJECT (demux, "CodecID: %s", GST_STR_NULL (text));
context->codec_id = text;
break;
}
/* codec private data */
case GST_MATROSKA_ID_CODECPRIVATE:{
guint8 *data;
guint64 size;
if ((ret =
gst_ebml_read_binary (ebml, &id, &data, &size)) != GST_FLOW_OK)
break;
context->codec_priv = data;
context->codec_priv_size = size;
GST_DEBUG_OBJECT (demux, "CodecPrivate of size %" G_GUINT64_FORMAT,
size);
break;
}
/* name of the codec */
case GST_MATROSKA_ID_CODECNAME:{
gchar *text;
if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
break;
GST_DEBUG_OBJECT (demux, "CodecName: %s", GST_STR_NULL (text));
context->codec_name = text;
break;
}
/* codec delay */
case GST_MATROSKA_ID_CODECDELAY:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
context->codec_delay = num;
GST_DEBUG_OBJECT (demux, "CodecDelay: %" GST_TIME_FORMAT,
GST_TIME_ARGS (num));
break;
}
/* codec delay */
case GST_MATROSKA_ID_SEEKPREROLL:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
context->seek_preroll = num;
GST_DEBUG_OBJECT (demux, "SeekPreroll: %" GST_TIME_FORMAT,
GST_TIME_ARGS (num));
break;
}
/* name of this track */
case GST_MATROSKA_ID_TRACKNAME:{
gchar *text;
if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
break;
context->name = text;
GST_DEBUG_OBJECT (demux, "TrackName: %s", GST_STR_NULL (text));
break;
}
/* language (matters for audio/subtitles, mostly) */
case GST_MATROSKA_ID_TRACKLANGUAGE:{
gchar *text;
if ((ret = gst_ebml_read_utf8 (ebml, &id, &text)) != GST_FLOW_OK)
break;
context->language = text;
/* fre-ca => fre */
if (strlen (context->language) >= 4 && context->language[3] == '-')
context->language[3] = '\0';
GST_DEBUG_OBJECT (demux, "TrackLanguage: %s",
GST_STR_NULL (context->language));
break;
}
/* whether this is actually used */
case GST_MATROSKA_ID_TRACKFLAGENABLED:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num)
context->flags |= GST_MATROSKA_TRACK_ENABLED;
else
context->flags &= ~GST_MATROSKA_TRACK_ENABLED;
GST_DEBUG_OBJECT (demux, "TrackEnabled: %d",
(context->flags & GST_MATROSKA_TRACK_ENABLED) ? 1 : 0);
break;
}
/* whether it's the default for this track type */
case GST_MATROSKA_ID_TRACKFLAGDEFAULT:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num)
context->flags |= GST_MATROSKA_TRACK_DEFAULT;
else
context->flags &= ~GST_MATROSKA_TRACK_DEFAULT;
GST_DEBUG_OBJECT (demux, "TrackDefault: %d",
(context->flags & GST_MATROSKA_TRACK_DEFAULT) ? 1 : 0);
break;
}
/* whether the track must be used during playback */
case GST_MATROSKA_ID_TRACKFLAGFORCED:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num)
context->flags |= GST_MATROSKA_TRACK_FORCED;
else
context->flags &= ~GST_MATROSKA_TRACK_FORCED;
GST_DEBUG_OBJECT (demux, "TrackForced: %d",
(context->flags & GST_MATROSKA_TRACK_FORCED) ? 1 : 0);
break;
}
/* lacing (like MPEG, where blocks don't end/start on frame
* boundaries) */
case GST_MATROSKA_ID_TRACKFLAGLACING:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num)
context->flags |= GST_MATROSKA_TRACK_LACING;
else
context->flags &= ~GST_MATROSKA_TRACK_LACING;
GST_DEBUG_OBJECT (demux, "TrackLacing: %d",
(context->flags & GST_MATROSKA_TRACK_LACING) ? 1 : 0);
break;
}
/* default length (in time) of one data block in this track */
case GST_MATROSKA_ID_TRACKDEFAULTDURATION:{
guint64 num;
if ((ret = gst_ebml_read_uint (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num == 0) {
GST_WARNING_OBJECT (demux, "Invalid TrackDefaultDuration 0");
break;
}
GST_DEBUG_OBJECT (demux, "TrackDefaultDuration: %" G_GUINT64_FORMAT,
num);
context->default_duration = num;
break;
}
case GST_MATROSKA_ID_CONTENTENCODINGS:{
ret = gst_matroska_read_common_read_track_encodings (&demux->common,
ebml, context);
break;
}
case GST_MATROSKA_ID_TRACKTIMECODESCALE:{
gdouble num;
if ((ret = gst_ebml_read_float (ebml, &id, &num)) != GST_FLOW_OK)
break;
if (num <= 0.0) {
GST_WARNING_OBJECT (demux, "Invalid TrackTimeCodeScale %lf", num);
break;
}
GST_DEBUG_OBJECT (demux, "TrackTimeCodeScale: %lf", num);
context->timecodescale = num;
break;
}
default:
GST_WARNING ("Unknown TrackEntry subelement 0x%x - ignoring", id);
/* pass-through */
/* we ignore these because they're nothing useful (i.e. crap)
* or simply not implemented yet. */
case GST_MATROSKA_ID_TRACKMINCACHE:
case GST_MATROSKA_ID_TRACKMAXCACHE:
case GST_MATROSKA_ID_MAXBLOCKADDITIONID:
case GST_MATROSKA_ID_TRACKATTACHMENTLINK:
case GST_MATROSKA_ID_TRACKOVERLAY:
case GST_MATROSKA_ID_TRACKTRANSLATE:
case GST_MATROSKA_ID_TRACKOFFSET:
case GST_MATROSKA_ID_CODECSETTINGS:
case GST_MATROSKA_ID_CODECINFOURL:
case GST_MATROSKA_ID_CODECDOWNLOADURL:
case GST_MATROSKA_ID_CODECDECODEALL:
ret = gst_ebml_read_skip (ebml);
break;
}
}
DEBUG_ELEMENT_STOP (demux, ebml, "TrackEntry", ret);
/* Decode codec private data if necessary */
if (context->encodings && context->encodings->len > 0 && context->codec_priv
&& context->codec_priv_size > 0) {
if (!gst_matroska_decode_data (context->encodings,
&context->codec_priv, &context->codec_priv_size,
GST_MATROSKA_TRACK_ENCODING_SCOPE_CODEC_DATA, TRUE)) {
GST_WARNING_OBJECT (demux, "Decoding codec private data failed");
ret = GST_FLOW_ERROR;
}
}
if (context->type == 0 || context->codec_id == NULL || (ret != GST_FLOW_OK
&& ret != GST_FLOW_EOS)) {
if (ret == GST_FLOW_OK || ret == GST_FLOW_EOS)
GST_WARNING_OBJECT (ebml, "Unknown stream/codec in track entry header");
gst_matroska_track_free (context);
context = NULL;
*dest_context = NULL;
return ret;
}
/* check for a cached track taglist */
cached_taglist =
(GstTagList *) g_hash_table_lookup (demux->common.cached_track_taglists,
GUINT_TO_POINTER (context->uid));
if (cached_taglist)
gst_tag_list_insert (context->tags, cached_taglist, GST_TAG_MERGE_APPEND);
/* compute caps */
switch (context->type) {
case GST_MATROSKA_TRACK_TYPE_VIDEO:{
GstMatroskaTrackVideoContext *videocontext =
(GstMatroskaTrackVideoContext *) context;
caps = gst_matroska_demux_video_caps (videocontext,
context->codec_id, context->codec_priv,
context->codec_priv_size, &codec, &riff_fourcc);
if (codec) {
gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_VIDEO_CODEC, codec, NULL);
context->tags_changed = TRUE;
g_free (codec);
}
break;
}
case GST_MATROSKA_TRACK_TYPE_AUDIO:{
GstMatroskaTrackAudioContext *audiocontext =
(GstMatroskaTrackAudioContext *) context;
caps = gst_matroska_demux_audio_caps (audiocontext,
context->codec_id, context->codec_priv, context->codec_priv_size,
&codec, &riff_audio_fmt);
if (codec) {
gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_AUDIO_CODEC, codec, NULL);
context->tags_changed = TRUE;
g_free (codec);
}
break;
}
case GST_MATROSKA_TRACK_TYPE_SUBTITLE:{
GstMatroskaTrackSubtitleContext *subtitlecontext =
(GstMatroskaTrackSubtitleContext *) context;
caps = gst_matroska_demux_subtitle_caps (subtitlecontext,
context->codec_id, context->codec_priv, context->codec_priv_size);
break;
}
case GST_MATROSKA_TRACK_TYPE_COMPLEX:
case GST_MATROSKA_TRACK_TYPE_LOGO:
case GST_MATROSKA_TRACK_TYPE_BUTTONS:
case GST_MATROSKA_TRACK_TYPE_CONTROL:
default:
/* we should already have quit by now */
g_assert_not_reached ();
}
if ((context->language == NULL || *context->language == '\0') &&
(context->type == GST_MATROSKA_TRACK_TYPE_AUDIO ||
context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)) {
GST_LOG ("stream %d: language=eng (assuming default)", context->index);
context->language = g_strdup ("eng");
}
if (context->language) {
const gchar *lang;
/* Matroska contains ISO 639-2B codes, we want ISO 639-1 */
lang = gst_tag_get_language_code (context->language);
gst_tag_list_add (context->tags, GST_TAG_MERGE_REPLACE,
GST_TAG_LANGUAGE_CODE, (lang) ? lang : context->language, NULL);
context->tags_changed = TRUE;
}
if (caps == NULL) {
GST_WARNING_OBJECT (demux, "could not determine caps for stream with "
"codec_id='%s'", context->codec_id);
switch (context->type) {
case GST_MATROSKA_TRACK_TYPE_VIDEO:
caps = gst_caps_new_empty_simple ("video/x-unknown");
break;
case GST_MATROSKA_TRACK_TYPE_AUDIO:
caps = gst_caps_new_empty_simple ("audio/x-unknown");
break;
case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
caps = gst_caps_new_empty_simple ("application/x-subtitle-unknown");
break;
case GST_MATROSKA_TRACK_TYPE_COMPLEX:
default:
caps = gst_caps_new_empty_simple ("application/x-matroska-unknown");
break;
}
gst_caps_set_simple (caps, "codec-id", G_TYPE_STRING, context->codec_id,
NULL);
/* add any unrecognised riff fourcc / audio format, but after codec-id */
if (context->type == GST_MATROSKA_TRACK_TYPE_AUDIO && riff_audio_fmt != 0)
gst_caps_set_simple (caps, "format", G_TYPE_INT, riff_audio_fmt, NULL);
else if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO && riff_fourcc != 0) {
gchar *fstr = g_strdup_printf ("%" GST_FOURCC_FORMAT,
GST_FOURCC_ARGS (riff_fourcc));
gst_caps_set_simple (caps, "fourcc", G_TYPE_STRING, fstr, NULL);
g_free (fstr);
}
} else if (context->stream_headers != NULL) {
gst_matroska_demux_add_stream_headers_to_caps (demux,
context->stream_headers, caps);
}
context->caps = caps;
/* tadaah! */
*dest_context = context;
return ret;
}
static void
gst_matroska_demux_add_stream (GstMatroskaDemux * demux,
GstMatroskaTrackContext * context)
{
GstElementClass *klass = GST_ELEMENT_GET_CLASS (demux);
gchar *padname = NULL;
GstPadTemplate *templ = NULL;
GstStreamFlags stream_flags;
GstEvent *stream_start;
gchar *stream_id;
g_ptr_array_add (demux->common.src, context);
context->index = demux->common.num_streams++;
g_assert (demux->common.src->len == demux->common.num_streams);
g_ptr_array_index (demux->common.src, demux->common.num_streams - 1) =
context;
/* now create the GStreamer connectivity */
switch (context->type) {
case GST_MATROSKA_TRACK_TYPE_VIDEO:
padname = g_strdup_printf ("video_%u", demux->num_v_streams++);
templ = gst_element_class_get_pad_template (klass, "video_%u");
break;
case GST_MATROSKA_TRACK_TYPE_AUDIO:
padname = g_strdup_printf ("audio_%u", demux->num_a_streams++);
templ = gst_element_class_get_pad_template (klass, "audio_%u");
break;
case GST_MATROSKA_TRACK_TYPE_SUBTITLE:
padname = g_strdup_printf ("subtitle_%u", demux->num_t_streams++);
templ = gst_element_class_get_pad_template (klass, "subtitle_%u");
break;
default:
/* we should already have quit by now */
g_assert_not_reached ();
}
/* the pad in here */
context->pad = gst_pad_new_from_template (templ, padname);
gst_pad_set_event_function (context->pad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_event));
gst_pad_set_query_function (context->pad,
GST_DEBUG_FUNCPTR (gst_matroska_demux_handle_src_query));
GST_INFO_OBJECT (demux, "Adding pad '%s' with caps %" GST_PTR_FORMAT,
padname, context->caps);
gst_pad_set_element_private (context->pad, context);
gst_pad_use_fixed_caps (context->pad);
gst_pad_set_active (context->pad, TRUE);
stream_id =
gst_pad_create_stream_id_printf (context->pad, GST_ELEMENT_CAST (demux),
"%03" G_GUINT64_FORMAT ":%03" G_GUINT64_FORMAT,
context->num, context->uid);
stream_start =
gst_pad_get_sticky_event (demux->common.sinkpad, GST_EVENT_STREAM_START,
0);
if (stream_start) {
if (gst_event_parse_group_id (stream_start, &demux->group_id))
demux->have_group_id = TRUE;
else
demux->have_group_id = FALSE;
gst_event_unref (stream_start);
} else if (!demux->have_group_id) {
demux->have_group_id = TRUE;
demux->group_id = gst_util_group_id_next ();
}
stream_start = gst_event_new_stream_start (stream_id);
g_free (stream_id);
if (demux->have_group_id)
gst_event_set_group_id (stream_start, demux->group_id);
stream_flags = GST_STREAM_FLAG_NONE;
if (context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
stream_flags |= GST_STREAM_FLAG_SPARSE;
if (context->flags & GST_MATROSKA_TRACK_DEFAULT)
stream_flags |= GST_STREAM_FLAG_SELECT;
gst_event_set_stream_flags (stream_start, stream_flags);
gst_pad_push_event (context->pad, stream_start);
gst_pad_set_caps (context->pad, context->caps);
if (demux->common.global_tags) {
GstEvent *tag_event;
gst_tag_list_add (demux->common.global_tags, GST_TAG_MERGE_REPLACE,
GST_TAG_CONTAINER_FORMAT, "Matroska", NULL);
GST_DEBUG_OBJECT (context->pad, "Sending global_tags %p: %" GST_PTR_FORMAT,
demux->common.global_tags, demux->common.global_tags);
tag_event =
gst_event_new_tag (gst_tag_list_copy (demux->common.global_tags));
gst_pad_push_event (context->pad, tag_event);
}
if (G_UNLIKELY (context->tags_changed)) {
GST_DEBUG_OBJECT (context->pad, "Sending tags %p: %"
GST_PTR_FORMAT, context->tags, context->tags);
gst_pad_push_event (context->pad,
gst_event_new_tag (gst_tag_list_copy (context->tags)));
context->tags_changed = FALSE;
}
gst_element_add_pad (GST_ELEMENT (demux), context->pad);
gst_flow_combiner_add_pad (demux->flowcombiner, context->pad);
g_free (padname);
}
static gboolean
gst_matroska_demux_query (GstMatroskaDemux * demux, GstPad * pad,
GstQuery * query)
{
gboolean res = FALSE;
GstMatroskaTrackContext *context = NULL;
if (pad) {
context = gst_pad_get_element_private (pad);
}
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_POSITION:
{
GstFormat format;
gst_query_parse_position (query, &format, NULL);
res = TRUE;
if (format == GST_FORMAT_TIME) {
GST_OBJECT_LOCK (demux);
if (context)
gst_query_set_position (query, GST_FORMAT_TIME,
MAX (context->pos, demux->stream_start_time) -
demux->stream_start_time);
else
gst_query_set_position (query, GST_FORMAT_TIME,
MAX (demux->common.segment.position, demux->stream_start_time) -
demux->stream_start_time);
GST_OBJECT_UNLOCK (demux);
} else if (format == GST_FORMAT_DEFAULT && context
&& context->default_duration) {
GST_OBJECT_LOCK (demux);
gst_query_set_position (query, GST_FORMAT_DEFAULT,
context->pos / context->default_duration);
GST_OBJECT_UNLOCK (demux);
} else {
GST_DEBUG_OBJECT (demux,
"only position query in TIME and DEFAULT format is supported");
res = FALSE;
}
break;
}
case GST_QUERY_DURATION:
{
GstFormat format;
gst_query_parse_duration (query, &format, NULL);
res = TRUE;
if (format == GST_FORMAT_TIME) {
GST_OBJECT_LOCK (demux);
gst_query_set_duration (query, GST_FORMAT_TIME,
demux->common.segment.duration);
GST_OBJECT_UNLOCK (demux);
} else if (format == GST_FORMAT_DEFAULT && context
&& context->default_duration) {
GST_OBJECT_LOCK (demux);
gst_query_set_duration (query, GST_FORMAT_DEFAULT,
demux->common.segment.duration / context->default_duration);
GST_OBJECT_UNLOCK (demux);
} else {
GST_DEBUG_OBJECT (demux,
"only duration query in TIME and DEFAULT format is supported");
res = FALSE;
}
break;
}
case GST_QUERY_SEEKING:
{
GstFormat fmt;
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
GST_OBJECT_LOCK (demux);
if (fmt == GST_FORMAT_TIME) {
gboolean seekable;
if (demux->streaming) {
/* assuming we'll be able to get an index ... */
seekable = demux->seekable;
} else {
seekable = TRUE;
}
gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
0, demux->common.segment.duration);
res = TRUE;
}
GST_OBJECT_UNLOCK (demux);
break;
}
case GST_QUERY_SEGMENT:
{
GstFormat format;
gint64 start, stop;
format = demux->common.segment.format;
start =
gst_segment_to_stream_time (&demux->common.segment, format,
demux->common.segment.start);
if ((stop = demux->common.segment.stop) == -1)
stop = demux->common.segment.duration;
else
stop =
gst_segment_to_stream_time (&demux->common.segment, format, stop);
gst_query_set_segment (query, demux->common.segment.rate, format, start,
stop);
res = TRUE;
break;
}
default:
if (pad)
res = gst_pad_query_default (pad, (GstObject *) demux, query);
else
res =
GST_ELEMENT_CLASS (parent_class)->query (GST_ELEMENT_CAST (demux),
query);
break;
}
return res;
}
static gboolean
gst_matroska_demux_element_query (GstElement * element, GstQuery * query)
{
return gst_matroska_demux_query (GST_MATROSKA_DEMUX (element), NULL, query);
}
static gboolean
gst_matroska_demux_handle_src_query (GstPad * pad, GstObject * parent,
GstQuery * query)
{
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
return gst_matroska_demux_query (demux, pad, query);
}
/* returns FALSE if there are no pads to deliver event to,
* otherwise TRUE (whatever the outcome of event sending),
* takes ownership of the passed event! */
static gboolean
gst_matroska_demux_send_event (GstMatroskaDemux * demux, GstEvent * event)
{
gboolean ret = FALSE;
gint i;
g_return_val_if_fail (event != NULL, FALSE);
GST_DEBUG_OBJECT (demux, "Sending event of type %s to all source pads",
GST_EVENT_TYPE_NAME (event));
g_assert (demux->common.src->len == demux->common.num_streams);
for (i = 0; i < demux->common.src->len; i++) {
GstMatroskaTrackContext *stream;
stream = g_ptr_array_index (demux->common.src, i);
gst_event_ref (event);
gst_pad_push_event (stream->pad, event);
ret = TRUE;
}
gst_event_unref (event);
return ret;
}
static void
gst_matroska_demux_send_tags (GstMatroskaDemux * demux)
{
gint i;
if (G_UNLIKELY (demux->common.global_tags_changed)) {
GstEvent *tag_event;
gst_tag_list_add (demux->common.global_tags, GST_TAG_MERGE_REPLACE,
GST_TAG_CONTAINER_FORMAT, "Matroska", NULL);
GST_DEBUG_OBJECT (demux, "Sending global_tags %p : %" GST_PTR_FORMAT,
demux->common.global_tags, demux->common.global_tags);
tag_event =
gst_event_new_tag (gst_tag_list_copy (demux->common.global_tags));
for (i = 0; i < demux->common.src->len; i++) {
GstMatroskaTrackContext *stream;
stream = g_ptr_array_index (demux->common.src, i);
gst_pad_push_event (stream->pad, gst_event_ref (tag_event));
}
gst_event_unref (tag_event);
demux->common.global_tags_changed = FALSE;
}
g_assert (demux->common.src->len == demux->common.num_streams);
for (i = 0; i < demux->common.src->len; i++) {
GstMatroskaTrackContext *stream;
stream = g_ptr_array_index (demux->common.src, i);
if (G_UNLIKELY (stream->tags_changed)) {
GST_DEBUG_OBJECT (demux, "Sending tags %p for pad %s:%s : %"
GST_PTR_FORMAT, stream->tags,
GST_DEBUG_PAD_NAME (stream->pad), stream->tags);
gst_pad_push_event (stream->pad,
gst_event_new_tag (gst_tag_list_copy (stream->tags)));
stream->tags_changed = FALSE;
}
}
}
static gboolean
gst_matroska_demux_element_send_event (GstElement * element, GstEvent * event)
{
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (element);
gboolean res;
g_return_val_if_fail (event != NULL, FALSE);
if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
/* no seeking until we are (safely) ready */
if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
GST_DEBUG_OBJECT (demux, "not ready for seeking yet");
gst_event_unref (event);
return FALSE;
}
res = gst_matroska_demux_handle_seek_event (demux, NULL, event);
} else {
GST_WARNING_OBJECT (demux, "Unhandled event of type %s",
GST_EVENT_TYPE_NAME (event));
res = FALSE;
}
gst_event_unref (event);
return res;
}
static gboolean
gst_matroska_demux_move_to_entry (GstMatroskaDemux * demux,
GstMatroskaIndex * entry, gboolean reset, gboolean update)
{
gint i;
GST_OBJECT_LOCK (demux);
if (update) {
/* seek (relative to matroska segment) */
/* position might be invalid; will error when streaming resumes ... */
demux->common.offset = entry->pos + demux->common.ebml_segment_start;
demux->next_cluster_offset = 0;
GST_DEBUG_OBJECT (demux,
"Seeked to offset %" G_GUINT64_FORMAT ", block %d, " "time %"
GST_TIME_FORMAT, entry->pos + demux->common.ebml_segment_start,
entry->block, GST_TIME_ARGS (entry->time));
/* update the time */
gst_matroska_read_common_reset_streams (&demux->common, entry->time, TRUE);
gst_flow_combiner_reset (demux->flowcombiner);
demux->common.segment.position = entry->time;
demux->seek_block = entry->block;
demux->seek_first = TRUE;
demux->last_stop_end = GST_CLOCK_TIME_NONE;
}
for (i = 0; i < demux->common.src->len; i++) {
GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
if (reset) {
stream->to_offset = G_MAXINT64;
} else {
if (stream->from_offset != -1)
stream->to_offset = stream->from_offset;
}
stream->from_offset = -1;
stream->from_time = GST_CLOCK_TIME_NONE;
}
GST_OBJECT_UNLOCK (demux);
return TRUE;
}
static gint
gst_matroska_cluster_compare (gint64 * i1, gint64 * i2)
{
if (*i1 < *i2)
return -1;
else if (*i1 > *i2)
return 1;
else
return 0;
}
/* searches for a cluster start from @pos,
* return GST_FLOW_OK and cluster position in @pos if found */
static GstFlowReturn
gst_matroska_demux_search_cluster (GstMatroskaDemux * demux, gint64 * pos,
gboolean forward)
{
gint64 newpos = *pos;
gint64 orig_offset;
GstFlowReturn ret = GST_FLOW_OK;
const guint chunk = 128 * 1024;
GstBuffer *buf = NULL;
GstMapInfo map;
gpointer data = NULL;
gsize size;
guint64 length;
guint32 id;
guint needed;
gint64 oldpos, oldlength;
orig_offset = demux->common.offset;
GST_LOG_OBJECT (demux, "searching cluster %s offset %" G_GINT64_FORMAT,
forward ? "following" : "preceding", *pos);
if (demux->clusters) {
gint64 *cpos;
cpos = gst_util_array_binary_search (demux->clusters->data,
demux->clusters->len, sizeof (gint64),
(GCompareDataFunc) gst_matroska_cluster_compare,
forward ? GST_SEARCH_MODE_AFTER : GST_SEARCH_MODE_BEFORE, pos, NULL);
/* sanity check */
if (cpos) {
GST_DEBUG_OBJECT (demux,
"cluster reported at offset %" G_GINT64_FORMAT, *cpos);
demux->common.offset = *cpos;
ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
GST_ELEMENT_CAST (demux), &id, &length, &needed);
if (ret == GST_FLOW_OK && id == GST_MATROSKA_ID_CLUSTER) {
newpos = *cpos;
goto exit;
}
}
}
/* read in at newpos and scan for ebml cluster id */
oldpos = oldlength = -1;
while (1) {
GstByteReader reader;
gint cluster_pos;
guint toread = chunk;
if (!forward) {
/* never read beyond the requested target */
if (G_UNLIKELY (newpos < chunk)) {
toread = newpos;
newpos = 0;
} else {
newpos -= chunk;
}
}
if (buf != NULL) {
gst_buffer_unmap (buf, &map);
gst_buffer_unref (buf);
buf = NULL;
}
ret = gst_pad_pull_range (demux->common.sinkpad, newpos, toread, &buf);
if (ret != GST_FLOW_OK)
break;
GST_DEBUG_OBJECT (demux,
"read buffer size %" G_GSIZE_FORMAT " at offset %" G_GINT64_FORMAT,
gst_buffer_get_size (buf), newpos);
gst_buffer_map (buf, &map, GST_MAP_READ);
data = map.data;
size = map.size;
if (oldpos == newpos && oldlength == map.size) {
GST_ERROR_OBJECT (demux, "Stuck at same position");
ret = GST_FLOW_ERROR;
goto exit;
} else {
oldpos = newpos;
oldlength = map.size;
}
gst_byte_reader_init (&reader, data, size);
cluster_pos = -1;
while (1) {
gint found = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
GST_MATROSKA_ID_CLUSTER, 0, gst_byte_reader_get_remaining (&reader));
if (forward) {
cluster_pos = found;
break;
}
/* need last occurrence when searching backwards */
if (found >= 0) {
cluster_pos = gst_byte_reader_get_pos (&reader) + found;
gst_byte_reader_skip (&reader, found + 4);
} else {
break;
}
}
if (cluster_pos >= 0) {
newpos += cluster_pos;
GST_DEBUG_OBJECT (demux,
"found cluster ebml id at offset %" G_GINT64_FORMAT, newpos);
/* extra checks whether we really sync'ed to a cluster:
* - either it is the first and only cluster
* - either there is a cluster after this one
* - either cluster length is undefined
*/
/* ok if first cluster (there may not a subsequent one) */
if (newpos == demux->first_cluster_offset) {
GST_DEBUG_OBJECT (demux, "cluster is first cluster -> OK");
break;
}
demux->common.offset = newpos;
ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
GST_ELEMENT_CAST (demux), &id, &length, &needed);
if (ret != GST_FLOW_OK) {
GST_DEBUG_OBJECT (demux, "need more data -> continue");
goto next;
}
g_assert (id == GST_MATROSKA_ID_CLUSTER);
GST_DEBUG_OBJECT (demux, "cluster size %" G_GUINT64_FORMAT ", prefix %d",
length, needed);
/* ok if undefined length or first cluster */
if (length == GST_EBML_SIZE_UNKNOWN || length == G_MAXUINT64) {
GST_DEBUG_OBJECT (demux, "cluster has undefined length -> OK");
break;
}
/* skip cluster */
demux->common.offset += length + needed;
ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
GST_ELEMENT_CAST (demux), &id, &length, &needed);
if (ret != GST_FLOW_OK)
goto next;
GST_DEBUG_OBJECT (demux, "next element is %scluster",
id == GST_MATROSKA_ID_CLUSTER ? "" : "not ");
if (id == GST_MATROSKA_ID_CLUSTER)
break;
next:
if (forward)
newpos += 1;
} else {
/* partial cluster id may have been in tail of buffer */
newpos +=
forward ? MAX (gst_byte_reader_get_remaining (&reader), 4) - 3 : 3;
}
}
if (buf) {
gst_buffer_unmap (buf, &map);
gst_buffer_unref (buf);
buf = NULL;
}
exit:
demux->common.offset = orig_offset;
*pos = newpos;
return ret;
}
/* bisect and scan through file for cluster starting before @time,
* returns fake index entry with corresponding info on cluster */
static GstMatroskaIndex *
gst_matroska_demux_search_pos (GstMatroskaDemux * demux, GstClockTime time)
{
GstMatroskaIndex *entry = NULL;
GstMatroskaReadState current_state;
GstClockTime otime, prev_cluster_time, current_cluster_time, cluster_time;
GstClockTime atime;
gint64 opos, newpos, current_offset;
gint64 prev_cluster_offset = -1, current_cluster_offset, cluster_offset;
gint64 apos, maxpos;
guint64 cluster_size = 0;
GstFlowReturn ret;
guint64 length;
guint32 id;
guint needed;
/* estimate new position, resync using cluster ebml id,
* and bisect further or scan forward to appropriate cluster */
/* store some current state */
current_state = demux->common.state;
g_return_val_if_fail (current_state == GST_MATROSKA_READ_STATE_DATA, NULL);
current_cluster_offset = demux->cluster_offset;
current_cluster_time = demux->cluster_time;
current_offset = demux->common.offset;
demux->common.state = GST_MATROSKA_READ_STATE_SCANNING;
/* estimate using start and last known cluster */
GST_OBJECT_LOCK (demux);
apos = demux->first_cluster_offset;
atime = demux->stream_start_time;
opos = demux->last_cluster_offset;
otime = demux->stream_last_time;
GST_OBJECT_UNLOCK (demux);
/* sanitize */
time = MAX (time, atime);
otime = MAX (otime, atime);
opos = MAX (opos, apos);
maxpos = gst_matroska_read_common_get_length (&demux->common);
/* invariants;
* apos <= opos
* atime <= otime
* apos always refer to a cluster before target time;
* opos may or may not be after target time, but if it is once so,
* then also in next iteration
* */
retry:
GST_LOG_OBJECT (demux,
"apos: %" G_GUINT64_FORMAT ", atime: %" GST_TIME_FORMAT ", %"
GST_TIME_FORMAT " in stream time, "
"opos: %" G_GUINT64_FORMAT ", otime: %" GST_TIME_FORMAT ", %"
GST_TIME_FORMAT " in stream time (start %" GST_TIME_FORMAT "), time %"
GST_TIME_FORMAT, apos, GST_TIME_ARGS (atime),
GST_TIME_ARGS (atime - demux->stream_start_time), opos,
GST_TIME_ARGS (otime), GST_TIME_ARGS (otime - demux->stream_start_time),
GST_TIME_ARGS (demux->stream_start_time), GST_TIME_ARGS (time));
g_assert (atime <= otime);
g_assert (apos <= opos);
if (time == GST_CLOCK_TIME_NONE) {
GST_DEBUG_OBJECT (demux, "searching last cluster");
newpos = maxpos;
if (newpos == -1) {
GST_DEBUG_OBJECT (demux, "unknown file size; bailing out");
goto exit;
}
} else if (otime <= atime) {
newpos = apos;
} else {
newpos = apos +
gst_util_uint64_scale (opos - apos, time - atime, otime - atime);
if (maxpos != -1 && newpos > maxpos)
newpos = maxpos;
}
GST_DEBUG_OBJECT (demux,
"estimated offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
GST_TIME_ARGS (time), newpos);
/* search backwards */
if (newpos > apos) {
ret = gst_matroska_demux_search_cluster (demux, &newpos, FALSE);
if (ret != GST_FLOW_OK)
goto exit;
}
/* then start scanning and parsing for cluster time,
* re-estimate if possible, otherwise next cluster and so on */
/* note that each re-estimate is entered with a change in apos or opos,
* avoiding infinite loop */
demux->common.offset = newpos;
demux->cluster_time = cluster_time = GST_CLOCK_TIME_NONE;
cluster_size = 0;
prev_cluster_time = GST_CLOCK_TIME_NONE;
while (1) {
/* peek and parse some elements */
ret = gst_matroska_read_common_peek_id_length_pull (&demux->common,
GST_ELEMENT_CAST (demux), &id, &length, &needed);
if (ret != GST_FLOW_OK)
goto error;
GST_LOG_OBJECT (demux, "Offset %" G_GUINT64_FORMAT ", Element id 0x%x, "
"size %" G_GUINT64_FORMAT ", needed %d", demux->common.offset, id,
length, needed);
ret = gst_matroska_demux_parse_id (demux, id, length, needed);
if (ret != GST_FLOW_OK)
goto error;
if (id == GST_MATROSKA_ID_CLUSTER) {
cluster_time = GST_CLOCK_TIME_NONE;
if (length == G_MAXUINT64)
cluster_size = 0;
else
cluster_size = length + needed;
}
if (demux->cluster_time != GST_CLOCK_TIME_NONE &&
cluster_time == GST_CLOCK_TIME_NONE) {
cluster_time = demux->cluster_time * demux->common.time_scale;
cluster_offset = demux->cluster_offset;
GST_DEBUG_OBJECT (demux, "found cluster at offset %" G_GINT64_FORMAT
" with time %" GST_TIME_FORMAT, cluster_offset,
GST_TIME_ARGS (cluster_time));
if (time == GST_CLOCK_TIME_NONE) {
GST_DEBUG_OBJECT (demux, "found last cluster");
prev_cluster_time = cluster_time;
prev_cluster_offset = cluster_offset;
break;
}
if (cluster_time > time) {
GST_DEBUG_OBJECT (demux, "overshot target");
/* cluster overshoots */
if (cluster_offset == demux->first_cluster_offset) {
/* but no prev one */
GST_DEBUG_OBJECT (demux, "but using first cluster anyway");
prev_cluster_time = cluster_time;
prev_cluster_offset = cluster_offset;
break;
}
if (prev_cluster_time != GST_CLOCK_TIME_NONE) {
/* prev cluster did not overshoot, so prev cluster is target */
break;
} else {
/* re-estimate using this new position info */
opos = cluster_offset;
otime = cluster_time;
goto retry;
}
} else {
/* cluster undershoots */
GST_DEBUG_OBJECT (demux, "undershot target");
/* ok if close enough */
if (GST_CLOCK_DIFF (cluster_time, time) < 5 * GST_SECOND) {
GST_DEBUG_OBJECT (demux, "target close enough");
prev_cluster_time = cluster_time;
prev_cluster_offset = cluster_offset;
break;
}
if (otime > time) {
/* we are in between atime and otime => can bisect if worthwhile */
if (prev_cluster_time != GST_CLOCK_TIME_NONE &&
cluster_time > prev_cluster_time &&
(GST_CLOCK_DIFF (prev_cluster_time, cluster_time) * 10 <
GST_CLOCK_DIFF (cluster_time, time))) {
/* we moved at least one cluster forward,
* and it looks like target is still far away,
* let's estimate again */
GST_DEBUG_OBJECT (demux, "bisecting with new apos");
apos = cluster_offset;
atime = cluster_time;
goto retry;
}
}
/* cluster undershoots, goto next one */
prev_cluster_time = cluster_time;
prev_cluster_offset = cluster_offset;
/* skip cluster if length is defined,
* otherwise will be skippingly parsed into */
if (cluster_size) {
GST_DEBUG_OBJECT (demux, "skipping to next cluster");
demux->common.offset = cluster_offset + cluster_size;
demux->cluster_time = GST_CLOCK_TIME_NONE;
} else {
GST_DEBUG_OBJECT (demux, "parsing/skipping cluster elements");
}
}
}
continue;
error:
if (ret == GST_FLOW_EOS) {
if (prev_cluster_time != GST_CLOCK_TIME_NONE)
break;
}
goto exit;
}
entry = g_new0 (GstMatroskaIndex, 1);
entry->time = prev_cluster_time;
entry->pos = prev_cluster_offset - demux->common.ebml_segment_start;
GST_DEBUG_OBJECT (demux, "simulated index entry; time %" GST_TIME_FORMAT
", pos %" G_GUINT64_FORMAT, GST_TIME_ARGS (entry->time), entry->pos);
exit:
/* restore some state */
demux->cluster_offset = current_cluster_offset;
demux->cluster_time = current_cluster_time;
demux->common.offset = current_offset;
demux->common.state = current_state;
return entry;
}
static gboolean
gst_matroska_demux_handle_seek_event (GstMatroskaDemux * demux,
GstPad * pad, GstEvent * event)
{
GstMatroskaIndex *entry = NULL;
GstMatroskaIndex scan_entry;
GstSeekFlags flags;
GstSeekType cur_type, stop_type;
GstFormat format;
gboolean flush, keyunit, before, after, snap_next;
gdouble rate;
gint64 cur, stop;
GstMatroskaTrackContext *track = NULL;
GstSegment seeksegment = { 0, };
gboolean update = TRUE;
gboolean pad_locked = FALSE;
guint32 seqnum;
GstSearchMode snap_dir;
g_return_val_if_fail (event != NULL, FALSE);
if (pad)
track = gst_pad_get_element_private (pad);
GST_DEBUG_OBJECT (demux, "Have seek %" GST_PTR_FORMAT, event);
gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
&stop_type, &stop);
seqnum = gst_event_get_seqnum (event);
/* we can only seek on time */
if (format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
return FALSE;
}
/* copy segment, we need this because we still need the old
* segment when we close the current segment. */
memcpy (&seeksegment, &demux->common.segment, sizeof (GstSegment));
/* pull mode without index means that the actual duration is not known,
* we might be playing a file that's still being recorded
* so, invalidate our current duration, which is only a moving target,
* and should not be used to clamp anything */
if (!demux->streaming && !demux->common.index && demux->invalid_duration) {
seeksegment.duration = GST_CLOCK_TIME_NONE;
}
GST_DEBUG_OBJECT (demux, "configuring seek");
/* Subtract stream_start_time so we always seek on a segment
* in stream time */
if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
seeksegment.start -= demux->stream_start_time;
seeksegment.position -= demux->stream_start_time;
if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
seeksegment.stop -= demux->stream_start_time;
else
seeksegment.stop = seeksegment.duration;
}
gst_segment_do_seek (&seeksegment, rate, format, flags,
cur_type, cur, stop_type, stop, &update);
/* Restore the clip timestamp offset */
if (GST_CLOCK_TIME_IS_VALID (demux->stream_start_time)) {
seeksegment.position += demux->stream_start_time;
seeksegment.start += demux->stream_start_time;
if (!GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
seeksegment.stop = seeksegment.duration;
if (GST_CLOCK_TIME_IS_VALID (seeksegment.stop))
seeksegment.stop += demux->stream_start_time;
}
/* restore segment duration (if any effect),
* would be determined again when parsing, but anyway ... */
seeksegment.duration = demux->common.segment.duration;
flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
keyunit = ! !(flags & GST_SEEK_FLAG_KEY_UNIT);
after = ! !(flags & GST_SEEK_FLAG_SNAP_AFTER);
before = ! !(flags & GST_SEEK_FLAG_SNAP_BEFORE);
/* always do full update if flushing,
* otherwise problems might arise downstream with missing keyframes etc */
update = update || flush;
GST_DEBUG_OBJECT (demux, "New segment %" GST_SEGMENT_FORMAT, &seeksegment);
/* check sanity before we start flushing and all that */
snap_next = after && !before;
if (seeksegment.rate < 0)
snap_dir = snap_next ? GST_SEARCH_MODE_BEFORE : GST_SEARCH_MODE_AFTER;
else
snap_dir = snap_next ? GST_SEARCH_MODE_AFTER : GST_SEARCH_MODE_BEFORE;
GST_OBJECT_LOCK (demux);
track = gst_matroska_read_common_get_seek_track (&demux->common, track);
if ((entry = gst_matroska_read_common_do_index_seek (&demux->common, track,
seeksegment.position, &demux->seek_index, &demux->seek_entry,
snap_dir)) == NULL) {
/* pull mode without index can scan later on */
if (demux->streaming) {
GST_DEBUG_OBJECT (demux, "No matching seek entry in index");
GST_OBJECT_UNLOCK (demux);
return FALSE;
} else if (rate < 0.0) {
/* FIXME: We should build an index during playback or when scanning
* that can be used here. The reverse playback code requires seek_index
* and seek_entry to be set!
*/
GST_DEBUG_OBJECT (demux,
"No matching seek entry in index, needed for reverse playback");
GST_OBJECT_UNLOCK (demux);
return FALSE;
}
}
GST_DEBUG_OBJECT (demux, "Seek position looks sane");
GST_OBJECT_UNLOCK (demux);
if (!update) {
/* only have to update some segment,
* but also still have to honour flush and so on */
GST_DEBUG_OBJECT (demux, "... no update");
/* bad goto, bad ... */
goto next;
}
if (demux->streaming)
goto finish;
next:
if (flush) {
GstEvent *flush_event = gst_event_new_flush_start ();
gst_event_set_seqnum (flush_event, seqnum);
GST_DEBUG_OBJECT (demux, "Starting flush");
gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
gst_matroska_demux_send_event (demux, flush_event);
} else {
GST_DEBUG_OBJECT (demux, "Non-flushing seek, pausing task");
gst_pad_pause_task (demux->common.sinkpad);
}
/* ouch */
if (!update) {
GST_PAD_STREAM_LOCK (demux->common.sinkpad);
pad_locked = TRUE;
goto exit;
}
/* now grab the stream lock so that streaming cannot continue, for
* non flushing seeks when the element is in PAUSED this could block
* forever. */
GST_DEBUG_OBJECT (demux, "Waiting for streaming to stop");
GST_PAD_STREAM_LOCK (demux->common.sinkpad);
pad_locked = TRUE;
/* pull mode without index can do some scanning */
if (!demux->streaming && !entry) {
GstEvent *flush_event;
/* need to stop flushing upstream as we need it next */
if (flush) {
flush_event = gst_event_new_flush_stop (TRUE);
gst_event_set_seqnum (flush_event, seqnum);
gst_pad_push_event (demux->common.sinkpad, flush_event);
}
entry = gst_matroska_demux_search_pos (demux, seeksegment.position);
/* keep local copy */
if (entry) {
scan_entry = *entry;
g_free (entry);
entry = &scan_entry;
} else {
GST_DEBUG_OBJECT (demux, "Scan failed to find matching position");
if (flush) {
flush_event = gst_event_new_flush_stop (TRUE);
gst_event_set_seqnum (flush_event, seqnum);
gst_matroska_demux_send_event (demux, flush_event);
}
goto seek_error;
}
}
finish:
if (keyunit && seeksegment.rate > 0) {
GST_DEBUG_OBJECT (demux, "seek to key unit, adjusting segment start from %"
GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (entry->time));
seeksegment.start = MAX (entry->time, demux->stream_start_time);
seeksegment.position = seeksegment.start;
seeksegment.time = seeksegment.start - demux->stream_start_time;
} else if (keyunit) {
GST_DEBUG_OBJECT (demux, "seek to key unit, adjusting segment stop from %"
GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
GST_TIME_ARGS (seeksegment.stop), GST_TIME_ARGS (entry->time));
seeksegment.stop = MAX (entry->time, demux->stream_start_time);
seeksegment.position = seeksegment.stop;
}
if (demux->streaming) {
GST_OBJECT_LOCK (demux);
/* track real position we should start at */
GST_DEBUG_OBJECT (demux, "storing segment start");
demux->requested_seek_time = seeksegment.position;
demux->seek_offset = entry->pos + demux->common.ebml_segment_start;
GST_OBJECT_UNLOCK (demux);
/* need to seek to cluster start to pick up cluster time */
/* upstream takes care of flushing and all that
* ... and newsegment event handling takes care of the rest */
return perform_seek_to_offset (demux, rate,
entry->pos + demux->common.ebml_segment_start, seqnum, flags);
}
exit:
if (flush) {
GstEvent *flush_event = gst_event_new_flush_stop (TRUE);
gst_event_set_seqnum (flush_event, seqnum);
GST_DEBUG_OBJECT (demux, "Stopping flush");
gst_pad_push_event (demux->common.sinkpad, gst_event_ref (flush_event));
gst_matroska_demux_send_event (demux, flush_event);
}
GST_OBJECT_LOCK (demux);
/* now update the real segment info */
GST_DEBUG_OBJECT (demux, "Committing new seek segment");
memcpy (&demux->common.segment, &seeksegment, sizeof (GstSegment));
GST_OBJECT_UNLOCK (demux);
/* update some (segment) state */
if (!gst_matroska_demux_move_to_entry (demux, entry, TRUE, update))
goto seek_error;
/* notify start of new segment */
if (demux->common.segment.flags & GST_SEEK_FLAG_SEGMENT) {
GstMessage *msg;
msg = gst_message_new_segment_start (GST_OBJECT (demux),
GST_FORMAT_TIME, demux->common.segment.start);
gst_message_set_seqnum (msg, seqnum);
gst_element_post_message (GST_ELEMENT (demux), msg);
}
GST_OBJECT_LOCK (demux);
if (demux->new_segment)
gst_event_unref (demux->new_segment);
/* On port from 0.10, discarded !update (for segment.update) here, FIXME? */
demux->new_segment = gst_event_new_segment (&demux->common.segment);
gst_event_set_seqnum (demux->new_segment, seqnum);
if (demux->common.segment.rate < 0 && demux->common.segment.stop == -1)
demux->to_time = demux->common.segment.position;
else
demux->to_time = GST_CLOCK_TIME_NONE;
demux->segment_seqnum = seqnum;
GST_OBJECT_UNLOCK (demux);
/* restart our task since it might have been stopped when we did the
* flush. */
gst_pad_start_task (demux->common.sinkpad,
(GstTaskFunction) gst_matroska_demux_loop, demux->common.sinkpad, NULL);
/* streaming can continue now */
if (pad_locked) {
GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
}
return TRUE;
seek_error:
{
if (pad_locked) {
GST_PAD_STREAM_UNLOCK (demux->common.sinkpad);
}
GST_ELEMENT_ERROR (demux, STREAM, DEMUX, (NULL), ("Got a seek error"));
return FALSE;
}
}
/*
* Handle whether we can perform the seek event or if we have to let the chain
* function handle seeks to build the seek indexes first.
*/
static gboolean
gst_matroska_demux_handle_seek_push (GstMatroskaDemux * demux, GstPad * pad,
GstEvent * event)
{
GstSeekFlags flags;
GstSeekType cur_type, stop_type;
GstFormat format;
gdouble rate;
gint64 cur, stop;
gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
&stop_type, &stop);
/* sanity checks */
/* we can only seek on time */
if (format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (demux, "Can only seek on TIME");
return FALSE;
}
if (stop_type != GST_SEEK_TYPE_NONE && stop != GST_CLOCK_TIME_NONE) {
GST_DEBUG_OBJECT (demux, "Seek end-time not supported in streaming mode");
return FALSE;
}
if (!(flags & GST_SEEK_FLAG_FLUSH)) {
GST_DEBUG_OBJECT (demux,
"Non-flushing seek not supported in streaming mode");
return FALSE;
}
if (flags & GST_SEEK_FLAG_SEGMENT) {
GST_DEBUG_OBJECT (demux, "Segment seek not supported in streaming mode");
return FALSE;
}
/* check for having parsed index already */
if (!demux->common.index_parsed) {
gboolean building_index;
guint64 offset = 0;
if (!demux->index_offset) {
GST_DEBUG_OBJECT (demux, "no index (location); no seek in push mode");
return FALSE;
}
GST_OBJECT_LOCK (demux);
/* handle the seek event in the chain function */
demux->common.state = GST_MATROSKA_READ_STATE_SEEK;
/* no more seek can be issued until state reset to _DATA */
/* copy the event */
if (demux->seek_event)
gst_event_unref (demux->seek_event);
demux->seek_event = gst_event_ref (event);
/* set the building_index flag so that only one thread can setup the
* structures for index seeking. */
building_index = demux->building_index;
if (!building_index) {
demux->building_index = TRUE;
offset = demux->index_offset;
}
GST_OBJECT_UNLOCK (demux);
if (!building_index) {
/* seek to the first subindex or legacy index */
GST_INFO_OBJECT (demux, "Seeking to Cues at %" G_GUINT64_FORMAT, offset);
return perform_seek_to_offset (demux, rate, offset,
gst_event_get_seqnum (event), GST_SEEK_FLAG_NONE);
}
/* well, we are handling it already */
return TRUE;
}
/* delegate to tweaked regular seek */
return gst_matroska_demux_handle_seek_event (demux, pad, event);
}
static gboolean
gst_matroska_demux_handle_src_event (GstPad * pad, GstObject * parent,
GstEvent * event)
{
GstMatroskaDemux *demux = GST_MATROSKA_DEMUX (parent);
gboolean res = TRUE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:
/* no seeking until we are (safely) ready */
if (demux->common.state != GST_MATROSKA_READ_STATE_DATA) {
GST_DEBUG_OBJECT (demux, "not ready for seeking yet");
gst_event_unref (event);
return FALSE;
}
{
guint32 seqnum = gst_event_get_seqnum (event);
if (seqnum == demux->segment_seqnum) {
GST_LOG_OBJECT (pad,
"Drop duplicated SEEK event seqnum %" G_GUINT32_FORMAT, seqnum);
gst_event_unref (event);
return TRUE;
}
}
if (!demux->streaming)
res = gst_matroska_demux_handle_seek_event (demux, pad, event);
else
res = gst_matroska_demux_handle_seek_push (demux, pad, event);
gst_event_unref (event);
break;
case GST_EVENT_QOS:
{
GstMatroskaTrackContext *context = gst_pad_get_element_private (pad);
if (context->type == GST_MATROSKA_TRACK_TYPE_VIDEO) {
GstMatroskaTrackVideoContext *videocontext =
(GstMatroskaTrackVideoContext *) context;
gdouble proportion;
GstClockTimeDiff diff;
GstClockTime timestamp;
gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
GST_OBJECT_LOCK (demux);
videocontext->earliest_time = timestamp + diff;
GST_OBJECT_UNLOCK (demux);
}
res = TRUE;
gst_event_unref (event);
break;
}
case GST_EVENT_TOC_SELECT:
{
char *uid = NULL;
GstTocEntry *entry = NULL;
GstEvent *seek_event;
gint64 start_pos;
if (!demux->common.toc) {
GST_DEBUG_OBJECT (demux, "no TOC to select");
return FALSE;
} else {
gst_event_parse_toc_select (event, &uid);
if (uid != NULL) {
GST_OBJECT_LOCK (demux);
entry = gst_toc_find_entry (demux->common.toc, uid);
if (entry == NULL) {
GST_OBJECT_UNLOCK (demux);
GST_WARNING_OBJECT (demux, "no TOC entry with given UID: %s", uid);
res = FALSE;
} else {
gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
GST_OBJECT_UNLOCK (demux);
seek_event = gst_event_new_seek (1.0,
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
res = gst_matroska_demux_handle_seek_event (demux, pad, seek_event);
gst_event_unref (seek_event);
}
g_free (uid);
} else {
GST_WARNING_OBJECT (demux, "received empty TOC select event");
res = FALSE;
}
}
gst_event_unref (event);
break;
}
/* events we don't need to handle */
case GST_EVENT_NAVIGATION:
gst_event_unref (event);
res = FALSE;
break;
case GST_EVENT_LATENCY:
default:
res = gst_pad_push_event (demux->common.sinkpad, event);
break;
}
return res;
}
static GstFlowReturn
gst_matroska_demux_seek_to_previous_keyframe (GstMatroskaDemux * demux)
{
GstFlowReturn ret = GST_FLOW_EOS;
gboolean done = TRUE;
gint i;
g_return_val_if_fail (demux->seek_index, GST_FLOW_EOS);
g_return_val_if_fail (demux->seek_entry < demux->seek_index->len,
GST_FLOW_EOS);
GST_DEBUG_OBJECT (demux, "locating previous keyframe");
if (!demux->seek_entry) {
GST_DEBUG_OBJECT (demux, "no earlier index entry");
goto exit;
}
for (i = 0; i < demux->common.src->len; i++) {
GstMatroskaTrackContext *stream = g_ptr_array_index (demux->common.src, i);
GST_DEBUG_OBJECT (demux, "segment start %" GST_TIME_FORMAT
", stream %d at %" GST_TIME_FORMAT,
GST_TIME_ARGS (demux->common.segment.start), stream->index,
GST_TIME_ARGS (stream->from_time));
if (GST_CLOCK_TIME_IS_VALID (stream->from_time)) {
if (stream->from_time > demux->common.segment.start) {
GST_DEBUG_OBJECT (demux, "stream %d not finished yet", stream->index);
done = FALSE;
}
} else {
/* nothing pushed for this stream;
* likely seek entry did not start at keyframe, so all was skipped.
* So we need an earlier entry */
done = FALSE;
}
}
if (!done) {
GstMatroskaIndex *entry;
entry = &g_array_index (demux->seek_index, GstMatroskaIndex,
--demux->seek_entry);
if (!gst_matroska_demux_move_to_entry (demux, entry, FALSE, TRUE))
goto exit;
ret = GST_FLOW_OK;
}
exit:
return ret;
}
static GstFlowReturn
gst_matroska_demux_parse_tracks (GstMatroskaDemux * demux, GstEbmlRead * ebml)
{
GstFlowReturn ret = GST_FLOW_OK;
guint32 id;
DEBUG_ELEMENT_START (demux, ebml, "Tracks");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
return ret;
}
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* one track within the "all-tracks" header */
case GST_MATROSKA_ID_TRACKENTRY:{
GstMatroskaTrackContext *track;
ret = gst_matroska_demux_parse_stream (demux, ebml, &track);
if (track != NULL) {
if (gst_matroska_read_common_tracknumber_unique (&demux->common,
track->num)) {
gst_matroska_demux_add_stream (demux, track);
} else {
GST_ERROR_OBJECT (demux,
"TrackNumber %" G_GUINT64_FORMAT " is not unique", track->num);
ret = GST_FLOW_ERROR;
gst_matroska_track_free (track);
track = NULL;
}
}
break;
}
default:
ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
"Track", id);
break;
}
}
DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
demux->tracks_parsed = TRUE;
GST_DEBUG_OBJECT (demux, "signaling no more pads");
gst_element_no_more_pads (GST_ELEMENT (demux));
return ret;
}
static GstFlowReturn
gst_matroska_demux_update_tracks (GstMatroskaDemux * demux, GstEbmlRead * ebml)
{
GstFlowReturn ret = GST_FLOW_OK;
guint num_tracks_found = 0;
guint32 id;
GST_INFO_OBJECT (demux, "Reparsing Tracks element");
DEBUG_ELEMENT_START (demux, ebml, "Tracks");
if ((ret = gst_ebml_read_master (ebml, &id)) != GST_FLOW_OK) {
DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
return ret;
}
while (ret == GST_FLOW_OK && gst_ebml_read_has_remaining (ebml, 1, TRUE)) {
if ((ret = gst_ebml_peek_id (ebml, &id)) != GST_FLOW_OK)
break;
switch (id) {
/* one track within the "all-tracks" header */
case GST_MATROSKA_ID_TRACKENTRY:{
GstMatroskaTrackContext *new_track;
gint old_track_index;
GstMatroskaTrackContext *old_track;
ret = gst_matroska_demux_parse_stream (demux, ebml, &new_track);
if (new_track == NULL)
break;
num_tracks_found++;
if (gst_matroska_read_common_tracknumber_unique (&demux->common,
new_track->num)) {
GST_ERROR_OBJECT (demux,
"Unexpected new TrackNumber: %" G_GUINT64_FORMAT, new_track->num);
goto track_mismatch_error;
}
old_track_index =
gst_matroska_read_common_stream_from_num (&demux->common,
new_track->num);
g_assert (old_track_index != -1);
old_track = g_ptr_array_index (demux->common.src, old_track_index);
if (old_track->type != new_track->type) {
GST_ERROR_OBJECT (demux,
"Mismatch reparsing track %" G_GUINT64_FORMAT
" on track type. Expected %d, found %d", new_track->num,
old_track->type, new_track->type);
goto track_mismatch_error;
}
if (g_strcmp0 (old_track->codec_id, new_track->codec_id) != 0) {
GST_ERROR_OBJECT (demux,
"Mismatch reparsing track %" G_GUINT64_FORMAT
" on codec id. Expected '%s', found '%s'", new_track->num,
old_track->codec_id, new_track->codec_id);
goto track_mismatch_error;
}
/* The new track matches the old track. No problems on our side.
* Let's make it replace the old track. */
new_track->pad = old_track->pad;
new_track->index = old_track->index;
new_track->pos = old_track->pos;
g_ptr_array_index (demux->common.src, old_track_index) = new_track;
gst_pad_set_element_private (new_track->pad, new_track);
if (!gst_caps_is_equal (old_track->caps, new_track->caps)) {
gst_pad_set_caps (new_track->pad, new_track->caps);
}
if (!gst_tag_list_is_equal (old_track->tags, new_track->tags)) {
GST_DEBUG_OBJECT (old_track->pad, "Sending tags %p: %"
GST_PTR_FORMAT, new_track->tags, new_track->tags);
gst_pad_push_event (new_track->pad,
gst_event_new_tag (gst_tag_list_copy (new_track->tags)));
}
gst_matroska_track_free (old_track);
break;
track_mismatch_error:
gst_matroska_track_free (new_track);
new_track = NULL;
ret = GST_FLOW_ERROR;
break;
}
default:
ret = gst_matroska_read_common_parse_skip (&demux->common, ebml,
"Track", id);
break;
}
}
DEBUG_ELEMENT_STOP (demux, ebml, "Tracks", ret);
if (ret != GST_FLOW_ERROR && demux->common.num_streams != num_tracks_found) {
GST_ERROR_OBJECT (demux,
"Mismatch on the number of tracks. Expected %du tracks, found %du",
demux->common.num_streams, num_tracks_found);
ret = GST_FLOW_ERROR;
}
return ret;
}
/*
* Read signed/unsigned "EBML" numbers.
* Return: number of bytes processed.
*/
static gint
gst_matroska_ebmlnum_uint (guint8 * data, guint size, guint64 * num)
{
gint len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
guint64 total;
if (size <= 0) {
return -1;
}
total = data[0];
while (read <= 8 && !(total & len_mask)) {
read++;
len_mask >>= 1;
}
if (read > 8)
return -1;
if ((total &= (len_mask - 1)) == len_mask - 1)
num_ffs++;
if (size < read)
return -1;
while (n < read) {
if (data[n] == 0xff)
num_ffs++;
total = (total << 8) | data[n];
n++;
}
if (read == num_ffs && total != 0)
*num = G_MAXUINT64;
else
*num = total;
return read;
}
static gint
gst_matroska_ebmlnum_sint (guint8 * data, guint size, gint64 * num)
{
guint64 unum;
gint res;
/* read as unsigned number first */
if ((res = gst_matroska_ebmlnum_uint (data, size, &unum)) < 0)
return -1;
/* make signed */
if (unum == G_MAXUINT64)
*num = G_MAXINT64;
else
*num = unum - ((1 << ((7 * res) - 1)) - 1);
return res;
}
/*
* Mostly used for subtitles. We add void filler data for each
* lagging stream to make sure we don't deadlock.
*/
static void
gst_matroska_demux_sync_streams (GstMatroskaDemux * demux)
{
GstClockTime gap_threshold;
gint stream_nr;
GST_OBJECT_LOCK (demux);
GST_LOG_OBJECT (demux, "Sync to %" GST_TIME_FORMAT,
GST_TIME_ARGS (demux->common.segment.position));
g_assert (demux->common.num_streams == demux->common.src->len);
for (stream_nr = 0; stream_nr < demux->common.src->len; stream_nr++) {
GstMatroskaTrackContext *context;
context = g_ptr_array_index (demux->common.src, stream_nr);
GST_LOG_OBJECT (demux,
"Checking for resync on stream %d (%" GST_TIME_FORMAT ")", stream_nr,
GST_TIME_ARGS (context->pos));
/* Only send gap events on non-subtitle streams if lagging way behind.
* The 0.5 second threshold for subtitle streams is also quite random. */
if (context->type == GST_MATROSKA_TRACK_TYPE_SUBTITLE)
gap_threshold = GST_SECOND / 2;
else
gap_threshold = 3 * GST_SECOND;
/* Lag need only be considered if we have advanced into requested segment */
if (GST_CLOCK_TIME_IS_VALID (context->pos) &&
GST_CLOCK_TIME_IS_VALID (demux->common.segment.position) &&
demux->common.segment.position > demux->common.segment.start &&
context->pos + gap_threshold < demux->common.segment.position) {
GstEvent *event;
guint64 start = context->pos;
guint64 stop = demux->common.segment.position - gap_threshold;
GST_DEBUG_OBJECT (demux,
"Synchronizing stream %d with other by advancing time from %"
GST_TIME_FORMAT " to %" GST_TIME_FORMAT, stream_nr,
GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
context->pos = stop;
event = gst_event_new_gap (start, stop - start);
GST_OBJECT_UNLOCK (demux);
gst_pad_push_event (context->pad, event);
GST_OBJECT_LOCK (demux);
}
}
GST_OBJECT_UNLOCK (demux);
}
static GstFlowReturn
gst_matroska_demux_push_stream_headers (GstMatroskaDemux * demux,
GstMatroskaTrackContext * stream)
{
GstFlowReturn ret = GST_FLOW_OK;
gint i, num;
num = gst_buffer_list_length (stream->stream_headers);