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 GST_DEBUG_CATEGORY (dvdemux_debug);
88 #define GST_CAT_DEFAULT dvdemux_debug
90 static GstElementDetails dvdemux_details =
91 GST_ELEMENT_DETAILS ("DV system stream demuxer",
93 "Uses libdv to separate DV audio from DV video",
94 "Erik Walthinsen <omega@cse.ogi.edu>, Wim Taymans <wim@fluendo.com>");
96 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) true")
102 static GstStaticPadTemplate video_src_temp = GST_STATIC_PAD_TEMPLATE ("video",
105 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
108 static GstStaticPadTemplate audio_src_temp = GST_STATIC_PAD_TEMPLATE ("audio",
111 GST_STATIC_CAPS ("audio/x-raw-int, "
114 "signed = (boolean) TRUE, "
115 "channels = (int) {2, 4}, "
116 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
117 "rate = (int) { 32000, 44100, 48000 }")
121 GST_BOILERPLATE (GstDVDemux, gst_dvdemux, GstElement, GST_TYPE_ELEMENT);
124 static const GstQueryType *gst_dvdemux_get_src_query_types (GstPad * pad);
125 static gboolean gst_dvdemux_src_query (GstPad * pad, GstQuery * query);
126 static const GstQueryType *gst_dvdemux_get_sink_query_types (GstPad * pad);
127 static gboolean gst_dvdemux_sink_query (GstPad * pad, GstQuery * query);
129 static gboolean gst_dvdemux_sink_convert (GstPad * pad, GstFormat src_format,
130 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
131 static gboolean gst_dvdemux_src_convert (GstPad * pad, GstFormat src_format,
132 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
134 static gboolean gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event);
135 static GstFlowReturn gst_dvdemux_flush (GstDVDemux * dvdemux);
136 static GstFlowReturn gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer);
137 static gboolean gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event);
139 static GstStateChangeReturn gst_dvdemux_change_state (GstElement * element,
140 GstStateChange transition);
144 gst_dvdemux_base_init (gpointer g_class)
146 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
148 gst_element_class_add_pad_template (element_class,
149 gst_static_pad_template_get (&sink_temp));
150 gst_element_class_add_pad_template (element_class,
151 gst_static_pad_template_get (&video_src_temp));
152 gst_element_class_add_pad_template (element_class,
153 gst_static_pad_template_get (&audio_src_temp));
155 gst_element_class_set_details (element_class, &dvdemux_details);
157 GST_DEBUG_CATEGORY_INIT (dvdemux_debug, "dvdemux", 0, "DV demuxer element");
161 gst_dvdemux_class_init (GstDVDemuxClass * klass)
163 GstElementClass *gstelement_class;
165 gstelement_class = (GstElementClass *) klass;
167 gstelement_class->change_state = gst_dvdemux_change_state;
169 /* table initialization, only do once */
174 gst_dvdemux_init (GstDVDemux * dvdemux, GstDVDemuxClass * g_class)
179 gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
181 gst_pad_set_chain_function (dvdemux->sinkpad, gst_dvdemux_chain);
182 gst_pad_set_event_function (dvdemux->sinkpad, gst_dvdemux_handle_sink_event);
183 gst_pad_set_query_function (dvdemux->sinkpad,
184 GST_DEBUG_FUNCPTR (gst_dvdemux_sink_query));
185 gst_pad_set_query_type_function (dvdemux->sinkpad,
186 GST_DEBUG_FUNCPTR (gst_dvdemux_get_sink_query_types));
187 gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->sinkpad);
189 dvdemux->adapter = gst_adapter_new ();
191 for (i = 0; i < 4; i++) {
192 dvdemux->audio_buffers[i] =
193 (gint16 *) g_malloc (DV_AUDIO_MAX_SAMPLES * sizeof (gint16));
198 gst_dvdemux_add_pads (GstDVDemux * dvdemux)
200 dvdemux->videosrcpad =
201 gst_pad_new_from_template (gst_static_pad_template_get (&video_src_temp),
203 gst_pad_set_query_function (dvdemux->videosrcpad,
204 GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
205 gst_pad_set_query_type_function (dvdemux->videosrcpad,
206 GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
207 gst_pad_set_event_function (dvdemux->videosrcpad,
208 GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
209 gst_pad_use_fixed_caps (dvdemux->videosrcpad);
210 gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->videosrcpad);
212 dvdemux->audiosrcpad =
213 gst_pad_new_from_template (gst_static_pad_template_get (&audio_src_temp),
215 gst_pad_set_query_function (dvdemux->audiosrcpad,
216 GST_DEBUG_FUNCPTR (gst_dvdemux_src_query));
217 gst_pad_set_query_type_function (dvdemux->audiosrcpad,
218 GST_DEBUG_FUNCPTR (gst_dvdemux_get_src_query_types));
219 gst_pad_set_event_function (dvdemux->audiosrcpad,
220 GST_DEBUG_FUNCPTR (gst_dvdemux_handle_src_event));
221 gst_pad_use_fixed_caps (dvdemux->audiosrcpad);
222 gst_element_add_pad (GST_ELEMENT (dvdemux), dvdemux->audiosrcpad);
224 gst_element_no_more_pads (GST_ELEMENT (dvdemux));
228 gst_dvdemux_src_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
229 GstFormat * dest_format, gint64 * dest_value)
234 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
235 if (dvdemux->frame_len == -1)
238 if (dvdemux->decoder == NULL)
241 if (*dest_format == src_format) {
242 *dest_value = src_value;
246 GST_INFO ("pad:%s:%s, src_value:%lld, src_format:%d, dest_format:%d",
247 GST_DEBUG_PAD_NAME (pad), src_value, src_format, *dest_format);
249 switch (src_format) {
250 case GST_FORMAT_BYTES:
251 switch (*dest_format) {
252 case GST_FORMAT_DEFAULT:
253 if (pad == dvdemux->videosrcpad)
254 *dest_value = src_value / dvdemux->frame_len;
255 else if (pad == dvdemux->audiosrcpad)
256 *dest_value = src_value / gst_audio_frame_byte_size (pad);
258 case GST_FORMAT_TIME:
259 *dest_format = GST_FORMAT_TIME;
260 if (pad == dvdemux->videosrcpad)
261 *dest_value = gst_util_uint64_scale (src_value,
262 GST_SECOND * dvdemux->framerate_denominator,
263 dvdemux->frame_len * dvdemux->framerate_numerator);
264 else if (pad == dvdemux->audiosrcpad)
265 *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
266 2 * dvdemux->frequency * dvdemux->channels);
272 case GST_FORMAT_TIME:
273 switch (*dest_format) {
274 case GST_FORMAT_BYTES:
275 if (pad == dvdemux->videosrcpad)
276 *dest_value = gst_util_uint64_scale (src_value,
277 dvdemux->frame_len * dvdemux->framerate_numerator,
278 dvdemux->framerate_denominator * GST_SECOND);
279 else if (pad == dvdemux->audiosrcpad)
280 *dest_value = gst_util_uint64_scale_int (src_value,
281 2 * dvdemux->frequency * dvdemux->channels, GST_SECOND);
283 case GST_FORMAT_DEFAULT:
284 if (pad == dvdemux->videosrcpad) {
286 *dest_value = gst_util_uint64_scale (src_value,
287 dvdemux->framerate_numerator,
288 dvdemux->framerate_denominator * GST_SECOND);
291 } else if (pad == dvdemux->audiosrcpad) {
292 *dest_value = gst_util_uint64_scale (src_value,
293 2 * dvdemux->frequency * dvdemux->channels,
294 GST_SECOND * gst_audio_frame_byte_size (pad));
301 case GST_FORMAT_DEFAULT:
302 switch (*dest_format) {
303 case GST_FORMAT_TIME:
304 if (pad == dvdemux->videosrcpad) {
305 *dest_value = gst_util_uint64_scale (src_value,
306 GST_SECOND * dvdemux->framerate_denominator,
307 dvdemux->framerate_numerator);
308 } else if (pad == dvdemux->audiosrcpad) {
310 *dest_value = gst_util_uint64_scale (src_value,
311 GST_SECOND * gst_audio_frame_byte_size (pad),
312 2 * dvdemux->frequency * dvdemux->channels);
317 case GST_FORMAT_BYTES:
318 if (pad == dvdemux->videosrcpad) {
319 *dest_value = src_value * dvdemux->frame_len;
320 } else if (pad == dvdemux->audiosrcpad) {
321 *dest_value = src_value * gst_audio_frame_byte_size (pad);
333 gst_object_unref (dvdemux);
335 GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
336 *dest_format, *dest_value, res);
342 GST_INFO ("source conversion failed");
343 gst_object_unref (dvdemux);
349 gst_dvdemux_sink_convert (GstPad * pad, GstFormat src_format, gint64 src_value,
350 GstFormat * dest_format, gint64 * dest_value)
355 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
357 if (dvdemux->frame_len <= 0)
360 GST_DEBUG ("%d -> %d", src_format, *dest_format);
361 GST_INFO ("pad:%s:%s, src_value:%lld, src_format:%d, dest_format:%d",
362 GST_DEBUG_PAD_NAME (pad), src_value, src_format, *dest_format);
364 if (*dest_format == GST_FORMAT_DEFAULT)
365 *dest_format = GST_FORMAT_TIME;
367 if (*dest_format == src_format) {
368 *dest_value = src_value;
372 switch (src_format) {
373 case GST_FORMAT_BYTES:
374 switch (*dest_format) {
375 case GST_FORMAT_TIME:
379 /* get frame number */
380 frame = src_value / dvdemux->frame_len;
382 *dest_value = gst_util_uint64_scale (frame,
383 GST_SECOND * dvdemux->framerate_denominator,
384 dvdemux->framerate_numerator);
391 case GST_FORMAT_TIME:
392 switch (*dest_format) {
393 case GST_FORMAT_BYTES:
397 /* calculate the frame */
399 gst_util_uint64_scale (src_value, dvdemux->framerate_numerator,
400 dvdemux->framerate_denominator * GST_SECOND);
402 /* calculate the offset */
403 *dest_value = frame * dvdemux->frame_len;
413 GST_INFO ("Result : dest_format:%d, dest_value:%lld, res:%d",
414 *dest_format, *dest_value, res);
417 gst_object_unref (dvdemux);
422 GST_INFO ("sink conversion failed");
423 gst_object_unref (dvdemux);
428 static const GstQueryType *
429 gst_dvdemux_get_src_query_types (GstPad * pad)
431 static const GstQueryType src_query_types[] = {
438 return src_query_types;
442 gst_dvdemux_src_query (GstPad * pad, GstQuery * query)
447 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
449 switch (GST_QUERY_TYPE (query)) {
450 case GST_QUERY_POSITION:
455 /* get target format */
456 gst_query_parse_position (query, &format, NULL);
458 /* bring the position to the requested format. */
459 if (!(res = gst_pad_query_convert (pad,
460 GST_FORMAT_TIME, dvdemux->timestamp, &format, &cur)))
462 gst_query_set_position (query, format, cur);
465 case GST_QUERY_DURATION:
472 /* get target format */
473 gst_query_parse_duration (query, &format, NULL);
475 /* change query to bytes to perform on peer */
476 gst_query_set_duration (query, GST_FORMAT_BYTES, -1);
478 if ((peer = gst_pad_get_peer (dvdemux->sinkpad))) {
479 /* ask peer for total length */
480 if (!(res = gst_pad_query (peer, query))) {
481 gst_object_unref (peer);
485 /* get peer total length */
486 gst_query_parse_duration (query, NULL, &end);
488 gst_object_unref (peer);
490 /* convert end to requested format */
493 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
494 GST_FORMAT_BYTES, end, &format2, &end))) {
501 gst_query_set_duration (query, format, end);
504 case GST_QUERY_CONVERT:
506 GstFormat src_fmt, dest_fmt;
507 gint64 src_val, dest_val;
509 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
511 gst_dvdemux_src_convert (pad, src_fmt, src_val, &dest_fmt,
514 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
518 res = gst_pad_query_default (pad, query);
521 gst_object_unref (dvdemux);
527 gst_object_unref (dvdemux);
528 GST_DEBUG ("error source query");
533 static const GstQueryType *
534 gst_dvdemux_get_sink_query_types (GstPad * pad)
536 static const GstQueryType sink_query_types[] = {
541 return sink_query_types;
545 gst_dvdemux_sink_query (GstPad * pad, GstQuery * query)
550 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
552 switch (GST_QUERY_TYPE (query)) {
553 case GST_QUERY_CONVERT:
555 GstFormat src_fmt, dest_fmt;
556 gint64 src_val, dest_val;
558 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
560 gst_dvdemux_sink_convert (pad, src_fmt, src_val, &dest_fmt,
563 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
567 res = gst_pad_query_default (pad, query);
570 gst_object_unref (dvdemux);
576 gst_object_unref (dvdemux);
577 GST_DEBUG ("error handling sink query");
583 gst_dvdemux_send_event (GstDVDemux * dvdemux, GstEvent * event)
585 gboolean res = FALSE;
587 gst_event_ref (event);
588 if (dvdemux->videosrcpad)
589 res |= gst_pad_push_event (dvdemux->videosrcpad, event);
590 if (dvdemux->audiosrcpad)
591 res |= gst_pad_push_event (dvdemux->audiosrcpad, event);
597 gst_dvdemux_handle_sink_event (GstPad * pad, GstEvent * event)
599 GstDVDemux *dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
602 switch (GST_EVENT_TYPE (event)) {
603 case GST_EVENT_FLUSH_START:
604 /* we are not blocking on anything exect the push() calls
605 * to the peer which will be unblocked by forwarding the
607 res = gst_dvdemux_send_event (dvdemux, event);
609 case GST_EVENT_FLUSH_STOP:
610 gst_adapter_clear (dvdemux->adapter);
611 GST_DEBUG ("cleared adapter");
612 res = gst_dvdemux_send_event (dvdemux, event);
613 gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
614 gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
616 case GST_EVENT_NEWSEGMENT:
621 gint64 start, stop, time;
623 /* parse byte start and stop positions */
624 gst_event_parse_new_segment (event, &update, &rate, &format,
625 &start, &stop, &time);
628 case GST_FORMAT_BYTES:
629 gst_segment_set_newsegment (&dvdemux->byte_segment, update,
630 rate, format, start, stop, time);
632 /* and queue a SEGMENT before sending the next set of buffers, we
633 * cannot convert to time yet as we might not know the size of the
635 dvdemux->need_segment = TRUE;
636 gst_event_unref (event);
638 case GST_FORMAT_TIME:
639 gst_segment_set_newsegment (&dvdemux->time_segment, update,
640 rate, format, start, stop, time);
642 /* and we can just forward this time event */
643 res = gst_dvdemux_send_event (dvdemux, event);
646 gst_event_unref (event);
647 /* cannot accept this format */
654 /* flush any pending data */
655 gst_dvdemux_flush (dvdemux);
657 res = gst_dvdemux_send_event (dvdemux, event);
658 /* and clear the adapter */
659 gst_adapter_clear (dvdemux->adapter);
662 res = gst_dvdemux_send_event (dvdemux, event);
666 gst_object_unref (dvdemux);
672 gst_dvdemux_handle_src_event (GstPad * pad, GstEvent * event)
677 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
679 switch (GST_EVENT_TYPE (event)) {
684 GstFormat format, conv;
687 GstSeekType cur_type, stop_type;
689 gint64 start_position, end_position;
691 gst_event_parse_seek (event, &rate, &format, &flags,
692 &cur_type, &cur, &stop_type, &stop);
694 if ((offset = cur) != -1) {
695 GST_INFO ("starting conversion of cur");
696 /* bring the format to time on srcpad. */
697 conv = GST_FORMAT_TIME;
698 if (!(res = gst_pad_query_convert (pad,
699 format, offset, &conv, &start_position))) {
700 /* could not convert seek format to time offset */
703 /* and convert to bytes on sinkpad. */
704 conv = GST_FORMAT_BYTES;
705 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
706 GST_FORMAT_TIME, start_position, &conv, &start_position))) {
707 /* could not convert time format to bytes offset */
710 GST_INFO ("Finished conversion of cur, BYTES cur : %lld",
716 if ((offset = stop) != -1) {
717 GST_INFO ("starting conversion of stop");
718 /* bring the format to time on srcpad. */
719 conv = GST_FORMAT_TIME;
720 if (!(res = gst_pad_query_convert (pad,
721 format, offset, &conv, &end_position))) {
722 /* could not convert seek format to time offset */
725 conv = GST_FORMAT_BYTES;
726 /* and convert to bytes on sinkpad. */
727 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
728 GST_FORMAT_TIME, end_position, &conv, &end_position))) {
729 /* could not convert seek format to bytes offset */
732 GST_INFO ("Finished conversion of stop, BYTES cur : %lld",
737 /* now this is the updated seek event on bytes */
738 newevent = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
739 cur_type, start_position, stop_type, end_position);
741 res = gst_pad_push_event (dvdemux->sinkpad, newevent);
748 gst_event_unref (event);
750 gst_object_unref (dvdemux);
756 gst_dvdemux_demux_audio (GstDVDemux * dvdemux, const guint8 * data)
759 gint frequency, channels;
762 frequency = dv_get_frequency (dvdemux->decoder);
763 channels = dv_get_num_channels (dvdemux->decoder);
765 /* check if format changed */
766 if ((frequency != dvdemux->frequency) || (channels != dvdemux->channels)) {
769 dvdemux->frequency = frequency;
770 dvdemux->channels = channels;
772 /* and set new caps */
773 caps = gst_caps_new_simple ("audio/x-raw-int",
774 "rate", G_TYPE_INT, frequency,
775 "depth", G_TYPE_INT, 16,
776 "width", G_TYPE_INT, 16,
777 "signed", G_TYPE_BOOLEAN, TRUE,
778 "channels", G_TYPE_INT, channels,
779 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
780 gst_pad_set_caps (dvdemux->audiosrcpad, caps);
781 gst_caps_unref (caps);
784 dv_decode_full_audio (dvdemux->decoder, data, dvdemux->audio_buffers);
786 if ((num_samples = dv_get_num_samples (dvdemux->decoder)) > 0) {
791 outbuf = gst_buffer_new_and_alloc (num_samples *
792 sizeof (gint16) * dvdemux->channels);
794 a_ptr = (gint16 *) GST_BUFFER_DATA (outbuf);
796 for (i = 0; i < num_samples; i++) {
797 for (j = 0; j < dvdemux->channels; j++) {
798 *(a_ptr++) = dvdemux->audio_buffers[j][i];
802 GST_DEBUG ("pushing audio %" GST_TIME_FORMAT,
803 GST_TIME_ARGS (dvdemux->timestamp));
805 GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->timestamp;
806 GST_BUFFER_DURATION (outbuf) = dvdemux->duration;
807 GST_BUFFER_OFFSET (outbuf) = dvdemux->audio_offset;
808 dvdemux->audio_offset += num_samples;
809 GST_BUFFER_OFFSET_END (outbuf) = dvdemux->audio_offset;
811 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->audiosrcpad));
813 ret = gst_pad_push (dvdemux->audiosrcpad, outbuf);
823 gst_dvdemux_demux_video (GstDVDemux * dvdemux, const guint8 * data)
828 GstFlowReturn ret = GST_FLOW_OK;
831 /* framerate is already up-to-date */
832 height = (dvdemux->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
833 wide = dv_format_wide (dvdemux->decoder);
835 /* see if anything changed */
836 if ((dvdemux->height != height) || dvdemux->wide != wide) {
840 dvdemux->height = height;
841 dvdemux->wide = wide;
845 par_x = PAL_WIDE_PAR_X;
846 par_y = PAL_WIDE_PAR_Y;
848 par_x = PAL_NORMAL_PAR_X;
849 par_y = PAL_NORMAL_PAR_Y;
853 par_x = NTSC_WIDE_PAR_X;
854 par_y = NTSC_WIDE_PAR_Y;
856 par_x = NTSC_NORMAL_PAR_X;
857 par_y = NTSC_NORMAL_PAR_Y;
861 caps = gst_caps_new_simple ("video/x-dv",
862 "systemstream", G_TYPE_BOOLEAN, FALSE,
863 "width", G_TYPE_INT, 720,
864 "height", G_TYPE_INT, height,
865 "framerate", GST_TYPE_FRACTION, dvdemux->framerate_numerator,
866 dvdemux->framerate_denominator,
867 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_x, par_y, NULL);
868 gst_pad_set_caps (dvdemux->videosrcpad, caps);
869 gst_caps_unref (caps);
872 outbuf = gst_buffer_new ();
874 gst_buffer_set_data (outbuf, (guint8 *) data, dvdemux->frame_len);
875 outbuf->malloc_data = GST_BUFFER_DATA (outbuf);
877 GST_BUFFER_TIMESTAMP (outbuf) = dvdemux->timestamp;
878 GST_BUFFER_OFFSET (outbuf) = dvdemux->video_offset;
879 GST_BUFFER_OFFSET_END (outbuf) = dvdemux->video_offset + 1;
880 GST_BUFFER_DURATION (outbuf) = dvdemux->duration;
882 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dvdemux->videosrcpad));
884 GST_DEBUG ("pushing video %" GST_TIME_FORMAT,
885 GST_TIME_ARGS (dvdemux->timestamp));
887 ret = gst_pad_push (dvdemux->videosrcpad, outbuf);
889 dvdemux->video_offset++;
895 gst_dvdemux_demux_frame (GstDVDemux * dvdemux, const guint8 * data)
897 GstClockTime next_ts;
898 GstFlowReturn aret, vret, ret;
900 if (dvdemux->need_segment) {
905 /* convert to time and store as start/end_timestamp */
906 format = GST_FORMAT_TIME;
907 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
908 GST_FORMAT_BYTES, dvdemux->byte_segment.start,
909 &format, &dvdemux->time_segment.start))) {
912 dvdemux->timestamp = dvdemux->time_segment.start;
914 /* calculate current frame number */
915 format = GST_FORMAT_DEFAULT;
916 if (!(res = gst_pad_query_convert (dvdemux->videosrcpad,
917 GST_FORMAT_TIME, dvdemux->time_segment.start,
918 &format, &dvdemux->total_frames))) {
922 if (dvdemux->byte_segment.stop == -1) {
923 dvdemux->time_segment.stop = -1;
925 format = GST_FORMAT_TIME;
926 if (!(res = gst_pad_query_convert (dvdemux->sinkpad,
927 GST_FORMAT_BYTES, dvdemux->byte_segment.stop,
928 &format, &dvdemux->time_segment.stop))) {
933 GST_DEBUG_OBJECT (dvdemux, "sending segment start: %" GST_TIME_FORMAT
934 ", stop: %" GST_TIME_FORMAT ", time: %" GST_TIME_FORMAT,
935 dvdemux->time_segment.start, dvdemux->time_segment.stop,
936 dvdemux->time_segment.start);
938 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_TIME,
939 dvdemux->time_segment.start, dvdemux->time_segment.stop,
940 dvdemux->time_segment.start);
941 gst_dvdemux_send_event (dvdemux, event);
943 dvdemux->need_segment = FALSE;
946 next_ts = gst_util_uint64_scale_int (
947 (dvdemux->total_frames + 1) * GST_SECOND,
948 dvdemux->framerate_denominator, dvdemux->framerate_numerator);
949 dvdemux->duration = next_ts - dvdemux->timestamp;
951 dv_parse_packs (dvdemux->decoder, data);
952 if (dv_is_new_recording (dvdemux->decoder, data))
953 dvdemux->new_media = TRUE;
955 aret = ret = gst_dvdemux_demux_audio (dvdemux, data);
956 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
959 vret = ret = gst_dvdemux_demux_video (dvdemux, data);
960 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED)
963 if (aret == GST_FLOW_NOT_LINKED && vret == GST_FLOW_NOT_LINKED) {
964 ret = GST_FLOW_NOT_LINKED;
969 dvdemux->timestamp = next_ts;
970 dvdemux->total_frames++;
977 GST_DEBUG ("error generating new_segment event");
978 return GST_FLOW_ERROR;
982 /* flush any remaining data in the adapter */
984 gst_dvdemux_flush (GstDVDemux * dvdemux)
986 GstFlowReturn ret = GST_FLOW_OK;
988 while (gst_adapter_available (dvdemux->adapter) >= dvdemux->frame_len) {
992 /* get the accumulated bytes */
993 data = gst_adapter_peek (dvdemux->adapter, dvdemux->frame_len);
995 /* parse header to know the length and other params */
996 if (dv_parse_header (dvdemux->decoder, data) < 0)
997 goto parse_header_error;
999 dvdemux->found_header = TRUE;
1001 /* after parsing the header we know the length of the data */
1002 dvdemux->PAL = dv_system_50_fields (dvdemux->decoder);
1003 length = dvdemux->frame_len = (dvdemux->PAL ? PAL_BUFFER : NTSC_BUFFER);
1005 dvdemux->framerate_numerator = PAL_FRAMERATE_NUMERATOR;
1006 dvdemux->framerate_denominator = PAL_FRAMERATE_DENOMINATOR;
1008 dvdemux->framerate_numerator = NTSC_FRAMERATE_NUMERATOR;
1009 dvdemux->framerate_denominator = NTSC_FRAMERATE_DENOMINATOR;
1011 /* let demux_video set the height, it needs to detect when things change so
1012 * it can reset caps */
1014 /* if we still have enough for a frame, start decoding */
1015 if (gst_adapter_available (dvdemux->adapter) >= length) {
1017 data = gst_adapter_take (dvdemux->adapter, length);
1019 /* and decode the data */
1020 ret = gst_dvdemux_demux_frame (dvdemux, data);
1022 if (ret != GST_FLOW_OK)
1032 GST_ELEMENT_ERROR (dvdemux, STREAM, DECODE,
1033 ("Error parsing DV header"), ("Error parsing DV header"));
1035 return GST_FLOW_ERROR;
1039 /* streaming operation:
1041 * accumulate data until we have a frame, then decode.
1043 static GstFlowReturn
1044 gst_dvdemux_chain (GstPad * pad, GstBuffer * buffer)
1046 GstDVDemux *dvdemux;
1049 dvdemux = GST_DVDEMUX (gst_pad_get_parent (pad));
1051 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
1052 gst_adapter_clear (dvdemux->adapter);
1054 /* temporary hack? Can't do this from the state change */
1055 if (!dvdemux->videosrcpad)
1056 gst_dvdemux_add_pads (dvdemux);
1058 gst_adapter_push (dvdemux->adapter, buffer);
1060 /* Apparently dv_parse_header can read from the body of the frame
1061 * too, so it needs more than header_size bytes. Wacky!
1063 if (dvdemux->frame_len == -1) {
1064 /* if we don't know the length of a frame, we assume it is
1065 * the NTSC_BUFFER length, as this is enough to figure out
1066 * if this is PAL or NTSC */
1067 dvdemux->frame_len = NTSC_BUFFER;
1070 /* and try to flush pending frames */
1071 ret = gst_dvdemux_flush (dvdemux);
1073 gst_object_unref (dvdemux);
1078 static GstStateChangeReturn
1079 gst_dvdemux_change_state (GstElement * element, GstStateChange transition)
1081 GstDVDemux *dvdemux = GST_DVDEMUX (element);
1082 GstStateChangeReturn ret;
1085 switch (transition) {
1086 case GST_STATE_CHANGE_NULL_TO_READY:
1088 case GST_STATE_CHANGE_READY_TO_PAUSED:
1089 dvdemux->decoder = dv_decoder_new (0, FALSE, FALSE);
1090 dv_set_error_log (dvdemux->decoder, NULL);
1091 dvdemux->audio_offset = 0;
1092 dvdemux->video_offset = 0;
1093 dvdemux->framecount = 0;
1094 dvdemux->found_header = FALSE;
1095 dvdemux->frame_len = -1;
1096 dvdemux->timestamp = 0LL;
1097 dvdemux->need_segment = FALSE;
1098 dvdemux->new_media = FALSE;
1099 dvdemux->framerate_numerator = 0;
1100 dvdemux->framerate_denominator = 0;
1101 dvdemux->total_frames = 0;
1102 dvdemux->height = 0;
1103 dvdemux->frequency = 0;
1104 dvdemux->channels = 0;
1105 dvdemux->wide = FALSE;
1106 gst_segment_init (&dvdemux->byte_segment, GST_FORMAT_BYTES);
1107 gst_segment_init (&dvdemux->time_segment, GST_FORMAT_TIME);
1109 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1115 ret = parent_class->change_state (element, transition);
1117 switch (transition) {
1118 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1120 case GST_STATE_CHANGE_PAUSED_TO_READY:
1121 gst_adapter_clear (dvdemux->adapter);
1122 dv_decoder_free (dvdemux->decoder);
1123 dvdemux->decoder = NULL;
1125 case GST_STATE_CHANGE_READY_TO_NULL: