qtdemux.c 119 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_STATIC (qtdemux_debug);
68
#define GST_CAT_DEFAULT qtdemux_debug
69

70 71 72 73 74 75

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

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

struct _QtNode
{
83
  guint32 type;
84
  guint8 *data;
85
  gint len;
86 87
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
88 89
struct _QtDemuxSample
{
90 91
  guint32 chunk;
  guint32 size;
92
  guint64 offset;
93
  guint64 timestamp;            /* In GstClockTime */
94
  guint64 duration;             /* in GstClockTime */
95
  gboolean keyframe;            /* TRUE when this packet is a keyframe */
96 97
};

98 99 100 101 102 103 104 105 106 107 108 109
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
110 111
struct _QtDemuxStream
{
112 113 114
  GstPad *pad;

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

119 120 121 122 123 124 125 126
  /* 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) */
127
  guint32 min_duration;         /* duration in timescale of first sample, used for figuring out
128
                                   the framerate, in timescale units */
129

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

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

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

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

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

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

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

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
169 170
enum QtDemuxState
{
171 172 173
  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 */
174
  QTDEMUX_STATE_BUFFER_MDAT     /* Buffering the mdat atom */
175 176
};

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

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

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

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

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

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

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

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

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

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

228
static void gst_qtdemux_handle_esds (GstQTDemux * qtdemux,
229
    QtDemuxStream * stream, GNode * esds, GstTagList * list);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
230
static GstCaps *qtdemux_video_caps (GstQTDemux * qtdemux, guint32 fourcc,
231
    const guint8 * stsd_data, const gchar ** codec_name);
232 233 234
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
235

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

  if (!qtdemux_type) {
    static const GTypeInfo qtdemux_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
243 244 245 246 247
      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
248
    };
249

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

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

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
263 264 265 266 267
      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));
268
  gst_element_class_set_details (element_class, &gst_qtdemux_details);
David Schleef's avatar
David Schleef committed
269

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

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

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

282
  parent_class = g_type_class_peek_parent (klass);
283

284 285
  gobject_class->dispose = gst_qtdemux_dispose;

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

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
289 290
static void
gst_qtdemux_init (GstQTDemux * qtdemux)
Artyom Baginski's avatar
Artyom Baginski committed
291
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
292
  qtdemux->sinkpad =
293
      gst_pad_new_from_static_template (&gst_qtdemux_sink_template, "sink");
294 295 296
  gst_pad_set_activate_function (qtdemux->sinkpad, qtdemux_sink_activate);
  gst_pad_set_activatepull_function (qtdemux->sinkpad,
      qtdemux_sink_activate_pull);
297 298 299 300
  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);
301
  gst_element_add_pad (GST_ELEMENT_CAST (qtdemux), qtdemux->sinkpad);
302

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

316 317 318 319 320 321 322 323 324
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;
  }
325 326

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

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

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

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

  return res;
}
381
#endif
382 383

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

  return src_types;
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
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;
}

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

418
  switch (GST_QUERY_TYPE (query)) {
419
    case GST_QUERY_POSITION:
420 421 422
      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
423 424 425
        res = TRUE;
      }
      break;
426 427
    case GST_QUERY_DURATION:{
      GstFormat fmt;
428

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

433 434 435 436 437 438 439
        gst_qtdemux_get_duration (qtdemux, &duration);
        if (duration > 0) {
          gst_query_set_duration (query, GST_FORMAT_TIME, duration);
          res = TRUE;
        }
      }
      break;
440
    }
441 442 443 444 445 446 447 448 449 450 451 452
    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;
      }
453
      break;
454
    }
455
    default:
456
      res = gst_pad_query_default (pad, query);
457 458 459
      break;
  }

460 461
  gst_object_unref (qtdemux);

462 463 464
  return res;
}

465
/* push event on all source pads; takes ownership of the event */
466
static void
467
gst_qtdemux_push_event (GstQTDemux * qtdemux, GstEvent * event)
468 469 470 471 472 473 474
{
  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++) {
475 476 477 478
    GstPad *pad;

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

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

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

498 499 500 501
  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);
502
    }
503 504 505
  }
  return str->n_samples - 1;
}
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 555
/* 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;
556
    }
557
  }
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  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;
579 580 581 582
}

/* perform the seek.
 *
583 584 585 586 587 588 589 590 591 592 593
 * 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.
594 595
 *
 * Called with STREAM_LOCK
596
 */
597
static gboolean
598
gst_qtdemux_perform_seek (GstQTDemux * qtdemux, GstSegment * segment)
599
{
600
  gint64 desired_offset;
601
  gint n;
602

603
  desired_offset = segment->last_stop;
604

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

608
  if (segment->flags & GST_SEEK_FLAG_KEY_UNIT) {
609 610 611 612 613 614 615 616
    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;
617
      guint32 index, kindex;
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 643
      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);
644

645
      /* find previous keyframe */
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 671
      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;
        }
672 673 674 675
      }
    }
    GST_DEBUG_OBJECT (qtdemux, "keyframe seek, align to %"
        GST_TIME_FORMAT, GST_TIME_ARGS (desired_offset));
676
    desired_offset = min_offset;
677 678 679 680 681
  }

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

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

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

695 696
  return TRUE;
}
697

698
/* do a seek in pull based mode */
699 700 701 702 703 704 705 706 707 708 709 710
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;
711
  int i;
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 738

  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;
  }
739

740
  flush = flags & GST_SEEK_FLAG_FLUSH;
741

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

744
  /* stop streaming, either by flushing or by pausing the task */
745 746 747 748 749 750
  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 {
751
    /* non flushing seek, pause the task */
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    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);
  }

769
  /* now do the seek, this actually never returns FALSE */
770 771 772 773 774 775 776 777 778 779 780 781 782
  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);

783 784
    /* FIXME, needs to be done from the streaming thread. Also, the rate is the
     * product of the global rate and the (quicktime) segment rate. */
785 786 787 788 789 790 791
    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));
  }

792
  /* commit the new segment */
793 794 795 796 797 798 799 800
  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));
  }

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

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  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:
830 831 832 833 834 835
      if (qtdemux->pullbased) {
        res = gst_qtdemux_do_seek (qtdemux, pad, event);
      } else {
        GST_DEBUG_OBJECT (qtdemux, "cannot seek in streaming mode");
        res = FALSE;
      }
836
      break;
837 838 839 840 841
    default:
      res = FALSE;
      break;
  }

842 843
  gst_object_unref (qtdemux);

844 845 846 847 848
  gst_event_unref (event);

  return res;
}

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

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

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

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

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

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

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

888
      qtdemux->state = QTDEMUX_STATE_INITIAL;
889
      qtdemux->last_ts = GST_CLOCK_TIME_NONE;
890 891 892 893
      qtdemux->neededbytes = 16;
      qtdemux->todrop = 0;
      qtdemux->pullbased = FALSE;
      qtdemux->offset = 0;
894 895 896 897
      qtdemux->mdatoffset = GST_CLOCK_TIME_NONE;
      if (qtdemux->mdatbuffer)
        gst_buffer_unref (qtdemux->mdatbuffer);
      qtdemux->mdatbuffer = NULL;
898
      gst_adapter_clear (qtdemux->adapter);
899
      for (n = 0; n < qtdemux->n_streams; n++) {
900 901 902 903 904 905 906 907 908
        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);
909 910
      }
      qtdemux->n_streams = 0;
911 912
      qtdemux->n_video_streams = 0;
      qtdemux->n_audio_streams = 0;
913
      gst_segment_init (&qtdemux->segment, GST_FORMAT_TIME);
914
      break;
915
    }
916 917 918
    default:
      break;
  }
Artyom Baginski's avatar
Artyom Baginski committed
919

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

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

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

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

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

static GstFlowReturn
gst_qtdemux_loop_state_header (GstQTDemux * qtdemux)
952
{
953
  guint64 length;
954
  guint32 fourcc;
955
  GstBuffer *buf = NULL;
956 957 958 959 960 961 962 963 964 965
  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) {
966 967 968 969 970 971 972 973
    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,
974 975 976 977
          GST_FOURCC_ARGS (fourcc), cur_offset);
      cur_offset += length;
      qtdemux->offset += length;
      break;
978 979 980
    }
    case FOURCC_moov:
    {
981 982 983 984 985
      GstBuffer *moov;

      ret = gst_pad_pull_range (qtdemux->sinkpad, cur_offset, length, &moov);
      if (ret != GST_FLOW_OK)
        goto beach;
986
      if (length != GST_BUFFER_SIZE (moov)) {
987 988 989 990
        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));
991 992 993
        ret = GST_FLOW_ERROR;
        goto beach;
      }
994 995 996 997
      cur_offset += length;
      qtdemux->offset += length;

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

1000 1001 1002 1003 1004 1005 1006 1007
      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;
1008
    }
1009 1010 1011 1012 1013
    default:
    {
      GST_LOG_OBJECT (qtdemux,
          "unknown %08x '%" GST_FOURCC_FORMAT "' at %" G_GUINT64_FORMAT, fourcc,
          GST_FOURCC_ARGS (fourcc), cur_offset);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
      cur_offset += length;
      qtdemux->offset += length;
      break;
    }
  }

beach:
  return ret;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
/* 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;
1040
  gdouble rate;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  /* 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 */
1058 1059 1060 1061
  if (qtdemux->segment.stop == -1)
    stop = segment->media_stop;
  else
    stop = MIN (segment->media_stop, qtdemux->segment.stop);
1062
  start = MIN (segment->media_start + seg_time, stop);
1063 1064 1065 1066 1067

  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));

1068 1069 1070
  /* 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,
1071 1072
      start, stop, offset);

1073 1074
  if (stream->pad)
    gst_pad_push_event (stream->pad, event);
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 1118

  /* 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,
1119
    guint64 * duration, gboolean * keyframe)
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 1145
{
  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);

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

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

  *offset = sample->offset;
  *size = sample->size;
Wim Taymans's avatar