gstdvdemux.c 53.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *               <2005> Wim Taymans <wim@fluendo.com>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <math.h>

27
#include <gst/audio/audio.h>
28
29
#include "gstdvdemux.h"

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * SECTION:element-dvdemux
 *
 * <refsect2>
 * <para>
 * dvdemux splits raw DV into its audio and video components. The audio will be
 * decoded raw samples and the video will be encoded DV video.
 * </para>
 * <para>
 * This element can operate in both push and pull mode depending on the capabilities
 * of the upstream peer.
 * </para>
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
 * gst-launch filesrc location=test.dv ! dvdemux name=demux ! queue ! audioconvert ! alsasink demux. ! queue ! dvdec ! xvimagesink
 * </programlisting>
 * This pipeline decodes and renders the raw DV stream to an audio and a videosink.
 * </para>
 * Last reviewed on 2006-02-27 (0.10.3)
 * </refsect2>
 */

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* DV output has two modes, normal and wide. The resolution is the same in both
 * cases: 720 pixels wide by 576 pixels tall in PAL format, and 720x480 for
 * NTSC.
 *
 * Each of the modes has its own pixel aspect ratio, which is fixed in practice
 * by ITU-R BT.601 (also known as "CCIR-601" or "Rec.601"). Or so claims a
 * reference that I culled from the reliable "internet",
 * http://www.mir.com/DMG/aspect.html. Normal PAL is 59/54 and normal NTSC is
 * 10/11. Because the pixel resolution is the same for both cases, we can get
 * the pixel aspect ratio for wide recordings by multiplying by the ratio of
 * display aspect ratios, 16/9 (for wide) divided by 4/3 (for normal):
 *
 * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
 * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
 *
 * However, the pixel resolution coming out of a DV source does not combine with
 * the standard pixel aspect ratios to give a proper display aspect ratio. An
 * image 480 pixels tall, with a 4:3 display aspect ratio, will be 768 pixels
 * wide. But, if we take the normal PAL aspect ratio of 59/54, and multiply it
 * with the width of the DV image (720 pixels), we get 786.666..., which is
 * nonintegral and too wide. The camera is not outputting a 4:3 image.
 * 
 * If the video sink for this stream has fixed dimensions (such as for
 * fullscreen playback, or for a java applet in a web page), you then have two
 * choices. Either you show the whole image, but pad the image with black
 * borders on the top and bottom (like watching a widescreen video on a 4:3
 * device), or you crop the video to the proper ratio. Apparently the latter is
 * the standard practice.
 *
 * For its part, GStreamer is concerned with accuracy and preservation of
 * information. This element outputs the 720x576 or 720x480 video that it
 * recieves, noting the proper aspect ratio. This should not be a problem for
 * windowed applications, which can change size to fit the video. Applications
 * with fixed size requirements should decide whether to crop or pad which
 * an element such as videobox can do.
 */

#define NTSC_HEIGHT 480
#define NTSC_BUFFER 120000
92
93
#define NTSC_FRAMERATE_NUMERATOR 30000
#define NTSC_FRAMERATE_DENOMINATOR 1001
94
95
96

#define PAL_HEIGHT 576
#define PAL_BUFFER 144000
97
98
#define PAL_FRAMERATE_NUMERATOR 25
#define PAL_FRAMERATE_DENOMINATOR 1
99

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
100
101
102
103
#define PAL_NORMAL_PAR_X        59
#define PAL_NORMAL_PAR_Y        54
#define PAL_WIDE_PAR_X          118
#define PAL_WIDE_PAR_Y          81
104

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
105
106
107
108
#define NTSC_NORMAL_PAR_X       10
#define NTSC_NORMAL_PAR_Y       11
#define NTSC_WIDE_PAR_X         40
#define NTSC_WIDE_PAR_Y         33
109

110
GST_DEBUG_CATEGORY_STATIC (dvdemux_debug);
111
112
#define GST_CAT_DEFAULT dvdemux_debug

113
static const GstElementDetails dvdemux_details =
114
115
GST_ELEMENT_DETAILS ("DV system stream demuxer",
    "Codec/Demuxer",
116
    "Uses libdv to separate DV audio from DV video (libdv.sourceforge.net)",
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");

static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
    );

static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
    );

static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("audio/x-raw-int, "
        "depth = (int) 16, "
        "width = (int) 16, "
        "signed = (boolean) TRUE, "
        "channels = (int) {2, 4}, "
        "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
        "rate = (int) { 32000, 44100, 48000 }")
    );


GST_BOILERPLATE (GstDVDemux, gst_dvdemux, GstElement, GST_TYPE_ELEMENT);

146
static void gst_dvdemux_finalize (GObject * object);
147

148
/* query functions */
149
150
151
152
153
static const GstQueryType *gst_dvdemux_get_src_query_types (GstPad * pad);
static gboolean gst_dvdemux_src_query (GstPad * pad, GstQuery * query);
static const GstQueryType *gst_dvdemux_get_sink_query_types (GstPad * pad);
static gboolean gst_dvdemux_sink_query (GstPad * pad, GstQuery * query);

154
155
156
157
158
159
160
/* convert functions */
static gboolean gst_dvdemux_sink_convert (GstDVDemux * demux,
    GstFormat src_format, gint64 src_value, GstFormat * dest_format,
    gint64 * dest_value);
static gboolean gst_dvdemux_src_convert (GstDVDemux * demux, GstPad * pad,
    GstFormat src_format, gint64 src_value, GstFormat * dest_format,
    gint64 * dest_value);
161

162
/* event functions */
163
static gboolean gst_dvdemux_send_event (GstElement * element, GstEvent * event);
164
static gboolean gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event);
165
166
167
168
static gboolean gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event);

/* scheduling functions */
static void gst_dvdemux_loop (GstPad * pad);
169
170
171
static GstFlowReturn gst_dvdemux_flush (GstDVDemux * dvdemux);
static GstFlowReturn gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer);

172
173
174
175
176
177
/* state change functions */
static gboolean gst_dvdemux_sink_activate (GstPad * sinkpad);
static gboolean gst_dvdemux_sink_activate_push (GstPad * sinkpad,
    gboolean active);
static gboolean gst_dvdemux_sink_activate_pull (GstPad * sinkpad,
    gboolean active);
178
179
static GstStateChangeReturn gst_dvdemux_change_state (GstElement * element,
    GstStateChange transition);
180
181
182
183
184
185
186
187
188
189
190
191
192
193

static void
gst_dvdemux_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_temp));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&video_src_temp));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&audio_src_temp));

  gst_element_class_set_details (element_class, &dvdemux_details);
194
195

  GST_DEBUG_CATEGORY_INIT (dvdemux_debug, "dvdemux", 0, "DV demuxer element");
196
197
198
199
200
}

static void
gst_dvdemux_class_init (GstDVDemuxClass * klass)
{
201
  GObjectClass *gobject_class;
202
203
  GstElementClass *gstelement_class;

204
  gobject_class = (GObjectClass *) klass;
205
206
  gstelement_class = (GstElementClass *) klass;

207
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_dvdemux_finalize);
208

209
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_dvdemux_change_state);
210
  gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_dvdemux_send_event);
211
212
213
214
215
216

  /* table initialization, only do once */
  dv_init (0, 0);
}

static void
217
gst_dvdemux_init (GstDVDemux * dvdemux, GstDVDemuxClass * g_class)
218
219
220
{
  gint i;

221
  dvdemux->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  /* we can operate in pull and push mode so we install
   * a custom activate function */
  gst_pad_set_activate_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_sink_activate));
  /* the function to activate in push mode */
  gst_pad_set_activatepush_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_sink_activate_push));
  /* the function to activate in pull mode */
  gst_pad_set_activatepull_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_sink_activate_pull));
  /* for push mode, this is the chain function */
  gst_pad_set_chain_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_chain));
  /* handling events (in push mode only) */
  gst_pad_set_event_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_handle_sink_event));
  /* query functions */
239
240
241
242
  gst_pad_set_query_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_sink_query));
  gst_pad_set_query_type_function (dvdemux->sinkpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_get_sink_query_types));
243
244

  /* now add the pad */
245
246
247
248
  gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->sinkpad);

  dvdemux->adapter = gst_adapter_new ();

249
250
  /* we need 4 temp buffers for audio decoding which are of a static
   * size and which we can allocate here */
251
252
253
254
255
256
  for (i = 0; i < 4; i++) {
    dvdemux->audio_buffers[i] =
        (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
  }
}

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static void
gst_dvdemux_finalize (GObject * object)
{
  GstDVDemux *dvdemux;
  gint i;

  dvdemux = GST_DVDEMUX (object);

  g_object_unref (dvdemux->adapter);

  /* clean up temp audio buffers */
  for (i = 0; i < 4; i++) {
    g_free (dvdemux->audio_buffers[i]);
  }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/* reset to default values before starting streaming */
static void
gst_dvdemux_reset (GstDVDemux * dvdemux)
{
  dvdemux->frame_offset = 0;
  dvdemux->audio_offset = 0;
  dvdemux->video_offset = 0;
  dvdemux->framecount = 0;
283
  gst_atomic_int_set (&dvdemux->found_header, 0);
284
285
286
287
288
289
290
291
292
293
294
295
296
  dvdemux->frame_len = -1;
  dvdemux->need_segment = FALSE;
  dvdemux->new_media = FALSE;
  dvdemux->framerate_numerator = 0;
  dvdemux->framerate_denominator = 0;
  dvdemux->height = 0;
  dvdemux->frequency = 0;
  dvdemux->channels = 0;
  dvdemux->wide = FALSE;
  gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
  gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
}

297
298
299
300
static void
gst_dvdemux_add_pads (GstDVDemux * dvdemux)
{
  dvdemux->videosrcpad =
301
      gst_pad_new_from_static_template (&video_src_temp, "video");
302
303
304
305
306
307
308
  gst_pad_set_query_function (dvdemux->videosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
  gst_pad_set_query_type_function (dvdemux->videosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
  gst_pad_set_event_function (dvdemux->videosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
  gst_pad_use_fixed_caps (dvdemux->videosrcpad);
Wim Taymans's avatar
Wim Taymans committed
309
  gst_pad_set_active (dvdemux->videosrcpad, TRUE);
310
311
312
  gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->videosrcpad);

  dvdemux->audiosrcpad =
313
      gst_pad_new_from_static_template (&audio_src_temp, "audio");
314
315
316
317
318
319
320
  gst_pad_set_query_function (dvdemux->audiosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
  gst_pad_set_query_type_function (dvdemux->audiosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
  gst_pad_set_event_function (dvdemux->audiosrcpad,
      GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
  gst_pad_use_fixed_caps (dvdemux->audiosrcpad);
Wim Taymans's avatar
Wim Taymans committed
321
  gst_pad_set_active (dvdemux->audiosrcpad, TRUE);
322
  gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->audiosrcpad);
323
324

  gst_element_no_more_pads (GST_ELEMENT (dvdemux));
325
326
327
}

static gboolean
328
329
330
gst_dvdemux_src_convert (GstDVDemux * dvdemux, GstPad * pad,
    GstFormat src_format, gint64 src_value, GstFormat * dest_format,
    gint64 * dest_value)
331
332
333
{
  gboolean res = TRUE;

334
335
336
337
338
  if (*dest_format == src_format || src_value == -1) {
    *dest_value = src_value;
    goto done;
  }

339
  if (dvdemux->frame_len <= 0)
340
341
342
343
344
    goto error;

  if (dvdemux->decoder == NULL)
    goto error;

345
346
  GST_INFO_OBJECT (pad, "src_value:%lld, src_format:%d, dest_format:%d",
      src_value, src_format, *dest_format);
347

348
349
350
351
  switch (src_format) {
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
        case GST_FORMAT_DEFAULT:
352
353
354
          if (pad == dvdemux->videosrcpad)
            *dest_value = src_value / dvdemux->frame_len;
          else if (pad == dvdemux->audiosrcpad)
355
            *dest_value = src_value / (2 * dvdemux->channels);
356
          break;
357
358
359
        case GST_FORMAT_TIME:
          *dest_format = GST_FORMAT_TIME;
          if (pad == dvdemux->videosrcpad)
360
361
362
            *dest_value = gst_util_uint64_scale (src_value,
                GST_SECOND * dvdemux->framerate_denominator,
                dvdemux->frame_len * dvdemux->framerate_numerator);
363
          else if (pad == dvdemux->audiosrcpad)
364
365
            *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
                2 * dvdemux->frequency * dvdemux->channels);
366
367
368
369
370
371
372
373
374
          break;
        default:
          res = FALSE;
      }
      break;
    case GST_FORMAT_TIME:
      switch (*dest_format) {
        case GST_FORMAT_BYTES:
          if (pad == dvdemux->videosrcpad)
375
376
377
            *dest_value = gst_util_uint64_scale (src_value,
                dvdemux->frame_len * dvdemux->framerate_numerator,
                dvdemux->framerate_denominator * GST_SECOND);
378
          else if (pad == dvdemux->audiosrcpad)
379
380
            *dest_value = gst_util_uint64_scale_int (src_value,
                2 * dvdemux->frequency * dvdemux->channels, GST_SECOND);
381
382
          break;
        case GST_FORMAT_DEFAULT:
383
384
          if (pad == dvdemux->videosrcpad) {
            if (src_value)
385
386
387
              *dest_value = gst_util_uint64_scale (src_value,
                  dvdemux->framerate_numerator,
                  dvdemux->framerate_denominator * GST_SECOND);
388
389
            else
              *dest_value = 0;
390
          } else if (pad == dvdemux->audiosrcpad) {
391
            *dest_value = gst_util_uint64_scale (src_value,
392
                dvdemux->frequency, GST_SECOND);
393
          }
394
395
396
397
398
399
400
401
402
          break;
        default:
          res = FALSE;
      }
      break;
    case GST_FORMAT_DEFAULT:
      switch (*dest_format) {
        case GST_FORMAT_TIME:
          if (pad == dvdemux->videosrcpad) {
403
404
405
            *dest_value = gst_util_uint64_scale (src_value,
                GST_SECOND * dvdemux->framerate_denominator,
                dvdemux->framerate_numerator);
406
407
          } else if (pad == dvdemux->audiosrcpad) {
            if (src_value)
408
              *dest_value = gst_util_uint64_scale (src_value,
409
                  GST_SECOND, dvdemux->frequency);
410
411
412
413
414
415
416
417
            else
              *dest_value = 0;
          }
          break;
        case GST_FORMAT_BYTES:
          if (pad == dvdemux->videosrcpad) {
            *dest_value = src_value * dvdemux->frame_len;
          } else if (pad == dvdemux->audiosrcpad) {
418
            *dest_value = src_value * 2 * dvdemux->channels;
419
          }
420
421
422
423
424
425
426
427
428
429
          break;
        default:
          res = FALSE;
      }
      break;
    default:
      res = FALSE;
  }

done:
430
  GST_INFO_OBJECT (pad, "Result : dest_format:%d, dest_value:%lld, res:%d",
431
      *dest_format, *dest_value, res);
432
433
  return res;

434
  /* ERRORS */
435
436
error:
  {
437
    GST_INFO ("source conversion failed");
438
439
440
441
442
    return FALSE;
  }
}

static gboolean
443
444
gst_dvdemux_sink_convert (GstDVDemux * dvdemux, GstFormat src_format,
    gint64 src_value, GstFormat * dest_format, gint64 * dest_value)
445
446
447
{
  gboolean res = TRUE;

448
449
450
  GST_DEBUG_OBJECT (dvdemux, "%d -> %d", src_format, *dest_format);
  GST_INFO_OBJECT (dvdemux, "src_value:%lld, src_format:%d, dest_format:%d",
      src_value, src_format, *dest_format);
451
452
453
454

  if (*dest_format == GST_FORMAT_DEFAULT)
    *dest_format = GST_FORMAT_TIME;

455
  if (*dest_format == src_format || src_value == -1) {
456
457
458
459
    *dest_value = src_value;
    goto done;
  }

460
461
462
  if (dvdemux->frame_len <= 0)
    goto error;

463
464
465
466
467
468
469
  switch (src_format) {
    case GST_FORMAT_BYTES:
      switch (*dest_format) {
        case GST_FORMAT_TIME:
        {
          guint64 frame;

470
471
          /* get frame number, rounds down so don't combine this
           * line and the next line. */
472
473
          frame = src_value / dvdemux->frame_len;

474
475
476
          *dest_value = gst_util_uint64_scale (frame,
              GST_SECOND * dvdemux->framerate_denominator,
              dvdemux->framerate_numerator);
477
478
479
480
481
482
483
484
485
486
487
488
489
          break;
        }
        default:
          res = FALSE;
      }
      break;
    case GST_FORMAT_TIME:
      switch (*dest_format) {
        case GST_FORMAT_BYTES:
        {
          guint64 frame;

          /* calculate the frame */
490
491
492
493
          frame =
              gst_util_uint64_scale (src_value, dvdemux->framerate_numerator,
              dvdemux->framerate_denominator * GST_SECOND);

494
          /* calculate the offset from the rounded frame */
495
496
497
498
499
500
501
502
503
504
          *dest_value = frame * dvdemux->frame_len;
          break;
        }
        default:
          res = FALSE;
      }
      break;
    default:
      res = FALSE;
  }
505
  GST_INFO_OBJECT (dvdemux, "Result : dest_format:%d, dest_value:%lld, res:%d",
506
      *dest_format, *dest_value, res);
507
508
509
510
511
512

done:
  return res;

error:
  {
513
    GST_INFO_OBJECT (dvdemux, "sink conversion failed");
514
515
516
517
518
519
520
521
522
    return FALSE;
  }
}

static const GstQueryType *
gst_dvdemux_get_src_query_types (GstPad * pad)
{
  static const GstQueryType src_query_types[] = {
    GST_QUERY_POSITION,
Wim Taymans's avatar
Wim Taymans committed
523
    GST_QUERY_DURATION,
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
    GST_QUERY_CONVERT,
    0
  };

  return src_query_types;
}

static gboolean
gst_dvdemux_src_query (GstPad * pad, GstQuery * query)
{
  gboolean res = TRUE;
  GstDVDemux *dvdemux;

  dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_POSITION:
Wim Taymans's avatar
Wim Taymans committed
541
542
543
544
545
546
547
548
    {
      GstFormat format;
      gint64 cur;

      /* get target format */
      gst_query_parse_position (query, &format, NULL);

      /* bring the position to the requested format. */
549
550
551
      if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
                  GST_FORMAT_TIME, dvdemux->time_segment.last_stop,
                  &format, &cur)))
Wim Taymans's avatar
Wim Taymans committed
552
553
554
555
556
        goto error;
      gst_query_set_position (query, format, cur);
      break;
    }
    case GST_QUERY_DURATION:
557
558
    {
      GstFormat format;
559
      GstFormat format2;
Wim Taymans's avatar
Wim Taymans committed
560
      gint64 end;
561
562
563
      GstPad *peer;

      /* get target format */
Wim Taymans's avatar
Wim Taymans committed
564
      gst_query_parse_duration (query, &format, NULL);
565

Wim Taymans's avatar
Wim Taymans committed
566
567
      /* change query to bytes to perform on peer */
      gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
568
569
570
571
572
573
574
575
576

      if ((peer = gst_pad_get_peer (dvdemux->sinkpad))) {
        /* ask peer for total length */
        if (!(res = gst_pad_query (peer, query))) {
          gst_object_unref (peer);
          goto error;
        }

        /* get peer total length */
Wim Taymans's avatar
Wim Taymans committed
577
578
579
        gst_query_parse_duration (query, NULL, &end);

        gst_object_unref (peer);
580
581
582

        /* convert end to requested format */
        if (end != -1) {
583
          format2 = format;
584
          if (!(res = gst_dvdemux_sink_convert (dvdemux,
585
                      GST_FORMAT_BYTES, end, &format2, &end))) {
586
587
588
589
590
591
            goto error;
          }
        }
      } else {
        end = -1;
      }
Wim Taymans's avatar
Wim Taymans committed
592
      gst_query_set_duration (query, format, end);
593
594
595
596
597
598
599
600
601
      break;
    }
    case GST_QUERY_CONVERT:
    {
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;

      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
      if (!(res =
602
603
              gst_dvdemux_src_convert (dvdemux, pad, src_fmt, src_val,
                  &dest_fmt, &dest_val)))
604
605
606
607
608
        goto error;
      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
    default:
609
      res = gst_pad_query_default (pad, query);
610
611
612
613
614
615
      break;
  }
  gst_object_unref (dvdemux);

  return res;

616
  /* ERRORS */
617
618
619
error:
  {
    gst_object_unref (dvdemux);
620
    GST_DEBUG ("error source query");
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
    return FALSE;
  }
}

