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.
22 * SECTION:element-dvdec
24 * dvdec decodes DV video into raw video. The element expects a full DV frame
25 * as input, which is 120000 bytes for NTSC and 144000 for PAL video.
27 * This element can perform simple frame dropping with the #GstDVDec:drop-factor
28 * property. Setting this property to a value N > 1 will only decode every
32 * <title>Example launch line</title>
34 * gst-launch filesrc location=test.dv ! dvdemux name=demux ! dvdec ! xvimagesink
35 * ]| This pipeline decodes and renders the raw DV stream to a videosink.
38 * Last reviewed on 2006-02-28 (0.10.3)
50 static const GstElementDetails dvdec_details =
51 GST_ELEMENT_DETAILS ("DV video decoder",
52 "Codec/Decoder/Video",
53 "Uses libdv to decode DV video (smpte314) (libdv.sourceforge.net)",
54 "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
56 /* sizes of one input buffer */
57 #define NTSC_HEIGHT 480
58 #define NTSC_BUFFER 120000
59 #define NTSC_FRAMERATE_NUMERATOR 30000
60 #define NTSC_FRAMERATE_DENOMINATOR 1001
62 #define PAL_HEIGHT 576
63 #define PAL_BUFFER 144000
64 #define PAL_FRAMERATE_NUMERATOR 25
65 #define PAL_FRAMERATE_DENOMINATOR 1
67 #define PAL_NORMAL_PAR_X 59
68 #define PAL_NORMAL_PAR_Y 54
69 #define PAL_WIDE_PAR_X 118
70 #define PAL_WIDE_PAR_Y 81
72 #define NTSC_NORMAL_PAR_X 10
73 #define NTSC_NORMAL_PAR_Y 11
74 #define NTSC_WIDE_PAR_X 40
75 #define NTSC_WIDE_PAR_Y 33
77 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
78 #define DV_DEFAULT_DECODE_NTH 1
80 GST_DEBUG_CATEGORY_STATIC (dvdec_debug);
81 #define GST_CAT_DEFAULT dvdec_debug
92 const gint qualities[] = {
96 DV_QUALITY_DC | DV_QUALITY_COLOR,
97 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
98 DV_QUALITY_AC_2 | DV_QUALITY_COLOR
101 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
104 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
107 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
110 GST_STATIC_CAPS ("video/x-raw-yuv, "
111 "format = (fourcc) YUY2, "
112 "width = (int) 720, "
113 "framerate = (fraction) [ 1/1, 60/1 ];"
117 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
118 "red_mask = (int) 0x0000ff00, "
119 "green_mask = (int) 0x00ff0000, "
120 "blue_mask = (int) 0xff000000, "
121 "width = (int) 720, "
122 "framerate = (fraction) [ 1/1, 60/1 ];"
126 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
127 "red_mask = (int) 0x00ff0000, "
128 "green_mask = (int) 0x0000ff00, "
129 "blue_mask = (int) 0x000000ff, "
130 "width = (int) 720, " "framerate = (fraction) [ 1/1, 60/1 ]")
133 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
135 gst_dvdec_quality_get_type (void)
137 static GType qtype = 0;
140 static const GEnumValue values[] = {
141 {0, "Monochrome, DC (Fastest)", "fastest"},
142 {1, "Monochrome, first AC coefficient", "monochrome-ac"},
143 {2, "Monochrome, highest quality", "monochrome-best"},
144 {3, "Colour, DC, fastest", "colour-fastest"},
145 {4, "Colour, using only the first AC coefficient", "colour-ac"},
146 {5, "Highest quality colour decoding", "best"},
150 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
155 GST_BOILERPLATE (GstDVDec, gst_dvdec, GstElement, GST_TYPE_ELEMENT);
157 static void gst_dvdec_finalize (GObject * object);
158 static gboolean gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps);
159 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
160 static gboolean gst_dvdec_sink_event (GstPad * pad, GstEvent * event);
162 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
163 GstStateChange transition);
165 static void gst_dvdec_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec);
167 static void gst_dvdec_get_property (GObject * object, guint prop_id,
168 GValue * value, GParamSpec * pspec);
171 gst_dvdec_base_init (gpointer g_class)
173 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
175 gst_element_class_add_pad_template (element_class,
176 gst_static_pad_template_get (&sink_temp));
177 gst_element_class_add_pad_template (element_class,
178 gst_static_pad_template_get (&src_temp));
180 gst_element_class_set_details (element_class, &dvdec_details);
182 GST_DEBUG_CATEGORY_INIT (dvdec_debug, "dvdec", 0, "DV decoding element");
186 gst_dvdec_class_init (GstDVDecClass * klass)
188 GObjectClass *gobject_class;
189 GstElementClass *gstelement_class;
191 gobject_class = (GObjectClass *) klass;
192 gstelement_class = (GstElementClass *) klass;
194 gobject_class->finalize = gst_dvdec_finalize;
195 gobject_class->set_property = gst_dvdec_set_property;
196 gobject_class->get_property = gst_dvdec_get_property;
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
199 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
200 FALSE, G_PARAM_READWRITE));
201 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
202 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
203 FALSE, G_PARAM_READWRITE));
204 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
205 g_param_spec_enum ("quality", "Quality", "Decoding quality",
206 GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
207 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
208 g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
209 1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
211 gstelement_class->change_state = gst_dvdec_change_state;
215 gst_dvdec_init (GstDVDec * dvdec, GstDVDecClass * g_class)
217 dvdec->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
218 gst_pad_set_setcaps_function (dvdec->sinkpad,
219 GST_DEBUG_FUNCPTR (gst_dvdec_sink_setcaps));
220 gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
221 gst_pad_set_event_function (dvdec->sinkpad, gst_dvdec_sink_event);
222 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
224 dvdec->srcpad = gst_pad_new_from_static_template (&src_temp, "src");
225 gst_pad_use_fixed_caps (dvdec->srcpad);
226 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
228 dvdec->framerate_numerator = 0;
229 dvdec->framerate_denominator = 0;
231 dvdec->drop_factor = 1;
233 dvdec->clamp_luma = FALSE;
234 dvdec->clamp_chroma = FALSE;
235 dvdec->quality = DV_DEFAULT_QUALITY;
236 dvdec->segment = gst_segment_new ();
240 gst_dvdec_finalize (GObject * object)
242 GstDVDec *dvdec = GST_DVDEC (object);
244 gst_segment_free (dvdec->segment);
246 G_OBJECT_CLASS (parent_class)->finalize (object);
250 gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps)
254 const GValue *par = NULL, *rate = NULL;
256 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
258 /* first parse the caps */
259 s = gst_caps_get_structure (caps, 0);
261 /* we allow framerate and PAR to be overwritten. framerate is mandatory. */
262 if (!(rate = gst_structure_get_value (s, "framerate")))
264 par = gst_structure_get_value (s, "pixel-aspect-ratio");
267 dvdec->par_x = gst_value_get_fraction_numerator (par);
268 dvdec->par_y = gst_value_get_fraction_denominator (par);
269 dvdec->need_par = FALSE;
273 dvdec->need_par = TRUE;
275 dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
276 dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
277 dvdec->sink_negotiated = TRUE;
278 dvdec->src_negotiated = FALSE;
280 gst_object_unref (dvdec);
287 GST_DEBUG_OBJECT (dvdec, "no framerate specified in caps");
288 gst_object_unref (dvdec);
294 gst_dvdec_src_negotiate (GstDVDec * dvdec)
298 /* no PAR was specified in input, derive from encoded data */
299 if (dvdec->need_par) {
302 dvdec->par_x = PAL_WIDE_PAR_X;
303 dvdec->par_y = PAL_WIDE_PAR_Y;
305 dvdec->par_x = PAL_NORMAL_PAR_X;
306 dvdec->par_y = PAL_NORMAL_PAR_Y;
310 dvdec->par_x = NTSC_WIDE_PAR_X;
311 dvdec->par_y = NTSC_WIDE_PAR_Y;
313 dvdec->par_x = NTSC_NORMAL_PAR_X;
314 dvdec->par_y = NTSC_NORMAL_PAR_Y;
317 GST_DEBUG_OBJECT (dvdec, "Inferred PAR %d/%d from video format",
318 dvdec->par_x, dvdec->par_y);
321 /* ignoring rgb, bgr0 for now */
324 othercaps = gst_caps_new_simple ("video/x-raw-yuv",
325 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
326 "width", G_TYPE_INT, 720,
327 "height", G_TYPE_INT, dvdec->height,
328 "framerate", GST_TYPE_FRACTION, dvdec->framerate_numerator,
329 dvdec->framerate_denominator,
330 "pixel-aspect-ratio", GST_TYPE_FRACTION, dvdec->par_x,
333 gst_pad_set_caps (dvdec->srcpad, othercaps);
334 gst_caps_unref (othercaps);
336 dvdec->src_negotiated = TRUE;
342 gst_dvdec_sink_event (GstPad * pad, GstEvent * event)
347 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
349 switch (GST_EVENT_TYPE (event)) {
350 case GST_EVENT_FLUSH_STOP:
351 gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
353 case GST_EVENT_NEWSEGMENT:{
355 gdouble rate, applied_rate;
357 gint64 start, stop, position;
359 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
360 &format, &start, &stop, &position);
362 GST_DEBUG_OBJECT (dvdec, "Got NEWSEGMENT [%" GST_TIME_FORMAT
363 " - %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "]",
364 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
365 GST_TIME_ARGS (position));
367 gst_segment_set_newsegment_full (dvdec->segment, update, rate,
368 applied_rate, format, start, stop, position);
375 res = gst_pad_push_event (dvdec->srcpad, event);
381 gst_dvdec_chain (GstPad * pad, GstBuffer * buf)
386 guint8 *outframe_ptrs[3];
387 gint outframe_pitches[3];
389 GstFlowReturn ret = GST_FLOW_OK;
391 gint64 cstart, cstop;
394 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
395 inframe = GST_BUFFER_DATA (buf);
397 /* buffer should be at least the size of one NTSC frame, this should
398 * be enough to decode the header. */
399 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < NTSC_BUFFER))
402 /* preliminary dropping. unref and return if outside of configured segment */
403 if ((dvdec->segment->format == GST_FORMAT_TIME) &&
404 (!(gst_segment_clip (dvdec->segment, GST_FORMAT_TIME,
405 GST_BUFFER_TIMESTAMP (buf),
406 GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf),
410 if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0))
411 goto parse_header_error;
414 PAL = dv_system_50_fields (dvdec->decoder);
415 wide = dv_format_wide (dvdec->decoder);
417 /* check the buffer is of right size after we know if we are
418 * dealing with PAL or NTSC */
419 length = (PAL ? PAL_BUFFER : NTSC_BUFFER);
420 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < length))
423 dv_parse_packs (dvdec->decoder, inframe);
425 if (dvdec->video_offset % dvdec->drop_factor != 0)
428 /* renegotiate on change */
429 if (PAL != dvdec->PAL || wide != dvdec->wide) {
430 dvdec->src_negotiated = FALSE;
435 dvdec->height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
438 /* negotiate if not done yet */
439 if (!dvdec->src_negotiated) {
440 if (!gst_dvdec_src_negotiate (dvdec))
445 gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0,
446 (720 * dvdec->height) * dvdec->bpp,
447 GST_PAD_CAPS (dvdec->srcpad), &outbuf);
448 if (G_UNLIKELY (ret != GST_FLOW_OK))
451 outframe = GST_BUFFER_DATA (outbuf);
453 outframe_ptrs[0] = outframe;
454 outframe_pitches[0] = 720 * dvdec->bpp;
456 /* the rest only matters for YUY2 */
457 if (dvdec->bpp < 3) {
458 outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height;
459 outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height;
461 outframe_pitches[1] = dvdec->height / 2;
462 outframe_pitches[2] = outframe_pitches[1];
465 GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
466 dv_decode_full_frame (dvdec->decoder, inframe,
467 e_dv_color_yuv, outframe_ptrs, outframe_pitches);
469 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
470 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf);
471 GST_BUFFER_TIMESTAMP (outbuf) = cstart;
472 GST_BUFFER_DURATION (outbuf) = cstop - cstart;
474 ret = gst_pad_push (dvdec->srcpad, outbuf);
477 dvdec->video_offset++;
480 gst_buffer_unref (buf);
481 gst_object_unref (dvdec);
488 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
489 (NULL), ("Input buffer too small"));
490 ret = GST_FLOW_ERROR;
495 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
496 (NULL), ("Error parsing DV header"));
497 ret = GST_FLOW_ERROR;
502 GST_DEBUG_OBJECT (dvdec, "could not negotiate output");
503 ret = GST_FLOW_NOT_NEGOTIATED;
508 GST_DEBUG_OBJECT (dvdec, "could not allocate buffer");
514 GST_DEBUG_OBJECT (dvdec,
515 "dropping buffer since it's out of the configured segment");
520 static GstStateChangeReturn
521 gst_dvdec_change_state (GstElement * element, GstStateChange transition)
523 GstDVDec *dvdec = GST_DVDEC (element);
524 GstStateChangeReturn ret;
527 switch (transition) {
528 case GST_STATE_CHANGE_NULL_TO_READY:
530 case GST_STATE_CHANGE_READY_TO_PAUSED:
532 dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
533 dvdec->decoder->quality = qualities[dvdec->quality];
534 dv_set_error_log (dvdec->decoder, NULL);
535 gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
536 dvdec->src_negotiated = FALSE;
537 dvdec->sink_negotiated = FALSE;
539 * Enable this function call when libdv2 0.100 or higher is more
542 /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
544 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
550 ret = parent_class->change_state (element, transition);
552 switch (transition) {
553 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
555 case GST_STATE_CHANGE_PAUSED_TO_READY:
556 dv_decoder_free (dvdec->decoder);
557 dvdec->decoder = NULL;
559 case GST_STATE_CHANGE_READY_TO_NULL:
568 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
571 GstDVDec *dvdec = GST_DVDEC (object);
574 case PROP_CLAMP_LUMA:
575 dvdec->clamp_luma = g_value_get_boolean (value);
577 case PROP_CLAMP_CHROMA:
578 dvdec->clamp_chroma = g_value_get_boolean (value);
581 dvdec->quality = g_value_get_enum (value);
582 if ((dvdec->quality < 0) || (dvdec->quality > 5))
585 case PROP_DECODE_NTH:
586 dvdec->drop_factor = g_value_get_int (value);
589 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
598 GstDVDec *dvdec = GST_DVDEC (object);
601 case PROP_CLAMP_LUMA:
602 g_value_set_boolean (value, dvdec->clamp_luma);
604 case PROP_CLAMP_CHROMA:
605 g_value_set_boolean (value, dvdec->clamp_chroma);
608 g_value_set_enum (value, dvdec->quality);
610 case PROP_DECODE_NTH:
611 g_value_set_int (value, dvdec->drop_factor);
614 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);