2 * Copyright (C) <2013> Sreerenj Balachandran <sreerenj.balachandran@intel.com>
3 * Copyright (C) <2013> Intel Corporation
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstwebpdec.h"
29 #define MAX_WIDTH 16383
31 #define MAX_HEIGHT 16383
36 PROP_BYPASS_FILTERING,
37 PROP_NO_FANCY_UPSAMPLING,
41 static GstStaticPadTemplate gst_webp_dec_sink_pad_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("image/webp")
48 /*Fixme: Add YUV support */
49 static GstStaticPadTemplate gst_webp_dec_src_pad_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
54 ("{ RGB, RGBA, BGR, BGRA, ARGB, RGB16}"))
57 GST_DEBUG_CATEGORY_STATIC (webp_dec_debug);
58 #define GST_CAT_DEFAULT webp_dec_debug
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);
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,
75 static gboolean gst_webp_dec_sink_event (GstVideoDecoder * bdec,
78 static gboolean gst_webp_dec_reset_frame (GstWebPDec * webpdec);
80 #define gst_webp_dec_parent_class parent_class
81 G_DEFINE_TYPE (GstWebPDec, gst_webp_dec, GST_TYPE_VIDEO_DECODER);
84 gst_webp_dec_class_init (GstWebPDecClass * klass)
86 GObjectClass *gobject_class;
87 GstElementClass *element_class;
88 GstVideoDecoderClass *vdec_class;
90 gobject_class = (GObjectClass *) klass;
91 element_class = (GstElementClass *) klass;
92 vdec_class = (GstVideoDecoderClass *) klass;
94 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->set_property = gst_webp_dec_set_property;
97 gobject_class->get_property = gst_webp_dec_get_property;
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>");
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));
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));
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));
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;
130 GST_DEBUG_CATEGORY_INIT (webp_dec_debug, "webpdec", 0, "WebP decoder");
134 gst_webp_dec_init (GstWebPDec * dec)
136 GST_DEBUG ("Initialize the webp decoder");
138 memset (&dec->config, 0, sizeof (dec->config));
139 dec->saw_header = FALSE;
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
146 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec));
150 gst_webp_dec_reset_frame (GstWebPDec * webpdec)
152 GST_DEBUG ("Reset the current frame properties");
154 webpdec->saw_header = FALSE;
156 if (!WebPInitDecoderConfig (&webpdec->config)) {
157 GST_WARNING_OBJECT (webpdec,
158 "Failed to configure the WebP image decoding libraray");
165 gst_webp_dec_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec)
170 dec = GST_WEBP_DEC (object);
173 case PROP_BYPASS_FILTERING:
174 dec->bypass_filtering = g_value_get_boolean (value);
176 case PROP_NO_FANCY_UPSAMPLING:
177 dec->no_fancy_upsampling = g_value_get_boolean (value);
179 case PROP_USE_THREADS:
180 dec->use_threads = g_value_get_boolean (value);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 gst_webp_dec_get_property (GObject * object, guint prop_id, GValue * value,
195 dec = GST_WEBP_DEC (object);
198 case PROP_BYPASS_FILTERING:
199 g_value_set_boolean (value, dec->bypass_filtering);
201 case PROP_NO_FANCY_UPSAMPLING:
202 g_value_set_boolean (value, dec->no_fancy_upsampling);
204 case PROP_USE_THREADS:
205 g_value_set_boolean (value, dec->use_threads);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_webp_dec_start (GstVideoDecoder * decoder)
217 GstWebPDec *webpdec = (GstWebPDec *) decoder;
219 return gst_webp_dec_reset_frame (webpdec);
223 gst_webp_dec_stop (GstVideoDecoder * bdec)
225 GstWebPDec *webpdec = (GstWebPDec *) bdec;
227 if (webpdec->input_state) {
228 gst_video_codec_state_unref (webpdec->input_state);
229 webpdec->input_state = NULL;
231 if (webpdec->output_state) {
232 gst_video_codec_state_unref (webpdec->output_state);
233 webpdec->output_state = NULL;
239 gst_webp_dec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
241 GstWebPDec *webpdec = (GstWebPDec *) decoder;
243 if (webpdec->input_state)
244 gst_video_codec_state_unref (webpdec->input_state);
245 webpdec->input_state = gst_video_codec_state_ref (state);
251 gst_webp_dec_decide_allocation (GstVideoDecoder * bdec, GstQuery * query)
253 GstBufferPool *pool = NULL;
254 GstStructure *config;
256 if (!GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (bdec, query))
259 if (gst_query_get_n_allocation_pools (query) > 0)
260 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
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);
270 gst_buffer_pool_set_config (pool, config);
271 gst_object_unref (pool);
277 gst_webp_dec_sink_event (GstVideoDecoder * bdec, GstEvent * event)
279 const GstSegment *segment;
281 if (GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT)
284 gst_event_parse_segment (event, &segment);
286 if (segment->format == GST_FORMAT_TIME)
287 gst_video_decoder_set_packetized (bdec, TRUE);
289 gst_video_decoder_set_packetized (bdec, FALSE);
292 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (bdec, event);
296 gst_webp_dec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
297 GstAdapter * adapter, gboolean at_eos)
302 GstByteReader reader;
303 GstWebPDec *webpdec = (GstWebPDec *) decoder;
305 size = gst_adapter_available (adapter);
306 GST_DEBUG_OBJECT (decoder,
307 "parsing webp image data (%" G_GSIZE_FORMAT " bytes)", size);
310 GST_DEBUG ("Flushing all data out");
313 /* If we have leftover data, throw it away */
314 if (!webpdec->saw_header)
316 goto have_full_frame;
319 if (!webpdec->saw_header) {
325 data = gst_adapter_map (adapter, size);
326 gst_byte_reader_init (&reader, data, size);
328 if (!gst_byte_reader_get_uint32_le (&reader, &code))
331 if (code == GST_MAKE_FOURCC ('R', 'I', 'F', 'F')) {
332 if (!gst_byte_reader_get_uint32_le (&reader, &webpdec->frame_size))
335 if (!gst_byte_reader_get_uint32_le (&reader, &code))
338 if (code == GST_MAKE_FOURCC ('W', 'E', 'B', 'P'))
339 webpdec->saw_header = TRUE;
344 if (!webpdec->saw_header)
347 if (size >= (webpdec->frame_size + 8)) {
348 toadd = webpdec->frame_size + 8;
349 webpdec->saw_header = FALSE;
350 goto have_full_frame;
354 return GST_VIDEO_DECODER_FLOW_NEED_DATA;
358 gst_video_decoder_add_to_frame (decoder, toadd);
359 return gst_video_decoder_have_frame (decoder);
362 gst_adapter_flush (adapter, size);
366 return GST_FLOW_ERROR;
370 gst_webp_dec_update_src_caps (GstWebPDec * dec, GstMapInfo * map_info)
372 WebPBitstreamFeatures features;
373 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
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;
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;
387 /* TODO: Add support for other formats */
388 if (features.has_alpha) {
389 format = GST_VIDEO_FORMAT_ARGB;
390 dec->colorspace = MODE_ARGB;
392 format = GST_VIDEO_FORMAT_RGB;
393 dec->colorspace = MODE_RGB;
396 /* Check if output state changed */
397 if (dec->output_state) {
398 GstVideoInfo *info = &dec->output_state->info;
400 if (features.width == GST_VIDEO_INFO_WIDTH (info) &&
401 features.height == GST_VIDEO_INFO_HEIGHT (info) &&
402 GST_VIDEO_INFO_FORMAT (info) == format) {
405 gst_video_codec_state_unref (dec->output_state);
409 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (dec), format,
410 features.width, features.height, dec->input_state);
412 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (dec)))
413 return GST_FLOW_NOT_NEGOTIATED;
420 gst_webp_dec_handle_frame (GstVideoDecoder * decoder,
421 GstVideoCodecFrame * frame)
423 GstWebPDec *webpdec = (GstWebPDec *) decoder;
425 GstFlowReturn ret = GST_FLOW_OK;
426 GstVideoFrame vframe;
428 gst_buffer_map (frame->input_buffer, &map_info, GST_MAP_READ);
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);
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);
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);
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;
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);
479 gst_video_frame_unmap (&vframe);
480 gst_buffer_unmap (frame->input_buffer, &map_info);
482 ret = gst_video_decoder_finish_frame (decoder, frame);
484 if (!gst_webp_dec_reset_frame (webpdec)) {
485 ret = GST_FLOW_ERROR;
494 gst_webp_dec_register (GstPlugin * plugin)
496 return gst_element_register (plugin, "webpdec",
497 GST_RANK_PRIMARY, GST_TYPE_WEBP_DEC);