static const GstQueryType *
gst_dvdemux_get_sink_query_types (GstPad * pad)
{
  static const GstQueryType sink_query_types[] = {
    GST_QUERY_CONVERT,
    0
  };

  return sink_query_types;
}

static gboolean
gst_dvdemux_sink_query (GstPad * pad, GstQuery * query)
{
  gboolean res = TRUE;
  GstDVDemux *dvdemux;

  dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CONVERT:
    {
      GstFormat src_fmt, dest_fmt;
      gint64 src_val, dest_val;

      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
      if (!(res =
652
              gst_dvdemux_sink_convert (dvdemux, src_fmt, src_val, &dest_fmt,
653
654
655
656
657
658
                  &dest_val)))
        goto error;
      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
    default:
659
      res = gst_pad_query_default (pad, query);
660
661
662
663
664
665
      break;
  }
  gst_object_unref (dvdemux);

  return res;

666
  /* ERRORS */
667
668
669
error:
  {
    gst_object_unref (dvdemux);
670
    GST_DEBUG ("error handling sink query");
671
672
673
674
    return FALSE;
  }
}

675
/* takes ownership of the event */
676
static gboolean
677
gst_dvdemux_push_event (GstDVDemux * dvdemux, GstEvent * event)
678
679
680
{
  gboolean res = FALSE;

681
682
  if (dvdemux->videosrcpad) {
    gst_event_ref (event);
683
    res |= gst_pad_push_event (dvdemux->videosrcpad, event);
684
685
  }

686
687
  if (dvdemux->audiosrcpad)
    res |= gst_pad_push_event (dvdemux->audiosrcpad, event);
688
689
  else
    gst_event_unref (event);
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

  return res;
}

static gboolean
gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event)
{
  GstDVDemux *dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
  gboolean res = TRUE;

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_FLUSH_START:
      /* we are not blocking on anything exect the push() calls
       * to the peer which will be unblocked by forwarding the
       * event.*/
705
      res = gst_dvdemux_push_event (dvdemux, event);
706
707
708
709
      break;
    case GST_EVENT_FLUSH_STOP:
      gst_adapter_clear (dvdemux->adapter);
      GST_DEBUG ("cleared adapter");
710
711
      gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
      gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
712
      res = gst_dvdemux_push_event (dvdemux, event);
713
714
715
      break;
    case GST_EVENT_NEWSEGMENT:
    {
716
717
      gboolean update;
      gdouble rate;
718
      GstFormat format;
719
      gint64 start, stop, time;
720
721

      /* parse byte start and stop positions */
722
723
724
725
726
727
728
729
      gst_event_parse_new_segment (event, &update, &rate, &format,
          &start, &stop, &time);

      switch (format) {
        case GST_FORMAT_BYTES:
          gst_segment_set_newsegment (&dvdemux->byte_segment, update,
              rate, format, start, stop, time);

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
          /* the update can always be sent */
          if (update) {
            GstEvent *update;

            update = gst_event_new_new_segment (TRUE,
                dvdemux->time_segment.rate, dvdemux->time_segment.format,
                dvdemux->time_segment.start, dvdemux->time_segment.last_stop,
                dvdemux->time_segment.time);

            gst_dvdemux_push_event (dvdemux, update);
          } else {
            /* and queue a SEGMENT before sending the next set of buffers, we
             * cannot convert to time yet as we might not know the size of the
             * frames, etc.. */
            dvdemux->need_segment = TRUE;
          }
746
747
748
749
750
          gst_event_unref (event);
          break;
        case GST_FORMAT_TIME:
          gst_segment_set_newsegment (&dvdemux->time_segment, update,
              rate, format, start, stop, time);
751

752
          /* and we can just forward this time event */
753
          res = gst_dvdemux_push_event (dvdemux, event);
754
755
756
757
758
759
760
          break;
        default:
          gst_event_unref (event);
          /* cannot accept this format */
          res = FALSE;
          break;
      }
761
762
763
      break;
    }
    case GST_EVENT_EOS:
764
      /* flush any pending data, should be nothing left. */
765
766
      gst_dvdemux_flush (dvdemux);
      /* forward event */
767
      res = gst_dvdemux_push_event (dvdemux, event);
768
769
770
      /* and clear the adapter */
      gst_adapter_clear (dvdemux->adapter);
      break;
771
    default:
772
      res = gst_dvdemux_push_event (dvdemux, event);
773
      break;
774
775
776
777
778
779
780
  }

  gst_object_unref (dvdemux);

  return res;
}

