8f1ebd1af746ff84244fa5e39e6734c92b4f6545
[platform/upstream/gstreamer.git] / gst / qtdemux / qtdemux.c
1 /* GStreamer
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>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:element-qtdemux
26  *
27  * Demuxes a .mov file into raw or compressed audio and/or video streams.
28  *
29  * This element supports both push and pull-based scheduling, depending on the
30  * capabilities of the upstream elements.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
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.
40  * </refsect2>
41  *
42  * Last reviewed on 2006-12-29 (0.10.5)
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "gst/gst-i18n-plugin.h"
50
51 #include <gst/tag/tag.h>
52
53 #include "qtatomparser.h"
54 #include "qtdemux_types.h"
55 #include "qtdemux_dump.h"
56 #include "qtdemux_fourcc.h"
57 #include "qtdemux.h"
58 #include "qtpalette.h"
59
60 #include <stdlib.h>
61 #include <string.h>
62
63 #ifdef HAVE_ZLIB
64 # include <zlib.h>
65 #endif
66
67 /* max. size considered 'sane' for non-mdat atoms */
68 #define QTDEMUX_MAX_ATOM_SIZE (25*1024*1024)
69
70 /* if the sample index is larger than this, something is likely wrong */
71 #define QTDEMUX_MAX_SAMPLE_INDEX_SIZE (50*1024*1024)
72
73 GST_DEBUG_CATEGORY (qtdemux_debug);
74
75 /*typedef struct _QtNode QtNode; */
76 typedef struct _QtDemuxSegment QtDemuxSegment;
77 typedef struct _QtDemuxSample QtDemuxSample;
78
79 /*struct _QtNode
80 {
81   guint32 type;
82   guint8 *data;
83   gint len;
84 };*/
85
86 struct _QtDemuxSample
87 {
88   guint32 size;
89   guint64 offset;
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 */
94 };
95
96 /*
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.
101  *
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.
105  *
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:
109  *
110  * segment 1: media_time: 1 second, duration: 1 second, rate 1
111  * segment 2: media_time: 3 second, duration: 2 second, rate 2
112  *
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
115  * at a rate of 2.
116  *
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
120  * track anymore.
121  *
122  * Visually, assuming the track has 4 second of media_time:
123  *
124  *                (a)                   (b)          (c)              (d)
125  *         .-----------------------------------------------------------.
126  * track:  | K.....K.........K........K.......K.......K...........K... |
127  *         '-----------------------------------------------------------'
128  *         0              1              2              3              4    
129  *           .------------^              ^   .----------^              ^
130  *          /              .-------------'  /       .------------------'
131  *         /              /          .-----'       /
132  *         .--------------.         .--------------.
133  *         | segment 1    |         | segment 2    |
134  *         '--------------'         '--------------'
135  *       
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.
139  *
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.
144  *
145  * We then proceed to push data from keyframe (a) to frame (b). The decoder
146  * decodes but clips all before media_time 1.
147  * 
148  * After finishing a segment, we push out a new SEGMENT event with the clipping
149  * boundaries of the new data.
150  *
151  * This is a good usecase for the GStreamer accumulated SEGMENT events.
152  */
153
154 struct _QtDemuxSegment
155 {
156   /* global time and duration, all gst time */
157   guint64 time;
158   guint64 stop_time;
159   guint64 duration;
160   /* media time of trak, all gst time */
161   guint64 media_start;
162   guint64 media_stop;
163   gdouble rate;
164 };
165
166 struct _QtDemuxStream
167 {
168   GstPad *pad;
169
170   /* stream type */
171   guint32 subtype;
172   GstCaps *caps;
173   guint32 fourcc;
174
175   /* duration/scale */
176   guint64 duration;             /* in timescale */
177   guint32 timescale;
178
179   /* our samples */
180   guint32 n_samples;
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 */
185
186   /* if we use chunks or samples */
187   gboolean sampled;
188   guint padding;
189
190   /* video info */
191   gint width;
192   gint height;
193   /* aspect ratio */
194   gint display_width;
195   gint display_height;
196   gint par_w;
197   gint par_h;
198   /* Numerator/denominator framerate */
199   gint fps_n;
200   gint fps_d;
201   guint16 bits_per_sample;
202   guint16 color_table_id;
203
204   /* audio info */
205   gdouble rate;
206   gint n_channels;
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;
212   guint compression;
213
214   /* when a discontinuity is pending */
215   gboolean discont;
216
217   /* list of buffers to push first */
218   GSList *buffers;
219
220   /* if we need to clip this buffer. This is only needed for uncompressed
221    * data */
222   gboolean need_clip;
223
224   /* current position */
225   guint32 segment_index;
226   guint32 sample_index;
227   guint64 time_position;        /* in gst time */
228
229   /* the Gst segment we are processing out, used for clipping */
230   GstSegment segment;
231
232   /* last GstFlowReturn */
233   GstFlowReturn last_ret;
234
235   /* quicktime segments */
236   guint32 n_segments;
237   QtDemuxSegment *segments;
238   guint32 from_sample;
239   guint32 to_sample;
240
241   gboolean sent_eos;
242   GstTagList *pending_tags;
243   gboolean send_global_tags;
244 };
245
246 enum QtDemuxState
247 {
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 */
252 };
253
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);
258
259 static const GstElementDetails gst_qtdemux_details =
260 GST_ELEMENT_DETAILS ("QuickTime demuxer",
261     "Codec/Demuxer",
262     "Demultiplex a QuickTime file into audio and video streams",
263     "David Schleef <ds@schleef.org>, Wim Taymans <wim@fluendo.com>");
264
265 static GstStaticPadTemplate gst_qtdemux_sink_template =
266     GST_STATIC_PAD_TEMPLATE ("sink",
267     GST_PAD_SINK,
268     GST_PAD_ALWAYS,
269     GST_STATIC_CAPS ("video/quicktime; video/mj2; audio/x-m4a; "
270         "application/x-3gp")
271     );
272
273 static GstStaticPadTemplate gst_qtdemux_videosrc_template =
274 GST_STATIC_PAD_TEMPLATE ("video_%02d",
275     GST_PAD_SRC,
276     GST_PAD_SOMETIMES,
277     GST_STATIC_CAPS_ANY);
278
279 static GstStaticPadTemplate gst_qtdemux_audiosrc_template =
280 GST_STATIC_PAD_TEMPLATE ("audio_%02d",
281     GST_PAD_SRC,
282     GST_PAD_SOMETIMES,
283     GST_STATIC_CAPS_ANY);
284
285 static GstStaticPadTemplate gst_qtdemux_subpsrc_template =
286 GST_STATIC_PAD_TEMPLATE ("subp_%02d",
287     GST_PAD_SRC,
288     GST_PAD_SOMETIMES,
289     GST_STATIC_CAPS_ANY);
290
291 static GstElementClass *parent_class = NULL;
292
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);
297
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);
303
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);
307
308 static gboolean qtdemux_parse_moov (GstQTDemux * qtdemux, const guint8 * buffer,
309     guint length);
310 static gboolean qtdemux_parse_node (GstQTDemux * qtdemux, GNode * node,
311     const guint8 * buffer, guint length);
312 static gboolean qtdemux_parse_tree (GstQTDemux * qtdemux);
313
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);
325
326 GType
327 gst_qtdemux_get_type (void)
328 {
329   static GType qtdemux_type = 0;
330
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,
338     };
339
340     qtdemux_type =
341         g_type_register_static (GST_TYPE_ELEMENT, "GstQTDemux", &qtdemux_info,
342         0);
343   }
344   return qtdemux_type;
345 }
346
347 static void
348 gst_qtdemux_base_init (GstQTDemuxClass * klass)
349 {
350   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
351
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);
359
360   GST_DEBUG_CATEGORY_INIT (qtdemux_debug, "qtdemux", 0, "qtdemux plugin");
361 }
362
363 static void
364 gst_qtdemux_class_init (GstQTDemuxClass * klass)
365 {
366   GObjectClass *gobject_class;
367   GstElementClass *gstelement_class;
368
369   gobject_class = (GObjectClass *) klass;
370   gstelement_class = (GstElementClass *) klass;
371
372   parent_class = g_type_class_peek_parent (klass);
373
374   gobject_class->dispose = gst_qtdemux_dispose;
375
376   gstelement_class->change_state = gst_qtdemux_change_state;
377 }
378
379 static void
380 gst_qtdemux_init (GstQTDemux * qtdemux)
381 {
382   qtdemux->sinkpad =
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);
392
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;
398   qtdemux->todrop = 0;
399   qtdemux->adapter = gst_adapter_new ();
400   qtdemux->offset = 0;
401   qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
402   qtdemux->mdatbuffer = NULL;
403   gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
404 }
405
406 static void
407 gst_qtdemux_dispose (GObject * object)
408 {
409   GstQTDemux *qtdemux = GST_QTDEMUX (object);
410
411   if (qtdemux->adapter) {
412     g_object_unref (G_OBJECT (qtdemux->adapter));
413     qtdemux->adapter = NULL;
414   }
415
416   G_OBJECT_CLASS (parent_class)->dispose (object);
417 }
418
419 static GstFlowReturn
420 gst_qtdemux_pull_atom (GstQTDemux * qtdemux, guint64 offset, guint64 size,
421     GstBuffer ** buf)
422 {
423   GstFlowReturn flow;
424
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;
431   }
432
433   flow = gst_pad_pull_range (qtdemux->sinkpad, offset, size, buf);
434
435   if (G_UNLIKELY (flow != GST_FLOW_OK))
436     return flow;
437
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);
443     *buf = NULL;
444     return GST_FLOW_UNEXPECTED;
445   }
446
447   return flow;
448 }
449
450 #if 0
451 static gboolean
452 gst_qtdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
453     GstFormat * dest_format, gint64 * dest_value)
454 {
455   gboolean res = TRUE;
456   QtDemuxStream *stream = gst_pad_get_element_private (pad);
457
458   if (stream->subtype == GST_MAKE_FOURCC ('v', 'i', 'd', 'e') &&
459       (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
460     return FALSE;
461
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 */
467           break;
468         case GST_FORMAT_DEFAULT:
469           *dest_value = src_value * 1;  /* FIXME */
470           break;
471         default:
472           res = FALSE;
473           break;
474       }
475       break;
476     case GST_FORMAT_BYTES:
477       switch (*dest_format) {
478         case GST_FORMAT_TIME:
479           *dest_value = src_value * 1;  /* FIXME */
480           break;
481         default:
482           res = FALSE;
483           break;
484       }
485       break;
486     case GST_FORMAT_DEFAULT:
487       switch (*dest_format) {
488         case GST_FORMAT_TIME:
489           *dest_value = src_value * 1;  /* FIXME */
490           break;
491         default:
492           res = FALSE;
493           break;
494       }
495       break;
496     default:
497       res = FALSE;
498   }
499
500   return res;
501 }
502 #endif
503
504 static const GstQueryType *
505 gst_qtdemux_get_src_query_types (GstPad * pad)
506 {
507   static const GstQueryType src_types[] = {
508     GST_QUERY_POSITION,
509     GST_QUERY_DURATION,
510     GST_QUERY_SEEKING,
511     0
512   };
513
514   return src_types;
515 }
516
517 static gboolean
518 gst_qtdemux_get_duration (GstQTDemux * qtdemux, gint64 * duration)
519 {
520   gboolean res = TRUE;
521
522   *duration = GST_CLOCK_TIME_NONE;
523
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);
528     }
529   }
530   return res;
531 }
532
533 static gboolean
534 gst_qtdemux_handle_src_query (GstPad * pad, GstQuery * query)
535 {
536   gboolean res = FALSE;
537   GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
538
539   GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
540
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);
546         res = TRUE;
547       }
548       break;
549     case GST_QUERY_DURATION:{
550       GstFormat fmt;
551
552       gst_query_parse_duration (query, &fmt, NULL);
553       if (fmt == GST_FORMAT_TIME) {
554         gint64 duration = -1;
555
556         gst_qtdemux_get_duration (qtdemux, &duration);
557         if (duration > 0) {
558           gst_query_set_duration (query, GST_FORMAT_TIME, duration);
559           res = TRUE;
560         }
561       }
562       break;
563     }
564     case GST_QUERY_SEEKING:{
565       GstFormat fmt;
566       gboolean seekable;
567
568       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
569       if (fmt == GST_FORMAT_TIME) {
570         gint64 duration = -1;
571
572         gst_qtdemux_get_duration (qtdemux, &duration);
573         seekable = TRUE;
574         if (!qtdemux->pullbased) {
575           GstQuery *q;
576
577           /* we might be able with help from upstream */
578           seekable = FALSE;
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);
583           }
584           gst_query_unref (q);
585         }
586         gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
587         res = TRUE;
588       }
589       break;
590     }
591     default:
592       res = gst_pad_query_default (pad, query);
593       break;
594   }
595
596   gst_object_unref (qtdemux);
597
598   return res;
599 }
600
601 static void
602 gst_qtdemux_push_tags (GstQTDemux * qtdemux, QtDemuxStream * stream)
603 {
604   if (G_LIKELY (stream->pad)) {
605     GST_DEBUG_OBJECT (qtdemux, "Checking pad %s:%s for tags",
606         GST_DEBUG_PAD_NAME (stream->pad));
607
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;
614     }
615
616     if (G_UNLIKELY (stream->send_global_tags && qtdemux->tag_list)) {
617       GST_DEBUG_OBJECT (qtdemux, "Sending global tags %" GST_PTR_FORMAT,
618           qtdemux->tag_list);
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;
622     }
623   }
624 }
625
626 /* push event on all source pads; takes ownership of the event */
627 static void
628 gst_qtdemux_push_event (GstQTDemux * qtdemux, GstEvent * event)
629 {
630   guint n;
631
632   GST_DEBUG_OBJECT (qtdemux, "pushing %s event on all source pads",
633       GST_EVENT_TYPE_NAME (event));
634
635   for (n = 0; n < qtdemux->n_streams; n++) {
636     GstPad *pad;
637
638     if ((pad = qtdemux->streams[n]->pad))
639       gst_pad_push_event (pad, gst_event_ref (event));
640   }
641   gst_event_unref (event);
642 }
643
644 /* push a pending newsegment event, if any from the streaming thread */
645 static void
646 gst_qtdemux_push_pending_newsegment (GstQTDemux * qtdemux)
647 {
648   if (qtdemux->pending_newsegment) {
649     gst_qtdemux_push_event (qtdemux, qtdemux->pending_newsegment);
650     qtdemux->pending_newsegment = NULL;
651   }
652 }
653
654 typedef struct
655 {
656   guint64 media_time;
657 } FindData;
658
659 static gint
660 find_func (QtDemuxSample * s1, guint64 * media_time, gpointer user_data)
661 {
662   if (s1->timestamp > *media_time)
663     return 1;
664
665   return -1;
666 }
667
668 /* find the index of the sample that includes the data for @media_time
669  *
670  * Returns the index of the sample.
671  */
672 static guint32
673 gst_qtdemux_find_index (GstQTDemux * qtdemux, QtDemuxStream * str,
674     guint64 media_time)
675 {
676   QtDemuxSample *result;
677   guint32 index;
678
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);
682
683   if (G_LIKELY (result))
684     index = result - str->samples;
685   else
686     index = 0;
687
688   return index;
689 }
690
691 /* find the index of the keyframe needed to decode the sample at @index
692  * of stream @str.
693  *
694  * Returns the index of the keyframe.
695  */
696 static guint32
697 gst_qtdemux_find_keyframe (GstQTDemux * qtdemux, QtDemuxStream * str,
698     guint32 index)
699 {
700   guint32 new_index = index;
701
702   if (index >= str->n_samples) {
703     new_index = str->n_samples;
704     goto beach;
705   }
706
707   /* all keyframes, return index */
708   if (str->all_keyframe) {
709     new_index = index;
710     goto beach;
711   }
712
713   /* else go back until we have a keyframe */
714   while (TRUE) {
715     if (str->samples[new_index].keyframe)
716       break;
717
718     if (new_index == 0)
719       break;
720
721     new_index--;
722   }
723
724 beach:
725   GST_DEBUG_OBJECT (qtdemux, "searching for keyframe index before index %u "
726       "gave %u", index, new_index);
727
728   return new_index;
729 }
730
731 /* find the segment for @time_position for @stream
732  *
733  * Returns -1 if the segment cannot be found.
734  */
735 static guint32
736 gst_qtdemux_find_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
737     guint64 time_position)
738 {
739   gint i;
740   guint32 seg_idx;
741
742   GST_LOG_OBJECT (qtdemux, "finding segment for %" GST_TIME_FORMAT,
743       GST_TIME_ARGS (time_position));
744
745   /* find segment corresponding to time_position if we are looking
746    * for a segment. */
747   seg_idx = -1;
748   for (i = 0; i < stream->n_segments; i++) {
749     QtDemuxSegment *segment = &stream->segments[i];
750
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));
754
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);
759         seg_idx = i;
760         break;
761       }
762     } else {
763       if (segment->time <= time_position && time_position <= segment->stop_time) {
764         GST_LOG_OBJECT (qtdemux, "segment %d matches", i);
765         seg_idx = i;
766         break;
767       }
768     }
769   }
770   return seg_idx;
771 }
772
773 /* move the stream @str to the sample position @index.
774  *
775  * Updates @str->sample_index and marks discontinuity if needed.
776  */
777 static void
778 gst_qtdemux_move_stream (GstQTDemux * qtdemux, QtDemuxStream * str,
779     guint32 index)
780 {
781   /* no change needed */
782   if (index == str->sample_index)
783     return;
784
785   GST_DEBUG_OBJECT (qtdemux, "moving to sample %u of %u", index,
786       str->n_samples);
787
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 
791    * starting from */
792   str->from_sample = index;
793   str->discont = TRUE;
794 }
795
796 static void
797 gst_qtdemux_adjust_seek (GstQTDemux * qtdemux, gint64 desired_time,
798     gint64 * key_time, gint64 * key_offset)
799 {
800   guint64 min_offset;
801   gint64 min_byte_offset = -1;
802   gint n;
803
804   min_offset = desired_time;
805
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++) {
809     QtDemuxStream *str;
810     guint32 index, kindex;
811     guint32 seg_idx;
812     guint64 media_start;
813     guint64 media_time;
814     guint64 seg_time;
815     QtDemuxSegment *seg;
816
817     str = qtdemux->streams[n];
818
819     seg_idx = gst_qtdemux_find_segment (qtdemux, str, desired_time);
820     GST_DEBUG_OBJECT (qtdemux, "align segment %d", seg_idx);
821
822     /* segment not found, continue with normal flow */
823     if (seg_idx == -1)
824       continue;
825
826     /* get segment and time in the segment */
827     seg = &str->segments[seg_idx];
828     seg_time = desired_time - seg->time;
829
830     /* get the media time in the segment */
831     media_start = seg->media_start + seg_time;
832
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);
837
838     /* find previous keyframe */
839     kindex = gst_qtdemux_find_keyframe (qtdemux, str, index);
840
841     /* if the keyframe is at a different position, we need to update the
842      * requested seek time */
843     if (index != kindex) {
844       index = kindex;
845
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));
850
851       /* keyframes in the segment get a chance to change the
852        * desired_offset. keyframes out of the segment are
853        * ignored. */
854       if (media_time >= seg->media_start) {
855         guint64 seg_time;
856
857         /* this keyframe is inside the segment, convert back to
858          * segment time */
859         seg_time = (media_time - seg->media_start) + seg->time;
860         if (seg_time < min_offset)
861           min_offset = seg_time;
862       }
863     }
864
865     if (min_byte_offset < 0 || str->samples[index].offset < min_byte_offset)
866       min_byte_offset = str->samples[index].offset;
867   }
868
869   if (key_time)
870     *key_time = min_offset;
871   if (key_offset)
872     *key_offset = min_byte_offset;
873 }
874
875 static gboolean
876 gst_qtdemux_convert_seek (GstPad * pad, GstFormat * format,
877     GstSeekType cur_type, gint64 * cur, GstSeekType stop_type, gint64 * stop)
878 {
879   gboolean res;
880   GstFormat fmt;
881
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);
885
886   if (*format == GST_FORMAT_TIME)
887     return TRUE;
888
889   fmt = GST_FORMAT_TIME;
890   res = TRUE;
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);
895
896   if (res)
897     *format = GST_FORMAT_TIME;
898
899   return res;
900 }
901
902 /* perform seek in push based mode:
903    find BYTE position to move to based on time and delegate to upstream
904 */
905 static gboolean
906 gst_qtdemux_do_push_seek (GstQTDemux * qtdemux, GstPad * pad, GstEvent * event)
907 {
908   gdouble rate;
909   GstFormat format;
910   GstSeekFlags flags;
911   GstSeekType cur_type, stop_type;
912   gint64 cur, stop;
913   gboolean res;
914   gint64 byte_cur;
915
916   GST_DEBUG_OBJECT (qtdemux, "doing push-based seek");
917
918   gst_event_parse_seek (event, &rate, &format, &flags,
919       &cur_type, &cur, &stop_type, &stop);
920
921   if (stop_type != GST_SEEK_TYPE_NONE)
922     goto unsupported_seek;
923   stop = -1;
924
925   /* only forward streaming and seeking is possible */
926   if (rate <= 0)
927     goto unsupported_seek;
928
929   /* convert to TIME if needed and possible */
930   if (!gst_qtdemux_convert_seek (pad, &format, cur_type, &cur,
931           stop_type, &stop))
932     goto no_format;
933
934   /* find reasonable corresponding BYTE position,
935    * also try to mind about keyframes, since we can not go back a bit for them
936    * later on */
937   gst_qtdemux_adjust_seek (qtdemux, cur, NULL, &byte_cur);
938
939   if (byte_cur == -1)
940     goto abort_seek;
941
942   GST_DEBUG_OBJECT (qtdemux, "Pushing BYTE seek rate %g, "
943       "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, byte_cur,
944       stop);
945   /* BYTE seek event */
946   event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, byte_cur,
947       stop_type, stop);
948   res = gst_pad_push_event (qtdemux->sinkpad, event);
949
950   return res;
951
952   /* ERRORS */
953 abort_seek:
954   {
955     GST_DEBUG_OBJECT (qtdemux, "could not determine byte position to seek to, "
956         "seek aborted.");
957     return FALSE;
958   }
959 unsupported_seek:
960   {
961     GST_DEBUG_OBJECT (qtdemux, "unsupported seek, seek aborted.");
962     return FALSE;
963   }
964 no_format:
965   {
966     GST_DEBUG_OBJECT (qtdemux, "unsupported format given, seek aborted.");
967     return FALSE;
968   }
969 }
970
971 /* perform the seek.
972  *
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.
977  *
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.
984  *
985  * Called with STREAM_LOCK
986  */
987 static gboolean
988 gst_qtdemux_perform_seek (GstQTDemux * qtdemux, GstSegment * segment)
989 {
990   gint64 desired_offset;
991   gint n;
992
993   desired_offset = segment->last_stop;
994
995   GST_DEBUG_OBJECT (qtdemux, "seeking to %" GST_TIME_FORMAT,
996       GST_TIME_ARGS (desired_offset));
997
998   if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
999     gint64 min_offset;
1000
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;
1005   }
1006
1007   /* and set all streams to the final position */
1008   for (n = 0; n < qtdemux->n_streams; n++) {
1009     QtDemuxStream *stream = qtdemux->streams[n];
1010
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;
1016   }
1017   segment->last_stop = desired_offset;
1018   segment->time = desired_offset;
1019
1020   /* we stop at the end */
1021   if (segment->stop == -1)
1022     segment->stop = segment->duration;
1023
1024   return TRUE;
1025 }
1026
1027 /* do a seek in pull based mode */
1028 static gboolean
1029 gst_qtdemux_do_seek (GstQTDemux * qtdemux, GstPad * pad, GstEvent * event)
1030 {
1031   gdouble rate;
1032   GstFormat format;
1033   GstSeekFlags flags;
1034   GstSeekType cur_type, stop_type;
1035   gint64 cur, stop;
1036   gboolean flush;
1037   gboolean update;
1038   GstSegment seeksegment;
1039   int i;
1040
1041   if (event) {
1042     GST_DEBUG_OBJECT (qtdemux, "doing seek with event");
1043
1044     gst_event_parse_seek (event, &rate, &format, &flags,
1045         &cur_type, &cur, &stop_type, &stop);
1046
1047     /* we have to have a format as the segment format. Try to convert
1048      * if not. */
1049     if (!gst_qtdemux_convert_seek (pad, &format, cur_type, &cur,
1050             stop_type, &stop))
1051       goto no_format;
1052
1053     GST_DEBUG_OBJECT (qtdemux, "seek format %s", gst_format_get_name (format));
1054   } else {
1055     GST_DEBUG_OBJECT (qtdemux, "doing seek without event");
1056     flags = 0;
1057   }
1058
1059   flush = flags & GST_SEEK_FLAG_FLUSH;
1060
1061   /* stop streaming, either by flushing or by pausing the task */
1062   if (flush) {
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 ());
1067   } else {
1068     /* non flushing seek, pause the task */
1069     gst_pad_pause_task (qtdemux->sinkpad);
1070   }
1071
1072   /* wait for streaming to finish */
1073   GST_PAD_STREAM_LOCK (qtdemux->sinkpad);
1074
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));
1078
1079   if (event) {
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);
1084   }
1085
1086   /* now do the seek, this actually never returns FALSE */
1087   gst_qtdemux_perform_seek (qtdemux, &seeksegment);
1088
1089   /* prepare for streaming again */
1090   if (flush) {
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);
1099
1100     if (qtdemux->segment.rate >= 0) {
1101       /* FIXME, rate is the product of the global rate and the (quicktime)
1102        * segment rate. */
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 */
1108       guint64 stop;
1109
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);
1116     }
1117   }
1118
1119   /* commit the new segment */
1120   memcpy (&qtdemux->segment, &seeksegment, sizeof (GstSegment));
1121
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));
1126   }
1127
1128   /* restart streaming, NEWSEGMENT will be sent from the streaming
1129    * thread. */
1130   qtdemux->segment_running = TRUE;
1131   for (i = 0; i < qtdemux->n_streams; i++)
1132     qtdemux->streams[i]->last_ret = GST_FLOW_OK;
1133
1134   gst_pad_start_task (qtdemux->sinkpad, (GstTaskFunction) gst_qtdemux_loop,
1135       qtdemux->sinkpad);
1136
1137   GST_PAD_STREAM_UNLOCK (qtdemux->sinkpad);
1138
1139   return TRUE;
1140
1141   /* ERRORS */
1142 no_format:
1143   {
1144     GST_DEBUG_OBJECT (qtdemux, "unsupported format given, seek aborted.");
1145     return FALSE;
1146   }
1147 }
1148
1149 static gboolean
1150 gst_qtdemux_handle_src_event (GstPad * pad, GstEvent * event)
1151 {
1152   gboolean res = TRUE;
1153   GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
1154
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);
1161       } else {
1162         GST_DEBUG_OBJECT (qtdemux,
1163             "ignoring seek in push mode in current state");
1164         res = FALSE;
1165       }
1166       gst_event_unref (event);
1167       break;
1168     case GST_EVENT_QOS:
1169     case GST_EVENT_NAVIGATION:
1170       res = FALSE;
1171       gst_event_unref (event);
1172       break;
1173     default:
1174       res = gst_pad_event_default (pad, event);
1175       break;
1176   }
1177
1178   gst_object_unref (qtdemux);
1179
1180   return res;
1181 }
1182
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 */
1186 static void
1187 gst_qtdemux_find_sample (GstQTDemux * qtdemux, gint64 byte_pos, gboolean fw,
1188     gboolean set, QtDemuxStream ** _stream, gint * _index, gint64 * _time)
1189 {
1190   gint i, n, index;
1191   gint64 time, min_time;
1192   QtDemuxStream *stream;
1193
1194   min_time = -1;
1195   stream = NULL;
1196   index = -1;
1197
1198   for (n = 0; n < qtdemux->n_streams; ++n) {
1199     QtDemuxStream *str;
1200     gint inc;
1201     gboolean set_sample;
1202
1203
1204     str = qtdemux->streams[n];
1205     set_sample = !set;
1206
1207     if (fw) {
1208       i = 0;
1209       inc = 1;
1210     } else {
1211       i = str->n_samples - 1;
1212       inc = -1;
1213     }
1214     for (; (i >= 0) && (i < str->n_samples); i += inc) {
1215       if (str->samples[i].size &&
1216           ((fw && (str->samples[i].offset >= byte_pos)) ||
1217               (!fw &&
1218                   (str->samples[i].offset + str->samples[i].size <=
1219                       byte_pos)))) {
1220         /* move stream to first available sample */
1221         if (set) {
1222           gst_qtdemux_move_stream (qtdemux, str, i);
1223           set_sample = TRUE;
1224         }
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)) {
1229           min_time = time;
1230         }
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)) {
1235           stream = str;
1236           index = i;
1237         }
1238         break;
1239       }
1240     }
1241     /* no sample for this stream, mark eos */
1242     if (!set_sample)
1243       gst_qtdemux_move_stream (qtdemux, str, str->n_samples);
1244   }
1245
1246   if (_time)
1247     *_time = min_time;
1248   if (_stream)
1249     *_stream = stream;
1250   if (_index)
1251     *_index = index;
1252 }
1253
1254 static gboolean
1255 gst_qtdemux_handle_sink_event (GstPad * sinkpad, GstEvent * event)
1256 {
1257   GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
1258   gboolean res;
1259
1260   GST_LOG_OBJECT (demux, "handling %s event", GST_EVENT_TYPE_NAME (event));
1261
1262   switch (GST_EVENT_TYPE (event)) {
1263     case GST_EVENT_NEWSEGMENT:
1264     {
1265       GstFormat format;
1266       gdouble rate, arate;
1267       gint64 start, stop, time, offset = 0;
1268       QtDemuxStream *stream;
1269       gint idx;
1270       gboolean update;
1271       GstSegment segment;
1272
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,
1278           start, stop, time);
1279       GST_DEBUG_OBJECT (demux,
1280           "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
1281           &segment);
1282
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");
1286         goto exit;
1287       }
1288
1289       /* we only expect a BYTE segment, e.g. following a seek */
1290       if (format == GST_FORMAT_BYTES) {
1291         if (start > 0) {
1292           offset = start;
1293           gst_qtdemux_find_sample (demux, start, TRUE, FALSE, NULL, NULL,
1294               &start);
1295           start = MAX (start, 0);
1296         }
1297         if (stop > 0) {
1298           gst_qtdemux_find_sample (demux, stop, FALSE, FALSE, NULL, NULL,
1299               &stop);
1300           stop = MAX (stop, 0);
1301         }
1302       } else {
1303         GST_DEBUG_OBJECT (demux, "unsupported segment format, ignoring");
1304         goto exit;
1305       }
1306
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,
1313           start, stop);
1314       gst_qtdemux_push_event (demux,
1315           gst_event_new_new_segment_full (update, rate, arate, GST_FORMAT_TIME,
1316               start, stop, start));
1317
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;
1323       if (stream) {
1324         demux->todrop = stream->samples[idx].offset - offset;
1325         demux->neededbytes = demux->todrop + stream->samples[idx].size;
1326       } else {
1327         /* set up for EOS */
1328         demux->neededbytes = -1;
1329         demux->todrop = 0;
1330       }
1331     exit:
1332       gst_event_unref (event);
1333       res = TRUE;
1334       goto drop;
1335       break;
1336     }
1337     case GST_EVENT_FLUSH_STOP:
1338     {
1339       gint i;
1340
1341       /* clean up, force EOS if no more info follows */
1342       gst_adapter_clear (demux->adapter);
1343       demux->offset = 0;
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;
1349       }
1350       break;
1351     }
1352     case GST_EVENT_EOS:
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"));
1359       }
1360       break;
1361     default:
1362       break;
1363   }
1364
1365   res = gst_pad_event_default (demux->sinkpad, event);
1366
1367 drop:
1368   return res;
1369 }
1370
1371 static GstStateChangeReturn
1372 gst_qtdemux_change_state (GstElement * element, GstStateChange transition)
1373 {
1374   GstQTDemux *qtdemux = GST_QTDEMUX (element);
1375   GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1376
1377   switch (transition) {
1378     case GST_STATE_CHANGE_PAUSED_TO_READY:
1379       break;
1380     default:
1381       break;
1382   }
1383
1384   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1385
1386   switch (transition) {
1387     case GST_STATE_CHANGE_PAUSED_TO_READY:{
1388       gint n;
1389
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];
1409
1410         while (stream->buffers) {
1411           gst_buffer_unref (GST_BUFFER_CAST (stream->buffers->data));
1412           stream->buffers =
1413               g_slist_delete_link (stream->buffers, stream->buffers);
1414         }
1415         if (stream->pad)
1416           gst_element_remove_pad (element, stream->pad);
1417         if (stream->samples)
1418           g_free (stream->samples);
1419         if (stream->caps)
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);
1425         g_free (stream);
1426       }
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);
1433       break;
1434     }
1435     default:
1436       break;
1437   }
1438
1439   return result;
1440 }
1441
1442 static void
1443 qtdemux_parse_ftyp (GstQTDemux * qtdemux, const guint8 * buffer, gint length)
1444 {
1445   /* only consider at least a sufficiently complete ftyp atom */
1446   if (length >= 20) {
1447     GstBuffer *buf;
1448
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));
1454   }
1455 }
1456
1457 static void
1458 extract_initial_length_and_fourcc (const guint8 * data, guint64 * plength,
1459     guint32 * pfourcc)
1460 {
1461   guint64 length;
1462   guint32 fourcc;
1463
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));
1468
1469   if (length == 0) {
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);
1476   }
1477
1478   if (plength)
1479     *plength = length;
1480   if (pfourcc)
1481     *pfourcc = fourcc;
1482 }
1483
1484 static GstFlowReturn
1485 gst_qtdemux_loop_state_header (GstQTDemux * qtdemux)
1486 {
1487   guint64 length = 0;
1488   guint32 fourcc = 0;
1489   GstBuffer *buf = NULL;
1490   GstFlowReturn ret = GST_FLOW_OK;
1491   guint64 cur_offset = qtdemux->offset;
1492
1493   ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, 16, &buf);
1494   if (G_UNLIKELY (ret != GST_FLOW_OK))
1495     goto beach;
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);
1499
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;
1506     goto beach;
1507   }
1508
1509   switch (fourcc) {
1510     case FOURCC_mdat:
1511     case FOURCC_free:
1512     case FOURCC_wide:
1513     case FOURCC_PICT:
1514     case FOURCC_pnot:
1515     {
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;
1520       break;
1521     }
1522     case FOURCC_moov:
1523     {
1524       GstBuffer *moov;
1525
1526       ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, length, &moov);
1527       if (ret != GST_FLOW_OK)
1528         goto beach;
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.
1533          */
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,
1550                 final_length - 8);
1551             gst_buffer_unref (moov);
1552             moov = newmoov;
1553           }
1554         }
1555       }
1556
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;
1564         goto beach;
1565       }
1566       qtdemux->offset += length;
1567
1568       qtdemux_parse_moov (qtdemux, GST_BUFFER_DATA (moov), length);
1569       qtdemux_node_dump (qtdemux, qtdemux->moov_node);
1570
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)",
1577           qtdemux->state);
1578       break;
1579     }
1580     case FOURCC_ftyp:
1581     {
1582       GstBuffer *ftyp;
1583
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)
1587         goto beach;
1588       qtdemux->offset += length;
1589       qtdemux_parse_ftyp (qtdemux, GST_BUFFER_DATA (ftyp),
1590           GST_BUFFER_SIZE (ftyp));
1591       gst_buffer_unref (ftyp);
1592       break;
1593     }
1594     default:
1595     {
1596       GstBuffer *unknown;
1597
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,
1601           cur_offset);
1602       ret = gst_qtdemux_pull_atom (qtdemux, cur_offset, length, &unknown);
1603       if (ret != GST_FLOW_OK)
1604         goto beach;
1605       GST_MEMDUMP ("Unknown tag", GST_BUFFER_DATA (unknown),
1606           GST_BUFFER_SIZE (unknown));
1607       gst_buffer_unref (unknown);
1608       qtdemux->offset += length;
1609       break;
1610     }
1611   }
1612
1613 beach:
1614   return ret;
1615 }
1616
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
1620  */
1621 static GstFlowReturn
1622 gst_qtdemux_seek_to_previous_keyframe (GstQTDemux * qtdemux)
1623 {
1624   guint8 n = 0;
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;
1629
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];
1635
1636     seg_idx = gst_qtdemux_find_segment (qtdemux, str,
1637         qtdemux->segment.last_stop);
1638
1639     /* segment not found, continue with normal flow */
1640     if (seg_idx == -1)
1641       continue;
1642
1643     /* No candidate yet, take that one */
1644     if (!ref_str) {
1645       ref_str = str;
1646       continue;
1647     }
1648
1649     /* So that stream has a segment, we prefer video streams */
1650     if (str->subtype == FOURCC_vide) {
1651       ref_str = str;
1652       break;
1653     }
1654   }
1655
1656   if (G_UNLIKELY (!ref_str)) {
1657     GST_DEBUG_OBJECT (qtdemux, "couldn't find any stream");
1658     goto eos;
1659   }
1660
1661   if (G_UNLIKELY (!ref_str->from_sample)) {
1662     GST_DEBUG_OBJECT (qtdemux, "reached the beginning of the file");
1663     goto eos;
1664   }
1665
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);
1672   } else {
1673     k_index = ref_str->from_sample - 10;
1674   }
1675
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 */
1684       goto eos;
1685     }
1686     ref_str->segment_index--;
1687     seg = &ref_str->segments[ref_str->segment_index];
1688   }
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;
1693
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));
1697
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));
1702
1703   if (G_UNLIKELY (last_stop < qtdemux->segment.start)) {
1704     GST_DEBUG_OBJECT (qtdemux, "reached the beginning of segment");
1705     goto eos;
1706   }
1707
1708   /* Align them all on this */
1709   for (n = 0; n < qtdemux->n_streams; n++) {
1710     guint32 index = 0;
1711     guint64 media_start = 0, seg_time = 0;
1712     QtDemuxStream *str = qtdemux->streams[n];
1713
1714     seg_idx = gst_qtdemux_find_segment (qtdemux, str, k_pos);
1715     GST_DEBUG_OBJECT (qtdemux, "align segment %d", seg_idx);
1716
1717     /* segment not found, continue with normal flow */
1718     if (seg_idx == -1)
1719       continue;
1720
1721     /* get segment and time in the segment */
1722     seg = &str->segments[seg_idx];
1723     seg_time = k_pos - seg->time;
1724
1725     /* get the media time in the segment */
1726     media_start = seg->media_start + seg_time;
1727
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);
1732
1733     /* find previous keyframe */
1734     k_index = gst_qtdemux_find_keyframe (qtdemux, str, index);
1735
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);
1746   }
1747
1748   return GST_FLOW_OK;
1749
1750 eos:
1751   return GST_FLOW_UNEXPECTED;
1752 }
1753
1754 /* activate the given segment number @seg_idx of @stream at time @offset.
1755  * @offset is an absolute global position over all the segments.
1756  *
1757  * This will push out a NEWSEGMENT event with the right values and
1758  * position the stream index to the first decodable sample before
1759  * @offset.
1760  */
1761 static gboolean
1762 gst_qtdemux_activate_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
1763     guint32 seg_idx, guint64 offset)
1764 {
1765   GstEvent *event;
1766   QtDemuxSegment *segment;
1767   guint32 index, kf_index;
1768   guint64 seg_time;
1769   guint64 start, stop, time;
1770   gdouble rate;
1771
1772   GST_LOG_OBJECT (qtdemux, "activate segment %d, offset %" G_GUINT64_FORMAT,
1773       seg_idx, offset);
1774
1775   /* update the current segment */
1776   stream->segment_index = seg_idx;
1777
1778   /* get the segment */
1779   segment = &stream->segments[seg_idx];
1780
1781   if (G_UNLIKELY (offset < segment->time)) {
1782     GST_WARNING_OBJECT (qtdemux, "offset < segment->time %" G_GUINT64_FORMAT,
1783         segment->time);
1784     return FALSE;
1785   }
1786
1787   /* get time in this segment */
1788   seg_time = offset - segment->time;
1789
1790   GST_LOG_OBJECT (qtdemux, "seg_time %" GST_TIME_FORMAT,
1791       GST_TIME_ARGS (seg_time));
1792
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));
1796     return FALSE;
1797   }
1798
1799   /* qtdemux->segment.stop is in outside-time-realm, whereas
1800    * segment->media_stop is in track-time-realm.
1801    * 
1802    * In order to compare the two, we need to bring segment.stop
1803    * into the track-time-realm */
1804
1805   if (qtdemux->segment.stop == -1)
1806     stop = segment->media_stop;
1807   else
1808     stop =
1809         MIN (segment->media_stop,
1810         qtdemux->segment.stop - segment->time + segment->media_start);
1811
1812   if (qtdemux->segment.rate >= 0) {
1813     start = MIN (segment->media_start + seg_time, stop);
1814     time = offset;
1815   } else {
1816     start = segment->media_start;
1817     stop = MIN (segment->media_start + seg_time, stop);
1818     time = segment->time;
1819   }
1820
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));
1824
1825   /* combine global rate with that of the segment */
1826   rate = segment->rate * qtdemux->segment.rate;
1827
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,
1831       start, stop, time);
1832
1833   /* now prepare and send the segment */
1834   if (stream->pad) {
1835     event = gst_event_new_new_segment (FALSE, rate, GST_FORMAT_TIME,
1836         start, stop, 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);
1842   }
1843
1844   /* and move to the keyframe before the indicated media time of the
1845    * segment */
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));
1852   } else {
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));
1858   }
1859
1860   /* we're at the right spot */
1861   if (index == stream->sample_index) {
1862     GST_DEBUG_OBJECT (qtdemux, "we are at the right index");
1863     return TRUE;
1864   }
1865
1866   /* find keyframe of the target index */
1867   kf_index = gst_qtdemux_find_keyframe (qtdemux, stream, index);
1868
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);
1879     } else {
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));
1883     }
1884   } else {
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);
1889   }
1890
1891   return TRUE;
1892 }
1893
1894 /* prepare to get the current sample of @stream, getting essential values.
1895  *
1896  * This function will also prepare and send the segment when needed.
1897  *
1898  * Return FALSE if the stream is EOS.
1899  */
1900 static gboolean
1901 gst_qtdemux_prepare_current_sample (GstQTDemux * qtdemux,
1902     QtDemuxStream * stream, guint64 * offset, guint * size, guint64 * timestamp,
1903     guint64 * duration, gboolean * keyframe)
1904 {
1905   QtDemuxSample *sample;
1906   guint64 time_position;
1907   guint32 seg_idx;
1908
1909   g_return_val_if_fail (stream != NULL, FALSE);
1910
1911   time_position = stream->time_position;
1912   if (G_UNLIKELY (time_position == -1))
1913     goto eos;
1914
1915   seg_idx = stream->segment_index;
1916   if (G_UNLIKELY (seg_idx == -1)) {
1917     /* find segment corresponding to time_position if we are looking
1918      * for a segment. */
1919     seg_idx = gst_qtdemux_find_segment (qtdemux, stream, time_position);
1920
1921     /* nothing found, we're really eos */
1922     if (seg_idx == -1)
1923       goto eos;
1924   }
1925
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);
1929
1930   GST_LOG_OBJECT (qtdemux, "segment active, index = %u of %u",
1931       stream->sample_index, stream->n_samples);
1932
1933   /* send out pending buffers */
1934   while (stream->buffers) {
1935     GstBuffer *buffer = (GstBuffer *) stream->buffers->data;
1936
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;
1941     }
1942     gst_buffer_set_caps (buffer, stream->caps);
1943
1944     gst_pad_push (stream->pad, buffer);
1945
1946     stream->buffers = g_slist_delete_link (stream->buffers, stream->buffers);
1947   }
1948
1949   if (G_UNLIKELY (stream->sample_index >= stream->n_samples))
1950     goto eos;
1951
1952   /* now get the info for the sample we're at */
1953   sample = &stream->samples[stream->sample_index];
1954
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;
1960
1961   /* add padding */
1962   if (stream->padding) {
1963     *offset += stream->padding;
1964     *size -= stream->padding;
1965   }
1966
1967   return TRUE;
1968
1969   /* special cases */
1970 eos:
1971   {
1972     stream->time_position = -1;
1973     return FALSE;
1974   }
1975 }
1976
1977 /* move to the next sample in @stream.
1978  *
1979  * Moves to the next segment when needed.
1980  */
1981 static void
1982 gst_qtdemux_advance_sample (GstQTDemux * qtdemux, QtDemuxStream * stream)
1983 {
1984   QtDemuxSample *sample;
1985   QtDemuxSegment *segment;
1986
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",
1990         stream->to_sample);
1991     stream->time_position = -1;
1992     return;
1993   }
1994
1995   /* move to next sample */
1996   stream->sample_index++;
1997
1998   /* get current segment */
1999   segment = &stream->segments[stream->segment_index];
2000
2001   /* reached the last sample, we need the next segment */
2002   if (G_UNLIKELY (stream->sample_index >= stream->n_samples))
2003     goto next_segment;
2004
2005   /* get next sample */
2006   sample = &stream->samples[stream->sample_index];
2007
2008   /* see if we are past the segment */
2009   if (G_UNLIKELY (sample->timestamp >= segment->media_stop))
2010     goto next_segment;
2011
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;
2017   } else {
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;
2022   }
2023   return;
2024
2025   /* move to the next segment */
2026 next_segment:
2027   {
2028     GST_DEBUG_OBJECT (qtdemux, "segment %d ended ", stream->segment_index);
2029
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;
2033     } else {
2034       /* else we're only at the end of the current segment */
2035       stream->time_position = segment->stop_time;
2036     }
2037     /* make sure we select a new segment */
2038     stream->segment_index = -1;
2039   }
2040 }
2041
2042 static void
2043 gst_qtdemux_sync_streams (GstQTDemux * demux)
2044 {
2045   gint i;
2046
2047   if (demux->n_streams <= 1)
2048     return;
2049
2050   for (i = 0; i < demux->n_streams; i++) {
2051     QtDemuxStream *stream;
2052     GstClockTime end_time;
2053
2054     stream = demux->streams[i];
2055
2056     if (!stream->pad)
2057       continue;
2058
2059     /* TODO advance time on subtitle streams here, if any some day */
2060
2061     /* some clips/trailers may have unbalanced streams at the end,
2062      * so send EOS on shorter stream to prevent stalling others */
2063
2064     /* do not mess with EOS if SEGMENT seeking */
2065     if (demux->segment.flags & GST_SEEK_FLAG_SEGMENT)
2066       continue;
2067
2068     if (demux->pullbased) {
2069       /* loop mode is sample time based */
2070       if (stream->time_position != -1)
2071         continue;
2072     } else {
2073       /* push mode is byte position based */
2074       if (stream->samples[stream->n_samples - 1].offset >= demux->offset)
2075         continue;
2076     }
2077
2078     if (stream->sent_eos)
2079       continue;
2080
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 ());
2091     }
2092   }
2093 }
2094
2095 /* UNEXPECTED and NOT_LINKED need to be combined. This means that we return:
2096  *  
2097  *  GST_FLOW_NOT_LINKED: when all pads NOT_LINKED.
2098  *  GST_FLOW_UNEXPECTED: when all pads UNEXPECTED or NOT_LINKED.
2099  */
2100 static GstFlowReturn
2101 gst_qtdemux_combine_flows (GstQTDemux * demux, QtDemuxStream * stream,
2102     GstFlowReturn ret)
2103 {
2104   gint i;
2105   gboolean unexpected = FALSE, not_linked = TRUE;
2106
2107   GST_LOG_OBJECT (demux, "flow return: %s", gst_flow_get_name (ret));
2108
2109   /* store the value */
2110   stream->last_ret = ret;
2111
2112   for (i = 0; i < demux->n_streams; i++) {
2113     QtDemuxStream *ostream = demux->streams[i];
2114
2115     ret = ostream->last_ret;
2116
2117     /* no unexpected or unlinked, return */
2118     if (G_LIKELY (ret != GST_FLOW_UNEXPECTED && ret != GST_FLOW_NOT_LINKED))
2119       goto done;
2120
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);
2124   }
2125
2126   /* when we get here, we all have unlinked or unexpected */
2127   if (not_linked)
2128     ret = GST_FLOW_NOT_LINKED;
2129   else if (unexpected)
2130     ret = GST_FLOW_UNEXPECTED;
2131 done:
2132   GST_LOG_OBJECT (demux, "combined flow return: %s", gst_flow_get_name (ret));
2133   return ret;
2134 }
2135
2136 /* the input buffer metadata must be writable. Returns NULL when the buffer is
2137  * completely cliped */
2138 static GstBuffer *
2139 gst_qtdemux_clip_buffer (GstQTDemux * qtdemux, QtDemuxStream * stream,
2140     GstBuffer * buf)
2141 {
2142   gint64 start, stop, cstart, cstop, diff;
2143   GstClockTime timestamp = GST_CLOCK_TIME_NONE, duration = GST_CLOCK_TIME_NONE;
2144   guint8 *data;
2145   guint size;
2146   gint num_rate, denom_rate;
2147   gint frame_size;
2148   gboolean clip_data;
2149
2150   data = GST_BUFFER_DATA (buf);
2151   size = GST_BUFFER_SIZE (buf);
2152
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;
2158     clip_data = TRUE;
2159   } else if (stream->subtype == FOURCC_vide) {
2160     frame_size = size;
2161     num_rate = stream->fps_n;
2162     denom_rate = stream->fps_d;
2163     clip_data = FALSE;
2164   } else
2165     goto wrong_type;
2166
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)))
2170     goto no_timestamp;
2171
2172   if (G_LIKELY (GST_BUFFER_DURATION_IS_VALID (buf))) {
2173     duration = GST_BUFFER_DURATION (buf);
2174   } else {
2175     duration =
2176         gst_util_uint64_scale_int (size / frame_size, num_rate, denom_rate);
2177   }
2178
2179   start = timestamp;
2180   stop = start + duration;
2181
2182   if (G_UNLIKELY (!gst_segment_clip (&stream->segment, GST_FORMAT_TIME,
2183               start, stop, &cstart, &cstop)))
2184     goto clipped;
2185
2186   /* see if some clipping happened */
2187   diff = cstart - start;
2188   if (diff > 0) {
2189     timestamp = cstart;
2190     duration -= diff;
2191
2192     if (clip_data) {
2193       /* bring clipped time to samples and to bytes */
2194       diff = gst_util_uint64_scale_int (diff, denom_rate, num_rate);
2195       diff *= frame_size;
2196
2197       GST_DEBUG_OBJECT (qtdemux, "clipping start to %" GST_TIME_FORMAT " %"
2198           G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstart), diff);
2199
2200       data += diff;
2201       size -= diff;
2202     }
2203   }
2204   diff = stop - cstop;
2205   if (diff > 0) {
2206     duration -= diff;
2207
2208     if (clip_data) {
2209       /* bring clipped time to samples and then to bytes */
2210       diff = gst_util_uint64_scale_int (diff, denom_rate, num_rate);
2211       diff *= frame_size;
2212
2213       GST_DEBUG_OBJECT (qtdemux, "clipping stop to %" GST_TIME_FORMAT " %"
2214           G_GUINT64_FORMAT " bytes", GST_TIME_ARGS (cstop), diff);
2215
2216       size -= diff;
2217     }
2218   }
2219
2220   GST_BUFFER_TIMESTAMP (buf) = timestamp;
2221   GST_BUFFER_DURATION (buf) = duration;
2222   GST_BUFFER_SIZE (buf) = size;
2223   GST_BUFFER_DATA (buf) = data;
2224
2225   return buf;
2226
2227   /* dropped buffer */
2228 wrong_type:
2229   {
2230     GST_DEBUG_OBJECT (qtdemux, "unknown stream type");
2231     return buf;
2232   }
2233 no_timestamp:
2234   {
2235     GST_DEBUG_OBJECT (qtdemux, "no timestamp on buffer");
2236     return buf;
2237   }
2238 clipped:
2239   {
2240     GST_DEBUG_OBJECT (qtdemux, "clipped buffer");
2241     gst_buffer_unref (buf);
2242     return NULL;
2243   }
2244 }
2245
2246 static GstFlowReturn
2247 gst_qtdemux_loop_state_movie (GstQTDemux * qtdemux)
2248 {
2249   GstFlowReturn ret = GST_FLOW_OK;
2250   GstBuffer *buf = NULL;
2251   QtDemuxStream *stream;
2252   guint64 min_time;
2253   guint64 offset = 0;
2254   guint64 timestamp = GST_CLOCK_TIME_NONE;
2255   guint64 duration = 0;
2256   gboolean keyframe = FALSE;
2257   guint size = 0;
2258   gint index;
2259   gint i;
2260
2261   gst_qtdemux_push_pending_newsegment (qtdemux);
2262
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;
2266   index = -1;
2267   for (i = 0; i < qtdemux->n_streams; i++) {
2268     guint64 position;
2269
2270     stream = qtdemux->streams[i];
2271     position = stream->time_position;
2272
2273     /* position of -1 is EOS */
2274     if (position != -1 && position < min_time) {
2275       min_time = position;
2276       index = i;
2277     }
2278   }
2279   /* all are EOS */
2280   if (G_UNLIKELY (index == -1)) {
2281     GST_DEBUG_OBJECT (qtdemux, "all streams are EOS");
2282     goto eos;
2283   }
2284
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.");
2289     goto eos;
2290   }
2291
2292   stream = qtdemux->streams[index];
2293
2294   /* fetch info for the current sample of this stream */
2295   if (G_UNLIKELY (!gst_qtdemux_prepare_current_sample (qtdemux, stream, &offset,
2296               &size, &timestamp, &duration, &keyframe)))
2297     goto eos_stream;
2298
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));
2303
2304   /* hmm, empty sample, skip and move to next sample */
2305   if (G_UNLIKELY (size <= 0))
2306     goto next;
2307
2308   /* last pushed sample was out of boundary, goto next sample */
2309   if (G_UNLIKELY (stream->last_ret == GST_FLOW_UNEXPECTED))
2310     goto next;
2311
2312   GST_LOG_OBJECT (qtdemux, "reading %d bytes @ %" G_GUINT64_FORMAT, size,
2313       offset);
2314
2315   ret = gst_qtdemux_pull_atom (qtdemux, offset, size, &buf);
2316   if (G_UNLIKELY (ret != GST_FLOW_OK))
2317     goto beach;
2318
2319   if (G_UNLIKELY (stream->fourcc == FOURCC_rtsp)) {
2320     GstMessage *m;
2321     gchar *url;
2322
2323     url = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
2324
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));
2329     g_free (url);
2330
2331     gst_element_post_message (GST_ELEMENT_CAST (qtdemux), m);
2332   }
2333
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);
2338   }
2339   if (G_LIKELY (stream->pad)) {
2340     /* we're going to modify the metadata */
2341     buf = gst_buffer_make_metadata_writable (buf);
2342
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;
2347
2348     if (stream->need_clip)
2349       buf = gst_qtdemux_clip_buffer (qtdemux, stream, buf);
2350
2351     if (buf == NULL)
2352       goto next;
2353
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;
2358     }
2359
2360     if (!keyframe)
2361       GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
2362
2363     gst_buffer_set_caps (buf, stream->caps);
2364
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));
2370
2371     ret = gst_pad_push (stream->pad, buf);
2372   } else {
2373     GST_DEBUG_OBJECT (qtdemux, "No output pad for stream, ignoring");
2374     gst_buffer_unref (buf);
2375     ret = GST_FLOW_OK;
2376   }
2377
2378   /* combine flows */
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)
2383     ret = GST_FLOW_OK;
2384
2385 next:
2386   gst_qtdemux_advance_sample (qtdemux, stream);
2387
2388 beach:
2389   return ret;
2390
2391   /* special cases */
2392 eos:
2393   {
2394     GST_DEBUG_OBJECT (qtdemux, "No samples left for any streams - EOS");
2395     ret = GST_FLOW_UNEXPECTED;
2396     goto beach;
2397   }
2398 eos_stream:
2399   {
2400     GST_DEBUG_OBJECT (qtdemux, "No samples left for stream");
2401     /* EOS will be raised if all are EOS */
2402     ret = GST_FLOW_OK;
2403     goto beach;
2404   }
2405 }
2406
2407 static void
2408 gst_qtdemux_loop (GstPad * pad)
2409 {
2410   GstQTDemux *qtdemux;
2411   guint64 cur_offset;
2412   GstFlowReturn ret;
2413
2414   qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
2415
2416   cur_offset = qtdemux->offset;
2417   GST_LOG_OBJECT (qtdemux, "loop at position %" G_GUINT64_FORMAT ", state %d",
2418       cur_offset, qtdemux->state);
2419
2420   switch (qtdemux->state) {
2421     case QTDEMUX_STATE_INITIAL:
2422     case QTDEMUX_STATE_HEADER:
2423       ret = gst_qtdemux_loop_state_header (qtdemux);
2424       break;
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);
2429       }
2430       break;
2431     default:
2432       /* ouch */
2433       goto invalid_state;
2434   }
2435
2436   /* if something went wrong, pause */
2437   if (ret != GST_FLOW_OK)
2438     goto pause;
2439
2440 done:
2441   gst_object_unref (qtdemux);
2442   return;
2443
2444   /* ERRORS */
2445 invalid_state:
2446   {
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 ());
2452     goto done;
2453   }
2454 pause:
2455   {
2456     const gchar *reason = gst_flow_get_name (ret);
2457
2458     GST_LOG_OBJECT (qtdemux, "pausing task, reason %s", reason);
2459
2460     qtdemux->segment_running = FALSE;
2461     gst_pad_pause_task (pad);
2462
2463     /* fatal errors need special actions */
2464     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
2465       /* check EOS */
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"));
2472         }
2473         if (qtdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2474           gint64 stop;
2475
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;
2480
2481           if ((stop = qtdemux->segment.stop) == -1)
2482             stop = qtdemux->segment.duration;
2483
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));
2489           } else {
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));
2496           }
2497         } else {
2498           GST_LOG_OBJECT (qtdemux, "Sending EOS at end of segment");
2499           gst_qtdemux_push_event (qtdemux, gst_event_new_eos ());
2500         }
2501       } else {
2502         GST_ELEMENT_ERROR (qtdemux, STREAM, FAILED,
2503             (NULL), ("streaming stopped, reason %s", reason));
2504         gst_qtdemux_push_event (qtdemux, gst_event_new_eos ());
2505       }
2506     }
2507     goto done;
2508   }
2509 }
2510
2511 /*
2512  * next_entry_size
2513  *
2514  * Returns the size of the first entry at the current offset.
2515  * If -1, there are none (which means EOS or empty file).
2516  */
2517 static guint64
2518 next_entry_size (GstQTDemux * demux)
2519 {
2520   QtDemuxStream *stream;
2521   int i;
2522   int smallidx = -1;
2523   guint64 smalloffs = (guint64) - 1;
2524   QtDemuxSample *sample;
2525
2526   GST_LOG_OBJECT (demux, "Finding entry at offset %lld", demux->offset);
2527
2528   for (i = 0; i < demux->n_streams; i++) {
2529     stream = demux->streams[i];
2530
2531     if (stream->sample_index == -1)
2532       stream->sample_index = 0;
2533
2534     if (stream->sample_index >= stream->n_samples) {
2535       GST_LOG_OBJECT (demux, "stream %d samples exhausted", i);
2536       continue;
2537     }
2538
2539     sample = &stream->samples[stream->sample_index];
2540
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);
2544
2545     if (((smalloffs == -1)
2546             || (sample->offset < smalloffs)) && (sample->size)) {
2547       smallidx = i;
2548       smalloffs = sample->offset;
2549     }
2550   }
2551
2552   GST_LOG_OBJECT (demux, "stream %d offset %lld demux->offset :%lld",
2553       smallidx, smalloffs, demux->offset);
2554
2555   if (smallidx == -1)
2556     return -1;
2557
2558   stream = demux->streams[smallidx];
2559   sample = &stream->samples[stream->sample_index];
2560
2561   if (sample->offset >= demux->offset) {
2562     demux->todrop = sample->offset - demux->offset;
2563     return sample->size + demux->todrop;
2564   }
2565
2566   GST_DEBUG_OBJECT (demux, "There wasn't any entry at offset %lld",
2567       demux->offset);
2568   return -1;
2569 }
2570
2571 static void
2572 gst_qtdemux_post_progress (GstQTDemux * demux, gint num, gint denom)
2573 {
2574   gint perc = (gint) ((gdouble) num * 100.0 / (gdouble) denom);
2575
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)));
2579 }
2580
2581 /* FIXME, unverified after edit list updates */
2582 static GstFlowReturn
2583 gst_qtdemux_chain (GstPad * sinkpad, GstBuffer * inbuf)
2584 {
2585   GstQTDemux *demux;
2586   GstFlowReturn ret = GST_FLOW_OK;
2587
2588   demux = GST_QTDEMUX (gst_pad_get_parent (sinkpad));
2589
2590   gst_adapter_push (demux->adapter, inbuf);
2591
2592   /* we never really mean to buffer that much */
2593   if (demux->neededbytes == -1)
2594     goto eos;
2595
2596   GST_DEBUG_OBJECT (demux, "pushing in inbuf %p, neededbytes:%u, available:%u",
2597       inbuf, demux->neededbytes, gst_adapter_available (demux->adapter));
2598
2599   while (((gst_adapter_available (demux->adapter)) >= demux->neededbytes) &&
2600       (ret == GST_FLOW_OK)) {
2601
2602     GST_DEBUG_OBJECT (demux,
2603         "state:%d , demux->neededbytes:%d, demux->offset:%lld", demux->state,
2604         demux->neededbytes, demux->offset);
2605
2606     switch (demux->state) {
2607       case QTDEMUX_STATE_INITIAL:{
2608         const guint8 *data;
2609         guint32 fourcc;
2610         guint64 size;
2611
2612         data = gst_adapter_peek (demux->adapter, demux->neededbytes);
2613
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);
2618         if (size == 0) {
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;
2624           break;
2625         }
2626         if (fourcc == FOURCC_mdat) {
2627           if (demux->n_streams > 0) {
2628             demux->state = QTDEMUX_STATE_MOVIE;
2629             demux->neededbytes = next_entry_size (demux);
2630           } else {
2631             guint bs;
2632
2633           buffer_data:
2634             /* there may be multiple mdat (or alike) buffers */
2635             /* sanity check */
2636             if (demux->mdatbuffer)
2637               bs = GST_BUFFER_SIZE (demux->mdatbuffer);
2638             else
2639               bs = 0;
2640             if (size + bs > 10 * (1 << 20))
2641               goto no_moov;
2642             demux->state = QTDEMUX_STATE_BUFFER_MDAT;
2643             demux->neededbytes = size;
2644             if (!demux->mdatbuffer)
2645               demux->mdatoffset = demux->offset;
2646           }
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;
2653           break;
2654         } else {
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))
2658             goto buffer_data;
2659           demux->neededbytes = size;
2660           demux->state = QTDEMUX_STATE_HEADER;
2661         }
2662         break;
2663       }
2664       case QTDEMUX_STATE_HEADER:{
2665         const guint8 *data;
2666         guint32 fourcc;
2667
2668         GST_DEBUG_OBJECT (demux, "In header");
2669
2670         data = gst_adapter_peek (demux->adapter, demux->neededbytes);
2671
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]");
2676
2677           qtdemux_parse_moov (demux, data, demux->neededbytes);
2678           qtdemux_node_dump (demux, demux->moov_node);
2679           qtdemux_parse_tree (demux);
2680
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);
2687         } else {
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 */
2692         }
2693
2694         if (demux->mdatbuffer && demux->n_streams) {
2695           GstBuffer *buf;
2696
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;
2712         } else {
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;
2718         }
2719
2720         break;
2721       }
2722       case QTDEMUX_STATE_BUFFER_MDAT:{
2723         GstBuffer *buf;
2724
2725         GST_DEBUG_OBJECT (demux, "Got our buffer at offset %lld",
2726             demux->offset);
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);
2732         else
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);
2738
2739         break;
2740       }
2741       case QTDEMUX_STATE_MOVIE:{
2742         GstBuffer *outbuf;
2743         QtDemuxStream *stream = NULL;
2744         int i = -1;
2745
2746         GST_DEBUG_OBJECT (demux, "BEGIN // in MOVIE for offset %lld",
2747             demux->offset);
2748
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;
2754         }
2755
2756         /* first buffer? */
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]);
2766           }
2767         }
2768
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)
2773             continue;
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);
2779
2780           if (stream->samples[stream->sample_index].offset == demux->offset)
2781             break;
2782         }
2783
2784         if (G_UNLIKELY (stream == NULL || i == demux->n_streams))
2785           goto unknown_stream;
2786
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));
2791
2792         g_return_val_if_fail (outbuf != NULL, GST_FLOW_ERROR);
2793
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;
2798         } else {
2799           GST_BUFFER_TIMESTAMP (outbuf) =
2800               stream->samples[stream->sample_index].timestamp;
2801           demux->last_ts = GST_BUFFER_TIMESTAMP (outbuf);
2802         }
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);
2808
2809         /* position reporting */
2810         gst_segment_set_last_stop (&demux->segment, GST_FORMAT_TIME,
2811             demux->last_ts);
2812         gst_qtdemux_sync_streams (demux);
2813
2814         /* send buffer */
2815         if (stream->pad) {
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);
2821         } else {
2822           gst_buffer_unref (outbuf);
2823           ret = GST_FLOW_OK;
2824         }
2825
2826         /* combine flows */
2827         ret = gst_qtdemux_combine_flows (demux, stream, ret);
2828
2829         stream->sample_index++;
2830
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);
2836
2837         if ((demux->neededbytes = next_entry_size (demux)) == -1)
2838           goto eos;
2839         break;
2840       }
2841       default:
2842         goto invalid_state;
2843     }
2844   }
2845
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);
2851   }
2852 done:
2853   gst_object_unref (demux);
2854
2855   return ret;
2856
2857   /* ERRORS */
2858 unknown_stream:
2859   {
2860     GST_ELEMENT_ERROR (demux, STREAM, FAILED, (NULL), ("unknown stream found"));
2861     ret = GST_FLOW_ERROR;
2862     goto done;
2863   }
2864 eos:
2865   {
2866     GST_DEBUG_OBJECT (demux, "no next entry, EOS");
2867     ret = GST_FLOW_UNEXPECTED;
2868     goto done;
2869   }
2870 invalid_state:
2871   {
2872     GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2873         (NULL), ("qtdemuxer invalid state %d", demux->state));
2874     ret = GST_FLOW_ERROR;
2875     goto done;
2876   }
2877 no_moov:
2878   {
2879     GST_ELEMENT_ERROR (demux, STREAM, FAILED,
2880         (NULL), ("no 'moov' atom withing first 10 MB"));
2881     ret = GST_FLOW_ERROR;
2882     goto done;
2883   }
2884 }
2885
2886 static gboolean
2887 qtdemux_sink_activate (GstPad * sinkpad)
2888 {
2889   if (gst_pad_check_pull_range (sinkpad))
2890     return gst_pad_activate_pull (sinkpad, TRUE);
2891   else
2892     return gst_pad_activate_push (sinkpad, TRUE);
2893 }
2894
2895 static gboolean
2896 qtdemux_sink_activate_pull (GstPad * sinkpad, gboolean active)
2897 {
2898   GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
2899
2900   if (active) {
2901     demux->pullbased = TRUE;
2902     demux->segment_running = TRUE;
2903     return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_qtdemux_loop,
2904         sinkpad);
2905   } else {
2906     demux->segment_running = FALSE;
2907     return gst_pad_stop_task (sinkpad);
2908   }
2909 }
2910
2911 static gboolean
2912 qtdemux_sink_activate_push (GstPad * sinkpad, gboolean active)
2913 {
2914   GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
2915
2916   demux->pullbased = FALSE;
2917
2918   return TRUE;
2919 }
2920
2921 #ifdef HAVE_ZLIB
2922 static void *
2923 qtdemux_zalloc (void *opaque, unsigned int items, unsigned int size)
2924 {
2925   return g_malloc (items * size);
2926 }
2927
2928 static void
2929 qtdemux_zfree (void *opaque, void *addr)
2930 {
2931   g_free (addr);
2932 }
2933
2934 static void *
2935 qtdemux_inflate (void *z_buffer, guint z_length, guint length)
2936 {
2937   guint8 *buffer;
2938   z_stream *z;
2939   int ret;
2940
2941   z = g_new0 (z_stream, 1);
2942   z->zalloc = qtdemux_zalloc;
2943   z->zfree = qtdemux_zfree;
2944   z->opaque = NULL;
2945
2946   z->next_in = z_buffer;
2947   z->avail_in = z_length;
2948
2949   buffer = (guint8 *) g_malloc (length);
2950   ret = inflateInit (z);
2951   while (z->avail_in > 0) {
2952     if (z->avail_out == 0) {
2953       length += 1024;
2954       buffer = (guint8 *) g_realloc (buffer, length);
2955       z->next_out = buffer + z->total_out;
2956       z->avail_out = 1024;
2957     }
2958     ret = inflate (z, Z_SYNC_FLUSH);
2959     if (ret != Z_OK)
2960       break;
2961   }
2962   if (ret != Z_STREAM_END) {
2963     g_warning ("inflate() returned %d", ret);
2964   }
2965
2966   g_free (z);
2967   return buffer;
2968 }
2969 #endif /* HAVE_ZLIB */
2970
2971 static gboolean
2972 qtdemux_parse_moov (GstQTDemux * qtdemux, const guint8 * buffer, guint length)
2973 {
2974   GNode *cmov;
2975
2976   qtdemux->moov_node = g_node_new ((guint8 *) buffer);
2977
2978   GST_DEBUG_OBJECT (qtdemux, "parsing 'moov' atom");
2979   qtdemux_parse_node (qtdemux, qtdemux->moov_node, buffer, length);
2980
2981   cmov = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_cmov);
2982   if (cmov) {
2983     guint32 method;
2984     GNode *dcom;
2985     GNode *cmvd;
2986
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;
2991
2992     method = QT_FOURCC ((guint8 *) dcom->data + 8);
2993     switch (method) {
2994 #ifdef HAVE_ZLIB
2995       case GST_MAKE_FOURCC ('z', 'l', 'i', 'b'):{
2996         guint uncompressed_length;
2997         guint compressed_length;
2998         guint8 *buf;
2999
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);
3003
3004         buf =
3005             (guint8 *) qtdemux_inflate ((guint8 *) cmvd->data + 12,
3006             compressed_length, uncompressed_length);
3007
3008         qtdemux->moov_node_compressed = qtdemux->moov_node;
3009         qtdemux->moov_node = g_node_new (buf);
3010
3011         qtdemux_parse_node (qtdemux, qtdemux->moov_node, buf,
3012             uncompressed_length);
3013         break;
3014       }
3015 #endif /* HAVE_ZLIB */
3016       default:
3017         GST_WARNING_OBJECT (qtdemux, "unknown or unhandled header compression "
3018             "type %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (method));
3019         break;
3020     }
3021   }
3022   return TRUE;
3023
3024   /* ERRORS */
3025 invalid_compression:
3026   {
3027     GST_ERROR_OBJECT (qtdemux, "invalid compressed header");
3028     return FALSE;
3029   }
3030 }
3031
3032 static gboolean
3033 qtdemux_parse_container (GstQTDemux * qtdemux, GNode * node, const guint8 * buf,
3034     const guint8 * end)
3035 {
3036   while (G_UNLIKELY (buf < end)) {
3037     GNode *child;
3038     guint32 len;
3039
3040     if (G_UNLIKELY (buf + 4 > end)) {
3041       GST_LOG_OBJECT (qtdemux, "buffer overrun");
3042       break;
3043     }
3044     len = QT_UINT32 (buf);
3045     if (G_UNLIKELY (len == 0)) {
3046       GST_LOG_OBJECT (qtdemux, "empty container");
3047       break;
3048     }
3049     if (G_UNLIKELY (len < 8)) {
3050       GST_WARNING_OBJECT (qtdemux, "length too short (%d < 8)", len);
3051       break;
3052     }
3053     if (G_UNLIKELY (len > (end - buf))) {
3054       GST_WARNING_OBJECT (qtdemux, "length too long (%d > %d)", len, end - buf);
3055       break;
3056     }
3057
3058     child = g_node_new ((guint8 *) buf);
3059     g_node_append (node, child);
3060     qtdemux_parse_node (qtdemux, child, buf, len);
3061
3062     buf += len;
3063   }
3064   return TRUE;
3065 }
3066
3067 static gboolean
3068 qtdemux_parse_theora_extension (GstQTDemux * qtdemux, QtDemuxStream * stream,
3069     GNode * xdxt)
3070 {
3071   int len = QT_UINT32 (xdxt->data);
3072   guint8 *buf = xdxt->data;
3073   guint8 *end = buf + len;
3074   GstBuffer *buffer;
3075
3076   /* skip size and type */
3077   buf += 8;
3078   end -= 8;
3079
3080   while (buf < end) {
3081     gint size;
3082     guint32 type;
3083
3084     size = QT_UINT32 (buf);
3085     type = QT_FOURCC (buf + 4);
3086
3087     GST_LOG_OBJECT (qtdemux, "%p %p", buf, end);
3088
3089     if (buf + size > end || size <= 0)
3090       break;
3091
3092     buf += 8;
3093     size -= 8;
3094
3095     GST_WARNING_OBJECT (qtdemux, "have cookie %" GST_FOURCC_FORMAT,
3096         GST_FOURCC_ARGS (type));
3097
3098     switch (type) {
3099       case FOURCC_tCtH:
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");
3104         break;
3105       case FOURCC_tCt_:
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");
3110         break;
3111       case FOURCC_tCtC:
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");
3116         break;
3117       default:
3118         GST_WARNING_OBJECT (qtdemux,
3119             "unknown theora cookie %" GST_FOURCC_FORMAT,
3120             GST_FOURCC_ARGS (type));
3121         break;
3122     }
3123     buf += size;
3124   }
3125   return TRUE;
3126 }
3127
3128 static gboolean
3129 qtdemux_parse_node (GstQTDemux * qtdemux, GNode * node, const guint8 * buffer,
3130     guint length)
3131 {
3132   guint32 fourcc = 0;
3133   guint32 node_length = 0;
3134   const QtNodeType *type;
3135   const guint8 *end;
3136
3137   GST_LOG_OBJECT (qtdemux, "qtdemux_parse buffer %p length %u", buffer, length);
3138
3139   if (G_UNLIKELY (length < 8))
3140     goto not_enough_data;
3141
3142   node_length = QT_UINT32 (buffer);
3143   fourcc = QT_FOURCC (buffer + 4);
3144
3145   /* ignore empty nodes */
3146   if (G_UNLIKELY (fourcc == 0 || node_length == 8))
3147     return TRUE;
3148
3149   type = qtdemux_type_get (fourcc);
3150
3151   end = buffer + length;
3152
3153   GST_LOG_OBJECT (qtdemux,
3154       "parsing '%" GST_FOURCC_FORMAT "', length=%u, name '%s'",
3155       GST_FOURCC_ARGS (fourcc), node_length, type->name);
3156
3157   if (node_length > length)
3158     goto broken_atom_size;
3159
3160   if (type->flags & QT_FLAG_CONTAINER) {
3161     qtdemux_parse_container (qtdemux, node, buffer + 8, end);
3162   } else {
3163     switch (fourcc) {
3164       case FOURCC_stsd:
3165       {
3166         if (node_length < 20) {
3167           GST_LOG_OBJECT (qtdemux, "skipping small stsd box");
3168           break;
3169         }
3170         GST_DEBUG_OBJECT (qtdemux,
3171             "parsing stsd (sample table, sample description) atom");
3172         qtdemux_parse_container (qtdemux, node, buffer + 16, end);
3173         break;
3174       }
3175       case FOURCC_mp4a:
3176       {
3177         guint32 version;
3178         guint32 offset;
3179
3180         if (length < 20) {
3181           /* small boxes are also inside wave inside the mp4a box */
3182           GST_LOG_OBJECT (qtdemux, "skipping small mp4a box");
3183           break;
3184         }
3185         version = QT_UINT32 (buffer + 16);
3186
3187         GST_DEBUG_OBJECT (qtdemux, "mp4a version 0x%08x", version);
3188
3189         /* parse any esds descriptors */
3190         switch (version) {
3191           case 0x00000000:
3192             offset = 0x24;
3193             break;
3194           case 0x00010000:
3195             offset = 0x34;
3196             break;
3197           case 0x00020000:
3198             offset = 0x58;
3199             break;
3200           default:
3201             GST_WARNING_OBJECT (qtdemux, "unhandled mp4a version 0x%08x",
3202                 version);
3203             offset = 0;
3204             break;
3205         }
3206         if (offset)
3207           qtdemux_parse_container (qtdemux, node, buffer + offset, end);
3208         break;
3209       }
3210       case FOURCC_mp4v:
3211       {
3212         const guint8 *buf;
3213         guint32 version;
3214         int tlen;
3215
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;
3221
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);
3227           buf++;
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 */
3231           buf += 31;
3232           buf += 4;             /* and 4 bytes reserved */
3233
3234           GST_MEMDUMP_OBJECT (qtdemux, "mp4v", buf, end - buf);
3235
3236           qtdemux_parse_container (qtdemux, node, buf, end);
3237         }
3238         break;
3239       }
3240       case FOURCC_avc1:
3241       {
3242         GST_MEMDUMP_OBJECT (qtdemux, "avc1", buffer, end - buffer);
3243         qtdemux_parse_container (qtdemux, node, buffer + 0x56, end);
3244         break;
3245       }
3246       case FOURCC_mjp2:
3247       {
3248         qtdemux_parse_container (qtdemux, node, buffer + 86, end);
3249         break;
3250       }
3251       case FOURCC_meta:
3252       {
3253         GST_DEBUG_OBJECT (qtdemux, "parsing meta atom");
3254         qtdemux_parse_container (qtdemux, node, buffer + 12, end);
3255         break;
3256       }
3257       case FOURCC_XiTh:
3258       {
3259         guint32 version;
3260         guint32 offset;
3261
3262         version = QT_UINT32 (buffer + 12);
3263         GST_DEBUG_OBJECT (qtdemux, "parsing XiTh atom version 0x%08x", version);
3264
3265         switch (version) {
3266           case 0x00000001:
3267             offset = 0x62;
3268             break;
3269           default:
3270             GST_DEBUG_OBJECT (qtdemux, "unknown version 0x%08x", version);
3271             offset = 0;
3272             break;
3273         }
3274         if (offset)
3275           qtdemux_parse_container (qtdemux, node, buffer + offset, end);
3276         break;
3277       }
3278       case FOURCC_in24:
3279       {
3280         qtdemux_parse_container (qtdemux, node, buffer + 0x34, end);
3281         break;
3282       }
3283       default:
3284         if (!strcmp (type->name, "unknown"))
3285           GST_MEMDUMP ("Unknown tag", buffer + 4, end - buffer - 4);
3286         break;
3287     }
3288   }
3289   GST_LOG_OBJECT (qtdemux, "parsed '%" GST_FOURCC_FORMAT,
3290       GST_FOURCC_ARGS (fourcc));
3291   return TRUE;
3292
3293 /* ERRORS */
3294 not_enough_data:
3295   {
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));
3299     return FALSE;
3300   }
3301 broken_atom_size:
3302   {
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,
3307             length));
3308     return FALSE;
3309   }
3310 }
3311
3312 static GNode *
3313 qtdemux_tree_get_child_by_type (GNode * node, guint32 fourcc)
3314 {
3315   GNode *child;
3316   guint8 *buffer;
3317   guint32 child_fourcc;
3318
3319   for (child = g_node_first_child (node); child;
3320       child = g_node_next_sibling (child)) {
3321     buffer = (guint8 *) child->data;
3322
3323     child_fourcc = QT_FOURCC (buffer + 4);
3324
3325     if (G_UNLIKELY (child_fourcc == fourcc)) {
3326       return child;
3327     }
3328   }
3329   return NULL;
3330 }
3331
3332 static GNode *
3333 qtdemux_tree_get_child_by_type_full (GNode * node, guint32 fourcc,
3334     QtAtomParser * parser)
3335 {
3336   GNode *child;
3337   guint8 *buffer;
3338   guint32 child_fourcc, child_len;
3339
3340   for (child = g_node_first_child (node); child;
3341       child = g_node_next_sibling (child)) {
3342     buffer = (guint8 *) child->data;
3343
3344     child_len = QT_UINT32 (buffer);
3345     child_fourcc = QT_FOURCC (buffer + 4);
3346
3347     if (G_UNLIKELY (child_fourcc == fourcc)) {
3348       if (G_UNLIKELY (child_len < (4 + 4)))
3349         return NULL;
3350       /* FIXME: must verify if atom length < parent atom length */
3351       qt_atom_parser_init (parser, buffer + (4 + 4), child_len - (4 + 4));
3352       return child;
3353     }
3354   }
3355   return NULL;
3356 }
3357
3358 static GNode *
3359 qtdemux_tree_get_sibling_by_type (GNode * node, guint32 fourcc)
3360 {
3361   GNode *child;
3362   guint8 *buffer;
3363   guint32 child_fourcc;
3364
3365   for (child = g_node_next_sibling (node); child;
3366       child = g_node_next_sibling (child)) {
3367     buffer = (guint8 *) child->data;
3368
3369     child_fourcc = QT_FOURCC (buffer + 4);
3370
3371     if (child_fourcc == fourcc) {
3372       return child;
3373     }
3374   }
3375   return NULL;
3376 }
3377
3378 static gboolean
3379 gst_qtdemux_add_stream (GstQTDemux * qtdemux,
3380     QtDemuxStream * stream, GstTagList * list)
3381 {
3382   if (qtdemux->n_streams >= GST_QTDEMUX_MAX_STREAMS)
3383     goto too_many_streams;
3384
3385   if (stream->subtype == FOURCC_vide) {
3386     gchar *name = g_strdup_printf ("video_%02d", qtdemux->n_video_streams);
3387
3388     stream->pad =
3389         gst_pad_new_from_static_template (&gst_qtdemux_videosrc_template, name);
3390     g_free (name);
3391
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)) {
3395       /* still frame */
3396       stream->fps_n = 0;
3397       stream->fps_d = 1;
3398     } else {
3399       stream->fps_n = stream->timescale;
3400       if (stream->min_duration == 0)
3401         stream->fps_d = 1;
3402       else
3403         stream->fps_d = stream->min_duration;
3404     }
3405
3406     if (stream->caps) {
3407       gboolean gray;
3408       gint depth, palette_count;
3409       const guint32 *palette_data = NULL;
3410
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);
3415
3416       /* iso files:
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);
3422
3423         if (stream->display_width > 0 && stream->display_height > 0 &&
3424             stream->width > 0 && stream->height > 0) {
3425           gint n, d;
3426
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;
3430           if (n != d) {
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);
3434           }
3435         }
3436       }
3437
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);
3443       }
3444
3445       depth = stream->bits_per_sample;
3446
3447       /* more than 32 bits means grayscale */
3448       gray = (depth > 32);
3449       /* low 32 bits specify the depth  */
3450       depth &= 0x1F;
3451
3452       /* different number of palette entries is determined by depth. */
3453       palette_count = 0;
3454       if ((depth == 1) || (depth == 2) || (depth == 4) || (depth == 8))
3455         palette_count = (1 << depth);
3456
3457       switch (palette_count) {
3458         case 0:
3459           break;
3460         case 2:
3461           palette_data = ff_qt_default_palette_2;
3462           break;
3463         case 4:
3464           palette_data = ff_qt_default_palette_4;
3465           break;
3466         case 16:
3467           if (gray)
3468             palette_data = ff_qt_grayscale_palette_16;
3469           else
3470             palette_data = ff_qt_default_palette_16;
3471           break;
3472         case 256:
3473           if (gray)
3474             palette_data = ff_qt_grayscale_palette_256;
3475           else
3476             palette_data = ff_qt_default_palette_256;
3477           break;
3478         default:
3479           GST_ELEMENT_WARNING (qtdemux, STREAM, DECODE,
3480               (_("The video in this file might not play correctly.")),
3481               ("unsupported palette depth %d", depth));
3482           break;
3483       }
3484       if (palette_data) {
3485         GstBuffer *palette;
3486
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;
3493
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));
3500
3501         gst_object_unref (stream->pad);
3502         stream->pad = NULL;
3503       }
3504     }
3505     qtdemux->n_video_streams++;
3506   } else if (stream->subtype == FOURCC_soun) {
3507     gchar *name = g_strdup_printf ("audio_%02d", qtdemux->n_audio_streams);
3508
3509     stream->pad =
3510         gst_pad_new_from_static_template (&gst_qtdemux_audiosrc_template, name);
3511     g_free (name);
3512     if (stream->caps) {
3513       gst_caps_set_simple (stream->caps,
3514           "rate", G_TYPE_INT, (int) stream->rate,
3515           "channels", G_TYPE_INT, stream->n_channels, NULL);
3516     }
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);
3522
3523     stream->pad =
3524         gst_pad_new_from_static_template (&gst_qtdemux_subpsrc_template, name);
3525     g_free (name);
3526     qtdemux->n_subp_streams++;
3527   } else {
3528     GST_DEBUG_OBJECT (qtdemux, "unknown stream type");
3529     goto done;
3530   }
3531
3532   qtdemux->streams[qtdemux->n_streams] = stream;
3533   qtdemux->n_streams++;
3534   GST_DEBUG_OBJECT (qtdemux, "n_streams is now %d", qtdemux->n_streams);
3535
3536   if (stream->pad) {
3537     GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
3538
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);
3544
3545     GST_DEBUG_OBJECT (qtdemux, "setting caps %" GST_PTR_FORMAT, stream->caps);
3546     gst_pad_set_caps (stream->pad, stream->caps);
3547
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;
3562   }
3563 done:
3564   return TRUE;
3565
3566 too_many_streams:
3567   {
3568     GST_ELEMENT_WARNING (qtdemux, STREAM, DECODE,
3569         (_("This file contains too many streams. Only playing first %d"),
3570             GST_QTDEMUX_MAX_STREAMS), (NULL));
3571     return TRUE;
3572   }
3573 }
3574
3575 /* collect all samples for @stream by reading the info from @stbl
3576  */
3577 static gboolean
3578 qtdemux_parse_samples (GstQTDemux * qtdemux, QtDemuxStream * stream,
3579     GNode * stbl)
3580 {
3581   QtAtomParser co_reader;
3582   QtAtomParser stsz;
3583   QtAtomParser stsc;
3584   QtAtomParser stts;
3585   GNode *ctts;
3586   guint32 sample_size;
3587   guint32 n_samples;
3588   guint32 n_samples_per_chunk;
3589   int sample_index;
3590   QtDemuxSample *samples;
3591   gint i, j, k;
3592   int index;
3593   guint64 timestamp;
3594   guint co_size;
3595
3596   /* sample to chunk */
3597   if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stsc, &stsc))
3598     goto corrupt_file;
3599
3600   /* sample size */
3601   if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stsz, &stsz))
3602     goto corrupt_file;
3603
3604   /* chunk offsets */
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);
3609   else
3610     goto corrupt_file;
3611
3612   /* sample time */
3613   if (!qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stts, &stts))
3614     goto corrupt_file;
3615
3616   if (!qt_atom_parser_skip (&stsz, 1 + 3) ||
3617       !qt_atom_parser_get_uint32 (&stsz, &sample_size))
3618     goto corrupt_file;
3619
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))
3623       goto corrupt_file;
3624
3625     if (!qt_atom_parser_get_uint32 (&stsz, &n_samples))
3626       goto corrupt_file;
3627
3628     if (n_samples == 0)
3629       goto no_samples;
3630
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);
3634
3635     if (n_samples >= QTDEMUX_MAX_SAMPLE_INDEX_SIZE / sizeof (QtDemuxSample))
3636       goto index_too_big;
3637
3638     samples = g_try_new0 (QtDemuxSample, n_samples);
3639     if (samples == NULL)
3640       goto out_of_memory;
3641
3642     stream->n_samples = n_samples;
3643     stream->samples = samples;
3644
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))
3649         goto corrupt_file;
3650
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);
3654       }
3655     } else {
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;
3660     }
3661
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))
3665       goto corrupt_file;
3666
3667     if (!qt_atom_parser_has_chunks (&stsc, n_samples_per_chunk, 12))
3668       goto corrupt_file;
3669
3670     index = 0;
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;
3675
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);
3679
3680       /* chunk numbers are counted from 1 it seems */
3681       if (G_UNLIKELY (first_chunk == 0))
3682         goto corrupt_file;
3683       else
3684         --first_chunk;
3685
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
3688        * INT_MAX */
3689       if (G_UNLIKELY (i == n_samples_per_chunk - 1)) {
3690         last_chunk = G_MAXUINT32;
3691       } else {
3692         last_chunk = qt_atom_parser_peek_uint32_unchecked (&stsc);
3693         if (G_UNLIKELY (last_chunk == 0))
3694           goto corrupt_file;
3695         else
3696           --last_chunk;
3697       }
3698
3699       if (G_UNLIKELY (last_chunk < first_chunk))
3700         goto corrupt_file;
3701
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))
3705           goto corrupt_file;
3706       } else {
3707         co_chunk = co_reader;
3708         if (!qt_atom_parser_skip (&co_chunk, first_chunk * co_size))
3709           goto corrupt_file;
3710       }
3711
3712       for (j = first_chunk; j < last_chunk; j++) {
3713         guint64 chunk_offset;
3714
3715         if (!qt_atom_parser_get_offset (&co_chunk, co_size, &chunk_offset))
3716           goto corrupt_file;
3717
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;
3723           index++;
3724           if (G_UNLIKELY (index >= n_samples))
3725             goto done2;
3726         }
3727       }
3728     }
3729   done2:
3730     {
3731       guint32 n_sample_times;
3732       guint32 time;
3733
3734       if (!qt_atom_parser_skip (&stts, 4))
3735         goto corrupt_file;
3736       if (!qt_atom_parser_get_uint32 (&stts, &n_sample_times))
3737         goto corrupt_file;
3738       GST_LOG_OBJECT (qtdemux, "%u timestamp blocks", n_sample_times);
3739
3740       /* make sure there's enough data */
3741       if (!qt_atom_parser_has_chunks (&stts, n_sample_times, 2 * 4))
3742         goto corrupt_file;
3743
3744       timestamp = 0;
3745       stream->min_duration = 0;
3746       time = 0;
3747       index = 0;
3748       for (i = 0; i < n_sample_times; i++) {
3749         guint32 n;
3750         guint32 duration;
3751
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,
3755             duration);
3756
3757         /* take first duration for fps */
3758         if (G_UNLIKELY (stream->min_duration == 0))
3759           stream->min_duration = duration;
3760
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));
3765
3766           samples[index].timestamp = timestamp;
3767           /* add non-scaled values to avoid rounding errors */
3768           time += duration;
3769           timestamp =
3770               gst_util_uint64_scale (time, GST_SECOND, stream->timescale);
3771           samples[index].duration = timestamp - samples[index].timestamp;
3772
3773           index++;
3774           if (G_UNLIKELY (index >= n_samples))
3775             goto done3;
3776         }
3777       }
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;
3788       }
3789     }
3790   done3:
3791     {
3792       /* FIXME: split this block out into a separate function */
3793       QtAtomParser stss, stps;
3794
3795       /* sample sync, can be NULL */
3796       if (qtdemux_tree_get_child_by_type_full (stbl, FOURCC_stss, &stss)) {
3797         guint32 n_sample_syncs;
3798
3799         /* mark keyframes */
3800         if (!qt_atom_parser_skip (&stss, 4))
3801           goto corrupt_file;
3802         if (!qt_atom_parser_get_uint32 (&stss, &n_sample_syncs))
3803           goto corrupt_file;
3804
3805         if (n_sample_syncs == 0) {
3806           stream->all_keyframe = TRUE;
3807         } else {
3808           /* make sure there's enough data */
3809           if (!qt_atom_parser_has_chunks (&stss, n_sample_syncs, 4))
3810             goto corrupt_file;
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;
3816           }
3817         }
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;
3821
3822           if (!qt_atom_parser_skip (&stps, 4))
3823             goto corrupt_file;
3824           if (!qt_atom_parser_get_uint32 (&stps, &n_sample_syncs))
3825             goto corrupt_file;
3826
3827           if (n_sample_syncs != 0) {
3828             /* no entries, the stss table contains the real sync
3829              * samples */
3830           } else {
3831             /* make sure there's enough data */
3832             if (!qt_atom_parser_has_chunks (&stps, n_sample_syncs, 4))
3833               goto corrupt_file;
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;
3839             }
3840           }
3841         }
3842       } else {
3843         /* no stss, all samples are keyframes */
3844         stream->all_keyframe = TRUE;
3845       }
3846     }
3847   } else {
3848     GST_DEBUG_OBJECT (qtdemux,
3849         "stsz sample_size %d != 0, treating chunks as samples", sample_size);
3850
3851     /* skip version + flags */
3852     if (!gst_byte_reader_skip (&co_reader, 1 + 3))
3853       goto corrupt_file;
3854
3855     /* treat chunks as samples */
3856     if (!gst_byte_reader_get_uint32_be (&co_reader, &n_samples))
3857       goto corrupt_file;
3858
3859     if (n_samples == 0)
3860       goto no_samples;
3861
3862     GST_DEBUG_OBJECT (qtdemux, "allocating n_samples %u (%u MB)", n_samples,
3863         (guint) (n_samples * sizeof (QtDemuxSample)) >> 20);
3864
3865     if (n_samples >= QTDEMUX_MAX_SAMPLE_INDEX_SIZE / sizeof (QtDemuxSample))
3866       goto index_too_big;
3867
3868     samples = g_try_new0 (QtDemuxSample, n_samples);
3869     if (samples == NULL)
3870       goto out_of_memory;
3871
3872     stream->n_samples = n_samples;
3873     stream->samples = samples;
3874
3875     if (!qt_atom_parser_skip (&stsc, 1 + 3) ||
3876         !qt_atom_parser_get_uint32 (&stsc, &n_samples_per_chunk))
3877       goto corrupt_file;
3878
3879     GST_DEBUG_OBJECT (qtdemux, "n_samples_per_chunk %u", n_samples_per_chunk);
3880     sample_index = 0;
3881     timestamp = 0;
3882
3883     if (!qt_atom_parser_has_chunks (&stsc, n_samples_per_chunk, 12))
3884       goto corrupt_file;
3885
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;
3890
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);
3894
3895       /* chunk numbers are counted from 1 it seems */
3896       if (G_UNLIKELY (first_chunk == 0))
3897         goto corrupt_file;
3898       else
3899         --first_chunk;
3900
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
3903        * INT_MAX */
3904       if (G_UNLIKELY (i == (n_samples_per_chunk - 1))) {
3905         last_chunk = G_MAXUINT32;
3906       } else {
3907         last_chunk = qt_atom_parser_peek_uint32_unchecked (&stsc);
3908         if (G_UNLIKELY (last_chunk == 0))
3909           goto corrupt_file;
3910         else
3911           --last_chunk;
3912       }
3913
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);
3917
3918       if (G_UNLIKELY (last_chunk < first_chunk))
3919         goto corrupt_file;
3920
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))
3924           goto corrupt_file;
3925       } else {
3926         co_chunk = co_reader;
3927         if (!qt_atom_parser_skip (&co_chunk, first_chunk * co_size))
3928           goto corrupt_file;
3929       }
3930
3931       for (j = first_chunk; j < last_chunk; j++) {
3932         if (j >= n_samples)
3933           goto done;
3934
3935         samples[j].offset =
3936             qt_atom_parser_get_offset_unchecked (&co_chunk, co_size);
3937
3938         GST_LOG_OBJECT (qtdemux, "Created entry %d with offset "
3939             "%" G_GUINT64_FORMAT, j, samples[j].offset);
3940
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;
3944         } else {
3945           samples[j].size = samples_per_chunk;
3946         }
3947
3948         GST_DEBUG_OBJECT (qtdemux, "sample %d: timestamp %" GST_TIME_FORMAT
3949             ", size %u", j, GST_TIME_ARGS (timestamp), samples[j].size);
3950
3951         samples[j].timestamp = timestamp;
3952         sample_index += samples_per_chunk;
3953
3954         timestamp = gst_util_uint64_scale (sample_index,
3955             GST_SECOND, stream->timescale);
3956         samples[j].duration = timestamp - samples[j].timestamp;
3957
3958         samples[j].keyframe = TRUE;
3959       }
3960     }
3961   }
3962
3963   /* composition time to sample */
3964   if ((ctts = qtdemux_tree_get_child_by_type (stbl, FOURCC_ctts))) {
3965     const guint8 *ctts_data, *ctts_p;
3966     guint32 n_entries;
3967     guint32 count;
3968     gint32 soffset;
3969
3970     ctts_data = (const guint8 *) ctts->data;
3971     n_entries = QT_UINT32 (ctts_data + 12);
3972
3973     /* Fill in the pts_offsets */
3974     index = 0;
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);
3979       ctts_p += 4;
3980       soffset = QT_UINT32 (ctts_p);
3981       ctts_p += 4;
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;
3985         index++;
3986         if (G_UNLIKELY (index >= n_samples))
3987           goto done;
3988       }
3989     }
3990   }
3991 done:
3992   return TRUE;
3993
3994 /* ERRORS */
3995 corrupt_file:
3996   {
3997     GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
3998         (_("This file is corrupt and cannot be played.")), (NULL));
3999     return FALSE;
4000   }
4001 no_samples:
4002   {
4003     GST_WARNING_OBJECT (qtdemux, "stream has no samples");
4004     return FALSE;
4005   }
4006 out_of_memory:
4007   {
4008     GST_WARNING_OBJECT (qtdemux, "failed to allocate %d samples", n_samples);
4009     return FALSE;
4010   }
4011 index_too_big:
4012   {
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);
4016     return FALSE;
4017   }
4018 }
4019
4020 /* collect all segment info for @stream.
4021  */
4022 static gboolean
4023 qtdemux_parse_segments (GstQTDemux * qtdemux, QtDemuxStream * stream,
4024     GNode * trak)
4025 {
4026   GNode *edts;
4027
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))) {
4033     GNode *elst;
4034     gint n_segments;
4035     gint i, count;
4036     guint64 time, stime;
4037     guint8 *buffer;
4038
4039     GST_DEBUG_OBJECT (qtdemux, "looking for edit list");
4040     if (!(elst = qtdemux_tree_get_child_by_type (edts, FOURCC_elst)))
4041       goto done;
4042
4043     buffer = elst->data;
4044
4045     n_segments = QT_UINT32 (buffer + 12);
4046
4047     /* we might allocate a bit too much, at least allocate 1 segment */
4048     stream->segments = g_new (QtDemuxSegment, MAX (n_segments, 1));
4049
4050     /* segments always start from 0 */
4051     time = 0;
4052     stime = 0;
4053     count = 0;
4054     for (i = 0; i < n_segments; i++) {
4055       guint64 duration;
4056       guint64 media_time;
4057       QtDemuxSegment *segment;
4058       guint32 rate_int;
4059
4060       media_time = QT_UINT32 (buffer + 20 + i * 12);
4061
4062       /* -1 media time is an empty segment, just ignore it */
4063       if (media_time == G_MAXUINT32)
4064         continue;
4065
4066       duration = QT_UINT32 (buffer + 16 + i * 12);
4067
4068       segment = &stream->segments[count++];
4069
4070       /* time and duration expressed in global timescale */
4071       segment->time = stime;
4072       /* add non scaled values so we don't cause roundoff errors */
4073       time += duration;
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);
4082
4083       if (rate_int <= 1) {
4084         /* 0 is not allowed, some programs write 1 instead of the floating point
4085          * value */
4086         GST_WARNING_OBJECT (qtdemux, "found suspicious rate %" G_GUINT32_FORMAT,
4087             rate_int);
4088         segment->rate = 1;
4089       } else {
4090         segment->rate = rate_int / 65536.0;
4091       }
4092
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);
4098     }
4099     GST_DEBUG_OBJECT (qtdemux, "found %d non-empty segments", count);
4100     stream->n_segments = count;
4101   }
4102 done:
4103
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;
4112   }
4113
4114   /* no segments, create one to play the complete trak */
4115   if (stream->n_segments == 0) {
4116     GstClockTime stream_duration = 0;
4117
4118     if (stream->segments == NULL)
4119       stream->segments = g_new (QtDemuxSegment, 1);
4120
4121     /* samples know best */
4122     if (stream->n_samples > 0) {
4123       stream_duration =
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;
4127     }
4128
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;
4135
4136     GST_DEBUG_OBJECT (qtdemux, "created dummy segment %" GST_TIME_FORMAT,
4137         GST_TIME_ARGS (stream_duration));
4138     stream->n_segments = 1;
4139   }
4140   GST_DEBUG_OBJECT (qtdemux, "using %d segments", stream->n_segments);
4141
4142   return TRUE;
4143 }
4144
4145 /* parse the traks.
4146  * With each track we associate a new QtDemuxStream that contains all the info
4147  * about the trak.
4148  * traks that do not decode to something (like strm traks) will not have a pad.
4149  */
4150 static gboolean
4151 qtdemux_parse_trak (GstQTDemux * qtdemux, GNode * trak)
4152 {
4153   QtAtomParser tkhd;
4154   int offset;
4155   GNode *mdia;
4156   GNode *mdhd;
4157   GNode *hdlr;
4158   GNode *minf;
4159   GNode *stbl;
4160   GNode *stsd;
4161   GNode *mp4a;
4162   GNode *mp4v;
4163   GNode *wave;
4164   GNode *esds;
4165   GNode *pasp;
4166   QtDemuxStream *stream;
4167   GstTagList *list = NULL;
4168   gchar *codec = NULL;
4169   const guint8 *stsd_data;
4170   guint32 version;
4171   guint32 tkhd_flags;
4172   guint8 tkhd_version;
4173
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;
4183
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))
4187     goto corrupt_file;
4188
4189   GST_LOG_OBJECT (qtdemux, "track[tkhd] version/flags: 0x%02x/%06x",
4190       tkhd_version, tkhd_flags);
4191
4192   if (!(mdia = qtdemux_tree_get_child_by_type (trak, FOURCC_mdia)))
4193     goto corrupt_file;
4194
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)))
4199       goto corrupt_file;
4200   }
4201
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);
4207   } else {
4208     stream->timescale = QT_UINT32 ((guint8 *) mdhd->data + 20);
4209     stream->duration = QT_UINT32 ((guint8 *) mdhd->data + 24);
4210   }
4211
4212   GST_LOG_OBJECT (qtdemux, "track timescale: %" G_GUINT32_FORMAT,
4213       stream->timescale);
4214   GST_LOG_OBJECT (qtdemux, "track duration: %" G_GUINT64_FORMAT,
4215       stream->duration);
4216
4217   if (G_UNLIKELY (stream->timescale == 0 || qtdemux->timescale == 0))
4218     goto corrupt_file;
4219
4220   if (qtdemux->duration != G_MAXINT32 && stream->duration != G_MAXINT32) {
4221     guint64 tdur1, tdur2;
4222
4223     /* don't overflow */
4224     tdur1 = stream->timescale * (guint64) qtdemux->duration;
4225     tdur2 = qtdemux->timescale * (guint64) stream->duration;
4226
4227     /* HACK:
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);
4238       g_free (stream);
4239       return TRUE;
4240     }
4241   }
4242
4243   if (!(hdlr = qtdemux_tree_get_child_by_type (mdia, FOURCC_hdlr)))
4244     goto corrupt_file;
4245
4246   GST_LOG_OBJECT (qtdemux, "track type: %" GST_FOURCC_FORMAT,
4247       GST_FOURCC_ARGS (QT_FOURCC ((guint8 *) hdlr->data + 12)));
4248
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));
4252
4253   if (!(minf = qtdemux_tree_get_child_by_type (mdia, FOURCC_minf)))
4254     goto corrupt_file;
4255
4256   if (!(stbl = qtdemux_tree_get_child_by_type (minf, FOURCC_stbl)))
4257     goto corrupt_file;
4258
4259   /* parse stsd */
4260   if (!(stsd = qtdemux_tree_get_child_by_type (stbl, FOURCC_stsd)))
4261     goto corrupt_file;
4262   stsd_data = (const guint8 *) stsd->data;
4263
4264   if (stream->subtype == FOURCC_vide) {
4265     guint32 w, h, fourcc;
4266
4267     stream->sampled = TRUE;
4268
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))
4273       goto corrupt_file;
4274
4275     stream->display_width = w >> 16;
4276     stream->display_height = h >> 16;
4277
4278     offset = 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));
4282
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);
4289
4290     GST_LOG_OBJECT (qtdemux, "frame count:   %u",
4291         QT_UINT16 (stsd_data + offset + 48));
4292
4293     if (fourcc == FOURCC_drms)
4294       goto error_encrypted;
4295
4296     stream->caps =
4297         qtdemux_video_caps (qtdemux, stream, fourcc, stsd_data, &codec);
4298     if (codec) {
4299       list = gst_tag_list_new ();
4300       gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
4301           GST_TAG_VIDEO_CODEC, codec, NULL);
4302       g_free (codec);
4303       codec = NULL;
4304     }
4305
4306     esds = NULL;
4307     pasp = 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 */
4311     if (!mp4v)
4312       mp4v = qtdemux_tree_get_child_by_type (stsd, FOURCC_avc1);
4313     if (mp4v) {
4314       esds = qtdemux_tree_get_child_by_type (mp4v, FOURCC_esds);
4315       pasp = qtdemux_tree_get_child_by_type (mp4v, FOURCC_pasp);
4316     }
4317
4318     if (pasp) {
4319       const guint8 *pasp_data = (const guint8 *) pasp->data;
4320
4321       stream->par_w = QT_UINT32 (pasp_data + 8);
4322       stream->par_h = QT_UINT32 (pasp_data + 12);
4323     } else {
4324       stream->par_w = 0;
4325       stream->par_h = 0;
4326     }
4327
4328     if (esds) {
4329       gst_qtdemux_handle_esds (qtdemux, stream, esds, list);
4330     } else {
4331       switch (fourcc) {
4332         case FOURCC_avc1:
4333         {
4334           gint len = QT_UINT32 (stsd_data) - 0x66;
4335           const guint8 *avc_data = stsd_data + 0x66;
4336
4337           /* find avcC */
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);
4343           }
4344
4345           /* parse, if found */
4346           if (len > 0x8 && QT_FOURCC (avc_data + 0x4) == FOURCC_avcC) {
4347             GstBuffer *buf;
4348             gint size;
4349
4350             if (QT_UINT32 (avc_data) < len)
4351               size = QT_UINT32 (avc_data) - 0x8;
4352             else
4353               size = len - 0x8;
4354
4355             GST_DEBUG_OBJECT (qtdemux, "found avcC codec_data in stsd");
4356
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);
4362           }
4363           break;
4364         }
4365         case FOURCC_mjp2:
4366         {
4367           GNode *jp2h, *colr, *mjp2, *field, *prefix;
4368           const guint8 *data;
4369           guint32 fourcc = 0;
4370
4371           GST_DEBUG_OBJECT (qtdemux, "found mjp2");
4372           /* some required atoms */
4373           mjp2 = qtdemux_tree_get_child_by_type (stsd, FOURCC_mjp2);
4374           if (!mjp2)
4375             break;
4376           jp2h = qtdemux_tree_get_child_by_type (mjp2, FOURCC_jp2h);
4377           if (!jp2h)
4378             break;
4379           colr = qtdemux_tree_get_child_by_type (jp2h, FOURCC_colr);
4380           if (!colr)
4381             break;
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)) {
4386               case 16:
4387                 fourcc = GST_MAKE_FOURCC ('s', 'R', 'G', 'B');
4388                 break;
4389               case 17:
4390                 fourcc = GST_MAKE_FOURCC ('G', 'R', 'A', 'Y');
4391                 break;
4392               case 18:
4393                 fourcc = GST_MAKE_FOURCC ('s', 'Y', 'U', 'V');
4394                 break;
4395               default:
4396                 break;
4397             }
4398           }
4399
4400           if (fourcc)
4401             gst_caps_set_simple (stream->caps,
4402                 "fourcc", GST_TYPE_FOURCC, fourcc, NULL);
4403
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);
4407
4408           /* indicate possible fields in caps */
4409           if (field) {
4410             data = (guint8 *) field->data + 8;
4411             if (*data != 1)
4412               gst_caps_set_simple (stream->caps, "fields", G_TYPE_INT,
4413                   (gint) * data, NULL);
4414           }
4415           /* add codec_data if provided */
4416           if (prefix) {
4417             GstBuffer *buf;
4418             gint len;
4419
4420             GST_DEBUG_OBJECT (qtdemux, "found prefix data in stsd");
4421             data = prefix->data;
4422             len = QT_UINT32 (data);
4423             if (len > 0x8) {
4424               len -= 0x8;
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);
4430             }
4431           }
4432           break;
4433         }
4434         case FOURCC_SVQ3:
4435         case FOURCC_VP31:
4436         {
4437           GstBuffer *buf;
4438           gint len = QT_UINT32 (stsd_data);
4439
4440           GST_DEBUG_OBJECT (qtdemux, "found codec_data in stsd");
4441
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);
4447           break;
4448         }
4449         case FOURCC_rle_:
4450         {
4451           gst_caps_set_simple (stream->caps,
4452               "depth", G_TYPE_INT, QT_UINT16 (stsd_data + offset + 82), NULL);
4453           break;
4454         }
4455         case FOURCC_XiTh:
4456         {
4457           GNode *xith, *xdxt;
4458
4459           GST_DEBUG_OBJECT (qtdemux, "found XiTh");
4460           xith = qtdemux_tree_get_child_by_type (stsd, FOURCC_XiTh);
4461           if (!xith)
4462             break;
4463
4464           xdxt = qtdemux_tree_get_child_by_type (xith, FOURCC_XdxT);
4465           if (!xdxt)
4466             break;
4467
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);
4472           break;
4473         }
4474         default:
4475           break;
4476       }
4477     }
4478
4479     GST_INFO_OBJECT (qtdemux,
4480         "type %" GST_FOURCC_FORMAT " caps %" GST_PTR_FORMAT,
4481         GST_FOURCC_ARGS (fourcc), stream->caps);
4482
4483   } else if (stream->subtype == FOURCC_soun) {
4484     int version, samplesize;
4485     guint32 fourcc;
4486     int len;
4487     guint16 compression_id;
4488
4489     len = QT_UINT32 (stsd_data + 16);
4490     GST_LOG_OBJECT (qtdemux, "stsd len:           %d", len);
4491
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));
4495
4496     offset = 32;
4497
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);
4503
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);
4513
4514     if (compression_id == 0xfffe)
4515       stream->sampled = TRUE;
4516
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;
4523
4524     offset = 52;
4525     switch (fourcc) {
4526         /* Yes, these have to be hard-coded */
4527       case FOURCC_MAC6:
4528       {
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;
4534         break;
4535       }
4536       case FOURCC_MAC3:
4537       {
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;
4543         break;
4544       }
4545       case FOURCC_ima4:
4546       {
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;
4552         break;
4553       }
4554       case FOURCC_ulaw:
4555       case FOURCC_alaw:
4556       {
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;
4562         break;
4563       }
4564       case FOURCC_agsm:
4565       {
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;
4571         break;
4572       }
4573       default:
4574         break;
4575     }
4576
4577     if (version == 0x00010000) {
4578       switch (fourcc) {
4579         case FOURCC_twos:
4580         case FOURCC_sowt:
4581         case FOURCC_raw_:
4582           break;
4583         default:
4584         {
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);
4590
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);
4599
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);
4605           }
4606           break;
4607         }
4608       }
4609     } else if (version == 0x00020000) {
4610       union
4611       {
4612         gdouble fp;
4613         guint64 val;
4614       } qtfp;
4615
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);
4620
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);
4625
4626     } else {
4627       GST_WARNING_OBJECT (qtdemux, "unknown version %08x", version);
4628     }
4629
4630     if (fourcc == FOURCC_drms)
4631       goto error_encrypted;
4632
4633     stream->caps = qtdemux_audio_caps (qtdemux, stream, fourcc, NULL, 0,
4634         &codec);
4635
4636     switch (fourcc) {
4637       case FOURCC_in24:
4638       {
4639         GNode *enda;
4640         GNode *in24;
4641
4642         in24 = qtdemux_tree_get_child_by_type (stsd, FOURCC_in24);
4643
4644         enda = qtdemux_tree_get_child_by_type (in24, FOURCC_enda);
4645         if (!enda) {
4646           wave = qtdemux_tree_get_child_by_type (in24, FOURCC_wave);
4647           if (wave)
4648             enda = qtdemux_tree_get_child_by_type (wave, FOURCC_enda);
4649         }
4650         if (enda) {
4651           gst_caps_set_simple (stream->caps,
4652               "endianness", G_TYPE_INT, G_LITTLE_ENDIAN, NULL);
4653         }
4654         break;
4655       }
4656       default:
4657         break;
4658     }
4659
4660     if (codec) {
4661       list = gst_tag_list_new ();
4662       gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
4663           GST_TAG_AUDIO_CODEC, codec, NULL);
4664       g_free (codec);
4665       codec = NULL;
4666     }
4667
4668     mp4a = qtdemux_tree_get_child_by_type (stsd, FOURCC_mp4a);
4669     wave = NULL;
4670     esds = NULL;
4671     if (mp4a) {
4672       wave = qtdemux_tree_get_child_by_type (mp4a, FOURCC_wave);
4673       if (wave)
4674         esds = qtdemux_tree_get_child_by_type (wave, FOURCC_esds);
4675       if (!esds)
4676         esds = qtdemux_tree_get_child_by_type (mp4a, FOURCC_esds);
4677     }
4678
4679     if (esds) {
4680       gst_qtdemux_handle_esds (qtdemux, stream, esds, list);
4681     } else {
4682       switch (fourcc) {
4683 #if 0
4684           /* FIXME: what is in the chunk? */
4685         case FOURCC_QDMC:
4686         {
4687           gint len = QT_UINT32 (stsd_data);
4688
4689           /* seems to be always = 116 = 0x74 */
4690           break;
4691         }
4692 #endif
4693         case FOURCC_QDM2:
4694         {
4695           gint len = QT_UINT32 (stsd_data);
4696
4697           if (len > 0x4C) {
4698             GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x4C);
4699
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);
4704           }
4705           gst_caps_set_simple (stream->caps,
4706               "samplesize", G_TYPE_INT, samplesize, NULL);
4707           break;
4708         }
4709         case FOURCC_alac:
4710         {
4711           gint len = QT_UINT32 (stsd_data);
4712
4713           if (len > 0x34) {
4714             GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x34);
4715
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);
4720           }
4721           gst_caps_set_simple (stream->caps,
4722               "samplesize", G_TYPE_INT, samplesize, NULL);
4723           break;
4724         }
4725         case FOURCC_samr:
4726         {
4727           gint len = QT_UINT32 (stsd_data);
4728
4729           if (len > 0x34) {
4730             GstBuffer *buf = gst_buffer_new_and_alloc (len - 0x34);
4731
4732             memcpy (GST_BUFFER_DATA (buf), stsd_data + 0x34, len - 0x34);
4733
4734             gst_caps_set_simple (stream->caps,
4735                 "codec_data", GST_TYPE_BUFFER, buf, NULL);
4736             gst_buffer_unref (buf);
4737           }
4738           break;
4739         }
4740         default:
4741           break;
4742       }
4743     }
4744     GST_INFO_OBJECT (qtdemux,
4745         "type %" GST_FOURCC_FORMAT " caps %" GST_PTR_FORMAT,
4746         GST_FOURCC_ARGS (fourcc), stream->caps);
4747
4748   } else if (stream->subtype == FOURCC_strm) {
4749     guint32 fourcc;
4750
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));
4754
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;
4759     }
4760     stream->sampled = TRUE;
4761   } else if (stream->subtype == FOURCC_subp) {
4762     guint32 fourcc;
4763
4764     stream->sampled = TRUE;
4765
4766     offset = 16;
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));
4770
4771     stream->caps =
4772         qtdemux_subp_caps (qtdemux, stream, fourcc, stsd_data, &codec);
4773   } else {
4774     goto unknown_stream;
4775   }
4776
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;
4790   }
4791
4792   /* collect sample information */
4793   if (!qtdemux_parse_samples (qtdemux, stream, stbl))
4794     goto samples_failed;
4795
4796   /* configure segments */
4797   if (!qtdemux_parse_segments (qtdemux, stream, trak))
4798     goto segments_failed;
4799
4800   /* now we are ready to add the stream */
4801   gst_qtdemux_add_stream (qtdemux, stream, list);
4802
4803   return TRUE;
4804
4805 /* ERRORS */
4806 corrupt_file:
4807   {
4808     GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
4809         (_("This file is corrupt and cannot be played.")), (NULL));
4810     g_free (stream);
4811     return FALSE;
4812   }
4813 error_encrypted:
4814   {
4815     GST_ELEMENT_ERROR (qtdemux, STREAM, DECRYPT, (NULL), (NULL));
4816     g_free (stream);
4817     return FALSE;
4818   }
4819 samples_failed:
4820   {
4821     /* we posted an error already */
4822     g_free (stream);
4823     return FALSE;
4824   }
4825 segments_failed:
4826   {
4827     /* we posted an error already */
4828     g_free (stream);
4829     return FALSE;
4830   }
4831 unknown_stream:
4832   {
4833     GST_INFO_OBJECT (qtdemux, "unknown subtype %" GST_FOURCC_FORMAT,
4834         GST_FOURCC_ARGS (stream->subtype));
4835     g_free (stream);
4836     return TRUE;
4837   }
4838 }
4839
4840 /* check if major or compatible brand is 3GP */
4841 static inline gboolean
4842 qtdemux_is_brand_3gp (GstQTDemux * qtdemux, gboolean major)
4843 {
4844   if (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;
4851
4852     while (size >= 4) {
4853       res = res || ((QT_FOURCC (data) & GST_MAKE_FOURCC (255, 255, 0, 0)) ==
4854           GST_MAKE_FOURCC ('3', 'g', 0, 0));
4855       data += 4;
4856       size -= 4;
4857     }
4858     return res;
4859   } else {
4860     return FALSE;
4861   }
4862 }
4863
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)
4867 {
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;
4871 }
4872
4873 static void
4874 qtdemux_tag_add_location (GstQTDemux * qtdemux, const char *tag,
4875     const char *dummy, GNode * node)
4876 {
4877   const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
4878   int offset;
4879   char *name;
4880   gchar *data;
4881   gdouble longitude, latitude, altitude;
4882
4883   data = node->data;
4884   offset = 14;
4885
4886   /* TODO: language code skipped */
4887
4888   name = gst_tag_freeform_string_to_utf8 (data + offset, -1, env_vars);
4889
4890   if (!name) {
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, "
4894           "giving up", tag);
4895     }
4896   } else {
4897     gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
4898         GST_TAG_GEO_LOCATION_NAME, name, NULL);
4899     offset += strlen (name);
4900     g_free (name);
4901   }
4902
4903   /* +1 +1 = skip null-terminator and location role byte */
4904   offset += 1 + 1;
4905   longitude = QT_FP32 (data + offset);
4906
4907   offset += 4;
4908   latitude = QT_FP32 (data + offset);
4909
4910   offset += 4;
4911   altitude = QT_FP32 (data + offset);
4912
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);
4920   }
4921
4922   /* TODO: no GST_TAG_, so astronomical body and additional notes skipped */
4923 }
4924
4925
4926 static void
4927 qtdemux_tag_add_year (GstQTDemux * qtdemux, const char *tag, const char *dummy,
4928     GNode * node)
4929 {
4930   guint16 y;
4931   GDate *date;
4932
4933   y = QT_UINT16 ((guint8 *) node->data + 12);
4934   GST_DEBUG_OBJECT (qtdemux, "year: %u", y);
4935
4936   date = g_date_new_dmy (1, 1, y);
4937   gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag, date, NULL);
4938   g_date_free (date);
4939 }
4940
4941 static void
4942 qtdemux_tag_add_classification (GstQTDemux * qtdemux, const char *tag,
4943     const char *dummy, GNode * node)
4944 {
4945   int offset;
4946   char *tag_str = NULL;
4947   guint8 *entity;
4948   guint16 table;
4949
4950
4951   offset = 12;
4952   entity = (guint8 *) node->data + offset;
4953
4954   offset += 4;
4955   table = QT_UINT16 ((guint8 *) node->data + offset);
4956
4957   /* Language code skipped */
4958
4959   offset += 4;
4960
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.
4964    */
4965   tag_str = g_strdup_printf ("----://%u/%s",
4966       table, (char *) node->data + offset);
4967
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);
4971
4972   gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_APPEND, tag,
4973       tag_str, NULL);
4974
4975   g_free (tag_str);
4976 }
4977
4978 static gboolean
4979 qtdemux_tag_add_str_full (GstQTDemux * qtdemux, const char *tag,
4980     const char *dummy, GNode * node)
4981 {
4982   const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
4983   GNode *data;
4984   char *s;
4985   int len;
4986   guint32 type;
4987   int offset;
4988   gboolean ret = TRUE;
4989
4990   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
4991   if (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,
4996           env_vars);
4997       if (s) {
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,
5000             NULL);
5001         g_free (s);
5002       } else {
5003         GST_DEBUG_OBJECT (qtdemux, "failed to convert %s tag to UTF-8", tag);
5004       }
5005     }
5006   } else {
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 */
5012       offset = 12;
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);
5017
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))) {
5023         offset = 14;
5024         /* 16-bit Language code is ignored here as well */
5025         GST_DEBUG_OBJECT (qtdemux, "found 3gpp text tag");
5026       } else {
5027         goto normal;
5028       }
5029     } else {
5030     normal:
5031       offset = 8;
5032       GST_DEBUG_OBJECT (qtdemux, "found normal text tag");
5033       ret = FALSE;              /* may have to fallback */
5034     }
5035     s = gst_tag_freeform_string_to_utf8 ((char *) node->data + offset,
5036         len - offset, env_vars);
5037     if (s) {
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);
5040       g_free (s);
5041       ret = TRUE;
5042     } else {
5043       GST_DEBUG_OBJECT (qtdemux, "failed to convert %s tag to UTF-8", tag);
5044     }
5045   }
5046   return ret;
5047 }
5048
5049 static void
5050 qtdemux_tag_add_str (GstQTDemux * qtdemux, const char *tag,
5051     const char *dummy, GNode * node)
5052 {
5053   qtdemux_tag_add_str_full (qtdemux, tag, dummy, node);
5054 }
5055
5056 static void
5057 qtdemux_tag_add_keywords (GstQTDemux * qtdemux, const char *tag,
5058     const char *dummy, GNode * node)
5059 {
5060   const gchar *env_vars[] = { "GST_QT_TAG_ENCODING", "GST_TAG_ENCODING", NULL };
5061   guint8 *data;
5062   char *s, *t, *k = NULL;
5063   int len;
5064   int offset;
5065   int count;
5066
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 */
5072       data = node->data;
5073       if (QT_UINT32 (data) < 15 || !qtdemux_is_brand_3gp (qtdemux, FALSE))
5074         return;
5075     } else
5076       return;
5077   }
5078
5079   GST_DEBUG_OBJECT (qtdemux, "found 3gpp keyword tag");
5080
5081   data = node->data;
5082
5083   len = QT_UINT32 (data);
5084   if (len < 15)
5085     goto short_read;
5086
5087   count = QT_UINT8 (data + 14);
5088   offset = 15;
5089   for (; count; count--) {
5090     gint slen;
5091
5092     if (offset + 1 > len)
5093       goto short_read;
5094     slen = QT_UINT8 (data + offset);
5095     offset += 1;
5096     if (offset + slen > len)
5097       goto short_read;
5098     s = gst_tag_freeform_string_to_utf8 ((char *) node->data + offset,
5099         slen, env_vars);
5100     if (s) {
5101       GST_DEBUG_OBJECT (qtdemux, "adding keyword %s", GST_STR_NULL (s));
5102       if (k) {
5103         t = g_strjoin (",", k, s, NULL);
5104         g_free (s);
5105         g_free (k);
5106         k = t;
5107       } else {
5108         k = s;
5109       }
5110     } else {
5111       GST_DEBUG_OBJECT (qtdemux, "failed to convert keyword to UTF-8");
5112     }
5113     offset += slen;
5114   }
5115
5116 done:
5117   if (k) {
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);
5120   }
5121   g_free (k);
5122
5123   return;
5124
5125   /* ERRORS */
5126 short_read:
5127   {
5128     GST_DEBUG_OBJECT (qtdemux, "short read parsing 3GP keywords");
5129     goto done;
5130   }
5131 }
5132
5133 static void
5134 qtdemux_tag_add_num (GstQTDemux * qtdemux, const char *tag1,
5135     const char *tag2, GNode * node)
5136 {
5137   GNode *data;
5138   int len;
5139   int type;
5140   int n1, n2;
5141
5142   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5143   if (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);
5149       if (n1 > 0) {
5150         GST_DEBUG_OBJECT (qtdemux, "adding tag %s=%d", tag1, n1);
5151         gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5152             tag1, n1, NULL);
5153       }
5154       if (n2 > 0) {
5155         GST_DEBUG_OBJECT (qtdemux, "adding tag %s=%d", tag2, n2);
5156         gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE,
5157             tag2, n2, NULL);
5158       }
5159     }
5160   }
5161 }
5162
5163 static void
5164 qtdemux_tag_add_tmpo (GstQTDemux * qtdemux, const char *tag1, const char *dummy,
5165     GNode * node)
5166 {
5167   GNode *data;
5168   int len;
5169   int type;
5170   int n1;
5171
5172   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5173   if (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);
5180       if (n1) {
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);
5185       }
5186     }
5187   }
5188 }
5189
5190 static void
5191 qtdemux_tag_add_covr (GstQTDemux * qtdemux, const char *tag1, const char *dummy,
5192     GNode * node)
5193 {
5194   GNode *data;
5195   int len;
5196   int type;
5197   GstBuffer *buf;
5198
5199   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5200   if (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,
5209             tag1, buf, NULL);
5210         gst_buffer_unref (buf);
5211       }
5212     }
5213   }
5214 }
5215
5216 static void
5217 qtdemux_tag_add_date (GstQTDemux * qtdemux, const char *tag, const char *dummy,
5218     GNode * node)
5219 {
5220   GNode *data;
5221   char *s;
5222   int len;
5223   int type;
5224
5225   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5226   if (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;
5231       gint ret;
5232
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) {
5237         GDate *date;
5238
5239         date = g_date_new_dmy (d, m, y);
5240         gst_tag_list_add (qtdemux->tag_list, GST_TAG_MERGE_REPLACE, tag,
5241             date, NULL);
5242         g_date_free (date);
5243       } else {
5244         GST_DEBUG_OBJECT (qtdemux, "could not parse date string '%s'", s);
5245       }
5246       g_free (s);
5247     }
5248   }
5249 }
5250
5251 static void
5252 qtdemux_tag_add_gnre (GstQTDemux * qtdemux, const char *tag, const char *dummy,
5253     GNode * node)
5254 {
5255   GNode *data;
5256
5257   data = qtdemux_tree_get_child_by_type (node, FOURCC_data);
5258
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);
5264     return;
5265   }
5266
5267   if (data) {
5268     guint len, type, n;
5269
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);
5274       if (n > 0) {
5275         const gchar *genre;
5276
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,
5281               tag, genre, NULL);
5282         }
5283       }
5284     }
5285   }
5286 }
5287
5288 typedef void (*GstQTDemuxAddTagFunc) (GstQTDemux * demux,
5289     const char *tag, const char *tag_bis, GNode * node);
5290
5291 static const struct
5292 {
5293   guint32 fourcc;
5294   const gchar *gst_tag;
5295   const gchar *gst_tag_bis;
5296   const GstQTDemuxAddTagFunc func;
5297 } add_funcs[] = {
5298   {
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}
5332 };
5333
5334 static void
5335 qtdemux_tag_add_blob (GNode * node, GstQTDemux * demux)
5336 {
5337   gint len;
5338   guint8 *data;
5339   GstBuffer *buf;
5340   gchar *media_type, *style;
5341   GstCaps *caps;
5342   guint i;
5343   guint8 ndata[4];
5344
5345   data = node->data;
5346   len = QT_UINT32 (data);
5347   buf = gst_buffer_new_and_alloc (len);
5348   memcpy (GST_BUFFER_DATA (buf), data, len);
5349
5350   /* heuristic to determine style of tag */
5351   if (QT_FOURCC (data + 4) == FOURCC_____ ||
5352       (len > 8 + 12 && QT_FOURCC (data + 12) == FOURCC_data))
5353     style = "itunes";
5354   else if (demux->major_brand == FOURCC_qt__)
5355     style = "quicktime";
5356   /* fall back to assuming iso/3gp tag style */
5357   else
5358     style = "iso";
5359
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);
5365     else
5366       ndata[i] = '_';
5367   }
5368
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);
5372
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);
5377
5378   GST_DEBUG_OBJECT (demux, "adding private tag; size %d, caps %" GST_PTR_FORMAT,
5379       GST_BUFFER_SIZE (buf), caps);
5380
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);
5384 }
5385
5386 static void
5387 qtdemux_parse_udta (GstQTDemux * qtdemux, GNode * udta)
5388 {
5389   GNode *meta;
5390   GNode *ilst;
5391   GNode *node;
5392   gint i;
5393
5394   meta = qtdemux_tree_get_child_by_type (udta, FOURCC_meta);
5395   if (meta != NULL) {
5396     ilst = qtdemux_tree_get_child_by_type (meta, FOURCC_ilst);
5397     if (ilst == NULL) {
5398       GST_LOG_OBJECT (qtdemux, "no ilst");
5399       return;
5400     }
5401   } else {
5402     ilst = udta;
5403     GST_LOG_OBJECT (qtdemux, "no meta so using udta itself");
5404   }
5405
5406   GST_DEBUG_OBJECT (qtdemux, "new tag list");
5407   qtdemux->tag_list = gst_tag_list_new ();
5408
5409   for (i = 0; i < G_N_ELEMENTS (add_funcs); ++i) {
5410     node = qtdemux_tree_get_child_by_type (ilst, add_funcs[i].fourcc);
5411     if (node) {
5412       add_funcs[i].func (qtdemux, add_funcs[i].gst_tag,
5413           add_funcs[i].gst_tag_bis, node);
5414       g_node_destroy (node);
5415     }
5416   }
5417
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);
5421
5422 }
5423
5424 typedef struct
5425 {
5426   GstStructure *structure;      /* helper for sort function */
5427   gchar *location;
5428   guint min_req_bitrate;
5429   guint min_req_qt_version;
5430 } GstQtReference;
5431
5432 static gint
5433 qtdemux_redirects_sort_func (gconstpointer a, gconstpointer b)
5434 {
5435   GstQtReference *ref_a = (GstQtReference *) a;
5436   GstQtReference *ref_b = (GstQtReference *) b;
5437
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;
5440
5441   /* known bitrates go before unknown; higher bitrates go first */
5442   return ref_b->min_req_bitrate - ref_a->min_req_bitrate;
5443 }
5444
5445 /* sort the redirects and post a message for the application.
5446  */
5447 static void
5448 qtdemux_process_redirects (GstQTDemux * qtdemux, GList * references)
5449 {
5450   GstQtReference *best;
5451   GstStructure *s;
5452   GstMessage *msg;
5453   GValue list_val = { 0, };
5454   GList *l;
5455
5456   g_assert (references != NULL);
5457
5458   references = g_list_sort (references, qtdemux_redirects_sort_func);
5459
5460   best = (GstQtReference *) references->data;
5461
5462   g_value_init (&list_val, GST_TYPE_LIST);
5463
5464   for (l = references; l != NULL; l = l->next) {
5465     GstQtReference *ref = (GstQtReference *) l->data;
5466     GValue struct_val = { 0, };
5467
5468     ref->structure = gst_structure_new ("redirect",
5469         "new-location", G_TYPE_STRING, ref->location, NULL);
5470
5471     if (ref->min_req_bitrate > 0) {
5472       gst_structure_set (ref->structure, "minimum-bitrate", G_TYPE_INT,
5473           ref->min_req_bitrate, NULL);
5474     }
5475
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 */
5481   }
5482
5483   g_assert (best != NULL);
5484   s = gst_structure_copy (best->structure);
5485
5486   if (g_list_length (references) > 1) {
5487     gst_structure_set_value (s, "locations", &list_val);
5488   }
5489
5490   g_value_unset (&list_val);
5491
5492   for (l = references; l != NULL; l = l->next) {
5493     GstQtReference *ref = (GstQtReference *) l->data;
5494
5495     gst_structure_free (ref->structure);
5496     g_free (ref->location);
5497     g_free (ref);
5498   }
5499   g_list_free (references);
5500
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);
5504 }
5505
5506 /* look for redirect nodes, collect all redirect information and
5507  * process it.
5508  */
5509 static gboolean
5510 qtdemux_parse_redirects (GstQTDemux * qtdemux)
5511 {
5512   GNode *rmra, *rmda, *rdrf;
5513
5514   rmra = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_rmra);
5515   if (rmra) {
5516     GList *redirects = NULL;
5517
5518     rmda = qtdemux_tree_get_child_by_type (rmra, FOURCC_rmda);
5519     while (rmda) {
5520       GstQtReference ref = { NULL, NULL, 0, 0 };
5521       GNode *rmdr, *rmvc;
5522
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);
5527       }
5528
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);
5532
5533 #ifndef GST_DISABLE_GST_DEBUG
5534         guint bitmask = QT_UINT32 ((guint8 *) rmvc->data + 20);
5535 #endif
5536         guint check_type = QT_UINT16 ((guint8 *) rmvc->data + 24);
5537
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;
5544         }
5545       }
5546
5547       rdrf = qtdemux_tree_get_child_by_type (rmda, FOURCC_rdrf);
5548       if (rdrf) {
5549         guint32 ref_type;
5550         guint8 *ref_data;
5551
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;
5556
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);
5563           }
5564         } else if (ref_type == FOURCC_url_) {
5565           ref.location = g_strdup ((gchar *) ref_data);
5566         } else {
5567           GST_DEBUG_OBJECT (qtdemux,
5568               "unknown rdrf reference type %" GST_FOURCC_FORMAT,
5569               GST_FOURCC_ARGS (ref_type));
5570         }
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)));
5574         } else {
5575           GST_WARNING_OBJECT (qtdemux,
5576               "Failed to extract redirect location from rdrf atom");
5577         }
5578       }
5579
5580       /* look for others */
5581       rmda = qtdemux_tree_get_sibling_by_type (rmda, FOURCC_rmda);
5582     }
5583
5584     if (redirects != NULL) {
5585       qtdemux_process_redirects (qtdemux, redirects);
5586     }
5587   }
5588   return TRUE;
5589 }
5590
5591 static GstTagList *
5592 qtdemux_add_container_format (GstQTDemux * qtdemux, GstTagList * tags)
5593 {
5594   const gchar *fmt;
5595
5596   if (tags == NULL)
5597     tags = gst_tag_list_new ();
5598
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))
5602     fmt = "3GP";
5603   else if (qtdemux->major_brand == FOURCC_qt__)
5604     fmt = "Quicktime";
5605   else
5606     fmt = "ISO MP4/M4A";
5607
5608   GST_LOG_OBJECT (qtdemux, "mapped %" GST_FOURCC_FORMAT " to '%s'",
5609       GST_FOURCC_ARGS (qtdemux->major_brand), fmt);
5610
5611   gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_CONTAINER_FORMAT,
5612       fmt, NULL);
5613
5614   return tags;
5615 }
5616
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
5620  */
5621 static gboolean
5622 qtdemux_parse_tree (GstQTDemux * qtdemux)
5623 {
5624   GNode *mvhd;
5625   GNode *trak;
5626   GNode *udta;
5627   gint64 duration;
5628
5629   mvhd = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_mvhd);
5630   if (mvhd == NULL) {
5631     GST_LOG_OBJECT (qtdemux, "No mvhd node found, looking for redirects.");
5632     return qtdemux_parse_redirects (qtdemux);
5633   }
5634
5635   qtdemux->timescale = QT_UINT32 ((guint8 *) mvhd->data + 20);
5636   qtdemux->duration = QT_UINT32 ((guint8 *) mvhd->data + 24);
5637
5638   GST_INFO_OBJECT (qtdemux, "timescale: %u", qtdemux->timescale);
5639   GST_INFO_OBJECT (qtdemux, "duration: %u", qtdemux->duration);
5640
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);
5644
5645   /* parse all traks */
5646   trak = qtdemux_tree_get_child_by_type (qtdemux->moov_node, FOURCC_trak);
5647   while (trak) {
5648     qtdemux_parse_trak (qtdemux, trak);
5649     /* iterate all siblings */
5650     trak = qtdemux_tree_get_sibling_by_type (trak, FOURCC_trak);
5651   }
5652   gst_element_no_more_pads (GST_ELEMENT_CAST (qtdemux));
5653
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);
5657   if (udta) {
5658     qtdemux_parse_udta (qtdemux, udta);
5659   } else {
5660     GST_LOG_OBJECT (qtdemux, "No udta node found.");
5661   }
5662
5663   qtdemux->tag_list = qtdemux_add_container_format (qtdemux, qtdemux->tag_list);
5664   GST_INFO_OBJECT (qtdemux, "posting global tags: %" GST_PTR_FORMAT,
5665       qtdemux->tag_list);
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)));
5670
5671   return TRUE;
5672 }
5673
5674 /* taken from ffmpeg */
5675 static unsigned int
5676 get_size (guint8 * ptr, guint8 ** end)
5677 {
5678   int count = 4;
5679   int len = 0;
5680
5681   while (count--) {
5682     int c = *ptr;
5683
5684     ptr++;
5685     len = (len << 7) | (c & 0x7f);
5686     if (!(c & 0x80))
5687       break;
5688   }
5689   if (end)
5690     *end = ptr;
5691   return len;
5692 }
5693
5694 /* this can change the codec originally present in @list */
5695 static void
5696 gst_qtdemux_handle_esds (GstQTDemux * qtdemux, QtDemuxStream * stream,
5697     GNode * esds, GstTagList * list)
5698 {
5699   int len = QT_UINT32 (esds->data);
5700   guint8 *ptr = esds->data;
5701   guint8 *end = ptr + len;
5702   int tag;
5703   guint8 *data_ptr = NULL;
5704   int data_len = 0;
5705   guint8 object_type_id = 0;
5706   char *codec_name = NULL;
5707   GstCaps *caps = NULL;
5708
5709   GST_MEMDUMP_OBJECT (qtdemux, "esds", ptr, len);
5710   ptr += 8;
5711   GST_DEBUG_OBJECT (qtdemux, "version/flags = %08x", QT_UINT32 (ptr));
5712   ptr += 4;
5713   while (ptr < end) {
5714     tag = QT_UINT8 (ptr);
5715     GST_DEBUG_OBJECT (qtdemux, "tag = %02x", tag);
5716     ptr++;
5717     len = get_size (ptr, &ptr);
5718     GST_DEBUG_OBJECT (qtdemux, "len = %d", len);
5719
5720     switch (tag) {
5721       case 0x03:
5722         GST_DEBUG_OBJECT (qtdemux, "ID %04x", QT_UINT16 (ptr));
5723         GST_DEBUG_OBJECT (qtdemux, "priority %04x", QT_UINT8 (ptr + 2));
5724         ptr += 3;
5725         break;
5726       case 0x04:
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));
5733         ptr += 13;
5734         break;
5735       case 0x05:
5736         GST_MEMDUMP_OBJECT (qtdemux, "data", ptr, len);
5737         data_ptr = ptr;
5738         data_len = len;
5739         ptr += len;
5740         break;
5741       case 0x06:
5742         GST_DEBUG_OBJECT (qtdemux, "data %02x", QT_UINT8 (ptr));
5743         ptr += 1;
5744         break;
5745       default:
5746         GST_ERROR_OBJECT (qtdemux, "parse error");
5747         break;
5748     }
5749   }
5750
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
5753    * codecs. */
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);
5760       break;
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
5766        * wrong. */
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
5771         };
5772
5773         channels = (data_ptr[1] & 0x7f) >> 3;
5774         if (channels <= 7) {
5775           stream->n_channels = channels;
5776         }
5777
5778         rateindex = ((data_ptr[0] & 0x7) << 1) | ((data_ptr[1] & 0x80) >> 7);
5779         if (rateindex < sizeof (rates) / sizeof (*rates)) {
5780           stream->rate = rates[rateindex];
5781         }
5782       }
5783       break;
5784     case 0x60:                 /* MPEG-2, various profiles */
5785     case 0x61:
5786     case 0x62:
5787     case 0x63:
5788     case 0x64:
5789     case 0x65:
5790       codec_name = "MPEG-2 video";
5791
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);
5796       break;
5797     case 0x69:                 /* MP3 has two different values, accept either */
5798     case 0x6B:
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";
5803       break;
5804     case 0x6A:                 /* MPEG-1 */
5805       codec_name = "MPEG-1 video";
5806
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);
5811       break;
5812     case 0x6C:                 /* MJPEG */
5813       caps = gst_caps_new_simple ("image/jpeg", NULL);
5814       codec_name = "Motion-JPEG";
5815       break;
5816     case 0x6D:                 /* PNG */
5817       caps = gst_caps_new_simple ("image/png", NULL);
5818       codec_name = "PNG still images";
5819       break;
5820     case 0x6E:                 /* JPEG2000 */
5821       codec_name = "JPEG-2000";
5822       caps = gst_caps_new_simple ("image/x-j2c", "fields", G_TYPE_INT, 1, NULL);
5823       break;
5824     case 0xA4:                 /* Dirac */
5825       codec_name = "Dirac";
5826       caps = gst_caps_new_simple ("video/x-dirac", NULL);
5827       break;
5828     case 0xA5:                 /* AC3 */
5829       codec_name = "AC-3 audio";
5830       caps = gst_caps_new_simple ("audio/x-ac3", NULL);
5831       break;
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";
5837       break;
5838     default:
5839       break;
5840   }
5841
5842   /* If we have a replacement caps, then change our caps for this stream */
5843   if (caps) {
5844     gst_caps_unref (stream->caps);
5845     stream->caps = caps;
5846   }
5847
5848   if (codec_name && list)
5849     gst_tag_list_add (list, GST_TAG_MERGE_REPLACE,
5850         GST_TAG_AUDIO_CODEC, codec_name, NULL);
5851
5852   /* Add the codec_data attribute to caps, if we have it */
5853   if (data_ptr) {
5854     GstBuffer *buffer;
5855
5856     buffer = gst_buffer_new_and_alloc (data_len);
5857     memcpy (GST_BUFFER_DATA (buffer), data_ptr, data_len);
5858
5859     GST_DEBUG_OBJECT (qtdemux, "setting codec_data from esds");
5860     GST_MEMDUMP_OBJECT (qtdemux, "codec_data from esds", data_ptr, data_len);
5861
5862     gst_caps_set_simple (stream->caps, "codec_data", GST_TYPE_BUFFER,
5863         buffer, NULL);
5864     gst_buffer_unref (buffer);
5865   }
5866
5867 }
5868
5869 #define _codec(name) \
5870   do { \
5871     if (codec_name) { \
5872       *codec_name = g_strdup (name); \
5873     } \
5874   } while (0)
5875
5876 static GstCaps *
5877 qtdemux_video_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
5878     guint32 fourcc, const guint8 * stsd_data, gchar ** codec_name)
5879 {
5880   GstCaps *caps;
5881   const GstStructure *s;
5882   const gchar *name;
5883
5884   switch (fourcc) {
5885     case GST_MAKE_FOURCC ('p', 'n', 'g', ' '):
5886       _codec ("PNG still images");
5887       caps = gst_caps_new_simple ("image/png", NULL);
5888       break;
5889     case GST_MAKE_FOURCC ('j', 'p', 'e', 'g'):
5890       _codec ("JPEG still images");
5891       caps = gst_caps_new_simple ("image/jpeg", NULL);
5892       break;
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);
5898       break;
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);
5902       break;
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);
5908       break;
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);
5913       break;
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);
5919       break;
5920     case GST_MAKE_FOURCC ('r', 'a', 'w', ' '):
5921     {
5922       guint16 bps;
5923
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,
5929           NULL);
5930
5931       switch (bps) {
5932         case 15:
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);
5939           break;
5940         case 16:
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);
5947           break;
5948         case 24:
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);
5955           break;
5956         case 32:
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);
5964           break;
5965         default:
5966           /* unknown */
5967           break;
5968       }
5969       break;
5970     }
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'),
5975           NULL);
5976       break;
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'),
5982           NULL);
5983       break;
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'),
5988           NULL);
5989       break;
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'),
5994           NULL);
5995       break;
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);
6001       break;
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);
6019       break;
6020     case GST_MAKE_FOURCC ('g', 'i', 'f', ' '):
6021       _codec ("GIF still images");
6022       caps = gst_caps_new_simple ("image/gif", NULL);
6023       break;
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'):
6028       _codec ("H.263");
6029       /* ffmpeg uses the height/width props, don't know why */
6030       caps = gst_caps_new_simple ("video/x-h263", NULL);
6031       break;
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);
6036       break;
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);
6042       break;
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);
6047       break;
6048     case GST_MAKE_FOURCC ('D', 'I', 'V', '3'):
6049       _codec ("DivX 3");
6050       caps = gst_caps_new_simple ("video/x-divx",
6051           "divxversion", G_TYPE_INT, 3, NULL);
6052       break;
6053     case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
6054     case GST_MAKE_FOURCC ('d', 'i', 'v', 'x'):
6055       _codec ("DivX 4");
6056       caps = gst_caps_new_simple ("video/x-divx",
6057           "divxversion", G_TYPE_INT, 4, NULL);
6058       break;
6059     case GST_MAKE_FOURCC ('D', 'X', '5', '0'):
6060       _codec ("DivX 5");
6061       caps = gst_caps_new_simple ("video/x-divx",
6062           "divxversion", G_TYPE_INT, 5, NULL);
6063       break;
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);
6068       break;
6069
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);
6074       if (codec_name)
6075         *codec_name = g_strdup ("FFmpeg MPEG-4");
6076       break;
6077
6078     case GST_MAKE_FOURCC ('c', 'v', 'i', 'd'):
6079       _codec ("Cinepak");
6080       caps = gst_caps_new_simple ("video/x-cinepak", NULL);
6081       break;
6082     case GST_MAKE_FOURCC ('q', 'd', 'r', 'w'):
6083       _codec ("Apple QuickDraw");
6084       caps = gst_caps_new_simple ("video/x-qdrw", NULL);
6085       break;
6086     case GST_MAKE_FOURCC ('r', 'p', 'z', 'a'):
6087       _codec ("Apple video");
6088       caps = gst_caps_new_simple ("video/x-apple-video", NULL);
6089       break;
6090     case GST_MAKE_FOURCC ('a', 'v', 'c', '1'):
6091       _codec ("H.264 / AVC");
6092       caps = gst_caps_new_simple ("video/x-h264", NULL);
6093       break;
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);
6098       break;
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);
6103       break;
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);
6109       break;
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);
6121       break;
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);
6127       break;
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);
6133       break;
6134     case GST_MAKE_FOURCC ('s', 'm', 'c', ' '):
6135       _codec ("Apple Graphics (SMC)");
6136       caps = gst_caps_new_simple ("video/x-smc", NULL);
6137       break;
6138     case GST_MAKE_FOURCC ('V', 'P', '3', '1'):
6139       _codec ("VP3");
6140       caps = gst_caps_new_simple ("video/x-vp3", NULL);
6141       break;
6142     case GST_MAKE_FOURCC ('X', 'i', 'T', 'h'):
6143       _codec ("Theora");
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;
6148       break;
6149     case GST_MAKE_FOURCC ('d', 'r', 'a', 'c'):
6150       _codec ("Dirac");
6151       caps = gst_caps_new_simple ("video/x-dirac", NULL);
6152       break;
6153     case GST_MAKE_FOURCC ('t', 'i', 'f', 'f'):
6154       _codec ("TIFF still images");
6155       caps = gst_caps_new_simple ("image/tiff", NULL);
6156       break;
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");
6160       break;
6161     case GST_MAKE_FOURCC ('A', 'V', 'd', 'n'):
6162       _codec ("AVID DNxHD");
6163       caps = gst_caps_from_string ("video/x-dnxhd");
6164       break;
6165     case GST_MAKE_FOURCC ('k', 'p', 'c', 'd'):
6166     default:
6167     {
6168       char *s;
6169
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);
6173       break;
6174     }
6175   }
6176
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;
6182   }
6183   return caps;
6184 }
6185
6186 static GstCaps *
6187 qtdemux_audio_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
6188     guint32 fourcc, const guint8 * data, int len, gchar ** codec_name)
6189 {
6190   GstCaps *caps;
6191   const GstStructure *s;
6192   const gchar *name;
6193   gint endian = 0;
6194
6195   GST_DEBUG_OBJECT (qtdemux, "resolve fourcc %08x", fourcc);
6196
6197   switch (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);
6203       break;
6204     case GST_MAKE_FOURCC ('t', 'w', 'o', 's'):
6205       endian = G_BIG_ENDIAN;
6206       /* fall-through */
6207     case GST_MAKE_FOURCC ('s', 'o', 'w', 't'):
6208     {
6209       gchar *str;
6210       gint depth;
6211
6212       if (!endian)
6213         endian = G_LITTLE_ENDIAN;
6214
6215       depth = stream->bytes_per_packet * 8;
6216       str = g_strdup_printf ("Raw %d-bit PCM audio", depth);
6217       _codec (str);
6218       g_free (str);
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);
6223       break;
6224     }
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);
6229       break;
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);
6234       break;
6235     case FOURCC_in24:
6236       _codec ("Raw 24-bit PCM audio");
6237       /* we assume BIG ENDIAN, an enda box will tell us to change this to little
6238        * endian later */
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);
6243       break;
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);
6250       break;
6251     case GST_MAKE_FOURCC ('u', 'l', 'a', 'w'):
6252       _codec ("Mu-law audio");
6253       caps = gst_caps_new_simple ("audio/x-mulaw", NULL);
6254       break;
6255     case GST_MAKE_FOURCC ('a', 'l', 'a', 'w'):
6256       _codec ("A-law audio");
6257       caps = gst_caps_new_simple ("audio/x-alaw", NULL);
6258       break;
6259     case 0x0200736d:
6260     case 0x6d730002:
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);
6265       break;
6266     case 0x1100736d:
6267     case 0x6d730011:
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);
6273       break;
6274     case 0x1700736d:
6275     case 0x6d730017:
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);
6280       break;
6281     case 0x5500736d:
6282     case 0x6d730055:
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);
6289       break;
6290     case 0x20736d:
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;
6295       break;
6296     case GST_MAKE_FOURCC ('M', 'A', 'C', '3'):
6297       _codec ("MACE-3");
6298       caps = gst_caps_new_simple ("audio/x-mace",
6299           "maceversion", G_TYPE_INT, 3, NULL);
6300       break;
6301     case GST_MAKE_FOURCC ('M', 'A', 'C', '6'):
6302       _codec ("MACE-6");
6303       caps = gst_caps_new_simple ("audio/x-mace",
6304           "maceversion", G_TYPE_INT, 6, NULL);
6305       break;
6306     case GST_MAKE_FOURCC ('O', 'g', 'g', 'V'):
6307       /* ogg/vorbis */
6308       caps = gst_caps_new_simple ("application/ogg", NULL);
6309       break;
6310     case GST_MAKE_FOURCC ('d', 'v', 'c', 'a'):
6311       _codec ("DV audio");
6312       caps = gst_caps_new_simple ("audio/x-dv", NULL);
6313       break;
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);
6318       break;
6319     case GST_MAKE_FOURCC ('Q', 'D', 'M', 'C'):
6320       _codec ("QDesign Music");
6321       caps = gst_caps_new_simple ("audio/x-qdm", NULL);
6322       break;
6323     case GST_MAKE_FOURCC ('Q', 'D', 'M', '2'):
6324       _codec ("QDesign Music v.2");
6325       /* FIXME: QDesign music version 2 (no constant) */
6326       if (data) {
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);
6331       } else {
6332         caps = gst_caps_new_simple ("audio/x-qdm2", NULL);
6333       }
6334       break;
6335     case GST_MAKE_FOURCC ('a', 'g', 's', 'm'):
6336       _codec ("GSM audio");
6337       caps = gst_caps_new_simple ("audio/x-gsm", NULL);
6338       break;
6339     case GST_MAKE_FOURCC ('s', 'a', 'm', 'r'):
6340       _codec ("AMR audio");
6341       caps = gst_caps_new_simple ("audio/AMR", NULL);
6342       break;
6343     case GST_MAKE_FOURCC ('s', 'a', 'w', 'b'):
6344       _codec ("AMR-WB audio");
6345       caps = gst_caps_new_simple ("audio/AMR-WB", NULL);
6346       break;
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);
6351       break;
6352     case GST_MAKE_FOURCC ('a', 'l', 'a', 'c'):
6353       _codec ("Apple lossless audio");
6354       caps = gst_caps_new_simple ("audio/x-alac", NULL);
6355       break;
6356     case GST_MAKE_FOURCC ('Q', 'c', 'l', 'p'):
6357       _codec ("QualComm PureVoice");
6358       caps = gst_caps_from_string ("audio/qcelp");
6359       break;
6360     case GST_MAKE_FOURCC ('q', 't', 'v', 'r'):
6361       /* ? */
6362     default:
6363     {
6364       char *s;
6365
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);
6369       break;
6370     }
6371   }
6372
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;
6378   }
6379   return caps;
6380 }
6381
6382 static GstCaps *
6383 qtdemux_subp_caps (GstQTDemux * qtdemux, QtDemuxStream * stream,
6384     guint32 fourcc, const guint8 * stsd_data, gchar ** codec_name)
6385 {
6386   GstCaps *caps;
6387
6388   GST_DEBUG_OBJECT (qtdemux, "resolve fourcc %08x", fourcc);
6389
6390   switch (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);
6394       break;
6395     default:
6396     {
6397       char *s;
6398
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);
6402       break;
6403     }
6404   }
6405   return caps;
6406 }