gstdvdemux.c 53 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
111
112
GST_DEBUG_CATEGORY (dvdemux_debug);
#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
309
310
311
  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);
  gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->videosrcpad);

  dvdemux->audiosrcpad =
312
      gst_pad_new_from_static_template (&audio_src_temp, "audio");
313
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);
  gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->audiosrcpad);
321
322

  gst_element_no_more_pads (GST_ELEMENT (dvdemux));
323
324
325
}

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

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

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

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

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

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

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

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

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

446
447
448
  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);
449
450
451
452

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

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

458
459
460
  if (dvdemux->frame_len <= 0)
    goto error;

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

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

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

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

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

done:
  return res;

error:
  {
511
    GST_INFO_OBJECT (dvdemux, "sink conversion failed");
512
513
514
515
516
517
518
519
520
    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
521
    GST_QUERY_DURATION,
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    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
539
540
541
542
543
544
545
546
    {
      GstFormat format;
      gint64 cur;

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

      /* bring the position to the requested format. */
547
548
549
      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
550
551
552
553
554
        goto error;
      gst_query_set_position (query, format, cur);
      break;
    }
    case GST_QUERY_DURATION:
555
556
    {
      GstFormat format;
557
      GstFormat format2;
Wim Taymans's avatar
Wim Taymans committed
558
      gint64 end;
559
560
561
      GstPad *peer;

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

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

      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
575
576
577
        gst_query_parse_duration (query, NULL, &end);

        gst_object_unref (peer);
578
579
580

        /* convert end to requested format */
        if (end != -1) {
581
          format2 = format;
582
          if (!(res = gst_dvdemux_sink_convert (dvdemux,
583
                      GST_FORMAT_BYTES, end, &format2, &end))) {
584
585
586
587
588
589
            goto error;
          }
        }
      } else {
        end = -1;
      }
Wim Taymans's avatar
Wim Taymans committed
590
      gst_query_set_duration (query, format, end);
591
592
593
594
595
596
597
598
599
      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 =
600
601
              gst_dvdemux_src_convert (dvdemux, pad, src_fmt, src_val,
                  &dest_fmt, &dest_val)))
602
603
604
605
606
        goto error;
      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
    default:
607
      res = gst_pad_query_default (pad, query);
608
609
610
611
612
613
      break;
  }
  gst_object_unref (dvdemux);

  return res;

614
  /* ERRORS */
615
616
617
error:
  {
    gst_object_unref (dvdemux);
618
    GST_DEBUG ("error source query");
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
644
645
646
647
648
649
    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 =
650
              gst_dvdemux_sink_convert (dvdemux, src_fmt, src_val, &dest_fmt,
651
652
653
654
655
656
                  &dest_val)))
        goto error;
      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
      break;
    }
    default:
657
      res = gst_pad_query_default (pad, query);
658
659
660
661
662
663
      break;
  }
  gst_object_unref (dvdemux);

  return res;

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

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

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

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

  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.*/
703
      res = gst_dvdemux_push_event (dvdemux, event);
704
705
706
707
      break;
    case GST_EVENT_FLUSH_STOP:
      gst_adapter_clear (dvdemux->adapter);
      GST_DEBUG ("cleared adapter");
708
709
      gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
      gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
710
      res = gst_dvdemux_push_event (dvdemux, event);
711
712
713
      break;
    case GST_EVENT_NEWSEGMENT:
    {
714
715
      gboolean update;
      gdouble rate;
716
      GstFormat format;
717
      gint64 start, stop, time;
718
719

      /* parse byte start and stop positions */
720
721
722
723
724
725
726
727
      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);

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
          /* 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;
          }
744
745
746
747
748
          gst_event_unref (event);
          break;
        case GST_FORMAT_TIME:
          gst_segment_set_newsegment (&dvdemux->time_segment, update,
              rate, format, start, stop, time);
749

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

  gst_object_unref (dvdemux);

  return res;
}

779
780
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
/* 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;
}

863
864
865
866
867
868
869
870
871
872
873
874
875
#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

876
877
878
879
880
881
882
/* 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.
883
884
 *
 * Does not take ownership of the event.
885
886
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
 */
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.
970
971
 *
 * Does not take ownership of the event.
972
973
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
 */
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;