781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/* convert a pair of values on the given srcpad */
static gboolean
gst_dvdemux_convert_src_pair (GstDVDemux * dvdemux, GstPad * pad,
    GstFormat src_format, gint64 src_start, gint64 src_stop,
    GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
{
  gboolean res;

  GST_INFO ("starting conversion of start");
  /* bring the format to time on srcpad. */
  if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
              src_format, src_start, &dst_format, dst_start))) {
    goto done;
  }
  GST_INFO ("Finished conversion of start: %lld", *dst_start);

  GST_INFO ("starting conversion of stop");
  /* bring the format to time on srcpad. */
  if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
              src_format, src_stop, &dst_format, dst_stop))) {
    /* could not convert seek format to time offset */
    goto done;
  }
  GST_INFO ("Finished conversion of stop: %lld", *dst_stop);
done:
  return res;
}

/* convert a pair of values on the sinkpad */
static gboolean
gst_dvdemux_convert_sink_pair (GstDVDemux * dvdemux,
    GstFormat src_format, gint64 src_start, gint64 src_stop,
    GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
{
  gboolean res;

  GST_INFO ("starting conversion of start");
  /* bring the format to time on srcpad. */
  if (!(res = gst_dvdemux_sink_convert (dvdemux,
              src_format, src_start, &dst_format, dst_start))) {
    goto done;
  }
  GST_INFO ("Finished conversion of start: %lld", *dst_start);

  GST_INFO ("starting conversion of stop");
  /* bring the format to time on srcpad. */
  if (!(res = gst_dvdemux_sink_convert (dvdemux,
              src_format, src_stop, &dst_format, dst_stop))) {
    /* could not convert seek format to time offset */
    goto done;
  }
  GST_INFO ("Finished conversion of stop: %lld", *dst_stop);
done:
  return res;
}

/* convert a pair of values on the srcpad to a pair of
 * values on the sinkpad 
 */
static gboolean
gst_dvdemux_convert_src_to_sink (GstDVDemux * dvdemux, GstPad * pad,
    GstFormat src_format, gint64 src_start, gint64 src_stop,
    GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
{
  GstFormat conv;
  gboolean res;

  conv = GST_FORMAT_TIME;
  /* convert to TIME intermediate format */
  if (!(res = gst_dvdemux_convert_src_pair (dvdemux, pad,
              src_format, src_start, src_stop, conv, dst_start, dst_stop))) {
    /* could not convert format to time offset */
    goto done;
  }
  /* convert to dst format on sinkpad */
  if (!(res = gst_dvdemux_convert_sink_pair (dvdemux,
              conv, *dst_start, *dst_stop, dst_format, dst_start, dst_stop))) {
    /* could not convert format to time offset */
    goto done;
  }
done:
  return res;
}

865
866
867
868
869
870
871
872
873
874
875
876
877
#if 0
static gboolean
gst_dvdemux_convert_segment (GstDVDemux * dvdemux, GstSegment * src,
    GstSegment * dest)
{
  dest->rate = src->rate;
  dest->abs_rate = src->abs_rate;
  dest->flags = src->flags;

  return TRUE;
}
#endif

878
879
880
881
882
883
884
/* handle seek in push base mode.
 *
 * Convert the time seek to a bytes seek and send it
 * upstream
 *
 * FIXME, upstream might be able to perform time based
 * seek too.
885
886
 *
 * Does not take ownership of the event.
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
 */
static gboolean
gst_dvdemux_handle_push_seek (GstDVDemux * dvdemux, GstPad * pad,
    GstEvent * event)
{
  gboolean res;
  gdouble rate;
  GstSeekFlags flags;
  GstFormat format;
  GstSeekType cur_type, stop_type;
  gint64 cur, stop;
  gint64 start_position, end_position;
  GstEvent *newevent;

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

  /* we convert the start/stop on the srcpad to the byte format
   * on the sinkpad and forward the event */
  res = gst_dvdemux_convert_src_to_sink (dvdemux, pad,
      format, cur, stop, GST_FORMAT_BYTES, &start_position, &end_position);
  if (!res)
    goto done;

  /* now this is the updated seek event on bytes */
  newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
      cur_type, start_position, stop_type, end_position);

  res = gst_pad_push_event (dvdemux->sinkpad, newevent);

done:
  return res;
}

/* position ourselves to the configured segment, used in pull mode.
 * The input segment is in TIME format. We convert the time values
 * to bytes values into our byte_segment which we use to pull data from
 * the sinkpad peer.
 */
static gboolean
gst_dvdemux_do_seek (GstDVDemux * demux, GstSegment * segment)
{
  gboolean res;
  GstFormat format;

  /* position to value configured is last_stop, this will round down
   * to the byte position where the frame containing the given 
   * timestamp can be found. */
  format = GST_FORMAT_BYTES;
  res = gst_dvdemux_sink_convert (demux,
      segment->format, segment->last_stop,
      &format, &demux->byte_segment.last_stop);
  if (!res)
    goto done;

  /* update byte segment start */
  gst_dvdemux_sink_convert (demux,
      segment->format, segment->start, &format, &demux->byte_segment.start);

  /* update byte segment stop */
  gst_dvdemux_sink_convert (demux,
      segment->format, segment->stop, &format, &demux->byte_segment.stop);

  /* update byte segment time */
  gst_dvdemux_sink_convert (demux,
      segment->format, segment->time, &format, &demux->byte_segment.time);

  /* calculate current frame number */
  format = GST_FORMAT_DEFAULT;
  gst_dvdemux_src_convert (demux, demux->videosrcpad,
      segment->format, segment->start, &format, &demux->video_offset);

  /* calculate current audio number */
  format = GST_FORMAT_DEFAULT;
  gst_dvdemux_src_convert (demux, demux->audiosrcpad,
      segment->format, segment->start, &format, &demux->audio_offset);

  /* every DV frame corresponts with one video frame */
  demux->frame_offset = demux->video_offset;

done:
  return res;
}

/* handle seek in pull base mode.
972
973
 *
 * Does not take ownership of the event.
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
 */
static gboolean
gst_dvdemux_handle_pull_seek (GstDVDemux * demux, GstPad * pad,
    GstEvent * event)
{
  gboolean res;
  gdouble rate;
  GstFormat format;
  GstSeekFlags flags;
  GstSeekType cur_type, stop_type;
  gint64 cur, stop;
  gboolean flush;
  gboolean update;
  GstSegment seeksegment;

  GST_DEBUG_OBJECT (demux, "doing seek");

  /* first bring the event format to TIME, our native format
   * to perform the seek on */
  if (event) {
    GstFormat conv;

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

    /* can't seek backwards yet */
    if (rate <= 0.0)
      goto wrong_rate;

    /* convert input format to TIME */
    conv = GST_FORMAT_TIME;
    if (!(res = gst_dvdemux_convert_src_pair (demux, pad,
                format, cur, stop, conv, &cur, &stop)))
      goto no_format;

    format = GST_FORMAT_TIME;
  } else {
    flags = 0;
  }

  flush = flags & GST_SEEK_FLAG_FLUSH;

  /* send flush start */
  if (flush)
    gst_dvdemux_push_event (demux, gst_event_new_flush_start ());
  else
    gst_pad_pause_task (demux->sinkpad);

  /* grab streaming lock, this should eventually be possible, either
   * because the task is paused or our streaming thread stopped
   * because our peer is flushing. */
  GST_PAD_STREAM_LOCK (demux->sinkpad);

  /* make copy into temp structure, we can only update the main one
   * when the subclass actually could to the seek. */
  memcpy (&seeksegment, &demux->time_segment, sizeof (GstSegment));

  /* now configure the seek segment */
  if (event) {
    gst_segment_set_seek (&seeksegment, rate, format, flags,
        cur_type, cur, stop_type, stop, &update);
  }

  GST_DEBUG_OBJECT (demux, "segment configured from %" G_GINT64_FORMAT
      " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
      seeksegment.start, seeksegment.stop, seeksegment.last_stop);

  /* do the seek, segment.last_stop contains new position. */
  res = gst_dvdemux_do_seek (demux, &seeksegment);

  /* and prepare to continue streaming */
  if (flush) {
    /* send flush stop, peer will accept data and events again. We
     * are not yet providing data as we still have the STREAM_LOCK. */
    gst_dvdemux_push_event (demux, gst_event_new_flush_stop ());
  } else if (res && demux->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 (demux, "closing running segment %" G_GINT64_FORMAT
        " to %" G_GINT64_FORMAT, demux->time_segment.start,
        demux->time_segment.last_stop);

    gst_dvdemux_push_event (demux,
        gst_event_new_new_segment (TRUE,
            demux->time_segment.rate, demux->time_segment.format,
            demux->time_segment.start, demux->time_segment.last_stop,
            demux->time_segment.time));
  }

  /* if successfull seek, we update our real segment and push
   * out the new segment. */
  if (res) {
    memcpy (&demux->time_segment, &seeksegment, sizeof (GstSegment));

    if (demux->time_segment.flags & GST_SEEK_FLAG_SEGMENT) {
      gst_element_post_message (GST_ELEMENT_CAST (demux),
          gst_message_new_segment_start (GST_OBJECT_CAST (demux),
              demux->time_segment.format, demux->time_segment.last_stop));
    }
    if ((stop = demux->time_segment.stop) == -1)
      stop = demux->time_segment.duration;

1076
1077
    GST_INFO_OBJECT (demux,
        "Saving newsegment event to be sent in streaming thread");
1078

1079
1080
1081
1082
1083
1084
    if (demux->pending_segment)
      gst_event_unref (demux->pending_segment);

    demux->pending_segment = gst_event_new_new_segment (FALSE,
        demux->time_segment.rate, demux->time_segment.format,
        demux->time_segment.last_stop, stop, demux->time_segment.time);
1085
1086

    demux->need_segment = FALSE;
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
  }

  demux->running = TRUE;
  /* and restart the task in case it got paused explicitely or by
   * the FLUSH_START event we pushed out. */
  gst_pad_start_task (demux->sinkpad, (GstTaskFunction) gst_dvdemux_loop,
      demux->sinkpad);

  /* and release the lock again so we can continue streaming */
  GST_PAD_STREAM_UNLOCK (demux->sinkpad);

  return TRUE;

  /* ERRORS */
wrong_rate:
  {
    GST_DEBUG_OBJECT (demux, "negative playback rate %lf not supported.", rate);
    return FALSE;
  }
no_format:
  {
    GST_DEBUG_OBJECT (demux, "cannot convert to TIME format, seek aborted.");
    return FALSE;
  }
}

1113
1114
1115
1116
1117
1118
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
1145
1146
1147
1148
1149
1150
static gboolean
gst_dvdemux_send_event (GstElement * element, GstEvent * event)
{
  GstDVDemux *dvdemux = GST_DVDEMUX (element);
  gboolean res = FALSE;

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
    {
      /* checking header and configuring the seek must be atomic */
      GST_OBJECT_LOCK (dvdemux);
      if (g_atomic_int_get (&dvdemux->found_header) == 0) {
        GstEvent **event_p;

        event_p = &dvdemux->seek_event;

        /* We don't have pads yet. Keep the event. */
        GST_INFO_OBJECT (dvdemux, "Keeping the seek event for later");

        gst_event_replace (event_p, event);
        GST_OBJECT_UNLOCK (dvdemux);

        res = TRUE;
      } else {
        GST_OBJECT_UNLOCK (dvdemux);

        if (dvdemux->seek_handler)
          res = dvdemux->seek_handler (dvdemux, dvdemux->videosrcpad, event);
      }
      break;
    }
    default:
      break;
  }

  return res;
}

