• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2020 Nicolas Dufresne <nicolas.dufresne@collabora.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include "gstv4l2codecallocator.h"
25 #include "gstv4l2codecalphadecodebin.h"
26 #include "gstv4l2codecpool.h"
27 #include "gstv4l2codecvp8dec.h"
28 #include "gstv4l2format.h"
29 
30 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
31 
32 #define V4L2_MIN_KERNEL_VER_MAJOR 5
33 #define V4L2_MIN_KERNEL_VER_MINOR 13
34 #define V4L2_MIN_KERNEL_VERSION KERNEL_VERSION(V4L2_MIN_KERNEL_VER_MAJOR, V4L2_MIN_KERNEL_VER_MINOR, 0)
35 
36 GST_DEBUG_CATEGORY_STATIC (v4l2_vp8dec_debug);
37 #define GST_CAT_DEFAULT v4l2_vp8dec_debug
38 
39 enum
40 {
41   PROP_0,
42   PROP_LAST = PROP_0
43 };
44 
45 static GstStaticPadTemplate sink_template =
46 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SINK_NAME,
47     GST_PAD_SINK, GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-vp8")
49     );
50 
51 static GstStaticPadTemplate alpha_template =
52 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SINK_NAME,
53     GST_PAD_SINK, GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("video/x-vp8, codec-alpha = (boolean) true")
55     );
56 
57 static GstStaticPadTemplate src_template =
58 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SRC_NAME,
59     GST_PAD_SRC, GST_PAD_ALWAYS,
60     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_V4L2_DEFAULT_VIDEO_FORMATS)));
61 
62 struct _GstV4l2CodecVp8Dec
63 {
64   GstVp8Decoder parent;
65   GstV4l2Decoder *decoder;
66   GstVideoCodecState *output_state;
67   GstVideoInfo vinfo;
68   gint width;
69   gint height;
70 
71   GstV4l2CodecAllocator *sink_allocator;
72   GstV4l2CodecAllocator *src_allocator;
73   GstV4l2CodecPool *src_pool;
74   gint min_pool_size;
75   gboolean has_videometa;
76   gboolean need_negotiation;
77   gboolean copy_frames;
78 
79   struct v4l2_ctrl_vp8_frame frame_header;
80 
81   GstMemory *bitstream;
82   GstMapInfo bitstream_map;
83 };
84 
85 G_DEFINE_ABSTRACT_TYPE (GstV4l2CodecVp8Dec, gst_v4l2_codec_vp8_dec,
86     GST_TYPE_VP8_DECODER);
87 
88 #define parent_class gst_v4l2_codec_vp8_dec_parent_class
89 
90 static guint
gst_v4l2_codec_vp8_dec_get_preferred_output_delay(GstVp8Decoder * decoder,gboolean is_live)91 gst_v4l2_codec_vp8_dec_get_preferred_output_delay (GstVp8Decoder * decoder,
92     gboolean is_live)
93 {
94 
95   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
96   guint delay;
97 
98   if (is_live)
99     delay = 0;
100   else
101     /* Just one for now, perhaps we can make this configurable in the future. */
102     delay = 1;
103 
104   gst_v4l2_decoder_set_render_delay (self->decoder, delay);
105   return delay;
106 }
107 
108 static gboolean
gst_v4l2_codec_vp8_dec_open(GstVideoDecoder * decoder)109 gst_v4l2_codec_vp8_dec_open (GstVideoDecoder * decoder)
110 {
111   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
112   guint version;
113 
114   if (!gst_v4l2_decoder_open (self->decoder)) {
115     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
116         ("Failed to open VP8 decoder"),
117         ("gst_v4l2_decoder_open() failed: %s", g_strerror (errno)));
118     return FALSE;
119   }
120 
121   version = gst_v4l2_decoder_get_version (self->decoder);
122   if (version < V4L2_MIN_KERNEL_VERSION)
123     GST_WARNING_OBJECT (self,
124         "V4L2 API v%u.%u too old, at least v%u.%u required",
125         (version >> 16) & 0xff, (version >> 8) & 0xff,
126         V4L2_MIN_KERNEL_VER_MAJOR, V4L2_MIN_KERNEL_VER_MINOR);
127 
128   return TRUE;
129 }
130 
131 static gboolean
gst_v4l2_codec_vp8_dec_close(GstVideoDecoder * decoder)132 gst_v4l2_codec_vp8_dec_close (GstVideoDecoder * decoder)
133 {
134   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
135   gst_v4l2_decoder_close (self->decoder);
136   return TRUE;
137 }
138 
139 static void
gst_v4l2_codec_vp8_dec_reset_allocation(GstV4l2CodecVp8Dec * self)140 gst_v4l2_codec_vp8_dec_reset_allocation (GstV4l2CodecVp8Dec * self)
141 {
142   if (self->sink_allocator) {
143     gst_v4l2_codec_allocator_detach (self->sink_allocator);
144     g_clear_object (&self->sink_allocator);
145   }
146 
147   if (self->src_allocator) {
148     gst_v4l2_codec_allocator_detach (self->src_allocator);
149     g_clear_object (&self->src_allocator);
150     g_clear_object (&self->src_pool);
151   }
152 }
153 
154 static gboolean
gst_v4l2_codec_vp8_dec_stop(GstVideoDecoder * decoder)155 gst_v4l2_codec_vp8_dec_stop (GstVideoDecoder * decoder)
156 {
157   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
158 
159   gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
160   gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
161 
162   gst_v4l2_codec_vp8_dec_reset_allocation (self);
163 
164   if (self->output_state)
165     gst_video_codec_state_unref (self->output_state);
166   self->output_state = NULL;
167 
168   return GST_VIDEO_DECODER_CLASS (parent_class)->stop (decoder);
169 }
170 
171 static gboolean
gst_v4l2_codec_vp8_dec_negotiate(GstVideoDecoder * decoder)172 gst_v4l2_codec_vp8_dec_negotiate (GstVideoDecoder * decoder)
173 {
174   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
175   GstVp8Decoder *vp8dec = GST_VP8_DECODER (decoder);
176   /* *INDENT-OFF* */
177   struct v4l2_ext_control control[] = {
178     {
179       .id = V4L2_CID_STATELESS_VP8_FRAME,
180       .ptr = &self->frame_header,
181       .size = sizeof (self->frame_header),
182     },
183   };
184   /* *INDENT-ON* */
185   GstCaps *filter, *caps;
186 
187   /* Ignore downstream renegotiation request. */
188   if (!self->need_negotiation)
189     return TRUE;
190   self->need_negotiation = FALSE;
191 
192   GST_DEBUG_OBJECT (self, "Negotiate");
193 
194   gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
195   gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
196 
197   gst_v4l2_codec_vp8_dec_reset_allocation (self);
198 
199   if (!gst_v4l2_decoder_set_sink_fmt (self->decoder, V4L2_PIX_FMT_VP8_FRAME,
200           self->width, self->height, 12 /* 8 bits 4:2:0 */ )) {
201     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
202         ("Failed to configure VP8 decoder"),
203         ("gst_v4l2_decoder_set_sink_fmt() failed: %s", g_strerror (errno)));
204     gst_v4l2_decoder_close (self->decoder);
205     return FALSE;
206   }
207 
208   if (!gst_v4l2_decoder_set_controls (self->decoder, NULL, control,
209           G_N_ELEMENTS (control))) {
210     GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
211         ("Driver does not support the selected stream."), (NULL));
212     return FALSE;
213   }
214 
215   filter = gst_v4l2_decoder_enum_src_formats (self->decoder);
216   if (!filter) {
217     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
218         ("No supported decoder output formats"), (NULL));
219     return FALSE;
220   }
221   GST_DEBUG_OBJECT (self, "Supported output formats: %" GST_PTR_FORMAT, filter);
222 
223   caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
224   gst_caps_unref (filter);
225   GST_DEBUG_OBJECT (self, "Peer supported formats: %" GST_PTR_FORMAT, caps);
226 
227   if (!gst_v4l2_decoder_select_src_format (self->decoder, caps, &self->vinfo)) {
228     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
229         ("Unsupported pixel format"),
230         ("No support for %ux%u format %s", self->width, self->height,
231             gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&self->vinfo))));
232     gst_caps_unref (caps);
233     return FALSE;
234   }
235   gst_caps_unref (caps);
236 
237   if (self->output_state)
238     gst_video_codec_state_unref (self->output_state);
239 
240   self->output_state =
241       gst_video_decoder_set_output_state (GST_VIDEO_DECODER (self),
242       self->vinfo.finfo->format, self->width,
243       self->height, vp8dec->input_state);
244 
245   self->output_state->caps = gst_video_info_to_caps (&self->output_state->info);
246 
247   if (GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder)) {
248     if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SINK)) {
249       GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
250           ("Could not enable the decoder driver."),
251           ("VIDIOC_STREAMON(SINK) failed: %s", g_strerror (errno)));
252       return FALSE;
253     }
254 
255     if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SRC)) {
256       GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
257           ("Could not enable the decoder driver."),
258           ("VIDIOC_STREAMON(SRC) failed: %s", g_strerror (errno)));
259       return FALSE;
260     }
261 
262     return TRUE;
263   }
264 
265   return FALSE;
266 }
267 
268 static gboolean
gst_v4l2_codec_vp8_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)269 gst_v4l2_codec_vp8_dec_decide_allocation (GstVideoDecoder * decoder,
270     GstQuery * query)
271 {
272   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
273   guint min = 0;
274   guint num_bitstream;
275 
276   self->has_videometa = gst_query_find_allocation_meta (query,
277       GST_VIDEO_META_API_TYPE, NULL);
278 
279   g_clear_object (&self->src_pool);
280   g_clear_object (&self->src_allocator);
281 
282   if (gst_query_get_n_allocation_pools (query) > 0)
283     gst_query_parse_nth_allocation_pool (query, 0, NULL, NULL, &min, NULL);
284 
285   min = MAX (2, min);
286 
287   num_bitstream = 1 +
288       MAX (1, gst_v4l2_decoder_get_render_delay (self->decoder));
289 
290   self->sink_allocator = gst_v4l2_codec_allocator_new (self->decoder,
291       GST_PAD_SINK, num_bitstream);
292   if (!self->sink_allocator) {
293     GST_ELEMENT_ERROR (self, RESOURCE, NO_SPACE_LEFT,
294         ("Not enough memory to allocate sink buffers."), (NULL));
295     return FALSE;
296   }
297 
298   self->src_allocator = gst_v4l2_codec_allocator_new (self->decoder,
299       GST_PAD_SRC, self->min_pool_size + min + 4);
300   if (!self->src_allocator) {
301     GST_ELEMENT_ERROR (self, RESOURCE, NO_SPACE_LEFT,
302         ("Not enough memory to allocate source buffers."), (NULL));
303     g_clear_object (&self->sink_allocator);
304     return FALSE;
305   }
306 
307   self->src_pool = gst_v4l2_codec_pool_new (self->src_allocator, &self->vinfo);
308 
309   /* Our buffer pool is internal, we will let the base class create a video
310    * pool, and use it if we are running out of buffers or if downstream does
311    * not support GstVideoMeta */
312   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
313       (decoder, query);
314 }
315 
316 static void
gst_v4l2_codec_vp8_dec_fill_segment(struct v4l2_vp8_segment * segment,const GstVp8Segmentation * segmentation)317 gst_v4l2_codec_vp8_dec_fill_segment (struct v4l2_vp8_segment
318     *segment, const GstVp8Segmentation * segmentation)
319 {
320   gint i;
321 
322   /* *INDENT-OFF* */
323   segment->flags =
324     (segmentation->segmentation_enabled ? V4L2_VP8_SEGMENT_FLAG_ENABLED : 0) |
325     (segmentation->update_mb_segmentation_map ? V4L2_VP8_SEGMENT_FLAG_UPDATE_MAP : 0) |
326     (segmentation->update_segment_feature_data ? V4L2_VP8_SEGMENT_FLAG_UPDATE_FEATURE_DATA : 0) |
327     (segmentation->segment_feature_mode ? 0 : V4L2_VP8_SEGMENT_FLAG_DELTA_VALUE_MODE);
328   /* *INDENT-ON* */
329 
330   for (i = 0; i < 4; i++) {
331     segment->quant_update[i] = segmentation->quantizer_update_value[i];
332     segment->lf_update[i] = segmentation->lf_update_value[i];
333   }
334 
335   for (i = 0; i < 3; i++)
336     segment->segment_probs[i] = segmentation->segment_prob[i];
337 
338   segment->padding = 0;
339 }
340 
341 static void
gst_v4l2_codec_vp8_dec_fill_lf(struct v4l2_vp8_loop_filter * lf,const GstVp8MbLfAdjustments * lf_adj)342 gst_v4l2_codec_vp8_dec_fill_lf (struct v4l2_vp8_loop_filter
343     *lf, const GstVp8MbLfAdjustments * lf_adj)
344 {
345   gint i;
346 
347   lf->flags |=
348       (lf_adj->loop_filter_adj_enable ? V4L2_VP8_LF_ADJ_ENABLE : 0) |
349       (lf_adj->mode_ref_lf_delta_update ? V4L2_VP8_LF_DELTA_UPDATE : 0);
350 
351   for (i = 0; i < 4; i++) {
352     lf->ref_frm_delta[i] = lf_adj->ref_frame_delta[i];
353     lf->mb_mode_delta[i] = lf_adj->mb_mode_delta[i];
354   }
355 }
356 
357 static void
gst_v4l2_codec_vp8_dec_fill_entropy(struct v4l2_vp8_entropy * entropy,const GstVp8FrameHdr * frame_hdr)358 gst_v4l2_codec_vp8_dec_fill_entropy (struct v4l2_vp8_entropy
359     *entropy, const GstVp8FrameHdr * frame_hdr)
360 {
361   memcpy (entropy->coeff_probs, frame_hdr->token_probs.prob,
362       sizeof (frame_hdr->token_probs.prob));
363   memcpy (entropy->y_mode_probs, frame_hdr->mode_probs.y_prob,
364       sizeof (frame_hdr->mode_probs.y_prob));
365   memcpy (entropy->uv_mode_probs, frame_hdr->mode_probs.uv_prob,
366       sizeof (frame_hdr->mode_probs.uv_prob));
367   memcpy (entropy->mv_probs, frame_hdr->mv_probs.prob,
368       sizeof (frame_hdr->mv_probs.prob));
369 }
370 
371 static void
gst_v4l2_codec_vp8_dec_fill_frame_header(GstV4l2CodecVp8Dec * self,const GstVp8FrameHdr * frame_hdr)372 gst_v4l2_codec_vp8_dec_fill_frame_header (GstV4l2CodecVp8Dec * self,
373     const GstVp8FrameHdr * frame_hdr)
374 {
375   gint i;
376 
377   /* *INDENT-OFF* */
378   self->frame_header = (struct v4l2_ctrl_vp8_frame) {
379     .lf = (struct v4l2_vp8_loop_filter) {
380       .sharpness_level = frame_hdr->sharpness_level,
381       .level = frame_hdr->loop_filter_level,
382       .flags = (frame_hdr->filter_type == 1 ? V4L2_VP8_LF_FILTER_TYPE_SIMPLE : 0)
383     },
384     .quant = (struct v4l2_vp8_quantization) {
385       .y_ac_qi = frame_hdr->quant_indices.y_ac_qi,
386       .y_dc_delta = frame_hdr->quant_indices.y_dc_delta,
387       .y2_dc_delta = frame_hdr->quant_indices.y2_dc_delta,
388       .y2_ac_delta = frame_hdr->quant_indices.y2_ac_delta,
389       .uv_dc_delta = frame_hdr->quant_indices.uv_dc_delta,
390       .uv_ac_delta = frame_hdr->quant_indices.uv_ac_delta
391     },
392     .coder_state = (struct v4l2_vp8_entropy_coder_state) {
393       .range = frame_hdr->rd_range,
394       .value = frame_hdr->rd_value,
395       .bit_count = frame_hdr->rd_count
396     },
397 
398     .width = self->width,
399     .height = self->height,
400 
401     .horizontal_scale = frame_hdr->horiz_scale_code,
402     .vertical_scale = frame_hdr->vert_scale_code,
403 
404     .version = frame_hdr->version,
405     .prob_skip_false = frame_hdr->prob_skip_false,
406     .prob_intra = frame_hdr->prob_intra,
407     .prob_last = frame_hdr->prob_last,
408     .prob_gf = frame_hdr->prob_gf,
409     .num_dct_parts = 1 << frame_hdr->log2_nbr_of_dct_partitions,
410 
411     .first_part_size = frame_hdr->first_part_size,
412     .first_part_header_bits = frame_hdr->header_size,
413 
414     .flags = (frame_hdr->key_frame ? V4L2_VP8_FRAME_FLAG_KEY_FRAME : 0) |
415              (frame_hdr->show_frame ? V4L2_VP8_FRAME_FLAG_SHOW_FRAME : 0) |
416              (frame_hdr->mb_no_skip_coeff ? V4L2_VP8_FRAME_FLAG_MB_NO_SKIP_COEFF : 0) |
417              (frame_hdr->sign_bias_golden ? V4L2_VP8_FRAME_FLAG_SIGN_BIAS_GOLDEN : 0) |
418              (frame_hdr->sign_bias_alternate ? V4L2_VP8_FRAME_FLAG_SIGN_BIAS_ALT : 0),
419   };
420   /* *INDENT-ON* */
421 
422   for (i = 0; i < 8; i++)
423     self->frame_header.dct_part_sizes[i] = frame_hdr->partition_size[i];
424 
425   gst_v4l2_codec_vp8_dec_fill_entropy (&self->frame_header.entropy, frame_hdr);
426 }
427 
428 static void
gst_v4l2_codec_vp8_dec_fill_references(GstV4l2CodecVp8Dec * self)429 gst_v4l2_codec_vp8_dec_fill_references (GstV4l2CodecVp8Dec * self)
430 {
431   GstVp8Decoder *decoder = &self->parent;
432 
433   if (decoder->last_picture)
434     self->frame_header.last_frame_ts =
435         decoder->last_picture->system_frame_number * 1000;
436 
437   if (decoder->golden_ref_picture)
438     self->frame_header.golden_frame_ts =
439         decoder->golden_ref_picture->system_frame_number * 1000;
440 
441   if (decoder->alt_ref_picture)
442     self->frame_header.alt_frame_ts =
443         decoder->alt_ref_picture->system_frame_number * 1000;
444 
445   GST_DEBUG_OBJECT (self, "Passing references: last %u, golden %u, alt %u",
446       (guint32) self->frame_header.last_frame_ts / 1000,
447       (guint32) self->frame_header.golden_frame_ts / 1000,
448       (guint32) self->frame_header.alt_frame_ts / 1000);
449 }
450 
451 static GstFlowReturn
gst_v4l2_codec_vp8_dec_new_sequence(GstVp8Decoder * decoder,const GstVp8FrameHdr * frame_hdr)452 gst_v4l2_codec_vp8_dec_new_sequence (GstVp8Decoder * decoder,
453     const GstVp8FrameHdr * frame_hdr)
454 {
455   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
456   gboolean negotiation_needed = FALSE;
457 
458   if (self->vinfo.finfo->format == GST_VIDEO_FORMAT_UNKNOWN)
459     negotiation_needed = TRUE;
460 
461   /* TODO Check if current buffers are large enough, and reuse them */
462   if (self->width != frame_hdr->width || self->height != frame_hdr->height) {
463     self->width = frame_hdr->width;
464     self->height = frame_hdr->height;
465     negotiation_needed = TRUE;
466     GST_INFO_OBJECT (self, "Resolution changed to %dx%d",
467         self->width, self->height);
468   }
469 
470   gst_v4l2_codec_vp8_dec_fill_frame_header (self, frame_hdr);
471 
472   if (negotiation_needed) {
473     self->need_negotiation = TRUE;
474     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
475       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
476       return GST_FLOW_NOT_NEGOTIATED;
477     }
478   }
479 
480   /* Check if we can zero-copy buffers */
481   if (!self->has_videometa) {
482     GstVideoInfo ref_vinfo;
483     gint i;
484 
485     gst_video_info_set_format (&ref_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
486         self->width, self->height);
487 
488     for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&self->vinfo); i++) {
489       if (self->vinfo.stride[i] != ref_vinfo.stride[i] ||
490           self->vinfo.offset[i] != ref_vinfo.offset[i]) {
491         GST_WARNING_OBJECT (self,
492             "GstVideoMeta support required, copying frames.");
493         self->copy_frames = TRUE;
494         break;
495       }
496     }
497   } else {
498     self->copy_frames = FALSE;
499   }
500 
501   return GST_FLOW_OK;
502 }
503 
504 static GstFlowReturn
gst_v4l2_codec_vp8_dec_start_picture(GstVp8Decoder * decoder,GstVp8Picture * picture)505 gst_v4l2_codec_vp8_dec_start_picture (GstVp8Decoder * decoder,
506     GstVp8Picture * picture)
507 {
508   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
509 
510   /* FIXME base class should not call us if negotiation failed */
511   if (!self->sink_allocator)
512     return GST_FLOW_NOT_NEGOTIATED;
513 
514   /* Ensure we have a bitstream to write into */
515   if (!self->bitstream) {
516     self->bitstream = gst_v4l2_codec_allocator_alloc (self->sink_allocator);
517 
518     if (!self->bitstream) {
519       GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
520           ("Not enough memory to decode VP8 stream."), (NULL));
521       return GST_FLOW_ERROR;
522     }
523 
524     if (!gst_memory_map (self->bitstream, &self->bitstream_map, GST_MAP_WRITE)) {
525       GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
526           ("Could not access bitstream memory for writing"), (NULL));
527       g_clear_pointer (&self->bitstream, gst_memory_unref);
528       return GST_FLOW_ERROR;
529     }
530   }
531 
532   /* We use this field to track how much we have written */
533   self->bitstream_map.size = 0;
534 
535   return GST_FLOW_OK;
536 }
537 
538 static GstFlowReturn
gst_v4l2_codec_vp8_dec_decode_picture(GstVp8Decoder * decoder,GstVp8Picture * picture,GstVp8Parser * parser)539 gst_v4l2_codec_vp8_dec_decode_picture (GstVp8Decoder * decoder,
540     GstVp8Picture * picture, GstVp8Parser * parser)
541 {
542   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
543   guint8 *bitstream_data = self->bitstream_map.data;
544 
545   if (self->bitstream_map.maxsize < picture->size) {
546     GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
547         ("Not enough space to send picture bitstream."), (NULL));
548     return GST_FLOW_ERROR;
549   }
550 
551   gst_v4l2_codec_vp8_dec_fill_frame_header (self, &picture->frame_hdr);
552   gst_v4l2_codec_vp8_dec_fill_segment (&self->frame_header.segment,
553       &parser->segmentation);
554   gst_v4l2_codec_vp8_dec_fill_lf (&self->frame_header.lf,
555       &parser->mb_lf_adjust);
556   gst_v4l2_codec_vp8_dec_fill_references (self);
557 
558   memcpy (bitstream_data, picture->data, picture->size);
559   self->bitstream_map.size = picture->size;
560 
561   return GST_FLOW_OK;
562 }
563 
564 static void
gst_v4l2_codec_vp8_dec_reset_picture(GstV4l2CodecVp8Dec * self)565 gst_v4l2_codec_vp8_dec_reset_picture (GstV4l2CodecVp8Dec * self)
566 {
567   if (self->bitstream) {
568     if (self->bitstream_map.memory)
569       gst_memory_unmap (self->bitstream, &self->bitstream_map);
570     g_clear_pointer (&self->bitstream, gst_memory_unref);
571     self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
572   }
573 }
574 
575 static GstFlowReturn
gst_v4l2_codec_vp8_dec_end_picture(GstVp8Decoder * decoder,GstVp8Picture * picture)576 gst_v4l2_codec_vp8_dec_end_picture (GstVp8Decoder * decoder,
577     GstVp8Picture * picture)
578 {
579   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
580   GstVideoCodecFrame *frame;
581   GstV4l2Request *request;
582   GstBuffer *buffer;
583   GstFlowReturn flow_ret = GST_FLOW_OK;
584   gsize bytesused;
585 
586   /* *INDENT-OFF* */
587   struct v4l2_ext_control control[] = {
588     {
589       .id = V4L2_CID_STATELESS_VP8_FRAME,
590       .ptr = &self->frame_header,
591       .size = sizeof(self->frame_header),
592     },
593   };
594   /* *INDENT-ON* */
595 
596   bytesused = self->bitstream_map.size;
597   gst_memory_unmap (self->bitstream, &self->bitstream_map);
598   self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
599   gst_memory_resize (self->bitstream, 0, bytesused);
600 
601   flow_ret = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (self->src_pool),
602       &buffer, NULL);
603   if (flow_ret != GST_FLOW_OK) {
604     if (flow_ret == GST_FLOW_FLUSHING)
605       GST_DEBUG_OBJECT (self, "Frame decoding aborted, we are flushing.");
606     else
607       GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
608           ("No more picture buffer available."), (NULL));
609     goto fail;
610   }
611 
612   frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
613       picture->system_frame_number);
614   g_return_val_if_fail (frame, GST_FLOW_ERROR);
615   g_warn_if_fail (frame->output_buffer == NULL);
616   frame->output_buffer = buffer;
617   gst_video_codec_frame_unref (frame);
618 
619   request = gst_v4l2_decoder_alloc_request (self->decoder,
620       picture->system_frame_number, self->bitstream, buffer);
621   if (!request) {
622     GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
623         ("Failed to allocate a media request object."), (NULL));
624     goto fail;
625   }
626 
627   gst_vp8_picture_set_user_data (picture, request,
628       (GDestroyNotify) gst_v4l2_request_unref);
629 
630   if (!gst_v4l2_decoder_set_controls (self->decoder, request, control,
631           G_N_ELEMENTS (control))) {
632     GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
633         ("Driver did not accept the bitstream parameters."), (NULL));
634     goto fail;
635   }
636 
637   if (!gst_v4l2_request_queue (request, 0)) {
638     GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
639         ("Driver did not accept the decode request."), (NULL));
640     goto fail;
641   }
642 
643   gst_v4l2_codec_vp8_dec_reset_picture (self);
644 
645   return GST_FLOW_OK;
646 
647 fail:
648   gst_v4l2_codec_vp8_dec_reset_picture (self);
649 
650   if (flow_ret != GST_FLOW_OK)
651     return flow_ret;
652 
653   return GST_FLOW_ERROR;
654 }
655 
656 static gboolean
gst_v4l2_codec_vp8_dec_copy_output_buffer(GstV4l2CodecVp8Dec * self,GstVideoCodecFrame * codec_frame)657 gst_v4l2_codec_vp8_dec_copy_output_buffer (GstV4l2CodecVp8Dec * self,
658     GstVideoCodecFrame * codec_frame)
659 {
660   GstVideoFrame src_frame;
661   GstVideoFrame dest_frame;
662   GstVideoInfo dest_vinfo;
663   GstBuffer *buffer;
664 
665   gst_video_info_set_format (&dest_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
666       self->width, self->height);
667 
668   buffer = gst_video_decoder_allocate_output_buffer (GST_VIDEO_DECODER (self));
669   if (!buffer)
670     goto fail;
671 
672   if (!gst_video_frame_map (&src_frame, &self->vinfo,
673           codec_frame->output_buffer, GST_MAP_READ))
674     goto fail;
675 
676   if (!gst_video_frame_map (&dest_frame, &dest_vinfo, buffer, GST_MAP_WRITE)) {
677     gst_video_frame_unmap (&dest_frame);
678     goto fail;
679   }
680 
681   /* gst_video_frame_copy can crop this, but does not know, so let make it
682    * think it's all right */
683   GST_VIDEO_INFO_WIDTH (&src_frame.info) = self->width;
684   GST_VIDEO_INFO_HEIGHT (&src_frame.info) = self->height;
685 
686   if (!gst_video_frame_copy (&dest_frame, &src_frame)) {
687     gst_video_frame_unmap (&src_frame);
688     gst_video_frame_unmap (&dest_frame);
689     goto fail;
690   }
691 
692   gst_video_frame_unmap (&src_frame);
693   gst_video_frame_unmap (&dest_frame);
694   gst_buffer_replace (&codec_frame->output_buffer, buffer);
695   gst_buffer_unref (buffer);
696 
697   return TRUE;
698 
699 fail:
700   GST_ERROR_OBJECT (self, "Failed copy output buffer.");
701   return FALSE;
702 }
703 
704 static GstFlowReturn
gst_v4l2_codec_vp8_dec_output_picture(GstVp8Decoder * decoder,GstVideoCodecFrame * frame,GstVp8Picture * picture)705 gst_v4l2_codec_vp8_dec_output_picture (GstVp8Decoder * decoder,
706     GstVideoCodecFrame * frame, GstVp8Picture * picture)
707 {
708   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
709   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
710   GstV4l2Request *request = gst_vp8_picture_get_user_data (picture);
711   gint ret;
712 
713   GST_DEBUG_OBJECT (self, "Output picture %u", picture->system_frame_number);
714 
715   ret = gst_v4l2_request_set_done (request);
716   if (ret == 0) {
717     GST_ELEMENT_ERROR (self, STREAM, DECODE,
718         ("Decoding frame took too long"), (NULL));
719     goto error;
720   } else if (ret < 0) {
721     GST_ELEMENT_ERROR (self, STREAM, DECODE,
722         ("Decoding request failed: %s", g_strerror (errno)), (NULL));
723     goto error;
724   }
725 
726   g_return_val_if_fail (frame->output_buffer, GST_FLOW_ERROR);
727 
728   if (gst_v4l2_request_failed (request)) {
729     GST_ELEMENT_ERROR (self, STREAM, DECODE,
730         ("Failed to decode frame %u", picture->system_frame_number), (NULL));
731     goto error;
732   }
733 
734   /* Hold on reference buffers for the rest of the picture lifetime */
735   gst_vp8_picture_set_user_data (picture,
736       gst_buffer_ref (frame->output_buffer), (GDestroyNotify) gst_buffer_unref);
737 
738   if (self->copy_frames)
739     gst_v4l2_codec_vp8_dec_copy_output_buffer (self, frame);
740 
741   gst_vp8_picture_unref (picture);
742 
743   return gst_video_decoder_finish_frame (vdec, frame);
744 
745 error:
746   gst_video_decoder_drop_frame (vdec, frame);
747   gst_vp8_picture_unref (picture);
748 
749   return GST_FLOW_ERROR;
750 }
751 
752 static void
gst_v4l2_codec_vp8_dec_set_flushing(GstV4l2CodecVp8Dec * self,gboolean flushing)753 gst_v4l2_codec_vp8_dec_set_flushing (GstV4l2CodecVp8Dec * self,
754     gboolean flushing)
755 {
756   if (self->sink_allocator)
757     gst_v4l2_codec_allocator_set_flushing (self->sink_allocator, flushing);
758   if (self->src_allocator)
759     gst_v4l2_codec_allocator_set_flushing (self->src_allocator, flushing);
760 }
761 
762 static gboolean
gst_v4l2_codec_vp8_dec_flush(GstVideoDecoder * decoder)763 gst_v4l2_codec_vp8_dec_flush (GstVideoDecoder * decoder)
764 {
765   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
766 
767   GST_DEBUG_OBJECT (self, "Flushing decoder state.");
768 
769   gst_v4l2_decoder_flush (self->decoder);
770   gst_v4l2_codec_vp8_dec_set_flushing (self, FALSE);
771 
772   return GST_VIDEO_DECODER_CLASS (parent_class)->flush (decoder);
773 }
774 
775 static gboolean
gst_v4l2_codec_vp8_dec_sink_event(GstVideoDecoder * decoder,GstEvent * event)776 gst_v4l2_codec_vp8_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
777 {
778   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (decoder);
779 
780   switch (GST_EVENT_TYPE (event)) {
781     case GST_EVENT_FLUSH_START:
782       GST_DEBUG_OBJECT (self, "flush start");
783       gst_v4l2_codec_vp8_dec_set_flushing (self, TRUE);
784       break;
785     default:
786       break;
787   }
788 
789   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
790 }
791 
792 static GstStateChangeReturn
gst_v4l2_codec_vp8_dec_change_state(GstElement * element,GstStateChange transition)793 gst_v4l2_codec_vp8_dec_change_state (GstElement * element,
794     GstStateChange transition)
795 {
796   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (element);
797 
798   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
799     gst_v4l2_codec_vp8_dec_set_flushing (self, TRUE);
800 
801   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
802 }
803 
804 static void
gst_v4l2_codec_vp8_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)805 gst_v4l2_codec_vp8_dec_set_property (GObject * object, guint prop_id,
806     const GValue * value, GParamSpec * pspec)
807 {
808   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
809   GObject *dec = G_OBJECT (self->decoder);
810 
811   switch (prop_id) {
812     default:
813       gst_v4l2_decoder_set_property (dec, prop_id - PROP_LAST, value, pspec);
814       break;
815   }
816 }
817 
818 static void
gst_v4l2_codec_vp8_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)819 gst_v4l2_codec_vp8_dec_get_property (GObject * object, guint prop_id,
820     GValue * value, GParamSpec * pspec)
821 {
822   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
823   GObject *dec = G_OBJECT (self->decoder);
824 
825   switch (prop_id) {
826     default:
827       gst_v4l2_decoder_get_property (dec, prop_id - PROP_LAST, value, pspec);
828       break;
829   }
830 }
831 
832 static void
gst_v4l2_codec_vp8_dec_init(GstV4l2CodecVp8Dec * self)833 gst_v4l2_codec_vp8_dec_init (GstV4l2CodecVp8Dec * self)
834 {
835 }
836 
837 static void
gst_v4l2_codec_vp8_dec_subinit(GstV4l2CodecVp8Dec * self,GstV4l2CodecVp8DecClass * klass)838 gst_v4l2_codec_vp8_dec_subinit (GstV4l2CodecVp8Dec * self,
839     GstV4l2CodecVp8DecClass * klass)
840 {
841   self->decoder = gst_v4l2_decoder_new (klass->device);
842   gst_video_info_init (&self->vinfo);
843 }
844 
845 static void
gst_v4l2_codec_vp8_dec_dispose(GObject * object)846 gst_v4l2_codec_vp8_dec_dispose (GObject * object)
847 {
848   GstV4l2CodecVp8Dec *self = GST_V4L2_CODEC_VP8_DEC (object);
849 
850   g_clear_object (&self->decoder);
851 
852   G_OBJECT_CLASS (parent_class)->dispose (object);
853 }
854 
855 static void
gst_v4l2_codec_vp8_dec_class_init(GstV4l2CodecVp8DecClass * klass)856 gst_v4l2_codec_vp8_dec_class_init (GstV4l2CodecVp8DecClass * klass)
857 {
858 }
859 
860 static void
gst_v4l2_codec_vp8_dec_subclass_init(GstV4l2CodecVp8DecClass * klass,GstV4l2CodecDevice * device)861 gst_v4l2_codec_vp8_dec_subclass_init (GstV4l2CodecVp8DecClass * klass,
862     GstV4l2CodecDevice * device)
863 {
864   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
865   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
866   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
867   GstVp8DecoderClass *vp8decoder_class = GST_VP8_DECODER_CLASS (klass);
868 
869   gobject_class->set_property = gst_v4l2_codec_vp8_dec_set_property;
870   gobject_class->get_property = gst_v4l2_codec_vp8_dec_get_property;
871   gobject_class->dispose = gst_v4l2_codec_vp8_dec_dispose;
872 
873   gst_element_class_set_static_metadata (element_class,
874       "V4L2 Stateless VP8 Video Decoder",
875       "Codec/Decoder/Video/Hardware",
876       "A V4L2 based VP8 video decoder",
877       "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
878 
879   gst_element_class_add_static_pad_template (element_class, &sink_template);
880   gst_element_class_add_static_pad_template (element_class, &src_template);
881   element_class->change_state =
882       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_change_state);
883 
884   decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_open);
885   decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_close);
886   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_stop);
887   decoder_class->negotiate =
888       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_negotiate);
889   decoder_class->decide_allocation =
890       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_decide_allocation);
891   decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_flush);
892   decoder_class->sink_event =
893       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_sink_event);
894 
895   vp8decoder_class->new_sequence =
896       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_new_sequence);
897   vp8decoder_class->start_picture =
898       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_start_picture);
899   vp8decoder_class->decode_picture =
900       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_decode_picture);
901   vp8decoder_class->end_picture =
902       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_end_picture);
903   vp8decoder_class->output_picture =
904       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_output_picture);
905   vp8decoder_class->get_preferred_output_delay =
906       GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp8_dec_get_preferred_output_delay);
907 
908   klass->device = device;
909   gst_v4l2_decoder_install_properties (gobject_class, PROP_LAST, device);
910 }
911 
gst_v4l2_codec_vp8_alpha_decode_bin_subclass_init(GstV4l2CodecAlphaDecodeBinClass * klass,gchar * decoder_name)912 static void gst_v4l2_codec_vp8_alpha_decode_bin_subclass_init
913     (GstV4l2CodecAlphaDecodeBinClass * klass, gchar * decoder_name)
914 {
915   GstV4l2CodecAlphaDecodeBinClass *adbin_class =
916       (GstV4l2CodecAlphaDecodeBinClass *) klass;
917   GstElementClass *element_class = (GstElementClass *) klass;
918 
919   adbin_class->decoder_name = decoder_name;
920   gst_element_class_add_static_pad_template (element_class, &alpha_template);
921 
922   gst_element_class_set_static_metadata (element_class,
923       "VP8 Alpha Decoder", "Codec/Decoder/Video",
924       "Wrapper bin to decode VP8 with alpha stream.",
925       "Daniel Almeida <daniel.almeida@collabora.com>");
926 }
927 
928 void
gst_v4l2_codec_vp8_dec_register(GstPlugin * plugin,GstV4l2Decoder * decoder,GstV4l2CodecDevice * device,guint rank)929 gst_v4l2_codec_vp8_dec_register (GstPlugin * plugin, GstV4l2Decoder * decoder,
930     GstV4l2CodecDevice * device, guint rank)
931 {
932   gchar *element_name;
933   GstCaps *src_caps, *alpha_caps;
934 
935   GST_DEBUG_CATEGORY_INIT (v4l2_vp8dec_debug, "v4l2codecs-vp8dec", 0,
936       "V4L2 stateless VP8 decoder");
937 
938   if (!gst_v4l2_decoder_set_sink_fmt (decoder, V4L2_PIX_FMT_VP8_FRAME,
939           320, 240, 8))
940     return;
941   src_caps = gst_v4l2_decoder_enum_src_formats (decoder);
942 
943   if (gst_caps_is_empty (src_caps)) {
944     GST_WARNING ("Not registering VP8 decoder since it produces no "
945         "supported format");
946     goto done;
947   }
948 
949   gst_v4l2_decoder_register (plugin, GST_TYPE_V4L2_CODEC_VP8_DEC,
950       (GClassInitFunc) gst_v4l2_codec_vp8_dec_subclass_init,
951       gst_mini_object_ref (GST_MINI_OBJECT (device)),
952       (GInstanceInitFunc) gst_v4l2_codec_vp8_dec_subinit,
953       "v4l2sl%svp8dec", device, rank, &element_name);
954 
955   if (!element_name)
956     goto done;
957 
958   alpha_caps = gst_caps_from_string ("video/x-raw,format={I420, NV12}");
959 
960   if (gst_caps_can_intersect (src_caps, alpha_caps))
961     gst_v4l2_codec_alpha_decode_bin_register (plugin,
962         (GClassInitFunc) gst_v4l2_codec_vp8_alpha_decode_bin_subclass_init,
963         element_name, "v4l2slvp8%salphadecodebin", device, rank);
964 
965   gst_caps_unref (alpha_caps);
966 
967 done:
968   gst_caps_unref (src_caps);
969 }
970