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