1151
/* handle an event on the source pad, it's most likely a seek */
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
static gboolean
gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event)
{
  gboolean res = TRUE;
  GstDVDemux *dvdemux;

  dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
1162
1163
1164
1165
1166
      /* seek handler is installed based on scheduling mode */
      if (dvdemux->seek_handler)
        res = dvdemux->seek_handler (dvdemux, pad, event);
      else
        res = FALSE;
1167
      break;
1168
1169
1170
1171
    case GST_EVENT_QOS:
      /* we can't really (yet) do QoS */
    case GST_EVENT_NAVIGATION:
      /* no navigation either... */
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
    default:
      res = FALSE;
      break;
  }
  gst_event_unref (event);

  gst_object_unref (dvdemux);

  return res;
}

1183
/* does not take ownership of buffer */
1184
static GstFlowReturn
1185
1186
gst_dvdemux_demux_audio (GstDVDemux * dvdemux, GstBuffer * buffer,
    guint64 duration)
1187
1188
1189
1190
{
  gint num_samples;
  gint frequency, channels;
  GstFlowReturn ret;
1191
  const guint8 *data;
1192
1193
1194
1195

  frequency = dv_get_frequency (dvdemux->decoder);
  channels = dv_get_num_channels (dvdemux->decoder);

1196
1197
  data = GST_BUFFER_DATA (buffer);

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
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
  /* check if format changed */
  if ((frequency != dvdemux->frequency) || (channels != dvdemux->channels)) {
    GstCaps *caps;

    dvdemux->frequency = frequency;
    dvdemux->channels = channels;

    /* and set new caps */
    caps = gst_caps_new_simple ("audio/x-raw-int",
        "rate", G_TYPE_INT, frequency,
        "depth", G_TYPE_INT, 16,
        "width", G_TYPE_INT, 16,
        "signed", G_TYPE_BOOLEAN, TRUE,
        "channels", G_TYPE_INT, channels,
        "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
    gst_pad_set_caps (dvdemux->audiosrcpad, caps);
    gst_caps_unref (caps);
  }

  dv_decode_full_audio (dvdemux->decoder, data, dvdemux->audio_buffers);

  if ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0) {
    gint16 *a_ptr;
    gint i, j;
    GstBuffer *outbuf;

    outbuf = gst_buffer_new_and_alloc (num_samples *
        sizeof (gint16) * dvdemux->channels);

    a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);

    for (i = 0; i < num_samples; i++) {
      for (j = 0; j < dvdemux->channels; j++) {
        *(a_ptr++) = dvdemux->audio_buffers[j][i];
      }
    }

    GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
1236
        GST_TIME_ARGS (dvdemux->time_segment.last_stop));
1237

1238
1239
    GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.last_stop;
    GST_BUFFER_DURATION (outbuf) = duration;
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
    GST_BUFFER_OFFSET (outbuf) = dvdemux->audio_offset;
    dvdemux->audio_offset += num_samples;
    GST_BUFFER_OFFSET_END (outbuf) = dvdemux->audio_offset;

    gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->audiosrcpad));

    ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
  } else {
    /* no samples */
    ret = GST_FLOW_OK;
  }

  return ret;
}

1255
/* takes ownership of buffer */
1256
static GstFlowReturn
1257
1258
gst_dvdemux_demux_video (GstDVDemux * dvdemux, GstBuffer * buffer,
    guint64 duration)
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
{
  GstBuffer *outbuf;
  gint height;
  gboolean wide;
  GstFlowReturn ret = GST_FLOW_OK;

  /* get params */
  /* framerate is already up-to-date */
  height = (dvdemux->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
  wide = dv_format_wide (dvdemux->decoder);

  /* see if anything changed */
1271
  if (G_UNLIKELY ((dvdemux->height != height) || dvdemux->wide != wide)) {
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
    GstCaps *caps;
    gint par_x, par_y;

    dvdemux->height = height;
    dvdemux->wide = wide;

    if (dvdemux->PAL) {
      if (wide) {
        par_x = PAL_WIDE_PAR_X;
        par_y = PAL_WIDE_PAR_Y;
      } else {
        par_x = PAL_NORMAL_PAR_X;
        par_y = PAL_NORMAL_PAR_Y;
      }
    } else {
      if (wide) {
        par_x = NTSC_WIDE_PAR_X;
        par_y = NTSC_WIDE_PAR_Y;
      } else {
        par_x = NTSC_NORMAL_PAR_X;
        par_y = NTSC_NORMAL_PAR_Y;
      }
    }

    caps = gst_caps_new_simple ("video/x-dv",
        "systemstream", G_TYPE_BOOLEAN, FALSE,
        "width", G_TYPE_INT, 720,
        "height", G_TYPE_INT, height,
1300
1301
        "framerate", GST_TYPE_FRACTION, dvdemux->framerate_numerator,
        dvdemux->framerate_denominator,
1302
1303
1304
1305
1306
        "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
    gst_pad_set_caps (dvdemux->videosrcpad, caps);
    gst_caps_unref (caps);
  }

1307
1308
1309
  /* takes ownership of buffer here, we just need to modify
   * the metadata. */
  outbuf = gst_buffer_make_metadata_writable (buffer);
1310

1311
  GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.last_stop;
1312
1313
  GST_BUFFER_OFFSET (outbuf) = dvdemux->video_offset;
  GST_BUFFER_OFFSET_END (outbuf) = dvdemux->video_offset + 1;
1314
  GST_BUFFER_DURATION (outbuf) = duration;
1315
1316
1317
1318

  gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->videosrcpad));

  GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
1319
      GST_TIME_ARGS (dvdemux->time_segment.last_stop));
1320
1321
1322
1323
1324
1325
1326
1327

  ret = gst_pad_push (dvdemux->videosrcpad, outbuf);

  dvdemux->video_offset++;

  return ret;
}

1328
/* takes ownership of buffer */
1329
static GstFlowReturn
1330
gst_dvdemux_demux_frame (GstDVDemux * dvdemux, GstBuffer * buffer)
1331
1332
1333
{
  GstClockTime next_ts;
  GstFlowReturn aret, vret, ret;
1334
1335
  guint8 *data;
  guint64 duration;
1336

1337
  if (G_UNLIKELY (dvdemux->need_segment)) {
1338
1339
1340
1341
1342
1343
    GstEvent *event;
    GstFormat format;
    gboolean res;

    /* convert to time and store as start/end_timestamp */
    format = GST_FORMAT_TIME;
1344
    if (!(res = gst_dvdemux_convert_sink_pair (dvdemux,
1345
                GST_FORMAT_BYTES, dvdemux->byte_segment.start,
1346
1347
                dvdemux->byte_segment.stop, format,
                &dvdemux->time_segment.start, &dvdemux->time_segment.stop)))
1348
      goto segment_error;
1349

1350
1351
1352
1353
    dvdemux->time_segment.rate = dvdemux->byte_segment.rate;
    dvdemux->time_segment.abs_rate = dvdemux->byte_segment.abs_rate;
    dvdemux->time_segment.last_stop = dvdemux->time_segment.start;

1354
1355
    /* calculate current frame number */
    format = GST_FORMAT_DEFAULT;
1356
    if (!(res = gst_dvdemux_src_convert (dvdemux, dvdemux->videosrcpad,
1357
                GST_FORMAT_TIME, dvdemux->time_segment.start,
1358
                &format, &dvdemux->frame_offset)))
1359
      goto segment_error;
1360

1361
1362
    GST_DEBUG_OBJECT (dvdemux, "sending segment start: %" GST_TIME_FORMAT
        ", stop: %" GST_TIME_FORMAT ", time: %" GST_TIME_FORMAT,
1363
1364
1365
        GST_TIME_ARGS (dvdemux->time_segment.start),
        GST_TIME_ARGS (dvdemux->time_segment.stop),
        GST_TIME_ARGS (dvdemux->time_segment.start));
1366

1367
1368
1369
    event = gst_event_new_new_segment (FALSE, dvdemux->byte_segment.rate,
        GST_FORMAT_TIME, dvdemux->time_segment.start,
        dvdemux->time_segment.stop, dvdemux->time_segment.start);
1370
    gst_dvdemux_push_event (dvdemux, event);
1371

1372
    dvdemux->need_segment = FALSE;
1373
1374
  }

1375
  next_ts = gst_util_uint64_scale_int (
1376
      (dvdemux->frame_offset + 1) * GST_SECOND,
1377
      dvdemux->framerate_denominator, dvdemux->framerate_numerator);
1378
1379
1380
  duration = next_ts - dvdemux->time_segment.last_stop;

  data = GST_BUFFER_DATA (buffer);
1381
1382

  dv_parse_packs (dvdemux->decoder, data);
1383
  if (G_UNLIKELY (dv_is_new_recording (dvdemux->decoder, data)))
1384
1385
    dvdemux->new_media = TRUE;

1386
1387
1388
1389
  /* does not take ownership of buffer */
  aret = ret = gst_dvdemux_demux_audio (dvdemux, buffer, duration);
  if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)) {
    gst_buffer_unref (buffer);
1390
    goto done;
1391
  }
