qtdemux.c 120 KB
Newer Older
Artyom Baginski's avatar
Artyom Baginski committed
1 2
/* GStreamer
 * 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>
Artyom Baginski's avatar
Artyom Baginski committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * SECTION:element-qtdemux
 *
 * <refsect2>
 * <para>
 * Demuxes a .mov file into raw or compressed audio and/or video streams.
 * </para>
 * <para>
 * This element supports both push and pull-based scheduling, depending on the
 * capabilities of the upstream elements.
 * </para>
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
 * gst-launch filesrc location=test.mov ! qtdemux name=demux  demux.audio_00 ! decodebin ! audioconvert ! audioresample ! autoaudiosink   demux.video_00 ! queue ! decodebin ! ffmpegcolorspace ! videoscale ! autovideosink
 * </programlisting>
 * Play (parse and decode) a .mov file and try to output it to
 * an automatically detected soundcard and videosink. If the MOV file contains
 * compressed audio or video data, this will only work if you have the
 * right decoder elements/plugins installed.
 * </para>
 * </refsect2>
 *
 * Last reviewed on 2006-12-29 (0.10.5)
 */

48 49 50
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
51 52 53

#include "gst/gst-i18n-plugin.h"

54 55 56
#include "qtdemux_types.h"
#include "qtdemux_dump.h"
#include "qtdemux_fourcc.h"
57
#include "qtdemux.h"
58
#include "qtpalette.h"
59

60
#include <stdlib.h>
Artyom Baginski's avatar
Artyom Baginski committed
61
#include <string.h>
62 63 64 65

#ifdef HAVE_ZLIB
# include <zlib.h>
#endif
66

67
GST_DEBUG_CATEGORY (qtdemux_debug);
68 69 70 71 72 73

#if 0
#define qtdemux_dump_mem(a,b)  gst_util_dump_mem(a,b)
#else
#define qtdemux_dump_mem(a,b)   /* */
#endif
74

75
typedef struct _QtNode QtNode;
76
typedef struct _QtDemuxSegment QtDemuxSegment;
77
typedef struct _QtDemuxSample QtDemuxSample;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78 79 80

struct _QtNode
{
81
  guint32 type;
82
  guint8 *data;
83
  gint len;
84 85
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
86 87
struct _QtDemuxSample
{
88 89
  guint32 chunk;
  guint32 size;
90
  guint64 offset;
91
  GstClockTimeDiff pts_offset;  /* Add this value to timestamp to get the pts */
92
  guint64 timestamp;            /* In GstClockTime */
93
  guint64 duration;             /* in GstClockTime */
94
  gboolean keyframe;            /* TRUE when this packet is a keyframe */
95 96
};

97 98 99 100 101 102 103 104 105 106 107 108
struct _QtDemuxSegment
{
  /* global time and duration, all gst time */
  guint64 time;
  guint64 stop_time;
  guint64 duration;
  /* media time of trak, all gst time */
  guint64 media_start;
  guint64 media_stop;
  gdouble rate;
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
109 110
struct _QtDemuxStream
{
111 112 113
  GstPad *pad;

  /* stream type */
114 115
  guint32 subtype;
  GstCaps *caps;
116
  guint32 fourcc;
117

118 119 120 121 122 123 124 125
  /* duration/scale */
  guint32 duration;             /* in timescale */
  guint32 timescale;

  /* our samples */
  guint32 n_samples;
  QtDemuxSample *samples;
  gboolean all_keyframe;        /* TRUE when all samples are keyframes (no stss) */
126
  guint32 min_duration;         /* duration in timescale of first sample, used for figuring out
127
                                   the framerate, in timescale units */
128

129 130 131 132
  /* if we use chunks or samples */
  gboolean sampled;

  /* video info */
133 134
  gint width;
  gint height;
135 136 137
  /* Numerator/denominator framerate */
  gint fps_n;
  gint fps_d;
138 139
  guint16 bits_per_sample;
  guint16 color_table_id;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
140

141
  /* audio info */
142 143
  gdouble rate;
  gint n_channels;
144 145 146 147
  guint samples_per_packet;
  guint samples_per_frame;
  guint bytes_per_packet;
  guint bytes_per_sample;
148
  guint bytes_per_frame;
149
  guint compression;
150 151 152

  /* when a discontinuity is pending */
  gboolean discont;
153 154 155 156 157 158

  /* current position */
  guint32 segment_index;
  guint32 sample_index;
  guint64 time_position;        /* in gst time */

159 160 161
  /* last GstFlowReturn */
  GstFlowReturn last_ret;

162 163 164 165
  /* quicktime segments */
  guint32 n_segments;
  QtDemuxSegment *segments;
  gboolean segment_pending;
166 167
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
168 169
enum QtDemuxState
{
170 171 172
  QTDEMUX_STATE_INITIAL,        /* Initial state (haven't got the header yet) */
  QTDEMUX_STATE_HEADER,         /* Parsing the header */
  QTDEMUX_STATE_MOVIE,          /* Parsing/Playing the media data */
173
  QTDEMUX_STATE_BUFFER_MDAT     /* Buffering the mdat atom */
174 175
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
176 177
static GNode *qtdemux_tree_get_child_by_type (GNode * node, guint32 fourcc);
static GNode *qtdemux_tree_get_sibling_by_type (GNode * node, guint32 fourcc);
178

179
static const GstElementDetails gst_qtdemux_details =
180 181 182
GST_ELEMENT_DETAILS ("QuickTime demuxer",
    "Codec/Demuxer",
    "Demultiplex a QuickTime file into audio and video streams",
183
    "David Schleef <ds@schleef.org>, Wim Taymans <wim@fluendo.com>");
Artyom Baginski's avatar
Artyom Baginski committed
184

David Schleef's avatar
David Schleef committed
185
static GstStaticPadTemplate gst_qtdemux_sink_template =
186
    GST_STATIC_PAD_TEMPLATE ("sink",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
187
    GST_PAD_SINK,
188
    GST_PAD_ALWAYS,
189
    GST_STATIC_CAPS ("video/quicktime; audio/x-m4a; application/x-3gp")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
190
    );
David Schleef's avatar
David Schleef committed
191 192

static GstStaticPadTemplate gst_qtdemux_videosrc_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193 194 195 196
GST_STATIC_PAD_TEMPLATE ("audio_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);
David Schleef's avatar
David Schleef committed
197 198

static GstStaticPadTemplate gst_qtdemux_audiosrc_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
199 200 201 202
GST_STATIC_PAD_TEMPLATE ("video_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);
Artyom Baginski's avatar
Artyom Baginski committed
203

204
static GstElementClass *parent_class = NULL;
Artyom Baginski's avatar
Artyom Baginski committed
205

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
206 207 208
static void gst_qtdemux_class_init (GstQTDemuxClass * klass);
static void gst_qtdemux_base_init (GstQTDemuxClass * klass);
static void gst_qtdemux_init (GstQTDemux * quicktime_demux);
209
static void gst_qtdemux_dispose (GObject * object);
210

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
211 212
static GstStateChangeReturn gst_qtdemux_change_state (GstElement * element,
    GstStateChange transition);
213 214
static gboolean qtdemux_sink_activate (GstPad * sinkpad);
static gboolean qtdemux_sink_activate_pull (GstPad * sinkpad, gboolean active);
215
static gboolean qtdemux_sink_activate_push (GstPad * sinkpad, gboolean active);
216 217 218

static void gst_qtdemux_loop (GstPad * pad);
static GstFlowReturn gst_qtdemux_chain (GstPad * sinkpad, GstBuffer * inbuf);
219
static gboolean gst_qtdemux_handle_sink_event (GstPad * pad, GstEvent * event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220

221
static gboolean qtdemux_parse_moov (GstQTDemux * qtdemux, guint8 * buffer,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
222
    int length);
223 224 225
static gboolean qtdemux_parse_node (GstQTDemux * qtdemux, GNode * node,
    guint8 * buffer, int length);
static gboolean qtdemux_parse_tree (GstQTDemux * qtdemux);
226

227
static void gst_qtdemux_handle_esds (GstQTDemux * qtdemux,
228
    QtDemuxStream * stream, GNode * esds, GstTagList * list);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
229
static GstCaps *qtdemux_video_caps (GstQTDemux * qtdemux, guint32 fourcc,
230
    const guint8 * stsd_data, const gchar ** codec_name);
231 232 233
static GstCaps *qtdemux_audio_caps (GstQTDemux * qtdemux,
    QtDemuxStream * stream, guint32 fourcc, const guint8 * data, int len,
    const gchar ** codec_name);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
234

235
GType
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236
gst_qtdemux_get_type (void)
Artyom Baginski's avatar
Artyom Baginski committed
237 238 239 240 241
{
  static GType qtdemux_type = 0;

  if (!qtdemux_type) {
    static const GTypeInfo qtdemux_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
242 243 244 245 246
      sizeof (GstQTDemuxClass),
      (GBaseInitFunc) gst_qtdemux_base_init, NULL,
      (GClassInitFunc) gst_qtdemux_class_init,
      NULL, NULL, sizeof (GstQTDemux), 0,
      (GInstanceInitFunc) gst_qtdemux_init,
Artyom Baginski's avatar
Artyom Baginski committed
247
    };
248

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
249
    qtdemux_type =
250 251
        g_type_register_static (GST_TYPE_ELEMENT, "GstQTDemux", &qtdemux_info,
        0);
Artyom Baginski's avatar
Artyom Baginski committed
252 253 254 255
  }
  return qtdemux_type;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
256 257
static void
gst_qtdemux_base_init (GstQTDemuxClass * klass)
258 259 260 261
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
262 263 264 265 266
      gst_static_pad_template_get (&gst_qtdemux_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_qtdemux_videosrc_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_qtdemux_audiosrc_template));
267
  gst_element_class_set_details (element_class, &gst_qtdemux_details);
David Schleef's avatar
David Schleef committed
268

269
  GST_DEBUG_CATEGORY_INIT (qtdemux_debug, "qtdemux", 0, "qtdemux plugin");
270 271
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
272 273
static void
gst_qtdemux_class_init (GstQTDemuxClass * klass)
Artyom Baginski's avatar
Artyom Baginski committed
274 275 276 277
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
278 279
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
Artyom Baginski's avatar
Artyom Baginski committed
280

281
  parent_class = g_type_class_peek_parent (klass);
282

283 284
  gobject_class->dispose = gst_qtdemux_dispose;

Artyom Baginski's avatar
Artyom Baginski committed
285 286 287
  gstelement_class->change_state = gst_qtdemux_change_state;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
288 289
static void
gst_qtdemux_init (GstQTDemux * qtdemux)
Artyom Baginski's avatar
Artyom Baginski committed
290
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
291
  qtdemux->sinkpad =
292
      gst_pad_new_from_static_template (&gst_qtdemux_sink_template, "sink");
293 294 295
  gst_pad_set_activate_function (qtdemux->sinkpad, qtdemux_sink_activate);
  gst_pad_set_activatepull_function (qtdemux->sinkpad,
      qtdemux_sink_activate_pull);
296 297 298 299
  gst_pad_set_activatepush_function (qtdemux->sinkpad,
      qtdemux_sink_activate_push);
  gst_pad_set_chain_function (qtdemux->sinkpad, gst_qtdemux_chain);
  gst_pad_set_event_function (qtdemux->sinkpad, gst_qtdemux_handle_sink_event);
300
  gst_element_add_pad (GST_ELEMENT_CAST (qtdemux), qtdemux->sinkpad);
301

302
  qtdemux->state = QTDEMUX_STATE_INITIAL;
303
  /* FIXME, use segment last_stop for this */
304
  qtdemux->last_ts = GST_CLOCK_TIME_NONE;
305 306 307 308
  qtdemux->pullbased = FALSE;
  qtdemux->neededbytes = 16;
  qtdemux->todrop = 0;
  qtdemux->adapter = gst_adapter_new ();
309 310 311
  qtdemux->offset = 0;
  qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
  qtdemux->mdatbuffer = NULL;
312
  gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
Artyom Baginski's avatar
Artyom Baginski committed
313 314
}

315 316 317 318 319 320 321 322 323
static void
gst_qtdemux_dispose (GObject * object)
{
  GstQTDemux *qtdemux = GST_QTDEMUX (object);

  if (qtdemux->adapter) {
    g_object_unref (G_OBJECT (qtdemux->adapter));
    qtdemux->adapter = NULL;
  }
324 325

  G_OBJECT_CLASS (parent_class)->dispose (object);
326 327
}

328
#if 0
329
static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
330 331
gst_qtdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
    GstFormat * dest_format, gint64 * dest_value)
332 333
{
  gboolean res = TRUE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
334
  QtDemuxStream *stream = gst_pad_get_element_private (pad);
335

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
336
  if (stream->subtype == GST_MAKE_FOURCC ('v', 'i', 'd', 'e') &&
337 338 339 340 341 342
      (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
    return FALSE;

  switch (src_format) {
    case GST_FORMAT_TIME:
      switch (*dest_format) {
343 344 345 346 347 348 349 350 351
        case GST_FORMAT_BYTES:
          *dest_value = src_value * 1;  /* FIXME */
          break;
        case GST_FORMAT_DEFAULT:
          *dest_value = src_value * 1;  /* FIXME */
          break;
        default:
          res = FALSE;
          break;
352 353 354 355
      }
      break;
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
356 357 358 359 360 361
        case GST_FORMAT_TIME:
          *dest_value = src_value * 1;  /* FIXME */
          break;
        default:
          res = FALSE;
          break;
362 363 364 365
      }
      break;
    case GST_FORMAT_DEFAULT:
      switch (*dest_format) {
366 367 368 369 370 371
        case GST_FORMAT_TIME:
          *dest_value = src_value * 1;  /* FIXME */
          break;
        default:
          res = FALSE;
          break;
372 373 374 375 376 377 378 379
      }
      break;
    default:
      res = FALSE;
  }

  return res;
}
380
#endif
381 382

static const GstQueryType *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
383
gst_qtdemux_get_src_query_types (GstPad * pad)
384 385 386
{
  static const GstQueryType src_types[] = {
    GST_QUERY_POSITION,
Wim Taymans's avatar
Wim Taymans committed
387
    GST_QUERY_DURATION,
388
    GST_QUERY_SEEKING,
389 390 391 392 393 394
    0
  };

  return src_types;
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static gboolean
gst_qtdemux_get_duration (GstQTDemux * qtdemux, gint64 * duration)
{
  gboolean res = TRUE;

  *duration = GST_CLOCK_TIME_NONE;

  if (qtdemux->duration != 0) {
    if (qtdemux->duration != G_MAXINT32 && qtdemux->timescale != 0) {
      *duration = gst_util_uint64_scale_int (qtdemux->duration,
          GST_SECOND, qtdemux->timescale);
    }
  }
  return res;
}

411
static gboolean
412
gst_qtdemux_handle_src_query (GstPad * pad, GstQuery * query)
413
{
414 415
  gboolean res = FALSE;
  GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
416

417
  switch (GST_QUERY_TYPE (query)) {
418
    case GST_QUERY_POSITION:
419 420 421
      if (GST_CLOCK_TIME_IS_VALID (qtdemux->segment.last_stop)) {
        gst_query_set_position (query, GST_FORMAT_TIME,
            qtdemux->segment.last_stop);
Wim Taymans's avatar
Wim Taymans committed
422 423 424
        res = TRUE;
      }
      break;
425 426
    case GST_QUERY_DURATION:{
      GstFormat fmt;
427

428 429 430
      gst_query_parse_duration (query, &fmt, NULL);
      if (fmt == GST_FORMAT_TIME) {
        gint64 duration = -1;
431

432 433 434 435 436 437 438
        gst_qtdemux_get_duration (qtdemux, &duration);
        if (duration > 0) {
          gst_query_set_duration (query, GST_FORMAT_TIME, duration);
          res = TRUE;
        }
      }
      break;
439
    }
440 441 442 443 444 445 446 447 448 449 450 451
    case GST_QUERY_SEEKING:{
      GstFormat fmt;

      gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
      if (fmt == GST_FORMAT_TIME) {
        gint64 duration = -1;

        gst_qtdemux_get_duration (qtdemux, &duration);
        gst_query_set_seeking (query, GST_FORMAT_TIME, qtdemux->pullbased,
            0, duration);
        res = TRUE;
      }
452
      break;
453
    }
454
    default:
455
      res = gst_pad_query_default (pad, query);
456 457 458
      break;
  }

459 460
  gst_object_unref (qtdemux);

461 462 463
  return res;
}

464
/* push event on all source pads; takes ownership of the event */
465
static void
466
gst_qtdemux_push_event (GstQTDemux * qtdemux, GstEvent * event)
467 468 469 470 471 472 473
{
  guint n;

  GST_DEBUG_OBJECT (qtdemux, "pushing %s event on all source pads",
      GST_EVENT_TYPE_NAME (event));

  for (n = 0; n < qtdemux->n_streams; n++) {
474 475 476 477
    GstPad *pad;

    if ((pad = qtdemux->streams[n]->pad))
      gst_pad_push_event (pad, gst_event_ref (event));
478 479 480 481
  }
  gst_event_unref (event);
}

482
/* find the index of the sample that includes the data for @media_time
483
 *
484 485
 * Returns the index of the sample or n_samples when the sample was not
 * found.
486
 */
487
/* FIXME, binary search would be nice here */
488 489 490
static guint32
gst_qtdemux_find_index (GstQTDemux * qtdemux, QtDemuxStream * str,
    guint64 media_time)
491
{
492
  guint32 i;
493

494 495
  if (str->n_samples == 0)
    return 0;
496

497 498 499 500
  for (i = 0; i < str->n_samples; i++) {
    if (str->samples[i].timestamp > media_time) {
      /* first sample after media_time, we need the previous one */
      return (i == 0 ? 0 : i - 1);
501
    }
502 503 504
  }
  return str->n_samples - 1;
}
505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/* find the index of the keyframe needed to decode the sample at @index
 * of stream @str.
 *
 * Returns the index of the keyframe.
 */
static guint32
gst_qtdemux_find_keyframe (GstQTDemux * qtdemux, QtDemuxStream * str,
    guint32 index)
{
  if (index >= str->n_samples)
    return str->n_samples;

  /* all keyframes, return index */
  if (str->all_keyframe)
    return index;

  /* else go back until we have a keyframe */
  while (TRUE) {
    if (str->samples[index].keyframe)
      break;

    if (index == 0)
      break;

    index--;
  }
  return index;
}

/* find the segment for @time_position for @stream
 *
 * Returns -1 if the segment cannot be found.
 */
static guint32
gst_qtdemux_find_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
    guint64 time_position)
{
  gint i;
  guint32 seg_idx;

  /* find segment corresponding to time_position if we are looking
   * for a segment. */
  seg_idx = -1;
  for (i = 0; i < stream->n_segments; i++) {
    QtDemuxSegment *segment = &stream->segments[i];

    if (segment->time <= time_position && time_position < segment->stop_time) {
      seg_idx = i;
      break;
555
    }
556
  }
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  return seg_idx;
}

/* move the stream @str to the sample position @index.
 *
 * Updates @str->sample_index and marks discontinuity if needed.
 */
static void
gst_qtdemux_move_stream (GstQTDemux * qtdemux, QtDemuxStream * str,
    guint32 index)
{
  /* no change needed */
  if (index == str->sample_index)
    return;

  GST_DEBUG_OBJECT (qtdemux, "moving to sample %u of %u", index,
      str->n_samples);

  /* position changed, we have a discont */
  str->sample_index = index;
  str->discont = TRUE;
578 579 580 581
}

/* perform the seek.
 *
582 583 584 585 586 587 588 589 590 591 592
 * We set all segment_indexes in the streams to unknown and
 * adjust the time_position to the desired position. this is enough
 * to trigger a segment switch in the streaming thread to start
 * streaming from the desired position.
 *
 * Keyframe seeking is a little more complicated when dealing with
 * segments. Ideally we want to move to the previous keyframe in 
 * the segment but there might not be a keyframe in the segment. In
 * fact, none of the segments could contain a keyframe. We take a
 * practical approach: seek to the previous keyframe in the segment,
 * if there is none, seek to the beginning of the segment.
593 594
 *
 * Called with STREAM_LOCK
595
 */
596
static gboolean
597
gst_qtdemux_perform_seek (GstQTDemux * qtdemux, GstSegment * segment)
598
{
599
  gint64 desired_offset;
600
  gint n;
601

602
  desired_offset = segment->last_stop;
603

604 605
  GST_DEBUG_OBJECT (qtdemux, "seeking to %" GST_TIME_FORMAT,
      GST_TIME_ARGS (desired_offset));
606

607
  if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
608 609 610 611 612 613 614 615
    guint64 min_offset;

    min_offset = desired_offset;

    /* for each stream, find the index of the sample in the segment
     * and move back to the previous keyframe. */
    for (n = 0; n < qtdemux->n_streams; n++) {
      QtDemuxStream *str;
616
      guint32 index, kindex;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
      guint32 seg_idx;
      guint64 media_start;
      guint64 media_time;
      guint64 seg_time;
      QtDemuxSegment *seg;

      str = qtdemux->streams[n];

      seg_idx = gst_qtdemux_find_segment (qtdemux, str, desired_offset);
      GST_DEBUG_OBJECT (qtdemux, "align segment %d", seg_idx);

      /* segment not found, continue with normal flow */
      if (seg_idx == -1)
        continue;

      /* get segment and time in the segment */
      seg = &str->segments[seg_idx];
      seg_time = desired_offset - seg->time;

      /* get the media time in the segment */
      media_start = seg->media_start + seg_time;

      /* get the index of the sample with media time */
      index = gst_qtdemux_find_index (qtdemux, str, media_start);
      GST_DEBUG_OBJECT (qtdemux, "sample for %" GST_TIME_FORMAT " at %u",
          GST_TIME_ARGS (media_start), index);
643

644
      /* find previous keyframe */
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
      kindex = gst_qtdemux_find_keyframe (qtdemux, str, index);

      GST_DEBUG_OBJECT (qtdemux, "keyframe at %u", kindex);

      /* if the keyframe is at a different position, we need to update the
       * requiested seek time */
      if (index != kindex) {
        index = kindex;

        /* get timestamp of keyframe */
        media_time = str->samples[kindex].timestamp;
        GST_DEBUG_OBJECT (qtdemux, "keyframe at %u with time %" GST_TIME_FORMAT,
            kindex, GST_TIME_ARGS (media_time));

        /* keyframes in the segment get a chance to change the
         * desired_offset. keyframes out of the segment are
         * ignored. */
        if (media_time >= seg->media_start) {
          guint64 seg_time;

          /* this keyframe is inside the segment, convert back to
           * segment time */
          seg_time = (media_time - seg->media_start) + seg->time;
          if (seg_time < min_offset)
            min_offset = seg_time;
        }
671 672 673 674
      }
    }
    GST_DEBUG_OBJECT (qtdemux, "keyframe seek, align to %"
        GST_TIME_FORMAT, GST_TIME_ARGS (desired_offset));
675
    desired_offset = min_offset;
676 677 678 679 680
  }

  /* and set all streams to the final position */
  for (n = 0; n < qtdemux->n_streams; n++) {
    QtDemuxStream *stream = qtdemux->streams[n];
681

682 683 684
    stream->time_position = desired_offset;
    stream->sample_index = 0;
    stream->segment_index = -1;
685
    stream->last_ret = GST_FLOW_OK;
686
  }
687 688
  segment->last_stop = desired_offset;
  segment->time = desired_offset;
689

690
  /* we stop at the end */
691 692 693
  if (segment->stop == -1)
    segment->stop = segment->duration;

694 695
  return TRUE;
}
696

697
/* do a seek in pull based mode */
698 699 700 701 702 703 704 705 706 707 708 709
static gboolean
gst_qtdemux_do_seek (GstQTDemux * qtdemux, GstPad * pad, GstEvent * event)
{
  gdouble rate;
  GstFormat format;
  GstSeekFlags flags;
  GstSeekType cur_type, stop_type;
  gint64 cur, stop;
  gboolean flush;
  gboolean res;
  gboolean update;
  GstSegment seeksegment;
710
  int i;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

  if (event) {
    GST_DEBUG_OBJECT (qtdemux, "doing seek with event");

    gst_event_parse_seek (event, &rate, &format, &flags,
        &cur_type, &cur, &stop_type, &stop);

    /* we have to have a format as the segment format. Try to convert
     * if not. */
    if (format != GST_FORMAT_TIME) {
      GstFormat fmt;

      fmt = GST_FORMAT_TIME;
      res = TRUE;
      if (cur_type != GST_SEEK_TYPE_NONE)
        res = gst_pad_query_convert (pad, format, cur, &fmt, &cur);
      if (res && stop_type != GST_SEEK_TYPE_NONE)
        res = gst_pad_query_convert (pad, format, stop, &fmt, &stop);
      if (!res)
        goto no_format;

      format = fmt;
    }
  } else {
    GST_DEBUG_OBJECT (qtdemux, "doing seek without event");
    flags = 0;
  }
738

739
  flush = flags & GST_SEEK_FLAG_FLUSH;
740

741
  GST_DEBUG_OBJECT (qtdemux, "seek format %d", format);
742

743
  /* stop streaming, either by flushing or by pausing the task */
744 745 746 747 748 749
  if (flush) {
    /* unlock upstream pull_range */
    gst_pad_push_event (qtdemux->sinkpad, gst_event_new_flush_start ());
    /* make sure out loop function exits */
    gst_qtdemux_push_event (qtdemux, gst_event_new_flush_start ());
  } else {
750
    /* non flushing seek, pause the task */
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    gst_pad_pause_task (qtdemux->sinkpad);
  }

  /* wait for streaming to finish */
  GST_PAD_STREAM_LOCK (qtdemux->sinkpad);

  /* copy segment, we need this because we still need the old
   * segment when we close the current segment. */
  memcpy (&seeksegment, &qtdemux->segment, sizeof (GstSegment));

  if (event) {
    /* configure the segment with the seek variables */
    GST_DEBUG_OBJECT (qtdemux, "configuring seek");
    gst_segment_set_seek (&seeksegment, rate, format, flags,
        cur_type, cur, stop_type, stop, &update);
  }

768
  /* now do the seek, this actually never returns FALSE */
769 770 771 772 773 774 775 776 777 778 779 780 781
  res = gst_qtdemux_perform_seek (qtdemux, &seeksegment);

  /* prepare for streaming again */
  if (flush) {
    gst_pad_push_event (qtdemux->sinkpad, gst_event_new_flush_stop ());
    gst_qtdemux_push_event (qtdemux, gst_event_new_flush_stop ());
  } else if (qtdemux->segment_running) {
    /* we are running the current segment and doing a non-flushing seek,
     * close the segment first based on the last_stop. */
    GST_DEBUG_OBJECT (qtdemux, "closing running segment %" G_GINT64_FORMAT
        " to %" G_GINT64_FORMAT, qtdemux->segment.start,
        qtdemux->segment.last_stop);

782 783
    /* FIXME, needs to be done from the streaming thread. Also, the rate is the
     * product of the global rate and the (quicktime) segment rate. */
784 785 786 787 788 789 790
    gst_qtdemux_push_event (qtdemux,
        gst_event_new_new_segment (TRUE,
            qtdemux->segment.rate, qtdemux->segment.format,
            qtdemux->segment.start, qtdemux->segment.last_stop,
            qtdemux->segment.time));
  }

791
  /* commit the new segment */
792 793 794 795 796 797 798 799
  memcpy (&qtdemux->segment, &seeksegment, sizeof (GstSegment));

  if (qtdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
    gst_element_post_message (GST_ELEMENT_CAST (qtdemux),
        gst_message_new_segment_start (GST_OBJECT_CAST (qtdemux),
            qtdemux->segment.format, qtdemux->segment.last_stop));
  }

800 801
  /* restart streaming, NEWSEGMENT will be sent from the streaming
   * thread. */
802
  qtdemux->segment_running = TRUE;
803
  for (i = 0; i < qtdemux->n_streams; i++)
804
    qtdemux->streams[i]->last_ret = GST_FLOW_OK;
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  gst_pad_start_task (qtdemux->sinkpad, (GstTaskFunction) gst_qtdemux_loop,
      qtdemux->sinkpad);

  GST_PAD_STREAM_UNLOCK (qtdemux->sinkpad);

  return TRUE;

  /* ERRORS */
no_format:
  {
    GST_DEBUG_OBJECT (qtdemux, "unsupported format given, seek aborted.");
    return FALSE;
  }
}

static gboolean
gst_qtdemux_handle_src_event (GstPad * pad, GstEvent * event)
{
  gboolean res = TRUE;
  GstQTDemux *qtdemux = GST_QTDEMUX (gst_pad_get_parent (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
829 830 831 832 833 834
      if (qtdemux->pullbased) {
        res = gst_qtdemux_do_seek (qtdemux, pad, event);
      } else {
        GST_DEBUG_OBJECT (qtdemux, "cannot seek in streaming mode");
        res = FALSE;
      }
835
      break;
836 837 838 839 840
    default:
      res = FALSE;
      break;
  }

841 842
  gst_object_unref (qtdemux);

843 844 845 846 847
  gst_event_unref (event);

  return res;
}

848
static gboolean
849
gst_qtdemux_handle_sink_event (GstPad * sinkpad, GstEvent * event)
Artyom Baginski's avatar
Artyom Baginski committed
850
{
851
  GstQTDemux *demux = GST_QTDEMUX (GST_PAD_PARENT (sinkpad));
852
  gboolean res;
853 854 855 856

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NEWSEGMENT:
      /* We need to convert it to a GST_FORMAT_TIME new segment */
857 858 859
      gst_event_unref (event);
      res = TRUE;
      break;
Artyom Baginski's avatar
Artyom Baginski committed
860
    default:
861 862
      res = gst_pad_event_default (demux->sinkpad, event);
      break;
Artyom Baginski's avatar
Artyom Baginski committed
863
  }
864

865
  return res;
Artyom Baginski's avatar
Artyom Baginski committed
866 867
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
868 869
static GstStateChangeReturn
gst_qtdemux_change_state (GstElement * element, GstStateChange transition)
870
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
871
  GstQTDemux *qtdemux = GST_QTDEMUX (element);
872 873
  GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;

874 875 876 877 878 879 880
  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      break;
    default:
      break;
  }

881
  result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Artyom Baginski's avatar
Artyom Baginski committed
882

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
883 884
  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:{
885 886
      gint n;

887
      qtdemux->state = QTDEMUX_STATE_INITIAL;
888
      qtdemux->last_ts = GST_CLOCK_TIME_NONE;
889 890 891 892
      qtdemux->neededbytes = 16;
      qtdemux->todrop = 0;
      qtdemux->pullbased = FALSE;
      qtdemux->offset = 0;
893 894 895 896
      qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
      if (qtdemux->mdatbuffer)
        gst_buffer_unref (qtdemux->mdatbuffer);
      qtdemux->mdatbuffer = NULL;
897
      gst_adapter_clear (qtdemux->adapter);
898
      for (n = 0; n < qtdemux->n_streams; n++) {
899 900 901 902 903 904 905 906 907
        QtDemuxStream *stream = qtdemux->streams[n];

        if (stream->pad)
          gst_element_remove_pad (element, stream->pad);
        g_free (stream->samples);
        if (stream->caps)
          gst_caps_unref (stream->caps);
        g_free (stream->segments);
        g_free (stream);
908 909
      }
      qtdemux->n_streams = 0;
910 911
      qtdemux->n_video_streams = 0;
      qtdemux->n_audio_streams = 0;
912
      gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
913
      break;
914
    }
915 916 917
    default:
      break;
  }
Artyom Baginski's avatar
Artyom Baginski committed
918

919
  return result;
920
}
Artyom Baginski's avatar
Artyom Baginski committed
921

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
922
static void
923
extract_initial_length_and_fourcc (guint8 * data, guint64 * plength,
924 925
    guint32 * pfourcc)
{
926
  guint64 length;
927 928
  guint32 fourcc;

929
  length = QT_UINT32 (data);
930
  GST_DEBUG ("length %08" G_GINT64_MODIFIER "x", length);
931
  fourcc = QT_FOURCC (data + 4);
932 933 934 935
  GST_DEBUG ("atom type %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));

  if (length == 0) {
    length = G_MAXUINT32;
936
  } else if (length == 1) {
937 938
    /* this means we have an extended size, which is the 64 bit value of
     * the next 8 bytes */
939
    length = QT_UINT64 (data + 8);
940
    GST_DEBUG ("length %08llx", length);
941 942 943 944 945 946 947 948 949 950
  }

  if (plength)
    *plength = length;
  if (pfourcc)
    *pfourcc = fourcc;
}

static GstFlowReturn
gst_qtdemux_loop_state_header (GstQTDemux * qtdemux)
951
{
952
  guint64 length;
953
  guint32 fourcc;
954
  GstBuffer *buf = NULL;
955 956 957 958 959 960 961 962 963 964
  GstFlowReturn ret = GST_FLOW_OK;
  guint64 cur_offset = qtdemux->offset;

  ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, 16, &buf);
  if (ret != GST_FLOW_OK)
    goto beach;
  extract_initial_length_and_fourcc (GST_BUFFER_DATA (buf), &length, &fourcc);
  gst_buffer_unref (buf);

  switch (fourcc) {
965 966 967 968 969 970 971 972
    case FOURCC_mdat:
    case FOURCC_free:
    case FOURCC_wide:
    case FOURCC_PICT:
    case FOURCC_pnot:
    {
      GST_LOG_OBJECT (qtdemux,
          "skipping atom '%" GST_FOURCC_FORMAT "' at %" G_GUINT64_FORMAT,
973 974 975 976
          GST_FOURCC_ARGS (fourcc), cur_offset);
      cur_offset += length;
      qtdemux->offset += length;
      break;
977 978 979
    }
    case FOURCC_moov:
    {
980 981 982 983 984
      GstBuffer *moov;

      ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, length, &moov);
      if (ret != GST_FLOW_OK)
        goto beach;
985
      if (length != GST_BUFFER_SIZE (moov)) {
986 987 988 989
        GST_ELEMENT_ERROR (qtdemux, STREAM, DECODE,
            (_("This file is incomplete and cannot be played.")),
            ("We got less than expected (received %u, wanted %u)",
                GST_BUFFER_SIZE (moov), (guint) length));
990 991 992
        ret = GST_FLOW_ERROR;
        goto beach;
      }
993 994 995 996
      cur_offset += length;
      qtdemux->offset += length;

      qtdemux_parse_moov (qtdemux, GST_BUFFER_DATA (moov), length);
997 998
      qtdemux_node_dump (qtdemux, qtdemux->moov_node);

999 1000 1001 1002 1003 1004 1005 1006
      qtdemux_parse_tree (qtdemux);
      g_node_destroy (qtdemux->moov_node);
      gst_buffer_unref (moov);
      qtdemux->moov_node = NULL;
      qtdemux->state = QTDEMUX_STATE_MOVIE;
      GST_DEBUG_OBJECT (qtdemux, "switching state to STATE_MOVIE (%d)",
          qtdemux->state);
      break;
1007
    }
1008 1009 1010 1011 1012
    default:
    {
      GST_LOG_OBJECT (qtdemux,
          "unknown %08x '%" GST_FOURCC_FORMAT "' at %" G_GUINT64_FORMAT, fourcc,
          GST_FOURCC_ARGS (fourcc), cur_offset);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
      cur_offset += length;
      qtdemux->offset += length;
      break;
    }
  }

beach:
  return ret;
}

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
/* activate the given segment number @seg_idx of @stream at time @offset.
 * @offset is an absolute global position over all the segments.
 *
 * This will push out a NEWSEGMENT event with the right values and
 * position the stream index to the first decodable sample before 
 * @offset.
 */
static gboolean
gst_qtdemux_activate_segment (GstQTDemux * qtdemux, QtDemuxStream * stream,
    guint32 seg_idx, guint64 offset)
{
  GstEvent *event;
  QtDemuxSegment *segment;
  guint32 index, kf_index;
  guint64 seg_time;
  guint64 start, stop;
1039
  gdouble rate;
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

  /* update the current segment */
  stream->segment_index = seg_idx;

  /* get the segment */
  segment = &stream->segments[seg_idx];

  if (offset < segment->time)
    return FALSE;

  /* get time in this segment */
  seg_time = offset - segment->time;

  if (seg_time >= segment->duration)
    return FALSE;

  /* calc media start/stop */
1057 1058 1059 1060
  if (qtdemux->segment.stop == -1)
    stop = segment->media_stop;
  else
    stop = MIN (segment->media_stop, qtdemux->segment.stop);
1061
  start = MIN (segment->media_start + seg_time, stop);
1062 1063 1064 1065 1066

  GST_DEBUG_OBJECT (qtdemux, "newsegment %d from %" GST_TIME_FORMAT
      " to %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT, seg_idx,
      GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (offset));

1067 1068 1069
  /* combine global rate with that of the segment */
  rate = segment->rate * qtdemux->segment.rate;
  event = gst_event_new_new_segment (FALSE, rate, GST_FORMAT_TIME,
1070 1071
      start, stop, offset);

1072 1073
  if (stream->pad)
    gst_pad_push_event (stream->pad, event);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

  /* and move to the keyframe before the indicated media time of the
   * segment */
  index = gst_qtdemux_find_index (qtdemux, stream, start);

  GST_DEBUG_OBJECT (qtdemux, "moving data pointer to %" GST_TIME_FORMAT
      ", index: %u", GST_TIME_ARGS (start), index);

  /* we're at the right spot */
  if (index == stream->sample_index)
    return TRUE;

  /* find keyframe of the target index */
  kf_index = gst_qtdemux_find_keyframe (qtdemux, stream, index);

  /* if we move forwards, we don't have to go back to the previous
   * keyframe since we already sent that. We can also just jump to
   * the keyframe right before the target index if there is one. */
  if (index > stream->sample_index) {
    /* moving forwards check if we move past a keyframe */
    if (kf_index > stream->sample_index) {
      GST_DEBUG_OBJECT (qtdemux, "moving forwards to keyframe at %u", kf_index);
      gst_qtdemux_move_stream (qtdemux, stream, kf_index);
    } else {
      GST_DEBUG_OBJECT (qtdemux, "moving forwards, keyframe at %u already sent",
          kf_index);
    }
  } else {
    GST_DEBUG_OBJECT (qtdemux, "moving backwards to keyframe at %u", kf_index);
    gst_qtdemux_move_stream (qtdemux, stream, kf_index);
  }

  return TRUE;
}

/* prepare to get the current sample of @stream, getting essential values.
 * 
 * This function will also prepare and send the segment when needed.
 *
 * Return FALSE if the stream is EOS.
 */
static gboolean
gst_qtdemux_prepare_current_sample (GstQTDemux * qtdemux,
    QtDemuxStream * stream, guint64 * offset, guint * size, guint64 * timestamp,
1118
    guint64 * duration, gboolean * keyframe)
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
{
  QtDemuxSample *sample;
  guint64 time_position;
  guint32 seg_idx;

  g_return_val_if_fail (stream != NULL, FALSE);

  time_position = stream->time_position;
  if (time_position == -1)
    goto eos;

  seg_idx = stream->segment_index;
  if (seg_idx == -1) {
    /* find segment corresponding to time_position if we are looking
     * for a segment. */
    seg_idx = gst_qtdemux_find_segment (qtdemux, stream, time_position);

    /* nothing found, we're really eos */
    if (seg_idx == -1)
      goto eos;
  }

  /* different segment, activate it, sample_index will be set. */
  if (stream->segment_index != seg_idx)
    gst_qtdemux_activate_segment (qtdemux, stream, seg_idx, time_position);

1145 1146 1147
  if (stream->sample_index >= stream->n_samples)
    goto eos;

1148 1149 1150
  /* now get the info for the sample we're at */
  sample = &stream->samples[stream->sample_index];

1151
  *timestamp = sample->timestamp + sample->pts_offset;
1152 1153
  *offset = sample->offset;
  *size = sample->size;
1154
  *duration = sample->duration;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  *keyframe = stream->all_keyframe || sample->keyframe;

  return TRUE;

  /* special cases */
eos:
  {
    stream->time_position = -1;
    return FALSE;
  }
}

/* move to the next sample in @stream.
 *
 * Moves to the next segment when needed.
 */
static void
gst_qtdemux_advance_sample (GstQTDemux * qtdemux, QtDemuxStream * stream)
{
  QtDemuxSample *sample;
  QtDemuxSegment *segment;

  /* move to next sample */
  stream->sample_index++;

  /* get current segment */
  segment = &stream->segments[stream->segment_index];

  /* reached the last sample, we need the next segment */
1184
  if (stream->sample_index >= stream->n_samples)
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    goto next_segment;

  /* get next sample */
  sample = &stream->samples[stream->sample_index];

  /* see if we are past the segment */
  if (sample->timestamp >= segment->media_stop)
    goto next_segment;

  if (sample->timestamp >= segment->media_start) {
    /* inside the segment, update time_position, looks very familiar to
     * GStreamer segments, doesn't it? */
    stream->time_position =
        (sample->timestamp - segment->media_start) + segment->time;
  } else {
    /* not yet in segment, time does not yet increment. This means
     * that we are still prerolling keyframes to the decoder so it can
     * decode the first sample of the segment. */
    stream->time_position = segment->time;
  }
  return;

  /* move to the next segment */
next_segment:
  {
    GST_DEBUG_OBJECT (qtdemux, "segment %d ended ", stream->segment_index);

    if (stream->segment_index == stream->n_segments - 1) {
      /* are we at the end of the last segment, we're EOS */
      stream->time_position = -1;
    } else {
      /* else we're only at the end of the current segment */
      stream->time_position = segment->stop_time;
    }
    /* make sure we select a new segment */
    stream->segment_index = -1;
  }
}

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
static GstFlowReturn
gst_qtdemux_combine_flows (GstQTDemux * demux, QtDemuxStream * stream,
    GstFlowReturn ret)
{
  gint i;

  /* store the value */
  stream->last_ret = ret;

  /* any other error that is not-linked can be returned right
   * away */
  if (ret != GST_FLOW_NOT_LINKED)
    goto done;

  /* only return NOT_LINKED if all other pads returned NOT_LINKED */
  for (i = 0; i < demux->n_streams; i++) {
    QtDemuxStream *ostream = demux->streams[i];

    ret = ostream->last_ret;
    /* some other return value (must be SUCCESS but we can return
     * other values as well) */
    if (ret != GST_FLOW_NOT_LINKED)
      goto done;
  }
  /* if we get here, all other pads were unlinked and we return
   * NOT_LINKED then */
done:
  return ret;
}

1254 1255 1256 1257 1258 1259 1260
static GstFlowReturn
gst_qtdemux_loop_state_movie (GstQTDemux * qtdemux)
{
  GstFlowReturn ret = GST_FLOW_OK;
  GstBuffer *buf = NULL;
  QtDemuxStream *stream;
  guint64 min_time;
1261
  guint64 offset;
1262
  guint64 timestamp;
1263
  guint64 duration;
1264 1265 1266
  gboolean keyframe;
  guint size;
  gint index;
1267
  gint i;
1268

1269 1270
  /* Figure out the next stream sample to output, min_time is expressed in
   * global time and runs over the edit list segments. */
1271
  min_time = G_MAXUINT64;
1272
  index = -1;
1273
  for (i = 0; i < qtdemux->n_streams; i++) {
1274
    guint64 position;
1275

1276 1277
    stream = qtdemux->streams[i];
    position = stream->time_position;
1278

1279 1280 1281 1282
    /* position of -1 is EOS */
    if (position != -1 && position < min_time) {
      min_time = position;
      index = i;
1283 1284
    }
  }
1285
  /* all are EOS */
1286 1287 1288 1289 1290 1291
  if (index == -1)
    goto eos;

  /* check for segment end */
  if (qtdemux->segment.stop != -1 && qtdemux->segment.stop < min_time)
    goto eos;
1292 1293 1294

  stream = qtdemux->streams[index];

1295 1296 1297 1298
  /* fetch info for the current sample of this stream */
  if (!gst_qtdemux_prepare_current_sample (qtdemux, stream, &offset, &size,
          &timestamp, &duration, &keyframe))
    goto eos;
1299 1300

  GST_LOG_OBJECT (qtdemux,
1301
      "pushing from stream %d, offset=%" G_GUINT64_FORMAT
1302
      ",size=%d timestamp=%" GST_TIME_FORMAT,
1303
      index, offset, size, GST_TIME_ARGS (timestamp));
1304

1305
  /* hmm, empty sample, skip and move to next sample */
1306
  if (G_UNLIKELY (size <= 0))
1307
    goto next;
1308

1309 1310
  GST_LOG_OBJECT (qtdemux, "reading %d bytes @ %" G_GUINT64_FORMAT, size,
      offset);
1311

1312 1313 1314
  ret = gst_pad_pull_range (qtdemux->sinkpad, offset, size, &buf);
  if (ret != GST_FLOW_OK)
    goto beach;
1315

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
  if (stream->fourcc == FOURCC_rtsp) {
    GstMessage *m;
    gchar *url;

    url = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));

    /* we have RTSP redirect now */
    m = gst_message_new_element (GST_OBJECT_CAST (qtdemux),
        gst_structure_new ("redirect",
            "new-location", G_TYPE_STRING, url, NULL));
    g_free (url);

    gst_element_post_message (GST_ELEMENT_CAST (qtdemux), m);
  }

1331 1332
  qtdemux->last_ts = min_time;
  gst_segment_set_last_stop (&qtdemux->segment, GST_FORMAT_TIME, min_time);
1333

1334 1335 1336
  if (stream->pad) {
    /* we're going to modify the metadata */
    buf = gst_buffer_make_metadata_writable (buf);
1337

1338