5765aa30bcb2b0bd33d90bbd04a08ce397858589
[platform/upstream/gst-plugins-good.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
30 /* DV output has two modes, normal and wide. The resolution is the same in both
31  * cases: 720 pixels wide by 576 pixels tall in PAL format, and 720x480 for
32  * NTSC.
33  *
34  * Each of the modes has its own pixel aspect ratio, which is fixed in practice
35  * by ITU-R BT.601 (also known as "CCIR-601" or "Rec.601"). Or so claims a
36  * reference that I culled from the reliable "internet",
37  * http://www.mir.com/DMG/aspect.html. Normal PAL is 59/54 and normal NTSC is
38  * 10/11. Because the pixel resolution is the same for both cases, we can get
39  * the pixel aspect ratio for wide recordings by multiplying by the ratio of
40  * display aspect ratios, 16/9 (for wide) divided by 4/3 (for normal):
41  *
42  * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
43  * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
44  *
45  * However, the pixel resolution coming out of a DV source does not combine with
46  * the standard pixel aspect ratios to give a proper display aspect ratio. An
47  * image 480 pixels tall, with a 4:3 display aspect ratio, will be 768 pixels
48  * wide. But, if we take the normal PAL aspect ratio of 59/54, and multiply it
49  * with the width of the DV image (720 pixels), we get 786.666..., which is
50  * nonintegral and too wide. The camera is not outputting a 4:3 image.
51  * 
52  * If the video sink for this stream has fixed dimensions (such as for
53  * fullscreen playback, or for a java applet in a web page), you then have two
54  * choices. Either you show the whole image, but pad the image with black
55  * borders on the top and bottom (like watching a widescreen video on a 4:3
56  * device), or you crop the video to the proper ratio. Apparently the latter is
57  * the standard practice.
58  *
59  * For its part, GStreamer is concerned with accuracy and preservation of
60  * information. This element outputs the 720x576 or 720x480 video that it
61  * recieves, noting the proper aspect ratio. This should not be a problem for
62  * windowed applications, which can change size to fit the video. Applications
63  * with fixed size requirements should decide whether to crop or pad which
64  * an element such as videobox can do.
65  */
66
67 #define NTSC_HEIGHT 480
68 #define NTSC_BUFFER 120000
69 #define NTSC_FRAMERATE_NUMERATOR 30000
70 #define NTSC_FRAMERATE_DENOMINATOR 1001
71
72 #define PAL_HEIGHT 576
73 #define PAL_BUFFER 144000
74 #define PAL_FRAMERATE_NUMERATOR 25
75 #define PAL_FRAMERATE_DENOMINATOR 1
76
77 #define PAL_NORMAL_PAR_X        59
78 #define PAL_NORMAL_PAR_Y        54
79 #define PAL_WIDE_PAR_X          118
80 #define PAL_WIDE_PAR_Y          81
81
82 #define NTSC_NORMAL_PAR_X       10
83 #define NTSC_NORMAL_PAR_Y       11
84 #define NTSC_WIDE_PAR_X         40
85 #define NTSC_WIDE_PAR_Y         33
86
87 static GstElementDetails dvdemux_details =
88 GST_ELEMENT_DETAILS ("DV system stream demuxer",
89     "Codec/Demuxer",
90     "Uses libdv to separate DV audio from DV video",
91     "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
92
93 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
94     GST_PAD_SINK,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
97     );
98
99 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
100     GST_PAD_SRC,
101     GST_PAD_SOMETIMES,
102     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
103     );
104
105 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
106     GST_PAD_SRC,
107     GST_PAD_SOMETIMES,
108     GST_STATIC_CAPS ("audio/x-raw-int, "
109         "depth = (int) 16, "
110         "width = (int) 16, "
111         "signed = (boolean) TRUE, "
112         "channels = (int) {2, 4}, "
113         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
114         "rate = (int) { 32000, 44100, 48000 }")
115     );
116
117
118 GST_BOILERPLATE (GstDVDemux, gst_dvdemux, GstElement, GST_TYPE_ELEMENT);
119
120
121 static const GstQueryType *gst_dvdemux_get_src_query_types (GstPad * pad);
122 static gboolean gst_dvdemux_src_query (GstPad * pad, GstQuery * query);
123 static const GstQueryType *gst_dvdemux_get_sink_query_types (GstPad * pad);
124 static gboolean gst_dvdemux_sink_query (GstPad * pad, GstQuery * query);
125
126 static gboolean gst_dvdemux_sink_convert (GstPad * pad, GstFormat src_format,
127     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
128 static gboolean gst_dvdemux_src_convert (GstPad * pad, GstFormat src_format,
129     gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
130
131 static gboolean gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event);
132 static GstFlowReturn gst_dvdemux_flush (GstDVDemux * dvdemux);
133 static GstFlowReturn gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer);
134 static gboolean gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event);
135
136 static GstStateChangeReturn gst_dvdemux_change_state (GstElement * element,
137     GstStateChange transition);
138
139
140 static void
141 gst_dvdemux_base_init (gpointer g_class)
142 {
143   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
144
145   gst_element_class_add_pad_template (element_class,
146       gst_static_pad_template_get (&sink_temp));
147   gst_element_class_add_pad_template (element_class,
148       gst_static_pad_template_get (&video_src_temp));
149   gst_element_class_add_pad_template (element_class,
150       gst_static_pad_template_get (&audio_src_temp));
151
152   gst_element_class_set_details (element_class, &dvdemux_details);
153 }
154
155 static void
156 gst_dvdemux_class_init (GstDVDemuxClass * klass)
157 {
158   GstElementClass *gstelement_class;
159
160   gstelement_class = (GstElementClass *) klass;
161
162   gstelement_class->change_state = gst_dvdemux_change_state;
163
164   /* table initialization, only do once */
165   dv_init (0, 0);
166 }
167
168 static void
169 gst_dvdemux_init (GstDVDemux * dvdemux, GstDVDemuxClass * g_class)
170 {
171   gint i;
172
173   dvdemux->sinkpad =
174       gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
175       "sink");
176   gst_pad_set_chain_function (dvdemux->sinkpad, gst_dvdemux_chain);
177   gst_pad_set_event_function (dvdemux->sinkpad, gst_dvdemux_handle_sink_event);
178   gst_pad_set_query_function (dvdemux->sinkpad,
179       GST_DEBUG_FUNCPTR (gst_dvdemux_sink_query));
180   gst_pad_set_query_type_function (dvdemux->sinkpad,
181       GST_DEBUG_FUNCPTR (gst_dvdemux_get_sink_query_types));
182   gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->sinkpad);
183
184   dvdemux->adapter = gst_adapter_new ();
185
186   dvdemux->timestamp = 0LL;
187
188   dvdemux->start_timestamp = -1LL;
189   dvdemux->stop_timestamp = -1LL;
190   dvdemux->need_discont = FALSE;
191   dvdemux->new_media = FALSE;
192   dvdemux->framerate_numerator = 0;
193   dvdemux->framerate_denominator = 0;
194   dvdemux->total_frames = 0;
195   dvdemux->height = 0;
196   dvdemux->frequency = 0;
197   dvdemux->channels = 0;
198   dvdemux->wide = FALSE;
199
200   for (i = 0; i < 4; i++) {
201     dvdemux->audio_buffers[i] =
202         (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
203   }
204 }
205
206 static void
207 gst_dvdemux_add_pads (GstDVDemux * dvdemux)
208 {
209   dvdemux->videosrcpad =
210       gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
211       "video");
212   gst_pad_set_query_function (dvdemux->videosrcpad,
213       GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
214   gst_pad_set_query_type_function (dvdemux->videosrcpad,
215       GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
216   gst_pad_set_event_function (dvdemux->videosrcpad,
217       GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
218   gst_pad_use_fixed_caps (dvdemux->videosrcpad);
219   gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->videosrcpad);
220
221   dvdemux->audiosrcpad =
222       gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
223       "audio");
224   gst_pad_set_query_function (dvdemux->audiosrcpad,
225       GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
226   gst_pad_set_query_type_function (dvdemux->audiosrcpad,
227       GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
228   gst_pad_set_event_function (dvdemux->audiosrcpad,
229       GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
230   gst_pad_use_fixed_caps (dvdemux->audiosrcpad);
231   gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->audiosrcpad);
232
233   gst_element_no_more_pads (GST_ELEMENT (dvdemux));
234 }
235
236 static gboolean
237 gst_dvdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
238     GstFormat * dest_format, gint64 * dest_value)
239 {
240   gboolean res = TRUE;
241   GstDVDemux *dvdemux;
242
243   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
244   if (dvdemux->frame_len == -1)
245     goto error;
246
247   if (dvdemux->decoder == NULL)
248     goto error;
249
250   if (*dest_format == src_format) {
251     *dest_value = src_value;
252     goto done;
253   }
254
255   GST_INFO ("pad:%s:%s, src_value:%lld, src_format:%d, dest_format:%d",
256       GST_DEBUG_PAD_NAME (pad), src_value, src_format, *dest_format);
257
258   switch (src_format) {
259     case GST_FORMAT_BYTES:
260       switch (*dest_format) {
261         case GST_FORMAT_DEFAULT:
262           if (pad == dvdemux->videosrcpad)
263             *dest_value = src_value / dvdemux->frame_len;
264           else if (pad == dvdemux->audiosrcpad)
265             *dest_value = src_value / gst_audio_frame_byte_size (pad);
266           break;
267         case GST_FORMAT_TIME:
268           *dest_format = GST_FORMAT_TIME;
269           if (pad == dvdemux->videosrcpad)
270             *dest_value = src_value * GST_SECOND *
271                 dvdemux->framerate_denominator /
272                 (dvdemux->frame_len * dvdemux->framerate_numerator);
273           else if (pad == dvdemux->audiosrcpad)
274             *dest_value = src_value * GST_SECOND /
275                 (2 * dvdemux->frequency * dvdemux->channels);
276           break;
277         default:
278           res = FALSE;
279       }
280       break;
281     case GST_FORMAT_TIME:
282       switch (*dest_format) {
283         case GST_FORMAT_BYTES:
284           if (pad == dvdemux->videosrcpad)
285             *dest_value = src_value * dvdemux->frame_len *
286                 dvdemux->framerate_numerator /
287                 (dvdemux->framerate_denominator * GST_SECOND);
288           else if (pad == dvdemux->audiosrcpad)
289             *dest_value = 2 * src_value * dvdemux->frequency *
290                 dvdemux->channels / GST_SECOND;
291           break;
292         case GST_FORMAT_DEFAULT:
293           if (pad == dvdemux->videosrcpad) {
294             if (src_value)
295               *dest_value = src_value * dvdemux->framerate_numerator /
296                   (dvdemux->framerate_denominator * GST_SECOND);
297             else
298               *dest_value = 0;
299           } else if (pad == dvdemux->audiosrcpad) {
300             *dest_value = 2 * src_value * dvdemux->frequency *
301                 dvdemux->channels / (GST_SECOND *
302                 gst_audio_frame_byte_size (pad));
303           }
304           break;
305         default:
306           res = FALSE;
307       }
308       break;
309     case GST_FORMAT_DEFAULT:
310       switch (*dest_format) {
311         case GST_FORMAT_TIME:
312           if (pad == dvdemux->videosrcpad) {
313             *dest_value = src_value * GST_SECOND *
314                 dvdemux->framerate_denominator / dvdemux->framerate_numerator;
315           } else if (pad == dvdemux->audiosrcpad) {
316             if (src_value)
317               *dest_value =
318                   src_value * GST_SECOND * gst_audio_frame_byte_size (pad)
319                   / (2 * dvdemux->frequency * dvdemux->channels);
320             else
321               *dest_value = 0;
322           }
323           break;
324         case GST_FORMAT_BYTES:
325           if (pad == dvdemux->videosrcpad) {
326             *dest_value = src_value * dvdemux->frame_len;
327           } else if (pad == dvdemux->audiosrcpad) {
328             *dest_value = src_value * gst_audio_frame_byte_size (pad);
329           }
330           break;
331         default:
332           res = FALSE;
333       }
334       break;
335     default:
336       res = FALSE;
337   }
338
339 done:
340   gst_object_unref (dvdemux);
341
342   GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
343       *dest_format, *dest_value, res);
344   return res;
345
346 error:
347   {
348     gst_object_unref (dvdemux);
349     return FALSE;
350   }
351 }
352
353 static gboolean
354 gst_dvdemux_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
355     GstFormat * dest_format, gint64 * dest_value)
356 {
357   gboolean res = TRUE;
358   GstDVDemux *dvdemux;
359
360   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
361
362   if (dvdemux->frame_len <= 0)
363     goto error;
364
365   GST_DEBUG ("%d -> %d", src_format, *dest_format);
366   GST_INFO ("pad:%s:%s, src_value:%lld, src_format:%d, dest_format:%d",
367       GST_DEBUG_PAD_NAME (pad), src_value, src_format, *dest_format);
368
369   if (*dest_format == GST_FORMAT_DEFAULT)
370     *dest_format = GST_FORMAT_TIME;
371
372   if (*dest_format == src_format) {
373     *dest_value = src_value;
374     goto done;
375   }
376
377   switch (src_format) {
378     case GST_FORMAT_BYTES:
379       switch (*dest_format) {
380         case GST_FORMAT_TIME:
381         {
382           guint64 frame;
383
384           /* get frame number */
385           frame = src_value / dvdemux->frame_len;
386
387           *dest_value = (frame * GST_SECOND * dvdemux->framerate_denominator) /
388               dvdemux->framerate_numerator;
389           break;
390         }
391         default:
392           res = FALSE;
393       }
394       break;
395     case GST_FORMAT_TIME:
396       switch (*dest_format) {
397         case GST_FORMAT_BYTES:
398         {
399           guint64 frame;
400
401           /* calculate the frame */
402           frame = src_value * dvdemux->framerate_numerator /
403               (dvdemux->framerate_denominator * GST_SECOND);
404           /* calculate the offset */
405           *dest_value = frame * dvdemux->frame_len;
406           break;
407         }
408         default:
409           res = FALSE;
410       }
411       break;
412     default:
413       res = FALSE;
414   }
415   GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
416       *dest_format, *dest_value, res);
417
418 done:
419   gst_object_unref (dvdemux);
420   return res;
421
422 error:
423   {
424     gst_object_unref (dvdemux);
425     return FALSE;
426   }
427 }
428
429 static const GstQueryType *
430 gst_dvdemux_get_src_query_types (GstPad * pad)
431 {
432   static const GstQueryType src_query_types[] = {
433     GST_QUERY_POSITION,
434     GST_QUERY_DURATION,
435     GST_QUERY_CONVERT,
436     0
437   };
438
439   return src_query_types;
440 }
441
442 static gboolean
443 gst_dvdemux_src_query (GstPad * pad, GstQuery * query)
444 {
445   gboolean res = TRUE;
446   GstDVDemux *dvdemux;
447
448   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
449
450   switch (GST_QUERY_TYPE (query)) {
451     case GST_QUERY_POSITION:
452     {
453       GstFormat format;
454       gint64 cur;
455
456       /* get target format */
457       gst_query_parse_position (query, &format, NULL);
458
459       /* bring the position to the requested format. */
460       if (!(res = gst_pad_query_convert (pad,
461                   GST_FORMAT_TIME, dvdemux->timestamp, &format, &cur)))
462         goto error;
463       gst_query_set_position (query, format, cur);
464       break;
465     }
466     case GST_QUERY_DURATION:
467     {
468       GstFormat format;
469       GstFormat format2;
470       gint64 end;
471       GstPad *peer;
472
473       /* get target format */
474       gst_query_parse_duration (query, &format, NULL);
475
476       /* change query to bytes to perform on peer */
477       gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
478
479       if ((peer = gst_pad_get_peer (dvdemux->sinkpad))) {
480         /* ask peer for total length */
481         if (!(res = gst_pad_query (peer, query))) {
482           gst_object_unref (peer);
483           goto error;
484         }
485
486         /* get peer total length */
487         gst_query_parse_duration (query, NULL, &end);
488
489         gst_object_unref (peer);
490
491         /* convert end to requested format */
492         if (end != -1) {
493           format2 = format;
494           if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
495                       GST_FORMAT_BYTES, end, &format2, &end))) {
496             goto error;
497           }
498         }
499       } else {
500         end = -1;
501       }
502       gst_query_set_duration (query, format, end);
503       break;
504     }
505     case GST_QUERY_CONVERT:
506     {
507       GstFormat src_fmt, dest_fmt;
508       gint64 src_val, dest_val;
509
510       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
511       if (!(res =
512               gst_dvdemux_src_convert (pad, src_fmt, src_val, &dest_fmt,
513                   &dest_val)))
514         goto error;
515       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
516       break;
517     }
518     default:
519       res = gst_pad_query_default (pad, query);
520       break;
521   }
522   gst_object_unref (dvdemux);
523
524   return res;
525
526 error:
527   {
528     gst_object_unref (dvdemux);
529     GST_DEBUG ("error handling event");
530     return FALSE;
531   }
532 }
533
534 static const GstQueryType *
535 gst_dvdemux_get_sink_query_types (GstPad * pad)
536 {
537   static const GstQueryType sink_query_types[] = {
538     GST_QUERY_CONVERT,
539     0
540   };
541
542   return sink_query_types;
543 }
544
545 static gboolean
546 gst_dvdemux_sink_query (GstPad * pad, GstQuery * query)
547 {
548   gboolean res = TRUE;
549   GstDVDemux *dvdemux;
550
551   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
552
553   switch (GST_QUERY_TYPE (query)) {
554     case GST_QUERY_CONVERT:
555     {
556       GstFormat src_fmt, dest_fmt;
557       gint64 src_val, dest_val;
558
559       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
560       if (!(res =
561               gst_dvdemux_sink_convert (pad, src_fmt, src_val, &dest_fmt,
562                   &dest_val)))
563         goto error;
564       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
565       break;
566     }
567     default:
568       res = gst_pad_query_default (pad, query);
569       break;
570   }
571   gst_object_unref (dvdemux);
572
573   return res;
574
575 error:
576   {
577     gst_object_unref (dvdemux);
578     GST_DEBUG ("error handling event");
579     return FALSE;
580   }
581 }
582
583 static gboolean
584 gst_dvdemux_send_event (GstDVDemux * dvdemux, GstEvent * event)
585 {
586   gboolean res = FALSE;
587
588   gst_event_ref (event);
589   if (dvdemux->videosrcpad)
590     res |= gst_pad_push_event (dvdemux->videosrcpad, event);
591   if (dvdemux->audiosrcpad)
592     res |= gst_pad_push_event (dvdemux->audiosrcpad, event);
593
594   return res;
595 }
596
597 static gboolean
598 gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event)
599 {
600   GstDVDemux *dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
601   gboolean res = TRUE;
602
603   switch (GST_EVENT_TYPE (event)) {
604     case GST_EVENT_FLUSH_START:
605       /* we are not blocking on anything exect the push() calls
606        * to the peer which will be unblocked by forwarding the
607        * event.*/
608       res = gst_dvdemux_send_event (dvdemux, event);
609       break;
610     case GST_EVENT_FLUSH_STOP:
611       gst_adapter_clear (dvdemux->adapter);
612       GST_DEBUG ("cleared adapter");
613       res = gst_dvdemux_send_event (dvdemux, event);
614       break;
615     case GST_EVENT_NEWSEGMENT:
616     {
617       GstFormat format;
618
619       /* parse byte start and stop positions */
620       gst_event_parse_new_segment (event, NULL, NULL, &format,
621           &dvdemux->start_byte, &dvdemux->stop_byte, NULL);
622
623       /* and queue a DISCONT before sending the next set of buffers */
624       dvdemux->need_discont = TRUE;
625       gst_event_unref (event);
626       break;
627     }
628     case GST_EVENT_EOS:
629     default:
630       /* flush any pending data */
631       gst_dvdemux_flush (dvdemux);
632       /* forward event */
633       res = gst_dvdemux_send_event (dvdemux, event);
634       /* and clear the adapter */
635       gst_adapter_clear (dvdemux->adapter);
636       break;
637   }
638
639   gst_object_unref (dvdemux);
640
641   return res;
642 }
643
644 static gboolean
645 gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event)
646 {
647   gboolean res = TRUE;
648   GstDVDemux *dvdemux;
649
650   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
651
652   switch (GST_EVENT_TYPE (event)) {
653     case GST_EVENT_SEEK:
654     {
655       GstEvent *newevent;
656       gint64 offset;
657       GstFormat format, conv;
658       gint64 cur, stop;
659       gdouble rate;
660       GstSeekType cur_type, stop_type;
661       GstSeekFlags flags;
662       gint64 start_position, end_position;
663
664       gst_event_parse_seek (event, &rate, &format, &flags,
665           &cur_type, &cur, &stop_type, &stop);
666
667       if ((offset = cur) != -1) {
668         GST_INFO ("starting conversion of cur");
669         /* bring the format to time on srcpad. */
670         conv = GST_FORMAT_TIME;
671         if (!(res = gst_pad_query_convert (pad,
672                     format, offset, &conv, &start_position))) {
673           /* could not convert seek format to time offset */
674           break;
675         }
676         /* and convert to bytes on sinkpad. */
677         conv = GST_FORMAT_BYTES;
678         if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
679                     GST_FORMAT_TIME, start_position, &conv, &start_position))) {
680           /* could not convert time format to bytes offset */
681           break;
682         }
683         GST_INFO ("Finished conversion of cur, BYTES cur : %lld",
684             start_position);
685       } else {
686         start_position = -1;
687       }
688
689       if ((offset = stop) != -1) {
690         GST_INFO ("starting conversion of stop");
691         /* bring the format to time on srcpad. */
692         conv = GST_FORMAT_TIME;
693         if (!(res = gst_pad_query_convert (pad,
694                     format, offset, &conv, &end_position))) {
695           /* could not convert seek format to time offset */
696           break;
697         }
698         conv = GST_FORMAT_BYTES;
699         /* and convert to bytes on sinkpad. */
700         if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
701                     GST_FORMAT_TIME, end_position, &conv, &end_position))) {
702           /* could not convert seek format to bytes offset */
703           break;
704         }
705         GST_INFO ("Finished conversion of stop, BYTES cur : %lld",
706             start_position);
707       } else {
708         end_position = -1;
709       }
710       /* now this is the updated seek event on bytes */
711       newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
712           cur_type, start_position, stop_type, end_position);
713
714       res = gst_pad_push_event (dvdemux->sinkpad, newevent);
715       break;
716     }
717     default:
718       res = FALSE;
719       break;
720   }
721   gst_event_unref (event);
722
723   gst_object_unref (dvdemux);
724
725   return res;
726 }
727
728 static GstFlowReturn
729 gst_dvdemux_demux_audio (GstDVDemux * dvdemux, const guint8 * data)
730 {
731   gint num_samples;
732   gint frequency, channels;
733   GstFlowReturn ret;
734
735   frequency = dv_get_frequency (dvdemux->decoder);
736   channels = dv_get_num_channels (dvdemux->decoder);
737
738   /* check if format changed */
739   if ((frequency != dvdemux->frequency) || (channels != dvdemux->channels)) {
740     GstCaps *caps;
741
742     dvdemux->frequency = frequency;
743     dvdemux->channels = channels;
744
745     /* and set new caps */
746     caps = gst_caps_new_simple ("audio/x-raw-int",
747         "rate", G_TYPE_INT, frequency,
748         "depth", G_TYPE_INT, 16,
749         "width", G_TYPE_INT, 16,
750         "signed", G_TYPE_BOOLEAN, TRUE,
751         "channels", G_TYPE_INT, channels,
752         "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
753     gst_pad_set_caps (dvdemux->audiosrcpad, caps);
754     gst_caps_unref (caps);
755   }
756
757   dv_decode_full_audio (dvdemux->decoder, data, dvdemux->audio_buffers);
758
759   if ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0) {
760     gint16 *a_ptr;
761     gint i, j;
762     GstBuffer *outbuf;
763
764     outbuf = gst_buffer_new_and_alloc (num_samples *
765         sizeof (gint16) * dvdemux->channels);
766
767     a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
768
769     for (i = 0; i < num_samples; i++) {
770       for (j = 0; j < dvdemux->channels; j++) {
771         *(a_ptr++) = dvdemux->audio_buffers[j][i];
772       }
773     }
774
775     GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
776         GST_TIME_ARGS (dvdemux->timestamp));
777
778     GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->timestamp;
779     GST_BUFFER_DURATION (outbuf) = dvdemux->duration;
780     GST_BUFFER_OFFSET (outbuf) = dvdemux->audio_offset;
781     dvdemux->audio_offset += num_samples;
782     GST_BUFFER_OFFSET_END (outbuf) = dvdemux->audio_offset;
783
784     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->audiosrcpad));
785
786     ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
787   } else {
788     /* no samples */
789     ret = GST_FLOW_OK;
790   }
791
792   return ret;
793 }
794
795 static GstFlowReturn
796 gst_dvdemux_demux_video (GstDVDemux * dvdemux, const guint8 * data)
797 {
798   GstBuffer *outbuf;
799   gint height;
800   gboolean wide;
801   GstFlowReturn ret = GST_FLOW_OK;
802
803   /* get params */
804   /* framerate is already up-to-date */
805   height = (dvdemux->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
806   wide = dv_format_wide (dvdemux->decoder);
807
808   /* see if anything changed */
809   if ((dvdemux->height != height) || dvdemux->wide != wide) {
810     GstCaps *caps;
811     gint par_x, par_y;
812
813     dvdemux->height = height;
814     dvdemux->wide = wide;
815
816     if (dvdemux->PAL) {
817       if (wide) {
818         par_x = PAL_WIDE_PAR_X;
819         par_y = PAL_WIDE_PAR_Y;
820       } else {
821         par_x = PAL_NORMAL_PAR_X;
822         par_y = PAL_NORMAL_PAR_Y;
823       }
824     } else {
825       if (wide) {
826         par_x = NTSC_WIDE_PAR_X;
827         par_y = NTSC_WIDE_PAR_Y;
828       } else {
829         par_x = NTSC_NORMAL_PAR_X;
830         par_y = NTSC_NORMAL_PAR_Y;
831       }
832     }
833
834     caps = gst_caps_new_simple ("video/x-dv",
835         "systemstream", G_TYPE_BOOLEAN, FALSE,
836         "width", G_TYPE_INT, 720,
837         "height", G_TYPE_INT, height,
838         "framerate", GST_TYPE_FRACTION, dvdemux->framerate_numerator,
839         dvdemux->framerate_denominator,
840         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
841     gst_pad_set_caps (dvdemux->videosrcpad, caps);
842     gst_caps_unref (caps);
843   }
844
845   outbuf = gst_buffer_new ();
846
847   gst_buffer_set_data (outbuf, (guint8 *) data, dvdemux->frame_len);
848   outbuf->malloc_data = GST_BUFFER_DATA (outbuf);
849
850   GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->timestamp;
851   GST_BUFFER_OFFSET (outbuf) = dvdemux->video_offset;
852   GST_BUFFER_OFFSET_END (outbuf) = dvdemux->video_offset + 1;
853   GST_BUFFER_DURATION (outbuf) = dvdemux->duration;
854
855   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->videosrcpad));
856
857   GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
858       GST_TIME_ARGS (dvdemux->timestamp));
859
860   ret = gst_pad_push (dvdemux->videosrcpad, outbuf);
861
862   dvdemux->video_offset++;
863
864   return ret;
865 }
866
867 static GstFlowReturn
868 gst_dvdemux_demux_frame (GstDVDemux * dvdemux, const guint8 * data)
869 {
870   GstClockTime next_ts;
871   GstFlowReturn aret, vret, ret;
872
873   if (dvdemux->need_discont) {
874     GstEvent *event;
875     GstFormat format;
876     gboolean res;
877
878     /* convert to time and store as start/end_timestamp */
879     format = GST_FORMAT_TIME;
880     if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
881                 GST_FORMAT_BYTES,
882                 dvdemux->start_byte, &format, &dvdemux->start_timestamp))) {
883       goto discont_error;
884     }
885     dvdemux->timestamp = dvdemux->start_timestamp;
886
887     /* calculate current frame number */
888     format = GST_FORMAT_DEFAULT;
889     if (!(res = gst_pad_query_convert (dvdemux->videosrcpad,
890                 GST_FORMAT_TIME,
891                 dvdemux->start_timestamp, &format, &dvdemux->total_frames))) {
892       goto discont_error;
893     }
894
895     if (dvdemux->stop_byte == -1) {
896       dvdemux->stop_timestamp = -1;
897     } else {
898       format = GST_FORMAT_TIME;
899       if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
900                   GST_FORMAT_BYTES,
901                   dvdemux->stop_byte, &format, &dvdemux->stop_timestamp))) {
902         goto discont_error;
903       }
904     }
905
906     event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
907         dvdemux->start_timestamp, dvdemux->stop_timestamp,
908         dvdemux->start_timestamp);
909     gst_dvdemux_send_event (dvdemux, event);
910
911     dvdemux->need_discont = FALSE;
912   }
913
914   next_ts = gst_util_uint64_scale_int (
915       (dvdemux->total_frames + 1) * GST_SECOND,
916       dvdemux->framerate_denominator, dvdemux->framerate_numerator);
917   dvdemux->duration = next_ts - dvdemux->timestamp;
918
919   dv_parse_packs (dvdemux->decoder, data);
920   if (dv_is_new_recording (dvdemux->decoder, data))
921     dvdemux->new_media = TRUE;
922
923   aret = ret = gst_dvdemux_demux_audio (dvdemux, data);
924   if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
925     goto done;
926
927   vret = ret = gst_dvdemux_demux_video (dvdemux, data);
928   if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
929     goto done;
930
931   if (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED) {
932     ret = GST_FLOW_NOT_LINKED;
933     goto done;
934   }
935
936   ret = GST_FLOW_OK;
937   dvdemux->timestamp = next_ts;
938   dvdemux->total_frames++;
939
940 done:
941   return ret;
942
943 discont_error:
944   {
945     GST_DEBUG ("error generating discont event");
946     return GST_FLOW_ERROR;
947   }
948 }
949
950 /* flush any remaining data in the adapter */
951 static GstFlowReturn
952 gst_dvdemux_flush (GstDVDemux * dvdemux)
953 {
954   GstFlowReturn ret = GST_FLOW_OK;
955
956   while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
957     const guint8 *data;
958     gint length;
959
960     /* get the accumulated bytes */
961     data = gst_adapter_peek (dvdemux->adapter, dvdemux->frame_len);
962
963     /* parse header to know the length and other params */
964     if (dv_parse_header (dvdemux->decoder, data) < 0)
965       goto parse_header_error;
966
967     dvdemux->found_header = TRUE;
968
969     /* after parsing the header we know the length of the data */
970     dvdemux->PAL = dv_system_50_fields (dvdemux->decoder);
971     length = dvdemux->frame_len = (dvdemux->PAL ? PAL_BUFFER : NTSC_BUFFER);
972     if (dvdemux->PAL) {
973       dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
974       dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
975     } else {
976       dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
977       dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
978     }
979     /* let demux_video set the height, it needs to detect when things change so
980      * it can reset caps */
981
982     /* if we still have enough for a frame, start decoding */
983     if (gst_adapter_available (dvdemux->adapter) >= length) {
984
985       data = gst_adapter_take (dvdemux->adapter, length);
986
987       /* and decode the data */
988       ret = gst_dvdemux_demux_frame (dvdemux, data);
989
990       if (ret != GST_FLOW_OK)
991         goto done;
992     }
993   }
994 done:
995   return ret;
996
997   /* ERRORS */
998 parse_header_error:
999   {
1000     GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1001         ("Error parsing DV header"), ("Error parsing DV header"));
1002     gst_object_unref (dvdemux);
1003
1004     return GST_FLOW_ERROR;
1005   }
1006 }
1007
1008 /* streaming operation: 
1009  *
1010  * accumulate data until we have a frame, then decode. 
1011  */
1012 static GstFlowReturn
1013 gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer)
1014 {
1015   GstDVDemux *dvdemux;
1016   GstFlowReturn ret;
1017
1018   dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1019
1020   /* temporary hack? Can't do this from the state change */
1021   if (!dvdemux->videosrcpad)
1022     gst_dvdemux_add_pads (dvdemux);
1023
1024   gst_adapter_push (dvdemux->adapter, buffer);
1025
1026   /* Apparently dv_parse_header can read from the body of the frame
1027    * too, so it needs more than header_size bytes. Wacky!
1028    */
1029   if (dvdemux->frame_len == -1) {
1030     /* if we don't know the length of a frame, we assume it is
1031      * the NTSC_BUFFER length, as this is enough to figure out 
1032      * if this is PAL or NTSC */
1033     dvdemux->frame_len = NTSC_BUFFER;
1034   }
1035
1036   /* and try to flush pending frames */
1037   ret = gst_dvdemux_flush (dvdemux);
1038
1039   gst_object_unref (dvdemux);
1040
1041   return ret;
1042 }
1043
1044 static GstStateChangeReturn
1045 gst_dvdemux_change_state (GstElement * element, GstStateChange transition)
1046 {
1047   GstDVDemux *dvdemux = GST_DVDEMUX (element);
1048   GstStateChangeReturn ret;
1049
1050
1051   switch (transition) {
1052     case GST_STATE_CHANGE_NULL_TO_READY:
1053       break;
1054     case GST_STATE_CHANGE_READY_TO_PAUSED:
1055       dvdemux->decoder = dv_decoder_new (0, FALSE, FALSE);
1056       dv_set_error_log (dvdemux->decoder, NULL);
1057       dvdemux->audio_offset = 0;
1058       dvdemux->video_offset = 0;
1059       dvdemux->framecount = 0;
1060       dvdemux->found_header = FALSE;
1061       dvdemux->frame_len = -1;
1062       break;
1063     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1064       break;
1065     default:
1066       break;
1067   }
1068
1069   ret = parent_class->change_state (element, transition);
1070
1071   switch (transition) {
1072     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1073       break;
1074     case GST_STATE_CHANGE_PAUSED_TO_READY:
1075       gst_adapter_clear (dvdemux->adapter);
1076       dv_decoder_free (dvdemux->decoder);
1077       dvdemux->decoder = NULL;
1078       break;
1079     case GST_STATE_CHANGE_READY_TO_NULL:
1080       break;
1081     default:
1082       break;
1083   }
1084   return ret;
1085 }