1392

1393
1394
1395
  /* takes ownership of buffer */
  vret = ret = gst_dvdemux_demux_video (dvdemux, buffer, duration);
  if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED))
1396
1397
    goto done;

1398
1399
  /* if both are not linked, we stop */
  if (G_UNLIKELY (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED)) {
1400
1401
1402
1403
    ret = GST_FLOW_NOT_LINKED;
    goto done;
  }

1404
  gst_segment_set_last_stop (&dvdemux->time_segment, GST_FORMAT_TIME, next_ts);
1405
  dvdemux->frame_offset++;
1406

1407
1408
1409
1410
1411
1412
  /* check for the end of the segment */
  if (dvdemux->time_segment.stop != -1 && next_ts > dvdemux->time_segment.stop)
    ret = GST_FLOW_UNEXPECTED;
  else
    ret = GST_FLOW_OK;

1413
1414
1415
done:
  return ret;

1416
  /* ERRORS */
1417
segment_error:
1418
  {
1419
    GST_DEBUG ("error generating new_segment event");
1420
    gst_buffer_unref (buffer);
1421
1422
1423
1424
    return GST_FLOW_ERROR;
  }
}

1425
/* flush any remaining data in the adapter, used in chain based scheduling mode */
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
static GstFlowReturn
gst_dvdemux_flush (GstDVDemux * dvdemux)
{
  GstFlowReturn ret = GST_FLOW_OK;

  while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
    const guint8 *data;
    gint length;

    /* get the accumulated bytes */
    data = gst_adapter_peek (dvdemux->adapter, dvdemux->frame_len);

    /* parse header to know the length and other params */
1439
    if (G_UNLIKELY (dv_parse_header (dvdemux->decoder, data) < 0))
1440
1441
1442
1443
1444
      goto parse_header_error;

    /* after parsing the header we know the length of the data */
    dvdemux->PAL = dv_system_50_fields (dvdemux->decoder);
    length = dvdemux->frame_len = (dvdemux->PAL ? PAL_BUFFER : NTSC_BUFFER);
1445
1446
1447
1448
1449
1450
1451
    if (dvdemux->PAL) {
      dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
      dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
    } else {
      dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
      dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
    }
1452
1453
    gst_atomic_int_set (&dvdemux->found_header, 1);

1454
1455
1456
1457
    /* let demux_video set the height, it needs to detect when things change so
     * it can reset caps */

    /* if we still have enough for a frame, start decoding */
1458
1459
    if (G_LIKELY (gst_adapter_available (dvdemux->adapter) >= length)) {
      GstBuffer *buffer;
1460

1461
      data = gst_adapter_take (dvdemux->adapter, length);
1462

1463
1464
1465
1466
1467
      /* create buffer for the remainder of the code */
      buffer = gst_buffer_new ();
      GST_BUFFER_DATA (buffer) = (guint8 *) data;
      GST_BUFFER_SIZE (buffer) = length;
      GST_BUFFER_MALLOCDATA (buffer) = (guint8 *) data;
1468

1469
1470
1471
      /* and decode the buffer, takes ownership */
      ret = gst_dvdemux_demux_frame (dvdemux, buffer);
      if (G_UNLIKELY (ret != GST_FLOW_OK))
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
        goto done;
    }
  }
done:
  return ret;

  /* ERRORS */
parse_header_error:
  {
    GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1482
        (NULL), ("Error parsing DV header"));
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
    return GST_FLOW_ERROR;
  }
}

/* streaming operation: 
 *
 * accumulate data until we have a frame, then decode. 
 */
static GstFlowReturn
gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer)
{
  GstDVDemux *dvdemux;
  GstFlowReturn ret;
Wim Taymans's avatar
Wim Taymans committed
1496
  GstClockTime timestamp;
1497
1498
1499

  dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));

1500
1501
1502
1503
  /* a discontinuity in the stream, we need to get rid of
   * accumulated data in the adapter and assume a new frame
   * starts after the discontinuity */
  if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)))
1504
1505
    gst_adapter_clear (dvdemux->adapter);

Wim Taymans's avatar
Wim Taymans committed
1506
1507
1508
1509
1510
1511
1512
1513
  /* a timestamp always should be respected */
  timestamp = GST_BUFFER_TIMESTAMP (buffer);
  if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
    gst_segment_set_last_stop (&dvdemux->time_segment, GST_FORMAT_TIME,
        timestamp);
    /* FIXME, adjust frame_offset and other counters */
  }

1514
  /* temporary hack? Can't do this from the state change */
1515
  if (G_UNLIKELY (!dvdemux->videosrcpad))
1516
1517
    gst_dvdemux_add_pads (dvdemux);

1518
1519
1520
1521
1522
  gst_adapter_push (dvdemux->adapter, buffer);

  /* Apparently dv_parse_header can read from the body of the frame
   * too, so it needs more than header_size bytes. Wacky!
   */
1523
  if (G_UNLIKELY (dvdemux->frame_len == -1)) {
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
    /* if we don't know the length of a frame, we assume it is
     * the NTSC_BUFFER length, as this is enough to figure out 
     * if this is PAL or NTSC */
    dvdemux->frame_len = NTSC_BUFFER;
  }

  /* and try to flush pending frames */
  ret = gst_dvdemux_flush (dvdemux);

  gst_object_unref (dvdemux);

  return ret;
}

1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
/* pull based operation.
 *
 * Read header first to figure out the frame size. Then read
 * and decode full