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