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
26 * dvdec decodes DV video into raw video. The element expects a full DV frame
27 * as input, which is 120000 bytes for NTSC and 144000 for PAL video.
30 * This element can perform simple frame dropping with the drop-factor
31 * property. Setting this property to a value N > 1 will only decode every
34 * <title>Example launch line</title>
37 * gst-launch filesrc location=test.dv ! dvdemux name=demux ! dvdec ! xvimagesink
39 * This pipeline decodes and renders the raw DV stream to a videosink.
41 * Last reviewed on 2006-02-28 (0.10.3)
54 static const GstElementDetails dvdec_details =
55 GST_ELEMENT_DETAILS ("DV video decoder",
56 "Codec/Decoder/Video",
57 "Uses libdv to decode DV video (smpte314) (libdv.sourceforge.net)",
58 "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
60 /* sizes of one input buffer */
61 #define NTSC_BUFFER 120000
62 #define PAL_BUFFER 144000
64 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
65 #define DV_DEFAULT_DECODE_NTH 1
67 GST_DEBUG_CATEGORY_STATIC (dvdec_debug);
68 #define GST_CAT_DEFAULT dvdec_debug
79 const gint qualities[] = {
83 DV_QUALITY_DC | DV_QUALITY_COLOR,
84 DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
85 DV_QUALITY_AC_2 | DV_QUALITY_COLOR
88 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
94 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS ("video/x-raw-yuv, "
98 "format = (fourcc) YUY2, "
100 "framerate = (fraction) [ 1/1, 60/1 ];"
104 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
105 "red_mask = (int) 0x0000ff00, "
106 "green_mask = (int) 0x00ff0000, "
107 "blue_mask = (int) 0xff000000, "
108 "width = (int) 720, "
109 "framerate = (fraction) [ 1/1, 60/1 ];"
113 "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
114 "red_mask = (int) 0x00ff0000, "
115 "green_mask = (int) 0x0000ff00, "
116 "blue_mask = (int) 0x000000ff, "
117 "width = (int) 720, " "framerate = (fraction) [ 1/1, 60/1 ]")
120 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
122 gst_dvdec_quality_get_type (void)
124 static GType qtype = 0;
127 static const GEnumValue values[] = {
128 {0, "Monochrome, DC (Fastest)", "fastest"},
129 {1, "Monochrome, first AC coefficient", "monochrome-ac"},
130 {2, "Monochrome, highest quality", "monochrome-best"},
131 {3, "Colour, DC, fastest", "colour-fastest"},
132 {4, "Colour, using only the first AC coefficient", "colour-ac"},
133 {5, "Highest quality colour decoding", "best"},
137 qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
142 GST_BOILERPLATE (GstDVDec, gst_dvdec, GstElement, GST_TYPE_ELEMENT);
144 static void gst_dvdec_finalize (GObject * object);
145 static gboolean gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps);
146 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
147 static gboolean gst_dvdec_sink_event (GstPad * pad, GstEvent * event);
149 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
150 GstStateChange transition);
152 static void gst_dvdec_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec);
154 static void gst_dvdec_get_property (GObject * object, guint prop_id,
155 GValue * value, GParamSpec * pspec);
158 gst_dvdec_base_init (gpointer g_class)
160 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
162 gst_element_class_add_pad_template (element_class,
163 gst_static_pad_template_get (&sink_temp));
164 gst_element_class_add_pad_template (element_class,
165 gst_static_pad_template_get (&src_temp));
167 gst_element_class_set_details (element_class, &dvdec_details);
169 GST_DEBUG_CATEGORY_INIT (dvdec_debug, "dvdec", 0, "DV decoding element");
173 gst_dvdec_class_init (GstDVDecClass * klass)
175 GObjectClass *gobject_class;
176 GstElementClass *gstelement_class;
178 gobject_class = (GObjectClass *) klass;
179 gstelement_class = (GstElementClass *) klass;
181 gobject_class->finalize = gst_dvdec_finalize;
182 gobject_class->set_property = gst_dvdec_set_property;
183 gobject_class->get_property = gst_dvdec_get_property;
185 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
186 g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
187 FALSE, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
189 g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
190 FALSE, G_PARAM_READWRITE));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
192 g_param_spec_enum ("quality", "Quality", "Decoding quality",
193 GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
194 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
195 g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
196 1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
198 gstelement_class->change_state = gst_dvdec_change_state;
200 /* table initialization, only do once */
205 gst_dvdec_init (GstDVDec * dvdec, GstDVDecClass * g_class)
207 dvdec->sinkpad = gst_pad_new_from_static_template (&sink_temp, "sink");
208 gst_pad_set_setcaps_function (dvdec->sinkpad,
209 GST_DEBUG_FUNCPTR (gst_dvdec_sink_setcaps));
210 gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
211 gst_pad_set_event_function (dvdec->sinkpad, gst_dvdec_sink_event);
212 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
214 dvdec->srcpad = gst_pad_new_from_static_template (&src_temp, "src");
215 gst_pad_use_fixed_caps (dvdec->srcpad);
216 gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
218 dvdec->framerate_numerator = 0;
219 dvdec->framerate_denominator = 0;
222 dvdec->drop_factor = 1;
224 dvdec->clamp_luma = FALSE;
225 dvdec->clamp_chroma = FALSE;
226 dvdec->quality = DV_DEFAULT_QUALITY;
227 dvdec->segment = gst_segment_new ();
231 gst_dvdec_finalize (GObject * object)
233 GstDVDec *dvdec = GST_DVDEC (object);
235 gst_segment_free (dvdec->segment);
237 G_OBJECT_CLASS (parent_class)->finalize (object);
241 gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps)
246 gboolean gotpar = FALSE;
247 const GValue *par = NULL, *rate = NULL;
249 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
251 /* first parse the caps */
252 s = gst_caps_get_structure (caps, 0);
254 if (!gst_structure_get_int (s, "height", &dvdec->height))
256 if ((par = gst_structure_get_value (s, "pixel-aspect-ratio")))
258 if (!(rate = gst_structure_get_value (s, "framerate")))
261 dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
262 dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
264 /* ignoring rgb, bgr0 for now */
268 othercaps = gst_caps_new_simple ("video/x-raw-yuv",
269 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
270 "width", G_TYPE_INT, 720,
271 "height", G_TYPE_INT, dvdec->height,
272 "framerate", GST_TYPE_FRACTION, dvdec->framerate_numerator,
273 dvdec->framerate_denominator, NULL);
275 gst_structure_set_value (gst_caps_get_structure (othercaps, 0),
276 "pixel-aspect-ratio", par);
278 gst_pad_set_caps (dvdec->srcpad, othercaps);
280 gst_caps_unref (othercaps);
282 gst_object_unref (dvdec);
288 gst_object_unref (dvdec);
295 gst_dvdec_sink_event (GstPad * pad, GstEvent * event)
300 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
302 switch (GST_EVENT_TYPE (event)) {
303 case GST_EVENT_FLUSH_STOP:
304 gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
306 case GST_EVENT_NEWSEGMENT:{
308 gdouble rate, applied_rate;
310 gint64 start, stop, position;
312 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
313 &format, &start, &stop, &position);
315 GST_DEBUG_OBJECT (dvdec, "Got NEWSEGMENT [%" GST_TIME_FORMAT
316 " - %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "]",
317 GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
318 GST_TIME_ARGS (position));
320 gst_segment_set_newsegment_full (dvdec->segment, update, rate,
321 applied_rate, format, start, stop, position);
328 res = gst_pad_push_event (dvdec->srcpad, event);
334 gst_dvdec_chain (GstPad * pad, GstBuffer * buf)
339 guint8 *outframe_ptrs[3];
340 gint outframe_pitches[3];
342 GstFlowReturn ret = GST_FLOW_OK;
344 gint64 cstart, cstop;
346 dvdec = GST_DVDEC (gst_pad_get_parent (pad));
347 inframe = GST_BUFFER_DATA (buf);
349 /* buffer should be at least the size of one NTSC frame, this should
350 * be enough to decode the header. */
351 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < NTSC_BUFFER))
354 /* preliminary dropping. unref and return if outside of configured segment */
355 if ((dvdec->segment->format == GST_FORMAT_TIME) &&
356 (!(gst_segment_clip (dvdec->segment, GST_FORMAT_TIME,
357 GST_BUFFER_TIMESTAMP (buf),
358 GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf),
362 if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0))
363 goto parse_header_error;
365 /* check the buffer is of right size after we know if we are
366 * dealing with PAL or NTSC */
367 length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
368 if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < length))
371 dv_parse_packs (dvdec->decoder, inframe);
373 if (dvdec->video_offset % dvdec->drop_factor != 0)
377 gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0,
378 (720 * dvdec->height) * dvdec->bpp,
379 GST_PAD_CAPS (dvdec->srcpad), &outbuf);
380 if (G_UNLIKELY (ret != GST_FLOW_OK))
383 outframe = GST_BUFFER_DATA (outbuf);
385 outframe_ptrs[0] = outframe;
386 outframe_pitches[0] = 720 * dvdec->bpp;
388 /* the rest only matters for YUY2 */
389 if (dvdec->bpp < 3) {
390 outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height;
391 outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height;
393 outframe_pitches[1] = dvdec->height / 2;
394 outframe_pitches[2] = outframe_pitches[1];
397 GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
398 dv_decode_full_frame (dvdec->decoder, inframe,
399 e_dv_color_yuv, outframe_ptrs, outframe_pitches);
401 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
402 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf);
403 GST_BUFFER_TIMESTAMP (outbuf) = cstart;
404 GST_BUFFER_DURATION (outbuf) = cstop - cstart;
406 ret = gst_pad_push (dvdec->srcpad, outbuf);
409 dvdec->video_offset++;
412 gst_buffer_unref (buf);
413 gst_object_unref (dvdec);
420 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
421 (NULL), ("Input buffer too small"));
422 ret = GST_FLOW_ERROR;
427 GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
428 (NULL), ("Error parsing DV header"));
429 ret = GST_FLOW_ERROR;
434 GST_DEBUG_OBJECT (dvdec, "could not allocate buffer");
440 GST_DEBUG_OBJECT (dvdec,
441 "dropping buffer since it's out of the configured segment");
446 static GstStateChangeReturn
447 gst_dvdec_change_state (GstElement * element, GstStateChange transition)
449 GstDVDec *dvdec = GST_DVDEC (element);
450 GstStateChangeReturn ret;
453 switch (transition) {
454 case GST_STATE_CHANGE_NULL_TO_READY:
456 case GST_STATE_CHANGE_READY_TO_PAUSED:
458 dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
459 dvdec->decoder->quality = qualities[dvdec->quality];
460 dv_set_error_log (dvdec->decoder, NULL);
461 gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
463 * Enable this function call when libdv2 0.100 or higher is more
466 /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
468 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
474 ret = parent_class->change_state (element, transition);
476 switch (transition) {
477 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
479 case GST_STATE_CHANGE_PAUSED_TO_READY:
480 dv_decoder_free (dvdec->decoder);
481 dvdec->decoder = NULL;
483 case GST_STATE_CHANGE_READY_TO_NULL:
492 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
495 GstDVDec *dvdec = GST_DVDEC (object);
498 case PROP_CLAMP_LUMA:
499 dvdec->clamp_luma = g_value_get_boolean (value);
501 case PROP_CLAMP_CHROMA:
502 dvdec->clamp_chroma = g_value_get_boolean (value);
505 dvdec->quality = g_value_get_enum (value);
506 if ((dvdec->quality < 0) || (dvdec->quality > 5))
509 case PROP_DECODE_NTH:
510 dvdec->drop_factor = g_value_get_int (value);
513 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
519 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
522 GstDVDec *dvdec = GST_DVDEC (object);
525 case PROP_CLAMP_LUMA:
526 g_value_set_boolean (value, dvdec->clamp_luma);
528 case PROP_CLAMP_CHROMA:
529 g_value_set_boolean (value, dvdec->clamp_chroma);
532 g_value_set_enum (value, dvdec->quality);
534 case PROP_DECODE_NTH:
535 g_value_set_int (value, dvdec->drop_factor);
538 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);