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)
46 #include <gst/video/video.h>
50 /* sizes of one input buffer */
51 #define NTSC_HEIGHT 480
52 #define NTSC_BUFFER 120000
53 #define NTSC_FRAMERATE_NUMERATOR 30000
54 #define NTSC_FRAMERATE_DENOMINATOR 1001
56 #define PAL_HEIGHT 576
57 #define PAL_BUFFER 144000
58 #define PAL_FRAMERATE_NUMERATOR 25
59 #define PAL_FRAMERATE_DENOMINATOR 1
61 #define PAL_NORMAL_PAR_X 59
62 #define PAL_NORMAL_PAR_Y 54
63 #define PAL_WIDE_PAR_X 118
64 #define PAL_WIDE_PAR_Y 81
66 #define NTSC_NORMAL_PAR_X 10
67 #define NTSC_NORMAL_PAR_Y 11
68 #define NTSC_WIDE_PAR_X 40
69 #define NTSC_WIDE_PAR_Y 33
71 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
72 #define DV_DEFAULT_DECODE_NTH 1
74 GST_DEBUG_CATEGORY_STATIC (dvdec_debug);
75 #define GST_CAT_DEFAULT dvdec_debug
86 const gint qualities[] = {
90 DV_QUALITY_DC | DV_QUALITY_COLOR,
91 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
92 DV_QUALITY_AC_2 | DV_QUALITY_COLOR
95 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
101 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
104 GST_STATIC_CAPS ("video/x-raw-yuv, "
105 "format = (fourcc) YUY2, "
106 "width = (int) 720, "
107 "framerate = (fraction) [ 1/1, 60/1 ];"
111 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
112 "red_mask = (int) 0x0000ff00, "
113 "green_mask = (int) 0x00ff0000, "
114 "blue_mask = (int) 0xff000000, "
115 "width = (int) 720, "
116 "framerate = (fraction) [ 1/1, 60/1 ];"
120 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
121 "red_mask = (int) 0x00ff0000, "
122 "green_mask = (int) 0x0000ff00, "
123 "blue_mask = (int) 0x000000ff, "
124 "width = (int) 720, " "framerate = (fraction) [ 1/1, 60/1 ]")
127 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
129 gst_dvdec_quality_get_type (void)
131 static GType qtype = 0;
134 static const GEnumValue values[] = {
135 {0, "Monochrome, DC (Fastest)", "fastest"},
136 {1, "Monochrome, first AC coefficient", "monochrome-ac"},
137 {2, "Monochrome, highest quality", "monochrome-best"},
138 {3, "Colour, DC, fastest", "colour-fastest"},
139 {4, "Colour, using only the first AC coefficient", "colour-ac"},
140 {5, "Highest quality colour decoding", "best"},
144 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
149 GST_BOILERPLATE (GstDVDec, gst_dvdec, GstElement, GST_TYPE_ELEMENT);
151 static void gst_dvdec_finalize (GObject * object);
152 static gboolean gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps);
153 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
154 static gboolean gst_dvdec_sink_event (GstPad * pad, GstEvent * event);
156 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
157 GstStateChange transition);
159 static void gst_dvdec_set_property (GObject * object, guint prop_id,
160 const GValue * value, GParamSpec * pspec);
161 static void gst_dvdec_get_property (GObject * object, guint prop_id,
162 GValue * value, GParamSpec * pspec);
165 gst_dvdec_base_init (gpointer g_class)
167 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
169 gst_element_class_add_pad_template (element_class,
170 gst_static_pad_template_get (&sink_temp));
171 gst_element_class_add_pad_template (element_class,
172 gst_static_pad_template_get (&src_temp));
174 gst_element_class_set_details_simple (element_class, "DV video decoder",
175 "Codec/Decoder/Video",
176 "Uses libdv to decode DV video (smpte314) (libdv.sourceforge.net)",
177 "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
179 GST_DEBUG_CATEGORY_INIT (dvdec_debug, "dvdec", 0, "DV decoding element");
183 gst_dvdec_class_init (GstDVDecClass * klass)
185 GObjectClass *gobject_class;
186 GstElementClass *gstelement_class;
188 gobject_class = (GObjectClass *) klass;
189 gstelement_class = (GstElementClass *) klass;
191 gobject_class->finalize = gst_dvdec_finalize;
192 gobject_class->set_property = gst_dvdec_set_property;
193 gobject_class->get_property = gst_dvdec_get_property;
195 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
196 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
197 FALSE, G_PARAM_READWRITE));
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
199 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
200 FALSE, G_PARAM_READWRITE));
201 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
202 g_param_spec_enum ("quality", "Quality", "Decoding quality",
203 GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
204 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
205 g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
206 1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
208 gstelement_class->change_state = gst_dvdec_change_state;
212 gst_dvdec_init (GstDVDec * dvdec, GstDVDecClass * g_class)
214 dvdec->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
215 gst_pad_set_setcaps_function (dvdec->sinkpad,
216 GST_DEBUG_FUNCPTR (gst_dvdec_sink_setcaps));
217 gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
218 gst_pad_set_event_function (dvdec->sinkpad, gst_dvdec_sink_event);
219 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
221 dvdec->srcpad = gst_pad_new_from_static_template (&src_temp, "src");
222 gst_pad_use_fixed_caps (dvdec->srcpad);
223 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
225 dvdec->framerate_numerator = 0;
226 dvdec->framerate_denominator = 0;
228 dvdec->drop_factor = 1;
230 dvdec->clamp_luma = FALSE;
231 dvdec->clamp_chroma = FALSE;
232 dvdec->quality = DV_DEFAULT_QUALITY;
233 dvdec->segment = gst_segment_new ();
237 gst_dvdec_finalize (GObject * object)
239 GstDVDec *dvdec = GST_DVDEC (object);
241 gst_segment_free (dvdec->segment);
243 G_OBJECT_CLASS (parent_class)->finalize (object);
247 gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps)
251 const GValue *par = NULL, *rate = NULL;
253 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
255 /* first parse the caps */
256 s = gst_caps_get_structure (caps, 0);
258 /* we allow framerate and PAR to be overwritten. framerate is mandatory. */
259 if (!(rate = gst_structure_get_value (s, "framerate")))
261 par = gst_structure_get_value (s, "pixel-aspect-ratio");
264 dvdec->par_x = gst_value_get_fraction_numerator (par);
265 dvdec->par_y = gst_value_get_fraction_denominator (par);
266 dvdec->need_par = FALSE;
270 dvdec->need_par = TRUE;
272 dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
273 dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
274 dvdec->sink_negotiated = TRUE;
275 dvdec->src_negotiated = FALSE;
277 gst_object_unref (dvdec);
284 GST_DEBUG_OBJECT (dvdec, "no framerate specified in caps");
285 gst_object_unref (dvdec);
291 gst_dvdec_src_negotiate (GstDVDec * dvdec)
295 /* no PAR was specified in input, derive from encoded data */
296 if (dvdec->need_par) {
299 dvdec->par_x = PAL_WIDE_PAR_X;
300 dvdec->par_y = PAL_WIDE_PAR_Y;
302 dvdec->par_x = PAL_NORMAL_PAR_X;
303 dvdec->par_y = PAL_NORMAL_PAR_Y;
307 dvdec->par_x = NTSC_WIDE_PAR_X;
308 dvdec->par_y = NTSC_WIDE_PAR_Y;
310 dvdec->par_x = NTSC_NORMAL_PAR_X;
311 dvdec->par_y = NTSC_NORMAL_PAR_Y;
314 GST_DEBUG_OBJECT (dvdec, "Inferred PAR %d/%d from video format",
315 dvdec->par_x, dvdec->par_y);
318 /* ignoring rgb, bgr0 for now */
321 othercaps = gst_caps_new_simple ("video/x-raw-yuv",
322 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
323 "width", G_TYPE_INT, 720,
324 "height", G_TYPE_INT, dvdec->height,
325 "framerate", GST_TYPE_FRACTION, dvdec->framerate_numerator,
326 dvdec->framerate_denominator,
327 "pixel-aspect-ratio", GST_TYPE_FRACTION, dvdec->par_x,
328 dvdec->par_y, "interlaced", G_TYPE_BOOLEAN, dvdec->interlaced, NULL);
330 gst_pad_set_caps (dvdec->srcpad, othercaps);
331 gst_caps_unref (othercaps);
333 dvdec->src_negotiated = TRUE;
339 gst_dvdec_sink_event (GstPad * pad, GstEvent * event)
344 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
346 switch (GST_EVENT_TYPE (event)) {
347 case GST_EVENT_FLUSH_STOP:
348 gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
350 case GST_EVENT_NEWSEGMENT:{
352 gdouble rate, applied_rate;
354 gint64 start, stop, position;
356 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
357 &format, &start, &stop, &position);
359 GST_DEBUG_OBJECT (dvdec, "Got NEWSEGMENT [%" GST_TIME_FORMAT
360 " - %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "]",
361 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
362 GST_TIME_ARGS (position));
364 gst_segment_set_newsegment_full (dvdec->segment, update, rate,
365 applied_rate, format, start, stop, position);
372 res = gst_pad_push_event (dvdec->srcpad, event);
378 gst_dvdec_chain (GstPad * pad, GstBuffer * buf)
383 guint8 *outframe_ptrs[3];
384 gint outframe_pitches[3];
386 GstFlowReturn ret = GST_FLOW_OK;
388 gint64 cstart, cstop;
391 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
392 inframe = GST_BUFFER_DATA (buf);
394 /* buffer should be at least the size of one NTSC frame, this should
395 * be enough to decode the header. */
396 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < NTSC_BUFFER))
399 /* preliminary dropping. unref and return if outside of configured segment */
400 if ((dvdec->segment->format == GST_FORMAT_TIME) &&
401 (!(gst_segment_clip (dvdec->segment, GST_FORMAT_TIME,
402 GST_BUFFER_TIMESTAMP (buf),
403 GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf),
407 if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0))
408 goto parse_header_error;
411 PAL = dv_system_50_fields (dvdec->decoder);
412 wide = dv_format_wide (dvdec->decoder);
414 /* check the buffer is of right size after we know if we are
415 * dealing with PAL or NTSC */
416 length = (PAL ? PAL_BUFFER : NTSC_BUFFER);
417 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < length))
420 dv_parse_packs (dvdec->decoder, inframe);
422 if (dvdec->video_offset % dvdec->drop_factor != 0)
425 /* renegotiate on change */
426 if (PAL != dvdec->PAL || wide != dvdec->wide) {
427 dvdec->src_negotiated = FALSE;
432 dvdec->height = (dvdec->PAL ? PAL_HEIGHT : NTSC_HEIGHT);
434 dvdec->interlaced = !dv_is_progressive (dvdec->decoder);
436 /* negotiate if not done yet */
437 if (!dvdec->src_negotiated) {
438 if (!gst_dvdec_src_negotiate (dvdec))
443 gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0,
444 (720 * dvdec->height) * dvdec->bpp,
445 GST_PAD_CAPS (dvdec->srcpad), &outbuf);
446 if (G_UNLIKELY (ret != GST_FLOW_OK))
449 outframe = GST_BUFFER_DATA (outbuf);
451 outframe_ptrs[0] = outframe;
452 outframe_pitches[0] = 720 * dvdec->bpp;
454 /* the rest only matters for YUY2 */
455 if (dvdec->bpp < 3) {
456 outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height;
457 outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height;
459 outframe_pitches[1] = dvdec->height / 2;
460 outframe_pitches[2] = outframe_pitches[1];
463 GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
464 dv_decode_full_frame (dvdec->decoder, inframe,
465 e_dv_color_yuv, outframe_ptrs, outframe_pitches);
467 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_TFF);
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);