2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2005> Wim Taymans <wim@fluendo.com>
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.
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.
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.
27 #include <gst/audio/audio.h>
28 #include "gstdvdemux.h"
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
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):
42 * Wide NTSC: 10/11 * (16/9)/(4/3) = 40/33
43 * Wide PAL: 59/54 * (16/9)/(4/3) = 118/81
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.
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.
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.
67 #define NTSC_HEIGHT 480
68 #define NTSC_BUFFER 120000
69 #define NTSC_FRAMERATE_NUMERATOR 30000
70 #define NTSC_FRAMERATE_DENOMINATOR 1001
72 #define PAL_HEIGHT 576
73 #define PAL_BUFFER 144000
74 #define PAL_FRAMERATE_NUMERATOR 25
75 #define PAL_FRAMERATE_DENOMINATOR 1
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
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
87 static GstElementDetails dvdemux_details =
88 GST_ELEMENT_DETAILS ("DV system stream demuxer",
90 "Uses libdv to separate DV audio from DV video",
91 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
93 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
99 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
102 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
105 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
108 GST_STATIC_CAPS ("audio/x-raw-int, "
111 "signed = (boolean) TRUE, "
112 "channels = (int) {2, 4}, "
113 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
114 "rate = (int) { 32000, 44100, 48000 }")
118 GST_BOILERPLATE (GstDVDemux, gst_dvdemux, GstElement, GST_TYPE_ELEMENT);
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);
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);
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);
136 static GstStateChangeReturn gst_dvdemux_change_state (GstElement * element,
137 GstStateChange transition);
141 gst_dvdemux_base_init (gpointer g_class)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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));
152 gst_element_class_set_details (element_class, &dvdemux_details);
156 gst_dvdemux_class_init (GstDVDemuxClass * klass)
158 GstElementClass *gstelement_class;
160 gstelement_class = (GstElementClass *) klass;
162 gstelement_class->change_state = gst_dvdemux_change_state;
164 /* table initialization, only do once */
169 gst_dvdemux_init (GstDVDemux * dvdemux, GstDVDemuxClass * g_class)
174 gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
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);
184 dvdemux->adapter = gst_adapter_new ();
186 dvdemux->timestamp = 0LL;
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;
196 dvdemux->frequency = 0;
197 dvdemux->channels = 0;
198 dvdemux->wide = FALSE;
200 for (i = 0; i < 4; i++) {
201 dvdemux->audio_buffers[i] =
202 (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
207 gst_dvdemux_add_pads (GstDVDemux * dvdemux)
209 dvdemux->videosrcpad =
210 gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
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);
221 dvdemux->audiosrcpad =
222 gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
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);
233 gst_element_no_more_pads (GST_ELEMENT (dvdemux));
237 gst_dvdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
238 GstFormat * dest_format, gint64 * dest_value)
243 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
244 if (dvdemux->frame_len == -1)
247 if (dvdemux->decoder == NULL)
250 if (*dest_format == src_format) {
251 *dest_value = src_value;
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);
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);
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);
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;
292 case GST_FORMAT_DEFAULT:
293 if (pad == dvdemux->videosrcpad) {
295 *dest_value = src_value * dvdemux->framerate_numerator /
296 (dvdemux->framerate_denominator * GST_SECOND);
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));
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) {
318 src_value * GST_SECOND * gst_audio_frame_byte_size (pad)
319 / (2 * dvdemux->frequency * dvdemux->channels);
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);
340 gst_object_unref (dvdemux);
342 GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
343 *dest_format, *dest_value, res);
348 gst_object_unref (dvdemux);
354 gst_dvdemux_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
355 GstFormat * dest_format, gint64 * dest_value)
360 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
362 if (dvdemux->frame_len <= 0)
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);
369 if (*dest_format == GST_FORMAT_DEFAULT)
370 *dest_format = GST_FORMAT_TIME;
372 if (*dest_format == src_format) {
373 *dest_value = src_value;
377 switch (src_format) {
378 case GST_FORMAT_BYTES:
379 switch (*dest_format) {
380 case GST_FORMAT_TIME:
384 /* get frame number */
385 frame = src_value / dvdemux->frame_len;
387 *dest_value = (frame * GST_SECOND * dvdemux->framerate_denominator) /
388 dvdemux->framerate_numerator;
395 case GST_FORMAT_TIME:
396 switch (*dest_format) {
397 case GST_FORMAT_BYTES:
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;
415 GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
416 *dest_format, *dest_value, res);
419 gst_object_unref (dvdemux);
424 gst_object_unref (dvdemux);
429 static const GstQueryType *
430 gst_dvdemux_get_src_query_types (GstPad * pad)
432 static const GstQueryType src_query_types[] = {
439 return src_query_types;
443 gst_dvdemux_src_query (GstPad * pad, GstQuery * query)
448 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
450 switch (GST_QUERY_TYPE (query)) {
451 case GST_QUERY_POSITION:
456 /* get target format */
457 gst_query_parse_position (query, &format, NULL);
459 /* bring the position to the requested format. */
460 if (!(res = gst_pad_query_convert (pad,
461 GST_FORMAT_TIME, dvdemux->timestamp, &format, &cur)))
463 gst_query_set_position (query, format, cur);
466 case GST_QUERY_DURATION:
473 /* get target format */
474 gst_query_parse_duration (query, &format, NULL);
476 /* change query to bytes to perform on peer */
477 gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
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);
486 /* get peer total length */
487 gst_query_parse_duration (query, NULL, &end);
489 gst_object_unref (peer);
491 /* convert end to requested format */
494 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
495 GST_FORMAT_BYTES, end, &format2, &end))) {
502 gst_query_set_duration (query, format, end);
505 case GST_QUERY_CONVERT:
507 GstFormat src_fmt, dest_fmt;
508 gint64 src_val, dest_val;
510 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
512 gst_dvdemux_src_convert (pad, src_fmt, src_val, &dest_fmt,
515 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
519 res = gst_pad_query_default (pad, query);
522 gst_object_unref (dvdemux);
528 gst_object_unref (dvdemux);
529 GST_DEBUG ("error handling event");
534 static const GstQueryType *
535 gst_dvdemux_get_sink_query_types (GstPad * pad)
537 static const GstQueryType sink_query_types[] = {
542 return sink_query_types;
546 gst_dvdemux_sink_query (GstPad * pad, GstQuery * query)
551 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
553 switch (GST_QUERY_TYPE (query)) {
554 case GST_QUERY_CONVERT:
556 GstFormat src_fmt, dest_fmt;
557 gint64 src_val, dest_val;
559 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
561 gst_dvdemux_sink_convert (pad, src_fmt, src_val, &dest_fmt,
564 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
568 res = gst_pad_query_default (pad, query);
571 gst_object_unref (dvdemux);
577 gst_object_unref (dvdemux);
578 GST_DEBUG ("error handling event");
584 gst_dvdemux_send_event (GstDVDemux * dvdemux, GstEvent * event)
586 gboolean res = FALSE;
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);
598 gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event)
600 GstDVDemux *dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
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
608 res = gst_dvdemux_send_event (dvdemux, event);
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);
615 case GST_EVENT_NEWSEGMENT:
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);
623 /* and queue a DISCONT before sending the next set of buffers */
624 dvdemux->need_discont = TRUE;
625 gst_event_unref (event);
630 /* flush any pending data */
631 gst_dvdemux_flush (dvdemux);
633 res = gst_dvdemux_send_event (dvdemux, event);
634 /* and clear the adapter */
635 gst_adapter_clear (dvdemux->adapter);
639 gst_object_unref (dvdemux);
645 gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event)
650 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
652 switch (GST_EVENT_TYPE (event)) {
657 GstFormat format, conv;
660 GstSeekType cur_type, stop_type;
662 gint64 start_position, end_position;
664 gst_event_parse_seek (event, &rate, &format, &flags,
665 &cur_type, &cur, &stop_type, &stop);
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 */
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 */
683 GST_INFO ("Finished conversion of cur, BYTES cur : %lld",
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 */
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 */
705 GST_INFO ("Finished conversion of stop, BYTES cur : %lld",
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);
714 res = gst_pad_push_event (dvdemux->sinkpad, newevent);
721 gst_event_unref (event);
723 gst_object_unref (dvdemux);
729 gst_dvdemux_demux_audio (GstDVDemux * dvdemux, const guint8 * data)
732 gint frequency, channels;
735 frequency = dv_get_frequency (dvdemux->decoder);
736 channels = dv_get_num_channels (dvdemux->decoder);
738 /* check if format changed */
739 if ((frequency != dvdemux->frequency) || (channels != dvdemux->channels)) {
742 dvdemux->frequency = frequency;
743 dvdemux->channels = channels;
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);
757 dv_decode_full_audio (dvdemux->decoder, data, dvdemux->audio_buffers);
759 if ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0) {
764 outbuf = gst_buffer_new_and_alloc (num_samples *
765 sizeof (gint16) * dvdemux->channels);
767 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
769 for (i = 0; i < num_samples; i++) {
770 for (j = 0; j < dvdemux->channels; j++) {
771 *(a_ptr++) = dvdemux->audio_buffers[j][i];
775 GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
776 GST_TIME_ARGS (dvdemux->timestamp));
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;
784 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->audiosrcpad));
786 ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
796 gst_dvdemux_demux_video (GstDVDemux * dvdemux, const guint8 * data)
801 GstFlowReturn ret = GST_FLOW_OK;
804 /* framerate is already up-to-date */
805 height = (dvdemux->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
806 wide = dv_format_wide (dvdemux->decoder);
808 /* see if anything changed */
809 if ((dvdemux->height != height) || dvdemux->wide != wide) {
813 dvdemux->height = height;
814 dvdemux->wide = wide;
818 par_x = PAL_WIDE_PAR_X;
819 par_y = PAL_WIDE_PAR_Y;
821 par_x = PAL_NORMAL_PAR_X;
822 par_y = PAL_NORMAL_PAR_Y;
826 par_x = NTSC_WIDE_PAR_X;
827 par_y = NTSC_WIDE_PAR_Y;
829 par_x = NTSC_NORMAL_PAR_X;
830 par_y = NTSC_NORMAL_PAR_Y;
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);
845 outbuf = gst_buffer_new ();
847 gst_buffer_set_data (outbuf, (guint8 *) data, dvdemux->frame_len);
848 outbuf->malloc_data = GST_BUFFER_DATA (outbuf);
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;
855 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->videosrcpad));
857 GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
858 GST_TIME_ARGS (dvdemux->timestamp));
860 ret = gst_pad_push (dvdemux->videosrcpad, outbuf);
862 dvdemux->video_offset++;
868 gst_dvdemux_demux_frame (GstDVDemux * dvdemux, const guint8 * data)
870 GstClockTime next_ts;
871 GstFlowReturn aret, vret, ret;
873 if (dvdemux->need_discont) {
878 /* convert to time and store as start/end_timestamp */
879 format = GST_FORMAT_TIME;
880 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
882 dvdemux->start_byte, &format, &dvdemux->start_timestamp))) {
885 dvdemux->timestamp = dvdemux->start_timestamp;
887 /* calculate current frame number */
888 format = GST_FORMAT_DEFAULT;
889 if (!(res = gst_pad_query_convert (dvdemux->videosrcpad,
891 dvdemux->start_timestamp, &format, &dvdemux->total_frames))) {
895 if (dvdemux->stop_byte == -1) {
896 dvdemux->stop_timestamp = -1;
898 format = GST_FORMAT_TIME;
899 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
901 dvdemux->stop_byte, &format, &dvdemux->stop_timestamp))) {
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);
911 dvdemux->need_discont = FALSE;
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;
919 dv_parse_packs (dvdemux->decoder, data);
920 if (dv_is_new_recording (dvdemux->decoder, data))
921 dvdemux->new_media = TRUE;
923 aret = ret = gst_dvdemux_demux_audio (dvdemux, data);
924 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
927 vret = ret = gst_dvdemux_demux_video (dvdemux, data);
928 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
931 if (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED) {
932 ret = GST_FLOW_NOT_LINKED;
937 dvdemux->timestamp = next_ts;
938 dvdemux->total_frames++;
945 GST_DEBUG ("error generating discont event");
946 return GST_FLOW_ERROR;
950 /* flush any remaining data in the adapter */
952 gst_dvdemux_flush (GstDVDemux * dvdemux)
954 GstFlowReturn ret = GST_FLOW_OK;
956 while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
960 /* get the accumulated bytes */
961 data = gst_adapter_peek (dvdemux->adapter, dvdemux->frame_len);
963 /* parse header to know the length and other params */
964 if (dv_parse_header (dvdemux->decoder, data) < 0)
965 goto parse_header_error;
967 dvdemux->found_header = TRUE;
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);
973 dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
974 dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
976 dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
977 dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
979 /* let demux_video set the height, it needs to detect when things change so
980 * it can reset caps */
982 /* if we still have enough for a frame, start decoding */
983 if (gst_adapter_available (dvdemux->adapter) >= length) {
985 data = gst_adapter_take (dvdemux->adapter, length);
987 /* and decode the data */
988 ret = gst_dvdemux_demux_frame (dvdemux, data);
990 if (ret != GST_FLOW_OK)
1000 GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1001 ("Error parsing DV header"), ("Error parsing DV header"));
1002 gst_object_unref (dvdemux);
1004 return GST_FLOW_ERROR;
1008 /* streaming operation:
1010 * accumulate data until we have a frame, then decode.
1012 static GstFlowReturn
1013 gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer)
1015 GstDVDemux *dvdemux;
1018 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1020 /* temporary hack? Can't do this from the state change */
1021 if (!dvdemux->videosrcpad)
1022 gst_dvdemux_add_pads (dvdemux);
1024 gst_adapter_push (dvdemux->adapter, buffer);
1026 /* Apparently dv_parse_header can read from the body of the frame
1027 * too, so it needs more than header_size bytes. Wacky!
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;
1036 /* and try to flush pending frames */
1037 ret = gst_dvdemux_flush (dvdemux);
1039 gst_object_unref (dvdemux);
1044 static GstStateChangeReturn
1045 gst_dvdemux_change_state (GstElement * element, GstStateChange transition)
1047 GstDVDemux *dvdemux = GST_DVDEMUX (element);
1048 GstStateChangeReturn ret;
1051 switch (transition) {
1052 case GST_STATE_CHANGE_NULL_TO_READY:
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;
1063 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1069 ret = parent_class->change_state (element, transition);
1071 switch (transition) {
1072 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1074 case GST_STATE_CHANGE_PAUSED_TO_READY:
1075 gst_adapter_clear (dvdemux->adapter);
1076 dv_decoder_free (dvdemux->decoder);
1077 dvdemux->decoder = NULL;
1079 case GST_STATE_CHANGE_READY_TO_NULL: