upload tizen1.0 source
[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 #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  * Does not take ownership of the event.
899  */
900 static gboolean
901 gst_dvdemux_handle_push_seek (GstDVDemux * dvdemux, GstPad * pad,
902     GstEvent * event)
903 {
904   gboolean res = FALSE;
905   gdouble rate;
906   GstSeekFlags flags;
907   GstFormat format;
908   GstSeekType cur_type, stop_type;
909   gint64 cur, stop;
910   gint64 start_position, end_position;
911   GstEvent *newevent;
912
913   gst_event_parse_seek (event, &rate, &format, &flags,
914       &cur_type, &cur, &stop_type, &stop);
915
916   /* First try if upstream can handle time based seeks */
917   if (format == GST_FORMAT_TIME)
918     res = gst_pad_push_event (dvdemux->sinkpad, gst_event_ref (event));
919
920   if (!res) {
921     /* we convert the start/stop on the srcpad to the byte format
922      * on the sinkpad and forward the event */
923     res = gst_dvdemux_convert_src_to_sink (dvdemux, pad,
924         format, cur, stop, GST_FORMAT_BYTES, &start_position, &end_position);
925     if (!res)
926       goto done;
927
928     /* now this is the updated seek event on bytes */
929     newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
930         cur_type, start_position, stop_type, end_position);
931
932     res = gst_pad_push_event (dvdemux->sinkpad, newevent);
933   }
934 done:
935   return res;
936 }
937
938 /* position ourselves to the configured segment, used in pull mode.
939  * The input segment is in TIME format. We convert the time values
940  * to bytes values into our byte_segment which we use to pull data from
941  * the sinkpad peer.
942  */
943 static gboolean
944 gst_dvdemux_do_seek (GstDVDemux * demux, GstSegment * segment)
945 {
946   gboolean res;
947   GstFormat format;
948
949   /* position to value configured is last_stop, this will round down
950    * to the byte position where the frame containing the given 
951    * timestamp can be found. */
952   format = GST_FORMAT_BYTES;
953   res = gst_dvdemux_sink_convert (demux,
954       segment->format, segment->last_stop,
955       &format, &demux->byte_segment.last_stop);
956   if (!res)
957     goto done;
958
959   /* update byte segment start */
960   gst_dvdemux_sink_convert (demux,
961       segment->format, segment->start, &format, &demux->byte_segment.start);
962
963   /* update byte segment stop */
964   gst_dvdemux_sink_convert (demux,
965       segment->format, segment->stop, &format, &demux->byte_segment.stop);
966
967   /* update byte segment time */
968   gst_dvdemux_sink_convert (demux,
969       segment->format, segment->time, &format, &demux->byte_segment.time);
970
971   /* calculate current frame number */
972   format = GST_FORMAT_DEFAULT;
973   gst_dvdemux_src_convert (demux, demux->videosrcpad,
974       segment->format, segment->start, &format, &demux->video_offset);
975
976   /* calculate current audio number */
977   format = GST_FORMAT_DEFAULT;
978   gst_dvdemux_src_convert (demux, demux->audiosrcpad,
979       segment->format, segment->start, &format, &demux->audio_offset);
980
981   /* every DV frame corresponts with one video frame */
982   demux->frame_offset = demux->video_offset;
983
984 done:
985   return res;
986 }
987
988 /* handle seek in pull base mode.
989  *
990  * Does not take ownership of the event.
991  */
992 static gboolean
993 gst_dvdemux_handle_pull_seek (GstDVDemux * demux, GstPad * pad,
994     GstEvent * event)
995 {
996   gboolean res;
997   gdouble rate;
998   GstFormat format;
999   GstSeekFlags flags;
1000   GstSeekType cur_type, stop_type;
1001   gint64 cur, stop;
1002   gboolean flush;
1003   gboolean update;
1004   GstSegment seeksegment;
1005
1006   GST_DEBUG_OBJECT (demux, "doing seek");
1007
1008   /* first bring the event format to TIME, our native format
1009    * to perform the seek on */
1010   if (event) {
1011     GstFormat conv;
1012
1013     gst_event_parse_seek (event, &rate, &format, &flags,
1014         &cur_type, &cur, &stop_type, &stop);
1015
1016     /* can't seek backwards yet */
1017     if (rate <= 0.0)
1018       goto wrong_rate;
1019
1020     /* convert input format to TIME */
1021     conv = GST_FORMAT_TIME;
1022     if (!(gst_dvdemux_convert_src_pair (demux, pad,
1023                 format, cur, stop, conv, &cur, &stop)))
1024       goto no_format;
1025
1026     format = GST_FORMAT_TIME;
1027   } else {
1028     flags = 0;
1029   }
1030
1031   flush = flags & GST_SEEK_FLAG_FLUSH;
1032
1033   /* send flush start */
1034   if (flush)
1035     gst_dvdemux_push_event (demux, gst_event_new_flush_start ());
1036   else
1037     gst_pad_pause_task (demux->sinkpad);
1038
1039   /* grab streaming lock, this should eventually be possible, either
1040    * because the task is paused or our streaming thread stopped
1041    * because our peer is flushing. */
1042   GST_PAD_STREAM_LOCK (demux->sinkpad);
1043
1044   /* make copy into temp structure, we can only update the main one
1045    * when the subclass actually could to the seek. */
1046   memcpy (&seeksegment, &demux->time_segment, sizeof (GstSegment));
1047
1048   /* now configure the seek segment */
1049   if (event) {
1050     gst_segment_set_seek (&seeksegment, rate, format, flags,
1051         cur_type, cur, stop_type, stop, &update);
1052   }
1053
1054   GST_DEBUG_OBJECT (demux, "segment configured from %" G_GINT64_FORMAT
1055       " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
1056       seeksegment.start, seeksegment.stop, seeksegment.last_stop);
1057
1058   /* do the seek, segment.last_stop contains new position. */
1059   res = gst_dvdemux_do_seek (demux, &seeksegment);
1060
1061   /* and prepare to continue streaming */
1062   if (flush) {
1063     /* send flush stop, peer will accept data and events again. We
1064      * are not yet providing data as we still have the STREAM_LOCK. */
1065     gst_dvdemux_push_event (demux, gst_event_new_flush_stop ());
1066   } else if (res && demux->running) {
1067     /* we are running the current segment and doing a non-flushing seek,
1068      * close the segment first based on the last_stop. */
1069     GST_DEBUG_OBJECT (demux, "closing running segment %" G_GINT64_FORMAT
1070         " to %" G_GINT64_FORMAT, demux->time_segment.start,
1071         demux->time_segment.last_stop);
1072
1073     gst_dvdemux_push_event (demux,
1074         gst_event_new_new_segment (TRUE,
1075             demux->time_segment.rate, demux->time_segment.format,
1076             demux->time_segment.start, demux->time_segment.last_stop,
1077             demux->time_segment.time));
1078   }
1079
1080   /* if successfull seek, we update our real segment and push
1081    * out the new segment. */
1082   if (res) {
1083     memcpy (&demux->time_segment, &seeksegment, sizeof (GstSegment));
1084
1085     if (demux->time_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1086       gst_element_post_message (GST_ELEMENT_CAST (demux),
1087           gst_message_new_segment_start (GST_OBJECT_CAST (demux),
1088               demux->time_segment.format, demux->time_segment.last_stop));
1089     }
1090     if ((stop = demux->time_segment.stop) == -1)
1091       stop = demux->time_segment.duration;
1092
1093     GST_INFO_OBJECT (demux,
1094         "Saving newsegment event to be sent in streaming thread");
1095
1096     if (demux->pending_segment)
1097       gst_event_unref (demux->pending_segment);
1098
1099     demux->pending_segment = gst_event_new_new_segment (FALSE,
1100         demux->time_segment.rate, demux->time_segment.format,
1101         demux->time_segment.last_stop, stop, demux->time_segment.time);
1102
1103     demux->need_segment = FALSE;
1104   }
1105
1106   demux->running = TRUE;
1107   /* and restart the task in case it got paused explicitely or by
1108    * the FLUSH_START event we pushed out. */
1109   gst_pad_start_task (demux->sinkpad, (GstTaskFunction) gst_dvdemux_loop,
1110       demux->sinkpad);
1111
1112   /* and release the lock again so we can continue streaming */
1113   GST_PAD_STREAM_UNLOCK (demux->sinkpad);
1114
1115   return TRUE;
1116
1117   /* ERRORS */
1118 wrong_rate:
1119   {
1120     GST_DEBUG_OBJECT (demux, "negative playback rate %lf not supported.", rate);
1121     return FALSE;
1122   }
1123 no_format:
1124   {
1125     GST_DEBUG_OBJECT (demux, "cannot convert to TIME format, seek aborted.");
1126     return FALSE;
1127   }
1128 }
1129
1130 static gboolean
1131 gst_dvdemux_send_event (GstElement * element, GstEvent * event)
1132 {
1133   GstDVDemux *dvdemux = GST_DVDEMUX (element);
1134   gboolean res = FALSE;
1135
1136   switch (GST_EVENT_TYPE (event)) {
1137     case GST_EVENT_SEEK:
1138     {
1139       /* checking header and configuring the seek must be atomic */
1140       GST_OBJECT_LOCK (dvdemux);
1141       if (g_atomic_int_get (&dvdemux->found_header) == 0) {
1142         GstEvent **event_p;
1143
1144         event_p = &dvdemux->seek_event;
1145
1146         /* We don't have pads yet. Keep the event. */
1147         GST_INFO_OBJECT (dvdemux, "Keeping the seek event for later");
1148
1149         gst_event_replace (event_p, event);
1150         GST_OBJECT_UNLOCK (dvdemux);
1151
1152         res = TRUE;
1153       } else {
1154         GST_OBJECT_UNLOCK (dvdemux);
1155
1156         if (dvdemux->seek_handler) {
1157           res = dvdemux->seek_handler (dvdemux, dvdemux->videosrcpad, event);
1158           gst_event_unref (event);
1159         }
1160       }
1161       break;
1162     }
1163     default:
1164       res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
1165       break;
1166   }
1167
1168   return res;
1169 }
1170
1171 /* handle an event on the source pad, it's most likely a seek */
1172 static gboolean
1173 gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event)
1174 {
1175   gboolean res = TRUE;
1176   GstDVDemux *dvdemux;
1177
1178   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1179
1180   switch (GST_EVENT_TYPE (event)) {
1181     case GST_EVENT_SEEK:
1182       /* seek handler is installed based on scheduling mode */
1183       if (dvdemux->seek_handler)
1184         res = dvdemux->seek_handler (dvdemux, pad, event);
1185       else
1186         res = FALSE;
1187       break;
1188     case GST_EVENT_QOS:
1189       /* we can't really (yet) do QoS */
1190       res = FALSE;
1191       break;
1192     case GST_EVENT_NAVIGATION:
1193       /* no navigation either... */
1194       res = FALSE;
1195       break;
1196     default:
1197       res = gst_pad_push_event (dvdemux->sinkpad, event);
1198       event = NULL;
1199       break;
1200   }
1201   if (event)
1202     gst_event_unref (event);
1203
1204   gst_object_unref (dvdemux);
1205
1206   return res;
1207 }
1208
1209 /* does not take ownership of buffer */
1210 static GstFlowReturn
1211 gst_dvdemux_demux_audio (GstDVDemux * dvdemux, GstBuffer * buffer,
1212     guint64 duration)
1213 {
1214   gint num_samples;
1215   GstFlowReturn ret;
1216   const guint8 *data;
1217
1218   data = GST_BUFFER_DATA (buffer);
1219
1220   dv_decode_full_audio (dvdemux->decoder, data, dvdemux->audio_buffers);
1221
1222   if (G_LIKELY ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0)) {
1223     gint16 *a_ptr;
1224     gint i, j;
1225     GstBuffer *outbuf;
1226     gint frequency, channels;
1227
1228     if (G_UNLIKELY (dvdemux->audiosrcpad == NULL))
1229       dvdemux->audiosrcpad = gst_dvdemux_add_pad (dvdemux, &audio_src_temp);
1230
1231     /* get initial format or check if format changed */
1232     frequency = dv_get_frequency (dvdemux->decoder);
1233     channels = dv_get_num_channels (dvdemux->decoder);
1234
1235     if (G_UNLIKELY ((frequency != dvdemux->frequency)
1236             || (channels != dvdemux->channels))) {
1237       GstCaps *caps;
1238
1239       dvdemux->frequency = frequency;
1240       dvdemux->channels = channels;
1241
1242       /* and set new caps */
1243       caps = gst_caps_new_simple ("audio/x-raw-int",
1244           "rate", G_TYPE_INT, frequency,
1245           "depth", G_TYPE_INT, 16,
1246           "width", G_TYPE_INT, 16,
1247           "signed", G_TYPE_BOOLEAN, TRUE,
1248           "channels", G_TYPE_INT, channels,
1249           "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
1250       gst_pad_set_caps (dvdemux->audiosrcpad, caps);
1251       gst_caps_unref (caps);
1252     }
1253
1254     outbuf = gst_buffer_new_and_alloc (num_samples *
1255         sizeof (gint16) * dvdemux->channels);
1256
1257     a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
1258
1259     for (i = 0; i < num_samples; i++) {
1260       for (j = 0; j < dvdemux->channels; j++) {
1261         *(a_ptr++) = dvdemux->audio_buffers[j][i];
1262       }
1263     }
1264
1265     GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
1266         GST_TIME_ARGS (dvdemux->time_segment.last_stop));
1267
1268     GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.last_stop;
1269     GST_BUFFER_DURATION (outbuf) = duration;
1270     GST_BUFFER_OFFSET (outbuf) = dvdemux->audio_offset;
1271     dvdemux->audio_offset += num_samples;
1272     GST_BUFFER_OFFSET_END (outbuf) = dvdemux->audio_offset;
1273
1274     if (dvdemux->new_media)
1275       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1276     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->audiosrcpad));
1277
1278     ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
1279   } else {
1280     /* no samples */
1281     ret = GST_FLOW_OK;
1282   }
1283
1284   return ret;
1285 }
1286
1287 /* takes ownership of buffer */
1288 static GstFlowReturn
1289 gst_dvdemux_demux_video (GstDVDemux * dvdemux, GstBuffer * buffer,
1290     guint64 duration)
1291 {
1292   GstBuffer *outbuf;
1293   gint height;
1294   gboolean wide;
1295   GstFlowReturn ret = GST_FLOW_OK;
1296
1297   if (G_UNLIKELY (dvdemux->videosrcpad == NULL))
1298     dvdemux->videosrcpad = gst_dvdemux_add_pad (dvdemux, &video_src_temp);
1299
1300   /* get params */
1301   /* framerate is already up-to-date */
1302   height = dvdemux->decoder->height;
1303   wide = dv_format_wide (dvdemux->decoder);
1304
1305   /* see if anything changed */
1306   if (G_UNLIKELY ((dvdemux->height != height) || dvdemux->wide != wide)) {
1307     GstCaps *caps;
1308     gint par_x, par_y;
1309
1310     dvdemux->height = height;
1311     dvdemux->wide = wide;
1312
1313     if (dvdemux->decoder->system == e_dv_system_625_50) {
1314       if (wide) {
1315         par_x = PAL_WIDE_PAR_X;
1316         par_y = PAL_WIDE_PAR_Y;
1317       } else {
1318         par_x = PAL_NORMAL_PAR_X;
1319         par_y = PAL_NORMAL_PAR_Y;
1320       }
1321     } else {
1322       if (wide) {
1323         par_x = NTSC_WIDE_PAR_X;
1324         par_y = NTSC_WIDE_PAR_Y;
1325       } else {
1326         par_x = NTSC_NORMAL_PAR_X;
1327         par_y = NTSC_NORMAL_PAR_Y;
1328       }
1329     }
1330
1331     caps = gst_caps_new_simple ("video/x-dv",
1332         "systemstream", G_TYPE_BOOLEAN, FALSE,
1333         "width", G_TYPE_INT, 720,
1334         "height", G_TYPE_INT, height,
1335         "framerate", GST_TYPE_FRACTION, dvdemux->framerate_numerator,
1336         dvdemux->framerate_denominator,
1337         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
1338     gst_pad_set_caps (dvdemux->videosrcpad, caps);
1339     gst_caps_unref (caps);
1340   }
1341
1342   /* takes ownership of buffer here, we just need to modify
1343    * the metadata. */
1344   outbuf = gst_buffer_make_metadata_writable (buffer);
1345
1346   GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->time_segment.last_stop;
1347   GST_BUFFER_OFFSET (outbuf) = dvdemux->video_offset;
1348   GST_BUFFER_OFFSET_END (outbuf) = dvdemux->video_offset + 1;
1349   GST_BUFFER_DURATION (outbuf) = duration;
1350
1351   if (dvdemux->new_media)
1352     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1353   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->videosrcpad));
1354
1355   GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
1356       GST_TIME_ARGS (dvdemux->time_segment.last_stop));
1357
1358   ret = gst_pad_push (dvdemux->videosrcpad, outbuf);
1359
1360   dvdemux->video_offset++;
1361
1362   return ret;
1363 }
1364
1365 static int
1366 get_ssyb_offset (int dif, int ssyb)
1367 {
1368   int offset;
1369
1370   offset = dif * 12000;         /* to dif */
1371   offset += 80 * (1 + (ssyb / 6));      /* to subcode pack */
1372   offset += 3;                  /* past header */
1373   offset += 8 * (ssyb % 6);     /* to ssyb */
1374
1375   return offset;
1376 }
1377
1378 static gboolean
1379 gst_dvdemux_get_timecode (GstDVDemux * dvdemux, GstBuffer * buffer,
1380     GstSMPTETimeCode * timecode)
1381 {
1382   guint8 *data = GST_BUFFER_DATA (buffer);
1383   int offset;
1384   int dif;
1385   int n_difs = dvdemux->decoder->num_dif_seqs;
1386
1387   for (dif = 0; dif < n_difs; dif++) {
1388     offset = get_ssyb_offset (dif, 3);
1389     if (data[offset + 3] == 0x13) {
1390       timecode->frames = ((data[offset + 4] >> 4) & 0x3) * 10 +
1391           (data[offset + 4] & 0xf);
1392       timecode->seconds = ((data[offset + 5] >> 4) & 0x3) * 10 +
1393           (data[offset + 5] & 0xf);
1394       timecode->minutes = ((data[offset + 6] >> 4) & 0x3) * 10 +
1395           (data[offset + 6] & 0xf);
1396       timecode->hours = ((data[offset + 7] >> 4) & 0x3) * 10 +
1397           (data[offset + 7] & 0xf);
1398       GST_DEBUG ("got timecode %" GST_SMPTE_TIME_CODE_FORMAT,
1399           GST_SMPTE_TIME_CODE_ARGS (timecode));
1400       return TRUE;
1401     }
1402   }
1403
1404   return FALSE;
1405 }
1406
1407 static gboolean
1408 gst_dvdemux_is_new_media (GstDVDemux * dvdemux, GstBuffer * buffer)
1409 {
1410   guint8 *data = GST_BUFFER_DATA (buffer);
1411   int aaux_offset;
1412   int dif;
1413   int n_difs;
1414
1415   n_difs = dvdemux->decoder->num_dif_seqs;
1416
1417   for (dif = 0; dif < n_difs; dif++) {
1418     if (dif & 1) {
1419       aaux_offset = (dif * 12000) + (6 + 16 * 1) * 80 + 3;
1420     } else {
1421       aaux_offset = (dif * 12000) + (6 + 16 * 4) * 80 + 3;
1422     }
1423     if (data[aaux_offset + 0] == 0x51) {
1424       if ((data[aaux_offset + 2] & 0x80) == 0)
1425         return TRUE;
1426     }
1427   }
1428
1429   return FALSE;
1430 }
1431
1432 /* takes ownership of buffer */
1433 static GstFlowReturn
1434 gst_dvdemux_demux_frame (GstDVDemux * dvdemux, GstBuffer * buffer)
1435 {
1436   GstClockTime next_ts;
1437   GstFlowReturn aret, vret, ret;
1438   guint8 *data;
1439   guint64 duration;
1440   GstSMPTETimeCode timecode;
1441   int frame_number;
1442
1443   if (G_UNLIKELY (dvdemux->need_segment)) {
1444     GstEvent *event;
1445     GstFormat format;
1446
1447     /* convert to time and store as start/end_timestamp */
1448     format = GST_FORMAT_TIME;
1449     if (!(gst_dvdemux_convert_sink_pair (dvdemux,
1450                 GST_FORMAT_BYTES, dvdemux->byte_segment.start,
1451                 dvdemux->byte_segment.stop, format,
1452                 &dvdemux->time_segment.start, &dvdemux->time_segment.stop)))
1453       goto segment_error;
1454
1455     dvdemux->time_segment.rate = dvdemux->byte_segment.rate;
1456     dvdemux->time_segment.abs_rate = dvdemux->byte_segment.abs_rate;
1457     dvdemux->time_segment.last_stop = dvdemux->time_segment.start;
1458
1459     /* calculate current frame number */
1460     format = GST_FORMAT_DEFAULT;
1461     if (!(gst_dvdemux_src_convert (dvdemux, dvdemux->videosrcpad,
1462                 GST_FORMAT_TIME, dvdemux->time_segment.start,
1463                 &format, &dvdemux->frame_offset)))
1464       goto segment_error;
1465
1466     GST_DEBUG_OBJECT (dvdemux, "sending segment start: %" GST_TIME_FORMAT
1467         ", stop: %" GST_TIME_FORMAT ", time: %" GST_TIME_FORMAT,
1468         GST_TIME_ARGS (dvdemux->time_segment.start),
1469         GST_TIME_ARGS (dvdemux->time_segment.stop),
1470         GST_TIME_ARGS (dvdemux->time_segment.start));
1471
1472     event = gst_event_new_new_segment (FALSE, dvdemux->byte_segment.rate,
1473         GST_FORMAT_TIME, dvdemux->time_segment.start,
1474         dvdemux->time_segment.stop, dvdemux->time_segment.start);
1475     gst_dvdemux_push_event (dvdemux, event);
1476
1477     dvdemux->need_segment = FALSE;
1478   }
1479
1480   gst_dvdemux_get_timecode (dvdemux, buffer, &timecode);
1481   gst_smpte_time_code_get_frame_number (
1482       (dvdemux->decoder->system == e_dv_system_625_50) ?
1483       GST_SMPTE_TIME_CODE_SYSTEM_25 : GST_SMPTE_TIME_CODE_SYSTEM_30,
1484       &frame_number, &timecode);
1485
1486   next_ts = gst_util_uint64_scale_int (
1487       (dvdemux->frame_offset + 1) * GST_SECOND,
1488       dvdemux->framerate_denominator, dvdemux->framerate_numerator);
1489   duration = next_ts - dvdemux->time_segment.last_stop;
1490
1491   data = GST_BUFFER_DATA (buffer);
1492
1493   dv_parse_packs (dvdemux->decoder, data);
1494   dvdemux->new_media = FALSE;
1495   if (gst_dvdemux_is_new_media (dvdemux, buffer) &&
1496       dvdemux->frames_since_new_media > 2) {
1497     dvdemux->new_media = TRUE;
1498     dvdemux->frames_since_new_media = 0;
1499   }
1500   dvdemux->frames_since_new_media++;
1501
1502   /* does not take ownership of buffer */
1503   aret = ret = gst_dvdemux_demux_audio (dvdemux, buffer, duration);
1504   if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)) {
1505     gst_buffer_unref (buffer);
1506     goto done;
1507   }
1508
1509   /* takes ownership of buffer */
1510   vret = ret = gst_dvdemux_demux_video (dvdemux, buffer, duration);
1511   if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED))
1512     goto done;
1513
1514   /* if both are not linked, we stop */
1515   if (G_UNLIKELY (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED)) {
1516     ret = GST_FLOW_NOT_LINKED;
1517     goto done;
1518   }
1519
1520   gst_segment_set_last_stop (&dvdemux->time_segment, GST_FORMAT_TIME, next_ts);
1521   dvdemux->frame_offset++;
1522
1523   /* check for the end of the segment */
1524   if (dvdemux->time_segment.stop != -1 && next_ts > dvdemux->time_segment.stop)
1525     ret = GST_FLOW_UNEXPECTED;
1526   else
1527     ret = GST_FLOW_OK;
1528
1529 done:
1530   return ret;
1531
1532   /* ERRORS */
1533 segment_error:
1534   {
1535     GST_DEBUG ("error generating new_segment event");
1536     gst_buffer_unref (buffer);
1537     return GST_FLOW_ERROR;
1538   }
1539 }
1540
1541 /* flush any remaining data in the adapter, used in chain based scheduling mode */
1542 static GstFlowReturn
1543 gst_dvdemux_flush (GstDVDemux * dvdemux)
1544 {
1545   GstFlowReturn ret = GST_FLOW_OK;
1546
1547   while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
1548     const guint8 *data;
1549     gint length;
1550
1551     /* get the accumulated bytes */
1552     data = gst_adapter_peek (dvdemux->adapter, dvdemux->frame_len);
1553
1554     /* parse header to know the length and other params */
1555     if (G_UNLIKELY (dv_parse_header (dvdemux->decoder, data) < 0))
1556       goto parse_header_error;
1557
1558     /* after parsing the header we know the length of the data */
1559     length = dvdemux->frame_len = dvdemux->decoder->frame_size;
1560     if (dvdemux->decoder->system == e_dv_system_625_50) {
1561       dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
1562       dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
1563     } else {
1564       dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
1565       dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
1566     }
1567     g_atomic_int_set (&dvdemux->found_header, 1);
1568
1569     /* let demux_video set the height, it needs to detect when things change so
1570      * it can reset caps */
1571
1572     /* if we still have enough for a frame, start decoding */
1573     if (G_LIKELY (gst_adapter_available (dvdemux->adapter) >= length)) {
1574       GstBuffer *buffer;
1575
1576       data = gst_adapter_take (dvdemux->adapter, length);
1577
1578       /* create buffer for the remainder of the code */
1579       buffer = gst_buffer_new ();
1580       GST_BUFFER_DATA (buffer) = (guint8 *) data;
1581       GST_BUFFER_SIZE (buffer) = length;
1582       GST_BUFFER_MALLOCDATA (buffer) = (guint8 *) data;
1583
1584       /* and decode the buffer, takes ownership */
1585       ret = gst_dvdemux_demux_frame (dvdemux, buffer);
1586       if (G_UNLIKELY (ret != GST_FLOW_OK))
1587         goto done;
1588     }
1589   }
1590 done:
1591   return ret;
1592
1593   /* ERRORS */
1594 parse_header_error:
1595   {
1596     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1597         (NULL), ("Error parsing DV header"));
1598     return GST_FLOW_ERROR;
1599   }
1600 }
1601
1602 /* streaming operation: 
1603  *
1604  * accumulate data until we have a frame, then decode. 
1605  */
1606 static GstFlowReturn
1607 gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer)
1608 {
1609   GstDVDemux *dvdemux;
1610   GstFlowReturn ret;
1611   GstClockTime timestamp;
1612
1613   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1614
1615   /* a discontinuity in the stream, we need to get rid of
1616    * accumulated data in the adapter and assume a new frame
1617    * starts after the discontinuity */
1618   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)))
1619     gst_adapter_clear (dvdemux->adapter);
1620
1621   /* a timestamp always should be respected */
1622   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1623   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1624     gst_segment_set_last_stop (&dvdemux->time_segment, GST_FORMAT_TIME,
1625         timestamp);
1626     /* FIXME, adjust frame_offset and other counters */
1627   }
1628
1629   gst_adapter_push (dvdemux->adapter, buffer);
1630
1631   /* Apparently dv_parse_header can read from the body of the frame
1632    * too, so it needs more than header_size bytes. Wacky!
1633    */
1634   if (G_UNLIKELY (dvdemux->frame_len == -1)) {
1635     /* if we don't know the length of a frame, we assume it is
1636      * the NTSC_BUFFER length, as this is enough to figure out 
1637      * if this is PAL or NTSC */
1638     dvdemux->frame_len = NTSC_BUFFER;
1639   }
1640
1641   /* and try to flush pending frames */
1642   ret = gst_dvdemux_flush (dvdemux);
1643
1644   gst_object_unref (dvdemux);
1645
1646   return ret;
1647 }
1648
1649 /* pull based operation.
1650  *
1651  * Read header first to figure out the frame size. Then read
1652  * and decode full frames.
1653  */
1654 static void
1655 gst_dvdemux_loop (GstPad * pad)
1656 {
1657   GstFlowReturn ret;
1658   GstDVDemux *dvdemux;
1659   GstBuffer *buffer = NULL;
1660   const guint8 *data;
1661
1662   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1663
1664   if (G_UNLIKELY (g_atomic_int_get (&dvdemux->found_header) == 0)) {
1665     GST_DEBUG_OBJECT (dvdemux, "pulling first buffer");
1666     /* pull in NTSC sized buffer to figure out the frame
1667      * length */
1668     ret = gst_pad_pull_range (dvdemux->sinkpad,
1669         dvdemux->byte_segment.last_stop, NTSC_BUFFER, &buffer);
1670     if (G_UNLIKELY (ret != GST_FLOW_OK))
1671       goto pause;
1672
1673     /* check buffer size, don't want to read small buffers */
1674     if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < NTSC_BUFFER))
1675       goto small_buffer;
1676
1677     data = GST_BUFFER_DATA (buffer);
1678
1679     /* parse header to know the length and other params */
1680     if (G_UNLIKELY (dv_parse_header (dvdemux->decoder, data) < 0))
1681       goto parse_header_error;
1682
1683     /* after parsing the header we know the length of the data */
1684     dvdemux->frame_len = dvdemux->decoder->frame_size;
1685     if (dvdemux->decoder->system == e_dv_system_625_50) {
1686       dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
1687       dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
1688     } else {
1689       dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
1690       dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
1691     }
1692     dvdemux->need_segment = TRUE;
1693
1694     /* see if we need to read a larger part */
1695     if (dvdemux->frame_len != NTSC_BUFFER) {
1696       gst_buffer_unref (buffer);
1697       buffer = NULL;
1698     }
1699
1700     {
1701       GstEvent *event;
1702
1703       /* setting header and prrforming the seek must be atomic */
1704       GST_OBJECT_LOCK (dvdemux);
1705       /* got header now */
1706       g_atomic_int_set (&dvdemux->found_header, 1);
1707
1708       /* now perform pending seek if any. */
1709       event = dvdemux->seek_event;
1710       if (event)
1711         gst_event_ref (event);
1712       GST_OBJECT_UNLOCK (dvdemux);
1713
1714       if (event) {
1715         if (!gst_dvdemux_handle_pull_seek (dvdemux, dvdemux->videosrcpad,
1716                 event)) {
1717           GST_ELEMENT_WARNING (dvdemux, STREAM, DECODE, (NULL),
1718               ("Error perfoming initial seek"));
1719         }
1720         gst_event_unref (event);
1721
1722         /* and we need to pull a new buffer in all cases. */
1723         if (buffer) {
1724           gst_buffer_unref (buffer);
1725           buffer = NULL;
1726         }
1727       }
1728     }
1729   }
1730
1731
1732   if (G_UNLIKELY (dvdemux->pending_segment)) {
1733
1734     /* now send the newsegment */
1735     GST_DEBUG_OBJECT (dvdemux, "Sending newsegment from");
1736
1737     gst_dvdemux_push_event (dvdemux, dvdemux->pending_segment);
1738     dvdemux->pending_segment = NULL;
1739   }
1740
1741   if (G_LIKELY (buffer == NULL)) {
1742     GST_DEBUG_OBJECT (dvdemux, "pulling buffer at offset %" G_GINT64_FORMAT,
1743         dvdemux->byte_segment.last_stop);
1744
1745     ret = gst_pad_pull_range (dvdemux->sinkpad,
1746         dvdemux->byte_segment.last_stop, dvdemux->frame_len, &buffer);
1747     if (ret != GST_FLOW_OK)
1748       goto pause;
1749
1750     /* check buffer size, don't want to read small buffers */
1751     if (GST_BUFFER_SIZE (buffer) < dvdemux->frame_len)
1752       goto small_buffer;
1753   }
1754   /* and decode the buffer */
1755   ret = gst_dvdemux_demux_frame (dvdemux, buffer);
1756   if (G_UNLIKELY (ret != GST_FLOW_OK))
1757     goto pause;
1758
1759   /* and position ourselves for the next buffer */
1760   dvdemux->byte_segment.last_stop += dvdemux->frame_len;
1761
1762 done:
1763   gst_object_unref (dvdemux);
1764
1765   return;
1766
1767   /* ERRORS */
1768 parse_header_error:
1769   {
1770     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1771         (NULL), ("Error parsing DV header"));
1772     gst_buffer_unref (buffer);
1773     dvdemux->running = FALSE;
1774     gst_pad_pause_task (dvdemux->sinkpad);
1775     gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1776     goto done;
1777   }
1778 small_buffer:
1779   {
1780     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1781         (NULL), ("Error reading buffer"));
1782     gst_buffer_unref (buffer);
1783     dvdemux->running = FALSE;
1784     gst_pad_pause_task (dvdemux->sinkpad);
1785     gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1786     goto done;
1787   }
1788 pause:
1789   {
1790     GST_INFO_OBJECT (dvdemux, "pausing task, %s", gst_flow_get_name (ret));
1791     dvdemux->running = FALSE;
1792     gst_pad_pause_task (dvdemux->sinkpad);
1793     if (ret == GST_FLOW_UNEXPECTED) {
1794       GST_LOG_OBJECT (dvdemux, "got eos");
1795       /* perform EOS logic */
1796       if (dvdemux->time_segment.flags & GST_SEEK_FLAG_SEGMENT) {
1797         gst_element_post_message (GST_ELEMENT (dvdemux),
1798             gst_message_new_segment_done (GST_OBJECT_CAST (dvdemux),
1799                 dvdemux->time_segment.format, dvdemux->time_segment.last_stop));
1800       } else {
1801         gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1802       }
1803     } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
1804       /* for fatal errors or not-linked we post an error message */
1805       GST_ELEMENT_ERROR (dvdemux, STREAM, FAILED,
1806           (NULL), ("streaming stopped, reason %s", gst_flow_get_name (ret)));
1807       gst_dvdemux_push_event (dvdemux, gst_event_new_eos ());
1808     }
1809     goto done;
1810   }
1811 }
1812
1813 static gboolean
1814 gst_dvdemux_sink_activate_push (GstPad * sinkpad, gboolean active)
1815 {
1816   GstDVDemux *demux = GST_DVDEMUX (gst_pad_get_parent (sinkpad));
1817
1818   if (active) {
1819     demux->seek_handler = gst_dvdemux_handle_push_seek;
1820   } else {
1821     demux->seek_handler = NULL;
1822   }
1823   gst_object_unref (demux);
1824
1825   return TRUE;
1826 }
1827
1828 static gboolean
1829 gst_dvdemux_sink_activate_pull (GstPad * sinkpad, gboolean active)
1830 {
1831   GstDVDemux *demux = GST_DVDEMUX (gst_pad_get_parent (sinkpad));
1832
1833   if (active) {
1834     demux->running = TRUE;
1835     demux->seek_handler = gst_dvdemux_handle_pull_seek;
1836     gst_pad_start_task (sinkpad, (GstTaskFunction) gst_dvdemux_loop, sinkpad);
1837   } else {
1838     demux->seek_handler = NULL;
1839     gst_pad_stop_task (sinkpad);
1840     demux->running = FALSE;
1841   }
1842
1843   gst_object_unref (demux);
1844
1845   return TRUE;
1846 };
1847
1848 /* decide on push or pull based scheduling */
1849 static gboolean
1850 gst_dvdemux_sink_activate (GstPad * sinkpad)
1851 {
1852   gboolean ret;
1853
1854   if (gst_pad_check_pull_range (sinkpad))
1855     ret = gst_pad_activate_pull (sinkpad, TRUE);
1856   else
1857     ret = gst_pad_activate_push (sinkpad, TRUE);
1858
1859   return ret;
1860 };
1861
1862 static GstStateChangeReturn
1863 gst_dvdemux_change_state (GstElement * element, GstStateChange transition)
1864 {
1865   GstDVDemux *dvdemux = GST_DVDEMUX (element);
1866   GstStateChangeReturn ret;
1867
1868   switch (transition) {
1869     case GST_STATE_CHANGE_NULL_TO_READY:
1870       break;
1871     case GST_STATE_CHANGE_READY_TO_PAUSED:
1872       dvdemux->decoder = dv_decoder_new (0, FALSE, FALSE);
1873       dv_set_error_log (dvdemux->decoder, NULL);
1874       gst_dvdemux_reset (dvdemux);
1875       break;
1876     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1877       break;
1878     default:
1879       break;
1880   }
1881
1882   ret = parent_class->change_state (element, transition);
1883
1884   switch (transition) {
1885     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1886       break;
1887     case GST_STATE_CHANGE_PAUSED_TO_READY:
1888       gst_adapter_clear (dvdemux->adapter);
1889       dv_decoder_free (dvdemux->decoder);
1890       dvdemux->decoder = NULL;
1891
1892       gst_dvdemux_remove_pads (dvdemux);
1893       break;
1894     case GST_STATE_CHANGE_READY_TO_NULL:
1895     {
1896       GstEvent **event_p;
1897
1898       event_p = &dvdemux->seek_event;
1899       gst_event_replace (event_p, NULL);
1900       if (dvdemux->pending_segment)
1901         gst_event_unref (dvdemux->pending_segment);
1902       dvdemux->pending_segment = NULL;
1903       break;
1904     }
1905     default:
1906       break;
1907   }
1908   return ret;
1909 }