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