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