ext/dv/: Call dv_set_error_log (dv_decoder_t *, NULL); after dv_decoder_new to not...
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25 #include <math.h>
26
27 #include "gstdvdec.h"
28
29
30 static GstElementDetails dvdec_details =
31 GST_ELEMENT_DETAILS ("DV (smpte314) decoder plugin",
32     "Codec/Decoder/Video",
33     "Uses libdv to decode DV video (libdv.sourceforge.net)",
34     "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
35
36
37 #define DV_DEFAULT_QUALITY DV_QUALITY_BEST
38 #define DV_DEFAULT_DECODE_NTH 1
39
40 enum
41 {
42   PROP_0,
43   PROP_CLAMP_LUMA,
44   PROP_CLAMP_CHROMA,
45   PROP_QUALITY,
46   PROP_DECODE_NTH
47 };
48
49 const gint qualities[] = {
50   DV_QUALITY_DC,
51   DV_QUALITY_AC_1,
52   DV_QUALITY_AC_2,
53   DV_QUALITY_DC | DV_QUALITY_COLOR,
54   DV_QUALITY_AC_1 | DV_QUALITY_COLOR,
55   DV_QUALITY_AC_2 | DV_QUALITY_COLOR
56 };
57
58 static GstStaticPadTemplate sink_temp = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("video/x-dv, systemstream = (boolean) false")
62     );
63
64 static GstStaticPadTemplate src_temp = GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("video/x-raw-yuv, "
68         "format = (fourcc) YUY2, "
69         "width = (int) 720, "
70         "framerate = (fraction) [ 1/1, 60/1 ];"
71         "video/x-raw-rgb, "
72         "bpp = (int) 32, "
73         "depth = (int) 24, "
74         "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
75         "red_mask =   (int) 0x0000ff00, "
76         "green_mask = (int) 0x00ff0000, "
77         "blue_mask =  (int) 0xff000000, "
78         "width = (int) 720, "
79         "framerate = (fraction) [ 1/1, 60/1 ];"
80         "video/x-raw-rgb, "
81         "bpp = (int) 24, "
82         "depth = (int) 24, "
83         "endianness = (int) " G_STRINGIFY (G_BIG_ENDIAN) ", "
84         "red_mask =   (int) 0x00ff0000, "
85         "green_mask = (int) 0x0000ff00, "
86         "blue_mask =  (int) 0x000000ff, "
87         "width = (int) 720, " "framerate = (fraction) [ 1/1, 60/1 ]")
88     );
89
90 #define GST_TYPE_DVDEC_QUALITY (gst_dvdec_quality_get_type())
91 GType
92 gst_dvdec_quality_get_type (void)
93 {
94   static GType qtype = 0;
95
96   if (qtype == 0) {
97     static const GEnumValue values[] = {
98       {0, "Monochrome, DC (Fastest)", "fastest"},
99       {1, "Monochrome, first AC coefficient", "monochrome-ac"},
100       {2, "Monochrome, highest quality", "monochrome-best"},
101       {3, "Colour, DC, fastest", "colour-fastest"},
102       {4, "Colour, using only the first AC coefficient", "colour-ac"},
103       {5, "Highest quality colour decoding", "best"},
104       {0, NULL, NULL},
105     };
106
107     qtype = g_enum_register_static ("GstDVDecQualityEnum", values);
108   }
109   return qtype;
110 }
111
112 GST_BOILERPLATE (GstDVDec, gst_dvdec, GstElement, GST_TYPE_ELEMENT);
113
114 static gboolean gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps);
115 static GstFlowReturn gst_dvdec_chain (GstPad * pad, GstBuffer * buffer);
116
117 static GstStateChangeReturn gst_dvdec_change_state (GstElement * element,
118     GstStateChange transition);
119
120 static void gst_dvdec_set_property (GObject * object, guint prop_id,
121     const GValue * value, GParamSpec * pspec);
122 static void gst_dvdec_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec);
124
125 static void
126 gst_dvdec_base_init (gpointer g_class)
127 {
128   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
129
130   gst_element_class_add_pad_template (element_class,
131       gst_static_pad_template_get (&sink_temp));
132   gst_element_class_add_pad_template (element_class,
133       gst_static_pad_template_get (&src_temp));
134
135   gst_element_class_set_details (element_class, &dvdec_details);
136 }
137
138 static void
139 gst_dvdec_class_init (GstDVDecClass * klass)
140 {
141   GObjectClass *gobject_class;
142   GstElementClass *gstelement_class;
143
144   gobject_class = (GObjectClass *) klass;
145   gstelement_class = (GstElementClass *) klass;
146
147   gobject_class->set_property = gst_dvdec_set_property;
148   gobject_class->get_property = gst_dvdec_get_property;
149
150   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_LUMA,
151       g_param_spec_boolean ("clamp_luma", "Clamp luma", "Clamp luma",
152           FALSE, G_PARAM_READWRITE));
153   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CLAMP_CHROMA,
154       g_param_spec_boolean ("clamp_chroma", "Clamp chroma", "Clamp chroma",
155           FALSE, G_PARAM_READWRITE));
156   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_QUALITY,
157       g_param_spec_enum ("quality", "Quality", "Decoding quality",
158           GST_TYPE_DVDEC_QUALITY, DV_DEFAULT_QUALITY, G_PARAM_READWRITE));
159   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DECODE_NTH,
160       g_param_spec_int ("drop-factor", "Drop Factor", "Only decode Nth frame",
161           1, G_MAXINT, DV_DEFAULT_DECODE_NTH, G_PARAM_READWRITE));
162
163   gstelement_class->change_state = gst_dvdec_change_state;
164
165   /* table initialization, only do once */
166   dv_init (0, 0);
167 }
168
169 static void
170 gst_dvdec_init (GstDVDec * dvdec, GstDVDecClass * g_class)
171 {
172   dvdec->sinkpad =
173       gst_pad_new_from_template (gst_static_pad_template_get (&sink_temp),
174       "sink");
175   gst_pad_set_setcaps_function (dvdec->sinkpad,
176       GST_DEBUG_FUNCPTR (gst_dvdec_sink_setcaps));
177   gst_pad_set_chain_function (dvdec->sinkpad, gst_dvdec_chain);
178   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->sinkpad);
179
180   dvdec->srcpad =
181       gst_pad_new_from_template (gst_static_pad_template_get (&src_temp),
182       "src");
183
184   gst_pad_use_fixed_caps (dvdec->srcpad);
185
186   gst_element_add_pad (GST_ELEMENT (dvdec), dvdec->srcpad);
187
188   dvdec->framerate_numerator = 0;
189   dvdec->framerate_denominator = 0;
190   dvdec->height = 0;
191   dvdec->wide = FALSE;
192   dvdec->drop_factor = 1;
193   dvdec->headers_seen = FALSE;
194
195   dvdec->clamp_luma = FALSE;
196   dvdec->clamp_chroma = FALSE;
197   dvdec->quality = DV_DEFAULT_QUALITY;
198 }
199
200 static gboolean
201 gst_dvdec_sink_setcaps (GstPad * pad, GstCaps * caps)
202 {
203   GstDVDec *dvdec;
204   GstStructure *s;
205   GstCaps *othercaps;
206   gboolean gotpar = FALSE;
207   const GValue *par = NULL, *rate = NULL;
208
209   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
210
211   /* first parse the caps */
212   s = gst_caps_get_structure (caps, 0);
213
214   if (!gst_structure_get_int (s, "height", &dvdec->height))
215     goto error;
216   if ((par = gst_structure_get_value (s, "pixel-aspect-ratio")))
217     gotpar = TRUE;
218   if (!(rate = gst_structure_get_value (s, "framerate")))
219     goto error;
220
221   dvdec->framerate_numerator = gst_value_get_fraction_numerator (rate);
222   dvdec->framerate_denominator = gst_value_get_fraction_denominator (rate);
223
224   /* ignoring rgb, bgr0 for now */
225
226   dvdec->bpp = 2;
227
228   othercaps = gst_caps_new_simple ("video/x-raw-yuv",
229       "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("YUY2"),
230       "width", G_TYPE_INT, 720,
231       "height", G_TYPE_INT, dvdec->height,
232       "framerate", GST_TYPE_FRACTION, dvdec->framerate_numerator,
233       dvdec->framerate_denominator, NULL);
234   if (gotpar)
235     gst_structure_set_value (gst_caps_get_structure (othercaps, 0),
236         "pixel-aspect-ratio", par);
237
238   gst_pad_set_caps (dvdec->srcpad, othercaps);
239
240   gst_caps_unref (othercaps);
241
242   gst_object_unref (dvdec);
243
244   return TRUE;
245
246 error:
247   {
248     gst_object_unref (dvdec);
249
250     return FALSE;
251   }
252 }
253
254 static GstFlowReturn
255 gst_dvdec_chain (GstPad * pad, GstBuffer * buf)
256 {
257   GstDVDec *dvdec;
258   guint8 *inframe;
259   guint8 *outframe;
260   guint8 *outframe_ptrs[3];
261   gint outframe_pitches[3];
262   GstBuffer *outbuf;
263   GstFlowReturn ret = GST_FLOW_OK;
264
265   dvdec = GST_DVDEC (gst_pad_get_parent (pad));
266   inframe = GST_BUFFER_DATA (buf);
267
268   if (dv_parse_header (dvdec->decoder, inframe) < 0)
269     g_assert_not_reached ();
270   dv_parse_packs (dvdec->decoder, inframe);
271
272   if (dvdec->video_offset % dvdec->drop_factor != 0)
273     goto skip;
274
275   ret =
276       gst_pad_alloc_buffer_and_set_caps (dvdec->srcpad, 0,
277       (720 * dvdec->height) * dvdec->bpp,
278       GST_PAD_CAPS (dvdec->srcpad), &outbuf);
279   if (ret != GST_FLOW_OK)
280     goto no_buffer;
281
282   outframe = GST_BUFFER_DATA (outbuf);
283
284   outframe_ptrs[0] = outframe;
285   outframe_pitches[0] = 720 * dvdec->bpp;
286
287   /* the rest only matters for YUY2 */
288   if (dvdec->bpp < 3) {
289     outframe_ptrs[1] = outframe_ptrs[0] + 720 * dvdec->height;
290     outframe_ptrs[2] = outframe_ptrs[1] + 360 * dvdec->height;
291
292     outframe_pitches[1] = dvdec->height / 2;
293     outframe_pitches[2] = outframe_pitches[1];
294   }
295
296   GST_DEBUG_OBJECT (dvdec, "decoding and pushing buffer");
297   dv_decode_full_frame (dvdec->decoder, inframe,
298       e_dv_color_yuv, outframe_ptrs, outframe_pitches);
299
300   gst_buffer_stamp (outbuf, buf);
301
302   ret = gst_pad_push (dvdec->srcpad, outbuf);
303
304 skip:
305   gst_buffer_unref (buf);
306   dvdec->video_offset++;
307   gst_object_unref (dvdec);
308
309   return ret;
310
311   /* ERRORS */
312 no_buffer:
313   {
314     GST_DEBUG_OBJECT (dvdec, "could not allocate buffer");
315     gst_buffer_unref (buf);
316     gst_object_unref (dvdec);
317     return ret;
318   }
319 }
320
321 static GstStateChangeReturn
322 gst_dvdec_change_state (GstElement * element, GstStateChange transition)
323 {
324   GstDVDec *dvdec = GST_DVDEC (element);
325   GstStateChangeReturn ret;
326
327
328   switch (transition) {
329     case GST_STATE_CHANGE_NULL_TO_READY:
330       break;
331     case GST_STATE_CHANGE_READY_TO_PAUSED:
332       dvdec->decoder =
333           dv_decoder_new (0, dvdec->clamp_luma, dvdec->clamp_chroma);
334       dvdec->decoder->quality = qualities[dvdec->quality];
335       dv_set_error_log (dvdec->decoder, NULL);
336       /* 
337        * Enable this function call when libdv2 0.100 or higher is more
338        * common
339        */
340       /* dv_set_quality (dvdec->decoder, qualities [dvdec->quality]); */
341       break;
342     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
343       break;
344     default:
345       break;
346   }
347
348   ret = parent_class->change_state (element, transition);
349
350   switch (transition) {
351     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
352       break;
353     case GST_STATE_CHANGE_PAUSED_TO_READY:
354       dv_decoder_free (dvdec->decoder);
355       dvdec->decoder = NULL;
356       dvdec->headers_seen = FALSE;
357       break;
358     case GST_STATE_CHANGE_READY_TO_NULL:
359       break;
360     default:
361       break;
362   }
363   return ret;
364 }
365
366 static void
367 gst_dvdec_set_property (GObject * object, guint prop_id, const GValue * value,
368     GParamSpec * pspec)
369 {
370   GstDVDec *dvdec = GST_DVDEC (object);
371
372   switch (prop_id) {
373     case PROP_CLAMP_LUMA:
374       dvdec->clamp_luma = g_value_get_boolean (value);
375       break;
376     case PROP_CLAMP_CHROMA:
377       dvdec->clamp_chroma = g_value_get_boolean (value);
378       break;
379     case PROP_QUALITY:
380       dvdec->quality = g_value_get_enum (value);
381       if ((dvdec->quality < 0) || (dvdec->quality > 5))
382         dvdec->quality = 0;
383       break;
384     case PROP_DECODE_NTH:
385       dvdec->drop_factor = g_value_get_int (value);
386       break;
387     default:
388       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389       break;
390   }
391 }
392
393 static void
394 gst_dvdec_get_property (GObject * object, guint prop_id, GValue * value,
395     GParamSpec * pspec)
396 {
397   GstDVDec *dvdec = GST_DVDEC (object);
398
399   switch (prop_id) {
400     case PROP_CLAMP_LUMA:
401       g_value_set_boolean (value, dvdec->clamp_luma);
402       break;
403     case PROP_CLAMP_CHROMA:
404       g_value_set_boolean (value, dvdec->clamp_chroma);
405       break;
406     case PROP_QUALITY:
407       g_value_set_enum (value, dvdec->quality);
408       break;
409     case PROP_DECODE_NTH:
410       g_value_set_int (value, dvdec->drop_factor);
411       break;
412     default:
413       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414       break;
415   }
416 }