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