dv: Use correct pixel-aspect-ratio values
[platform/upstream/gst-plugins-good.git] / ext / dv / gstdvdemux.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2005> Wim Taymans <wim@fluendo.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26 #include <math.h>
27
28 #include <gst/audio/audio.h>
29 #include "gstdvdemux.h"
30 #include "gstsmptetimecode.h"
31
32 /**
33  * SECTION:element-dvdemux
34  *
35  * dvdemux splits raw DV into its audio and video components. The audio will be
36  * decoded raw samples and the video will be encoded DV video.
37  *
38  * This element can operate in both push and pull mode depending on the
39  * capabilities of the upstream peer.
40  *
41  * <refsect2>
42  * <title>Example launch line</title>
43  * |[
44  * gst-launch-1.0 filesrc location=test.dv ! dvdemux name=demux ! queue ! audioconvert ! alsasink demux. ! queue ! dvdec ! xvimagesink
45  * ]| This pipeline decodes and renders the raw DV stream to an audio and a videosink.
46  * </refsect2>
47  */
48
49 /* DV output has two modes, normal and wide. The resolution is the same in both
50  * cases: 720 pixels wide by 576 pixels tall in PAL format, and 720x480 for
51  * NTSC.
52  *
53  * Each of the modes has its own pixel aspect ratio, which is fixed in practice
54  * by ITU-R BT.601 (also known as "CCIR-601" or "Rec.601"). Or so claims a
55  * reference that I culled from the reliable "internet",
56  * http://www.mir.com/DMG/aspect.html. Normal PAL is 59/54 and normal NTSC is
57  * 10/11. Because the pixel resolution is the same for both cases, we can get
58  * the pixel aspect ratio for wide recordings by multiplying by the ratio of
59  * display aspect ratios, 16/9 (for wide) divided by 4/3 (for normal):
60  *
61  * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
62  * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
63  *
64  * However, the pixel resolution coming out of a DV source does not combine with
65  * the standard pixel aspect ratios to give a proper display aspect ratio. An
66  * image 480 pixels tall, with a 4:3 display aspect ratio, will be 768 pixels
67  * wide. But, if we take the normal PAL aspect ratio of 59/54, and multiply it
68  * with the width of the DV image (720 pixels), we get 786.666..., which is
69  * nonintegral and too wide. The camera is not outputting a 4:3 image.
70  * 
71  * If the video sink for this stream has fixed dimensions (such as for
72  * fullscreen playback, or for a java applet in a web page), you then have two
73  * choices. Either you show the whole image, but pad the image with black
74  * borders on the top and bottom (like watching a widescreen video on a 4:3
75  * device), or you crop the video to the proper ratio. Apparently the latter is
76  * the standard practice.
77  *
78  * For its part, GStreamer is concerned with accuracy and preservation of
79  * information. This element outputs the 720x576 or 720x480 video that it
80  * recieves, noting the proper aspect ratio. This should not be a problem for
81  * windowed applications, which can change size to fit the video. Applications
82  * with fixed size requirements should decide whether to crop or pad which
83  * an element such as videobox can do.
84  */
85
86 #define NTSC_HEIGHT 480
87 #define NTSC_BUFFER 120000
88 #define NTSC_FRAMERATE_NUMERATOR 30000
89 #define NTSC_FRAMERATE_DENOMINATOR 1001
90
91 #define PAL_HEIGHT 576
92 #define PAL_BUFFER 144000
93 #define PAL_FRAMERATE_NUMERATOR 25
94 #define PAL_FRAMERATE_DENOMINATOR 1
95
96 #define PAL_NORMAL_PAR_X        16
97 #define PAL_NORMAL_PAR_Y        15
98 #define PAL_WIDE_PAR_X          64
99 #define PAL_WIDE_PAR_Y          45
100
101 #define NTSC_NORMAL_PAR_X       8
102 #define NTSC_NORMAL_PAR_Y       9
103 #define NTSC_WIDE_PAR_X         32
104 #define NTSC_WIDE_PAR_Y         27
105
106 GST_DEBUG_CATEGORY_STATIC (dvdemux_debug);
107 #define GST_CAT_DEFAULT dvdemux_debug
108
109 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
110     GST_PAD_SINK,
111     GST_PAD_ALWAYS,
112     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
113     );
114
115 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
116     GST_PAD_SRC,
117     GST_PAD_SOMETIMES,
118     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
119     );
120
121 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
122     GST_PAD_SRC,
123     GST_PAD_SOMETIMES,
124     GST_STATIC_CAPS ("audio/x-raw, "
125         "format = (string) " GST_AUDIO_NE (S16) ", "
126         "layout = (string) interleaved, "
127         "rate = (int) { 32000, 44100, 48000 }, " "channels = (int) {2, 4}")
128     );
129
130
131 #define gst_dvdemux_parent_class parent_class
132 G_DEFINE_TYPE (GstDVDemux, gst_dvdemux, GST_TYPE_ELEMENT);
133
134 static void gst_dvdemux_finalize (GObject * object);
135
136 /* query functions */
137 static gboolean gst_dvdemux_src_query (GstPad * pad, GstObject * parent,
138     GstQuery * query);
139 static gboolean gst_dvdemux_sink_query (GstPad * pad, GstObject * parent,
140     GstQuery * query);
141
142 /* convert functions */
143 static gboolean gst_dvdemux_sink_convert (GstDVDemux * demux,
144     GstFormat src_format, gint64 src_value, GstFormat dest_format,
145     gint64 * dest_value);
146 static gboolean gst_dvdemux_src_convert (GstDVDemux * demux, GstPad * pad,
147     GstFormat src_format, gint64 src_value, GstFormat dest_format,
148     gint64 * dest_value);
149
150 /* event functions */
151 static gboolean gst_dvdemux_send_event (GstElement * element, GstEvent * event);
152 static gboolean gst_dvdemux_handle_src_event (GstPad * pad, GstObject * parent,
153     GstEvent * event);
154 static gboolean gst_dvdemux_handle_sink_event (GstPad * pad, GstObject * parent,
155     GstEvent * event);
156
157 /* scheduling functions */
158 static void gst_dvdemux_loop (GstPad * pad);
159 static GstFlowReturn gst_dvdemux_flush (GstDVDemux * dvdemux);
160 static GstFlowReturn gst_dvdemux_chain (GstPad * pad, GstObject * parent,
161     GstBuffer * buffer);
162
163 /* state change functions */
164 static gboolean gst_dvdemux_sink_activate (GstPad * sinkpad,
165     GstObject * parent);
166 static gboolean gst_dvdemux_sink_activate_mode (GstPad * sinkpad,
167     GstObject * parent, GstPadMode mode, gboolean active);
168 static GstStateChangeReturn gst_dvdemux_change_state (GstElement * element,
169     GstStateChange transition);
170
171 static void
172 gst_dvdemux_class_init (GstDVDemuxClass * klass)
173 {
174   GObjectClass *gobject_class;
175   GstElementClass *gstelement_class;
176
177   gobject_class = (GObjectClass *) klass;
178   gstelement_class = (GstElementClass *) klass;
179
180   gobject_class->finalize = gst_dvdemux_finalize;
181
182   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_dvdemux_change_state);
183   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_dvdemux_send_event);
184
185   gst_element_class_add_static_pad_template (gstelement_class, &sink_temp);
186   gst_element_class_add_static_pad_template (gstelement_class, &video_src_temp);
187   gst_element_class_add_static_pad_template (gstelement_class, &audio_src_temp);
188
189   gst_element_class_set_static_metadata (gstelement_class,
190       "DV system stream demuxer", "Codec/Demuxer",
191       "Uses libdv to separate DV audio from DV video (libdv.sourceforge.net)",
192       "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
193
194   GST_DEBUG_CATEGORY_INIT (dvdemux_debug, "dvdemux", 0, "DV demuxer element");
195 }
196
197 static void
198 gst_dvdemux_init (GstDVDemux * dvdemux)
199 {
200   gint i;
201
202   dvdemux->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
203   /* we can operate in pull and push mode so we install
204    * a custom activate function */
205   gst_pad_set_activate_function (dvdemux->sinkpad,
206       GST_DEBUG_FUNCPTR (gst_dvdemux_sink_activate));
207   /* the function to activate in push mode */
208   gst_pad_set_activatemode_function (dvdemux->sinkpad,
209       GST_DEBUG_FUNCPTR (gst_dvdemux_sink_activate_mode));
210   /* for push mode, this is the chain function */
211   gst_pad_set_chain_function (dvdemux->sinkpad,
212       GST_DEBUG_FUNCPTR (gst_dvdemux_chain));
213   /* handling events (in push mode only) */
214   gst_pad_set_event_function (dvdemux->sinkpad,
215       GST_DEBUG_FUNCPTR (gst_dvdemux_handle_sink_event));
216   /* query functions */
217   gst_pad_set_query_function (dvdemux->sinkpad,
218       GST_DEBUG_FUNCPTR (gst_dvdemux_sink_query));
219
220   /* now add the pad */
221   gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->sinkpad);
222
223   dvdemux->adapter = gst_adapter_new ();
224
225   /* we need 4 temp buffers for audio decoding which are of a static
226    * size and which we can allocate here */
227   for (i = 0; i < 4; i++) {
228     dvdemux->audio_buffers[i] =
229         (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
230   }
231 }
232
233 static void
234 gst_dvdemux_finalize (GObject * object)
235 {
236   GstDVDemux *dvdemux;
237   gint i;
238
239   dvdemux = GST_DVDEMUX (object);
240
241   g_object_unref (dvdemux->adapter);
242
243   /* clean up temp audio buffers */
244   for (i = 0; i < 4; i++) {
245     g_free (dvdemux->audio_buffers[i]);
246   }
247
248   G_OBJECT_CLASS (parent_class)->finalize (object);
249 }
250
251 /* reset to default values before starting streaming */
252 static void
253 gst_dvdemux_reset (GstDVDemux * dvdemux)
254 {
255   dvdemux->frame_offset = 0;
256   dvdemux->audio_offset = 0;
257   dvdemux->video_offset = 0;
258   g_atomic_int_set (&dvdemux->found_header, 0);
259   dvdemux->frame_len = -1;
260   dvdemux->need_segment = FALSE;
261   dvdemux->new_media = FALSE;
262   dvdemux->framerate_numerator = 0;
263   dvdemux->framerate_denominator = 0;
264   dvdemux->height = 0;
265   dvdemux->frequency = 0;
266   dvdemux->channels = 0;
267   dvdemux->wide = FALSE;
268   gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
269   gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
270   dvdemux->have_group_id = FALSE;
271   dvdemux->group_id = G_MAXUINT;
272 }
273
274 static gboolean
275 have_group_id (GstDVDemux * demux)
276 {
277   GstEvent *event;
278
279   event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
280   if (event) {
281     if (gst_event_parse_group_id (event, &demux->group_id))
282       demux->have_group_id = TRUE;
283     else
284       demux->have_group_id = FALSE;
285     gst_event_unref (event);
286   } else if (!demux->have_group_id) {
287     demux->have_group_id = TRUE;
288     demux->group_id = gst_util_group_id_next ();
289   }
290
291   return demux->have_group_id;
292 }
293
294 static GstPad *
295 gst_dvdemux_add_pad (GstDVDemux * dvdemux, GstStaticPadTemplate * template,
296     GstCaps * caps)
297 {
298   gboolean no_more_pads;
299   GstPad *pad;
300   GstEvent *event;
301   gchar *stream_id;
302   gchar rec_datetime[40];
303   GstDateTime *rec_dt;
304
305   pad = gst_pad_new_from_static_template (template, template->name_template);
306
307   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
308
309   gst_pad_set_event_function (pad,
310       GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
311   gst_pad_use_fixed_caps (pad);
312   gst_pad_set_active (pad, TRUE);
313
314   stream_id =
315       gst_pad_create_stream_id (pad,
316       GST_ELEMENT_CAST (dvdemux),
317       template == &video_src_temp ? "video" : "audio");
318   event = gst_event_new_stream_start (stream_id);
319   if (have_group_id (dvdemux))
320     gst_event_set_group_id (event, dvdemux->group_id);
321   gst_pad_push_event (pad, event);
322   g_free (stream_id);
323
324   gst_pad_set_caps (pad, caps);
325
326   gst_pad_push_event (pad, gst_event_new_segment (&dvdemux->time_segment));
327
328   gst_element_add_pad (GST_ELEMENT (dvdemux), pad);
329
330   no_more_pads =
331       (dvdemux->videosrcpad != NULL && template == &audio_src_temp) ||
332       (dvdemux->audiosrcpad != NULL && template == &video_src_temp);
333
334   if (no_more_pads)
335     gst_element_no_more_pads (GST_ELEMENT (dvdemux));
336
337   if (no_more_pads) {
338     GstTagList *tags;
339
340     tags = gst_tag_list_new (GST_TAG_CONTAINER_FORMAT, "DV", NULL);
341     gst_tag_list_set_scope (tags, GST_TAG_SCOPE_GLOBAL);
342
343     if (dv_get_recording_datetime (dvdemux->decoder, rec_datetime)) {
344       rec_dt = gst_date_time_new_from_iso8601_string (rec_datetime);
345       if (rec_dt) {
346         gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE_TIME,
347             rec_dt, NULL);
348         gst_date_time_unref (rec_dt);
349       }
350     }
351
352     if (dvdemux->videosrcpad)
353       gst_pad_push_event (dvdemux->videosrcpad,
354           gst_event_new_tag (gst_tag_list_ref (tags)));
355     if (dvdemux->audiosrcpad)
356       gst_pad_push_event (dvdemux->audiosrcpad,
357           gst_event_new_tag (gst_tag_list_ref (tags)));
358     gst_tag_list_unref (tags);
359   }
360
361   return pad;
362 }
363
364 static void
365 gst_dvdemux_remove_pads (GstDVDemux * dvdemux)
366 {
367   if (dvdemux->videosrcpad) {
368     gst_element_remove_pad (GST_ELEMENT (dvdemux), dvdemux->videosrcpad);
369     dvdemux->videosrcpad = NULL;
370   }
371   if (dvdemux->audiosrcpad) {
372     gst_element_remove_pad (GST_ELEMENT (dvdemux), dvdemux->audiosrcpad);
373     dvdemux->audiosrcpad = NULL;
374   }
375 }
376
377 static gboolean
378 gst_dvdemux_src_convert (GstDVDemux * dvdemux, GstPad * pad,
379     GstFormat src_format, gint64 src_value, GstFormat dest_format,
380     gint64 * dest_value)
381 {
382   gboolean res = TRUE;
383
384   if (dest_format == src_format || src_value == -1) {
385     *dest_value = src_value;
386     goto done;
387   }
388
389   if (dvdemux->frame_len <= 0)
390     goto error;
391
392   if (dvdemux->decoder == NULL)
393     goto error;
394
395   GST_INFO_OBJECT (pad,
396       "src_value:%" G_GINT64_FORMAT ", src_format:%d, dest_format:%d",
397       src_value, src_format, dest_format);
398
399   switch (src_format) {
400     case GST_FORMAT_BYTES:
401       switch (dest_format) {
402         case GST_FORMAT_DEFAULT:
403           if (pad == dvdemux->videosrcpad)
404             *dest_value = src_value / dvdemux->frame_len;
405           else if (pad == dvdemux->audiosrcpad)
406             *dest_value = src_value / (2 * dvdemux->channels);
407           break;
408         case GST_FORMAT_TIME:
409           if (pad == dvdemux->videosrcpad)
410             *dest_value = gst_util_uint64_scale (src_value,
411                 GST_SECOND * dvdemux->framerate_denominator,
412                 dvdemux->frame_len * dvdemux->framerate_numerator);
413           else if (pad == dvdemux->audiosrcpad)
414             *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
415                 2 * dvdemux->frequency * dvdemux->channels);
416           break;
417         default:
418           res = FALSE;
419       }
420       break;
421     case GST_FORMAT_TIME:
422       switch (dest_format) {
423         case GST_FORMAT_BYTES:
424           if (pad == dvdemux->videosrcpad)
425             *dest_value = gst_util_uint64_scale (src_value,
426                 dvdemux->frame_len * dvdemux->framerate_numerator,
427                 dvdemux->framerate_denominator * GST_SECOND);
428           else if (pad == dvdemux->audiosrcpad)
429             *dest_value = gst_util_uint64_scale_int (src_value,
430                 2 * dvdemux->frequency * dvdemux->channels, GST_SECOND);
431           break;
432         case GST_FORMAT_DEFAULT:
433           if (pad == dvdemux->videosrcpad) {
434             if (src_value)
435               *dest_value = gst_util_uint64_scale (src_value,
436                   dvdemux->framerate_numerator,
437                   dvdemux->framerate_denominator * GST_SECOND);
438             else
439               *dest_value = 0;
440           } else if (pad == dvdemux->audiosrcpad) {
441             *dest_value = gst_util_uint64_scale (src_value,
442                 dvdemux->frequency, GST_SECOND);
443           }
444           break;
445         default:
446           res = FALSE;
447       }
448       break;
449     case GST_FORMAT_DEFAULT:
450       switch (dest_format) {
451         case GST_FORMAT_TIME:
452           if (pad == dvdemux->videosrcpad) {
453             *dest_value = gst_util_uint64_scale (src_value,
454                 GST_SECOND * dvdemux->framerate_denominator,
455                 dvdemux->framerate_numerator);
456           } else if (pad == dvdemux->audiosrcpad) {
457             if (src_value)
458               *dest_value = gst_util_uint64_scale (src_value,
459                   GST_SECOND, dvdemux->frequency);
460             else
461               *dest_value = 0;
462           }
463           break;
464         case GST_FORMAT_BYTES:
465           if (pad == dvdemux->videosrcpad) {
466             *dest_value = src_value * dvdemux->frame_len;
467           } else if (pad == dvdemux->audiosrcpad) {
468             *dest_value = src_value * 2 * dvdemux->channels;
469           }
470           break;
471         default:
472           res = FALSE;
473       }
474       break;
475     default:
476       res = FALSE;
477   }
478
479 done:
480   GST_INFO_OBJECT (pad,
481       "Result : dest_format:%d, dest_value:%" G_GINT64_FORMAT ", res:%d",
482       dest_format, *dest_value, res);
483   return res;
484
485   /* ERRORS */
486 error:
487   {
488     GST_INFO ("source conversion failed");
489     return FALSE;
490   }
491 }
492
493 static gboolean
494 gst_dvdemux_sink_convert (GstDVDemux * dvdemux, GstFormat src_format,
495     gint64 src_value, GstFormat dest_format, gint64 * dest_value)
496 {
497   gboolean res = TRUE;
498
499   GST_DEBUG_OBJECT (dvdemux, "%d -> %d", src_format, dest_format);
500   GST_INFO_OBJECT (dvdemux,
501       "src_value:%" G_GINT64_FORMAT ", src_format:%d, dest_format:%d",
502       src_value, src_format, dest_format);
503
504   if (dest_format == src_format || src_value == -1) {
505     *dest_value = src_value;
506     goto done;
507   }
508
509   if (dvdemux->frame_len <= 0)
510     goto error;
511
512   switch (src_format) {
513     case GST_FORMAT_BYTES:
514       switch (dest_format) {
515         case GST_FORMAT_TIME:
516         {
517           guint64 frame;
518
519           /* get frame number, rounds down so don't combine this
520            * line and the next line. */
521           frame = src_value / dvdemux->frame_len;
522
523           *dest_value = gst_util_uint64_scale (frame,
524               GST_SECOND * dvdemux->framerate_denominator,
525               dvdemux->framerate_numerator);
526           break;
527         }
528         default:
529           res = FALSE;
530       }
531       break;
532     case GST_FORMAT_TIME:
533       switch (dest_format) {
534         case GST_FORMAT_BYTES:
535         {
536           guint64 frame;
537
538           /* calculate the frame */
539           frame =
540               gst_util_uint64_scale (src_value, dvdemux->framerate_numerator,
541               dvdemux->framerate_denominator * GST_SECOND);
542
543           /* calculate the offset from the rounded frame */
544           *dest_value = frame * dvdemux->frame_len;
545           break;
546         }
547         default:
548           res = FALSE;
549       }
550       break;
551     default:
552       res = FALSE;
553   }
554   GST_INFO_OBJECT (dvdemux,
555       "Result : dest_format:%d, dest_value:%" G_GINT64_FORMAT ", res:%d",
556       dest_format, *dest_value, res);
557
558 done:
559   return res;
560
561 error:
562   {
563     GST_INFO_OBJECT (dvdemux, "sink conversion failed");
564     return FALSE;
565   }
566 }
567
568 static gboolean
569 gst_dvdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
570 {
571   gboolean res = TRUE;
572   GstDVDemux *dvdemux;
573
574   dvdemux = GST_DVDEMUX (parent);
575
576   switch (GST_QUERY_TYPE (query)) {
577     case GST_QUERY_POSITION:
578     {
579       GstFormat format;
580       gint64 cur;
581
582       /* get target format */
583       gst_query_parse_position (query, &format, NULL);
584
585       /* bring the position to the requested format. */
586       if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
587                   GST_FORMAT_TIME, dvdemux->time_segment.position,
588                   format, &cur)))
589         goto error;
590       gst_query_set_position (query, format, cur);
591       break;
592     }
593     case GST_QUERY_DURATION:
594     {
595       GstFormat format;
596       gint64 end;
597       GstQuery *pquery;
598
599       /* First ask the peer in the original format */
600       if (!gst_pad_peer_query (dvdemux->sinkpad, query)) {
601         /* get target format */
602         gst_query_parse_duration (query, &format, NULL);
603
604         /* Now ask the peer in BYTES format and try to convert */
605         pquery = gst_query_new_duration (GST_FORMAT_BYTES);
606         if (!gst_pad_peer_query (dvdemux->sinkpad, pquery)) {
607           gst_query_unref (pquery);
608           goto error;
609         }
610
611         /* get peer total length */
612         gst_query_parse_duration (pquery, NULL, &end);
613         gst_query_unref (pquery);
614
615         /* convert end to requested format */
616         if (end != -1) {
617           if (!(res = gst_dvdemux_sink_convert (dvdemux,
618                       GST_FORMAT_BYTES, end, format, &end))) {
619             goto error;
620           }
621           gst_query_set_duration (query, format, end);
622         }
623       }
624       break;
625     }
626     case GST_QUERY_CONVERT:
627     {
628       GstFormat src_fmt, dest_fmt;
629       gint64 src_val, dest_val;
630
631       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
632       if (!(res =
633               gst_dvdemux_src_convert (dvdemux, pad, src_fmt, src_val,
634                   dest_fmt, &dest_val)))
635         goto error;
636       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
637       break;
638     }
639     default:
640       res = gst_pad_query_default (pad, parent, query);
641       break;
642   }
643
644   return res;
645
646   /* ERRORS */
647 error:
648   {
649     GST_DEBUG ("error source query");
650     return FALSE;
651   }
652 }
653
654 static gboolean
655 gst_dvdemux_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
656 {
657   gboolean res = TRUE;
658   GstDVDemux *dvdemux;
659
660   dvdemux = GST_DVDEMUX (parent);
661
662   switch (GST_QUERY_TYPE (query)) {
663     case GST_QUERY_CONVERT:
664     {
665       GstFormat src_fmt, dest_fmt;
666       gint64 src_val, dest_val;
667
668       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
669       if (!(res =
670               gst_dvdemux_sink_convert (dvdemux, src_fmt, src_val, dest_fmt,
671                   &dest_val)))
672         goto error;
673       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
674       break;
675     }
676     default:
677       res = gst_pad_query_default (pad, parent, query);
678       break;
679   }
680
681   return res;
682
683   /* ERRORS */
684 error:
685   {
686     GST_DEBUG ("error handling sink query");
687     return FALSE;
688   }
689 }
690
691 /* takes ownership of the event */
692 static gboolean
693 gst_dvdemux_push_event (GstDVDemux * dvdemux, GstEvent * event)
694 {
695   gboolean res = FALSE;
696
697   if (dvdemux->videosrcpad) {
698     gst_event_ref (event);
699     res |= gst_pad_push_event (dvdemux->videosrcpad, event);
700   }
701
702   if (dvdemux->audiosrcpad)
703     res |= gst_pad_push_event (dvdemux->audiosrcpad, event);
704   else {
705     gst_event_unref (event);
706     res = TRUE;
707   }
708
709   return res;
710 }
711
712 static gboolean
713 gst_dvdemux_handle_sink_event (GstPad * pad, GstObject * parent,
714     GstEvent * event)
715 {
716   GstDVDemux *dvdemux = GST_DVDEMUX (parent);
717   gboolean res = TRUE;
718
719   switch (GST_EVENT_TYPE (event)) {
720     case GST_EVENT_FLUSH_START:
721       /* we are not blocking on anything exect the push() calls
722        * to the peer which will be unblocked by forwarding the
723        * event.*/
724       res = gst_dvdemux_push_event (dvdemux, event);
725       break;
726     case GST_EVENT_FLUSH_STOP:
727       gst_adapter_clear (dvdemux->adapter);
728       GST_DEBUG ("cleared adapter");
729       gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
730       gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
731       res = gst_dvdemux_push_event (dvdemux, event);
732       break;
733     case GST_EVENT_SEGMENT:
734     {
735       const GstSegment *segment;
736
737       gst_event_parse_segment (event, &segment);
738       switch (segment->format) {
739         case GST_FORMAT_BYTES:
740           gst_segment_copy_into (segment, &dvdemux->byte_segment);
741           dvdemux->need_segment = TRUE;
742           gst_event_unref (event);
743           break;
744         case GST_FORMAT_TIME:
745           gst_segment_copy_into (segment, &dvdemux->time_segment);
746
747           /* and we can just forward this time event */
748           res = gst_dvdemux_push_event (dvdemux, event);
749           break;
750         default:
751           gst_event_unref (event);
752           /* cannot accept this format */
753           res = FALSE;
754           break;
755       }
756       break;
757     }
758     case GST_EVENT_EOS:
759       /* flush any pending data, should be nothing left. */
760       gst_dvdemux_flush (dvdemux);
761       /* forward event */
762       res = gst_dvdemux_push_event (dvdemux, event);
763       /* and clear the adapter */
764       gst_adapter_clear (dvdemux->adapter);
765       break;
766     case GST_EVENT_CAPS:
767       gst_event_unref (event);
768       break;
769     default:
770       res = gst_dvdemux_push_event (dvdemux, event);
771       break;
772   }
773
774   return res;
775 }
776
777 /* convert a pair of values on the given srcpad */
778 static gboolean
779 gst_dvdemux_convert_src_pair (GstDVDemux * dvdemux, GstPad * pad,
780     GstFormat src_format, gint64 src_start, gint64 src_stop,
781     GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
782 {
783   gboolean res;
784
785   GST_INFO ("starting conversion of start");
786   /* bring the format to time on srcpad. */
787   if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
788               src_format, src_start, dst_format, dst_start))) {
789     goto done;
790   }
791   GST_INFO ("Finished conversion of start: %" G_GINT64_FORMAT, *dst_start);
792
793   GST_INFO ("starting conversion of stop");
794   /* bring the format to time on srcpad. */
795   if (!(res = gst_dvdemux_src_convert (dvdemux, pad,
796               src_format, src_stop, dst_format, dst_stop))) {
797     /* could not convert seek format to time offset */
798     goto done;
799   }
800   GST_INFO ("Finished conversion of stop: %" G_GINT64_FORMAT, *dst_stop);
801 done:
802   return res;
803 }
804
805 /* convert a pair of values on the sinkpad */
806 static gboolean
807 gst_dvdemux_convert_sink_pair (GstDVDemux * dvdemux,
808     GstFormat src_format, gint64 src_start, gint64 src_stop,
809     GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
810 {
811   gboolean res;
812
813   GST_INFO ("starting conversion of start");
814   /* bring the format to time on srcpad. */
815   if (!(res = gst_dvdemux_sink_convert (dvdemux,
816               src_format, src_start, dst_format, dst_start))) {
817     goto done;
818   }
819   GST_INFO ("Finished conversion of start: %" G_GINT64_FORMAT, *dst_start);
820
821   GST_INFO ("starting conversion of stop");
822   /* bring the format to time on srcpad. */
823   if (!(res = gst_dvdemux_sink_convert (dvdemux,
824               src_format, src_stop, dst_format, dst_stop))) {
825     /* could not convert seek format to time offset */
826     goto done;
827   }
828   GST_INFO ("Finished conversion of stop: %" G_GINT64_FORMAT, *dst_stop);
829 done:
830   return res;
831 }
832
833 /* convert a pair of values on the srcpad to a pair of
834  * values on the sinkpad 
835  */
836 static gboolean
837 gst_dvdemux_convert_src_to_sink (GstDVDemux * dvdemux, GstPad * pad,
838     GstFormat src_format, gint64 src_start, gint64 src_stop,
839     GstFormat dst_format, gint64 * dst_start, gint64 * dst_stop)
840 {
841   GstFormat conv;
842   gboolean res;
843
844   conv = GST_FORMAT_TIME;
845   /* convert to TIME intermediate format */
846   if (!(res = gst_dvdemux_convert_src_pair (dvdemux, pad,
847               src_format, src_start, src_stop, conv, dst_start, dst_stop))) {
848     /* could not convert format to time offset */
849     goto done;
850   }
851   /* convert to dst format on sinkpad */
852   if (!(res = gst_dvdemux_convert_sink_pair (dvdemux,
853               conv, *dst_start, *dst_stop, dst_format, dst_start, dst_stop))) {
854     /* could not convert format to time offset */
855     goto done;
856   }
857 done:
858   return res;
859 }
860
861 #if 0
862 static gboolean
863 gst_dvdemux_convert_segment (GstDVDemux * dvdemux, GstSegment * src,
864     GstSegment * dest)
865 {
866   dest->rate = src->rate;
867   dest->abs_rate = src->abs_rate;
868   dest->flags = src->flags;
869
870   return TRUE;
871 }
872 #endif
873
874 /* handle seek in push base mode.
875  *
876  * Convert the time seek to a bytes seek and send it
877  * upstream
878  * Does not take ownership of the event.
879  */
880 static gboolean
881 gst_dvdemux_handle_push_seek (GstDVDemux * dvdemux, GstPad * pad,
882     GstEvent * event)
883 {
884   gboolean res = FALSE;
885   gdouble rate;
886   GstSeekFlags flags;
887   GstFormat format;
888   GstSeekType cur_type, stop_type;
889   gint64 cur, stop;
890   gint64 start_position, end_position;
891   GstEvent *newevent;
892
893   gst_event_parse_seek (event, &rate, &format, &flags,
894       &cur_type, &cur, &stop_type, &stop);
895
896   /* First try if upstream can handle time based seeks */
897   if (format == GST_FORMAT_TIME)
898     res = gst_pad_push_event (dvdemux->sinkpad, gst_event_ref (event));
899
900   if (!res) {
901     /* we convert the start/stop on the srcpad to the byte format
902      * on the sinkpad and forward the event */
903     res = gst_dvdemux_convert_src_to_sink (dvdemux, pad,
904         format, cur, stop, GST_FORMAT_BYTES, &start_position, &end_position);
905     if (!res)
906       goto done;
907
908     /* now this is the updated seek event on bytes */
909     newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
910         cur_type, start_position, stop_type, end_position);
911
912     res = gst_pad_push_event (dvdemux->sinkpad, newevent);
913   }
914 done:
915   return res;
916 }
917
918 /* position ourselves to the configured segment, used in pull mode.
919  * The input segment is in TIME format. We convert the time values
920  * to bytes values into our byte_segment which we use to pull data from
921  * the sinkpad peer.
922  */
923 static gboolean
924 gst_dvdemux_do_seek (GstDVDemux * demux, GstSegment * segment)
925 {
926   gboolean res;
927   GstFormat format;
928
929   /* position to value configured is last_stop, this will round down
930    * to the byte position where the frame containing the given 
931    * timestamp can be found. */
932   format = GST_FORMAT_BYTES;
933   res = gst_dvdemux_sink_convert (demux,
934       segment->format, segment->position,
935       format, (gint64 *) & demux->byte_segment.position);
936   if (!res)
937     goto done;
938
939   /* update byte segment start */
940   gst_dvdemux_sink_convert (demux,
941       segment->format, segment->start, format,
942       (gint64 *) & demux->byte_segment.start);
943
944   /* update byte segment stop */
945   gst_dvdemux_sink_convert (demux,
946       segment->format, segment->stop, format,
947       (gint64 *) & demux->byte_segment.stop);
948
949   /* update byte segment time */
950   gst_dvdemux_sink_convert (demux,
951       segment->format, segment->time, format,
952       (gint64 *) & demux->byte_segment.time);
953
954   /* calculate current frame number */
955   format = GST_FORMAT_DEFAULT;
956   gst_dvdemux_src_convert (demux, demux->videosrcpad,
957       segment->format, segment->start, format, &demux->video_offset);
958
959   /* calculate current audio number */
960   format = GST_FORMAT_DEFAULT;
961   gst_dvdemux_src_convert (demux, demux->audiosrcpad,
962       segment->format, segment->start, format, &demux->audio_offset);
963
964   /* every DV frame corresponts with one video frame */
965   demux->frame_offset = demux->video_offset;
966
967 done:
968   return res;
969 }
970
971 /* handle seek in pull base mode.
972  *
973  * Does not take ownership of the event.
974  */
975 static gboolean
976 gst_dvdemux_handle_pull_seek (GstDVDemux * demux, GstPad * pad,
977     GstEvent * event)
978 {
979   gboolean res;
980   gdouble rate;
981   GstFormat format;
982   GstSeekFlags flags;
983   GstSeekType cur_type, stop_type;
984   gint64 cur, stop;
985   gboolean flush;
986   gboolean update;
987   GstSegment seeksegment;
988
989   GST_DEBUG_OBJECT (demux, "doing seek");
990
991   /* first bring the event format to TIME, our native format
992    * to perform the seek on */
993   if (event) {
994     GstFormat conv;
995
996     gst_event_parse_seek (event, &rate, &format, &flags,
997         &cur_type, &cur, &stop_type, &stop);
998
999     /* can't seek backwards yet */
1000     if (rate <= 0.0)
1001       goto wrong_rate;
1002
1003     /* convert input format to TIME */
1004     conv = GST_FORMAT_TIME;
1005     if (!(gst_dvdemux_convert_src_pair (demux, pad,
1006                 format, cur, stop, conv, &cur, &stop)))
1007       goto no_format;
1008
1009     format = GST_FORMAT_TIME;
1010   } else {
1011     flags = 0;
1012   }
1013
1014   flush = flags & GST_SEEK_FLAG_FLUSH;
1015
1016   /* send flush start */
1017   if (flush)
1018     gst_dvdemux_push_event (demux, gst_event_new_flush_start ());
1019   else
1020     gst_pad_pause_task (demux->sinkpad);
1021
1022   /* grab streaming lock, this should eventually be possible, either
1023    * because the task is paused or our streaming thread stopped
1024    * because our peer is flushing. */
1025   GST_PAD_STREAM_LOCK (demux->sinkpad);
1026
1027   /* make copy into temp structure, we can only update the main one
1028    * when the subclass actually could to the seek. */
1029   memcpy (&seeksegment, &demux->time_segment, sizeof (GstSegment));
1030
1031   /* now configure the seek segment */
1032   if (event) {
1033     gst_segment_do_seek (&seeksegment, rate, format, flags,
1034         cur_type, cur, stop_type, stop, &update);
1035   }
1036
1037   GST_DEBUG_OBJECT (demux, "segment configured from %" G_GINT64_FORMAT
1038       " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1039       seeksegment.start, seeksegment.stop, seeksegment.position);
1040
1041   /* do the seek, segment.position contains new position. */
1042   res = gst_dvdemux_do_seek (demux, &seeksegment);
1043
1044   /* and prepare to continue streaming */
1045   if (flush) {
1046     /* send flush stop, peer will accept data and events again. We
1047      * are not yet providing data as we still have the STREAM_LOCK. */
1048     gst_dvdemux_push_event (demux, gst_event_new_flush_stop (TRUE));
1049   }
1050
1051   /* if successfull seek, we update our real segment and push
1052    * out the new segment. */
1053   if (res) {
1054     memcpy (&demux->time_segment, &seeksegment, sizeof (GstSegment));
1055
1056     if (demux->time_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1057       gst_element_post_message (GST_ELEMENT_CAST (demux),
1058           gst_message_new_segment_start (GST_OBJECT_CAST (demux),
1059               demux->time_segment.format, demux->time_segment.position));
1060     }
1061     if ((stop = demux->time_segment.stop) == -1)
1062       stop = demux->time_segment.duration;
1063
1064     GST_INFO_OBJECT (demux,
1065         "Saving newsegment event to be sent in streaming thread");
1066
1067     if (demux->pending_segment)
1068       gst_event_unref (demux->pending_segment);
1069
1070     demux->pending_segment = gst_event_new_segment (&demux->time_segment);
1071
1072     demux->need_segment = FALSE;
1073   }
1074
1075   /* and restart the task in case it got paused explicitely or by
1076    * the FLUSH_START event we pushed out. */
1077   gst_pad_start_task (demux->sinkpad, (GstTaskFunction) gst_dvdemux_loop,
1078       demux->sinkpad, NULL);
1079
1080   /* and release the lock again so we can continue streaming */
1081   GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1082
1083   return TRUE;
1084
1085   /* ERRORS */
1086 wrong_rate:
1087   {
1088     GST_DEBUG_OBJECT (demux, "negative playback rate %lf not supported.", rate);
1089     return FALSE;
1090   }
1091 no_format:
1092   {
1093     GST_DEBUG_OBJECT (demux, "cannot convert to TIME format, seek aborted.");
1094     return FALSE;
1095   }
1096 }
1097
1098 static gboolean
1099 gst_dvdemux_send_event (GstElement * element, GstEvent * event)
1100 {
1101   GstDVDemux *dvdemux = GST_DVDEMUX (element);
1102   gboolean res = FALSE;
1103
1104   switch (GST_EVENT_TYPE (event)) {
1105     case GST_EVENT_SEEK:
1106     {
1107       /* checking header and configuring the seek must be atomic */
1108       GST_OBJECT_LOCK (dvdemux);
1109       if (g_atomic_int_get (&dvdemux->found_header) == 0) {
1110         GstEvent **event_p;
1111
1112         event_p = &dvdemux->seek_event;
1113
1114         /* We don't have pads yet. Keep the event. */
1115         GST_INFO_OBJECT (dvdemux, "Keeping the seek event for later");
1116
1117         gst_event_replace (event_p, event);
1118         GST_OBJECT_UNLOCK (dvdemux);
1119
1120         res = TRUE;
1121       } else {
1122         GST_OBJECT_UNLOCK (dvdemux);
1123
1124         if (dvdemux->seek_handler) {
1125           res = dvdemux->seek_handler (dvdemux, dvdemux->videosrcpad, event);
1126           gst_event_unref (event);
1127         }
1128       }
1129       break;
1130     }
1131     default:
1132       res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
1133       break;
1134   }
1135
1136   return res;
1137 }
1138
1139 /* handle an event on the source pad, it's most likely a seek */
1140 static gboolean
1141 gst_dvdemux_handle_src_event (GstPad * pad, GstObject * parent,
1142     GstEvent * event)
1143 {
1144   gboolean res = TRUE;
1145   GstDVDemux *dvdemux;
1146
1147   dvdemux = GST_DVDEMUX (parent);
1148
1149   switch (GST_EVENT_TYPE (event)) {
1150     case GST_EVENT_SEEK:
1151       /* seek handler is installed based on scheduling mode */
1152       if (dvdemux->seek_handler)
1153         res = dvdemux->seek_handler (dvdemux, pad, event);
1154       else
1155         res = FALSE;
1156       break;
1157     case GST_EVENT_QOS:
1158       /* we can't really (yet) do QoS */
1159       res = FALSE;
1160       break;
1161     case GST_EVENT_NAVIGATION:
1162     case GST_EVENT_CAPS:
1163       /* no navigation or caps either... */
1164       res = FALSE;
1165       break;
1166     default:
1167       res = gst_pad_push_event (dvdemux->sinkpad, event);
1168       event = NULL;
1169       break;
1170   }
1171   if (event)
1172     gst_event_unref (event);
1173
1174   return res;
1175 }
1176
1177 /* does not take ownership of buffer */
1178 static GstFlowReturn
1179 gst_dvdemux_demux_audio (GstDVDemux * dvdemux, GstBuffer * buffer,
1180     guint64 duration)
1181 {
1182   gint num_samples;
1183   GstFlowReturn ret;
1184   GstMapInfo map;
1185
1186   gst_buffer_map (buffer, &map, GST_MAP_READ);
1187   dv_decode_full_audio (dvdemux->decoder, map.data, dvdemux->audio_buffers);
1188   gst_buffer_unmap (buffer, &map);
1189
1190   if (G_LIKELY ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0)) {
1191     gint16 *a_ptr;
1192     gint i, j;
1193     GstBuffer *outbuf;
1194     gint frequency, channels;
1195
1196     /* get initial format or check if format changed */
1197     frequency = dv_get_frequency (dvdemux->decoder);
1198     channels = dv_get_num_channels (dvdemux->decoder);
1199
1200     if (G_UNLIKELY ((dvdemux->audiosrcpad == NULL)
1201             || (frequency != dvdemux->frequency)
1202             || (channels != dvdemux->channels))) {
1203       GstCaps *caps;
1204       GstAudioInfo info;
1205
1206       dvdemux->frequency = frequency;
1207       dvdemux->channels = channels;
1208
1209       gst_audio_info_init (&info);
1210       gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16LE,
1211           frequency, channels, NULL);
1212       caps = gst_audio_info_to_caps (&info);
1213       if (G_UNLIKELY (dvdemux->audiosrcpad == NULL)) {
1214         dvdemux->audiosrcpad =
1215             gst_dvdemux_add_pad (dvdemux, &audio_src_temp, caps);
1216       } else {
1217         gst_pad_set_caps (dvdemux->audiosrcpad, caps);
1218       }
1219       gst_caps_unref (caps);
1220     }
1221
1222     outbuf = gst_buffer_new_and_alloc (num_samples *
1223         sizeof (gint16) * dvdemux->channels);
1224
1225     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
1226     a_ptr = (gint16 *) map.data;
1227
1228     for (i = 0; i < num_samples; i++) {
1229       for (j = 0; j < dvdemux->channels; j++) {
1230         *(a_ptr++) = dvdemux->audio_buffers[j][i];
1231       }
1232     }
1233     gst_buffer_unmap (outbuf, &map);
1234
1235     GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
1236         GST_TIME_ARGS (dvdemux->time_segment.position));
1237
1238     GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.position;
1239     GST_BUFFER_DURATION (outbuf) = duration;
1240     GST_BUFFER_OFFSET (outbuf) = dvdemux->audio_offset;
1241     dvdemux->audio_offset += num_samples;
1242     GST_BUFFER_OFFSET_END (outbuf) = dvdemux->audio_offset;
1243
1244     if (dvdemux->new_media)
1245       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1246
1247     ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
1248   } else {
1249     /* no samples */
1250     ret = GST_FLOW_OK;
1251   }
1252
1253   return ret;
1254 }
1255
1256 /* takes ownership of buffer */
1257 static GstFlowReturn
1258 gst_dvdemux_demux_video (GstDVDemux * dvdemux, GstBuffer * buffer,
1259     guint64 duration)
1260 {
1261   GstBuffer *outbuf;
1262   gint height;
1263   gboolean wide;
1264   GstFlowReturn ret = GST_FLOW_OK;
1265
1266   /* get params */
1267   /* framerate is already up-to-date */
1268   height = dvdemux->decoder->height;
1269   wide = dv_format_wide (dvdemux->decoder);
1270
1271   /* see if anything changed */
1272   if (G_UNLIKELY ((dvdemux->videosrcpad == NULL) || (dvdemux->height != height)
1273           || dvdemux->wide != wide)) {
1274     gint par_x, par_y;
1275     GstCaps *caps;
1276
1277     dvdemux->height = height;
1278     dvdemux->wide = wide;
1279
1280     if (dvdemux->decoder->system == e_dv_system_625_50) {
1281       if (wide) {
1282         par_x = PAL_WIDE_PAR_X;
1283         par_y = PAL_WIDE_PAR_Y;
1284       } else {
1285         par_x = PAL_NORMAL_PAR_X;
1286         par_y = PAL_NORMAL_PAR_Y;
1287       }
1288     } else {
1289       if (wide) {
1290         par_x = NTSC_WIDE_PAR_X;
1291         par_y = NTSC_WIDE_PAR_Y;
1292       } else {
1293         par_x = NTSC_NORMAL_PAR_X;
1294         par_y = NTSC_NORMAL_PAR_Y;
1295       }
1296     }
1297
1298     caps = gst_caps_new_simple ("video/x-dv",
1299         "systemstream", G_TYPE_BOOLEAN, FALSE,
1300         "width", G_TYPE_INT, 720,
1301         "height", G_TYPE_INT, height,
1302         "framerate", GST_TYPE_FRACTION, dvdemux->framerate_numerator,
1303         dvdemux->framerate_denominator,
1304         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
1305
1306     if (G_UNLIKELY (dvdemux->videosrcpad == NULL)) {
1307       dvdemux->videosrcpad =
1308           gst_dvdemux_add_pad (dvdemux, &video_src_temp, caps);
1309     } else {
1310       gst_pad_set_caps (dvdemux->videosrcpad, caps);
1311     }
1312     gst_caps_unref (caps);
1313   }
1314
1315   /* takes ownership of buffer here, we just need to modify
1316    * the metadata. */
1317   outbuf = gst_buffer_make_writable (buffer);
1318
1319   GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.position;
1320   GST_BUFFER_OFFSET (outbuf) = dvdemux->video_offset;
1321   GST_BUFFER_OFFSET_END (outbuf) = dvdemux->video_offset + 1;
1322   GST_BUFFER_DURATION (outbuf) = duration;
1323
1324   if (dvdemux->new_media)
1325     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1326
1327   GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
1328       GST_TIME_ARGS (dvdemux->time_segment.position));
1329
1330   ret = gst_pad_push (dvdemux->videosrcpad, outbuf);
1331
1332   dvdemux->video_offset++;
1333
1334   return ret;
1335 }
1336
1337 static int
1338 get_ssyb_offset (int dif, int ssyb)
1339 {
1340   int offset;
1341
1342   offset = dif * 12000;         /* to dif */
1343   offset += 80 * (1 + (ssyb / 6));      /* to subcode pack */
1344   offset += 3;                  /* past header */
1345   offset += 8 * (ssyb % 6);     /* to ssyb */
1346
1347   return offset;
1348 }
1349
1350 static gboolean
1351 gst_dvdemux_get_timecode (GstDVDemux * dvdemux, GstBuffer * buffer,
1352     GstSMPTETimeCode * timecode)
1353 {
1354   guint8 *data;
1355   GstMapInfo map;
1356   int offset;
1357   int dif;
1358   int n_difs = dvdemux->decoder->num_dif_seqs;
1359
1360   gst_buffer_map (buffer, &map, GST_MAP_READ);
1361   data = map.data;
1362   for (dif = 0; dif < n_difs; dif++) {
1363     offset = get_ssyb_offset (dif, 3);
1364     if (data[offset + 3] == 0x13) {
1365       timecode->frames = ((data[offset + 4] >> 4) & 0x3) * 10 +
1366           (data[offset + 4] & 0xf);
1367       timecode->seconds = ((data[offset + 5] >> 4) & 0x3) * 10 +
1368           (data[offset + 5] & 0xf);
1369       timecode->minutes = ((data[offset + 6] >> 4) & 0x3) * 10 +
1370           (data[offset + 6] & 0xf);
1371       timecode->hours = ((data[offset + 7] >> 4) & 0x3) * 10 +
1372           (data[offset + 7] & 0xf);
1373       GST_DEBUG ("got timecode %" GST_SMPTE_TIME_CODE_FORMAT,
1374           GST_SMPTE_TIME_CODE_ARGS (timecode));
1375       gst_buffer_unmap (buffer, &map);
1376       return TRUE;
1377     }
1378   }
1379
1380   gst_buffer_unmap (buffer, &map);
1381   return FALSE;
1382 }
1383
1384 static gboolean
1385 gst_dvdemux_is_new_media (GstDVDemux * dvdemux, GstBuffer * buffer)
1386 {
1387   guint8 *data;
1388   GstMapInfo map;
1389   int aaux_offset;
1390   int dif;
1391   int n_difs;
1392
1393   n_difs = dvdemux->decoder->num_dif_seqs;
1394
1395   gst_buffer_map (buffer, &map, GST_MAP_READ);
1396   data = map.data;
1397   for (dif = 0; dif < n_difs; dif++) {
1398     if (dif & 1) {
1399       aaux_offset = (dif * 12000) + (6 + 16 * 1) * 80 + 3;
1400     } else {
1401       aaux_offset = (dif * 12000) + (6 + 16 * 4) * 80 + 3;
1402     }
1403     if (data[aaux_offset + 0] == 0x51) {
1404       if ((data[aaux_offset + 2] & 0x80) == 0) {
1405         gst_buffer_unmap (buffer, &map);
1406         return TRUE;
1407       }
1408     }
1409   }
1410
1411   gst_buffer_unmap (buffer, &map);
1412   return FALSE;
1413 }
1414
1415 /* takes ownership of buffer */
1416 static GstFlowReturn
1417 gst_dvdemux_demux_frame (GstDVDemux * dvdemux, GstBuffer * buffer)
1418 {
1419   GstClockTime next_ts;
1420   GstFlowReturn aret, vret, ret;
1421   GstMapInfo map;
1422   guint64 duration;
1423   GstSMPTETimeCode timecode;
1424   int frame_number;
1425
1426   if (G_UNLIKELY (dvdemux->need_segment)) {
1427     GstFormat format;
1428
1429     /* convert to time and store as start/end_timestamp */
1430     format = GST_FORMAT_TIME;
1431     if (!(gst_dvdemux_convert_sink_pair (dvdemux,
1432                 GST_FORMAT_BYTES, dvdemux->byte_segment.start,
1433                 dvdemux->byte_segment.stop, format,
1434                 (gint64 *) & dvdemux->time_segment.start,
1435                 (gint64 *) & dvdemux->time_segment.stop)))
1436       goto segment_error;
1437
1438     dvdemux->time_segment.rate = dvdemux->byte_segment.rate;
1439     dvdemux->time_segment.position = dvdemux->time_segment.start;
1440
1441     /* calculate current frame number */
1442     format = GST_FORMAT_DEFAULT;
1443     if (!(gst_dvdemux_src_convert (dvdemux, dvdemux->videosrcpad,
1444                 GST_FORMAT_TIME, dvdemux->time_segment.start,
1445                 format, &dvdemux->frame_offset)))
1446       goto segment_error;
1447
1448     GST_DEBUG_OBJECT (dvdemux, "sending segment start: %" GST_TIME_FORMAT
1449         ", stop: %" GST_TIME_FORMAT ", time: %" GST_TIME_FORMAT,
1450         GST_TIME_ARGS (dvdemux->time_segment.start),
1451         GST_TIME_ARGS (dvdemux->time_segment.stop),
1452         GST_TIME_ARGS (dvdemux->time_segment.start));
1453
1454     gst_dvdemux_push_event (dvdemux,
1455         gst_event_new_segment (&dvdemux->time_segment));
1456
1457     dvdemux->need_segment = FALSE;
1458   }
1459
1460   gst_dvdemux_get_timecode (dvdemux, buffer, &timecode);
1461   gst_smpte_time_code_get_frame_number (
1462       (dvdemux->decoder->system == e_dv_system_625_50) ?
1463       GST_SMPTE_TIME_CODE_SYSTEM_25 : GST_SMPTE_TIME_CODE_SYSTEM_30,
1464       &frame_number, &timecode);
1465
1466   next_ts = gst_util_uint64_scale_int (
1467       (dvdemux->frame_offset + 1) * GST_SECOND,
1468       dvdemux->framerate_denominator, dvdemux->framerate_numerator);
1469   duration = next_ts - dvdemux->time_segment.position;
1470
1471   gst_buffer_map (buffer, &map, GST_MAP_READ);
1472   dv_parse_packs (dvdemux->decoder, map.data);
1473   gst_buffer_unmap (buffer, &map);
1474   dvdemux->new_media = FALSE;
1475   if (gst_dvdemux_is_new_media (dvdemux, buffer) &&
1476       dvdemux->frames_since_new_media > 2) {
1477     dvdemux->new_media = TRUE;
1478     dvdemux->frames_since_new_media = 0;
1479   }
1480   dvdemux->frames_since_new_media++;
1481
1482   /* does not take ownership of buffer */
1483   aret = ret = gst_dvdemux_demux_audio (dvdemux, buffer, duration);
1484   if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)) {
1485     gst_buffer_unref (buffer);
1486     goto done;
1487   }
1488
1489   /* takes ownership of buffer */
1490   vret = ret = gst_dvdemux_demux_video (dvdemux, buffer, duration);
1491   if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED))
1492     goto done;
1493
1494   /* if both are not linked, we stop */
1495   if (G_UNLIKELY (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED)) {
1496     ret = GST_FLOW_NOT_LINKED;
1497     goto done;
1498   }
1499
1500   dvdemux->time_segment.position = next_ts;
1501   dvdemux->frame_offset++;
1502
1503   /* check for the end of the segment */
1504   if (dvdemux->time_segment.stop != -1 && next_ts > dvdemux->time_segment.stop)
1505     ret = GST_FLOW_EOS;
1506   else
1507     ret = GST_FLOW_OK;
1508
1509 done:
1510   return ret;
1511
1512   /* ERRORS */
1513 segment_error:
1514   {
1515     GST_DEBUG ("error generating new_segment event");
1516     gst_buffer_unref (buffer);
1517     return GST_FLOW_ERROR;
1518   }
1519 }
1520
1521 /* flush any remaining data in the adapter, used in chain based scheduling mode */
1522 static GstFlowReturn
1523 gst_dvdemux_flush (GstDVDemux * dvdemux)
1524 {
1525   GstFlowReturn ret = GST_FLOW_OK;
1526
1527   while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
1528     const guint8 *data;
1529     gint length;
1530
1531     /* get the accumulated bytes */
1532     data = gst_adapter_map (dvdemux->adapter, dvdemux->frame_len);
1533
1534     /* parse header to know the length and other params */
1535     if (G_UNLIKELY (dv_parse_header (dvdemux->decoder, data) < 0)) {
1536       gst_adapter_unmap (dvdemux->adapter);
1537       goto parse_header_error;
1538     }
1539     gst_adapter_unmap (dvdemux->adapter);
1540
1541     /* after parsing the header we know the length of the data */
1542     length = dvdemux->frame_len = dvdemux->decoder->frame_size;
1543     if (dvdemux->decoder->system == e_dv_system_625_50) {
1544       dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
1545       dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
1546     } else {
1547       dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
1548       dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
1549     }
1550     g_atomic_int_set (&dvdemux->found_header, 1);
1551
1552     /* let demux_video set the height, it needs to detect when things change so
1553      * it can reset caps */
1554
1555     /* if we still have enough for a frame, start decoding */
1556     if (G_LIKELY (gst_adapter_available (dvdemux->adapter) >= length)) {
1557       GstBuffer *buffer;
1558
1559       buffer = gst_adapter_take_buffer (dvdemux->adapter, length);
1560
1561       /* and decode the buffer, takes ownership */
1562       ret = gst_dvdemux_demux_frame (dvdemux, buffer);
1563       if (G_UNLIKELY (ret != GST_FLOW_OK))
1564         goto done;
1565     }
1566   }
1567 done:
1568   return ret;
1569
1570   /* ERRORS */
1571 parse_header_error:
1572   {
1573     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1574         (NULL), ("Error parsing DV header"));
1575     return GST_FLOW_ERROR;
1576   }
1577 }
1578
1579 /* streaming operation: 
1580  *
1581  * accumulate data until we have a frame, then decode. 
1582  */
1583 static GstFlowReturn
1584 gst_dvdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1585 {
1586   GstDVDemux *dvdemux;
1587   GstFlowReturn ret;
1588   GstClockTime timestamp;
1589
1590   dvdemux = GST_DVDEMUX (parent);
1591
1592   /* a discontinuity in the stream, we need to get rid of
1593    * accumulated data in the adapter and assume a new frame
1594    * starts after the discontinuity */
1595   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)))
1596     gst_adapter_clear (dvdemux->adapter);
1597
1598   /* a timestamp always should be respected */
1599   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1600   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1601     dvdemux->time_segment.position = timestamp;
1602     /* FIXME, adjust frame_offset and other counters */
1603   }
1604
1605   gst_adapter_push (dvdemux->adapter, buffer);
1606
1607   /* Apparently dv_parse_header can read from the body of the frame
1608    * too, so it needs more than header_size bytes. Wacky!
1609    */
1610   if (G_UNLIKELY (dvdemux->frame_len == -1)) {
1611     /* if we don't know the length of a frame, we assume it is
1612      * the NTSC_BUFFER length, as this is enough to figure out 
1613      * if this is PAL or NTSC */
1614     dvdemux->frame_len = NTSC_BUFFER;
1615   }
1616
1617   /* and try to flush pending frames */
1618   ret = gst_dvdemux_flush (dvdemux);
1619
1620   return ret;
1621 }
1622
1623 /* pull based operation.
1624  *
1625  * Read header first to figure out the frame size. Then read
1626  * and decode full frames.
1627  */
1628 static void
1629 gst_dvdemux_loop (GstPad * pad)
1630 {
1631   GstFlowReturn ret;
1632   GstDVDemux *dvdemux;
1633   GstBuffer *buffer = NULL;
1634   GstMapInfo map;
1635
1636   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1637
1638   if (G_UNLIKELY (g_atomic_int_get (&dvdemux->found_header) == 0)) {
1639     GST_DEBUG_OBJECT (dvdemux, "pulling first buffer");
1640     /* pull in NTSC sized buffer to figure out the frame
1641      * length */
1642     ret = gst_pad_pull_range (dvdemux->sinkpad,
1643         dvdemux->byte_segment.position, NTSC_BUFFER, &buffer);
1644     if (G_UNLIKELY (ret != GST_FLOW_OK))
1645       goto pause;
1646
1647     /* check buffer size, don't want to read small buffers */
1648     if (G_UNLIKELY (gst_buffer_get_size (buffer) < NTSC_BUFFER))
1649       goto small_buffer;
1650
1651     gst_buffer_map (buffer, &map, GST_MAP_READ);
1652     /* parse header to know the length and other params */
1653     if (G_UNLIKELY (dv_parse_header (dvdemux->decoder, map.data) < 0)) {
1654       gst_buffer_unmap (buffer, &map);
1655       goto parse_header_error;
1656     }
1657     gst_buffer_unmap (buffer, &map);
1658
1659     /* after parsing the header we know the length of the data */
1660     dvdemux->frame_len = dvdemux->decoder->frame_size;
1661     if (dvdemux->decoder->system == e_dv_system_625_50) {
1662       dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
1663       dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
1664     } else {
1665       dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
1666       dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
1667     }
1668     dvdemux->need_segment = TRUE;
1669
1670     /* see if we need to read a larger part */
1671     if (dvdemux->frame_len != NTSC_BUFFER) {
1672       gst_buffer_unref (buffer);
1673       buffer = NULL;
1674     }
1675
1676     {
1677       GstEvent *event;
1678
1679       /* setting header and prrforming the seek must be atomic */
1680       GST_OBJECT_LOCK (dvdemux);
1681       /* got header now */
1682       g_atomic_int_set (&dvdemux->found_header, 1);
1683
1684       /* now perform pending seek if any. */
1685       event = dvdemux->seek_event;
1686       if (event)
1687         gst_event_ref (event);
1688       GST_OBJECT_UNLOCK (dvdemux);
1689
1690       if (event) {
1691         if (!gst_dvdemux_handle_pull_seek (dvdemux, dvdemux->videosrcpad,
1692                 event)) {
1693           GST_ELEMENT_WARNING (dvdemux, STREAM, DECODE, (NULL),
1694               ("Error perfoming initial seek"));
1695         }
1696         gst_event_unref (event);
1697
1698         /* and we need to pull a new buffer in all cases. */
1699         if (buffer) {
1700           gst_buffer_unref (buffer);
1701           buffer = NULL;
1702         }
1703       }
1704     }
1705   }
1706
1707   if (G_UNLIKELY (dvdemux->pending_segment)) {
1708
1709     /* now send the newsegment */
1710     GST_DEBUG_OBJECT (dvdemux, "Sending newsegment from");
1711
1712     gst_dvdemux_push_event (dvdemux, dvdemux->pending_segment);
1713     dvdemux->pending_segment = NULL;
1714   }
1715
1716   if (G_LIKELY (buffer == NULL)) {
1717     GST_DEBUG_OBJECT (dvdemux, "pulling buffer at offset %" G_GINT64_FORMAT,
1718         dvdemux->byte_segment.position);
1719
1720     ret = gst_pad_pull_range (dvdemux->sinkpad,
1721         dvdemux->byte_segment.position, dvdemux->frame_len, &buffer);
1722     if (ret != GST_FLOW_OK)
1723       goto pause;
1724
1725     /* check buffer size, don't want to read small buffers */
1726     if (gst_buffer_get_size (buffer) < dvdemux->frame_len)
1727       goto small_buffer;
1728   }
1729   /* and decode the buffer */
1730   ret = gst_dvdemux_demux_frame (dvdemux, buffer);
1731   if (G_UNLIKELY (ret != GST_FLOW_OK))
1732     goto pause;
1733
1734   /* and position ourselves for the next buffer */
1735   dvdemux->byte_segment.position += dvdemux->frame_len;
1736
1737 done:
1738   gst_object_unref (dvdemux);
1739
1740   return;
1741
1742   /* ERRORS */
1743 parse_header_error:
1744   {
1745     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1746         (NULL), ("Error parsing DV header"));
1747     gst_buffer_unref (buffer);
1748     gst_pad_pause_task (dvdemux->sinkpad);
1749     gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1750     goto done;
1751   }
1752 small_buffer:
1753   {
1754     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1755         (NULL), ("Error reading buffer"));
1756     gst_buffer_unref (buffer);
1757     gst_pad_pause_task (dvdemux->sinkpad);
1758     gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1759     goto done;
1760   }
1761 pause:
1762   {
1763     GST_INFO_OBJECT (dvdemux, "pausing task, %s", gst_flow_get_name (ret));
1764     gst_pad_pause_task (dvdemux->sinkpad);
1765     if (ret == GST_FLOW_EOS) {
1766       GST_LOG_OBJECT (dvdemux, "got eos");
1767       /* so align our position with the end of it, if there is one
1768        * this ensures a subsequent will arrive at correct base/acc time */
1769       if (dvdemux->time_segment.rate > 0.0 &&
1770           GST_CLOCK_TIME_IS_VALID (dvdemux->time_segment.stop))
1771         dvdemux->time_segment.position = dvdemux->time_segment.stop;
1772       else if (dvdemux->time_segment.rate < 0.0)
1773         dvdemux->time_segment.position = dvdemux->time_segment.start;
1774       /* perform EOS logic */
1775       if (dvdemux->time_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1776         gst_element_post_message (GST_ELEMENT (dvdemux),
1777             gst_message_new_segment_done (GST_OBJECT_CAST (dvdemux),
1778                 dvdemux->time_segment.format, dvdemux->time_segment.position));
1779         gst_dvdemux_push_event (dvdemux,
1780             gst_event_new_segment_done (dvdemux->time_segment.format,
1781                 dvdemux->time_segment.position));
1782       } else {
1783         gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1784       }
1785     } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1786       /* for fatal errors or not-linked we post an error message */
1787       GST_ELEMENT_ERROR (dvdemux, STREAM, FAILED,
1788           (NULL), ("streaming stopped, reason %s", gst_flow_get_name (ret)));
1789       gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1790     }
1791     goto done;
1792   }
1793 }
1794
1795 static gboolean
1796 gst_dvdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
1797     GstPadMode mode, gboolean active)
1798 {
1799   gboolean res;
1800   GstDVDemux *demux = GST_DVDEMUX (parent);
1801
1802   switch (mode) {
1803     case GST_PAD_MODE_PULL:
1804       if (active) {
1805         demux->seek_handler = gst_dvdemux_handle_pull_seek;
1806         res = gst_pad_start_task (sinkpad,
1807             (GstTaskFunction) gst_dvdemux_loop, sinkpad, NULL);
1808       } else {
1809         demux->seek_handler = NULL;
1810         res = gst_pad_stop_task (sinkpad);
1811       }
1812       break;
1813     case GST_PAD_MODE_PUSH:
1814       if (active) {
1815         GST_DEBUG_OBJECT (demux, "activating push/chain function");
1816         demux->seek_handler = gst_dvdemux_handle_push_seek;
1817       } else {
1818         GST_DEBUG_OBJECT (demux, "deactivating push/chain function");
1819         demux->seek_handler = NULL;
1820       }
1821       res = TRUE;
1822       break;
1823     default:
1824       res = FALSE;
1825       break;
1826   }
1827   return res;
1828 }
1829
1830 /* decide on push or pull based scheduling */
1831 static gboolean
1832 gst_dvdemux_sink_activate (GstPad * sinkpad, GstObject * parent)
1833 {
1834   GstQuery *query;
1835   gboolean pull_mode;
1836
1837   query = gst_query_new_scheduling ();
1838
1839   if (!gst_pad_peer_query (sinkpad, query)) {
1840     gst_query_unref (query);
1841     goto activate_push;
1842   }
1843
1844   pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1845       GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1846   gst_query_unref (query);
1847
1848   if (!pull_mode)
1849     goto activate_push;
1850
1851   GST_DEBUG_OBJECT (sinkpad, "activating pull");
1852   return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
1853
1854 activate_push:
1855   {
1856     GST_DEBUG_OBJECT (sinkpad, "activating push");
1857     return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
1858   }
1859 }
1860
1861 static GstStateChangeReturn
1862 gst_dvdemux_change_state (GstElement * element, GstStateChange transition)
1863 {
1864   GstDVDemux *dvdemux = GST_DVDEMUX (element);
1865   GstStateChangeReturn ret;
1866
1867   switch (transition) {
1868     case GST_STATE_CHANGE_NULL_TO_READY:
1869       break;
1870     case GST_STATE_CHANGE_READY_TO_PAUSED:
1871       dvdemux->decoder = dv_decoder_new (0, FALSE, FALSE);
1872       dv_set_error_log (dvdemux->decoder, NULL);
1873       gst_dvdemux_reset (dvdemux);
1874       break;
1875     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1876       break;
1877     default:
1878       break;
1879   }
1880
1881   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1882
1883   switch (transition) {
1884     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1885       break;
1886     case GST_STATE_CHANGE_PAUSED_TO_READY:
1887       gst_adapter_clear (dvdemux->adapter);
1888       dv_decoder_free (dvdemux->decoder);
1889       dvdemux->decoder = NULL;
1890
1891       gst_dvdemux_remove_pads (dvdemux);
1892       break;
1893     case GST_STATE_CHANGE_READY_TO_NULL:
1894     {
1895       GstEvent **event_p;
1896
1897       event_p = &dvdemux->seek_event;
1898       gst_event_replace (event_p, NULL);
1899       if (dvdemux->pending_segment)
1900         gst_event_unref (dvdemux->pending_segment);
1901       dvdemux->pending_segment = NULL;
1902       break;
1903     }
1904     default:
1905       break;
1906   }
1907   return ret;
1908 }