documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / ext / webp / gstwebpdec.c
1 /* GStreamer
2  * Copyright (C) <2013> Sreerenj Balachandran <sreerenj.balachandran@intel.com>
3  * Copyright (C) <2013> Intel Corporation
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <string.h>
25
26 #include "gstwebpdec.h"
27
28 #define MIN_WIDTH 1
29 #define MAX_WIDTH 16383
30 #define MIN_HEIGHT 1
31 #define MAX_HEIGHT 16383
32
33 enum
34 {
35   PROP_0,
36   PROP_BYPASS_FILTERING,
37   PROP_NO_FANCY_UPSAMPLING,
38   PROP_USE_THREADS
39 };
40
41 static GstStaticPadTemplate gst_webp_dec_sink_pad_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("image/webp")
46     );
47
48 /*Fixme: Add YUV support */
49 static GstStaticPadTemplate gst_webp_dec_src_pad_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
54         ("{ RGB, RGBA, BGR, BGRA, ARGB, RGB16}"))
55     );
56
57 GST_DEBUG_CATEGORY_STATIC (webp_dec_debug);
58 #define GST_CAT_DEFAULT webp_dec_debug
59
60 static void gst_webp_dec_set_property (GObject * object, guint prop_id,
61     const GValue * value, GParamSpec * pspec);
62 static void gst_webp_dec_get_property (GObject * object, guint prop_id,
63     GValue * value, GParamSpec * pspec);
64
65 static gboolean gst_webp_dec_start (GstVideoDecoder * bdec);
66 static gboolean gst_webp_dec_stop (GstVideoDecoder * bdec);
67 static gboolean gst_webp_dec_set_format (GstVideoDecoder * dec,
68     GstVideoCodecState * state);
69 static GstFlowReturn gst_webp_dec_parse (GstVideoDecoder * bdec,
70     GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
71 static GstFlowReturn gst_webp_dec_handle_frame (GstVideoDecoder * bdec,
72     GstVideoCodecFrame * frame);
73 static gboolean gst_webp_dec_decide_allocation (GstVideoDecoder * bdec,
74     GstQuery * query);
75 static gboolean gst_webp_dec_sink_event (GstVideoDecoder * bdec,
76     GstEvent * event);
77
78 static gboolean gst_webp_dec_reset_frame (GstWebPDec * webpdec);
79
80 #define gst_webp_dec_parent_class parent_class
81 G_DEFINE_TYPE (GstWebPDec, gst_webp_dec, GST_TYPE_VIDEO_DECODER);
82
83 static void
84 gst_webp_dec_class_init (GstWebPDecClass * klass)
85 {
86   GObjectClass *gobject_class;
87   GstElementClass *element_class;
88   GstVideoDecoderClass *vdec_class;
89
90   gobject_class = (GObjectClass *) klass;
91   element_class = (GstElementClass *) klass;
92   vdec_class = (GstVideoDecoderClass *) klass;
93
94   parent_class = g_type_class_peek_parent (klass);
95
96   gobject_class->set_property = gst_webp_dec_set_property;
97   gobject_class->get_property = gst_webp_dec_get_property;
98
99   gst_element_class_add_static_pad_template (element_class,
100       &gst_webp_dec_src_pad_template);
101   gst_element_class_add_static_pad_template (element_class,
102       &gst_webp_dec_sink_pad_template);
103   gst_element_class_set_static_metadata (element_class, "WebP image decoder",
104       "Codec/Decoder/Image", "Decode images from WebP format",
105       "Sreerenj Balachandran <sreerenj.balachandrn@intel.com>");
106
107   g_object_class_install_property (gobject_class, PROP_BYPASS_FILTERING,
108       g_param_spec_boolean ("bypass-filtering", "Bypass Filtering",
109           "When enabled, skip the in-loop filtering", FALSE,
110           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111
112   g_object_class_install_property (gobject_class, PROP_NO_FANCY_UPSAMPLING,
113       g_param_spec_boolean ("no-fancy-upsampling", "No Fancy Upsampling",
114           "When enabled, use faster pointwise upsampler", FALSE,
115           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116
117   g_object_class_install_property (gobject_class, PROP_USE_THREADS,
118       g_param_spec_boolean ("use-threads", "Use Threads",
119           "When enabled, use multi-threaded decoding", FALSE,
120           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121
122   vdec_class->start = gst_webp_dec_start;
123   vdec_class->stop = gst_webp_dec_stop;
124   vdec_class->parse = gst_webp_dec_parse;
125   vdec_class->set_format = gst_webp_dec_set_format;
126   vdec_class->handle_frame = gst_webp_dec_handle_frame;
127   vdec_class->decide_allocation = gst_webp_dec_decide_allocation;
128   vdec_class->sink_event = gst_webp_dec_sink_event;
129
130   GST_DEBUG_CATEGORY_INIT (webp_dec_debug, "webpdec", 0, "WebP decoder");
131 }
132
133 static void
134 gst_webp_dec_init (GstWebPDec * dec)
135 {
136   GST_DEBUG ("Initialize the webp decoder");
137
138   memset (&dec->config, 0, sizeof (dec->config));
139   dec->saw_header = FALSE;
140
141   dec->bypass_filtering = FALSE;
142   dec->no_fancy_upsampling = FALSE;
143   dec->use_threads = FALSE;
144   gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
145       (dec), TRUE);
146   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec));
147 }
148
149 static gboolean
150 gst_webp_dec_reset_frame (GstWebPDec * webpdec)
151 {
152   GST_DEBUG ("Reset the current frame properties");
153
154   webpdec->saw_header = FALSE;
155
156   if (!WebPInitDecoderConfig (&webpdec->config)) {
157     GST_WARNING_OBJECT (webpdec,
158         "Failed to configure the WebP image decoding libraray");
159     return FALSE;
160   }
161   return TRUE;
162 }
163
164 static void
165 gst_webp_dec_set_property (GObject * object, guint prop_id,
166     const GValue * value, GParamSpec * pspec)
167 {
168   GstWebPDec *dec;
169
170   dec = GST_WEBP_DEC (object);
171
172   switch (prop_id) {
173     case PROP_BYPASS_FILTERING:
174       dec->bypass_filtering = g_value_get_boolean (value);
175       break;
176     case PROP_NO_FANCY_UPSAMPLING:
177       dec->no_fancy_upsampling = g_value_get_boolean (value);
178       break;
179     case PROP_USE_THREADS:
180       dec->use_threads = g_value_get_boolean (value);
181       break;
182
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186   }
187 }
188
189 static void
190 gst_webp_dec_get_property (GObject * object, guint prop_id, GValue * value,
191     GParamSpec * pspec)
192 {
193   GstWebPDec *dec;
194
195   dec = GST_WEBP_DEC (object);
196
197   switch (prop_id) {
198     case PROP_BYPASS_FILTERING:
199       g_value_set_boolean (value, dec->bypass_filtering);
200       break;
201     case PROP_NO_FANCY_UPSAMPLING:
202       g_value_set_boolean (value, dec->no_fancy_upsampling);
203       break;
204     case PROP_USE_THREADS:
205       g_value_set_boolean (value, dec->use_threads);
206       break;
207
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213
214 static gboolean
215 gst_webp_dec_start (GstVideoDecoder * decoder)
216 {
217   GstWebPDec *webpdec = (GstWebPDec *) decoder;
218
219   return gst_webp_dec_reset_frame (webpdec);
220 }
221
222 static gboolean
223 gst_webp_dec_stop (GstVideoDecoder * bdec)
224 {
225   GstWebPDec *webpdec = (GstWebPDec *) bdec;
226
227   if (webpdec->input_state) {
228     gst_video_codec_state_unref (webpdec->input_state);
229     webpdec->input_state = NULL;
230   }
231   if (webpdec->output_state) {
232     gst_video_codec_state_unref (webpdec->output_state);
233     webpdec->output_state = NULL;
234   }
235   return TRUE;
236 }
237
238 static gboolean
239 gst_webp_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
240 {
241   GstWebPDec *webpdec = (GstWebPDec *) decoder;
242
243   if (webpdec->input_state)
244     gst_video_codec_state_unref (webpdec->input_state);
245   webpdec->input_state = gst_video_codec_state_ref (state);
246
247   return TRUE;
248 }
249
250 static gboolean
251 gst_webp_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
252 {
253   GstBufferPool *pool = NULL;
254   GstStructure *config;
255
256   if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
257     return FALSE;
258
259   if (gst_query_get_n_allocation_pools (query) > 0)
260     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
261
262   if (pool == NULL)
263     return FALSE;
264
265   config = gst_buffer_pool_get_config (pool);
266   if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
267     gst_buffer_pool_config_add_option (config,
268         GST_BUFFER_POOL_OPTION_VIDEO_META);
269   }
270   gst_buffer_pool_set_config (pool, config);
271   gst_object_unref (pool);
272
273   return TRUE;
274 }
275
276 static gboolean
277 gst_webp_dec_sink_event (GstVideoDecoder * bdec, GstEvent * event)
278 {
279   const GstSegment *segment;
280
281   if (GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT)
282     goto done;
283
284   gst_event_parse_segment (event, &segment);
285
286   if (segment->format == GST_FORMAT_TIME)
287     gst_video_decoder_set_packetized (bdec, TRUE);
288   else
289     gst_video_decoder_set_packetized (bdec, FALSE);
290
291 done:
292   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (bdec, event);
293 }
294
295 static GstFlowReturn
296 gst_webp_dec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
297     GstAdapter * adapter, gboolean at_eos)
298 {
299   gsize toadd = 0;
300   gsize size;
301   gconstpointer data;
302   GstByteReader reader;
303   GstWebPDec *webpdec = (GstWebPDec *) decoder;
304
305   size = gst_adapter_available (adapter);
306   GST_DEBUG_OBJECT (decoder,
307       "parsing webp image data (%" G_GSIZE_FORMAT " bytes)", size);
308
309   if (at_eos) {
310     GST_DEBUG ("Flushing all data out");
311     toadd = size;
312
313     /* If we have leftover data, throw it away */
314     if (!webpdec->saw_header)
315       goto drop_frame;
316     goto have_full_frame;
317   }
318
319   if (!webpdec->saw_header) {
320     guint32 code;
321
322     if (size < 12)
323       goto need_more_data;
324
325     data = gst_adapter_map (adapter, size);
326     gst_byte_reader_init (&reader, data, size);
327
328     if (!gst_byte_reader_get_uint32_le (&reader, &code))
329       goto error;
330
331     if (code == GST_MAKE_FOURCC ('R', 'I', 'F', 'F')) {
332       if (!gst_byte_reader_get_uint32_le (&reader, &webpdec->frame_size))
333         goto error;
334
335       if (!gst_byte_reader_get_uint32_le (&reader, &code))
336         goto error;
337
338       if (code == GST_MAKE_FOURCC ('W', 'E', 'B', 'P'))
339         webpdec->saw_header = TRUE;
340
341     }
342   }
343
344   if (!webpdec->saw_header)
345     goto error;
346
347   if (size >= (webpdec->frame_size + 8)) {
348     toadd = webpdec->frame_size + 8;
349     webpdec->saw_header = FALSE;
350     goto have_full_frame;
351   }
352
353 need_more_data:
354   return GST_VIDEO_DECODER_FLOW_NEED_DATA;
355
356 have_full_frame:
357   if (toadd)
358     gst_video_decoder_add_to_frame (decoder, toadd);
359   return gst_video_decoder_have_frame (decoder);
360
361 drop_frame:
362   gst_adapter_flush (adapter, size);
363   return GST_FLOW_OK;
364
365 error:
366   return GST_FLOW_ERROR;
367 }
368
369 static GstFlowReturn
370 gst_webp_dec_update_src_caps (GstWebPDec * dec, GstMapInfo * map_info)
371 {
372   WebPBitstreamFeatures features;
373   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
374
375   if (WebPGetFeatures (map_info->data, map_info->size,
376           &features) != VP8_STATUS_OK) {
377     GST_ERROR_OBJECT (dec, "Failed to execute WebPGetFeatures");
378     return GST_FLOW_ERROR;
379   }
380
381   if (features.width < MIN_WIDTH || features.width > MAX_WIDTH
382       || features.height < MIN_HEIGHT || features.height > MAX_HEIGHT) {
383     GST_ERROR_OBJECT (dec, "Dimensions of the frame is unsupported by libwebp");
384     return GST_FLOW_ERROR;
385   }
386
387   /* TODO: Add support for other formats */
388   if (features.has_alpha) {
389     format = GST_VIDEO_FORMAT_ARGB;
390     dec->colorspace = MODE_ARGB;
391   } else {
392     format = GST_VIDEO_FORMAT_RGB;
393     dec->colorspace = MODE_RGB;
394   }
395
396   /* Check if output state changed */
397   if (dec->output_state) {
398     GstVideoInfo *info = &dec->output_state->info;
399
400     if (features.width == GST_VIDEO_INFO_WIDTH (info) &&
401         features.height == GST_VIDEO_INFO_HEIGHT (info) &&
402         GST_VIDEO_INFO_FORMAT (info) == format) {
403       goto beach;
404     }
405     gst_video_codec_state_unref (dec->output_state);
406   }
407
408   dec->output_state =
409       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec), format,
410       features.width, features.height, dec->input_state);
411
412   if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec)))
413     return GST_FLOW_NOT_NEGOTIATED;
414
415 beach:
416   return GST_FLOW_OK;
417 }
418
419 static GstFlowReturn
420 gst_webp_dec_handle_frame (GstVideoDecoder * decoder,
421     GstVideoCodecFrame * frame)
422 {
423   GstWebPDec *webpdec = (GstWebPDec *) decoder;
424   GstMapInfo map_info;
425   GstFlowReturn ret = GST_FLOW_OK;
426   GstVideoFrame vframe;
427
428   gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ);
429
430   ret = gst_webp_dec_update_src_caps (webpdec, &map_info);
431   if (ret != GST_FLOW_OK) {
432     gst_buffer_unmap (frame->input_buffer, &map_info);
433     gst_video_codec_frame_unref (frame);
434     goto done;
435   }
436
437   ret = gst_video_decoder_allocate_output_frame (decoder, frame);
438   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
439     GST_ERROR_OBJECT (decoder, "failed to allocate output frame");
440     ret = GST_FLOW_ERROR;
441     gst_buffer_unmap (frame->input_buffer, &map_info);
442     gst_video_codec_frame_unref (frame);
443
444     goto done;
445   }
446
447   if (!gst_video_frame_map (&vframe, &webpdec->output_state->info,
448           frame->output_buffer, GST_MAP_READWRITE)) {
449     GST_ERROR_OBJECT (decoder, "Failed to map output videoframe");
450     ret = GST_FLOW_ERROR;
451     gst_buffer_unmap (frame->input_buffer, &map_info);
452     gst_video_codec_frame_unref (frame);
453
454     goto done;
455   }
456
457   /* configure output buffer parameteres */
458   webpdec->config.options.bypass_filtering = webpdec->bypass_filtering;
459   webpdec->config.options.no_fancy_upsampling = webpdec->no_fancy_upsampling;
460   webpdec->config.options.use_threads = webpdec->use_threads;
461   webpdec->config.output.colorspace = webpdec->colorspace;
462   webpdec->config.output.u.RGBA.rgba = (uint8_t *) vframe.map[0].data;
463   webpdec->config.output.u.RGBA.stride =
464       GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0);
465   webpdec->config.output.u.RGBA.size = GST_VIDEO_FRAME_SIZE (&vframe);
466   webpdec->config.output.is_external_memory = 1;
467
468   if (WebPDecode (map_info.data, map_info.size,
469           &webpdec->config) != VP8_STATUS_OK) {
470     GST_ERROR_OBJECT (decoder, "Failed to decode the webp frame");
471     ret = GST_FLOW_ERROR;
472     gst_video_frame_unmap (&vframe);
473     gst_buffer_unmap (frame->input_buffer, &map_info);
474     gst_video_codec_frame_unref (frame);
475
476     goto done;
477   }
478
479   gst_video_frame_unmap (&vframe);
480   gst_buffer_unmap (frame->input_buffer, &map_info);
481
482   ret = gst_video_decoder_finish_frame (decoder, frame);
483
484   if (!gst_webp_dec_reset_frame (webpdec)) {
485     ret = GST_FLOW_ERROR;
486     goto done;
487   }
488
489 done:
490   return ret;
491 }
492
493 gboolean
494 gst_webp_dec_register (GstPlugin * plugin)
495 {
496   return gst_element_register (plugin, "webpdec",
497       GST_RANK_PRIMARY, GST_TYPE_WEBP_DEC);
498 }