2 * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gstv4l2codecallocator.h"
25 #include "gstv4l2codecpool.h"
26 #include "gstv4l2codecvp8dec.h"
27 #include "linux/vp8-ctrls.h"
29 GST_DEBUG_CATEGORY_STATIC (v4l2_vp8dec_debug);
30 #define GST_CAT_DEFAULT v4l2_vp8dec_debug
38 static GstStaticPadTemplate sink_template =
39 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SINK_NAME,
40 GST_PAD_SINK, GST_PAD_ALWAYS,
41 GST_STATIC_CAPS ("video/x-vp8")
44 static GstStaticPadTemplate src_template =
45 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SRC_NAME,
46 GST_PAD_SRC, GST_PAD_ALWAYS,
47 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ NV12, YUY2 }")));
49 struct _GstV4l2CodecVp8Dec
52 GstV4l2Decoder *decoder;
53 GstVideoCodecState *output_state;
58 GstV4l2CodecAllocator *sink_allocator;
59 GstV4l2CodecAllocator *src_allocator;
60 GstV4l2CodecPool *src_pool;
62 gboolean has_videometa;
63 gboolean need_negotiation;
66 struct v4l2_ctrl_vp8_frame_header frame_header;
69 GstMapInfo bitstream_map;
72 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstV4l2CodecVp8Dec,
73 gst_v4l2_codec_vp8_dec, GST_TYPE_VP8_DECODER,
74 GST_DEBUG_CATEGORY_INIT (v4l2_vp8dec_debug, "v4l2codecs-vp8dec", 0,
75 "V4L2 stateless VP8 decoder"));
76 #define parent_class gst_v4l2_codec_vp8_dec_parent_class
79 gst_v4l2_codec_vp8_dec_open (GstVideoDecoder * decoder)
81 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
83 if (!gst_v4l2_decoder_open (self->decoder)) {
84 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
85 ("Failed to open VP8 decoder"),
86 ("gst_v4l2_decoder_open() failed: %s", g_strerror (errno)));
94 gst_v4l2_codec_vp8_dec_close (GstVideoDecoder * decoder)
96 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
97 gst_v4l2_decoder_close (self->decoder);
102 gst_v4l2_codec_vp8_dec_reset_allocation (GstV4l2CodecVp8Dec * self)
104 if (self->sink_allocator) {
105 gst_v4l2_codec_allocator_detach (self->sink_allocator);
106 g_clear_object (&self->sink_allocator);
109 if (self->src_allocator) {
110 gst_v4l2_codec_allocator_detach (self->src_allocator);
111 g_clear_object (&self->src_allocator);
112 g_clear_object (&self->src_pool);
117 gst_v4l2_codec_vp8_dec_stop (GstVideoDecoder * decoder)
119 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
121 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
122 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
124 gst_v4l2_codec_vp8_dec_reset_allocation (self);
126 if (self->output_state)
127 gst_video_codec_state_unref (self->output_state);
128 self->output_state = NULL;
130 return GST_VIDEO_DECODER_CLASS (parent_class)->stop (decoder);
134 gst_v4l2_codec_vp8_dec_negotiate (GstVideoDecoder * decoder)
136 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
137 GstVp8Decoder *vp8dec = GST_VP8_DECODER (decoder);
139 struct v4l2_ext_control control[] = {
141 .id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HEADER,
142 .ptr = &self->frame_header,
143 .size = sizeof (self->frame_header),
147 GstCaps *filter, *caps;
149 /* Ignore downstream renegotiation request. */
150 if (!self->need_negotiation)
152 self->need_negotiation = FALSE;
154 GST_DEBUG_OBJECT (self, "Negotiate");
156 gst_v4l2_codec_vp8_dec_reset_allocation (self);
158 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
159 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
161 if (!gst_v4l2_decoder_set_sink_fmt (self->decoder, V4L2_PIX_FMT_VP8_FRAME,
162 self->width, self->height)) {
163 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
164 ("Failed to configure VP8 decoder"),
165 ("gst_v4l2_decoder_set_sink_fmt() failed: %s", g_strerror (errno)));
166 gst_v4l2_decoder_close (self->decoder);
170 if (!gst_v4l2_decoder_set_controls (self->decoder, NULL, control,
171 G_N_ELEMENTS (control))) {
172 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
173 ("Driver does not support the selected stream."), (NULL));
177 filter = gst_v4l2_decoder_enum_src_formats (self->decoder);
178 GST_DEBUG_OBJECT (self, "Supported output formats: %" GST_PTR_FORMAT, filter);
180 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
181 gst_caps_unref (filter);
182 GST_DEBUG_OBJECT (self, "Peer supported formats: %" GST_PTR_FORMAT, caps);
184 if (!gst_v4l2_decoder_select_src_format (self->decoder, caps, &self->vinfo)) {
185 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
186 ("Unsupported pixel format"),
187 ("No support for %ux%u format %s", self->width, self->height,
188 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&self->vinfo))));
189 gst_caps_unref (caps);
192 gst_caps_unref (caps);
194 if (self->output_state)
195 gst_video_codec_state_unref (self->output_state);
198 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (self),
199 self->vinfo.finfo->format, self->width,
200 self->height, vp8dec->input_state);
202 self->output_state->caps = gst_video_info_to_caps (&self->output_state->info);
204 if (GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder)) {
205 if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SINK)) {
206 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
207 ("Could not enable the decoder driver."),
208 ("VIDIOC_STREAMON(SINK) failed: %s", g_strerror (errno)));
212 if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SRC)) {
213 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
214 ("Could not enable the decoder driver."),
215 ("VIDIOC_STREAMON(SRC) failed: %s", g_strerror (errno)));
226 gst_v4l2_codec_vp8_dec_decide_allocation (GstVideoDecoder * decoder,
229 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
232 self->has_videometa = gst_query_find_allocation_meta (query,
233 GST_VIDEO_META_API_TYPE, NULL);
235 g_clear_object (&self->src_pool);
236 g_clear_object (&self->src_allocator);
238 if (gst_query_get_n_allocation_pools (query) > 0)
239 gst_query_parse_nth_allocation_pool (query, 0, NULL, NULL, &min, NULL);
243 self->sink_allocator = gst_v4l2_codec_allocator_new (self->decoder,
244 GST_PAD_SINK, self->min_pool_size + 2);
245 self->src_allocator = gst_v4l2_codec_allocator_new (self->decoder,
246 GST_PAD_SRC, self->min_pool_size + min + 4);
247 self->src_pool = gst_v4l2_codec_pool_new (self->src_allocator, &self->vinfo);
249 /* Our buffer pool is internal, we will let the base class create a video
250 * pool, and use it if we are running out of buffers or if downstream does
251 * not support GstVideoMeta */
252 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
257 gst_v4l2_codec_vp8_dec_fill_segment_header (struct v4l2_vp8_segment_header
258 *segment_header, const GstVp8Segmentation * segmentation)
263 segment_header->flags =
264 (segmentation->segmentation_enabled ? V4L2_VP8_SEGMENT_HEADER_FLAG_ENABLED : 0) |
265 (segmentation->update_mb_segmentation_map ? V4L2_VP8_SEGMENT_HEADER_FLAG_UPDATE_MAP : 0) |
266 (segmentation->update_segment_feature_data ? V4L2_VP8_SEGMENT_HEADER_FLAG_UPDATE_FEATURE_DATA : 0) |
267 (segmentation->segment_feature_mode == 1 ? V4L2_VP8_SEGMENT_HEADER_FLAG_DELTA_VALUE_MODE : 0);
270 for (i = 0; i < 4; i++) {
271 segment_header->quant_update[i] = segmentation->quantizer_update_value[i];
272 segment_header->lf_update[i] = segmentation->lf_update_value[i];
275 for (i = 0; i < 3; i++)
276 segment_header->segment_probs[i] = segmentation->segment_prob[i];
278 segment_header->padding = 0;
282 gst_v4l2_codec_vp8_dec_fill_lf_header (struct v4l2_vp8_loopfilter_header
283 *lf_header, const GstVp8MbLfAdjustments * lf_adj)
288 (lf_adj->loop_filter_adj_enable ? V4L2_VP8_LF_HEADER_ADJ_ENABLE : 0) |
289 (lf_adj->mode_ref_lf_delta_update ? V4L2_VP8_LF_HEADER_DELTA_UPDATE : 0);
291 for (i = 0; i < 4; i++) {
292 lf_header->ref_frm_delta[i] = lf_adj->ref_frame_delta[i];
293 lf_header->mb_mode_delta[i] = lf_adj->mb_mode_delta[i];
298 gst_v4l2_codec_vp8_dec_fill_entropy_header (struct v4l2_vp8_entropy_header
299 *entropy_header, const GstVp8FrameHdr * frame_hdr)
301 memcpy (entropy_header->coeff_probs, frame_hdr->token_probs.prob,
302 sizeof (frame_hdr->token_probs.prob));
303 memcpy (entropy_header->y_mode_probs, frame_hdr->mode_probs.y_prob,
304 sizeof (frame_hdr->mode_probs.y_prob));
305 memcpy (entropy_header->uv_mode_probs, frame_hdr->mode_probs.uv_prob,
306 sizeof (frame_hdr->mode_probs.uv_prob));
307 memcpy (entropy_header->mv_probs, frame_hdr->mv_probs.prob,
308 sizeof (frame_hdr->mv_probs.prob));
312 gst_v4l2_codec_vp8_dec_fill_frame_header (GstV4l2CodecVp8Dec * self,
313 const GstVp8FrameHdr * frame_hdr)
318 self->frame_header = (struct v4l2_ctrl_vp8_frame_header) {
319 .lf_header = (struct v4l2_vp8_loopfilter_header) {
320 .sharpness_level = frame_hdr->sharpness_level,
321 .level = frame_hdr->loop_filter_level,
322 .flags = (frame_hdr->filter_type == 1 ? V4L2_VP8_LF_FILTER_TYPE_SIMPLE : 0)
324 .quant_header = (struct v4l2_vp8_quantization_header) {
325 .y_ac_qi = frame_hdr->quant_indices.y_ac_qi,
326 .y_dc_delta = frame_hdr->quant_indices.y_dc_delta,
327 .y2_dc_delta = frame_hdr->quant_indices.y2_dc_delta,
328 .y2_ac_delta = frame_hdr->quant_indices.y2_ac_delta,
329 .uv_dc_delta = frame_hdr->quant_indices.uv_dc_delta,
330 .uv_ac_delta = frame_hdr->quant_indices.uv_ac_delta
332 .coder_state = (struct v4l2_vp8_entropy_coder_state) {
333 .range = frame_hdr->rd_range,
334 .value = frame_hdr->rd_value,
335 .bit_count = frame_hdr->rd_count
338 .width = self->width,
339 .height = self->height,
341 .horizontal_scale = frame_hdr->horiz_scale_code,
342 .vertical_scale = frame_hdr->vert_scale_code,
344 .version = frame_hdr->version,
345 .prob_skip_false = frame_hdr->prob_skip_false,
346 .prob_intra = frame_hdr->prob_intra,
347 .prob_last = frame_hdr->prob_last,
348 .prob_gf = frame_hdr->prob_gf,
349 .num_dct_parts = 1 << frame_hdr->log2_nbr_of_dct_partitions,
351 .first_part_size = frame_hdr->first_part_size,
352 .first_part_header_bits = frame_hdr->header_size,
354 .flags = (frame_hdr->key_frame ? V4L2_VP8_FRAME_HEADER_FLAG_KEY_FRAME : 0) |
355 (frame_hdr->show_frame ? V4L2_VP8_FRAME_HEADER_FLAG_SHOW_FRAME : 0) |
356 (frame_hdr->mb_no_skip_coeff ? V4L2_VP8_FRAME_HEADER_FLAG_MB_NO_SKIP_COEFF : 0) |
357 (frame_hdr->sign_bias_golden ? V4L2_VP8_FRAME_HEADER_FLAG_SIGN_BIAS_GOLDEN : 0) |
358 (frame_hdr->sign_bias_alternate ? V4L2_VP8_FRAME_HEADER_FLAG_SIGN_BIAS_ALT : 0),
362 for (i = 0; i < 8; i++)
363 self->frame_header.dct_part_sizes[i] = frame_hdr->partition_size[i];
365 gst_v4l2_codec_vp8_dec_fill_entropy_header (&self->
366 frame_header.entropy_header, frame_hdr);
370 gst_v4l2_codec_vp8_dec_fill_references (GstV4l2CodecVp8Dec * self)
372 GstVp8Decoder *decoder = &self->parent;
374 if (decoder->last_picture)
375 self->frame_header.last_frame_ts =
376 decoder->last_picture->system_frame_number * 1000;
378 if (decoder->golden_ref_picture)
379 self->frame_header.golden_frame_ts =
380 decoder->golden_ref_picture->system_frame_number * 1000;
382 if (decoder->alt_ref_picture)
383 self->frame_header.alt_frame_ts =
384 decoder->alt_ref_picture->system_frame_number * 1000;
386 GST_DEBUG_OBJECT (self, "Passing references: last %u, golden %u, alt %u",
387 (guint32) self->frame_header.last_frame_ts / 1000,
388 (guint32) self->frame_header.golden_frame_ts / 1000,
389 (guint32) self->frame_header.alt_frame_ts / 1000);
393 gst_v4l2_codec_vp8_dec_new_sequence (GstVp8Decoder * decoder,
394 const GstVp8FrameHdr * frame_hdr)
396 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
397 gboolean negotiation_needed = FALSE;
399 if (self->vinfo.finfo->format == GST_VIDEO_FORMAT_UNKNOWN)
400 negotiation_needed = TRUE;
402 /* TODO Check if current buffers are large enough, and reuse them */
403 if (self->width != frame_hdr->width || self->height != frame_hdr->height) {
404 self->width = frame_hdr->width;
405 self->height = frame_hdr->height;
406 negotiation_needed = TRUE;
407 GST_INFO_OBJECT (self, "Resolution changed to %dx%d",
408 self->width, self->height);
411 gst_v4l2_codec_vp8_dec_fill_frame_header (self, frame_hdr);
413 if (negotiation_needed) {
414 self->need_negotiation = TRUE;
415 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
416 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
421 /* Check if we can zero-copy buffers */
422 if (!self->has_videometa) {
423 GstVideoInfo ref_vinfo;
426 gst_video_info_set_format (&ref_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
427 self->width, self->height);
429 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&self->vinfo); i++) {
430 if (self->vinfo.stride[i] != ref_vinfo.stride[i] ||
431 self->vinfo.offset[i] != ref_vinfo.offset[i]) {
432 GST_WARNING_OBJECT (self,
433 "GstVideoMeta support required, copying frames.");
434 self->copy_frames = TRUE;
439 self->copy_frames = FALSE;
446 gst_v4l2_codec_vp8_dec_start_picture (GstVp8Decoder * decoder,
447 GstVp8Picture * picture)
449 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
451 /* FIXME base class should not call us if negotiation failed */
452 if (!self->sink_allocator)
455 /* Ensure we have a bitstream to write into */
456 if (!self->bitstream) {
457 self->bitstream = gst_v4l2_codec_allocator_alloc (self->sink_allocator);
459 if (!self->bitstream) {
460 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
461 ("Not enough memory to decode VP8 stream."), (NULL));
465 if (!gst_memory_map (self->bitstream, &self->bitstream_map, GST_MAP_WRITE)) {
466 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
467 ("Could not access bitstream memory for writing"), (NULL));
468 g_clear_pointer (&self->bitstream, gst_memory_unref);
473 /* We use this field to track how much we have written */
474 self->bitstream_map.size = 0;
480 gst_v4l2_codec_vp8_dec_decode_picture (GstVp8Decoder * decoder,
481 GstVp8Picture * picture, GstVp8Parser * parser)
483 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
484 guint8 *bitstream_data = self->bitstream_map.data;
486 if (self->bitstream_map.maxsize < picture->size) {
487 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
488 ("Not enough space to send picture bitstream."), (NULL));
492 gst_v4l2_codec_vp8_dec_fill_frame_header (self, &picture->frame_hdr);
493 gst_v4l2_codec_vp8_dec_fill_segment_header (&self->
494 frame_header.segment_header, &parser->segmentation);
495 gst_v4l2_codec_vp8_dec_fill_lf_header (&self->frame_header.lf_header,
496 &parser->mb_lf_adjust);
497 gst_v4l2_codec_vp8_dec_fill_references (self);
499 memcpy (bitstream_data, picture->data, picture->size);
500 self->bitstream_map.size = picture->size;
506 gst_v4l2_codec_vp8_dec_reset_picture (GstV4l2CodecVp8Dec * self)
508 if (self->bitstream) {
509 if (self->bitstream_map.memory)
510 gst_memory_unmap (self->bitstream, &self->bitstream_map);
511 g_clear_pointer (&self->bitstream, gst_memory_unref);
512 self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
517 gst_v4l2_codec_vp8_dec_end_picture (GstVp8Decoder * decoder,
518 GstVp8Picture * picture)
520 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
521 GstVideoCodecFrame *frame;
522 GstV4l2Request *request;
524 GstFlowReturn flow_ret;
528 struct v4l2_ext_control control[] = {
530 .id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HEADER,
531 .ptr = &self->frame_header,
532 .size = sizeof(self->frame_header),
537 request = gst_v4l2_decoder_alloc_request (self->decoder);
539 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
540 ("Failed to allocate a media request object."), (NULL));
544 gst_vp8_picture_set_user_data (picture, request,
545 (GDestroyNotify) gst_v4l2_request_free);
547 flow_ret = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (self->src_pool),
549 if (flow_ret != GST_FLOW_OK) {
550 if (flow_ret == GST_FLOW_FLUSHING)
551 GST_DEBUG_OBJECT (self, "Frame decoding aborted, we are flushing.");
553 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
554 ("No more picture buffer available."), (NULL));
558 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
559 picture->system_frame_number);
560 g_return_val_if_fail (frame, FALSE);
561 g_warn_if_fail (frame->output_buffer == NULL);
562 frame->output_buffer = buffer;
563 gst_video_codec_frame_unref (frame);
565 if (!gst_v4l2_decoder_set_controls (self->decoder, request, control,
566 G_N_ELEMENTS (control))) {
567 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
568 ("Driver did not accept the bitstream parameters."), (NULL));
572 bytesused = self->bitstream_map.size;
573 gst_memory_unmap (self->bitstream, &self->bitstream_map);
574 self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
576 if (!gst_v4l2_decoder_queue_sink_mem (self->decoder, request, self->bitstream,
577 picture->system_frame_number, bytesused)) {
578 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
579 ("Driver did not accept the bitstream data."), (NULL));
584 if (!gst_v4l2_decoder_queue_src_buffer (self->decoder, buffer,
585 picture->system_frame_number)) {
586 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
587 ("Driver did not accept the picture buffer."), (NULL));
591 if (!gst_v4l2_request_queue (request)) {
592 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
593 ("Driver did not accept the decode request."), (NULL));
597 gst_v4l2_codec_vp8_dec_reset_picture (self);
601 gst_v4l2_codec_vp8_dec_reset_picture (self);
606 gst_v4l2_codec_vp8_dec_copy_output_buffer (GstV4l2CodecVp8Dec * self,
607 GstVideoCodecFrame * codec_frame)
609 GstVideoFrame src_frame;
610 GstVideoFrame dest_frame;
611 GstVideoInfo dest_vinfo;
614 gst_video_info_set_format (&dest_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
615 self->width, self->height);
617 buffer = gst_video_decoder_allocate_output_buffer (GST_VIDEO_DECODER (self));
621 if (!gst_video_frame_map (&src_frame, &self->vinfo,
622 codec_frame->output_buffer, GST_MAP_READ))
625 if (!gst_video_frame_map (&dest_frame, &dest_vinfo, buffer, GST_MAP_WRITE)) {
626 gst_video_frame_unmap (&dest_frame);
630 /* gst_video_frame_copy can crop this, but does not know, so let make it
631 * think it's all right */
632 GST_VIDEO_INFO_WIDTH (&src_frame.info) = self->width;
633 GST_VIDEO_INFO_HEIGHT (&src_frame.info) = self->height;
635 if (!gst_video_frame_copy (&dest_frame, &src_frame)) {
636 gst_video_frame_unmap (&src_frame);
637 gst_video_frame_unmap (&dest_frame);
641 gst_video_frame_unmap (&src_frame);
642 gst_video_frame_unmap (&dest_frame);
643 gst_buffer_replace (&codec_frame->output_buffer, buffer);
644 gst_buffer_unref (buffer);
649 GST_ERROR_OBJECT (self, "Failed copy output buffer.");
654 gst_v4l2_codec_vp8_dec_output_picture (GstVp8Decoder * decoder,
655 GstVp8Picture * picture)
657 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
658 GstV4l2Request *request = gst_vp8_picture_get_user_data (picture);
661 GstVideoCodecFrame *frame, *other_frame;
662 GstVp8Picture *other_pic;
663 GstV4l2Request *other_request;
665 GST_DEBUG_OBJECT (self, "Output picture %u", picture->system_frame_number);
667 if (gst_v4l2_request_is_done (request))
670 ret = gst_v4l2_request_poll (request, GST_SECOND);
672 GST_ELEMENT_ERROR (self, STREAM, DECODE,
673 ("Decoding frame took too long"), (NULL));
674 return GST_FLOW_ERROR;
675 } else if (ret < 0) {
676 GST_ELEMENT_ERROR (self, STREAM, DECODE,
677 ("Decoding request failed: %s", g_strerror (errno)), (NULL));
678 return GST_FLOW_ERROR;
682 if (!gst_v4l2_decoder_dequeue_src (self->decoder, &frame_num)) {
683 GST_ELEMENT_ERROR (self, STREAM, DECODE,
684 ("Decoder did not produce a frame"), (NULL));
685 return GST_FLOW_ERROR;
688 if (frame_num == picture->system_frame_number)
691 other_frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
693 g_return_val_if_fail (other_frame, GST_FLOW_ERROR);
695 other_pic = gst_video_codec_frame_get_user_data (other_frame);
696 other_request = gst_vp8_picture_get_user_data (other_pic);
697 gst_v4l2_request_set_done (other_request);
698 gst_video_codec_frame_unref (other_frame);
702 gst_v4l2_request_set_done (request);
703 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
704 picture->system_frame_number);
705 g_return_val_if_fail (frame, GST_FLOW_ERROR);
706 g_return_val_if_fail (frame->output_buffer, GST_FLOW_ERROR);
708 /* Hold on reference buffers for the rest of the picture lifetime */
709 gst_vp8_picture_set_user_data (picture,
710 gst_buffer_ref (frame->output_buffer), (GDestroyNotify) gst_buffer_unref);
712 if (self->copy_frames)
713 gst_v4l2_codec_vp8_dec_copy_output_buffer (self, frame);
715 return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
719 gst_v4l2_codec_vp8_dec_set_flushing (GstV4l2CodecVp8Dec * self,
722 if (self->sink_allocator)
723 gst_v4l2_codec_allocator_set_flushing (self->sink_allocator, flushing);
724 if (self->src_allocator)
725 gst_v4l2_codec_allocator_set_flushing (self->src_allocator, flushing);
729 gst_v4l2_codec_vp8_dec_flush (GstVideoDecoder * decoder)
731 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
733 GST_DEBUG_OBJECT (self, "Flushing decoder state.");
735 gst_v4l2_decoder_flush (self->decoder);
736 gst_v4l2_codec_vp8_dec_set_flushing (self, FALSE);
738 return GST_VIDEO_DECODER_CLASS (parent_class)->flush (decoder);
742 gst_v4l2_codec_vp8_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
744 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
746 switch (GST_EVENT_TYPE (event)) {
747 case GST_EVENT_FLUSH_START:
748 GST_DEBUG_OBJECT (self, "flush start");
749 gst_v4l2_codec_vp8_dec_set_flushing (self, TRUE);
755 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
758 static GstStateChangeReturn
759 gst_v4l2_codec_vp8_dec_change_state (GstElement * element,
760 GstStateChange transition)
762 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (element);
764 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
765 gst_v4l2_codec_vp8_dec_set_flushing (self, TRUE);
767 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
771 gst_v4l2_codec_vp8_dec_set_property (GObject * object, guint prop_id,
772 const GValue * value, GParamSpec * pspec)
774 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
775 GObject *dec = G_OBJECT (self->decoder);
779 gst_v4l2_decoder_set_property (dec, prop_id - PROP_LAST, value, pspec);
785 gst_v4l2_codec_vp8_dec_get_property (GObject * object, guint prop_id,
786 GValue * value, GParamSpec * pspec)
788 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
789 GObject *dec = G_OBJECT (self->decoder);
793 gst_v4l2_decoder_get_property (dec, prop_id - PROP_LAST, value, pspec);
799 gst_v4l2_codec_vp8_dec_init (GstV4l2CodecVp8Dec * self)
804 gst_v4l2_codec_vp8_dec_subinit (GstV4l2CodecVp8Dec * self,
805 GstV4l2CodecVp8DecClass * klass)
807 self->decoder = gst_v4l2_decoder_new (klass->device);
808 gst_video_info_init (&self->vinfo);
812 gst_v4l2_codec_vp8_dec_dispose (GObject * object)
814 GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
816 g_clear_object (&self->decoder);
818 G_OBJECT_CLASS (parent_class)->dispose (object);
822 gst_v4l2_codec_vp8_dec_class_init (GstV4l2CodecVp8DecClass * klass)
827 gst_v4l2_codec_vp8_dec_subclass_init (GstV4l2CodecVp8DecClass * klass,
828 GstV4l2CodecDevice * device)
830 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
831 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
832 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
833 GstVp8DecoderClass *vp8decoder_class = GST_VP8_DECODER_CLASS (klass);
835 gobject_class->set_property = gst_v4l2_codec_vp8_dec_set_property;
836 gobject_class->get_property = gst_v4l2_codec_vp8_dec_get_property;
837 gobject_class->dispose = gst_v4l2_codec_vp8_dec_dispose;
839 gst_element_class_set_static_metadata (element_class,
840 "V4L2 Stateless VP8 Video Decoder",
841 "Codec/Decoder/Video/Hardware",
842 "A V4L2 based VP8 video decoder",
843 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
845 gst_element_class_add_static_pad_template (element_class, &sink_template);
846 gst_element_class_add_static_pad_template (element_class, &src_template);
847 element_class->change_state =
848 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_change_state);
850 decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_open);
851 decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_close);
852 decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_stop);
853 decoder_class->negotiate =
854 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_negotiate);
855 decoder_class->decide_allocation =
856 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_decide_allocation);
857 decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_flush);
858 decoder_class->sink_event =
859 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_sink_event);
861 vp8decoder_class->new_sequence =
862 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_new_sequence);
863 vp8decoder_class->start_picture =
864 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_start_picture);
865 vp8decoder_class->decode_picture =
866 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_decode_picture);
867 vp8decoder_class->end_picture =
868 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_end_picture);
869 vp8decoder_class->output_picture =
870 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_output_picture);
872 klass->device = device;
873 gst_v4l2_decoder_install_properties (gobject_class, PROP_LAST, device);
877 gst_v4l2_codec_vp8_dec_register (GstPlugin * plugin,
878 GstV4l2CodecDevice * device, guint rank)
880 gst_v4l2_decoder_register (plugin, GST_TYPE_V4L2_CODEC_VP8_DEC,
881 (GClassInitFunc) gst_v4l2_codec_vp8_dec_subclass_init,
882 (GInstanceInitFunc) gst_v4l2_codec_vp8_dec_subinit,
883 "v4l2sl%svp8dec", device, rank);