2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * Copyright (C) <2006> Wim Taymans <wim@fluendo.com>
5 * Copyright (C) <2007> Julien Moutte <julien@fluendo.com>
6 * Copyright (C) <2009> Tim-Philipp Müller <tim centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:element-qtdemux
27 * Demuxes a .mov file into raw or compressed audio and/or video streams.
29 * This element supports both push and pull-based scheduling, depending on the
30 * capabilities of the upstream elements.
33 * <title>Example launch line</title>
35 * gst-launch filesrc location=test.mov ! qtdemux name=demux demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
36 * ]| Play (parse and decode) a .mov file and try to output it to
37 * an automatically detected soundcard and videosink. If the MOV file contains
38 * compressed audio or video data, this will only work if you have the
39 * right decoder elements/plugins installed.
42 * Last reviewed on 2006-12-29 (0.10.5)
49 #include "gst/gst-i18n-plugin.h"
51 #include <gst/tag/tag.h>
53 #include "qtatomparser.h"
54 #include "qtdemux_types.h"
55 #include "qtdemux_dump.h"
56 #include "qtdemux_fourcc.h"
58 #include "qtpalette.h"
67 /* max. size considered 'sane' for non-mdat atoms */
68 #define QTDEMUX_MAX_ATOM_SIZE (25*1024*1024)
70 /* if the sample index is larger than this, something is likely wrong */
71 #define QTDEMUX_MAX_SAMPLE_INDEX_SIZE (50*1024*1024)
73 GST_DEBUG_CATEGORY (qtdemux_debug);
75 /*typedef struct _QtNode QtNode; */
76 typedef struct _QtDemuxSegment QtDemuxSegment;
77 typedef struct _QtDemuxSample QtDemuxSample;
90 GstClockTimeDiff pts_offset; /* Add this value to timestamp to get the pts */
91 guint64 timestamp; /* In GstClockTime */
92 guint64 duration; /* in GstClockTime */
93 gboolean keyframe; /* TRUE when this packet is a keyframe */
97 * Quicktime has tracks and segments. A track is a continuous piece of
98 * multimedia content. The track is not always played from start to finish but
99 * instead, pieces of the track are 'cut out' and played in sequence. This is
100 * what the segments do.
102 * Inside the track we have keyframes (K) and delta frames. The track has its
103 * own timing, which starts from 0 and extends to end. The position in the track
104 * is called the media_time.
106 * The segments now describe the pieces that should be played from this track
107 * and are basically tupples of media_time/duration/rate entries. We can have
108 * multiple segments and they are all played after one another. An example:
110 * segment 1: media_time: 1 second, duration: 1 second, rate 1
111 * segment 2: media_time: 3 second, duration: 2 second, rate 2
113 * To correctly play back this track, one must play: 1 second of media starting
114 * from media_time 1 followed by 2 seconds of media starting from media_time 3
117 * Each of the segments will be played at a specific time, the first segment at
118 * time 0, the second one after the duration of the first one, etc.. Note that
119 * the time in resulting playback is not identical to the media_time of the
122 * Visually, assuming the track has 4 second of media_time:
125 * .-----------------------------------------------------------.
126 * track: | K.....K.........K........K.......K.......K...........K... |
127 * '-----------------------------------------------------------'
129 * .------------^ ^ .----------^ ^
130 * / .-------------' / .------------------'
132 * .--------------. .--------------.
133 * | segment 1 | | segment 2 |
134 * '--------------' '--------------'
136 * The challenge here is to cut out the right pieces of the track for each of
137 * the playback segments. This fortunatly can easily be done with the SEGMENT
138 * events of gstreamer.
140 * For playback of segment 1, we need to provide the decoder with the keyframe
141 * (a), in the above figure, but we must instruct it only to output the decoded
142 * data between second 1 and 2. We do this with a SEGMENT event for 1 to 2, time
143 * position set to the time of the segment: 0.
145 * We then proceed to push data from keyframe (a) to frame (b). The decoder
146 * decodes but clips all before media_time 1.
148 * After finishing a segment, we push out a new SEGMENT event with the clipping
149 * boundaries of the new data.
151 * This is a good usecase for the GStreamer accumulated SEGMENT events.
154 struct _QtDemuxSegment
156 /* global time and duration, all gst time */
160 /* media time of trak, all gst time */
166 struct _QtDemuxStream
176 guint64 duration; /* in timescale */
181 QtDemuxSample *samples;
182 gboolean all_keyframe; /* TRUE when all samples are keyframes (no stss) */
183 guint32 min_duration; /* duration in timescale of first sample, used for figuring out
184 the framerate, in timescale units */
186 /* if we use chunks or samples */
198 /* Numerator/denominator framerate */
201 guint16 bits_per_sample;
202 guint16 color_table_id;
207 guint samples_per_packet;
208 guint samples_per_frame;
209 guint bytes_per_packet;
210 guint bytes_per_sample;
211 guint bytes_per_frame;
214 /* when a discontinuity is pending */
217 /* list of buffers to push first */
220 /* if we need to clip this buffer. This is only needed for uncompressed
224 /* current position */
225 guint32 segment_index;
226 guint32 sample_index;
227 guint64 time_position; /* in gst time */
229 /* the Gst segment we are processing out, used for clipping */
232 /* last GstFlowReturn */
233 GstFlowReturn last_ret;
235 /* quicktime segments */
237 QtDemuxSegment *segments;
242 GstTagList *pending_tags;
243 gboolean send_global_tags;
248 QTDEMUX_STATE_INITIAL, /* Initial state (haven't got the header yet) */
249 QTDEMUX_STATE_HEADER, /* Parsing the header */
250 QTDEMUX_STATE_MOVIE, /* Parsing/Playing the media data */
251 QTDEMUX_STATE_BUFFER_MDAT /* Buffering the mdat atom */
254 static GNode *qtdemux_tree_get_child_by_type (GNode * node, guint32 fourcc);
255 static GNode *qtdemux_tree_get_child_by_type_full (GNode * node,
256 guint32 fourcc, QtAtomParser * parser);
257 static GNode *qtdemux_tree_get_sibling_by_type (GNode * node, guint32 fourcc);
259 static const GstElementDetails gst_qtdemux_details =
260 GST_ELEMENT_DETAILS ("QuickTime demuxer",
262 "Demultiplex a QuickTime file into audio and video streams",
263 "David Schleef <ds@schleef.org>, Wim Taymans <wim@fluendo.com>");
265 static GstStaticPadTemplate gst_qtdemux_sink_template =
266 GST_STATIC_PAD_TEMPLATE ("sink",
269 GST_STATIC_CAPS ("video/quicktime; video/mj2; audio/x-m4a; "
273 static GstStaticPadTemplate gst_qtdemux_videosrc_template =
274 GST_STATIC_PAD_TEMPLATE ("video_%02d",
277 GST_STATIC_CAPS_ANY);
279 static GstStaticPadTemplate gst_qtdemux_audiosrc_template =
280 GST_STATIC_PAD_TEMPLATE ("audio_%02d",
283 GST_STATIC_CAPS_ANY);
285 static GstStaticPadTemplate gst_qtdemux_subpsrc_template =
286 GST_STATIC_PAD_TEMPLATE ("subp_%02d",
289 GST_STATIC_CAPS_ANY);
291 static GstElementClass *parent_class = NULL;
293 static void gst_qtdemux_class_init (GstQTDemuxClass * klass);
294 static void gst_qtdemux_base_init (GstQTDemuxClass * klass);
295 static void gst_qtdemux_init (GstQTDemux * quicktime_demux);
296 static void gst_qtdemux_dispose (GObject * object);
298 static GstStateChangeReturn gst_qtdemux_change_state (GstElement * element,
299 GstStateChange transition);
300 static gboolean qtdemux_sink_activate (GstPad * sinkpad);
301 static gboolean qtdemux_sink_activate_pull (GstPad * sinkpad, gboolean active);
302 static gboolean qtdemux_sink_activate_push (GstPad * sinkpad, gboolean active);
304 static void gst_qtdemux_loop (GstPad * pad);
305 static GstFlowReturn gst_qtdemux_chain (GstPad * sinkpad, GstBuffer * inbuf);
306 static gboolean gst_qtdemux_handle_sink_event (GstPad * pad, GstEvent * event);
308 static gboolean qtdemux_parse_moov (GstQTDemux * qtdemux, const guint8 * buffer,
310 static gboolean qtdemux_parse_node (GstQTDemux * qtdemux, GNode * node,
311 const guint8 * buffer, guint length);
312 static gboolean qtdemux_parse_tree (GstQTDemux * qtdemux);
314 static void gst_qtdemux_handle_esds (GstQTDemux * qtdemux,
315 QtDemuxStream * stream, GNode * esds, GstTagList * list);
316 static GstCaps *qtdemux_video_caps (GstQTDemux * qtdemux,
317 QtDemuxStream * stream, guint32 fourcc, const guint8 * stsd_data,
318 gchar ** codec_name);
319 static GstCaps *qtdemux_audio_caps (GstQTDemux * qtdemux,
320 QtDemuxStream * stream, guint32 fourcc, const guint8 * data, int len,
321 gchar ** codec_name);
322 static GstCaps *qtdemux_subp_caps (GstQTDemux * qtdemux,
323 QtDemuxStream * stream, guint32 fourcc, const guint8 * data,
324 gchar ** codec_name);
327 gst_qtdemux_get_type (void)
329 static GType qtdemux_type = 0;
331 if (G_UNLIKELY (!qtdemux_type)) {
332 static const GTypeInfo qtdemux_info = {
333 sizeof (GstQTDemuxClass),
334 (GBaseInitFunc) gst_qtdemux_base_init, NULL,
335 (GClassInitFunc) gst_qtdemux_class_init,
336 NULL, NULL, sizeof (GstQTDemux), 0,
337 (GInstanceInitFunc) gst_qtdemux_init,
341 g_type_register_static (GST_TYPE_ELEMENT, "GstQTDemux", &qtdemux_info,
348 gst_qtdemux_base_init (GstQTDemuxClass * klass)
350 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
352 gst_element_class_add_pad_template (element_class,
353 gst_static_pad_template_get (&gst_qtdemux_sink_template));
354 gst_element_class_add_pad_template (element_class,
355 gst_static_pad_template_get (&gst_qtdemux_videosrc_template));
356 gst_element_class_add_pad_template (element_class,
357 gst_static_pad_template_get (&gst_qtdemux_audiosrc_template));
358 gst_element_class_set_details (element_class, &gst_qtdemux_details);
360 GST_DEBUG_CATEGORY_INIT (qtdemux_debug, "qtdemux", 0, "qtdemux plugin");
364 gst_qtdemux_class_init (GstQTDemuxClass * klass)
366 GObjectClass *gobject_class;
367 GstElementClass *gstelement_class;
369 gobject_class = (GObjectClass *) klass;
370 gstelement_class = (GstElementClass *) klass;
372 parent_class = g_type_class_peek_parent (klass);
374 gobject_class->dispose = gst_qtdemux_dispose;
376 gstelement_class->change_state = gst_qtdemux_change_state;
380 gst_qtdemux_init (GstQTDemux * qtdemux)
383 gst_pad_new_from_static_template (&gst_qtdemux_sink_template, "sink");
384 gst_pad_set_activate_function (qtdemux->sinkpad, qtdemux_sink_activate);
385 gst_pad_set_activatepull_function (qtdemux->sinkpad,
386 qtdemux_sink_activate_pull);
387 gst_pad_set_activatepush_function (qtdemux->sinkpad,
388 qtdemux_sink_activate_push);
389 gst_pad_set_chain_function (qtdemux->sinkpad, gst_qtdemux_chain);
390 gst_pad_set_event_function (qtdemux->sinkpad, gst_qtdemux_handle_sink_event);
391 gst_element_add_pad (GST_ELEMENT_CAST (qtdemux), qtdemux->sinkpad);
393 qtdemux->state = QTDEMUX_STATE_INITIAL;
394 /* FIXME, use segment last_stop for this */
395 qtdemux->last_ts = GST_CLOCK_TIME_NONE;
396 qtdemux->pullbased = FALSE;
397 qtdemux->neededbytes = 16;
399 qtdemux->adapter = gst_adapter_new ();
401 qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
402 qtdemux->mdatbuffer = NULL;
403 gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
407 gst_qtdemux_dispose (GObject * object)
409 GstQTDemux *qtdemux = GST_QTDEMUX (object);
411 if (qtdemux->adapter) {
412 g_object_unref (G_OBJECT (qtdemux->adapter));
413 qtdemux->adapter = NULL;
416 G_OBJECT_CLASS (parent_class)->dispose (object);
420 gst_qtdemux_pull_atom (GstQTDemux * qtdemux, guint64 offset, guint64 size,
425 /* Sanity check: catch bogus sizes (fuzzed/broken files) */
426 if (G_UNLIKELY (size > QTDEMUX_MAX_ATOM_SIZE)) {
427 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
428 (_("This file is invalid and cannot be played.")),
429 ("atom has bogus size %" G_GUINT64_FORMAT, size));
430 return GST_FLOW_ERROR;
433 flow = gst_pad_pull_range (qtdemux->sinkpad, offset, size, buf);
435 if (G_UNLIKELY (flow != GST_FLOW_OK))
438 /* Catch short reads - we don't want any partial atoms */
439 if (G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size)) {
440 GST_WARNING_OBJECT (qtdemux, "short read: %u < %" G_GUINT64_FORMAT,
441 GST_BUFFER_SIZE (*buf), size);
442 gst_buffer_unref (*buf);
444 return GST_FLOW_UNEXPECTED;
452 gst_qtdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
453 GstFormat * dest_format, gint64 * dest_value)
456 QtDemuxStream *stream = gst_pad_get_element_private (pad);
458 if (stream->subtype == GST_MAKE_FOURCC ('v', 'i', 'd', 'e') &&
459 (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
462 switch (src_format) {
463 case GST_FORMAT_TIME:
464 switch (*dest_format) {
465 case GST_FORMAT_BYTES:
466 *dest_value = src_value * 1; /* FIXME */
468 case GST_FORMAT_DEFAULT:
469 *dest_value = src_value * 1; /* FIXME */
476 case GST_FORMAT_BYTES:
477 switch (*dest_format) {
478 case GST_FORMAT_TIME:
479 *dest_value = src_value * 1; /* FIXME */
486 case GST_FORMAT_DEFAULT:
487 switch (*dest_format) {
488 case GST_FORMAT_TIME:
489 *dest_value = src_value * 1; /* FIXME */
504 static const GstQueryType *
505 gst_qtdemux_get_src_query_types (GstPad * pad)
507 static const GstQueryType src_types[] = {
518 gst_qtdemux_get_duration (GstQTDemux * qtdemux, gint64 * duration)
522 *duration = GST_CLOCK_TIME_NONE;
524 if (qtdemux->duration != 0) {
525 if (qtdemux->duration != G_MAXINT32 && qtdemux->timescale != 0) {
526 *duration = gst_util_uint64_scale (qtdemux->duration,
527 GST_SECOND, qtdemux->timescale);
534 gst_qtdemux_handle_src_query (GstPad * pad, GstQuery * query)
536 gboolean res = FALSE;
537 GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
539 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
541 switch (GST_QUERY_TYPE (query)) {
542 case GST_QUERY_POSITION:
543 if (GST_CLOCK_TIME_IS_VALID (qtdemux->segment.last_stop)) {
544 gst_query_set_position (query, GST_FORMAT_TIME,
545 qtdemux->segment.last_stop);
549 case GST_QUERY_DURATION:{
552 gst_query_parse_duration (query, &fmt, NULL);
553 if (fmt == GST_FORMAT_TIME) {
554 gint64 duration = -1;
556 gst_qtdemux_get_duration (qtdemux, &duration);
558 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
564 case GST_QUERY_SEEKING:{
568 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
569 if (fmt == GST_FORMAT_TIME) {
570 gint64 duration = -1;
572 gst_qtdemux_get_duration (qtdemux, &duration);
574 if (!qtdemux->pullbased) {
577 /* we might be able with help from upstream */
579 q = gst_query_new_seeking (GST_FORMAT_BYTES);
580 if (gst_pad_peer_query (qtdemux->sinkpad, q)) {
581 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
582 GST_LOG_OBJECT (qtdemux, "upstream BYTE seekable %d", seekable);
586 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
592 res = gst_pad_query_default (pad, query);
596 gst_object_unref (qtdemux);
602 gst_qtdemux_push_tags (GstQTDemux * qtdemux, QtDemuxStream * stream)
604 if (G_LIKELY (stream->pad)) {
605 GST_DEBUG_OBJECT (qtdemux, "Checking pad %s:%s for tags",
606 GST_DEBUG_PAD_NAME (stream->pad));
608 if (G_UNLIKELY (stream->pending_tags)) {
609 GST_DEBUG_OBJECT (qtdemux, "Sending tags %" GST_PTR_FORMAT,
610 stream->pending_tags);
611 gst_pad_push_event (stream->pad,
612 gst_event_new_tag (stream->pending_tags));
613 stream->pending_tags = NULL;
616 if (G_UNLIKELY (stream->send_global_tags && qtdemux->tag_list)) {
617 GST_DEBUG_OBJECT (qtdemux, "Sending global tags %" GST_PTR_FORMAT,
619 gst_pad_push_event (stream->pad,
620 gst_event_new_tag (gst_tag_list_copy (qtdemux->tag_list)));
621 stream->send_global_tags = FALSE;
626 /* push event on all source pads; takes ownership of the event */
628 gst_qtdemux_push_event (GstQTDemux * qtdemux, GstEvent * event)
632 GST_DEBUG_OBJECT (qtdemux, "pushing %s event on all source pads",
633 GST_EVENT_TYPE_NAME (event));
635 for (n = 0; n < qtdemux->n_streams; n++) {
638 if ((pad = qtdemux->streams[n]->pad))
639 gst_pad_push_event (pad, gst_event_ref (event));
641 gst_event_unref (event);
644 /* push a pending newsegment event, if any from the streaming thread */
646 gst_qtdemux_push_pending_newsegment (GstQTDemux * qtdemux)
648 if (qtdemux->pending_newsegment) {
649 gst_qtdemux_push_event (qtdemux, qtdemux->pending_newsegment);
650 qtdemux->pending_newsegment = NULL;
660 find_func (QtDemuxSample * s1, guint64 * media_time, gpointer user_data)
662 if (s1->timestamp > *media_time)
668 /* find the index of the sample that includes the data for @media_time
670 * Returns the index of the sample.
673 gst_qtdemux_find_index (GstQTDemux * qtdemux, QtDemuxStream * str,
676 QtDemuxSample *result;
679 result = gst_util_array_binary_search (str->samples, str->n_samples,
680 sizeof (QtDemuxSample), (GCompareDataFunc) find_func,
681 GST_SEARCH_MODE_BEFORE, &media_time, NULL);
683 if (G_LIKELY (result))
684 index = result - str->samples;
691 /* find the index of the keyframe needed to decode the sample at @index
694 * Returns the index of the keyframe.
697 gst_qtdemux_find_keyframe (GstQTDemux * qtdemux, QtDemuxStream * str,
700 guint32 new_index = index;
702 if (index >= str->n_samples) {
703 new_index = str->n_samples;
707 /* all keyframes, return index */
708 if (str->all_keyframe) {
713 /* else go back until we have a keyframe */
715 if (str->samples[new_index].keyframe)
725 GST_DEBUG_OBJECT (qtdemux, "searching for keyframe index before index %u "
726 "gave %u", index, new_index);
731 /* find the segment for @time_position for @stream
733 * Returns -1 if the segment cannot be found.
736 gst_qtdemux_find_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
737 guint64 time_position)
742 GST_LOG_OBJECT (qtdemux, "finding segment for %" GST_TIME_FORMAT,
743 GST_TIME_ARGS (time_position));
745 /* find segment corresponding to time_position if we are looking
748 for (i = 0; i < stream->n_segments; i++) {
749 QtDemuxSegment *segment = &stream->segments[i];
751 GST_LOG_OBJECT (qtdemux,
752 "looking at segment %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
753 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->stop_time));
755 /* For the last segment we include stop_time in the last segment */
756 if (i < stream->n_segments - 1) {
757 if (segment->time <= time_position && time_position < segment->stop_time) {
758 GST_LOG_OBJECT (qtdemux, "segment %d matches", i);
763 if (segment->time <= time_position && time_position <= segment->stop_time) {
764 GST_LOG_OBJECT (qtdemux, "segment %d matches", i);
773 /* move the stream @str to the sample position @index.
775 * Updates @str->sample_index and marks discontinuity if needed.
778 gst_qtdemux_move_stream (GstQTDemux * qtdemux, QtDemuxStream * str,
781 /* no change needed */
782 if (index == str->sample_index)
785 GST_DEBUG_OBJECT (qtdemux, "moving to sample %u of %u", index,
788 /* position changed, we have a discont */
789 str->sample_index = index;
790 /* Each time we move in the stream we store the position where we are
792 str->from_sample = index;
797 gst_qtdemux_adjust_seek (GstQTDemux * qtdemux, gint64 desired_time,
798 gint64 * key_time, gint64 * key_offset)
801 gint64 min_byte_offset = -1;
804 min_offset = desired_time;
806 /* for each stream, find the index of the sample in the segment
807 * and move back to the previous keyframe. */
808 for (n = 0; n < qtdemux->n_streams; n++) {
810 guint32 index, kindex;
817 str = qtdemux->streams[n];
819 seg_idx = gst_qtdemux_find_segment (qtdemux, str, desired_time);
820 GST_DEBUG_OBJECT (qtdemux, "align segment %d", seg_idx);
822 /* segment not found, continue with normal flow */
826 /* get segment and time in the segment */
827 seg = &str->segments[seg_idx];
828 seg_time = desired_time - seg->time;
830 /* get the media time in the segment */
831 media_start = seg->media_start + seg_time;
833 /* get the index of the sample with media time */
834 index = gst_qtdemux_find_index (qtdemux, str, media_start);
835 GST_DEBUG_OBJECT (qtdemux, "sample for %" GST_TIME_FORMAT " at %u",
836 GST_TIME_ARGS (media_start), index);
838 /* find previous keyframe */
839 kindex = gst_qtdemux_find_keyframe (qtdemux, str, index);
841 /* if the keyframe is at a different position, we need to update the
842 * requested seek time */
843 if (index != kindex) {
846 /* get timestamp of keyframe */
847 media_time = str->samples[kindex].timestamp;
848 GST_DEBUG_OBJECT (qtdemux, "keyframe at %u with time %" GST_TIME_FORMAT,
849 kindex, GST_TIME_ARGS (media_time));
851 /* keyframes in the segment get a chance to change the
852 * desired_offset. keyframes out of the segment are
854 if (media_time >= seg->media_start) {
857 /* this keyframe is inside the segment, convert back to
859 seg_time = (media_time - seg->media_start) + seg->time;
860 if (seg_time < min_offset)
861 min_offset = seg_time;
865 if (min_byte_offset < 0 || str->samples[index].offset < min_byte_offset)
866 min_byte_offset = str->samples[index].offset;
870 *key_time = min_offset;
872 *key_offset = min_byte_offset;
876 gst_qtdemux_convert_seek (GstPad * pad, GstFormat * format,
877 GstSeekType cur_type, gint64 * cur, GstSeekType stop_type, gint64 * stop)
882 g_return_val_if_fail (format != NULL, FALSE);
883 g_return_val_if_fail (cur != NULL, FALSE);
884 g_return_val_if_fail (stop != NULL, FALSE);
886 if (*format == GST_FORMAT_TIME)
889 fmt = GST_FORMAT_TIME;
891 if (cur_type != GST_SEEK_TYPE_NONE)
892 res = gst_pad_query_convert (pad, *format, *cur, &fmt, cur);
893 if (res && stop_type != GST_SEEK_TYPE_NONE)
894 res = gst_pad_query_convert (pad, *format, *stop, &fmt, stop);
897 *format = GST_FORMAT_TIME;
902 /* perform seek in push based mode:
903 find BYTE position to move to based on time and delegate to upstream
906 gst_qtdemux_do_push_seek (GstQTDemux * qtdemux, GstPad * pad, GstEvent * event)
911 GstSeekType cur_type, stop_type;
916 GST_DEBUG_OBJECT (qtdemux, "doing push-based seek");
918 gst_event_parse_seek (event, &rate, &format, &flags,
919 &cur_type, &cur, &stop_type, &stop);
921 if (stop_type != GST_SEEK_TYPE_NONE)
922 goto unsupported_seek;
925 /* only forward streaming and seeking is possible */
927 goto unsupported_seek;
929 /* convert to TIME if needed and possible */
930 if (!gst_qtdemux_convert_seek (pad, &format, cur_type, &cur,
934 /* find reasonable corresponding BYTE position,
935 * also try to mind about keyframes, since we can not go back a bit for them
937 gst_qtdemux_adjust_seek (qtdemux, cur, NULL, &byte_cur);
942 GST_DEBUG_OBJECT (qtdemux, "Pushing BYTE seek rate %g, "
943 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, byte_cur,
945 /* BYTE seek event */
946 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, byte_cur,
948 res = gst_pad_push_event (qtdemux->sinkpad, event);
955 GST_DEBUG_OBJECT (qtdemux, "could not determine byte position to seek to, "
961 GST_DEBUG_OBJECT (qtdemux, "unsupported seek, seek aborted.");
966 GST_DEBUG_OBJECT (qtdemux, "unsupported format given, seek aborted.");
973 * We set all segment_indexes in the streams to unknown and
974 * adjust the time_position to the desired position. this is enough
975 * to trigger a segment switch in the streaming thread to start
976 * streaming from the desired position.
978 * Keyframe seeking is a little more complicated when dealing with
979 * segments. Ideally we want to move to the previous keyframe in
980 * the segment but there might not be a keyframe in the segment. In
981 * fact, none of the segments could contain a keyframe. We take a
982 * practical approach: seek to the previous keyframe in the segment,
983 * if there is none, seek to the beginning of the segment.
985 * Called with STREAM_LOCK
988 gst_qtdemux_perform_seek (GstQTDemux * qtdemux, GstSegment * segment)
990 gint64 desired_offset;
993 desired_offset = segment->last_stop;
995 GST_DEBUG_OBJECT (qtdemux, "seeking to %" GST_TIME_FORMAT,
996 GST_TIME_ARGS (desired_offset));
998 if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
1001 gst_qtdemux_adjust_seek (qtdemux, desired_offset, &min_offset, NULL);
1002 GST_DEBUG_OBJECT (qtdemux, "keyframe seek, align to %"
1003 GST_TIME_FORMAT, GST_TIME_ARGS (min_offset));
1004 desired_offset = min_offset;
1007 /* and set all streams to the final position */
1008 for (n = 0; n < qtdemux->n_streams; n++) {
1009 QtDemuxStream *stream = qtdemux->streams[n];
1011 stream->time_position = desired_offset;
1012 stream->sample_index = -1;
1013 stream->segment_index = -1;
1014 stream->last_ret = GST_FLOW_OK;
1015 stream->sent_eos = FALSE;
1017 segment->last_stop = desired_offset;
1018 segment->time = desired_offset;
1020 /* we stop at the end */
1021 if (segment->stop == -1)
1022 segment->stop = segment->duration;
1027 /* do a seek in pull based mode */
1029 gst_qtdemux_do_seek (GstQTDemux * qtdemux, GstPad * pad, GstEvent * event)
1034 GstSeekType cur_type, stop_type;
1038 GstSegment seeksegment;
1042 GST_DEBUG_OBJECT (qtdemux, "doing seek with event");
1044 gst_event_parse_seek (event, &rate, &format, &flags,
1045 &cur_type, &cur, &stop_type, &stop);
1047 /* we have to have a format as the segment format. Try to convert
1049 if (!gst_qtdemux_convert_seek (pad, &format, cur_type, &cur,
1053 GST_DEBUG_OBJECT (qtdemux, "seek format %s", gst_format_get_name (format));
1055 GST_DEBUG_OBJECT (qtdemux, "doing seek without event");
1059 flush = flags & GST_SEEK_FLAG_FLUSH;
1061 /* stop streaming, either by flushing or by pausing the task */
1063 /* unlock upstream pull_range */
1064 gst_pad_push_event (qtdemux->sinkpad, gst_event_new_flush_start ());
1065 /* make sure out loop function exits */
1066 gst_qtdemux_push_event (qtdemux, gst_event_new_flush_start ());
1068 /* non flushing seek, pause the task */
1069 gst_pad_pause_task (qtdemux->sinkpad);
1072 /* wait for streaming to finish */
1073 GST_PAD_STREAM_LOCK (qtdemux->sinkpad);
1075 /* copy segment, we need this because we still need the old
1076 * segment when we close the current segment. */
1077 memcpy (&seeksegment, &qtdemux->segment, sizeof (GstSegment));
1080 /* configure the segment with the seek variables */
1081 GST_DEBUG_OBJECT (qtdemux, "configuring seek");
1082 gst_segment_set_seek (&seeksegment, rate, format, flags,
1083 cur_type, cur, stop_type, stop, &update);
1086 /* now do the seek, this actually never returns FALSE */
1087 gst_qtdemux_perform_seek (qtdemux, &seeksegment);
1089 /* prepare for streaming again */
1091 gst_pad_push_event (qtdemux->sinkpad, gst_event_new_flush_stop ());
1092 gst_qtdemux_push_event (qtdemux, gst_event_new_flush_stop ());
1093 } else if (qtdemux->segment_running) {
1094 /* we are running the current segment and doing a non-flushing seek,
1095 * close the segment first based on the last_stop. */
1096 GST_DEBUG_OBJECT (qtdemux, "closing running segment %" G_GINT64_FORMAT
1097 " to %" G_GINT64_FORMAT, qtdemux->segment.start,
1098 qtdemux->segment.last_stop);
1100 if (qtdemux->segment.rate >= 0) {
1101 /* FIXME, rate is the product of the global rate and the (quicktime)
1103 qtdemux->pending_newsegment = gst_event_new_new_segment (TRUE,
1104 qtdemux->segment.rate, qtdemux->segment.format,
1105 qtdemux->segment.start, qtdemux->segment.last_stop,
1106 qtdemux->segment.time);
1107 } else { /* For Reverse Playback */
1110 if ((stop = qtdemux->segment.stop) == -1)
1111 stop = qtdemux->segment.duration;
1112 /* for reverse playback, we played from stop to last_stop. */
1113 qtdemux->pending_newsegment = gst_event_new_new_segment (TRUE,
1114 qtdemux->segment.rate, qtdemux->segment.format,
1115 qtdemux->segment.last_stop, stop, qtdemux->segment.last_stop);
1119 /* commit the new segment */
1120 memcpy (&qtdemux->segment, &seeksegment, sizeof (GstSegment));
1122 if (qtdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1123 gst_element_post_message (GST_ELEMENT_CAST (qtdemux),
1124 gst_message_new_segment_start (GST_OBJECT_CAST (qtdemux),
1125 qtdemux->segment.format, qtdemux->segment.last_stop));
1128 /* restart streaming, NEWSEGMENT will be sent from the streaming
1130 qtdemux->segment_running = TRUE;
1131 for (i = 0; i < qtdemux->n_streams; i++)
1132 qtdemux->streams[i]->last_ret = GST_FLOW_OK;
1134 gst_pad_start_task (qtdemux->sinkpad, (GstTaskFunction) gst_qtdemux_loop,
1137 GST_PAD_STREAM_UNLOCK (qtdemux->sinkpad);
1144 GST_DEBUG_OBJECT (qtdemux, "unsupported format given, seek aborted.");
1150 gst_qtdemux_handle_src_event (GstPad * pad, GstEvent * event)
1152 gboolean res = TRUE;
1153 GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
1155 switch (GST_EVENT_TYPE (event)) {
1156 case GST_EVENT_SEEK:
1157 if (qtdemux->pullbased) {
1158 res = gst_qtdemux_do_seek (qtdemux, pad, event);
1159 } else if (qtdemux->state == QTDEMUX_STATE_MOVIE && qtdemux->n_streams) {
1160 res = gst_qtdemux_do_push_seek (qtdemux, pad, event);
1162 GST_DEBUG_OBJECT (qtdemux,
1163 "ignoring seek in push mode in current state");
1166 gst_event_unref (event);
1169 case GST_EVENT_NAVIGATION:
1171 gst_event_unref (event);
1174 res = gst_pad_event_default (pad, event);
1178 gst_object_unref (qtdemux);
1183 /* stream/index return sample that is min/max w.r.t. byte position,
1184 * time is min/max w.r.t. time of samples,
1185 * the latter need not be time of the former sample */
1187 gst_qtdemux_find_sample (GstQTDemux * qtdemux, gint64 byte_pos, gboolean fw,
1188 gboolean set, QtDemuxStream ** _stream, gint * _index, gint64 * _time)
1191 gint64 time, min_time;
1192 QtDemuxStream *stream;
1198 for (n = 0; n < qtdemux->n_streams; ++n) {
1201 gboolean set_sample;
1204 str = qtdemux->streams[n];
1211 i = str->n_samples - 1;
1214 for (; (i >= 0) && (i < str->n_samples); i += inc) {
1215 if (str->samples[i].size &&
1216 ((fw && (str->samples[i].offset >= byte_pos)) ||
1218 (str->samples[i].offset + str->samples[i].size <=
1220 /* move stream to first available sample */
1222 gst_qtdemux_move_stream (qtdemux, str, i);
1225 /* determine min/max time */
1226 time = str->samples[i].timestamp + str->samples[i].pts_offset;
1227 if (min_time == -1 || (fw && min_time > time) ||
1228 (!fw && min_time < time)) {
1231 /* determine stream with leading sample, to get its position */
1232 /* only needed in fw case */
1233 if (fw && (!stream ||
1234 str->samples[i].offset < stream->samples[index].offset)) {
1241 /* no sample for this stream, mark eos */
1243 gst_qtdemux_move_stream (qtdemux, str, str->n_samples);
1255 gst_qtdemux_handle_sink_event (GstPad * sinkpad, GstEvent * event)
1257 GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
1260 GST_LOG_OBJECT (demux, "handling %s event", GST_EVENT_TYPE_NAME (event));
1262 switch (GST_EVENT_TYPE (event)) {
1263 case GST_EVENT_NEWSEGMENT:
1266 gdouble rate, arate;
1267 gint64 start, stop, time, offset = 0;
1268 QtDemuxStream *stream;
1273 /* some debug output */
1274 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
1275 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1276 &start, &stop, &time);
1277 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
1279 GST_DEBUG_OBJECT (demux,
1280 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
1283 /* chain will send initial newsegment after pads have been added */
1284 if (demux->state != QTDEMUX_STATE_MOVIE || !demux->n_streams) {
1285 GST_DEBUG_OBJECT (demux, "still starting, eating event");
1289 /* we only expect a BYTE segment, e.g. following a seek */
1290 if (format == GST_FORMAT_BYTES) {
1293 gst_qtdemux_find_sample (demux, start, TRUE, FALSE, NULL, NULL,
1295 start = MAX (start, 0);
1298 gst_qtdemux_find_sample (demux, stop, FALSE, FALSE, NULL, NULL,
1300 stop = MAX (stop, 0);
1303 GST_DEBUG_OBJECT (demux, "unsupported segment format, ignoring");
1307 /* accept upstream's notion of segment and distribute along */
1308 gst_segment_set_newsegment_full (&demux->segment, update, rate, arate,
1309 GST_FORMAT_TIME, start, stop, start);
1310 GST_DEBUG_OBJECT (demux, "Pushing newseg update %d, rate %g, "
1311 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
1312 "stop %" G_GINT64_FORMAT, update, rate, arate, GST_FORMAT_TIME,
1314 gst_qtdemux_push_event (demux,
1315 gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
1316 start, stop, start));
1318 /* clear leftover in current segment, if any */
1319 gst_adapter_clear (demux->adapter);
1320 /* set up streaming thread */
1321 gst_qtdemux_find_sample (demux, offset, TRUE, TRUE, &stream, &idx, NULL);
1322 demux->offset = offset;
1324 demux->todrop = stream->samples[idx].offset - offset;
1325 demux->neededbytes = demux->todrop + stream->samples[idx].size;
1327 /* set up for EOS */
1328 demux->neededbytes = -1;
1332 gst_event_unref (event);
1337 case GST_EVENT_FLUSH_STOP:
1341 /* clean up, force EOS if no more info follows */
1342 gst_adapter_clear (demux->adapter);
1344 demux->neededbytes = -1;
1345 /* reset flow return, e.g. following seek */
1346 for (i = 0; i < demux->n_streams; i++) {
1347 demux->streams[i]->last_ret = GST_FLOW_OK;
1348 demux->streams[i]->sent_eos = FALSE;
1353 /* If we are in push mode, and get an EOS before we've seen any streams,
1354 * then error out - we have nowhere to send the EOS */
1355 if (!demux->pullbased && demux->n_streams == 0) {
1356 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
1357 (_("This file contains no playable streams.")),
1358 ("no known streams found"));
1365 res = gst_pad_event_default (demux->sinkpad, event);
1371 static GstStateChangeReturn
1372 gst_qtdemux_change_state (GstElement * element, GstStateChange transition)
1374 GstQTDemux *qtdemux = GST_QTDEMUX (element);
1375 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1377 switch (transition) {
1378 case GST_STATE_CHANGE_PAUSED_TO_READY:
1384 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1386 switch (transition) {
1387 case GST_STATE_CHANGE_PAUSED_TO_READY:{
1390 qtdemux->state = QTDEMUX_STATE_INITIAL;
1391 qtdemux->last_ts = GST_CLOCK_TIME_NONE;
1392 qtdemux->neededbytes = 16;
1393 qtdemux->todrop = 0;
1394 qtdemux->pullbased = FALSE;
1395 qtdemux->offset = 0;
1396 qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
1397 if (qtdemux->mdatbuffer)
1398 gst_buffer_unref (qtdemux->mdatbuffer);
1399 qtdemux->mdatbuffer = NULL;
1400 if (qtdemux->comp_brands)
1401 gst_buffer_unref (qtdemux->comp_brands);
1402 qtdemux->comp_brands = NULL;
1403 if (qtdemux->tag_list)
1404 gst_tag_list_free (qtdemux->tag_list);
1405 qtdemux->tag_list = NULL;
1406 gst_adapter_clear (qtdemux->adapter);
1407 for (n = 0; n < qtdemux->n_streams; n++) {
1408 QtDemuxStream *stream = qtdemux->streams[n];
1410 while (stream->buffers) {
1411 gst_buffer_unref (GST_BUFFER_CAST (stream->buffers->data));
1413 g_slist_delete_link (stream->buffers, stream->buffers);
1416 gst_element_remove_pad (element, stream->pad);
1417 if (stream->samples)
1418 g_free (stream->samples);
1420 gst_caps_unref (stream->caps);
1421 if (stream->segments)
1422 g_free (stream->segments);
1423 if (stream->pending_tags)
1424 gst_tag_list_free (stream->pending_tags);
1427 qtdemux->major_brand = 0;
1428 qtdemux->n_streams = 0;
1429 qtdemux->n_video_streams = 0;
1430 qtdemux->n_audio_streams = 0;
1431 qtdemux->n_subp_streams = 0;
1432 gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
1443 qtdemux_parse_ftyp (GstQTDemux * qtdemux, const guint8 * buffer, gint length)
1445 /* only consider at least a sufficiently complete ftyp atom */
1449 qtdemux->major_brand = QT_FOURCC (buffer + 8);
1450 GST_DEBUG_OBJECT (qtdemux, "major brand: %" GST_FOURCC_FORMAT,
1451 GST_FOURCC_ARGS (qtdemux->major_brand));
1452 buf = qtdemux->comp_brands = gst_buffer_new_and_alloc (length - 16);
1453 memcpy (GST_BUFFER_DATA (buf), buffer + 16, GST_BUFFER_SIZE (buf));
1458 extract_initial_length_and_fourcc (const guint8 * data, guint64 * plength,
1464 length = QT_UINT32 (data);
1465 GST_DEBUG ("length 0x%08" G_GINT64_MODIFIER "x", length);
1466 fourcc = QT_FOURCC (data + 4);
1467 GST_DEBUG ("atom type %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
1470 length = G_MAXUINT32;
1471 } else if (length == 1) {
1472 /* this means we have an extended size, which is the 64 bit value of
1473 * the next 8 bytes */
1474 length = QT_UINT64 (data + 8);
1475 GST_DEBUG ("length 0x%08" G_GINT64_MODIFIER "x", length);
1484 static GstFlowReturn
1485 gst_qtdemux_loop_state_header (GstQTDemux * qtdemux)
1489 GstBuffer *buf = NULL;
1490 GstFlowReturn ret = GST_FLOW_OK;
1491 guint64 cur_offset = qtdemux->offset;
1493 ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, 16, &buf);
1494 if (G_UNLIKELY (ret != GST_FLOW_OK))
1496 if (G_LIKELY (GST_BUFFER_SIZE (buf) == 16))
1497 extract_initial_length_and_fourcc (GST_BUFFER_DATA (buf), &length, &fourcc);
1498 gst_buffer_unref (buf);
1500 if (G_UNLIKELY (length == 0)) {
1501 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
1502 (_("This file is invalid and cannot be played.")),
1503 ("Header atom '%" GST_FOURCC_FORMAT "' has empty length",
1504 GST_FOURCC_ARGS (fourcc)));
1505 ret = GST_FLOW_ERROR;
1516 GST_LOG_OBJECT (qtdemux,
1517 "skipping atom '%" GST_FOURCC_FORMAT "' at %" G_GUINT64_FORMAT,
1518 GST_FOURCC_ARGS (fourcc), cur_offset);
1519 qtdemux->offset += length;
1526 ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, length, &moov);
1527 if (ret != GST_FLOW_OK)
1529 if (length != GST_BUFFER_SIZE (moov)) {
1530 /* Some files have a 'moov' atom at the end of the file which contains
1531 * a terminal 'free' atom where the body of the atom is missing.
1532 * Check for, and permit, this special case.
1534 if (GST_BUFFER_SIZE (moov) >= 8) {
1535 guint8 *final_data = GST_BUFFER_DATA (moov) +
1536 (GST_BUFFER_SIZE (moov) - 8);
1537 guint32 final_length = QT_UINT32 (final_data);
1538 guint32 final_fourcc = QT_FOURCC (final_data + 4);
1539 if (final_fourcc == FOURCC_free &&
1540 GST_BUFFER_SIZE (moov) + final_length - 8 == length) {
1541 /* Ok, we've found that special case. Allocate a new buffer with
1542 * that free atom actually present. */
1543 GstBuffer *newmoov = gst_buffer_new_and_alloc (length);
1544 gst_buffer_copy_metadata (newmoov, moov,
1545 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1546 GST_BUFFER_COPY_CAPS);
1547 memcpy (GST_BUFFER_DATA (newmoov), GST_BUFFER_DATA (moov),
1548 GST_BUFFER_SIZE (moov));
1549 memset (GST_BUFFER_DATA (newmoov) + GST_BUFFER_SIZE (moov), 0,
1551 gst_buffer_unref (moov);
1557 if (length != GST_BUFFER_SIZE (moov)) {
1558 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
1559 (_("This file is incomplete and cannot be played.")),
1560 ("We got less than expected (received %u, wanted %u, offset %"
1561 G_GUINT64_FORMAT ")",
1562 GST_BUFFER_SIZE (moov), (guint) length, cur_offset));
1563 ret = GST_FLOW_ERROR;
1566 qtdemux->offset += length;
1568 qtdemux_parse_moov (qtdemux, GST_BUFFER_DATA (moov), length);
1569 qtdemux_node_dump (qtdemux, qtdemux->moov_node);
1571 qtdemux_parse_tree (qtdemux);
1572 g_node_destroy (qtdemux->moov_node);
1573 gst_buffer_unref (moov);
1574 qtdemux->moov_node = NULL;
1575 qtdemux->state = QTDEMUX_STATE_MOVIE;
1576 GST_DEBUG_OBJECT (qtdemux, "switching state to STATE_MOVIE (%d)",
1584 /* extract major brand; might come in handy for ISO vs QT issues */
1585 ret = gst_qtdemux_pull_atom (qtdemux, cur_offset, length, &ftyp);
1586 if (ret != GST_FLOW_OK)
1588 qtdemux->offset += length;
1589 qtdemux_parse_ftyp (qtdemux, GST_BUFFER_DATA (ftyp),
1590 GST_BUFFER_SIZE (ftyp));
1591 gst_buffer_unref (ftyp);
1598 GST_LOG_OBJECT (qtdemux,
1599 "unknown %08x '%" GST_FOURCC_FORMAT "' of size %" G_GUINT64_FORMAT
1600 " at %" G_GUINT64_FORMAT, fourcc, GST_FOURCC_ARGS (fourcc), length,
1602 ret = gst_qtdemux_pull_atom (qtdemux, cur_offset, length, &unknown);
1603 if (ret != GST_FLOW_OK)
1605 GST_MEMDUMP ("Unknown tag", GST_BUFFER_DATA (unknown),
1606 GST_BUFFER_SIZE (unknown));
1607 gst_buffer_unref (unknown);
1608 qtdemux->offset += length;
1617 /* Seeks to the previous keyframe of the indexed stream and
1618 * aligns other streams with respect to the keyframe timestamp
1619 * of indexed stream. Only called in case of Reverse Playback
1621 static GstFlowReturn
1622 gst_qtdemux_seek_to_previous_keyframe (GstQTDemux * qtdemux)
1625 guint32 seg_idx = 0, k_index = 0;
1626 guint64 k_pos = 0, last_stop = 0;
1627 QtDemuxSegment *seg = NULL;
1628 QtDemuxStream *ref_str = NULL;
1630 /* Now we choose an arbitrary stream, get the previous keyframe timestamp
1631 * and finally align all the other streams on that timestamp with their
1632 * respective keyframes */
1633 for (n = 0; n < qtdemux->n_streams; n++) {
1634 QtDemuxStream *str = qtdemux->streams[n];
1636 seg_idx = gst_qtdemux_find_segment (qtdemux, str,
1637 qtdemux->segment.last_stop);
1639 /* segment not found, continue with normal flow */
1643 /* No candidate yet, take that one */
1649 /* So that stream has a segment, we prefer video streams */
1650 if (str->subtype == FOURCC_vide) {
1656 if (G_UNLIKELY (!ref_str)) {
1657 GST_DEBUG_OBJECT (qtdemux, "couldn't find any stream");
1661 if (G_UNLIKELY (!ref_str->from_sample)) {
1662 GST_DEBUG_OBJECT (qtdemux, "reached the beginning of the file");
1666 /* So that stream has been playing from from_sample to to_sample. We will
1667 * get the timestamp of the previous sample and search for a keyframe before
1668 * that. For audio streams we do an arbitrary jump in the past (10 samples) */
1669 if (ref_str->subtype == FOURCC_vide) {
1670 k_index = gst_qtdemux_find_keyframe (qtdemux, ref_str,
1671 ref_str->from_sample - 1);
1673 k_index = ref_str->from_sample - 10;
1676 /* get current segment for that stream */
1677 seg = &ref_str->segments[ref_str->segment_index];
1678 /* Crawl back through segments to find the one containing this I frame */
1679 while (ref_str->samples[k_index].timestamp < seg->media_start) {
1680 GST_DEBUG_OBJECT (qtdemux, "keyframe position is out of segment %u",
1681 ref_str->segment_index);
1682 if (G_UNLIKELY (!ref_str->segment_index)) {
1683 /* Reached first segment, let's consider it's EOS */
1686 ref_str->segment_index--;
1687 seg = &ref_str->segments[ref_str->segment_index];
1689 /* Calculate time position of the keyframe and where we should stop */
1690 k_pos = (ref_str->samples[k_index].timestamp - seg->media_start) + seg->time;
1691 last_stop = ref_str->samples[ref_str->from_sample].timestamp;
1692 last_stop = (last_stop - seg->media_start) + seg->time;
1694 GST_DEBUG_OBJECT (qtdemux, "preferred stream played from sample %u, "
1695 "now going to sample %u (pts %" GST_TIME_FORMAT ")", ref_str->from_sample,
1696 k_index, GST_TIME_ARGS (k_pos));
1698 /* Set last_stop with the keyframe timestamp we pushed of that stream */
1699 gst_segment_set_last_stop (&qtdemux->segment, GST_FORMAT_TIME, last_stop);
1700 GST_DEBUG_OBJECT (qtdemux, "last_stop now is %" GST_TIME_FORMAT,
1701 GST_TIME_ARGS (last_stop));
1703 if (G_UNLIKELY (last_stop < qtdemux->segment.start)) {
1704 GST_DEBUG_OBJECT (qtdemux, "reached the beginning of segment");
1708 /* Align them all on this */
1709 for (n = 0; n < qtdemux->n_streams; n++) {
1711 guint64 media_start = 0, seg_time = 0;
1712 QtDemuxStream *str = qtdemux->streams[n];
1714 seg_idx = gst_qtdemux_find_segment (qtdemux, str, k_pos);
1715 GST_DEBUG_OBJECT (qtdemux, "align segment %d", seg_idx);
1717 /* segment not found, continue with normal flow */
1721 /* get segment and time in the segment */
1722 seg = &str->segments[seg_idx];
1723 seg_time = k_pos - seg->time;
1725 /* get the media time in the segment */
1726 media_start = seg->media_start + seg_time;
1728 /* get the index of the sample with media time */
1729 index = gst_qtdemux_find_index (qtdemux, str, media_start);
1730 GST_DEBUG_OBJECT (qtdemux, "sample for %" GST_TIME_FORMAT " at %u",
1731 GST_TIME_ARGS (media_start), index);
1733 /* find previous keyframe */
1734 k_index = gst_qtdemux_find_keyframe (qtdemux, str, index);
1736 /* Remember until where we want to go */
1737 str->to_sample = str->from_sample - 1;
1738 /* Define our time position */
1739 str->time_position =
1740 (str->samples[k_index].timestamp - seg->media_start) + seg->time;
1741 /* Now seek back in time */
1742 gst_qtdemux_move_stream (qtdemux, str, k_index);
1743 GST_DEBUG_OBJECT (qtdemux, "keyframe at %u, time position %"
1744 GST_TIME_FORMAT " playing from sample %u to %u", k_index,
1745 GST_TIME_ARGS (str->time_position), str->from_sample, str->to_sample);
1751 return GST_FLOW_UNEXPECTED;
1754 /* activate the given segment number @seg_idx of @stream at time @offset.
1755 * @offset is an absolute global position over all the segments.
1757 * This will push out a NEWSEGMENT event with the right values and
1758 * position the stream index to the first decodable sample before
1762 gst_qtdemux_activate_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
1763 guint32 seg_idx, guint64 offset)
1766 QtDemuxSegment *segment;
1767 guint32 index, kf_index;
1769 guint64 start, stop, time;
1772 GST_LOG_OBJECT (qtdemux, "activate segment %d, offset %" G_GUINT64_FORMAT,
1775 /* update the current segment */
1776 stream->segment_index = seg_idx;
1778 /* get the segment */
1779 segment = &stream->segments[seg_idx];
1781 if (G_UNLIKELY (offset < segment->time)) {
1782 GST_WARNING_OBJECT (qtdemux, "offset < segment->time %" G_GUINT64_FORMAT,
1787 /* get time in this segment */
1788 seg_time = offset - segment->time;
1790 GST_LOG_OBJECT (qtdemux, "seg_time %" GST_TIME_FORMAT,
1791 GST_TIME_ARGS (seg_time));
1793 if (G_UNLIKELY (seg_time > segment->duration)) {
1794 GST_LOG_OBJECT (qtdemux, "seg_time > segment->duration %" GST_TIME_FORMAT,
1795 GST_TIME_ARGS (segment->duration));
1799 /* qtdemux->segment.stop is in outside-time-realm, whereas
1800 * segment->media_stop is in track-time-realm.
1802 * In order to compare the two, we need to bring segment.stop
1803 * into the track-time-realm */
1805 if (qtdemux->segment.stop == -1)
1806 stop = segment->media_stop;
1809 MIN (segment->media_stop,
1810 qtdemux->segment.stop - segment->time + segment->media_start);
1812 if (qtdemux->segment.rate >= 0) {
1813 start = MIN (segment->media_start + seg_time, stop);
1816 start = segment->media_start;
1817 stop = MIN (segment->media_start + seg_time, stop);
1818 time = segment->time;
1821 GST_DEBUG_OBJECT (qtdemux, "newsegment %d from %" GST_TIME_FORMAT
1822 " to %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT, seg_idx,
1823 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (time));
1825 /* combine global rate with that of the segment */
1826 rate = segment->rate * qtdemux->segment.rate;
1828 /* update the segment values used for clipping */
1829 gst_segment_init (&stream->segment, GST_FORMAT_TIME);
1830 gst_segment_set_newsegment (&stream->segment, FALSE, rate, GST_FORMAT_TIME,
1833 /* now prepare and send the segment */
1835 event = gst_event_new_new_segment (FALSE, rate, GST_FORMAT_TIME,
1837 gst_pad_push_event (stream->pad, event);
1838 /* assume we can send more data now */
1839 stream->last_ret = GST_FLOW_OK;
1840 /* clear to send tags on this pad now */
1841 gst_qtdemux_push_tags (qtdemux, stream);
1844 /* and move to the keyframe before the indicated media time of the
1846 if (qtdemux->segment.rate >= 0) {
1847 index = gst_qtdemux_find_index (qtdemux, stream, start);
1848 stream->to_sample = stream->n_samples;
1849 GST_DEBUG_OBJECT (qtdemux, "moving data pointer to %" GST_TIME_FORMAT
1850 ", index: %u, pts %" GST_TIME_FORMAT, GST_TIME_ARGS (start), index,
1851 GST_TIME_ARGS (stream->samples[index].timestamp));
1853 index = gst_qtdemux_find_index (qtdemux, stream, stop);
1854 stream->to_sample = index;
1855 GST_DEBUG_OBJECT (qtdemux, "moving data pointer to %" GST_TIME_FORMAT
1856 ", index: %u, pts %" GST_TIME_FORMAT, GST_TIME_ARGS (stop), index,
1857 GST_TIME_ARGS (stream->samples[index].timestamp));
1860 /* we're at the right spot */
1861 if (index == stream->sample_index) {
1862 GST_DEBUG_OBJECT (qtdemux, "we are at the right index");
1866 /* find keyframe of the target index */
1867 kf_index = gst_qtdemux_find_keyframe (qtdemux, stream, index);
1869 /* if we move forwards, we don't have to go back to the previous
1870 * keyframe since we already sent that. We can also just jump to
1871 * the keyframe right before the target index if there is one. */
1872 if (index > stream->sample_index) {
1873 /* moving forwards check if we move past a keyframe */
1874 if (kf_index > stream->sample_index) {
1875 GST_DEBUG_OBJECT (qtdemux, "moving forwards to keyframe at %u (pts %"
1876 GST_TIME_FORMAT, kf_index,
1877 GST_TIME_ARGS (stream->samples[kf_index].timestamp));
1878 gst_qtdemux_move_stream (qtdemux, stream, kf_index);
1880 GST_DEBUG_OBJECT (qtdemux, "moving forwards, keyframe at %u (pts %"
1881 GST_TIME_FORMAT " already sent", kf_index,
1882 GST_TIME_ARGS (stream->samples[kf_index].timestamp));
1885 GST_DEBUG_OBJECT (qtdemux, "moving backwards to keyframe at %u (pts %"
1886 GST_TIME_FORMAT, kf_index,
1887 GST_TIME_ARGS (stream->samples[kf_index].timestamp));
1888 gst_qtdemux_move_stream (qtdemux, stream, kf_index);
1894 /* prepare to get the current sample of @stream, getting essential values.
1896 * This function will also prepare and send the segment when needed.
1898 * Return FALSE if the stream is EOS.
1901 gst_qtdemux_prepare_current_sample (GstQTDemux * qtdemux,
1902 QtDemuxStream * stream, guint64 * offset, guint * size, guint64 * timestamp,
1903 guint64 * duration, gboolean * keyframe)
1905 QtDemuxSample *sample;
1906 guint64 time_position;
1909 g_return_val_if_fail (stream != NULL, FALSE);
1911 time_position = stream->time_position;
1912 if (G_UNLIKELY (time_position == -1))
1915 seg_idx = stream->segment_index;
1916 if (G_UNLIKELY (seg_idx == -1)) {
1917 /* find segment corresponding to time_position if we are looking
1919 seg_idx = gst_qtdemux_find_segment (qtdemux, stream, time_position);
1921 /* nothing found, we're really eos */
1926 /* different segment, activate it, sample_index will be set. */
1927 if (G_UNLIKELY (stream->segment_index != seg_idx))
1928 gst_qtdemux_activate_segment (qtdemux, stream, seg_idx, time_position);
1930 GST_LOG_OBJECT (qtdemux, "segment active, index = %u of %u",
1931 stream->sample_index, stream->n_samples);
1933 /* send out pending buffers */
1934 while (stream->buffers) {
1935 GstBuffer *buffer = (GstBuffer *) stream->buffers->data;
1937 if (G_UNLIKELY (stream->discont)) {
1938 GST_LOG_OBJECT (qtdemux, "marking discont buffer");
1939 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1940 stream->discont = FALSE;
1942 gst_buffer_set_caps (buffer, stream->caps);
1944 gst_pad_push (stream->pad, buffer);
1946 stream->buffers = g_slist_delete_link (stream->buffers, stream->buffers);
1949 if (G_UNLIKELY (stream->sample_index >= stream->n_samples))
1952 /* now get the info for the sample we're at */
1953 sample = &stream->samples[stream->sample_index];
1955 *timestamp = sample->timestamp + sample->pts_offset;
1956 *offset = sample->offset;
1957 *size = sample->size;
1958 *duration = sample->duration;
1959 *keyframe = stream->all_keyframe || sample->keyframe;
1962 if (stream->padding) {
1963 *offset += stream->padding;
1964 *size -= stream->padding;
1972 stream->time_position = -1;
1977 /* move to the next sample in @stream.
1979 * Moves to the next segment when needed.
1982 gst_qtdemux_advance_sample (GstQTDemux * qtdemux, QtDemuxStream * stream)
1984 QtDemuxSample *sample;
1985 QtDemuxSegment *segment;
1987 if (G_UNLIKELY (stream->sample_index >= stream->to_sample)) {
1988 /* Mark the stream as EOS */
1989 GST_DEBUG_OBJECT (qtdemux, "reached max allowed sample %u, mark EOS",
1991 stream->time_position = -1;
1995 /* move to next sample */
1996 stream->sample_index++;
1998 /* get current segment */
1999 segment = &stream->segments[stream->segment_index];
2001 /* reached the last sample, we need the next segment */
2002 if (G_UNLIKELY (stream->sample_index >= stream->n_samples))
2005 /* get next sample */
2006 sample = &stream->samples[stream->sample_index];
2008 /* see if we are past the segment */
2009 if (G_UNLIKELY (sample->timestamp >= segment->media_stop))
2012 if (sample->timestamp >= segment->media_start) {
2013 /* inside the segment, update time_position, looks very familiar to
2014 * GStreamer segments, doesn't it? */
2015 stream->time_position =
2016 (sample->timestamp - segment->media_start) + segment->time;
2018 /* not yet in segment, time does not yet increment. This means
2019 * that we are still prerolling keyframes to the decoder so it can
2020 * decode the first sample of the segment. */
2021 stream->time_position = segment->time;
2025 /* move to the next segment */
2028 GST_DEBUG_OBJECT (qtdemux, "segment %d ended ", stream->segment_index);
2030 if (stream->segment_index == stream->n_segments - 1) {
2031 /* are we at the end of the last segment, we're EOS */
2032 stream->time_position = -1;
2034 /* else we're only at the end of the current segment */
2035 stream->time_position = segment->stop_time;
2037 /* make sure we select a new segment */
2038 stream->segment_index = -1;
2043 gst_qtdemux_sync_streams (GstQTDemux * demux)
2047 if (demux->n_streams <= 1)
2050 for (i = 0; i < demux->n_streams; i++) {
2051 QtDemuxStream *stream;
2052 GstClockTime end_time;
2054 stream = demux->streams[i];
2059 /* TODO advance time on subtitle streams here, if any some day */
2061 /* some clips/trailers may have unbalanced streams at the end,
2062 * so send EOS on shorter stream to prevent stalling others */
2064 /* do not mess with EOS if SEGMENT seeking */
2065 if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT)
2068 if (demux->pullbased) {
2069 /* loop mode is sample time based */
2070 if (stream->time_position != -1)
2073 /* push mode is byte position based */
2074 if (stream->samples[stream->n_samples - 1].offset >= demux->offset)
2078 if (stream->sent_eos)
2081 /* only act if some gap */
2082 end_time = stream->segments[stream->n_segments - 1].stop_time;
2083 GST_LOG_OBJECT (demux, "current position: %" GST_TIME_FORMAT
2084 ", stream end: %" GST_TIME_FORMAT, GST_TIME_ARGS (end_time),
2085 GST_TIME_ARGS (demux->segment.last_stop));
2086 if (end_time + 2 * GST_SECOND < demux->segment.last_stop) {
2087 GST_DEBUG_OBJECT (demux, "sending EOS for stream %s",
2088 GST_PAD_NAME (stream->pad));
2089 stream->sent_eos = TRUE;
2090 gst_pad_push_event (stream->pad, gst_event_new_eos ());
2095 /* UNEXPECTED and NOT_LINKED need to be combined. This means that we return:
2097 * GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
2098 * GST_FLOW_UNEXPECTED: when all pads UNEXPECTED or NOT_LINKED.
2100 static GstFlowReturn
2101 gst_qtdemux_combine_flows (GstQTDemux * demux, QtDemuxStream * stream,
2105 gboolean unexpected = FALSE, not_linked = TRUE;
2107 GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
2109 /* store the value */
2110 stream->last_ret = ret;
2112 for (i = 0; i < demux->n_streams; i++) {
2113 QtDemuxStream *ostream = demux->streams[i];
2115 ret = ostream->last_ret;
2117 /* no unexpected or unlinked, return */
2118 if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
2121 /* we check to see if we have at least 1 unexpected or all unlinked */
2122 unexpected |= (ret == GST_FLOW_UNEXPECTED);
2123 not_linked &= (ret == GST_FLOW_NOT_LINKED);
2126 /* when we get here, we all have unlinked or unexpected */
2128 ret = GST_FLOW_NOT_LINKED;
2129 else if (unexpected)
2130 ret = GST_FLOW_UNEXPECTED;
2132 GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
2136 /* the input buffer metadata must be writable. Returns NULL when the buffer is
2137 * completely cliped */
2139 gst_qtdemux_clip_buffer (GstQTDemux * qtdemux, QtDemuxStream * stream,
2142 gint64 start, stop, cstart, cstop, diff;
2143 GstClockTime timestamp = GST_CLOCK_TIME_NONE, duration = GST_CLOCK_TIME_NONE;
2146 gint num_rate, denom_rate;
2150 data = GST_BUFFER_DATA (buf);
2151 size = GST_BUFFER_SIZE (buf);
2153 /* depending on the type, setup the clip parameters */
2154 if (stream->subtype == FOURCC_soun) {
2155 frame_size = stream->bytes_per_frame;
2156 num_rate = GST_SECOND;
2157 denom_rate = (gint) stream->rate;
2159 } else if (stream->subtype == FOURCC_vide) {
2161 num_rate = stream->fps_n;
2162 denom_rate = stream->fps_d;
2167 /* we can only clip if we have a valid timestamp */
2168 timestamp = GST_BUFFER_TIMESTAMP (buf);
2169 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)))
2172 if (G_LIKELY (GST_BUFFER_DURATION_IS_VALID (buf))) {
2173 duration = GST_BUFFER_DURATION (buf);
2176 gst_util_uint64_scale_int (size / frame_size, num_rate, denom_rate);
2180 stop = start + duration;
2182 if (G_UNLIKELY (!gst_segment_clip (&stream->segment, GST_FORMAT_TIME,
2183 start, stop, &cstart, &cstop)))
2186 /* see if some clipping happened */
2187 diff = cstart - start;
2193 /* bring clipped time to samples and to bytes */
2194 diff = gst_util_uint64_scale_int (diff, denom_rate, num_rate);
2197 GST_DEBUG_OBJECT (qtdemux, "clipping start to %" GST_TIME_FORMAT " %"
2198 G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
2204 diff = stop - cstop;
2209 /* bring clipped time to samples and then to bytes */
2210 diff = gst_util_uint64_scale_int (diff, denom_rate, num_rate);
2213 GST_DEBUG_OBJECT (qtdemux, "clipping stop to %" GST_TIME_FORMAT " %"
2214 G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
2220 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2221 GST_BUFFER_DURATION (buf) = duration;
2222 GST_BUFFER_SIZE (buf) = size;
2223 GST_BUFFER_DATA (buf) = data;
2227 /* dropped buffer */
2230 GST_DEBUG_OBJECT (qtdemux, "unknown stream type");
2235 GST_DEBUG_OBJECT (qtdemux, "no timestamp on buffer");
2240 GST_DEBUG_OBJECT (qtdemux, "clipped buffer");
2241 gst_buffer_unref (buf);
2246 static GstFlowReturn
2247 gst_qtdemux_loop_state_movie (GstQTDemux * qtdemux)
2249 GstFlowReturn ret = GST_FLOW_OK;
2250 GstBuffer *buf = NULL;
2251 QtDemuxStream *stream;
2254 guint64 timestamp = GST_CLOCK_TIME_NONE;
2255 guint64 duration = 0;
2256 gboolean keyframe = FALSE;
2261 gst_qtdemux_push_pending_newsegment (qtdemux);
2263 /* Figure out the next stream sample to output, min_time is expressed in
2264 * global time and runs over the edit list segments. */
2265 min_time = G_MAXUINT64;
2267 for (i = 0; i < qtdemux->n_streams; i++) {
2270 stream = qtdemux->streams[i];
2271 position = stream->time_position;
2273 /* position of -1 is EOS */
2274 if (position != -1 && position < min_time) {
2275 min_time = position;
2280 if (G_UNLIKELY (index == -1)) {
2281 GST_DEBUG_OBJECT (qtdemux, "all streams are EOS");
2285 /* check for segment end */
2286 if (G_UNLIKELY (qtdemux->segment.stop != -1
2287 && qtdemux->segment.stop < min_time)) {
2288 GST_DEBUG_OBJECT (qtdemux, "we reached the end of our segment.");
2292 stream = qtdemux->streams[index];
2294 /* fetch info for the current sample of this stream */
2295 if (G_UNLIKELY (!gst_qtdemux_prepare_current_sample (qtdemux, stream, &offset,
2296 &size, ×tamp, &duration, &keyframe)))
2299 GST_LOG_OBJECT (qtdemux,
2300 "pushing from stream %d, offset %" G_GUINT64_FORMAT
2301 ", size %d, timestamp=%" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2302 index, offset, size, GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
2304 /* hmm, empty sample, skip and move to next sample */
2305 if (G_UNLIKELY (size <= 0))
2308 /* last pushed sample was out of boundary, goto next sample */
2309 if (G_UNLIKELY (stream->last_ret == GST_FLOW_UNEXPECTED))
2312 GST_LOG_OBJECT (qtdemux, "reading %d bytes @ %" G_GUINT64_FORMAT, size,
2315 ret = gst_qtdemux_pull_atom (qtdemux, offset, size, &buf);
2316 if (G_UNLIKELY (ret != GST_FLOW_OK))
2319 if (G_UNLIKELY (stream->fourcc == FOURCC_rtsp)) {
2323 url = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
2325 /* we have RTSP redirect now */
2326 m = gst_message_new_element (GST_OBJECT_CAST (qtdemux),
2327 gst_structure_new ("redirect",
2328 "new-location", G_TYPE_STRING, url, NULL));
2331 gst_element_post_message (GST_ELEMENT_CAST (qtdemux), m);
2334 qtdemux->last_ts = min_time;
2335 if (qtdemux->segment.rate >= 0) {
2336 gst_segment_set_last_stop (&qtdemux->segment, GST_FORMAT_TIME, min_time);
2337 gst_qtdemux_sync_streams (qtdemux);
2339 if (G_LIKELY (stream->pad)) {
2340 /* we're going to modify the metadata */
2341 buf = gst_buffer_make_metadata_writable (buf);
2343 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2344 GST_BUFFER_DURATION (buf) = duration;
2345 GST_BUFFER_OFFSET (buf) = -1;
2346 GST_BUFFER_OFFSET_END (buf) = -1;
2348 if (stream->need_clip)
2349 buf = gst_qtdemux_clip_buffer (qtdemux, stream, buf);
2354 if (stream->discont) {
2355 GST_LOG_OBJECT (qtdemux, "marking discont buffer");
2356 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2357 stream->discont = FALSE;
2361 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
2363 gst_buffer_set_caps (buf, stream->caps);
2365 GST_LOG_OBJECT (qtdemux,
2366 "Pushing buffer with time %" GST_TIME_FORMAT ", duration %"
2367 GST_TIME_FORMAT " on pad %s",
2368 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
2369 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_PAD_NAME (stream->pad));
2371 ret = gst_pad_push (stream->pad, buf);
2373 GST_DEBUG_OBJECT (qtdemux, "No output pad for stream, ignoring");
2374 gst_buffer_unref (buf);
2379 ret = gst_qtdemux_combine_flows (qtdemux, stream, ret);
2380 /* ignore unlinked, we will not push on the pad anymore and we will EOS when
2381 * we have no more data for the pad to push */
2382 if (ret == GST_FLOW_UNEXPECTED)
2386 gst_qtdemux_advance_sample (qtdemux, stream);
2394 GST_DEBUG_OBJECT (qtdemux, "No samples left for any streams - EOS");
2395 ret = GST_FLOW_UNEXPECTED;
2400 GST_DEBUG_OBJECT (qtdemux, "No samples left for stream");
2401 /* EOS will be raised if all are EOS */
2408 gst_qtdemux_loop (GstPad * pad)
2410 GstQTDemux *qtdemux;
2414 qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
2416 cur_offset = qtdemux->offset;
2417 GST_LOG_OBJECT (qtdemux, "loop at position %" G_GUINT64_FORMAT ", state %d",
2418 cur_offset, qtdemux->state);
2420 switch (qtdemux->state) {
2421 case QTDEMUX_STATE_INITIAL:
2422 case QTDEMUX_STATE_HEADER:
2423 ret = gst_qtdemux_loop_state_header (qtdemux);
2425 case QTDEMUX_STATE_MOVIE:
2426 ret = gst_qtdemux_loop_state_movie (qtdemux);
2427 if (qtdemux->segment.rate < 0 && ret == GST_FLOW_UNEXPECTED) {
2428 ret = gst_qtdemux_seek_to_previous_keyframe (qtdemux);
2436 /* if something went wrong, pause */
2437 if (ret != GST_FLOW_OK)
2441 gst_object_unref (qtdemux);
2447 GST_ELEMENT_ERROR (qtdemux, STREAM, FAILED,
2448 (NULL), ("streaming stopped, invalid state"));
2449 qtdemux->segment_running = FALSE;
2450 gst_pad_pause_task (pad);
2451 gst_qtdemux_push_event (qtdemux, gst_event_new_eos ());
2456 const gchar *reason = gst_flow_get_name (ret);
2458 GST_LOG_OBJECT (qtdemux, "pausing task, reason %s", reason);
2460 qtdemux->segment_running = FALSE;
2461 gst_pad_pause_task (pad);
2463 /* fatal errors need special actions */
2464 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2466 if (ret == GST_FLOW_UNEXPECTED) {
2467 if (qtdemux->n_streams == 0) {
2468 /* we have no streams, post an error */
2469 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
2470 (_("This file contains no playable streams.")),
2471 ("no known streams found"));
2473 if (qtdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2476 /* FIXME: I am not sure this is the right fix. If the sinks are
2477 * supposed to detect the segment is complete and accumulate
2478 * automatically, it does not seem to work here. Need more work */
2479 qtdemux->segment_running = TRUE;
2481 if ((stop = qtdemux->segment.stop) == -1)
2482 stop = qtdemux->segment.duration;
2484 if (qtdemux->segment.rate >= 0) {
2485 GST_LOG_OBJECT (qtdemux, "Sending segment done, at end of segment");
2486 gst_element_post_message (GST_ELEMENT_CAST (qtdemux),
2487 gst_message_new_segment_done (GST_OBJECT_CAST (qtdemux),
2488 GST_FORMAT_TIME, stop));
2490 /* For Reverse Playback */
2491 GST_LOG_OBJECT (qtdemux,
2492 "Sending segment done, at start of segment");
2493 gst_element_post_message (GST_ELEMENT_CAST (qtdemux),
2494 gst_message_new_segment_done (GST_OBJECT_CAST (qtdemux),
2495 GST_FORMAT_TIME, qtdemux->segment.start));
2498 GST_LOG_OBJECT (qtdemux, "Sending EOS at end of segment");
2499 gst_qtdemux_push_event (qtdemux, gst_event_new_eos ());
2502 GST_ELEMENT_ERROR (qtdemux, STREAM, FAILED,
2503 (NULL), ("streaming stopped, reason %s", reason));
2504 gst_qtdemux_push_event (qtdemux, gst_event_new_eos ());
2514 * Returns the size of the first entry at the current offset.
2515 * If -1, there are none (which means EOS or empty file).
2518 next_entry_size (GstQTDemux * demux)
2520 QtDemuxStream *stream;
2523 guint64 smalloffs = (guint64) - 1;
2524 QtDemuxSample *sample;
2526 GST_LOG_OBJECT (demux, "Finding entry at offset %lld", demux->offset);
2528 for (i = 0; i < demux->n_streams; i++) {
2529 stream = demux->streams[i];
2531 if (stream->sample_index == -1)
2532 stream->sample_index = 0;
2534 if (stream->sample_index >= stream->n_samples) {
2535 GST_LOG_OBJECT (demux, "stream %d samples exhausted", i);
2539 sample = &stream->samples[stream->sample_index];
2541 GST_LOG_OBJECT (demux,
2542 "Checking Stream %d (sample_index:%d / offset:%lld / size:%d)",
2543 i, stream->sample_index, sample->offset, sample->size);
2545 if (((smalloffs == -1)
2546 || (sample->offset < smalloffs)) && (sample->size)) {
2548 smalloffs = sample->offset;
2552 GST_LOG_OBJECT (demux, "stream %d offset %lld demux->offset :%lld",
2553 smallidx, smalloffs, demux->offset);
2558 stream = demux->streams[smallidx];
2559 sample = &stream->samples[stream->sample_index];
2561 if (sample->offset >= demux->offset) {
2562 demux->todrop = sample->offset - demux->offset;
2563 return sample->size + demux->todrop;
2566 GST_DEBUG_OBJECT (demux, "There wasn't any entry at offset %lld",
2572 gst_qtdemux_post_progress (GstQTDemux * demux, gint num, gint denom)
2574 gint perc = (gint) ((gdouble) num * 100.0 / (gdouble) denom);
2576 gst_element_post_message (GST_ELEMENT_CAST (demux),
2577 gst_message_new_element (GST_OBJECT_CAST (demux),
2578 gst_structure_new ("progress", "percent", G_TYPE_INT, perc, NULL)));
2581 /* FIXME, unverified after edit list updates */
2582 static GstFlowReturn
2583 gst_qtdemux_chain (GstPad * sinkpad, GstBuffer * inbuf)
2586 GstFlowReturn ret = GST_FLOW_OK;
2588 demux = GST_QTDEMUX (gst_pad_get_parent (sinkpad));
2590 gst_adapter_push (demux->adapter, inbuf);
2592 /* we never really mean to buffer that much */
2593 if (demux->neededbytes == -1)
2596 GST_DEBUG_OBJECT (demux, "pushing in inbuf %p, neededbytes:%u, available:%u",
2597 inbuf, demux->neededbytes, gst_adapter_available (demux->adapter));
2599 while (((gst_adapter_available (demux->adapter)) >= demux->neededbytes) &&
2600 (ret == GST_FLOW_OK)) {
2602 GST_DEBUG_OBJECT (demux,
2603 "state:%d , demux->neededbytes:%d, demux->offset:%lld", demux->state,
2604 demux->neededbytes, demux->offset);
2606 switch (demux->state) {
2607 case QTDEMUX_STATE_INITIAL:{
2612 data = gst_adapter_peek (demux->adapter, demux->neededbytes);
2614 /* get fourcc/length, set neededbytes */
2615 extract_initial_length_and_fourcc ((guint8 *) data, &size, &fourcc);
2616 GST_DEBUG_OBJECT (demux, "Peeking found [%" GST_FOURCC_FORMAT "] "
2617 "size: %" G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc), size);
2619 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
2620 (_("This file is invalid and cannot be played.")),
2621 ("initial atom '%" GST_FOURCC_FORMAT "' has empty length",
2622 GST_FOURCC_ARGS (fourcc)));
2623 ret = GST_FLOW_ERROR;
2626 if (fourcc == FOURCC_mdat) {
2627 if (demux->n_streams > 0) {
2628 demux->state = QTDEMUX_STATE_MOVIE;
2629 demux->neededbytes = next_entry_size (demux);
2634 /* there may be multiple mdat (or alike) buffers */
2636 if (demux->mdatbuffer)
2637 bs = GST_BUFFER_SIZE (demux->mdatbuffer);
2640 if (size + bs > 10 * (1 << 20))
2642 demux->state = QTDEMUX_STATE_BUFFER_MDAT;
2643 demux->neededbytes = size;
2644 if (!demux->mdatbuffer)
2645 demux->mdatoffset = demux->offset;
2647 } else if (G_UNLIKELY (size > QTDEMUX_MAX_ATOM_SIZE)) {
2648 GST_ELEMENT_ERROR (demux, STREAM, DECODE,
2649 (_("This file is invalid and cannot be played.")),
2650 ("atom %" GST_FOURCC_FORMAT " has bogus size %" G_GUINT64_FORMAT,
2651 GST_FOURCC_ARGS (fourcc), size));
2652 ret = GST_FLOW_ERROR;
2655 /* this means we already started buffering and still no moov header,
2656 * let's continue buffering everything till we get moov */
2657 if (demux->mdatbuffer && (fourcc != FOURCC_moov))
2659 demux->neededbytes = size;
2660 demux->state = QTDEMUX_STATE_HEADER;
2664 case QTDEMUX_STATE_HEADER:{
2668 GST_DEBUG_OBJECT (demux, "In header");
2670 data = gst_adapter_peek (demux->adapter, demux->neededbytes);
2672 /* parse the header */
2673 extract_initial_length_and_fourcc (data, NULL, &fourcc);
2674 if (fourcc == FOURCC_moov) {
2675 GST_DEBUG_OBJECT (demux, "Parsing [moov]");
2677 qtdemux_parse_moov (demux, data, demux->neededbytes);
2678 qtdemux_node_dump (demux, demux->moov_node);
2679 qtdemux_parse_tree (demux);
2681 g_node_destroy (demux->moov_node);
2682 demux->moov_node = NULL;
2683 GST_DEBUG_OBJECT (demux, "Finished parsing the header");
2684 } else if (fourcc == FOURCC_ftyp) {
2685 GST_DEBUG_OBJECT (demux, "Parsing [ftyp]");
2686 qtdemux_parse_ftyp (demux, data, demux->neededbytes);
2688 GST_WARNING_OBJECT (demux,
2689 "Unknown fourcc while parsing header : %" GST_FOURCC_FORMAT,
2690 GST_FOURCC_ARGS (fourcc));
2691 /* Let's jump that one and go back to initial state */
2694 if (demux->mdatbuffer && demux->n_streams) {
2697 /* the mdat was before the header */
2698 GST_DEBUG_OBJECT (demux, "We have n_streams:%d and mdatbuffer:%p",
2699 demux->n_streams, demux->mdatbuffer);
2700 /* restore our adapter/offset view of things with upstream;
2701 * put preceding buffered data ahead of current moov data.
2702 * This should also handle evil mdat, moov, mdat cases and alike */
2703 buf = gst_adapter_take_buffer (demux->adapter,
2704 gst_adapter_available (demux->adapter));
2705 gst_adapter_clear (demux->adapter);
2706 gst_adapter_push (demux->adapter, demux->mdatbuffer);
2707 gst_adapter_push (demux->adapter, buf);
2708 demux->mdatbuffer = NULL;
2709 demux->offset = demux->mdatoffset;
2710 demux->neededbytes = next_entry_size (demux);
2711 demux->state = QTDEMUX_STATE_MOVIE;
2713 GST_DEBUG_OBJECT (demux, "Carrying on normally");
2714 gst_adapter_flush (demux->adapter, demux->neededbytes);
2715 demux->offset += demux->neededbytes;
2716 demux->neededbytes = 16;
2717 demux->state = QTDEMUX_STATE_INITIAL;
2722 case QTDEMUX_STATE_BUFFER_MDAT:{
2725 GST_DEBUG_OBJECT (demux, "Got our buffer at offset %lld",
2727 buf = gst_adapter_take_buffer (demux->adapter, demux->neededbytes);
2728 GST_DEBUG_OBJECT (demux, "mdatbuffer starts with %" GST_FOURCC_FORMAT,
2729 GST_FOURCC_ARGS (QT_FOURCC (GST_BUFFER_DATA (buf) + 4)));
2730 if (demux->mdatbuffer)
2731 demux->mdatbuffer = gst_buffer_join (demux->mdatbuffer, buf);
2733 demux->mdatbuffer = buf;
2734 demux->offset += demux->neededbytes;
2735 demux->neededbytes = 16;
2736 demux->state = QTDEMUX_STATE_INITIAL;
2737 gst_qtdemux_post_progress (demux, 1, 1);
2741 case QTDEMUX_STATE_MOVIE:{
2743 QtDemuxStream *stream = NULL;
2746 GST_DEBUG_OBJECT (demux, "BEGIN // in MOVIE for offset %lld",
2749 if (demux->todrop) {
2750 GST_LOG_OBJECT (demux, "Dropping %d bytes", demux->todrop);
2751 gst_adapter_flush (demux->adapter, demux->todrop);
2752 demux->neededbytes -= demux->todrop;
2753 demux->offset += demux->todrop;
2757 /* initial newsegment sent here after having added pads,
2758 * possible others in sink_event */
2759 if (G_UNLIKELY (demux->last_ts == GST_CLOCK_TIME_NONE)) {
2760 gst_qtdemux_push_event (demux,
2761 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
2762 0, GST_CLOCK_TIME_NONE, 0));
2763 /* clear to send tags on all streams */
2764 for (i = 0; i < demux->n_streams; i++) {
2765 gst_qtdemux_push_tags (demux, demux->streams[i]);
2769 /* Figure out which stream this is packet belongs to */
2770 for (i = 0; i < demux->n_streams; i++) {
2771 stream = demux->streams[i];
2772 if (stream->sample_index >= stream->n_samples)
2774 GST_LOG_OBJECT (demux,
2775 "Checking stream %d (sample_index:%d / offset:%lld / size:%d)",
2776 i, stream->sample_index,
2777 stream->samples[stream->sample_index].offset,
2778 stream->samples[stream->sample_index].size);
2780 if (stream->samples[stream->sample_index].offset == demux->offset)
2784 if (G_UNLIKELY (stream == NULL || i == demux->n_streams))
2785 goto unknown_stream;
2787 /* Put data in a buffer, set timestamps, caps, ... */
2788 outbuf = gst_adapter_take_buffer (demux->adapter, demux->neededbytes);
2789 GST_DEBUG_OBJECT (demux, "stream : %" GST_FOURCC_FORMAT,
2790 GST_FOURCC_ARGS (stream->fourcc));
2792 g_return_val_if_fail (outbuf != NULL, GST_FLOW_ERROR);
2794 if (stream->samples[stream->sample_index].pts_offset) {
2795 demux->last_ts = stream->samples[stream->sample_index].timestamp;
2796 GST_BUFFER_TIMESTAMP (outbuf) = demux->last_ts +
2797 stream->samples[stream->sample_index].pts_offset;
2799 GST_BUFFER_TIMESTAMP (outbuf) =
2800 stream->samples[stream->sample_index].timestamp;
2801 demux->last_ts = GST_BUFFER_TIMESTAMP (outbuf);
2803 GST_BUFFER_DURATION (outbuf) =
2804 stream->samples[stream->sample_index].duration;
2805 if (!stream->all_keyframe &&
2806 !stream->samples[stream->sample_index].keyframe)
2807 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
2809 /* position reporting */
2810 gst_segment_set_last_stop (&demux->segment, GST_FORMAT_TIME,
2812 gst_qtdemux_sync_streams (demux);
2816 GST_LOG_OBJECT (demux,
2817 "Pushing buffer with time %" GST_TIME_FORMAT " on pad %p",
2818 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), stream->pad);
2819 gst_buffer_set_caps (outbuf, stream->caps);
2820 ret = gst_pad_push (stream->pad, outbuf);
2822 gst_buffer_unref (outbuf);
2827 ret = gst_qtdemux_combine_flows (demux, stream, ret);
2829 stream->sample_index++;
2831 /* update current offset and figure out size of next buffer */
2832 GST_LOG_OBJECT (demux, "increasing offset %" G_GUINT64_FORMAT " by %u",
2833 demux->offset, demux->neededbytes);
2834 demux->offset += demux->neededbytes;
2835 GST_LOG_OBJECT (demux, "offset is now %lld", demux->offset);
2837 if ((demux->neededbytes = next_entry_size (demux)) == -1)
2846 /* when buffering movie data, at least show user something is happening */
2847 if (ret == GST_FLOW_OK && demux->state == QTDEMUX_STATE_BUFFER_MDAT &&
2848 gst_adapter_available (demux->adapter) <= demux->neededbytes) {
2849 gst_qtdemux_post_progress (demux, gst_adapter_available (demux->adapter),
2850 demux->neededbytes);
2853 gst_object_unref (demux);
2860 GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL), ("unknown stream found"));
2861 ret = GST_FLOW_ERROR;
2866 GST_DEBUG_OBJECT (demux, "no next entry, EOS");
2867 ret = GST_FLOW_UNEXPECTED;
2872 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2873 (NULL), ("qtdemuxer invalid state %d", demux->state));
2874 ret = GST_FLOW_ERROR;
2879 GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2880 (NULL), ("no 'moov' atom withing first 10 MB"));
2881 ret = GST_FLOW_ERROR;
2887 qtdemux_sink_activate (GstPad * sinkpad)
2889 if (gst_pad_check_pull_range (sinkpad))
2890 return gst_pad_activate_pull (sinkpad, TRUE);
2892 return gst_pad_activate_push (sinkpad, TRUE);
2896 qtdemux_sink_activate_pull (GstPad * sinkpad, gboolean active)
2898 GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
2901 demux->pullbased = TRUE;
2902 demux->segment_running = TRUE;
2903 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_qtdemux_loop,
2906 demux->segment_running = FALSE;
2907 return gst_pad_stop_task (sinkpad);
2912 qtdemux_sink_activate_push (GstPad * sinkpad, gboolean active)
2914 GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
2916 demux->pullbased = FALSE;
2923 qtdemux_zalloc (void *opaque, unsigned int items, unsigned int size)
2925 return g_malloc (items * size);
2929 qtdemux_zfree (void *opaque, void *addr)
2935 qtdemux_inflate (void *z_buffer, guint z_length, guint length)
2941 z = g_new0 (z_stream, 1);
2942 z->zalloc = qtdemux_zalloc;
2943 z->zfree = qtdemux_zfree;
2946 z->next_in = z_buffer;
2947 z->avail_in = z_length;
2949 buffer = (guint8 *) g_malloc (length);
2950 ret = inflateInit (z);
2951 while (z->avail_in > 0) {
2952 if (z->avail_out == 0) {
2954 buffer = (guint8 *) g_realloc (buffer, length);
2955 z->next_out = buffer + z->total_out;
2956 z->avail_out = 1024;
2958 ret = inflate (z, Z_SYNC_FLUSH);
2962 if (ret != Z_STREAM_END) {
2963 g_warning ("inflate() returned %d", ret);
2969 #endif /* HAVE_ZLIB */
2972 qtdemux_parse_moov (GstQTDemux * qtdemux, const guint8 * buffer, guint length)
2976 qtdemux->moov_node = g_node_new ((guint8 *) buffer);
2978 GST_DEBUG_OBJECT (qtdemux, "parsing 'moov' atom");
2979 qtdemux_parse_node (qtdemux, qtdemux->moov_node, buffer, length);
2981 cmov = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_cmov);
2987 dcom = qtdemux_tree_get_child_by_type (cmov, FOURCC_dcom);
2988 cmvd = qtdemux_tree_get_child_by_type (cmov, FOURCC_cmvd);
2989 if (dcom == NULL || cmvd == NULL)
2990 goto invalid_compression;
2992 method = QT_FOURCC ((guint8 *) dcom->data + 8);
2995 case GST_MAKE_FOURCC ('z', 'l', 'i', 'b'):{
2996 guint uncompressed_length;
2997 guint compressed_length;
3000 uncompressed_length = QT_UINT32 ((guint8 *) cmvd->data + 8);
3001 compressed_length = QT_UINT32 ((guint8 *) cmvd->data + 4) - 12;
3002 GST_LOG ("length = %u", uncompressed_length);
3005 (guint8 *) qtdemux_inflate ((guint8 *) cmvd->data + 12,
3006 compressed_length, uncompressed_length);
3008 qtdemux->moov_node_compressed = qtdemux->moov_node;
3009 qtdemux->moov_node = g_node_new (buf);
3011 qtdemux_parse_node (qtdemux, qtdemux->moov_node, buf,
3012 uncompressed_length);
3015 #endif /* HAVE_ZLIB */
3017 GST_WARNING_OBJECT (qtdemux, "unknown or unhandled header compression "
3018 "type %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (method));
3025 invalid_compression:
3027 GST_ERROR_OBJECT (qtdemux, "invalid compressed header");
3033 qtdemux_parse_container (GstQTDemux * qtdemux, GNode * node, const guint8 * buf,
3036 while (G_UNLIKELY (buf < end)) {
3040 if (G_UNLIKELY (buf + 4 > end)) {
3041 GST_LOG_OBJECT (qtdemux, "buffer overrun");
3044 len = QT_UINT32 (buf);
3045 if (G_UNLIKELY (len == 0)) {
3046 GST_LOG_OBJECT (qtdemux, "empty container");
3049 if (G_UNLIKELY (len < 8)) {
3050 GST_WARNING_OBJECT (qtdemux, "length too short (%d < 8)", len);
3053 if (G_UNLIKELY (len > (end - buf))) {
3054 GST_WARNING_OBJECT (qtdemux, "length too long (%d > %d)", len, end - buf);
3058 child = g_node_new ((guint8 *) buf);
3059 g_node_append (node, child);
3060 qtdemux_parse_node (qtdemux, child, buf, len);
3068 qtdemux_parse_theora_extension (GstQTDemux * qtdemux, QtDemuxStream * stream,
3071 int len = QT_UINT32 (xdxt->data);
3072 guint8 *buf = xdxt->data;
3073 guint8 *end = buf + len;
3076 /* skip size and type */
3084 size = QT_UINT32 (buf);
3085 type = QT_FOURCC (buf + 4);
3087 GST_LOG_OBJECT (qtdemux, "%p %p", buf, end);
3089 if (buf + size > end || size <= 0)
3095 GST_WARNING_OBJECT (qtdemux, "have cookie %" GST_FOURCC_FORMAT,
3096 GST_FOURCC_ARGS (type));
3100 buffer = gst_buffer_new_and_alloc (size);
3101 memcpy (GST_BUFFER_DATA (buffer), buf, size);
3102 stream->buffers = g_slist_append (stream->buffers, buffer);
3103 GST_LOG_OBJECT (qtdemux, "parsing theora header");
3106 buffer = gst_buffer_new_and_alloc (size);
3107 memcpy (GST_BUFFER_DATA (buffer), buf, size);
3108 stream->buffers = g_slist_append (stream->buffers, buffer);
3109 GST_LOG_OBJECT (qtdemux, "parsing theora comment");
3112 buffer = gst_buffer_new_and_alloc (size);
3113 memcpy (GST_BUFFER_DATA (buffer), buf, size);
3114 stream->buffers = g_slist_append (stream->buffers, buffer);
3115 GST_LOG_OBJECT (qtdemux, "parsing theora codebook");
3118 GST_WARNING_OBJECT (qtdemux,
3119 "unknown theora cookie %" GST_FOURCC_FORMAT,
3120 GST_FOURCC_ARGS (type));
3129 qtdemux_parse_node (GstQTDemux * qtdemux, GNode * node, const guint8 * buffer,
3133 guint32 node_length = 0;
3134 const QtNodeType *type;
3137 GST_LOG_OBJECT (qtdemux, "qtdemux_parse buffer %p length %u", buffer, length);
3139 if (G_UNLIKELY (length < 8))
3140 goto not_enough_data;
3142 node_length = QT_UINT32 (buffer);
3143 fourcc = QT_FOURCC (buffer + 4);
3145 /* ignore empty nodes */
3146 if (G_UNLIKELY (fourcc == 0 || node_length == 8))
3149 type = qtdemux_type_get (fourcc);
3151 end = buffer + length;
3153 GST_LOG_OBJECT (qtdemux,
3154 "parsing '%" GST_FOURCC_FORMAT "', length=%u, name '%s'",
3155 GST_FOURCC_ARGS (fourcc), node_length, type->name);
3157 if (node_length > length)
3158 goto broken_atom_size;
3160 if (type->flags & QT_FLAG_CONTAINER) {
3161 qtdemux_parse_container (qtdemux, node, buffer + 8, end);
3166 if (node_length < 20) {
3167 GST_LOG_OBJECT (qtdemux, "skipping small stsd box");
3170 GST_DEBUG_OBJECT (qtdemux,
3171 "parsing stsd (sample table, sample description) atom");
3172 qtdemux_parse_container (qtdemux, node, buffer + 16, end);
3181 /* small boxes are also inside wave inside the mp4a box */
3182 GST_LOG_OBJECT (qtdemux, "skipping small mp4a box");
3185 version = QT_UINT32 (buffer + 16);
3187 GST_DEBUG_OBJECT (qtdemux, "mp4a version 0x%08x", version);
3189 /* parse any esds descriptors */
3201 GST_WARNING_OBJECT (qtdemux, "unhandled mp4a version 0x%08x",
3207 qtdemux_parse_container (qtdemux, node, buffer + offset, end);
3216 GST_DEBUG_OBJECT (qtdemux, "parsing in mp4v");
3217 version = QT_UINT32 (buffer + 16);
3218 GST_DEBUG_OBJECT (qtdemux, "version %08x", version);
3219 if (1 || version == 0x00000000) {
3220 buf = buffer + 0x32;
3222 /* FIXME Quicktime uses PASCAL string while
3223 * the iso format uses C strings. Check the file
3224 * type before attempting to parse the string here. */
3225 tlen = QT_UINT8 (buf);
3226 GST_DEBUG_OBJECT (qtdemux, "tlen = %d", tlen);
3228 GST_DEBUG_OBJECT (qtdemux, "string = %.*s", tlen, (char *) buf);
3229 /* the string has a reserved space of 32 bytes so skip
3230 * the remaining 31 */
3232 buf += 4; /* and 4 bytes reserved */
3234 GST_MEMDUMP_OBJECT (qtdemux, "mp4v", buf, end - buf);
3236 qtdemux_parse_container (qtdemux, node, buf, end);
3242 GST_MEMDUMP_OBJECT (qtdemux, "avc1", buffer, end - buffer);
3243 qtdemux_parse_container (qtdemux, node, buffer + 0x56, end);
3248 qtdemux_parse_container (qtdemux, node, buffer + 86, end);
3253 GST_DEBUG_OBJECT (qtdemux, "parsing meta atom");
3254 qtdemux_parse_container (qtdemux, node, buffer + 12, end);
3262 version = QT_UINT32 (buffer + 12);
3263 GST_DEBUG_OBJECT (qtdemux, "parsing XiTh atom version 0x%08x", version);
3270 GST_DEBUG_OBJECT (qtdemux, "unknown version 0x%08x", version);
3275 qtdemux_parse_container (qtdemux, node, buffer + offset, end);
3280 qtdemux_parse_container (qtdemux, node, buffer + 0x34, end);
3284 if (!strcmp (type->name, "unknown"))
3285 GST_MEMDUMP ("Unknown tag", buffer + 4, end - buffer - 4);
3289 GST_LOG_OBJECT (qtdemux, "parsed '%" GST_FOURCC_FORMAT,
3290 GST_FOURCC_ARGS (fourcc));
3296 GST_ELEMENT_ERROR (qtdemux, STREAM, DEMUX,
3297 (_("This file is corrupt and cannot be played.")),
3298 ("Not enough data for an atom header, got only %u bytes", length));
3303 GST_ELEMENT_ERROR (qtdemux, STREAM, DEMUX,
3304 (_("This file is corrupt and cannot be played.")),
3305 ("Atom '%" GST_FOURCC_FORMAT "' has size of %u bytes, but we have only "
3306 "%u bytes available.", GST_FOURCC_ARGS (fourcc), node_length,
3313 qtdemux_tree_get_child_by_type (GNode * node, guint32 fourcc)
3317 guint32 child_fourcc;
3319 for (child = g_node_first_child (node); child;
3320 child = g_node_next_sibling (child)) {
3321 buffer = (guint8 *) child->data;
3323 child_fourcc = QT_FOURCC (buffer + 4);
3325 if (G_UNLIKELY (child_fourcc == fourcc)) {
3333 qtdemux_tree_get_child_by_type_full (GNode * node, guint32 fourcc,
3334 QtAtomParser * parser)
3338 guint32 child_fourcc, child_len;
3340 for (child = g_node_first_child (node); child;
3341 child = g_node_next_sibling (child)) {
3342 buffer = (guint8 *) child->data;
3344 child_len = QT_UINT32 (buffer);
3345 child_fourcc = QT_FOURCC (buffer + 4);
3347 if (G_UNLIKELY (child_fourcc == fourcc)) {
3348 if (G_UNLIKELY (child_len < (4 + 4)))
3350 /* FIXME: must verify if atom length < parent atom length */
3351 qt_atom_parser_init (parser, buffer + (4 + 4), child_len - (4 + 4));
3359 qtdemux_tree_get_sibling_by_type (GNode * node, guint32 fourcc)
3363 guint32 child_fourcc;
3365 for (child = g_node_next_sibling (node); child;
3366 child = g_node_next_sibling (child)) {
3367 buffer = (guint8 *) child->data;
3369 child_fourcc = QT_FOURCC (buffer + 4);
3371 if (child_fourcc == fourcc) {
3379 gst_qtdemux_add_stream (GstQTDemux * qtdemux,
3380 QtDemuxStream * stream, GstTagList * list)
3382 if (qtdemux->n_streams >= GST_QTDEMUX_MAX_STREAMS)
3383 goto too_many_streams;
3385 if (stream->subtype == FOURCC_vide) {
3386 gchar *name = g_strdup_printf ("video_%02d", qtdemux->n_video_streams);
3389 gst_pad_new_from_static_template (&gst_qtdemux_videosrc_template, name);
3392 /* fps is calculated base on the duration of the first frames since
3393 * qt does not have a fixed framerate. */
3394 if ((stream->n_samples == 1) && (stream->min_duration == 0)) {
3399 stream->fps_n = stream->timescale;
3400 if (stream->min_duration == 0)
3403 stream->fps_d = stream->min_duration;
3408 gint depth, palette_count;
3409 const guint32 *palette_data = NULL;
3411 gst_caps_set_simple (stream->caps,
3412 "width", G_TYPE_INT, stream->width,
3413 "height", G_TYPE_INT, stream->height,
3414 "framerate", GST_TYPE_FRACTION, stream->fps_n, stream->fps_d, NULL);
3417 * calculate pixel-aspect-ratio using display width and height */
3418 if (qtdemux->major_brand != FOURCC_qt__) {
3419 GST_DEBUG_OBJECT (qtdemux,
3420 "video size %dx%d, target display size %dx%d", stream->width,
3421 stream->height, stream->display_width, stream->display_height);
3423 if (stream->display_width > 0 && stream->display_height > 0 &&
3424 stream->width > 0 && stream->height > 0) {
3427 /* calculate the pixel aspect ratio using the display and pixel w/h */
3428 n = stream->display_width * stream->height;
3429 d = stream->display_height * stream->width;
3431 GST_DEBUG_OBJECT (qtdemux, "setting PAR to %d/%d", n, d);
3432 gst_caps_set_simple (stream->caps, "pixel-aspect-ratio",
3433 GST_TYPE_FRACTION, n, d, NULL);
3438 /* qt file might have pasp atom */
3439 if (stream->par_w > 0 && stream->par_h > 0) {
3440 GST_DEBUG_OBJECT (qtdemux, "par %d:%d", stream->par_w, stream->par_h);
3441 gst_caps_set_simple (stream->caps, "pixel-aspect-ratio",
3442 GST_TYPE_FRACTION, stream->par_w, stream->par_h, NULL);
3445 depth = stream->bits_per_sample;
3447 /* more than 32 bits means grayscale */
3448 gray = (depth > 32);
3449 /* low 32 bits specify the depth */
3452 /* different number of palette entries is determined by depth. */
3454 if ((depth == 1) || (depth == 2) || (depth == 4) || (depth == 8))
3455 palette_count = (1 << depth);
3457 switch (palette_count) {
3461 palette_data = ff_qt_default_palette_2;
3464 palette_data = ff_qt_default_palette_4;
3468 palette_data = ff_qt_grayscale_palette_16;
3470 palette_data = ff_qt_default_palette_16;
3474 palette_data = ff_qt_grayscale_palette_256;
3476 palette_data = ff_qt_default_palette_256;
3479 GST_ELEMENT_WARNING (qtdemux, STREAM, DECODE,
3480 (_("The video in this file might not play correctly.")),
3481 ("unsupported palette depth %d", depth));
3487 /* make sure it's not writable. We leave MALLOCDATA to NULL so that we
3488 * don't free any of the buffer data. */
3489 palette = gst_buffer_new ();
3490 GST_BUFFER_FLAG_SET (palette, GST_BUFFER_FLAG_READONLY);
3491 GST_BUFFER_DATA (palette) = (guint8 *) palette_data;
3492 GST_BUFFER_SIZE (palette) = sizeof (guint32) * palette_count;
3494 gst_caps_set_simple (stream->caps, "palette_data",
3495 GST_TYPE_BUFFER, palette, NULL);
3496 gst_buffer_unref (palette);
3497 } else if (palette_count != 0) {
3498 GST_ELEMENT_WARNING (qtdemux, STREAM, NOT_IMPLEMENTED,
3499 (NULL), ("Unsupported palette depth %d. Ignoring stream.", depth));
3501 gst_object_unref (stream->pad);
3505 qtdemux->n_video_streams++;
3506 } else if (stream->subtype == FOURCC_soun) {
3507 gchar *name = g_strdup_printf ("audio_%02d", qtdemux->n_audio_streams);
3510 gst_pad_new_from_static_template (&gst_qtdemux_audiosrc_template, name);
3513 gst_caps_set_simple (stream->caps,
3514 "rate", G_TYPE_INT, (int) stream->rate,
3515 "channels", G_TYPE_INT, stream->n_channels, NULL);
3517 qtdemux->n_audio_streams++;
3518 } else if (stream->subtype == FOURCC_strm) {
3519 GST_DEBUG_OBJECT (qtdemux, "stream type, not creating pad");
3520 } else if (stream->subtype == FOURCC_subp) {
3521 gchar *name = g_strdup_printf ("subp_%02d", qtdemux->n_subp_streams);
3524 gst_pad_new_from_static_template (&gst_qtdemux_subpsrc_template, name);
3526 qtdemux->n_subp_streams++;
3528 GST_DEBUG_OBJECT (qtdemux, "unknown stream type");
3532 qtdemux->streams[qtdemux->n_streams] = stream;
3533 qtdemux->n_streams++;
3534 GST_DEBUG_OBJECT (qtdemux, "n_streams is now %d", qtdemux->n_streams);
3537 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
3539 gst_pad_use_fixed_caps (stream->pad);
3540 gst_pad_set_event_function (stream->pad, gst_qtdemux_handle_src_event);
3541 gst_pad_set_query_type_function (stream->pad,
3542 gst_qtdemux_get_src_query_types);
3543 gst_pad_set_query_function (stream->pad, gst_qtdemux_handle_src_query);
3545 GST_DEBUG_OBJECT (qtdemux, "setting caps %" GST_PTR_FORMAT, stream->caps);
3546 gst_pad_set_caps (stream->pad, stream->caps);
3548 GST_DEBUG_OBJECT (qtdemux, "adding pad %s %p to qtdemux %p",
3549 GST_OBJECT_NAME (stream->pad), stream->pad, qtdemux);
3550 gst_pad_set_active (stream->pad, TRUE);
3551 gst_element_add_pad (GST_ELEMENT_CAST (qtdemux), stream->pad);
3552 if (stream->pending_tags)
3553 gst_tag_list_free (stream->pending_tags);
3554 stream->pending_tags = list;
3555 /* post now, send event on pad later */
3556 GST_DEBUG_OBJECT (qtdemux, "Posting tags %" GST_PTR_FORMAT,
3557 stream->pending_tags);
3558 gst_element_post_message (GST_ELEMENT (qtdemux),
3559 gst_message_new_tag_full (GST_OBJECT (qtdemux), stream->pad,
3560 gst_tag_list_copy (list)));
3561 stream->send_global_tags = TRUE;
3568 GST_ELEMENT_WARNING (qtdemux, STREAM, DECODE,
3569 (_("This file contains too many streams. Only playing first %d"),
3570 GST_QTDEMUX_MAX_STREAMS), (NULL));
3575 /* collect all samples for @stream by reading the info from @stbl
3578 qtdemux_parse_samples (GstQTDemux * qtdemux, QtDemuxStream * stream,
3581 QtAtomParser co_reader;
3586 guint32 sample_size;
3588 guint32 n_samples_per_chunk;
3590 QtDemuxSample *samples;
3596 /* sample to chunk */
3597 if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stsc, &stsc))
3601 if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stsz, &stsz))
3605 if (qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stco, &co_reader))
3606 co_size = sizeof (guint32);
3607 else if (qtdemux_tree_get_child_by_type_full (stbl, FOURCC_co64, &co_reader))
3608 co_size = sizeof (guint64);
3613 if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stts, &stts))
3616 if (!qt_atom_parser_skip (&stsz, 1 + 3) ||
3617 !qt_atom_parser_get_uint32 (&stsz, &sample_size))
3620 if (sample_size == 0 || stream->sampled) {
3621 /* skip version, flags, number of entries */
3622 if (!gst_byte_reader_skip (&co_reader, 1 + 3 + 4))
3625 if (!qt_atom_parser_get_uint32 (&stsz, &n_samples))
3631 GST_DEBUG_OBJECT (qtdemux, "stsz sample_size 0, allocating n_samples %u "
3632 "(%u MB)", n_samples,
3633 (guint) (n_samples * sizeof (QtDemuxSample)) >> 20);
3635 if (n_samples >= QTDEMUX_MAX_SAMPLE_INDEX_SIZE / sizeof (QtDemuxSample))
3638 samples = g_try_new0 (QtDemuxSample, n_samples);
3639 if (samples == NULL)
3642 stream->n_samples = n_samples;
3643 stream->samples = samples;
3645 /* set the sample sizes */
3646 if (sample_size == 0) {
3647 /* different sizes for each sample */
3648 if (!qt_atom_parser_has_chunks (&stsz, n_samples, 4))
3651 for (i = 0; i < n_samples; i++) {
3652 samples[i].size = qt_atom_parser_get_uint32_unchecked (&stsz);
3653 GST_LOG_OBJECT (qtdemux, "sample %d has size %u", i, samples[i].size);
3656 /* samples have the same size */
3657 GST_LOG_OBJECT (qtdemux, "all samples have size %u", sample_size);
3658 for (i = 0; i < n_samples; i++)
3659 samples[i].size = sample_size;
3662 /* set the sample offsets in the file */
3663 if (!qt_atom_parser_skip (&stsc, 1 + 3) ||
3664 !qt_atom_parser_get_uint32 (&stsc, &n_samples_per_chunk))
3667 if (!qt_atom_parser_has_chunks (&stsc, n_samples_per_chunk, 12))
3671 for (i = 0; i < n_samples_per_chunk; i++) {
3672 QtAtomParser co_chunk;
3673 guint32 first_chunk, last_chunk;
3674 guint32 samples_per_chunk;
3676 first_chunk = qt_atom_parser_get_uint32_unchecked (&stsc);
3677 samples_per_chunk = qt_atom_parser_get_uint32_unchecked (&stsc);
3678 qt_atom_parser_skip_unchecked (&stsc, 4);
3680 /* chunk numbers are counted from 1 it seems */
3681 if (G_UNLIKELY (first_chunk == 0))
3686 /* the last chunk of each entry is calculated by taking the first chunk
3687 * of the next entry; except if there is no next, where we fake it with
3689 if (G_UNLIKELY (i == n_samples_per_chunk - 1)) {
3690 last_chunk = G_MAXUINT32;
3692 last_chunk = qt_atom_parser_peek_uint32_unchecked (&stsc);
3693 if (G_UNLIKELY (last_chunk == 0))
3699 if (G_UNLIKELY (last_chunk < first_chunk))
3702 if (last_chunk != G_MAXUINT32) {
3703 if (!qt_atom_parser_peek_sub (&co_reader, first_chunk * co_size,
3704 (last_chunk - first_chunk) * co_size, &co_chunk))
3707 co_chunk = co_reader;
3708 if (!qt_atom_parser_skip (&co_chunk, first_chunk * co_size))
3712 for (j = first_chunk; j < last_chunk; j++) {
3713 guint64 chunk_offset;
3715 if (!qt_atom_parser_get_offset (&co_chunk, co_size, &chunk_offset))
3718 for (k = 0; k < samples_per_chunk; k++) {
3719 GST_LOG_OBJECT (qtdemux, "Creating entry %d with offset %"
3720 G_GUINT64_FORMAT, index, chunk_offset);
3721 samples[index].offset = chunk_offset;
3722 chunk_offset += samples[index].size;
3724 if (G_UNLIKELY (index >= n_samples))
3731 guint32 n_sample_times;
3734 if (!qt_atom_parser_skip (&stts, 4))
3736 if (!qt_atom_parser_get_uint32 (&stts, &n_sample_times))
3738 GST_LOG_OBJECT (qtdemux, "%u timestamp blocks", n_sample_times);
3740 /* make sure there's enough data */
3741 if (!qt_atom_parser_has_chunks (&stts, n_sample_times, 2 * 4))
3745 stream->min_duration = 0;
3748 for (i = 0; i < n_sample_times; i++) {
3752 n = qt_atom_parser_get_uint32_unchecked (&stts);
3753 duration = qt_atom_parser_get_uint32_unchecked (&stts);
3754 GST_LOG_OBJECT (qtdemux, "block %d, %u timestamps, duration %u ", i, n,
3757 /* take first duration for fps */
3758 if (G_UNLIKELY (stream->min_duration == 0))
3759 stream->min_duration = duration;
3761 for (j = 0; j < n; j++) {
3762 GST_DEBUG_OBJECT (qtdemux,
3763 "sample %d: index %d, timestamp %" GST_TIME_FORMAT, index, j,
3764 GST_TIME_ARGS (timestamp));
3766 samples[index].timestamp = timestamp;
3767 /* add non-scaled values to avoid rounding errors */
3770 gst_util_uint64_scale (time, GST_SECOND, stream->timescale);
3771 samples[index].duration = timestamp - samples[index].timestamp;
3774 if (G_UNLIKELY (index >= n_samples))
3778 /* fill up empty timestamps with the last timestamp, this can happen when
3779 * the last samples do not decode and so we don't have timestamps for them.
3780 * We however look at the last timestamp to estimate the track length so we
3781 * need something in here. */
3782 for (; index < n_samples; index++) {
3783 GST_DEBUG_OBJECT (qtdemux,
3784 "fill sample %d: timestamp %" GST_TIME_FORMAT, index,
3785 GST_TIME_ARGS (timestamp));
3786 samples[index].timestamp = timestamp;
3787 samples[index].duration = -1;
3792 /* FIXME: split this block out into a separate function */
3793 QtAtomParser stss, stps;
3795 /* sample sync, can be NULL */
3796 if (qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stss, &stss)) {
3797 guint32 n_sample_syncs;
3799 /* mark keyframes */
3800 if (!qt_atom_parser_skip (&stss, 4))
3802 if (!qt_atom_parser_get_uint32 (&stss, &n_sample_syncs))
3805 if (n_sample_syncs == 0) {
3806 stream->all_keyframe = TRUE;
3808 /* make sure there's enough data */
3809 if (!qt_atom_parser_has_chunks (&stss, n_sample_syncs, 4))
3811 for (i = 0; i < n_sample_syncs; i++) {
3812 /* note that the first sample is index 1, not 0 */
3813 index = qt_atom_parser_get_uint32_unchecked (&stss);
3814 if (G_LIKELY (index > 0 && index <= n_samples))
3815 samples[index - 1].keyframe = TRUE;
3818 /* stps marks partial sync frames like open GOP I-Frames */
3819 if (qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stps, &stps)) {
3820 guint32 n_sample_syncs;
3822 if (!qt_atom_parser_skip (&stps, 4))
3824 if (!qt_atom_parser_get_uint32 (&stps, &n_sample_syncs))
3827 if (n_sample_syncs != 0) {
3828 /* no entries, the stss table contains the real sync
3831 /* make sure there's enough data */
3832 if (!qt_atom_parser_has_chunks (&stps, n_sample_syncs, 4))
3834 for (i = 0; i < n_sample_syncs; i++) {
3835 /* note that the first sample is index 1, not 0 */
3836 index = qt_atom_parser_get_uint32_unchecked (&stps);
3837 if (G_LIKELY (index > 0 && index <= n_samples))
3838 samples[index - 1].keyframe = TRUE;
3843 /* no stss, all samples are keyframes */
3844 stream->all_keyframe = TRUE;
3848 GST_DEBUG_OBJECT (qtdemux,
3849 "stsz sample_size %d != 0, treating chunks as samples", sample_size);
3851 /* skip version + flags */
3852 if (!gst_byte_reader_skip (&co_reader, 1 + 3))
3855 /* treat chunks as samples */
3856 if (!gst_byte_reader_get_uint32_be (&co_reader, &n_samples))
3862 GST_DEBUG_OBJECT (qtdemux, "allocating n_samples %u (%u MB)", n_samples,
3863 (guint) (n_samples * sizeof (QtDemuxSample)) >> 20);
3865 if (n_samples >= QTDEMUX_MAX_SAMPLE_INDEX_SIZE / sizeof (QtDemuxSample))
3868 samples = g_try_new0 (QtDemuxSample, n_samples);
3869 if (samples == NULL)
3872 stream->n_samples = n_samples;
3873 stream->samples = samples;
3875 if (!qt_atom_parser_skip (&stsc, 1 + 3) ||
3876 !qt_atom_parser_get_uint32 (&stsc, &n_samples_per_chunk))
3879 GST_DEBUG_OBJECT (qtdemux, "n_samples_per_chunk %u", n_samples_per_chunk);
3883 if (!qt_atom_parser_has_chunks (&stsc, n_samples_per_chunk, 12))
3886 for (i = 0; i < n_samples_per_chunk; i++) {
3887 QtAtomParser co_chunk;
3888 guint32 first_chunk, last_chunk;
3889 guint32 samples_per_chunk;
3891 first_chunk = qt_atom_parser_get_uint32_unchecked (&stsc);
3892 samples_per_chunk = qt_atom_parser_get_uint32_unchecked (&stsc);
3893 qt_atom_parser_skip_unchecked (&stsc, 4);
3895 /* chunk numbers are counted from 1 it seems */
3896 if (G_UNLIKELY (first_chunk == 0))
3901 /* the last chunk of each entry is calculated by taking the first chunk
3902 * of the next entry; except if there is no next, where we fake it with
3904 if (G_UNLIKELY (i == (n_samples_per_chunk - 1))) {
3905 last_chunk = G_MAXUINT32;
3907 last_chunk = qt_atom_parser_peek_uint32_unchecked (&stsc);
3908 if (G_UNLIKELY (last_chunk == 0))
3914 GST_LOG_OBJECT (qtdemux,
3915 "entry %d has first_chunk %d, last_chunk %d, samples_per_chunk %d", i,
3916 first_chunk, last_chunk, samples_per_chunk);
3918 if (G_UNLIKELY (last_chunk < first_chunk))
3921 if (last_chunk != G_MAXUINT32) {
3922 if (!qt_atom_parser_peek_sub (&co_reader, first_chunk * co_size,
3923 (last_chunk - first_chunk) * co_size, &co_chunk))
3926 co_chunk = co_reader;
3927 if (!qt_atom_parser_skip (&co_chunk, first_chunk * co_size))
3931 for (j = first_chunk; j < last_chunk; j++) {
3936 qt_atom_parser_get_offset_unchecked (&co_chunk, co_size);
3938 GST_LOG_OBJECT (qtdemux, "Created entry %d with offset "
3939 "%" G_GUINT64_FORMAT, j, samples[j].offset);
3941 if (stream->samples_per_frame * stream->bytes_per_frame) {
3942 samples[j].size = (samples_per_chunk * stream->n_channels) /
3943 stream->samples_per_frame * stream->bytes_per_frame;
3945 samples[j].size = samples_per_chunk;
3948 GST_DEBUG_OBJECT (qtdemux, "sample %d: timestamp %" GST_TIME_FORMAT
3949 ", size %u", j, GST_TIME_ARGS (timestamp), samples[j].size);
3951 samples[j].timestamp = timestamp;
3952 sample_index += samples_per_chunk;
3954 timestamp = gst_util_uint64_scale (sample_index,
3955 GST_SECOND, stream->timescale);
3956 samples[j].duration = timestamp - samples[j].timestamp;
3958 samples[j].keyframe = TRUE;
3963 /* composition time to sample */
3964 if ((ctts = qtdemux_tree_get_child_by_type (stbl, FOURCC_ctts))) {
3965 const guint8 *ctts_data, *ctts_p;
3970 ctts_data = (const guint8 *) ctts->data;
3971 n_entries = QT_UINT32 (ctts_data + 12);
3973 /* Fill in the pts_offsets */
3975 ctts_p = ctts_data + 16;
3976 /* FIXME: make sure we don't read beyond the atom size/boundary */
3977 for (i = 0; i < n_entries; i++) {
3978 count = QT_UINT32 (ctts_p);
3980 soffset = QT_UINT32 (ctts_p);
3982 for (j = 0; j < count; j++) {
3983 /* we operate with very small soffset values here, it shouldn't overflow */
3984 samples[index].pts_offset = soffset * GST_SECOND / stream->timescale;
3986 if (G_UNLIKELY (index >= n_samples))
3997 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
3998 (_("This file is corrupt and cannot be played.")), (NULL));
4003 GST_WARNING_OBJECT (qtdemux, "stream has no samples");
4008 GST_WARNING_OBJECT (qtdemux, "failed to allocate %d samples", n_samples);
4013 GST_WARNING_OBJECT (qtdemux, "not allocating index of %d samples, would "
4014 "be larger than %uMB (broken file?)", n_samples,
4015 QTDEMUX_MAX_SAMPLE_INDEX_SIZE >> 20);
4020 /* collect all segment info for @stream.
4023 qtdemux_parse_segments (GstQTDemux * qtdemux, QtDemuxStream * stream,
4028 /* parse and prepare segment info from the edit list */
4029 GST_DEBUG_OBJECT (qtdemux, "looking for edit list container");
4030 stream->n_segments = 0;
4031 stream->segments = NULL;
4032 if ((edts = qtdemux_tree_get_child_by_type (trak, FOURCC_edts))) {
4036 guint64 time, stime;
4039 GST_DEBUG_OBJECT (qtdemux, "looking for edit list");
4040 if (!(elst = qtdemux_tree_get_child_by_type (edts, FOURCC_elst)))
4043 buffer = elst->data;
4045 n_segments = QT_UINT32 (buffer + 12);
4047 /* we might allocate a bit too much, at least allocate 1 segment */
4048 stream->segments = g_new (QtDemuxSegment, MAX (n_segments, 1));
4050 /* segments always start from 0 */
4054 for (i = 0; i < n_segments; i++) {
4057 QtDemuxSegment *segment;
4060 media_time = QT_UINT32 (buffer + 20 + i * 12);
4062 /* -1 media time is an empty segment, just ignore it */
4063 if (media_time == G_MAXUINT32)
4066 duration = QT_UINT32 (buffer + 16 + i * 12);
4068 segment = &stream->segments[count++];
4070 /* time and duration expressed in global timescale */
4071 segment->time = stime;
4072 /* add non scaled values so we don't cause roundoff errors */
4074 stime = gst_util_uint64_scale (time, GST_SECOND, qtdemux->timescale);
4075 segment->stop_time = stime;
4076 segment->duration = stime - segment->time;
4077 /* media_time expressed in stream timescale */
4078 segment->media_start =
4079 gst_util_uint64_scale (media_time, GST_SECOND, stream->timescale);
4080 segment->media_stop = segment->media_start + segment->duration;
4081 rate_int = GST_READ_UINT32_BE (buffer + 24 + i * 12);
4083 if (rate_int <= 1) {
4084 /* 0 is not allowed, some programs write 1 instead of the floating point
4086 GST_WARNING_OBJECT (qtdemux, "found suspicious rate %" G_GUINT32_FORMAT,
4090 segment->rate = rate_int / 65536.0;
4093 GST_DEBUG_OBJECT (qtdemux, "created segment %d time %" GST_TIME_FORMAT
4094 ", duration %" GST_TIME_FORMAT ", media_time %" GST_TIME_FORMAT
4095 ", rate %g, (%d)", i, GST_TIME_ARGS (segment->time),
4096 GST_TIME_ARGS (segment->duration),
4097 GST_TIME_ARGS (segment->media_start), segment->rate, rate_int);
4099 GST_DEBUG_OBJECT (qtdemux, "found %d non-empty segments", count);
4100 stream->n_segments = count;
4104 /* push based does not handle segments, so act accordingly here,
4105 * and warn if applicable */
4106 if (!qtdemux->pullbased) {
4107 GST_WARNING_OBJECT (qtdemux, "streaming; discarding edit list segments");
4108 /* remove and use default one below, we stream like it anyway */
4109 g_free (stream->segments);
4110 stream->segments = NULL;
4111 stream->n_segments = 0;
4114 /* no segments, create one to play the complete trak */
4115 if (stream->n_segments == 0) {
4116 GstClockTime stream_duration = 0;
4118 if (stream->segments == NULL)
4119 stream->segments = g_new (QtDemuxSegment, 1);
4121 /* samples know best */
4122 if (stream->n_samples > 0) {
4124 stream->samples[stream->n_samples - 1].timestamp +
4125 stream->samples[stream->n_samples - 1].pts_offset +
4126 stream->samples[stream->n_samples - 1].duration;
4129 stream->segments[0].time = 0;
4130 stream->segments[0].stop_time = stream_duration;
4131 stream->segments[0].duration = stream_duration;
4132 stream->segments[0].media_start = 0;
4133 stream->segments[0].media_stop = stream_duration;
4134 stream->segments[0].rate = 1.0;
4136 GST_DEBUG_OBJECT (qtdemux, "created dummy segment %" GST_TIME_FORMAT,
4137 GST_TIME_ARGS (stream_duration));
4138 stream->n_segments = 1;
4140 GST_DEBUG_OBJECT (qtdemux, "using %d segments", stream->n_segments);
4146 * With each track we associate a new QtDemuxStream that contains all the info
4148 * traks that do not decode to something (like strm traks) will not have a pad.
4151 qtdemux_parse_trak (GstQTDemux * qtdemux, GNode * trak)
4166 QtDemuxStream *stream;
4167 GstTagList *list = NULL;
4168 gchar *codec = NULL;
4169 const guint8 *stsd_data;
4172 guint8 tkhd_version;
4174 stream = g_new0 (QtDemuxStream, 1);
4175 /* new streams always need a discont */
4176 stream->discont = TRUE;
4177 /* we enable clipping for raw audio/video streams */
4178 stream->need_clip = FALSE;
4179 stream->segment_index = -1;
4180 stream->time_position = 0;
4181 stream->sample_index = -1;
4182 stream->last_ret = GST_FLOW_OK;
4184 if (!qtdemux_tree_get_child_by_type_full (trak, FOURCC_tkhd, &tkhd) ||
4185 !qt_atom_parser_get_uint8 (&tkhd, &tkhd_version) ||
4186 !qt_atom_parser_get_uint24 (&tkhd, &tkhd_flags))
4189 GST_LOG_OBJECT (qtdemux, "track[tkhd] version/flags: 0x%02x/%06x",
4190 tkhd_version, tkhd_flags);
4192 if (!(mdia = qtdemux_tree_get_child_by_type (trak, FOURCC_mdia)))
4195 if (!(mdhd = qtdemux_tree_get_child_by_type (mdia, FOURCC_mdhd))) {
4196 /* be nice for some crooked mjp2 files that use mhdr for mdhd */
4197 if (qtdemux->major_brand != FOURCC_mjp2 ||
4198 !(mdhd = qtdemux_tree_get_child_by_type (mdia, FOURCC_mhdr)))
4202 version = QT_UINT32 ((guint8 *) mdhd->data + 8);
4203 GST_LOG_OBJECT (qtdemux, "track version/flags: %08x", version);
4204 if (version == 0x01000000) {
4205 stream->timescale = QT_UINT32 ((guint8 *) mdhd->data + 28);
4206 stream->duration = QT_UINT64 ((guint8 *) mdhd->data + 32);
4208 stream->timescale = QT_UINT32 ((guint8 *) mdhd->data + 20);
4209 stream->duration = QT_UINT32 ((guint8 *) mdhd->data + 24);
4212 GST_LOG_OBJECT (qtdemux, "track timescale: %" G_GUINT32_FORMAT,
4214 GST_LOG_OBJECT (qtdemux, "track duration: %" G_GUINT64_FORMAT,
4217 if (G_UNLIKELY (stream->timescale == 0 || qtdemux->timescale == 0))
4220 if (qtdemux->duration != G_MAXINT32 && stream->duration != G_MAXINT32) {
4221 guint64 tdur1, tdur2;
4223 /* don't overflow */
4224 tdur1 = stream->timescale * (guint64) qtdemux->duration;
4225 tdur2 = qtdemux->timescale * (guint64) stream->duration;
4228 * some of those trailers, nowadays, have prologue images that are
4229 * themselves vide tracks as well. I haven't really found a way to
4230 * identify those yet, except for just looking at their duration. */
4231 if (tdur1 != 0 && (tdur2 * 10 / tdur1) < 2) {
4232 GST_WARNING_OBJECT (qtdemux,
4233 "Track shorter than 20%% (%" G_GUINT64_FORMAT "/%" G_GUINT32_FORMAT
4234 " vs. %" G_GUINT32_FORMAT "/%" G_GUINT32_FORMAT ") of the stream "
4235 "found, assuming preview image or something; skipping track",
4236 stream->duration, stream->timescale, qtdemux->duration,
4237 qtdemux->timescale);
4243 if (!(hdlr = qtdemux_tree_get_child_by_type (mdia, FOURCC_hdlr)))
4246 GST_LOG_OBJECT (qtdemux, "track type: %" GST_FOURCC_FORMAT,
4247 GST_FOURCC_ARGS (QT_FOURCC ((guint8 *) hdlr->data + 12)));
4249 stream->subtype = QT_FOURCC ((guint8 *) hdlr->data + 16);
4250 GST_LOG_OBJECT (qtdemux, "track subtype: %" GST_FOURCC_FORMAT,
4251 GST_FOURCC_ARGS (stream->subtype));
4253 if (!(minf = qtdemux_tree_get_child_by_type (mdia, FOURCC_minf)))
4256 if (!(stbl = qtdemux_tree_get_child_by_type (minf, FOURCC_stbl)))
4260 if (!(stsd = qtdemux_tree_get_child_by_type (stbl, FOURCC_stsd)))
4262 stsd_data = (const guint8 *) stsd->data;
4264 if (stream->subtype == FOURCC_vide) {
4265 guint32 w, h, fourcc;
4267 stream->sampled = TRUE;
4269 /* version 1 uses some 64-bit ints */
4270 if (!qt_atom_parser_skip (&tkhd, (tkhd_version == 1) ? 84 : 72) ||
4271 !qt_atom_parser_get_uint32 (&tkhd, &w) ||
4272 !qt_atom_parser_get_uint32 (&tkhd, &h))
4275 stream->display_width = w >> 16;
4276 stream->display_height = h >> 16;
4279 stream->fourcc = fourcc = QT_FOURCC (stsd_data + offset + 4);
4280 GST_LOG_OBJECT (qtdemux, "st type: %" GST_FOURCC_FORMAT,
4281 GST_FOURCC_ARGS (fourcc));
4283 stream->width = QT_UINT16 (stsd_data + offset + 32);
4284 stream->height = QT_UINT16 (stsd_data + offset + 34);
4285 stream->fps_n = 0; /* this is filled in later */
4286 stream->fps_d = 0; /* this is filled in later */
4287 stream->bits_per_sample = QT_UINT16 (stsd_data + offset + 82);
4288 stream->color_table_id = QT_UINT16 (stsd_data + offset + 84);
4290 GST_LOG_OBJECT (qtdemux, "frame count: %u",
4291 QT_UINT16 (stsd_data + offset + 48));
4293 if (fourcc == FOURCC_drms)
4294 goto error_encrypted;
4297 qtdemux_video_caps (qtdemux, stream, fourcc, stsd_data, &codec);
4299 list = gst_tag_list_new ();
4300 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
4301 GST_TAG_VIDEO_CODEC, codec, NULL);
4308 mp4v = qtdemux_tree_get_child_by_type (stsd, FOURCC_mp4v);
4309 /* H264 is MPEG-4 after all,
4310 * and qt seems to put MPEG-4 stuff in there as well */
4312 mp4v = qtdemux_tree_get_child_by_type (stsd, FOURCC_avc1);
4314 esds = qtdemux_tree_get_child_by_type (mp4v, FOURCC_esds);
4315 pasp = qtdemux_tree_get_child_by_type (mp4v, FOURCC_pasp);
4319 const guint8 *pasp_data = (const guint8 *) pasp->data;
4321 stream->par_w = QT_UINT32 (pasp_data + 8);
4322 stream->par_h = QT_UINT32 (pasp_data + 12);
4329 gst_qtdemux_handle_esds (qtdemux, stream, esds, list);
4334 gint len = QT_UINT32 (stsd_data) - 0x66;
4335 const guint8 *avc_data = stsd_data + 0x66;
4338 while (len >= 0x8 &&
4339 QT_FOURCC (avc_data + 0x4) != FOURCC_avcC &&
4340 QT_UINT32 (avc_data) < len) {
4341 len -= QT_UINT32 (avc_data);
4342 avc_data += QT_UINT32 (avc_data);
4345 /* parse, if found */
4346 if (len > 0x8 && QT_FOURCC (avc_data + 0x4) == FOURCC_avcC) {
4350 if (QT_UINT32 (avc_data) < len)
4351 size = QT_UINT32 (avc_data) - 0x8;
4355 GST_DEBUG_OBJECT (qtdemux, "found avcC codec_data in stsd");
4357 buf = gst_buffer_new_and_alloc (size);
4358 memcpy (GST_BUFFER_DATA (buf), avc_data + 0x8, size);
4359 gst_caps_set_simple (stream->caps,
4360 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4361 gst_buffer_unref (buf);
4367 GNode *jp2h, *colr, *mjp2, *field, *prefix;
4371 GST_DEBUG_OBJECT (qtdemux, "found mjp2");
4372 /* some required atoms */
4373 mjp2 = qtdemux_tree_get_child_by_type (stsd, FOURCC_mjp2);
4376 jp2h = qtdemux_tree_get_child_by_type (mjp2, FOURCC_jp2h);
4379 colr = qtdemux_tree_get_child_by_type (jp2h, FOURCC_colr);
4382 GST_DEBUG_OBJECT (qtdemux, "found colr");
4383 /* try to extract colour space info */
4384 if (QT_UINT8 ((guint8 *) colr->data + 8) == 1) {
4385 switch (QT_UINT32 ((guint8 *) colr->data + 11)) {
4387 fourcc = GST_MAKE_FOURCC ('s', 'R', 'G', 'B');
4390 fourcc = GST_MAKE_FOURCC ('G', 'R', 'A', 'Y');
4393 fourcc = GST_MAKE_FOURCC ('s', 'Y', 'U', 'V');
4401 gst_caps_set_simple (stream->caps,
4402 "fourcc", GST_TYPE_FOURCC, fourcc, NULL);
4404 /* some optional atoms */
4405 field = qtdemux_tree_get_child_by_type (mjp2, FOURCC_fiel);
4406 prefix = qtdemux_tree_get_child_by_type (mjp2, FOURCC_jp2x);
4408 /* indicate possible fields in caps */
4410 data = (guint8 *) field->data + 8;
4412 gst_caps_set_simple (stream->caps, "fields", G_TYPE_INT,
4413 (gint) * data, NULL);
4415 /* add codec_data if provided */
4420 GST_DEBUG_OBJECT (qtdemux, "found prefix data in stsd");
4421 data = prefix->data;
4422 len = QT_UINT32 (data);
4425 buf = gst_buffer_new_and_alloc (len);
4426 memcpy (GST_BUFFER_DATA (buf), data + 8, len);
4427 gst_caps_set_simple (stream->caps,
4428 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4429 gst_buffer_unref (buf);
4438 gint len = QT_UINT32 (stsd_data);
4440 GST_DEBUG_OBJECT (qtdemux, "found codec_data in stsd");
4442 buf = gst_buffer_new_and_alloc (len);
4443 memcpy (GST_BUFFER_DATA (buf), stsd_data, len);
4444 gst_caps_set_simple (stream->caps,
4445 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4446 gst_buffer_unref (buf);
4451 gst_caps_set_simple (stream->caps,
4452 "depth", G_TYPE_INT, QT_UINT16 (stsd_data + offset + 82), NULL);
4459 GST_DEBUG_OBJECT (qtdemux, "found XiTh");
4460 xith = qtdemux_tree_get_child_by_type (stsd, FOURCC_XiTh);
4464 xdxt = qtdemux_tree_get_child_by_type (xith, FOURCC_XdxT);
4468 GST_DEBUG_OBJECT (qtdemux, "found XdxT node");
4469 /* collect the headers and store them in a stream list so that we can
4470 * send them out first */
4471 qtdemux_parse_theora_extension (qtdemux, stream, xdxt);
4479 GST_INFO_OBJECT (qtdemux,
4480 "type %" GST_FOURCC_FORMAT " caps %" GST_PTR_FORMAT,
4481 GST_FOURCC_ARGS (fourcc), stream->caps);
4483 } else if (stream->subtype == FOURCC_soun) {
4484 int version, samplesize;
4487 guint16 compression_id;
4489 len = QT_UINT32 (stsd_data + 16);
4490 GST_LOG_OBJECT (qtdemux, "stsd len: %d", len);
4492 stream->fourcc = fourcc = QT_FOURCC (stsd_data + 16 + 4);
4493 GST_LOG_OBJECT (qtdemux, "stsd type: %" GST_FOURCC_FORMAT,
4494 GST_FOURCC_ARGS (stream->fourcc));
4498 version = QT_UINT32 (stsd_data + offset);
4499 stream->n_channels = QT_UINT16 (stsd_data + offset + 8);
4500 samplesize = QT_UINT16 (stsd_data + offset + 10);
4501 compression_id = QT_UINT16 (stsd_data + offset + 12);
4502 stream->rate = QT_FP32 (stsd_data + offset + 16);
4504 GST_LOG_OBJECT (qtdemux, "version/rev: %08x", version);
4505 GST_LOG_OBJECT (qtdemux, "vendor: %08x",
4506 QT_UINT32 (stsd_data + offset + 4));
4507 GST_LOG_OBJECT (qtdemux, "n_channels: %d", stream->n_channels);
4508 GST_LOG_OBJECT (qtdemux, "sample_size: %d", samplesize);
4509 GST_LOG_OBJECT (qtdemux, "compression_id: %d", compression_id);
4510 GST_LOG_OBJECT (qtdemux, "packet size: %d",
4511 QT_UINT16 (stsd_data + offset + 14));
4512 GST_LOG_OBJECT (qtdemux, "sample rate: %g", stream->rate);
4514 if (compression_id == 0xfffe)
4515 stream->sampled = TRUE;
4517 /* first assume uncompressed audio */
4518 stream->bytes_per_sample = samplesize / 8;
4519 stream->samples_per_frame = stream->n_channels;
4520 stream->bytes_per_frame = stream->n_channels * stream->bytes_per_sample;
4521 stream->samples_per_packet = stream->samples_per_frame;
4522 stream->bytes_per_packet = stream->bytes_per_sample;
4526 /* Yes, these have to be hard-coded */
4529 stream->samples_per_packet = 6;
4530 stream->bytes_per_packet = 1;
4531 stream->bytes_per_frame = 1 * stream->n_channels;
4532 stream->bytes_per_sample = 1;
4533 stream->samples_per_frame = 6 * stream->n_channels;
4538 stream->samples_per_packet = 3;
4539 stream->bytes_per_packet = 1;
4540 stream->bytes_per_frame = 1 * stream->n_channels;
4541 stream->bytes_per_sample = 1;
4542 stream->samples_per_frame = 3 * stream->n_channels;
4547 stream->samples_per_packet = 64;
4548 stream->bytes_per_packet = 34;
4549 stream->bytes_per_frame = 34 * stream->n_channels;
4550 stream->bytes_per_sample = 2;
4551 stream->samples_per_frame = 64 * stream->n_channels;
4557 stream->samples_per_packet = 1;
4558 stream->bytes_per_packet = 1;
4559 stream->bytes_per_frame = 1 * stream->n_channels;
4560 stream->bytes_per_sample = 1;
4561 stream->samples_per_frame = 1 * stream->n_channels;
4566 stream->samples_per_packet = 160;
4567 stream->bytes_per_packet = 33;
4568 stream->bytes_per_frame = 33 * stream->n_channels;
4569 stream->bytes_per_sample = 2;
4570 stream->samples_per_frame = 160 * stream->n_channels;
4577 if (version == 0x00010000) {
4585 /* only parse extra decoding config for non-pcm audio */
4586 stream->samples_per_packet = QT_UINT32 (stsd_data + offset);
4587 stream->bytes_per_packet = QT_UINT32 (stsd_data + offset + 4);
4588 stream->bytes_per_frame = QT_UINT32 (stsd_data + offset + 8);
4589 stream->bytes_per_sample = QT_UINT32 (stsd_data + offset + 12);
4591 GST_LOG_OBJECT (qtdemux, "samples/packet: %d",
4592 stream->samples_per_packet);
4593 GST_LOG_OBJECT (qtdemux, "bytes/packet: %d",
4594 stream->bytes_per_packet);
4595 GST_LOG_OBJECT (qtdemux, "bytes/frame: %d",
4596 stream->bytes_per_frame);
4597 GST_LOG_OBJECT (qtdemux, "bytes/sample: %d",
4598 stream->bytes_per_sample);
4600 if (!stream->sampled && stream->bytes_per_packet) {
4601 stream->samples_per_frame = (stream->bytes_per_frame /
4602 stream->bytes_per_packet) * stream->samples_per_packet;
4603 GST_LOG_OBJECT (qtdemux, "samples/frame: %d",
4604 stream->samples_per_frame);
4609 } else if (version == 0x00020000) {
4616 stream->samples_per_packet = QT_UINT32 (stsd_data + offset);
4617 qtfp.val = QT_UINT64 (stsd_data + offset + 4);
4618 stream->rate = qtfp.fp;
4619 stream->n_channels = QT_UINT32 (stsd_data + offset + 12);
4621 GST_LOG_OBJECT (qtdemux, "samples/packet: %d",
4622 stream->samples_per_packet);
4623 GST_LOG_OBJECT (qtdemux, "sample rate: %g", stream->rate);
4624 GST_LOG_OBJECT (qtdemux, "n_channels: %d", stream->n_channels);
4627 GST_WARNING_OBJECT (qtdemux, "unknown version %08x", version);
4630 if (fourcc == FOURCC_drms)
4631 goto error_encrypted;
4633 stream->caps = qtdemux_audio_caps (qtdemux, stream, fourcc, NULL, 0,
4642 in24 = qtdemux_tree_get_child_by_type (stsd, FOURCC_in24);
4644 enda = qtdemux_tree_get_child_by_type (in24, FOURCC_enda);
4646 wave = qtdemux_tree_get_child_by_type (in24, FOURCC_wave);
4648 enda = qtdemux_tree_get_child_by_type (wave, FOURCC_enda);
4651 gst_caps_set_simple (stream->caps,
4652 "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL);
4661 list = gst_tag_list_new ();
4662 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
4663 GST_TAG_AUDIO_CODEC, codec, NULL);
4668 mp4a = qtdemux_tree_get_child_by_type (stsd, FOURCC_mp4a);
4672 wave = qtdemux_tree_get_child_by_type (mp4a, FOURCC_wave);
4674 esds = qtdemux_tree_get_child_by_type (wave, FOURCC_esds);
4676 esds = qtdemux_tree_get_child_by_type (mp4a, FOURCC_esds);
4680 gst_qtdemux_handle_esds (qtdemux, stream, esds, list);
4684 /* FIXME: what is in the chunk? */
4687 gint len = QT_UINT32 (stsd_data);
4689 /* seems to be always = 116 = 0x74 */
4695 gint len = QT_UINT32 (stsd_data);
4698 GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x4C);
4700 memcpy (GST_BUFFER_DATA (buf), stsd_data + 0x4C, len - 0x4C);
4701 gst_caps_set_simple (stream->caps,
4702 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4703 gst_buffer_unref (buf);
4705 gst_caps_set_simple (stream->caps,
4706 "samplesize", G_TYPE_INT, samplesize, NULL);
4711 gint len = QT_UINT32 (stsd_data);
4714 GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x34);
4716 memcpy (GST_BUFFER_DATA (buf), stsd_data + 0x34, len - 0x34);
4717 gst_caps_set_simple (stream->caps,
4718 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4719 gst_buffer_unref (buf);
4721 gst_caps_set_simple (stream->caps,
4722 "samplesize", G_TYPE_INT, samplesize, NULL);
4727 gint len = QT_UINT32 (stsd_data);
4730 GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x34);
4732 memcpy (GST_BUFFER_DATA (buf), stsd_data + 0x34, len - 0x34);
4734 gst_caps_set_simple (stream->caps,
4735 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4736 gst_buffer_unref (buf);
4744 GST_INFO_OBJECT (qtdemux,
4745 "type %" GST_FOURCC_FORMAT " caps %" GST_PTR_FORMAT,
4746 GST_FOURCC_ARGS (fourcc), stream->caps);
4748 } else if (stream->subtype == FOURCC_strm) {
4751 stream->fourcc = fourcc = QT_FOURCC (stsd_data + 16 + 4);
4752 GST_LOG_OBJECT (qtdemux, "stsd type: %" GST_FOURCC_FORMAT,
4753 GST_FOURCC_ARGS (fourcc));
4755 if (fourcc != FOURCC_rtsp) {
4756 GST_INFO_OBJECT (qtdemux, "unhandled stream type %" GST_FOURCC_FORMAT,
4757 GST_FOURCC_ARGS (fourcc));
4758 goto unknown_stream;
4760 stream->sampled = TRUE;
4761 } else if (stream->subtype == FOURCC_subp) {
4764 stream->sampled = TRUE;
4767 stream->fourcc = fourcc = QT_FOURCC (stsd_data + offset + 4);
4768 GST_LOG_OBJECT (qtdemux, "st type: %" GST_FOURCC_FORMAT,
4769 GST_FOURCC_ARGS (fourcc));
4772 qtdemux_subp_caps (qtdemux, stream, fourcc, stsd_data, &codec);
4774 goto unknown_stream;
4777 /* promote to sampled format */
4778 if (stream->fourcc == FOURCC_samr) {
4779 /* force mono 8000 Hz for AMR */
4780 stream->sampled = TRUE;
4781 stream->n_channels = 1;
4782 stream->rate = 8000;
4783 } else if (stream->fourcc == FOURCC_sawb) {
4784 /* force mono 16000 Hz for AMR-WB */
4785 stream->sampled = TRUE;
4786 stream->n_channels = 1;
4787 stream->rate = 16000;
4788 } else if (stream->fourcc == FOURCC_mp4a) {
4789 stream->sampled = TRUE;
4792 /* collect sample information */
4793 if (!qtdemux_parse_samples (qtdemux, stream, stbl))
4794 goto samples_failed;
4796 /* configure segments */
4797 if (!qtdemux_parse_segments (qtdemux, stream, trak))
4798 goto segments_failed;
4800 /* now we are ready to add the stream */
4801 gst_qtdemux_add_stream (qtdemux, stream, list);
4808 GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
4809 (_("This file is corrupt and cannot be played.")), (NULL));
4815 GST_ELEMENT_ERROR (qtdemux, STREAM, DECRYPT, (NULL), (NULL));
4821 /* we posted an error already */
4827 /* we posted an error already */
4833 GST_INFO_OBJECT (qtdemux, "unknown subtype %" GST_FOURCC_FORMAT,
4834 GST_FOURCC_ARGS (stream->subtype));
4840 /* check if major or compatible brand is 3GP */
4841 static inline gboolean
4842 qtdemux_is_brand_3gp (GstQTDemux * qtdemux, gboolean major)
4845 return ((qtdemux->major_brand & GST_MAKE_FOURCC (255, 255, 0, 0)) ==
4846 GST_MAKE_FOURCC ('3', 'g', 0, 0));
4847 } else if (qtdemux->comp_brands != NULL) {
4848 guint8 *data = GST_BUFFER_DATA (qtdemux->comp_brands);
4849 guint size = GST_BUFFER_SIZE (qtdemux->comp_brands);
4850 gboolean res = FALSE;
4853 res = res || ((QT_FOURCC (data) & GST_MAKE_FOURCC (255, 255, 0, 0)) ==
4854 GST_MAKE_FOURCC ('3', 'g', 0, 0));
4864 /* check if tag is a spec'ed 3GP tag keyword storing a string */
4865 static inline gboolean
4866 qtdemux_is_string_tag_3gp (GstQTDemux * qtdemux, guint32 fourcc)
4868 return fourcc == FOURCC_cprt || fourcc == FOURCC_gnre || fourcc == FOURCC_titl
4869 || fourcc == FOURCC_dscp || fourcc == FOURCC_perf || fourcc == FOURCC_auth
4870 || fourcc == FOURCC_albm;
4874 qtdemux_tag_add_location (GstQTDemux * qtdemux, const char *tag,
4875 const char *dummy, GNode * node)
4877 const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
4881 gdouble longitude, latitude, altitude;
4886 /* TODO: language code skipped */
4888 name = gst_tag_freeform_string_to_utf8 (data + offset, -1, env_vars);
4891 /* do not alarm in trivial case, but bail out otherwise */
4892 if (*(data + offset) != 0) {
4893 GST_DEBUG_OBJECT (qtdemux, "failed to convert %s tag to UTF-8, "
4897 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
4898 GST_TAG_GEO_LOCATION_NAME, name, NULL);
4899 offset += strlen (name);
4903 /* +1 +1 = skip null-terminator and location role byte */
4905 longitude = QT_FP32 (data + offset);
4908 latitude = QT_FP32 (data + offset);
4911 altitude = QT_FP32 (data + offset);
4913 /* one invalid means all are invalid */
4914 if (longitude >= -180.0 && longitude <= 180.0 &&
4915 latitude >= -90.0 && latitude <= 90.0) {
4916 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
4917 GST_TAG_GEO_LOCATION_LATITUDE, latitude,
4918 GST_TAG_GEO_LOCATION_LONGITUDE, longitude,
4919 GST_TAG_GEO_LOCATION_ELEVATION, altitude, NULL);
4922 /* TODO: no GST_TAG_, so astronomical body and additional notes skipped */
4927 qtdemux_tag_add_year (GstQTDemux * qtdemux, const char *tag, const char *dummy,
4933 y = QT_UINT16 ((guint8 *) node->data + 12);
4934 GST_DEBUG_OBJECT (qtdemux, "year: %u", y);
4936 date = g_date_new_dmy (1, 1, y);
4937 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag, date, NULL);
4942 qtdemux_tag_add_classification (GstQTDemux * qtdemux, const char *tag,
4943 const char *dummy, GNode * node)
4946 char *tag_str = NULL;
4952 entity = (guint8 *) node->data + offset;
4955 table = QT_UINT16 ((guint8 *) node->data + offset);
4957 /* Language code skipped */
4961 /* Tag format: "XXXX://Y[YYYY]/classification info string"
4962 * XXXX: classification entity, fixed length 4 chars.
4963 * Y[YYYY]: classification table, max 5 chars.
4965 tag_str = g_strdup_printf ("----://%u/%s",
4966 table, (char *) node->data + offset);
4968 /* memcpy To be sure we're preserving byte order */
4969 memcpy (tag_str, entity, 4);
4970 GST_DEBUG_OBJECT (qtdemux, "classification info: %s", tag_str);
4972 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_APPEND, tag,
4979 qtdemux_tag_add_str_full (GstQTDemux * qtdemux, const char *tag,
4980 const char *dummy, GNode * node)
4982 const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
4988 gboolean ret = TRUE;
4990 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
4992 len = QT_UINT32 (data->data);
4993 type = QT_UINT32 ((guint8 *) data->data + 8);
4994 if (type == 0x00000001) {
4995 s = gst_tag_freeform_string_to_utf8 ((char *) data->data + 16, len - 16,
4998 GST_DEBUG_OBJECT (qtdemux, "adding tag %s", GST_STR_NULL (s));
4999 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag, s,
5003 GST_DEBUG_OBJECT (qtdemux, "failed to convert %s tag to UTF-8", tag);
5007 len = QT_UINT32 (node->data);
5008 type = QT_UINT32 ((guint8 *) node->data + 4);
5009 if ((type >> 24) == 0xa9) {
5010 /* Type starts with the (C) symbol, so the next 32 bits are
5011 * the language code, which we ignore */
5013 GST_DEBUG_OBJECT (qtdemux, "found international text tag");
5014 } else if (len > 14 && qtdemux_is_string_tag_3gp (qtdemux,
5015 QT_FOURCC ((guint8 *) node->data + 4))) {
5016 guint32 type = QT_UINT32 ((guint8 *) node->data + 8);
5018 /* we go for 3GP style encoding if major brands claims so,
5019 * or if no hope for data be ok UTF-8, and compatible 3GP brand present */
5020 if (qtdemux_is_brand_3gp (qtdemux, TRUE) ||
5021 (qtdemux_is_brand_3gp (qtdemux, FALSE) &&
5022 ((type & 0x00FFFFFF) == 0x0) && (type >> 24 <= 0xF))) {
5024 /* 16-bit Language code is ignored here as well */
5025 GST_DEBUG_OBJECT (qtdemux, "found 3gpp text tag");
5032 GST_DEBUG_OBJECT (qtdemux, "found normal text tag");
5033 ret = FALSE; /* may have to fallback */
5035 s = gst_tag_freeform_string_to_utf8 ((char *) node->data + offset,
5036 len - offset, env_vars);
5038 GST_DEBUG_OBJECT (qtdemux, "adding tag %s", GST_STR_NULL (s));
5039 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag, s, NULL);
5043 GST_DEBUG_OBJECT (qtdemux, "failed to convert %s tag to UTF-8", tag);
5050 qtdemux_tag_add_str (GstQTDemux * qtdemux, const char *tag,
5051 const char *dummy, GNode * node)
5053 qtdemux_tag_add_str_full (qtdemux, tag, dummy, node);
5057 qtdemux_tag_add_keywords (GstQTDemux * qtdemux, const char *tag,
5058 const char *dummy, GNode * node)
5060 const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
5062 char *s, *t, *k = NULL;
5067 /* first try normal string tag if major brand not 3GP */
5068 if (!qtdemux_is_brand_3gp (qtdemux, TRUE)) {
5069 if (!qtdemux_tag_add_str_full (qtdemux, tag, dummy, node)) {
5070 /* hm, that did not work, maybe 3gpp storage in non-3gpp major brand;
5071 * let's try it 3gpp way after minor safety check */
5073 if (QT_UINT32 (data) < 15 || !qtdemux_is_brand_3gp (qtdemux, FALSE))
5079 GST_DEBUG_OBJECT (qtdemux, "found 3gpp keyword tag");
5083 len = QT_UINT32 (data);
5087 count = QT_UINT8 (data + 14);
5089 for (; count; count--) {
5092 if (offset + 1 > len)
5094 slen = QT_UINT8 (data + offset);
5096 if (offset + slen > len)
5098 s = gst_tag_freeform_string_to_utf8 ((char *) node->data + offset,
5101 GST_DEBUG_OBJECT (qtdemux, "adding keyword %s", GST_STR_NULL (s));
5103 t = g_strjoin (",", k, s, NULL);
5111 GST_DEBUG_OBJECT (qtdemux, "failed to convert keyword to UTF-8");
5118 GST_DEBUG_OBJECT (qtdemux, "adding tag %s", GST_STR_NULL (k));
5119 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag, k, NULL);
5128 GST_DEBUG_OBJECT (qtdemux, "short read parsing 3GP keywords");
5134 qtdemux_tag_add_num (GstQTDemux * qtdemux, const char *tag1,
5135 const char *tag2, GNode * node)
5142 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5144 len = QT_UINT32 (data->data);
5145 type = QT_UINT32 ((guint8 *) data->data + 8);
5146 if (type == 0x00000000 && len >= 22) {
5147 n1 = QT_UINT16 ((guint8 *) data->data + 18);
5148 n2 = QT_UINT16 ((guint8 *) data->data + 20);
5150 GST_DEBUG_OBJECT (qtdemux, "adding tag %s=%d", tag1, n1);
5151 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5155 GST_DEBUG_OBJECT (qtdemux, "adding tag %s=%d", tag2, n2);
5156 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5164 qtdemux_tag_add_tmpo (GstQTDemux * qtdemux, const char *tag1, const char *dummy,
5172 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5174 len = QT_UINT32 (data->data);
5175 type = QT_UINT32 ((guint8 *) data->data + 8);
5176 GST_DEBUG_OBJECT (qtdemux, "have tempo tag, type=%d,len=%d", type, len);
5177 /* some files wrongly have a type 0x0f=15, but it should be 0x15 */
5178 if ((type == 0x00000015 || type == 0x0000000f) && len >= 18) {
5179 n1 = QT_UINT16 ((guint8 *) data->data + 16);
5181 /* do not add bpm=0 */
5182 GST_DEBUG_OBJECT (qtdemux, "adding tag %d", n1);
5183 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5184 tag1, (gdouble) n1, NULL);
5191 qtdemux_tag_add_covr (GstQTDemux * qtdemux, const char *tag1, const char *dummy,
5199 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5201 len = QT_UINT32 (data->data);
5202 type = QT_UINT32 ((guint8 *) data->data + 8);
5203 GST_DEBUG_OBJECT (qtdemux, "have covr tag, type=%d,len=%d", type, len);
5204 if ((type == 0x0000000d || type == 0x0000000e) && len > 16) {
5205 if ((buf = gst_tag_image_data_to_image_buffer ((guint8 *) data->data + 16,
5206 len - 16, GST_TAG_IMAGE_TYPE_NONE))) {
5207 GST_DEBUG_OBJECT (qtdemux, "adding tag size %d", len - 16);
5208 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5210 gst_buffer_unref (buf);
5217 qtdemux_tag_add_date (GstQTDemux * qtdemux, const char *tag, const char *dummy,
5225 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5227 len = QT_UINT32 (data->data);
5228 type = QT_UINT32 ((guint8 *) data->data + 8);
5229 if (type == 0x00000001) {
5230 guint y, m = 1, d = 1;
5233 s = g_strndup ((char *) data->data + 16, len - 16);
5234 GST_DEBUG_OBJECT (qtdemux, "adding date '%s'", s);
5235 ret = sscanf (s, "%u-%u-%u", &y, &m, &d);
5236 if (ret >= 1 && y > 1500 && y < 3000) {
5239 date = g_date_new_dmy (d, m, y);
5240 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag,
5244 GST_DEBUG_OBJECT (qtdemux, "could not parse date string '%s'", s);
5252 qtdemux_tag_add_gnre (GstQTDemux * qtdemux, const char *tag, const char *dummy,
5257 data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5259 /* re-route to normal string tag if major brand says so
5260 * or no data atom and compatible brand suggests so */
5261 if (qtdemux_is_brand_3gp (qtdemux, TRUE) ||
5262 (qtdemux_is_brand_3gp (qtdemux, FALSE) && !data)) {
5263 qtdemux_tag_add_str (qtdemux, tag, dummy, node);
5270 len = QT_UINT32 (data->data);
5271 type = QT_UINT32 ((guint8 *) data->data + 8);
5272 if (type == 0x00000000 && len >= 18) {
5273 n = QT_UINT16 ((guint8 *) data->data + 16);
5277 genre = gst_tag_id3_genre_get (n - 1);
5278 if (genre != NULL) {
5279 GST_DEBUG_OBJECT (qtdemux, "adding %d [%s]", n, genre);
5280 gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5288 typedef void (*GstQTDemuxAddTagFunc) (GstQTDemux * demux,
5289 const char *tag, const char *tag_bis, GNode * node);
5294 const gchar *gst_tag;
5295 const gchar *gst_tag_bis;
5296 const GstQTDemuxAddTagFunc func;
5299 FOURCC__nam, GST_TAG_TITLE, NULL, qtdemux_tag_add_str}, {
5300 FOURCC_titl, GST_TAG_TITLE, NULL, qtdemux_tag_add_str}, {
5301 FOURCC__grp, GST_TAG_ARTIST, NULL, qtdemux_tag_add_str}, {
5302 FOURCC__wrt, GST_TAG_COMPOSER, NULL, qtdemux_tag_add_str}, {
5303 FOURCC__ART, GST_TAG_ARTIST, NULL, qtdemux_tag_add_str}, {
5304 FOURCC_perf, GST_TAG_ARTIST, NULL, qtdemux_tag_add_str}, {
5305 FOURCC_auth, GST_TAG_COMPOSER, NULL, qtdemux_tag_add_str}, {
5306 FOURCC__alb, GST_TAG_ALBUM, NULL, qtdemux_tag_add_str}, {
5307 FOURCC_albm, GST_TAG_ALBUM, NULL, qtdemux_tag_add_str}, {
5308 FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, qtdemux_tag_add_str}, {
5309 FOURCC__cpy, GST_TAG_COPYRIGHT, NULL, qtdemux_tag_add_str}, {
5310 FOURCC__cmt, GST_TAG_COMMENT, NULL, qtdemux_tag_add_str}, {
5311 FOURCC__des, GST_TAG_DESCRIPTION, NULL, qtdemux_tag_add_str}, {
5312 FOURCC_dscp, GST_TAG_DESCRIPTION, NULL, qtdemux_tag_add_str}, {
5313 FOURCC__day, GST_TAG_DATE, NULL, qtdemux_tag_add_date}, {
5314 FOURCC_yrrc, GST_TAG_DATE, NULL, qtdemux_tag_add_year}, {
5315 FOURCC__too, GST_TAG_COMMENT, NULL, qtdemux_tag_add_str}, {
5316 FOURCC__inf, GST_TAG_COMMENT, NULL, qtdemux_tag_add_str}, {
5317 FOURCC_trkn, GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT, qtdemux_tag_add_num}, {
5318 FOURCC_disk, GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT,
5319 qtdemux_tag_add_num}, {
5320 FOURCC_disc, GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT,
5321 qtdemux_tag_add_num}, {
5322 FOURCC__gen, GST_TAG_GENRE, NULL, qtdemux_tag_add_str}, {
5323 FOURCC_gnre, GST_TAG_GENRE, NULL, qtdemux_tag_add_gnre}, {
5324 FOURCC_tmpo, GST_TAG_BEATS_PER_MINUTE, NULL, qtdemux_tag_add_tmpo}, {
5325 FOURCC_covr, GST_TAG_PREVIEW_IMAGE, NULL, qtdemux_tag_add_covr}, {
5326 FOURCC_kywd, GST_TAG_KEYWORDS, NULL, qtdemux_tag_add_keywords}, {
5327 FOURCC_keyw, GST_TAG_KEYWORDS, NULL, qtdemux_tag_add_str}, {
5328 FOURCC__enc, GST_TAG_ENCODER, NULL, qtdemux_tag_add_str}, {
5329 FOURCC_loci, GST_TAG_GEO_LOCATION_NAME, NULL, qtdemux_tag_add_location}, {
5330 FOURCC_clsf, GST_QT_DEMUX_CLASSIFICATION_TAG, NULL,
5331 qtdemux_tag_add_classification}
5335 qtdemux_tag_add_blob (GNode * node, GstQTDemux * demux)
5340 gchar *media_type, *style;
5346 len = QT_UINT32 (data);
5347 buf = gst_buffer_new_and_alloc (len);
5348 memcpy (GST_BUFFER_DATA (buf), data, len);
5350 /* heuristic to determine style of tag */
5351 if (QT_FOURCC (data + 4) == FOURCC_____ ||
5352 (len > 8 + 12 && QT_FOURCC (data + 12) == FOURCC_data))
5354 else if (demux->major_brand == FOURCC_qt__)
5355 style = "quicktime";
5356 /* fall back to assuming iso/3gp tag style */
5360 /* santize the name for the caps. */
5361 for (i = 0; i < 4; i++) {
5362 guint8 d = data[4 + i];
5363 if (g_ascii_isalnum (d))
5364 ndata[i] = g_ascii_tolower (d);
5369 media_type = g_strdup_printf ("application/x-gst-qt-%c%c%c%c-tag",
5370 ndata[0], ndata[1], ndata[2], ndata[3]);
5371 GST_DEBUG_OBJECT (demux, "media type %s", media_type);
5373 caps = gst_caps_new_simple (media_type, "style", G_TYPE_STRING, style, NULL);
5374 gst_buffer_set_caps (buf, caps);
5375 gst_caps_unref (caps);
5376 g_free (media_type);
5378 GST_DEBUG_OBJECT (demux, "adding private tag; size %d, caps %" GST_PTR_FORMAT,
5379 GST_BUFFER_SIZE (buf), caps);
5381 gst_tag_list_add (demux->tag_list, GST_TAG_MERGE_APPEND,
5382 GST_QT_DEMUX_PRIVATE_TAG, buf, NULL);
5383 gst_buffer_unref (buf);
5387 qtdemux_parse_udta (GstQTDemux * qtdemux, GNode * udta)
5394 meta = qtdemux_tree_get_child_by_type (udta, FOURCC_meta);
5396 ilst = qtdemux_tree_get_child_by_type (meta, FOURCC_ilst);
5398 GST_LOG_OBJECT (qtdemux, "no ilst");
5403 GST_LOG_OBJECT (qtdemux, "no meta so using udta itself");
5406 GST_DEBUG_OBJECT (qtdemux, "new tag list");
5407 qtdemux->tag_list = gst_tag_list_new ();
5409 for (i = 0; i < G_N_ELEMENTS (add_funcs); ++i) {
5410 node = qtdemux_tree_get_child_by_type (ilst, add_funcs[i].fourcc);
5412 add_funcs[i].func (qtdemux, add_funcs[i].gst_tag,
5413 add_funcs[i].gst_tag_bis, node);
5414 g_node_destroy (node);
5418 /* parsed nodes have been removed, pass along remainder as blob */
5419 g_node_children_foreach (ilst, G_TRAVERSE_ALL,
5420 (GNodeForeachFunc) qtdemux_tag_add_blob, qtdemux);
5426 GstStructure *structure; /* helper for sort function */
5428 guint min_req_bitrate;
5429 guint min_req_qt_version;
5433 qtdemux_redirects_sort_func (gconstpointer a, gconstpointer b)
5435 GstQtReference *ref_a = (GstQtReference *) a;
5436 GstQtReference *ref_b = (GstQtReference *) b;
5438 if (ref_b->min_req_qt_version != ref_a->min_req_qt_version)
5439 return ref_b->min_req_qt_version - ref_a->min_req_qt_version;
5441 /* known bitrates go before unknown; higher bitrates go first */
5442 return ref_b->min_req_bitrate - ref_a->min_req_bitrate;
5445 /* sort the redirects and post a message for the application.
5448 qtdemux_process_redirects (GstQTDemux * qtdemux, GList * references)
5450 GstQtReference *best;
5453 GValue list_val = { 0, };
5456 g_assert (references != NULL);
5458 references = g_list_sort (references, qtdemux_redirects_sort_func);
5460 best = (GstQtReference *) references->data;
5462 g_value_init (&list_val, GST_TYPE_LIST);
5464 for (l = references; l != NULL; l = l->next) {
5465 GstQtReference *ref = (GstQtReference *) l->data;
5466 GValue struct_val = { 0, };
5468 ref->structure = gst_structure_new ("redirect",
5469 "new-location", G_TYPE_STRING, ref->location, NULL);
5471 if (ref->min_req_bitrate > 0) {
5472 gst_structure_set (ref->structure, "minimum-bitrate", G_TYPE_INT,
5473 ref->min_req_bitrate, NULL);
5476 g_value_init (&struct_val, GST_TYPE_STRUCTURE);
5477 g_value_set_boxed (&struct_val, ref->structure);
5478 gst_value_list_append_value (&list_val, &struct_val);
5479 g_value_unset (&struct_val);
5480 /* don't free anything here yet, since we need best->structure below */
5483 g_assert (best != NULL);
5484 s = gst_structure_copy (best->structure);
5486 if (g_list_length (references) > 1) {
5487 gst_structure_set_value (s, "locations", &list_val);
5490 g_value_unset (&list_val);
5492 for (l = references; l != NULL; l = l->next) {
5493 GstQtReference *ref = (GstQtReference *) l->data;
5495 gst_structure_free (ref->structure);
5496 g_free (ref->location);
5499 g_list_free (references);
5501 GST_INFO_OBJECT (qtdemux, "posting redirect message: %" GST_PTR_FORMAT, s);
5502 msg = gst_message_new_element (GST_OBJECT_CAST (qtdemux), s);
5503 gst_element_post_message (GST_ELEMENT_CAST (qtdemux), msg);
5506 /* look for redirect nodes, collect all redirect information and
5510 qtdemux_parse_redirects (GstQTDemux * qtdemux)
5512 GNode *rmra, *rmda, *rdrf;
5514 rmra = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_rmra);
5516 GList *redirects = NULL;
5518 rmda = qtdemux_tree_get_child_by_type (rmra, FOURCC_rmda);
5520 GstQtReference ref = { NULL, NULL, 0, 0 };
5523 if ((rmdr = qtdemux_tree_get_child_by_type (rmda, FOURCC_rmdr))) {
5524 ref.min_req_bitrate = QT_UINT32 ((guint8 *) rmdr->data + 12);
5525 GST_LOG_OBJECT (qtdemux, "data rate atom, required bitrate = %u",
5526 ref.min_req_bitrate);
5529 if ((rmvc = qtdemux_tree_get_child_by_type (rmda, FOURCC_rmvc))) {
5530 guint32 package = QT_FOURCC ((guint8 *) rmvc->data + 12);
5531 guint version = QT_UINT32 ((guint8 *) rmvc->data + 16);
5533 #ifndef GST_DISABLE_GST_DEBUG
5534 guint bitmask = QT_UINT32 ((guint8 *) rmvc->data + 20);
5536 guint check_type = QT_UINT16 ((guint8 *) rmvc->data + 24);
5538 GST_LOG_OBJECT (qtdemux,
5539 "version check atom [%" GST_FOURCC_FORMAT "], version=0x%08x"
5540 ", mask=%08x, check_type=%u", GST_FOURCC_ARGS (package), version,
5541 bitmask, check_type);
5542 if (package == FOURCC_qtim && check_type == 0) {
5543 ref.min_req_qt_version = version;
5547 rdrf = qtdemux_tree_get_child_by_type (rmda, FOURCC_rdrf);
5552 ref_type = QT_FOURCC ((guint8 *) rdrf->data + 12);
5553 ref_data = (guint8 *) rdrf->data + 20;
5554 if (ref_type == FOURCC_alis) {
5555 guint record_len, record_version, fn_len;
5557 /* MacOSX alias record, google for alias-layout.txt */
5558 record_len = QT_UINT16 (ref_data + 4);
5559 record_version = QT_UINT16 (ref_data + 4 + 2);
5560 fn_len = QT_UINT8 (ref_data + 50);
5561 if (record_len > 50 && record_version == 2 && fn_len > 0) {
5562 ref.location = g_strndup ((gchar *) ref_data + 51, fn_len);
5564 } else if (ref_type == FOURCC_url_) {
5565 ref.location = g_strdup ((gchar *) ref_data);
5567 GST_DEBUG_OBJECT (qtdemux,
5568 "unknown rdrf reference type %" GST_FOURCC_FORMAT,
5569 GST_FOURCC_ARGS (ref_type));
5571 if (ref.location != NULL) {
5572 GST_INFO_OBJECT (qtdemux, "New location: %s", ref.location);
5573 redirects = g_list_prepend (redirects, g_memdup (&ref, sizeof (ref)));
5575 GST_WARNING_OBJECT (qtdemux,
5576 "Failed to extract redirect location from rdrf atom");
5580 /* look for others */
5581 rmda = qtdemux_tree_get_sibling_by_type (rmda, FOURCC_rmda);
5584 if (redirects != NULL) {
5585 qtdemux_process_redirects (qtdemux, redirects);
5592 qtdemux_add_container_format (GstQTDemux * qtdemux, GstTagList * tags)
5597 tags = gst_tag_list_new ();
5599 if (qtdemux->major_brand == FOURCC_mjp2)
5600 fmt = "Motion JPEG 2000";
5601 else if ((qtdemux->major_brand & 0xffff) == GST_MAKE_FOURCC ('3', 'g', 0, 0))
5603 else if (qtdemux->major_brand == FOURCC_qt__)
5606 fmt = "ISO MP4/M4A";
5608 GST_LOG_OBJECT (qtdemux, "mapped %" GST_FOURCC_FORMAT " to '%s'",
5609 GST_FOURCC_ARGS (qtdemux->major_brand), fmt);
5611 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_CONTAINER_FORMAT,
5617 /* we have read th complete moov node now.
5618 * This function parses all of the relevant info, creates the traks and
5619 * prepares all data structures for playback
5622 qtdemux_parse_tree (GstQTDemux * qtdemux)
5629 mvhd = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_mvhd);
5631 GST_LOG_OBJECT (qtdemux, "No mvhd node found, looking for redirects.");
5632 return qtdemux_parse_redirects (qtdemux);
5635 qtdemux->timescale = QT_UINT32 ((guint8 *) mvhd->data + 20);
5636 qtdemux->duration = QT_UINT32 ((guint8 *) mvhd->data + 24);
5638 GST_INFO_OBJECT (qtdemux, "timescale: %u", qtdemux->timescale);
5639 GST_INFO_OBJECT (qtdemux, "duration: %u", qtdemux->duration);
5641 /* set duration in the segment info */
5642 gst_qtdemux_get_duration (qtdemux, &duration);
5643 gst_segment_set_duration (&qtdemux->segment, GST_FORMAT_TIME, duration);
5645 /* parse all traks */
5646 trak = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_trak);
5648 qtdemux_parse_trak (qtdemux, trak);
5649 /* iterate all siblings */
5650 trak = qtdemux_tree_get_sibling_by_type (trak, FOURCC_trak);
5652 gst_element_no_more_pads (GST_ELEMENT_CAST (qtdemux));
5654 /* find and push tags, we do this after adding the pads so we can push the
5655 * tags downstream as well. */
5656 udta = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_udta);
5658 qtdemux_parse_udta (qtdemux, udta);
5660 GST_LOG_OBJECT (qtdemux, "No udta node found.");
5663 qtdemux->tag_list = qtdemux_add_container_format (qtdemux, qtdemux->tag_list);
5664 GST_INFO_OBJECT (qtdemux, "posting global tags: %" GST_PTR_FORMAT,
5666 /* post now, send event on pads later */
5667 gst_element_post_message (GST_ELEMENT (qtdemux),
5668 gst_message_new_tag (GST_OBJECT (qtdemux),
5669 gst_tag_list_copy (qtdemux->tag_list)));
5674 /* taken from ffmpeg */
5676 get_size (guint8 * ptr, guint8 ** end)
5685 len = (len << 7) | (c & 0x7f);
5694 /* this can change the codec originally present in @list */
5696 gst_qtdemux_handle_esds (GstQTDemux * qtdemux, QtDemuxStream * stream,
5697 GNode * esds, GstTagList * list)
5699 int len = QT_UINT32 (esds->data);
5700 guint8 *ptr = esds->data;
5701 guint8 *end = ptr + len;
5703 guint8 *data_ptr = NULL;
5705 guint8 object_type_id = 0;
5706 char *codec_name = NULL;
5707 GstCaps *caps = NULL;
5709 GST_MEMDUMP_OBJECT (qtdemux, "esds", ptr, len);
5711 GST_DEBUG_OBJECT (qtdemux, "version/flags = %08x", QT_UINT32 (ptr));
5714 tag = QT_UINT8 (ptr);
5715 GST_DEBUG_OBJECT (qtdemux, "tag = %02x", tag);
5717 len = get_size (ptr, &ptr);
5718 GST_DEBUG_OBJECT (qtdemux, "len = %d", len);
5722 GST_DEBUG_OBJECT (qtdemux, "ID %04x", QT_UINT16 (ptr));
5723 GST_DEBUG_OBJECT (qtdemux, "priority %04x", QT_UINT8 (ptr + 2));
5727 object_type_id = QT_UINT8 (ptr);
5728 GST_DEBUG_OBJECT (qtdemux, "object_type_id %02x", object_type_id);
5729 GST_DEBUG_OBJECT (qtdemux, "stream_type %02x", QT_UINT8 (ptr + 1));
5730 GST_DEBUG_OBJECT (qtdemux, "buffer_size_db %02x", QT_UINT24 (ptr + 2));
5731 GST_DEBUG_OBJECT (qtdemux, "max bitrate %d", QT_UINT32 (ptr + 5));
5732 GST_DEBUG_OBJECT (qtdemux, "avg bitrate %d", QT_UINT32 (ptr + 9));
5736 GST_MEMDUMP_OBJECT (qtdemux, "data", ptr, len);
5742 GST_DEBUG_OBJECT (qtdemux, "data %02x", QT_UINT8 (ptr));
5746 GST_ERROR_OBJECT (qtdemux, "parse error");
5751 /* object_type_id in the esds atom in mp4a and mp4v tells us which codec is
5752 * in use, and should also be used to override some other parameters for some
5754 switch (object_type_id) {
5755 case 0x20: /* MPEG-4 */
5756 break; /* Nothing special needed here */
5757 case 0x21: /* H.264 */
5758 codec_name = "H.264 / AVC";
5759 caps = gst_caps_new_simple ("video/x-h264", NULL);
5761 case 0x40: /* AAC (any) */
5762 case 0x66: /* AAC Main */
5763 case 0x67: /* AAC LC */
5764 case 0x68: /* AAC SSR */
5765 /* Override channels and rate based on the codec_data, as it's often
5767 if (data_ptr && data_len >= 2) {
5768 guint channels, rateindex;
5769 int rates[] = { 96000, 88200, 64000, 48000, 44100, 32000,
5770 24000, 22050, 16000, 12000, 11025, 8000
5773 channels = (data_ptr[1] & 0x7f) >> 3;
5774 if (channels <= 7) {
5775 stream->n_channels = channels;
5778 rateindex = ((data_ptr[0] & 0x7) << 1) | ((data_ptr[1] & 0x80) >> 7);
5779 if (rateindex < sizeof (rates) / sizeof (*rates)) {
5780 stream->rate = rates[rateindex];
5784 case 0x60: /* MPEG-2, various profiles */
5790 codec_name = "MPEG-2 video";
5792 gst_caps_unref (stream->caps);
5793 stream->caps = gst_caps_new_simple ("video/mpeg",
5794 "mpegversion", G_TYPE_INT, 2,
5795 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
5797 case 0x69: /* MP3 has two different values, accept either */
5799 /* change to mpeg1 layer 3 audio */
5800 gst_caps_set_simple (stream->caps, "layer", G_TYPE_INT, 3,
5801 "mpegversion", G_TYPE_INT, 1, NULL);
5802 codec_name = "MPEG-1 layer 3";
5804 case 0x6A: /* MPEG-1 */
5805 codec_name = "MPEG-1 video";
5807 gst_caps_unref (stream->caps);
5808 stream->caps = gst_caps_new_simple ("video/mpeg",
5809 "mpegversion", G_TYPE_INT, 1,
5810 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
5812 case 0x6C: /* MJPEG */
5813 caps = gst_caps_new_simple ("image/jpeg", NULL);
5814 codec_name = "Motion-JPEG";
5816 case 0x6D: /* PNG */
5817 caps = gst_caps_new_simple ("image/png", NULL);
5818 codec_name = "PNG still images";
5820 case 0x6E: /* JPEG2000 */
5821 codec_name = "JPEG-2000";
5822 caps = gst_caps_new_simple ("image/x-j2c", "fields", G_TYPE_INT, 1, NULL);
5824 case 0xA4: /* Dirac */
5825 codec_name = "Dirac";
5826 caps = gst_caps_new_simple ("video/x-dirac", NULL);
5828 case 0xA5: /* AC3 */
5829 codec_name = "AC-3 audio";
5830 caps = gst_caps_new_simple ("audio/x-ac3", NULL);
5832 case 0xE1: /* QCELP */
5833 /* QCELP, the codec_data is a riff tag (little endian) with
5834 * more info (http://ftp.3gpp2.org/TSGC/Working/2003/2003-05-SanDiego/TSG-C-2003-05-San%20Diego/WG1/SWG12/C12-20030512-006%20=%20C12-20030217-015_Draft_Baseline%20Text%20of%20FFMS_R2.doc). */
5835 caps = gst_caps_new_simple ("audio/qcelp", NULL);
5836 codec_name = "QCELP";
5842 /* If we have a replacement caps, then change our caps for this stream */
5844 gst_caps_unref (stream->caps);
5845 stream->caps = caps;
5848 if (codec_name && list)
5849 gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
5850 GST_TAG_AUDIO_CODEC, codec_name, NULL);
5852 /* Add the codec_data attribute to caps, if we have it */
5856 buffer = gst_buffer_new_and_alloc (data_len);
5857 memcpy (GST_BUFFER_DATA (buffer), data_ptr, data_len);
5859 GST_DEBUG_OBJECT (qtdemux, "setting codec_data from esds");
5860 GST_MEMDUMP_OBJECT (qtdemux, "codec_data from esds", data_ptr, data_len);
5862 gst_caps_set_simple (stream->caps, "codec_data", GST_TYPE_BUFFER,
5864 gst_buffer_unref (buffer);
5869 #define _codec(name) \
5872 *codec_name = g_strdup (name); \
5877 qtdemux_video_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
5878 guint32 fourcc, const guint8 * stsd_data, gchar ** codec_name)
5881 const GstStructure *s;
5885 case GST_MAKE_FOURCC ('p', 'n', 'g', ' '):
5886 _codec ("PNG still images");
5887 caps = gst_caps_new_simple ("image/png", NULL);
5889 case GST_MAKE_FOURCC ('j', 'p', 'e', 'g'):
5890 _codec ("JPEG still images");
5891 caps = gst_caps_new_simple ("image/jpeg", NULL);
5893 case GST_MAKE_FOURCC ('m', 'j', 'p', 'a'):
5894 case GST_MAKE_FOURCC ('A', 'V', 'D', 'J'):
5895 case GST_MAKE_FOURCC ('M', 'J', 'P', 'G'):
5896 _codec ("Motion-JPEG");
5897 caps = gst_caps_new_simple ("image/jpeg", NULL);
5899 case GST_MAKE_FOURCC ('m', 'j', 'p', 'b'):
5900 _codec ("Motion-JPEG format B");
5901 caps = gst_caps_new_simple ("video/x-mjpeg-b", NULL);
5903 case GST_MAKE_FOURCC ('m', 'j', 'p', '2'):
5904 _codec ("JPEG-2000");
5905 /* override to what it should be according to spec, avoid palette_data */
5906 stream->bits_per_sample = 24;
5907 caps = gst_caps_new_simple ("image/x-j2c", "fields", G_TYPE_INT, 1, NULL);
5909 case GST_MAKE_FOURCC ('S', 'V', 'Q', '3'):
5910 _codec ("Sorensen video v.3");
5911 caps = gst_caps_new_simple ("video/x-svq",
5912 "svqversion", G_TYPE_INT, 3, NULL);
5914 case GST_MAKE_FOURCC ('s', 'v', 'q', 'i'):
5915 case GST_MAKE_FOURCC ('S', 'V', 'Q', '1'):
5916 _codec ("Sorensen video v.1");
5917 caps = gst_caps_new_simple ("video/x-svq",
5918 "svqversion", G_TYPE_INT, 1, NULL);
5920 case GST_MAKE_FOURCC ('r', 'a', 'w', ' '):
5924 _codec ("Raw RGB video");
5925 bps = QT_UINT16 (stsd_data + 98);
5926 /* set common stuff */
5927 caps = gst_caps_new_simple ("video/x-raw-rgb",
5928 "endianness", G_TYPE_INT, G_BYTE_ORDER, "depth", G_TYPE_INT, bps,
5933 gst_caps_set_simple (caps,
5934 "bpp", G_TYPE_INT, 16,
5935 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
5936 "red_mask", G_TYPE_INT, 0x7c00,
5937 "green_mask", G_TYPE_INT, 0x03e0,
5938 "blue_mask", G_TYPE_INT, 0x001f, NULL);
5941 gst_caps_set_simple (caps,
5942 "bpp", G_TYPE_INT, 16,
5943 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
5944 "red_mask", G_TYPE_INT, 0xf800,
5945 "green_mask", G_TYPE_INT, 0x07e0,
5946 "blue_mask", G_TYPE_INT, 0x001f, NULL);
5949 gst_caps_set_simple (caps,
5950 "bpp", G_TYPE_INT, 24,
5951 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
5952 "red_mask", G_TYPE_INT, 0xff0000,
5953 "green_mask", G_TYPE_INT, 0x00ff00,
5954 "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
5957 gst_caps_set_simple (caps,
5958 "bpp", G_TYPE_INT, 32,
5959 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
5960 "alpha_mask", G_TYPE_INT, 0xff000000,
5961 "red_mask", G_TYPE_INT, 0x00ff0000,
5962 "green_mask", G_TYPE_INT, 0x0000ff00,
5963 "blue_mask", G_TYPE_INT, 0x000000ff, NULL);
5971 case GST_MAKE_FOURCC ('y', 'v', '1', '2'):
5972 _codec ("Raw planar YUV 4:2:0");
5973 caps = gst_caps_new_simple ("video/x-raw-yuv",
5974 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'),
5977 case GST_MAKE_FOURCC ('y', 'u', 'v', '2'):
5978 case GST_MAKE_FOURCC ('Y', 'u', 'v', '2'):
5979 _codec ("Raw packed YUV 4:2:2");
5980 caps = gst_caps_new_simple ("video/x-raw-yuv",
5981 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'),
5984 case GST_MAKE_FOURCC ('2', 'v', 'u', 'y'):
5985 _codec ("Raw packed YUV 4:2:0");
5986 caps = gst_caps_new_simple ("video/x-raw-yuv",
5987 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
5990 case GST_MAKE_FOURCC ('v', '2', '1', '0'):
5991 _codec ("Raw packed YUV 10-bit 4:2:2");
5992 caps = gst_caps_new_simple ("video/x-raw-yuv",
5993 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('v', '2', '1', '0'),
5996 case GST_MAKE_FOURCC ('m', 'p', 'e', 'g'):
5997 case GST_MAKE_FOURCC ('m', 'p', 'g', '1'):
5998 _codec ("MPEG-1 video");
5999 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 1,
6000 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6002 case GST_MAKE_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
6003 case GST_MAKE_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
6004 case GST_MAKE_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
6005 case GST_MAKE_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
6006 case GST_MAKE_FOURCC ('h', 'd', 'v', '6'): // HDV 1080i60
6007 case GST_MAKE_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
6008 case GST_MAKE_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
6009 case GST_MAKE_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
6010 case GST_MAKE_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
6011 case GST_MAKE_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
6012 case GST_MAKE_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
6013 case GST_MAKE_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
6014 case GST_MAKE_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
6015 case GST_MAKE_FOURCC ('m', 'p', 'g', '2'): // AVID IMX PAL
6016 _codec ("MPEG-2 video");
6017 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 2,
6018 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6020 case GST_MAKE_FOURCC ('g', 'i', 'f', ' '):
6021 _codec ("GIF still images");
6022 caps = gst_caps_new_simple ("image/gif", NULL);
6024 case GST_MAKE_FOURCC ('h', '2', '6', '3'):
6025 case GST_MAKE_FOURCC ('H', '2', '6', '3'):
6026 case GST_MAKE_FOURCC ('s', '2', '6', '3'):
6027 case GST_MAKE_FOURCC ('U', '2', '6', '3'):
6029 /* ffmpeg uses the height/width props, don't know why */
6030 caps = gst_caps_new_simple ("video/x-h263", NULL);
6032 case GST_MAKE_FOURCC ('m', 'p', '4', 'v'):
6033 _codec ("MPEG-4 video");
6034 caps = gst_caps_new_simple ("video/mpeg", "mpegversion", G_TYPE_INT, 4,
6035 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6037 case GST_MAKE_FOURCC ('3', 'i', 'v', 'd'):
6038 case GST_MAKE_FOURCC ('3', 'I', 'V', 'D'):
6039 _codec ("Microsoft MPEG-4 4.3"); /* FIXME? */
6040 caps = gst_caps_new_simple ("video/x-msmpeg",
6041 "msmpegversion", G_TYPE_INT, 43, NULL);
6043 case GST_MAKE_FOURCC ('3', 'I', 'V', '1'):
6044 case GST_MAKE_FOURCC ('3', 'I', 'V', '2'):
6045 _codec ("3ivX video");
6046 caps = gst_caps_new_simple ("video/x-3ivx", NULL);
6048 case GST_MAKE_FOURCC ('D', 'I', 'V', '3'):
6050 caps = gst_caps_new_simple ("video/x-divx",
6051 "divxversion", G_TYPE_INT, 3, NULL);
6053 case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
6054 case GST_MAKE_FOURCC ('d', 'i', 'v', 'x'):
6056 caps = gst_caps_new_simple ("video/x-divx",
6057 "divxversion", G_TYPE_INT, 4, NULL);
6059 case GST_MAKE_FOURCC ('D', 'X', '5', '0'):
6061 caps = gst_caps_new_simple ("video/x-divx",
6062 "divxversion", G_TYPE_INT, 5, NULL);
6064 case GST_MAKE_FOURCC ('X', 'V', 'I', 'D'):
6065 case GST_MAKE_FOURCC ('x', 'v', 'i', 'd'):
6066 _codec ("XVID MPEG-4");
6067 caps = gst_caps_new_simple ("video/x-xvid", NULL);
6070 case GST_MAKE_FOURCC ('F', 'M', 'P', '4'):
6071 case GST_MAKE_FOURCC ('U', 'M', 'P', '4'):
6072 caps = gst_caps_new_simple ("video/mpeg",
6073 "mpegversion", G_TYPE_INT, 4, NULL);
6075 *codec_name = g_strdup ("FFmpeg MPEG-4");
6078 case GST_MAKE_FOURCC ('c', 'v', 'i', 'd'):
6080 caps = gst_caps_new_simple ("video/x-cinepak", NULL);
6082 case GST_MAKE_FOURCC ('q', 'd', 'r', 'w'):
6083 _codec ("Apple QuickDraw");
6084 caps = gst_caps_new_simple ("video/x-qdrw", NULL);
6086 case GST_MAKE_FOURCC ('r', 'p', 'z', 'a'):
6087 _codec ("Apple video");
6088 caps = gst_caps_new_simple ("video/x-apple-video", NULL);
6090 case GST_MAKE_FOURCC ('a', 'v', 'c', '1'):
6091 _codec ("H.264 / AVC");
6092 caps = gst_caps_new_simple ("video/x-h264", NULL);
6094 case GST_MAKE_FOURCC ('r', 'l', 'e', ' '):
6095 _codec ("Run-length encoding");
6096 caps = gst_caps_new_simple ("video/x-rle",
6097 "layout", G_TYPE_STRING, "quicktime", NULL);
6099 case GST_MAKE_FOURCC ('i', 'v', '3', '2'):
6100 _codec ("Indeo Video 3");
6101 caps = gst_caps_new_simple ("video/x-indeo",
6102 "indeoversion", G_TYPE_INT, 3, NULL);
6104 case GST_MAKE_FOURCC ('I', 'V', '4', '1'):
6105 case GST_MAKE_FOURCC ('i', 'v', '4', '1'):
6106 _codec ("Intel Video 4");
6107 caps = gst_caps_new_simple ("video/x-indeo",
6108 "indeoversion", G_TYPE_INT, 4, NULL);
6110 case GST_MAKE_FOURCC ('d', 'v', 'c', 'p'):
6111 case GST_MAKE_FOURCC ('d', 'v', 'c', ' '):
6112 case GST_MAKE_FOURCC ('d', 'v', 's', 'd'):
6113 case GST_MAKE_FOURCC ('D', 'V', 'S', 'D'):
6114 case GST_MAKE_FOURCC ('d', 'v', 'c', 's'):
6115 case GST_MAKE_FOURCC ('D', 'V', 'C', 'S'):
6116 case GST_MAKE_FOURCC ('d', 'v', '2', '5'):
6117 case GST_MAKE_FOURCC ('d', 'v', 'p', 'p'):
6118 _codec ("DV Video");
6119 caps = gst_caps_new_simple ("video/x-dv", "dvversion", G_TYPE_INT, 25,
6120 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6122 case GST_MAKE_FOURCC ('d', 'v', '5', 'n'): //DVCPRO50 NTSC
6123 case GST_MAKE_FOURCC ('d', 'v', '5', 'p'): //DVCPRO50 PAL
6124 _codec ("DVCPro50 Video");
6125 caps = gst_caps_new_simple ("video/x-dv", "dvversion", G_TYPE_INT, 50,
6126 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6128 case GST_MAKE_FOURCC ('d', 'v', 'h', '5'): //DVCPRO HD 50i produced by FCP
6129 case GST_MAKE_FOURCC ('d', 'v', 'h', '6'): //DVCPRO HD 60i produced by FCP
6130 _codec ("DVCProHD Video");
6131 caps = gst_caps_new_simple ("video/x-dv", "dvversion", G_TYPE_INT, 100,
6132 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
6134 case GST_MAKE_FOURCC ('s', 'm', 'c', ' '):
6135 _codec ("Apple Graphics (SMC)");
6136 caps = gst_caps_new_simple ("video/x-smc", NULL);
6138 case GST_MAKE_FOURCC ('V', 'P', '3', '1'):
6140 caps = gst_caps_new_simple ("video/x-vp3", NULL);
6142 case GST_MAKE_FOURCC ('X', 'i', 'T', 'h'):
6144 caps = gst_caps_new_simple ("video/x-theora", NULL);
6145 /* theora uses one byte of padding in the data stream because it does not
6146 * allow 0 sized packets while theora does */
6147 stream->padding = 1;
6149 case GST_MAKE_FOURCC ('d', 'r', 'a', 'c'):
6151 caps = gst_caps_new_simple ("video/x-dirac", NULL);
6153 case GST_MAKE_FOURCC ('t', 'i', 'f', 'f'):
6154 _codec ("TIFF still images");
6155 caps = gst_caps_new_simple ("image/tiff", NULL);
6157 case GST_MAKE_FOURCC ('i', 'c', 'o', 'd'):
6158 _codec ("Apple Intermediate Codec");
6159 caps = gst_caps_from_string ("video/x-apple-intermediate-codec");
6161 case GST_MAKE_FOURCC ('A', 'V', 'd', 'n'):
6162 _codec ("AVID DNxHD");
6163 caps = gst_caps_from_string ("video/x-dnxhd");
6165 case GST_MAKE_FOURCC ('k', 'p', 'c', 'd'):
6170 s = g_strdup_printf ("video/x-gst-fourcc-%" GST_FOURCC_FORMAT,
6171 GST_FOURCC_ARGS (fourcc));
6172 caps = gst_caps_new_simple (s, NULL);
6177 /* enable clipping for raw video streams */
6178 s = gst_caps_get_structure (caps, 0);
6179 name = gst_structure_get_name (s);
6180 if (g_str_has_prefix (name, "video/x-raw-")) {
6181 stream->need_clip = TRUE;
6187 qtdemux_audio_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
6188 guint32 fourcc, const guint8 * data, int len, gchar ** codec_name)
6191 const GstStructure *s;
6195 GST_DEBUG_OBJECT (qtdemux, "resolve fourcc %08x", fourcc);
6198 case GST_MAKE_FOURCC ('N', 'O', 'N', 'E'):
6199 case GST_MAKE_FOURCC ('r', 'a', 'w', ' '):
6200 _codec ("Raw 8-bit PCM audio");
6201 caps = gst_caps_new_simple ("audio/x-raw-int", "width", G_TYPE_INT, 8,
6202 "depth", G_TYPE_INT, 8, "signed", G_TYPE_BOOLEAN, FALSE, NULL);
6204 case GST_MAKE_FOURCC ('t', 'w', 'o', 's'):
6205 endian = G_BIG_ENDIAN;
6207 case GST_MAKE_FOURCC ('s', 'o', 'w', 't'):
6213 endian = G_LITTLE_ENDIAN;
6215 depth = stream->bytes_per_packet * 8;
6216 str = g_strdup_printf ("Raw %d-bit PCM audio", depth);
6219 caps = gst_caps_new_simple ("audio/x-raw-int",
6220 "width", G_TYPE_INT, depth, "depth", G_TYPE_INT, depth,
6221 "endianness", G_TYPE_INT, endian,
6222 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
6225 case GST_MAKE_FOURCC ('f', 'l', '6', '4'):
6226 _codec ("Raw 64-bit floating-point audio");
6227 caps = gst_caps_new_simple ("audio/x-raw-float", "width", G_TYPE_INT, 64,
6228 "endianness", G_TYPE_INT, G_BIG_ENDIAN, NULL);
6230 case GST_MAKE_FOURCC ('f', 'l', '3', '2'):
6231 _codec ("Raw 32-bit floating-point audio");
6232 caps = gst_caps_new_simple ("audio/x-raw-float", "width", G_TYPE_INT, 32,
6233 "endianness", G_TYPE_INT, G_BIG_ENDIAN, NULL);
6236 _codec ("Raw 24-bit PCM audio");
6237 /* we assume BIG ENDIAN, an enda box will tell us to change this to little
6239 caps = gst_caps_new_simple ("audio/x-raw-int", "width", G_TYPE_INT, 24,
6240 "depth", G_TYPE_INT, 24,
6241 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
6242 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
6244 case GST_MAKE_FOURCC ('i', 'n', '3', '2'):
6245 _codec ("Raw 32-bit PCM audio");
6246 caps = gst_caps_new_simple ("audio/x-raw-int", "width", G_TYPE_INT, 32,
6247 "depth", G_TYPE_INT, 32,
6248 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
6249 "signed", G_TYPE_BOOLEAN, TRUE, NULL);
6251 case GST_MAKE_FOURCC ('u', 'l', 'a', 'w'):
6252 _codec ("Mu-law audio");
6253 caps = gst_caps_new_simple ("audio/x-mulaw", NULL);
6255 case GST_MAKE_FOURCC ('a', 'l', 'a', 'w'):
6256 _codec ("A-law audio");
6257 caps = gst_caps_new_simple ("audio/x-alaw", NULL);
6261 _codec ("Microsoft ADPCM");
6262 /* Microsoft ADPCM-ACM code 2 */
6263 caps = gst_caps_new_simple ("audio/x-adpcm",
6264 "layout", G_TYPE_STRING, "microsoft", NULL);
6268 _codec ("IMA Loki SDL MJPEG ADPCM");
6269 /* Loki ADPCM, See #550288 for a file that only decodes
6270 * with the smjpeg variant of the ADPCM decoder. */
6271 caps = gst_caps_new_simple ("audio/x-adpcm",
6272 "layout", G_TYPE_STRING, "smjpeg", NULL);
6276 _codec ("DVI/Intel IMA ADPCM");
6277 /* FIXME DVI/Intel IMA ADPCM/ACM code 17 */
6278 caps = gst_caps_new_simple ("audio/x-adpcm",
6279 "layout", G_TYPE_STRING, "quicktime", NULL);
6283 /* MPEG layer 3, CBR only (pre QT4.1) */
6284 case GST_MAKE_FOURCC ('.', 'm', 'p', '3'):
6285 _codec ("MPEG-1 layer 3");
6286 /* MPEG layer 3, CBR & VBR (QT4.1 and later) */
6287 caps = gst_caps_new_simple ("audio/mpeg", "layer", G_TYPE_INT, 3,
6288 "mpegversion", G_TYPE_INT, 1, NULL);
6291 case GST_MAKE_FOURCC ('a', 'c', '-', '3'):
6292 _codec ("AC-3 audio");
6293 caps = gst_caps_new_simple ("audio/x-ac3", NULL);
6294 stream->sampled = TRUE;
6296 case GST_MAKE_FOURCC ('M', 'A', 'C', '3'):
6298 caps = gst_caps_new_simple ("audio/x-mace",
6299 "maceversion", G_TYPE_INT, 3, NULL);
6301 case GST_MAKE_FOURCC ('M', 'A', 'C', '6'):
6303 caps = gst_caps_new_simple ("audio/x-mace",
6304 "maceversion", G_TYPE_INT, 6, NULL);
6306 case GST_MAKE_FOURCC ('O', 'g', 'g', 'V'):
6308 caps = gst_caps_new_simple ("application/ogg", NULL);
6310 case GST_MAKE_FOURCC ('d', 'v', 'c', 'a'):
6311 _codec ("DV audio");
6312 caps = gst_caps_new_simple ("audio/x-dv", NULL);
6314 case GST_MAKE_FOURCC ('m', 'p', '4', 'a'):
6315 _codec ("MPEG-4 AAC audio");
6316 caps = gst_caps_new_simple ("audio/mpeg",
6317 "mpegversion", G_TYPE_INT, 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL);
6319 case GST_MAKE_FOURCC ('Q', 'D', 'M', 'C'):
6320 _codec ("QDesign Music");
6321 caps = gst_caps_new_simple ("audio/x-qdm", NULL);
6323 case GST_MAKE_FOURCC ('Q', 'D', 'M', '2'):
6324 _codec ("QDesign Music v.2");
6325 /* FIXME: QDesign music version 2 (no constant) */
6327 caps = gst_caps_new_simple ("audio/x-qdm2",
6328 "framesize", G_TYPE_INT, QT_UINT32 (data + 52),
6329 "bitrate", G_TYPE_INT, QT_UINT32 (data + 40),
6330 "blocksize", G_TYPE_INT, QT_UINT32 (data + 44), NULL);
6332 caps = gst_caps_new_simple ("audio/x-qdm2", NULL);
6335 case GST_MAKE_FOURCC ('a', 'g', 's', 'm'):
6336 _codec ("GSM audio");
6337 caps = gst_caps_new_simple ("audio/x-gsm", NULL);
6339 case GST_MAKE_FOURCC ('s', 'a', 'm', 'r'):
6340 _codec ("AMR audio");
6341 caps = gst_caps_new_simple ("audio/AMR", NULL);
6343 case GST_MAKE_FOURCC ('s', 'a', 'w', 'b'):
6344 _codec ("AMR-WB audio");
6345 caps = gst_caps_new_simple ("audio/AMR-WB", NULL);
6347 case GST_MAKE_FOURCC ('i', 'm', 'a', '4'):
6348 _codec ("Quicktime IMA ADPCM");
6349 caps = gst_caps_new_simple ("audio/x-adpcm",
6350 "layout", G_TYPE_STRING, "quicktime", NULL);
6352 case GST_MAKE_FOURCC ('a', 'l', 'a', 'c'):
6353 _codec ("Apple lossless audio");
6354 caps = gst_caps_new_simple ("audio/x-alac", NULL);
6356 case GST_MAKE_FOURCC ('Q', 'c', 'l', 'p'):
6357 _codec ("QualComm PureVoice");
6358 caps = gst_caps_from_string ("audio/qcelp");
6360 case GST_MAKE_FOURCC ('q', 't', 'v', 'r'):
6366 s = g_strdup_printf ("audio/x-gst-fourcc-%" GST_FOURCC_FORMAT,
6367 GST_FOURCC_ARGS (fourcc));
6368 caps = gst_caps_new_simple (s, NULL);
6373 /* enable clipping for raw audio streams */
6374 s = gst_caps_get_structure (caps, 0);
6375 name = gst_structure_get_name (s);
6376 if (g_str_has_prefix (name, "audio/x-raw-")) {
6377 stream->need_clip = TRUE;
6383 qtdemux_subp_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
6384 guint32 fourcc, const guint8 * stsd_data, gchar ** codec_name)
6388 GST_DEBUG_OBJECT (qtdemux, "resolve fourcc %08x", fourcc);
6391 case GST_MAKE_FOURCC ('m', 'p', '4', 's'):
6392 _codec ("DVD subtitle");
6393 caps = gst_caps_new_simple ("video/x-dvd-subpicture", NULL);
6399 s = g_strdup_printf ("audio/x-gst-fourcc-%" GST_FOURCC_FORMAT,
6400 GST_FOURCC_ARGS (fourcc));
6401 caps = gst_caps_new_simple (s, NULL);