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