ext/dv/gstdvdec.*: Added GstSegment handling, now implements dropping/clipping.
[platform/upstream/gst-plugins-good.git] / ext / dv / gstdvdec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2005> Wim Taymans <wim@fluendo.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-dvdec
23  *
24  * <refsect2>
25  * <para>
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.
28  * </para>
29  * <para>
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 
32  * Nth frame.
33  * </para>
34  * <title>Example launch line</title>
35  * <para>
36  * <programlisting>
37  * gst-launch filesrc location=test.dv ! dvdemux name=demux ! dvdec ! xvimagesink
38  * </programlisting>
39  * This pipeline decodes and renders the raw DV stream to a videosink.
40  * </para>
41  * Last reviewed on 2006-02-28 (0.10.3)
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48 #include <string.h>
49 #include <math.h>
50
51 #include "gstdvdec.h"
52
53
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>");
59
60 /* sizes of one input buffer */
61 #define NTSC_BUFFER 120000
62 #define PAL_BUFFER 144000
63
64 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
65 #define DV_DEFAULT_DECODE_NTH 1
66
67 GST_DEBUG_CATEGORY (dvdec_debug);
68 #define GST_CAT_DEFAULT dvdec_debug
69
70 enum
71 {
72   PROP_0,
73   PROP_CLAMP_LUMA,
74   PROP_CLAMP_CHROMA,
75   PROP_QUALITY,
76   PROP_DECODE_NTH
77 };
78
79 const gint qualities[] = {
80   DV_QUALITY_DC,
81   DV_QUALITY_AC_1,
82   DV_QUALITY_AC_2,
83   DV_QUALITY_DC | DV_QUALITY_COLOR,
84   DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
85   DV_QUALITY_AC_2 | DV_QUALITY_COLOR
86 };
87
88 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
92     );
93
94 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
95     GST_PAD_SRC,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS ("video/x-raw-yuv, "
98         "format = (fourcc) YUY2, "
99         "width = (int) 720, "
100         "framerate = (fraction) [ 1/1, 60/1 ];"
101         "video/x-raw-rgb, "
102         "bpp = (int) 32, "
103         "depth = (int) 24, "
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 ];"
110         "video/x-raw-rgb, "
111         "bpp = (int) 24, "
112         "depth = (int) 24, "
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 ]")
118     );
119
120 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
121 GType
122 gst_dvdec_quality_get_type (void)
123 {
124   static GType qtype = 0;
125
126   if (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"},
134       {0, NULL, NULL},
135     };
136
137     qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
138   }
139   return qtype;
140 }
141
142 GST_BOILERPLATE (GstDVDec, gst_dvdec, GstElement, GST_TYPE_ELEMENT);
143
144 static gboolean gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps);
145 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
146 static gboolean gst_dvdec_sink_event (GstPad * pad, GstEvent * event);
147
148 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
149     GstStateChange transition);
150
151 static void gst_dvdec_set_property (GObject * object, guint prop_id,
152     const GValue * value, GParamSpec * pspec);
153 static void gst_dvdec_get_property (GObject * object, guint prop_id,
154     GValue * value, GParamSpec * pspec);
155
156 static void
157 gst_dvdec_base_init (gpointer g_class)
158 {
159   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160
161   gst_element_class_add_pad_template (element_class,
162       gst_static_pad_template_get (&sink_temp));
163   gst_element_class_add_pad_template (element_class,
164       gst_static_pad_template_get (&src_temp));
165
166   gst_element_class_set_details (element_class, &dvdec_details);
167
168   GST_DEBUG_CATEGORY_INIT (dvdec_debug, "dvdec", 0, "DV decoding element");
169 }
170
171 static void
172 gst_dvdec_class_init (GstDVDecClass * klass)
173 {
174   GObjectClass *gobject_class;
175   GstElementClass *gstelement_class;
176
177   gobject_class = (GObjectClass *) klass;
178   gstelement_class = (GstElementClass *) klass;
179
180   gobject_class->set_property = gst_dvdec_set_property;
181   gobject_class->get_property = gst_dvdec_get_property;
182
183   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
184       g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
185           FALSE, G_PARAM_READWRITE));
186   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
187       g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
188           FALSE, G_PARAM_READWRITE));
189   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
190       g_param_spec_enum ("quality", "Quality", "Decoding quality",
191           GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
192   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
193       g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
194           1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
195
196   gstelement_class->change_state = gst_dvdec_change_state;
197
198   /* table initialization, only do once */
199   dv_init (0, 0);
200 }
201
202 static void
203 gst_dvdec_init (GstDVDec * dvdec, GstDVDecClass * g_class)
204 {
205   dvdec->sinkpad =
206       gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
207       "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);
213
214   dvdec->srcpad =
215       gst_pad_new_from_template (gst_static_pad_template_get (&src_temp),
216       "src");
217
218   gst_pad_use_fixed_caps (dvdec->srcpad);
219
220   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
221
222   dvdec->framerate_numerator = 0;
223   dvdec->framerate_denominator = 0;
224   dvdec->height = 0;
225   dvdec->wide = FALSE;
226   dvdec->drop_factor = 1;
227
228   dvdec->clamp_luma = FALSE;
229   dvdec->clamp_chroma = FALSE;
230   dvdec->quality = DV_DEFAULT_QUALITY;
231 }
232
233 static gboolean
234 gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps)
235 {
236   GstDVDec *dvdec;
237   GstStructure *s;
238   GstCaps *othercaps;
239   gboolean gotpar = FALSE;
240   const GValue *par = NULL, *rate = NULL;
241
242   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
243
244   /* first parse the caps */
245   s = gst_caps_get_structure (caps, 0);
246
247   if (!gst_structure_get_int (s, "height", &dvdec->height))
248     goto error;
249   if ((par = gst_structure_get_value (s, "pixel-aspect-ratio")))
250     gotpar = TRUE;
251   if (!(rate = gst_structure_get_value (s, "framerate")))
252     goto error;
253
254   dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
255   dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
256
257   /* ignoring rgb, bgr0 for now */
258
259   dvdec->bpp = 2;
260
261   othercaps = gst_caps_new_simple ("video/x-raw-yuv",
262       "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
263       "width", G_TYPE_INT, 720,
264       "height", G_TYPE_INT, dvdec->height,
265       "framerate", GST_TYPE_FRACTION, dvdec->framerate_numerator,
266       dvdec->framerate_denominator, NULL);
267   if (gotpar)
268     gst_structure_set_value (gst_caps_get_structure (othercaps, 0),
269         "pixel-aspect-ratio", par);
270
271   gst_pad_set_caps (dvdec->srcpad, othercaps);
272
273   gst_caps_unref (othercaps);
274
275   gst_object_unref (dvdec);
276
277   return TRUE;
278
279 error:
280   {
281     gst_object_unref (dvdec);
282
283     return FALSE;
284   }
285 }
286
287 static gboolean
288 gst_dvdec_sink_event (GstPad * pad, GstEvent * event)
289 {
290   GstDVDec *dvdec;
291   gboolean res = TRUE;
292
293   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
294
295   switch (GST_EVENT_TYPE (event)) {
296     case GST_EVENT_NEWSEGMENT:{
297       gboolean update;
298       gdouble rate;
299       GstFormat format;
300       gint64 start, stop, position;
301
302       /* Once -good depends on core >= 0.10.6, use newsegment_full */
303       gst_event_parse_new_segment (event, &update, &rate, &format,
304           &start, &stop, &position);
305       GST_DEBUG_OBJECT (dvdec, "Got NEWSEGMENT [%" GST_TIME_FORMAT
306           " - %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT "]",
307           GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
308           GST_TIME_ARGS (position));
309       gst_segment_set_newsegment (dvdec->segment, update, rate, format,
310           start, stop, position);
311     }
312       break;
313     default:
314       break;
315   }
316
317   res = gst_pad_push_event (dvdec->srcpad, event);
318
319   return res;
320 }
321
322 static GstFlowReturn
323 gst_dvdec_chain (GstPad * pad, GstBuffer * buf)
324 {
325   GstDVDec *dvdec;
326   guint8 *inframe;
327   guint8 *outframe;
328   guint8 *outframe_ptrs[3];
329   gint outframe_pitches[3];
330   GstBuffer *outbuf;
331   GstFlowReturn ret = GST_FLOW_OK;
332   guint length;
333   gint64 cstart, cstop;
334
335   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
336   inframe = GST_BUFFER_DATA (buf);
337
338   /* buffer should be at least the size of one NTSC frame, this should
339    * be enough to decode the header. */
340   if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < NTSC_BUFFER))
341     goto wrong_size;
342
343   /* preliminary dropping. unref and return if outside of configured segment */
344   if ((dvdec->segment->format == GST_FORMAT_TIME) &&
345       (!(gst_segment_clip (dvdec->segment, GST_FORMAT_TIME,
346                   GST_BUFFER_TIMESTAMP (buf),
347                   GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf),
348                   &cstart, &cstop))))
349     goto dropping;
350
351   if (G_UNLIKELY (dv_parse_header (dvdec->decoder, inframe) < 0))
352     goto parse_header_error;
353
354   /* check the buffer is of right size after we know if we are
355    * dealing with PAL or NTSC */
356   length = (dvdec->PAL ? PAL_BUFFER : NTSC_BUFFER);
357   if (G_UNLIKELY (GST_BUFFER_SIZE (buf) < length))
358     goto wrong_size;
359
360   dv_parse_packs (dvdec->decoder, inframe);
361
362   if (dvdec->video_offset % dvdec->drop_factor != 0)
363     goto skip;
364
365   ret =
366       gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0,
367       (720 * dvdec->height) * dvdec->bpp,
368       GST_PAD_CAPS (dvdec->srcpad), &outbuf);
369   if (G_UNLIKELY (ret != GST_FLOW_OK))
370     goto no_buffer;
371
372   outframe = GST_BUFFER_DATA (outbuf);
373
374   outframe_ptrs[0] = outframe;
375   outframe_pitches[0] = 720 * dvdec->bpp;
376
377   /* the rest only matters for YUY2 */
378   if (dvdec->bpp < 3) {
379     outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height;
380     outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height;
381
382     outframe_pitches[1] = dvdec->height / 2;
383     outframe_pitches[2] = outframe_pitches[1];
384   }
385
386   GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
387   dv_decode_full_frame (dvdec->decoder, inframe,
388       e_dv_color_yuv, outframe_ptrs, outframe_pitches);
389
390   GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
391   GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf);
392   GST_BUFFER_TIMESTAMP (outbuf) = cstart;
393   GST_BUFFER_DURATION (outbuf) = cstop - cstart;
394
395   ret = gst_pad_push (dvdec->srcpad, outbuf);
396
397 skip:
398   dvdec->video_offset++;
399
400 done:
401   gst_buffer_unref (buf);
402   gst_object_unref (dvdec);
403
404   return ret;
405
406   /* ERRORS */
407 wrong_size:
408   {
409     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
410         (NULL), ("Input buffer too small"));
411     ret = GST_FLOW_ERROR;
412     goto done;
413   }
414 parse_header_error:
415   {
416     GST_ELEMENT_ERROR (dvdec, STREAM, DECODE,
417         (NULL), ("Error parsing DV header"));
418     ret = GST_FLOW_ERROR;
419     goto done;
420   }
421 no_buffer:
422   {
423     GST_DEBUG_OBJECT (dvdec, "could not allocate buffer");
424     goto done;
425   }
426
427 dropping:
428   {
429     GST_DEBUG_OBJECT (dvdec,
430         "dropping buffer since it's out of the configured segment");
431     goto done;
432   }
433 }
434
435 static GstStateChangeReturn
436 gst_dvdec_change_state (GstElement * element, GstStateChange transition)
437 {
438   GstDVDec *dvdec = GST_DVDEC (element);
439   GstStateChangeReturn ret;
440
441
442   switch (transition) {
443     case GST_STATE_CHANGE_NULL_TO_READY:
444       break;
445     case GST_STATE_CHANGE_READY_TO_PAUSED:
446       dvdec->decoder =
447           dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
448       dvdec->decoder->quality = qualities[dvdec->quality];
449       dv_set_error_log (dvdec->decoder, NULL);
450       dvdec->segment = gst_segment_new ();
451       gst_segment_init (dvdec->segment, GST_FORMAT_UNDEFINED);
452       /* 
453        * Enable this function call when libdv2 0.100 or higher is more
454        * common
455        */
456       /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
457       break;
458     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
459       break;
460     default:
461       break;
462   }
463
464   ret = parent_class->change_state (element, transition);
465
466   switch (transition) {
467     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
468       break;
469     case GST_STATE_CHANGE_PAUSED_TO_READY:
470       dv_decoder_free (dvdec->decoder);
471       dvdec->decoder = NULL;
472       gst_segment_free (dvdec->segment);
473       dvdec->segment = NULL;
474       break;
475     case GST_STATE_CHANGE_READY_TO_NULL:
476       break;
477     default:
478       break;
479   }
480   return ret;
481 }
482
483 static void
484 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
485     GParamSpec * pspec)
486 {
487   GstDVDec *dvdec = GST_DVDEC (object);
488
489   switch (prop_id) {
490     case PROP_CLAMP_LUMA:
491       dvdec->clamp_luma = g_value_get_boolean (value);
492       break;
493     case PROP_CLAMP_CHROMA:
494       dvdec->clamp_chroma = g_value_get_boolean (value);
495       break;
496     case PROP_QUALITY:
497       dvdec->quality = g_value_get_enum (value);
498       if ((dvdec->quality < 0) || (dvdec->quality > 5))
499         dvdec->quality = 0;
500       break;
501     case PROP_DECODE_NTH:
502       dvdec->drop_factor = g_value_get_int (value);
503       break;
504     default:
505       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506       break;
507   }
508 }
509
510 static void
511 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
512     GParamSpec * pspec)
513 {
514   GstDVDec *dvdec = GST_DVDEC (object);
515
516   switch (prop_id) {
517     case PROP_CLAMP_LUMA:
518       g_value_set_boolean (value, dvdec->clamp_luma);
519       break;
520     case PROP_CLAMP_CHROMA:
521       g_value_set_boolean (value, dvdec->clamp_chroma);
522       break;
523     case PROP_QUALITY:
524       g_value_set_enum (value, dvdec->quality);
525       break;
526     case PROP_DECODE_NTH:
527       g_value_set_int (value, dvdec->drop_factor);
528       break;
529     default:
530       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531       break;
532   }
533 }