1
2 /* GStreamer
3 * Copyright (C) 2021 Daniel Almeida <daniel.almeida@collabora.com>
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
25 #include "gstv4l2codecallocator.h"
26 #include "gstv4l2codecalphadecodebin.h"
27 #include "gstv4l2codecpool.h"
28 #include "gstv4l2codecvp9dec.h"
29 #include "gstv4l2format.h"
30 #include "linux/v4l2-controls.h"
31 #include "linux/videodev2.h"
32
33 GST_DEBUG_CATEGORY_STATIC (v4l2_vp9dec_debug);
34 #define GST_CAT_DEFAULT v4l2_vp9dec_debug
35
36 /* Used to mark picture that have been outputed */
37 #define FLAG_PICTURE_OUTPUTED GST_MINI_OBJECT_FLAG_LAST
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-vp9, " "alignment=(string) frame")
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-vp9, codec-alpha = (boolean) true, "
55 "alignment = frame")
56 );
57
58 static GstStaticPadTemplate src_template =
59 GST_STATIC_PAD_TEMPLATE (GST_VIDEO_DECODER_SRC_NAME,
60 GST_PAD_SRC, GST_PAD_ALWAYS,
61 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_V4L2_DEFAULT_VIDEO_FORMATS)));
62
63 struct _GstV4l2CodecVp9Dec
64 {
65 GstVp9Decoder parent;
66 GstV4l2Decoder *decoder;
67 GstVideoCodecState *output_state;
68 GstVideoInfo vinfo;
69 gint width;
70 gint height;
71
72 GstV4l2CodecAllocator *sink_allocator;
73 GstV4l2CodecAllocator *src_allocator;
74 GstV4l2CodecPool *src_pool;
75 gboolean has_videometa;
76 gboolean need_negotiation;
77 gboolean copy_frames;
78
79 struct v4l2_ctrl_vp9_frame v4l2_vp9_frame;
80 struct v4l2_ctrl_vp9_compressed_hdr v4l2_delta_probs;
81
82 GstMemory *bitstream;
83 GstMapInfo bitstream_map;
84
85 /* will renegotiate if parser reports new values */
86 guint bit_depth;
87 guint color_range;
88 guint profile;
89 guint color_space;
90 guint subsampling_x;
91 guint subsampling_y;
92 };
93
94 G_DEFINE_ABSTRACT_TYPE (GstV4l2CodecVp9Dec, gst_v4l2_codec_vp9_dec,
95 GST_TYPE_VP9_DECODER);
96
97 #define parent_class gst_v4l2_codec_vp9_dec_parent_class
98
99 static guint
gst_v4l2_codec_vp9_dec_get_preferred_output_delay(GstVp9Decoder * decoder,gboolean is_live)100 gst_v4l2_codec_vp9_dec_get_preferred_output_delay (GstVp9Decoder * decoder,
101 gboolean is_live)
102 {
103
104 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
105 guint delay;
106
107 if (is_live)
108 delay = 0;
109 else
110 delay = 1;
111
112 gst_v4l2_decoder_set_render_delay (self->decoder, delay);
113 return delay;
114 }
115
116 static void
gst_v4l2_codec_vp9_dec_fill_lf_params(GstV4l2CodecVp9Dec * self,const GstVp9LoopFilterParams * lf)117 gst_v4l2_codec_vp9_dec_fill_lf_params (GstV4l2CodecVp9Dec * self,
118 const GstVp9LoopFilterParams * lf)
119 {
120 gint i;
121
122 G_STATIC_ASSERT (sizeof (self->v4l2_vp9_frame.lf.ref_deltas) ==
123 sizeof (lf->loop_filter_ref_deltas));
124 G_STATIC_ASSERT (sizeof (self->v4l2_vp9_frame.lf.mode_deltas) ==
125 sizeof (lf->loop_filter_mode_deltas));
126
127 for (i = 0; i < G_N_ELEMENTS (self->v4l2_vp9_frame.lf.ref_deltas); i++)
128 self->v4l2_vp9_frame.lf.ref_deltas[i] = lf->loop_filter_ref_deltas[i];
129
130 for (i = 0; i < G_N_ELEMENTS (self->v4l2_vp9_frame.lf.mode_deltas); i++)
131 self->v4l2_vp9_frame.lf.mode_deltas[i] = lf->loop_filter_mode_deltas[i];
132 }
133
134 static void
gst_v4l2_codec_vp9_dec_fill_seg_params(GstV4l2CodecVp9Dec * self,const GstVp9SegmentationParams * s)135 gst_v4l2_codec_vp9_dec_fill_seg_params (GstV4l2CodecVp9Dec * self,
136 const GstVp9SegmentationParams * s)
137 {
138 guint i;
139 struct v4l2_vp9_segmentation *v4l2_segmentation = &self->v4l2_vp9_frame.seg;
140
141 G_STATIC_ASSERT (sizeof (v4l2_segmentation->tree_probs) ==
142 sizeof (s->segmentation_tree_probs));
143 G_STATIC_ASSERT (sizeof (v4l2_segmentation->pred_probs) ==
144 sizeof (s->segmentation_pred_prob));
145 G_STATIC_ASSERT (sizeof (v4l2_segmentation->feature_data) ==
146 sizeof (s->feature_data));
147
148 for (i = 0; i < G_N_ELEMENTS (v4l2_segmentation->tree_probs); i++) {
149 v4l2_segmentation->tree_probs[i] = s->segmentation_tree_probs[i];
150 }
151 for (i = 0; i < G_N_ELEMENTS (v4l2_segmentation->pred_probs); i++) {
152 v4l2_segmentation->pred_probs[i] = s->segmentation_pred_prob[i];
153 }
154 for (i = 0; i < G_N_ELEMENTS (v4l2_segmentation->feature_enabled); i++) {
155 /* see 3. Symbols (and abbreviated terms) for reference */
156
157 /* *INDENT-OFF* */
158 v4l2_segmentation->feature_enabled[i] =
159 (s->feature_enabled[i][GST_VP9_SEG_LVL_ALT_Q] ? V4L2_VP9_SEGMENT_FEATURE_ENABLED(V4L2_VP9_SEG_LVL_ALT_Q) : 0) |
160 (s->feature_enabled[i][GST_VP9_SEG_LVL_ALT_L] ? V4L2_VP9_SEGMENT_FEATURE_ENABLED(V4L2_VP9_SEG_LVL_ALT_L) : 0) |
161 (s->feature_enabled[i][GST_VP9_SEG_LVL_REF_FRAME] ? V4L2_VP9_SEGMENT_FEATURE_ENABLED(V4L2_VP9_SEG_LVL_REF_FRAME) : 0) |
162 (s->feature_enabled[i][GST_VP9_SEG_SEG_LVL_SKIP] ? V4L2_VP9_SEGMENT_FEATURE_ENABLED(V4L2_VP9_SEG_LVL_SKIP) : 0);
163 /* *INDENT-ON* */
164 }
165
166 memcpy (v4l2_segmentation->feature_data, s->feature_data,
167 sizeof (v4l2_segmentation->feature_data));
168 }
169
170 static void
gst_v4l2_codec_vp9_dec_fill_prob_updates(GstV4l2CodecVp9Dec * self,const GstVp9FrameHeader * h)171 gst_v4l2_codec_vp9_dec_fill_prob_updates (GstV4l2CodecVp9Dec * self,
172 const GstVp9FrameHeader * h)
173 {
174 struct v4l2_ctrl_vp9_compressed_hdr *probs = &self->v4l2_delta_probs;
175
176 G_STATIC_ASSERT (sizeof (probs->tx8) ==
177 sizeof (h->delta_probabilities.tx_probs_8x8));
178 G_STATIC_ASSERT (sizeof (probs->tx16) ==
179 sizeof (h->delta_probabilities.tx_probs_16x16));
180 G_STATIC_ASSERT (sizeof (probs->tx32) ==
181 sizeof (h->delta_probabilities.tx_probs_32x32));
182 G_STATIC_ASSERT (sizeof (probs->coef) ==
183 sizeof (h->delta_probabilities.coef));
184 G_STATIC_ASSERT (sizeof (probs->skip) ==
185 sizeof (h->delta_probabilities.skip));
186 G_STATIC_ASSERT (sizeof (probs->inter_mode) ==
187 sizeof (h->delta_probabilities.inter_mode));
188 G_STATIC_ASSERT (sizeof (probs->interp_filter) ==
189 sizeof (h->delta_probabilities.interp_filter));
190 G_STATIC_ASSERT (sizeof (probs->is_inter) ==
191 sizeof (h->delta_probabilities.is_inter));
192 G_STATIC_ASSERT (sizeof (probs->comp_mode) ==
193 sizeof (h->delta_probabilities.comp_mode));
194 G_STATIC_ASSERT (sizeof (probs->single_ref) ==
195 sizeof (h->delta_probabilities.single_ref));
196 G_STATIC_ASSERT (sizeof (probs->comp_ref) ==
197 sizeof (h->delta_probabilities.comp_ref));
198 G_STATIC_ASSERT (sizeof (probs->y_mode) ==
199 sizeof (h->delta_probabilities.y_mode));
200 G_STATIC_ASSERT (sizeof (probs->partition) ==
201 sizeof (h->delta_probabilities.partition));
202 G_STATIC_ASSERT (sizeof (probs->mv.joint) ==
203 sizeof (h->delta_probabilities.mv.joint));
204 G_STATIC_ASSERT (sizeof (probs->mv.sign) ==
205 sizeof (h->delta_probabilities.mv.sign));
206 G_STATIC_ASSERT (sizeof (probs->mv.classes) ==
207 sizeof (h->delta_probabilities.mv.klass));
208 G_STATIC_ASSERT (sizeof (probs->mv.class0_bit) ==
209 sizeof (h->delta_probabilities.mv.class0_bit));
210 G_STATIC_ASSERT (sizeof (probs->mv.bits) ==
211 sizeof (h->delta_probabilities.mv.bits));
212 G_STATIC_ASSERT (sizeof (probs->mv.class0_fr) ==
213 sizeof (h->delta_probabilities.mv.class0_fr));
214 G_STATIC_ASSERT (sizeof (probs->mv.fr) ==
215 sizeof (h->delta_probabilities.mv.fr));
216 G_STATIC_ASSERT (sizeof (probs->mv.class0_hp) ==
217 sizeof (h->delta_probabilities.mv.class0_hp));
218 G_STATIC_ASSERT (sizeof (probs->mv.hp) ==
219 sizeof (h->delta_probabilities.mv.hp));
220
221 memset (probs, 0, sizeof (*probs));
222
223 probs->tx_mode = h->tx_mode;
224 memcpy (probs->tx8, h->delta_probabilities.tx_probs_8x8, sizeof (probs->tx8));
225 memcpy (probs->tx16, h->delta_probabilities.tx_probs_16x16,
226 sizeof (probs->tx16));
227 memcpy (probs->tx32, h->delta_probabilities.tx_probs_32x32,
228 sizeof (probs->tx32));
229 memcpy (probs->coef, h->delta_probabilities.coef, sizeof (probs->coef));
230 memcpy (probs->skip, h->delta_probabilities.skip, sizeof (probs->skip));
231 memcpy (probs->inter_mode, h->delta_probabilities.inter_mode,
232 sizeof (probs->inter_mode));
233 memcpy (probs->interp_filter,
234 h->delta_probabilities.interp_filter, sizeof (probs->interp_filter));
235 memcpy (probs->is_inter, h->delta_probabilities.is_inter,
236 sizeof (probs->is_inter));
237 memcpy (probs->comp_mode, h->delta_probabilities.comp_mode,
238 sizeof (probs->comp_mode));
239 memcpy (probs->single_ref, h->delta_probabilities.single_ref,
240 sizeof (probs->single_ref));
241 memcpy (probs->comp_ref, h->delta_probabilities.comp_ref,
242 sizeof (probs->comp_ref));
243 memcpy (probs->y_mode, h->delta_probabilities.y_mode, sizeof (probs->y_mode));
244 memcpy (probs->partition, h->delta_probabilities.partition,
245 sizeof (probs->partition));
246
247 memcpy (probs->mv.joint, h->delta_probabilities.mv.joint,
248 sizeof (h->delta_probabilities.mv.joint));
249 memcpy (probs->mv.sign, h->delta_probabilities.mv.sign,
250 sizeof (h->delta_probabilities.mv.sign));
251 memcpy (probs->mv.classes, h->delta_probabilities.mv.klass,
252 sizeof (h->delta_probabilities.mv.klass));
253 memcpy (probs->mv.class0_bit,
254 h->delta_probabilities.mv.class0_bit,
255 sizeof (h->delta_probabilities.mv.class0_bit));
256 memcpy (probs->mv.bits, h->delta_probabilities.mv.bits,
257 sizeof (h->delta_probabilities.mv.bits));
258 memcpy (probs->mv.class0_fr, h->delta_probabilities.mv.class0_fr,
259 sizeof (h->delta_probabilities.mv.class0_fr));
260 memcpy (probs->mv.fr, h->delta_probabilities.mv.fr,
261 sizeof (h->delta_probabilities.mv.fr));
262 memcpy (probs->mv.class0_hp, h->delta_probabilities.mv.class0_hp,
263 sizeof (h->delta_probabilities.mv.class0_hp));
264 memcpy (probs->mv.hp, h->delta_probabilities.mv.hp,
265 sizeof (h->delta_probabilities.mv.hp));
266 }
267
268 static void
gst_v4l2_codecs_vp9_dec_fill_refs(GstV4l2CodecVp9Dec * self,const GstVp9FrameHeader * h,const GstVp9Dpb * reference_frames)269 gst_v4l2_codecs_vp9_dec_fill_refs (GstV4l2CodecVp9Dec * self,
270 const GstVp9FrameHeader * h, const GstVp9Dpb * reference_frames)
271 {
272 GstVp9Picture *ref_pic;
273
274 if (reference_frames && reference_frames->pic_list[h->ref_frame_idx[0]]) {
275 ref_pic = reference_frames->pic_list[h->ref_frame_idx[0]];
276 self->v4l2_vp9_frame.last_frame_ts = ref_pic->system_frame_number * 1000;
277 }
278
279 if (reference_frames && reference_frames->pic_list[h->ref_frame_idx[1]]) {
280 ref_pic = reference_frames->pic_list[h->ref_frame_idx[1]];
281 self->v4l2_vp9_frame.golden_frame_ts = ref_pic->system_frame_number * 1000;
282 }
283
284 if (reference_frames && reference_frames->pic_list[h->ref_frame_idx[2]]) {
285 ref_pic = reference_frames->pic_list[h->ref_frame_idx[2]];
286 self->v4l2_vp9_frame.alt_frame_ts = ref_pic->system_frame_number * 1000;
287 }
288 }
289
290 static void
gst_v4l2_codec_vp9_dec_fill_dec_params(GstV4l2CodecVp9Dec * self,const GstVp9FrameHeader * h,const GstVp9Dpb * reference_frames)291 gst_v4l2_codec_vp9_dec_fill_dec_params (GstV4l2CodecVp9Dec * self,
292 const GstVp9FrameHeader * h, const GstVp9Dpb * reference_frames)
293 {
294 /* *INDENT-OFF* */
295 self->v4l2_vp9_frame = (struct v4l2_ctrl_vp9_frame) {
296 .flags =
297 (h->frame_type == GST_VP9_KEY_FRAME ? V4L2_VP9_FRAME_FLAG_KEY_FRAME : 0) |
298 (h->show_frame ? V4L2_VP9_FRAME_FLAG_SHOW_FRAME : 0) |
299 (h->error_resilient_mode ? V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT : 0) |
300 (h->intra_only ? V4L2_VP9_FRAME_FLAG_INTRA_ONLY : 0) |
301 (h->allow_high_precision_mv ? V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV : 0) |
302 (h->refresh_frame_context ? V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX : 0) |
303 (h->frame_parallel_decoding_mode ? V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE : 0) |
304 (self->subsampling_x ? V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING : 0) |
305 (self->subsampling_y ? V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING : 0) |
306 (self->color_range ? V4L2_VP9_FRAME_FLAG_COLOR_RANGE_FULL_SWING : 0),
307
308 .compressed_header_size = h->header_size_in_bytes,
309 .uncompressed_header_size = h->frame_header_length_in_bytes,
310 .profile = h->profile,
311 .frame_context_idx = h->frame_context_idx,
312 .bit_depth = self->bit_depth,
313 .interpolation_filter = h->interpolation_filter,
314 .tile_cols_log2 = h->tile_cols_log2,
315 .tile_rows_log2 = h->tile_rows_log2,
316 .reference_mode = h->reference_mode,
317 .frame_width_minus_1 = h->width - 1,
318 .frame_height_minus_1 = h->height - 1,
319 .render_width_minus_1 = h->render_width ? h->render_width - 1 : h->width - 1,
320 .render_height_minus_1 = h->render_height ? h->render_height - 1: h->height - 1,
321 .ref_frame_sign_bias =
322 (h->ref_frame_sign_bias[GST_VP9_REF_FRAME_LAST] ? V4L2_VP9_SIGN_BIAS_LAST : 0) |
323 (h->ref_frame_sign_bias[GST_VP9_REF_FRAME_GOLDEN] ? V4L2_VP9_SIGN_BIAS_GOLDEN : 0) |
324 (h->ref_frame_sign_bias[GST_VP9_REF_FRAME_ALTREF] ? V4L2_VP9_SIGN_BIAS_ALT : 0),
325
326 .lf = (struct v4l2_vp9_loop_filter) {
327 .flags =
328 (h->loop_filter_params.loop_filter_delta_enabled ? V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED : 0) |
329 (h->loop_filter_params.loop_filter_delta_update ? V4L2_VP9_LOOP_FILTER_FLAG_DELTA_UPDATE : 0),
330 .level = h->loop_filter_params.loop_filter_level,
331 .sharpness = h->loop_filter_params.loop_filter_sharpness
332 },
333
334 .quant = (struct v4l2_vp9_quantization) {
335 .base_q_idx = h->quantization_params.base_q_idx, /* used for Y (luma) AC coefficients */
336 .delta_q_y_dc = h->quantization_params.delta_q_y_dc,
337 .delta_q_uv_dc = h->quantization_params.delta_q_uv_dc,
338 .delta_q_uv_ac = h->quantization_params.delta_q_uv_ac,
339 },
340
341 .seg = (struct v4l2_vp9_segmentation) {
342 .flags =
343 (h->segmentation_params.segmentation_enabled ? V4L2_VP9_SEGMENTATION_FLAG_ENABLED : 0) |
344 (h->segmentation_params.segmentation_update_map ? V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP : 0) |
345 (h->segmentation_params.segmentation_temporal_update ? V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE : 0) |
346 (h->segmentation_params.segmentation_update_data ? V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA : 0) |
347 (h->segmentation_params.segmentation_abs_or_delta_update ? V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE : 0),
348 }
349 };
350 /* *INDENT-ON* */
351
352 switch (h->reset_frame_context) {
353 case 0:
354 case 1:
355 self->v4l2_vp9_frame.reset_frame_context = V4L2_VP9_RESET_FRAME_CTX_NONE;
356 break;
357 case 2:
358 self->v4l2_vp9_frame.reset_frame_context = V4L2_VP9_RESET_FRAME_CTX_SPEC;
359 break;
360 case 3:
361 self->v4l2_vp9_frame.reset_frame_context = V4L2_VP9_RESET_FRAME_CTX_ALL;
362 break;
363 default:
364 break;
365 }
366
367 gst_v4l2_codecs_vp9_dec_fill_refs (self, h, reference_frames);
368 gst_v4l2_codec_vp9_dec_fill_lf_params (self, &h->loop_filter_params);
369 gst_v4l2_codec_vp9_dec_fill_seg_params (self, &h->segmentation_params);
370 }
371
372 static gboolean
gst_v4l2_codec_vp9_dec_open(GstVideoDecoder * decoder)373 gst_v4l2_codec_vp9_dec_open (GstVideoDecoder * decoder)
374 {
375 GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
376 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
377
378 if (!gst_v4l2_decoder_open (self->decoder)) {
379 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
380 ("Failed to open VP9 decoder"),
381 ("gst_v4l2_decoder_open() failed: %s", g_strerror (errno)));
382 return FALSE;
383 }
384
385 /* V4L2 does not support non-keyframe resolution change, this will ask the
386 * base class to drop frame until the next keyframe as a workaround. */
387 gst_vp9_decoder_set_non_keyframe_format_change_support (vp9dec, FALSE);
388
389 return TRUE;
390 }
391
392 static gboolean
gst_v4l2_codec_vp9_dec_close(GstVideoDecoder * decoder)393 gst_v4l2_codec_vp9_dec_close (GstVideoDecoder * decoder)
394 {
395 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
396 gst_v4l2_decoder_close (self->decoder);
397 return TRUE;
398 }
399
400 static void
gst_v4l2_codec_vp9_dec_reset_allocation(GstV4l2CodecVp9Dec * self)401 gst_v4l2_codec_vp9_dec_reset_allocation (GstV4l2CodecVp9Dec * self)
402 {
403 if (self->sink_allocator) {
404 gst_v4l2_codec_allocator_detach (self->sink_allocator);
405 g_clear_object (&self->sink_allocator);
406 }
407
408 if (self->src_allocator) {
409 gst_v4l2_codec_allocator_detach (self->src_allocator);
410 g_clear_object (&self->src_allocator);
411 g_clear_object (&self->src_pool);
412 }
413 }
414
415 static gboolean
gst_v4l2_codec_vp9_dec_stop(GstVideoDecoder * decoder)416 gst_v4l2_codec_vp9_dec_stop (GstVideoDecoder * decoder)
417 {
418 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
419
420 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
421 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
422
423 gst_v4l2_codec_vp9_dec_reset_allocation (self);
424
425 if (self->output_state)
426 gst_video_codec_state_unref (self->output_state);
427 self->output_state = NULL;
428
429 return GST_VIDEO_DECODER_CLASS (parent_class)->stop (decoder);
430 }
431
432 static gboolean
gst_v4l2_codec_vp9_dec_negotiate(GstVideoDecoder * decoder)433 gst_v4l2_codec_vp9_dec_negotiate (GstVideoDecoder * decoder)
434 {
435 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
436 GstVp9Decoder *vp9dec = GST_VP9_DECODER (decoder);
437
438 /* *INDENT-OFF* */
439 struct v4l2_ext_control control[] = {
440 {
441 .id = V4L2_CID_STATELESS_VP9_FRAME,
442 .ptr = &self->v4l2_vp9_frame,
443 .size = sizeof (self->v4l2_vp9_frame),
444 },
445 };
446 /* *INDENT-ON* */
447
448 GstCaps *filter, *caps;
449 /* Ignore downstream renegotiation request. */
450 if (!self->need_negotiation)
451 return TRUE;
452 self->need_negotiation = FALSE;
453
454 GST_DEBUG_OBJECT (self, "Negotiate");
455
456 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SINK);
457 gst_v4l2_decoder_streamoff (self->decoder, GST_PAD_SRC);
458
459 gst_v4l2_codec_vp9_dec_reset_allocation (self);
460
461 if (!gst_v4l2_decoder_set_sink_fmt (self->decoder, V4L2_PIX_FMT_VP9_FRAME,
462 self->width, self->height, self->bit_depth)) {
463 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
464 ("Failed to configure VP9 decoder"),
465 ("gst_v4l2_decoder_set_sink_fmt() failed: %s", g_strerror (errno)));
466 gst_v4l2_decoder_close (self->decoder);
467 return FALSE;
468 }
469 if (!gst_v4l2_decoder_set_controls (self->decoder, NULL, control,
470 G_N_ELEMENTS (control))) {
471 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
472 ("Driver does not support the selected stream."), (NULL));
473 return FALSE;
474 }
475
476 filter = gst_v4l2_decoder_enum_src_formats (self->decoder);
477 if (!filter) {
478 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
479 ("No supported decoder output formats"), (NULL));
480 return FALSE;
481 }
482 GST_DEBUG_OBJECT (self, "Supported output formats: %" GST_PTR_FORMAT, filter);
483
484 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
485 gst_caps_unref (filter);
486 GST_DEBUG_OBJECT (self, "Peer supported formats: %" GST_PTR_FORMAT, caps);
487
488 if (!gst_v4l2_decoder_select_src_format (self->decoder, caps, &self->vinfo)) {
489 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
490 ("Unsupported pixel format"),
491 ("No support for %ux%u format %s", self->width, self->height,
492 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&self->vinfo))));
493 gst_caps_unref (caps);
494 return FALSE;
495 }
496 gst_caps_unref (caps);
497
498 if (self->output_state)
499 gst_video_codec_state_unref (self->output_state);
500
501 self->output_state =
502 gst_video_decoder_set_output_state (GST_VIDEO_DECODER (self),
503 self->vinfo.finfo->format, self->width,
504 self->height, vp9dec->input_state);
505
506 self->output_state->caps = gst_video_info_to_caps (&self->output_state->info);
507
508 if (GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder)) {
509 if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SINK)) {
510 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
511 ("Could not enable the decoder driver."),
512 ("VIDIOC_STREAMON(SINK) failed: %s", g_strerror (errno)));
513 return FALSE;
514 }
515
516 if (!gst_v4l2_decoder_streamon (self->decoder, GST_PAD_SRC)) {
517 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
518 ("Could not enable the decoder driver."),
519 ("VIDIOC_STREAMON(SRC) failed: %s", g_strerror (errno)));
520 return FALSE;
521 }
522
523 return TRUE;
524 }
525
526 return FALSE;
527 }
528
529 static gboolean
gst_v4l2_codec_vp9_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)530 gst_v4l2_codec_vp9_dec_decide_allocation (GstVideoDecoder * decoder,
531 GstQuery * query)
532 {
533 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
534 guint min = 0;
535 guint num_bitstream;
536
537 self->has_videometa = gst_query_find_allocation_meta (query,
538 GST_VIDEO_META_API_TYPE, NULL);
539
540 g_clear_object (&self->src_pool);
541 g_clear_object (&self->src_allocator);
542
543 if (gst_query_get_n_allocation_pools (query) > 0)
544 gst_query_parse_nth_allocation_pool (query, 0, NULL, NULL, &min, NULL);
545
546 min = MAX (2, min);
547
548 num_bitstream = 1 +
549 MAX (1, gst_v4l2_decoder_get_render_delay (self->decoder));
550
551 self->sink_allocator = gst_v4l2_codec_allocator_new (self->decoder,
552 GST_PAD_SINK, num_bitstream);
553 if (!self->sink_allocator) {
554 GST_ELEMENT_ERROR (self, RESOURCE, NO_SPACE_LEFT,
555 ("Not enough memory to allocate sink buffers."), (NULL));
556 return FALSE;
557 }
558
559 self->src_allocator = gst_v4l2_codec_allocator_new (self->decoder,
560 GST_PAD_SRC, GST_VP9_REF_FRAMES + min + 4);
561 if (!self->src_allocator) {
562 GST_ELEMENT_ERROR (self, RESOURCE, NO_SPACE_LEFT,
563 ("Not enough memory to allocate source buffers."), (NULL));
564 g_clear_object (&self->sink_allocator);
565 return FALSE;
566 }
567
568 self->src_pool = gst_v4l2_codec_pool_new (self->src_allocator, &self->vinfo);
569
570 /* Our buffer pool is internal, we will let the base class create a video
571 * pool, and use it if we are running out of buffers or if downstream does
572 * not support GstVideoMeta */
573 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
574 (decoder, query);
575 }
576
577 static GstFlowReturn
gst_v4l2_codec_vp9_dec_new_sequence(GstVp9Decoder * decoder,const GstVp9FrameHeader * frame_hdr)578 gst_v4l2_codec_vp9_dec_new_sequence (GstVp9Decoder * decoder,
579 const GstVp9FrameHeader * frame_hdr)
580 {
581 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
582 gboolean negotiation_needed = FALSE;
583
584 if (self->vinfo.finfo->format == GST_VIDEO_FORMAT_UNKNOWN)
585 negotiation_needed = TRUE;
586
587 /* TODO Check if current buffers are large enough, and reuse them */
588 if (self->width != frame_hdr->width || self->height != frame_hdr->height) {
589 self->width = frame_hdr->width;
590 self->height = frame_hdr->height;
591 negotiation_needed = TRUE;
592 GST_INFO_OBJECT (self, "Resolution changed to %dx%d",
593 self->width, self->height);
594 }
595
596 if (self->subsampling_x != frame_hdr->subsampling_x ||
597 self->subsampling_y != frame_hdr->subsampling_y) {
598 GST_DEBUG_OBJECT (self,
599 "subsampling changed from x: %d, y: %d to x: %d, y: %d",
600 self->subsampling_x, self->subsampling_y,
601 frame_hdr->subsampling_x, frame_hdr->subsampling_y);
602 self->subsampling_x = frame_hdr->subsampling_x;
603 self->subsampling_y = frame_hdr->subsampling_y;
604 negotiation_needed = TRUE;
605 }
606
607 if (frame_hdr->color_space != GST_VP9_CS_UNKNOWN &&
608 frame_hdr->color_space != GST_VP9_CS_RESERVED_2 &&
609 frame_hdr->color_space != self->color_space) {
610 GST_DEBUG_OBJECT (self, "colorspace changed from %d to %d",
611 self->color_space, frame_hdr->color_space);
612 self->color_space = frame_hdr->color_space;
613 negotiation_needed = TRUE;
614 }
615
616 if (frame_hdr->color_range != self->color_range) {
617 GST_DEBUG_OBJECT (self, "color range changed from %d to %d",
618 self->color_range, frame_hdr->color_range);
619 self->color_range = frame_hdr->color_range;
620 negotiation_needed = TRUE;
621 }
622
623 if (frame_hdr->profile != GST_VP9_PROFILE_UNDEFINED &&
624 frame_hdr->profile != self->profile) {
625 GST_DEBUG_OBJECT (self, "profile changed from %d to %d", self->profile,
626 frame_hdr->profile);
627 self->profile = frame_hdr->profile;
628 negotiation_needed = TRUE;
629 }
630
631 if (frame_hdr->bit_depth != self->bit_depth) {
632 GST_DEBUG_OBJECT (self, "bit-depth changed from %d to %d",
633 self->bit_depth, frame_hdr->bit_depth);
634 self->bit_depth = frame_hdr->bit_depth;
635 negotiation_needed = TRUE;
636 }
637
638 gst_v4l2_codec_vp9_dec_fill_dec_params (self, frame_hdr, NULL);
639 gst_v4l2_codec_vp9_dec_fill_prob_updates (self, frame_hdr);
640
641 if (negotiation_needed) {
642 self->need_negotiation = TRUE;
643 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
644 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
645 return GST_FLOW_ERROR;
646 }
647 }
648
649 /* Check if we can zero-copy buffers */
650 if (!self->has_videometa) {
651 GstVideoInfo ref_vinfo;
652 gint i;
653
654 gst_video_info_set_format (&ref_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
655 self->width, self->height);
656
657 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&self->vinfo); i++) {
658 if (self->vinfo.stride[i] != ref_vinfo.stride[i] ||
659 self->vinfo.offset[i] != ref_vinfo.offset[i]) {
660 GST_WARNING_OBJECT (self,
661 "GstVideoMeta support required, copying frames.");
662 self->copy_frames = TRUE;
663 break;
664 }
665 }
666 } else {
667 self->copy_frames = FALSE;
668 }
669
670 return GST_FLOW_OK;
671 }
672
673 static GstFlowReturn
gst_v4l2_codec_vp9_dec_start_picture(GstVp9Decoder * decoder,GstVp9Picture * picture)674 gst_v4l2_codec_vp9_dec_start_picture (GstVp9Decoder * decoder,
675 GstVp9Picture * picture)
676 {
677 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
678
679 /* FIXME base class should not call us if negotiation failed */
680 if (!self->sink_allocator)
681 return GST_FLOW_ERROR;
682
683 /* Ensure we have a bitstream to write into */
684 if (!self->bitstream) {
685 self->bitstream = gst_v4l2_codec_allocator_alloc (self->sink_allocator);
686
687 if (!self->bitstream) {
688 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
689 ("Not enough memory to decode VP9 stream."), (NULL));
690 return GST_FLOW_ERROR;
691 }
692
693 if (!gst_memory_map (self->bitstream, &self->bitstream_map, GST_MAP_WRITE)) {
694 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
695 ("Could not access bitstream memory for writing"), (NULL));
696 g_clear_pointer (&self->bitstream, gst_memory_unref);
697 return GST_FLOW_ERROR;
698 }
699 }
700
701 /* We use this field to track how much we have written */
702 self->bitstream_map.size = 0;
703
704 return GST_FLOW_OK;
705 }
706
707 static void
gst_v4l2_codec_vp9_dec_reset_picture(GstV4l2CodecVp9Dec * self)708 gst_v4l2_codec_vp9_dec_reset_picture (GstV4l2CodecVp9Dec * self)
709 {
710 if (self->bitstream) {
711 if (self->bitstream_map.memory)
712 gst_memory_unmap (self->bitstream, &self->bitstream_map);
713 g_clear_pointer (&self->bitstream, gst_memory_unref);
714 self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
715 }
716 }
717
718 static GstFlowReturn
gst_v4l2_codec_vp9_dec_decode_picture(GstVp9Decoder * decoder,GstVp9Picture * picture,GstVp9Dpb * dpb)719 gst_v4l2_codec_vp9_dec_decode_picture (GstVp9Decoder * decoder,
720 GstVp9Picture * picture, GstVp9Dpb * dpb)
721 {
722 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
723 guint8 *bitstream_data = self->bitstream_map.data;
724
725 if (self->bitstream_map.maxsize < picture->size) {
726 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
727 ("Not enough space to send picture bitstream."), (NULL));
728 gst_v4l2_codec_vp9_dec_reset_picture (self);
729 return GST_FLOW_ERROR;
730 }
731
732 gst_v4l2_codec_vp9_dec_fill_dec_params (self, &picture->frame_hdr, dpb);
733
734 if (decoder->parse_compressed_headers)
735 gst_v4l2_codec_vp9_dec_fill_prob_updates (self, &picture->frame_hdr);
736
737 memcpy (bitstream_data, picture->data, picture->size);
738 self->bitstream_map.size = picture->size;
739
740 return GST_FLOW_OK;
741 }
742
743 static GstFlowReturn
gst_v4l2_codec_vp9_dec_end_picture(GstVp9Decoder * decoder,GstVp9Picture * picture)744 gst_v4l2_codec_vp9_dec_end_picture (GstVp9Decoder * decoder,
745 GstVp9Picture * picture)
746 {
747 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
748 GstVideoCodecFrame *frame;
749 GstV4l2Request *request = NULL;
750 GstFlowReturn flow_ret;
751 gsize bytesused;
752
753 /* *INDENT-OFF* */
754 struct v4l2_ext_control decode_params_control[] = {
755 {
756 .id = V4L2_CID_STATELESS_VP9_FRAME,
757 .ptr = &self->v4l2_vp9_frame,
758 .size = sizeof(self->v4l2_vp9_frame),
759 },
760 {
761 .id = V4L2_CID_STATELESS_VP9_COMPRESSED_HDR,
762 .ptr = &self->v4l2_delta_probs,
763 .size = sizeof (self->v4l2_delta_probs),
764 },
765 };
766 /* *INDENT-ON* */
767
768 bytesused = self->bitstream_map.size;
769 gst_memory_unmap (self->bitstream, &self->bitstream_map);
770 self->bitstream_map = (GstMapInfo) GST_MAP_INFO_INIT;
771 gst_memory_resize (self->bitstream, 0, bytesused);
772
773 frame = gst_video_decoder_get_frame (GST_VIDEO_DECODER (self),
774 picture->system_frame_number);
775 g_return_val_if_fail (frame, FALSE);
776
777 flow_ret = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (self->src_pool),
778 &frame->output_buffer, NULL);
779 if (flow_ret != GST_FLOW_OK) {
780 if (flow_ret == GST_FLOW_FLUSHING)
781 GST_DEBUG_OBJECT (self, "Frame decoding aborted, we are flushing.");
782 else
783 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
784 ("No more picture buffer available."), (NULL));
785 goto fail;
786 }
787
788 request = gst_v4l2_decoder_alloc_request (self->decoder,
789 picture->system_frame_number, self->bitstream, frame->output_buffer);
790
791 gst_video_codec_frame_unref (frame);
792
793 if (!request) {
794 GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT,
795 ("Failed to allocate a media request object."), (NULL));
796 goto fail;
797 }
798
799 if (!gst_v4l2_decoder_set_controls (self->decoder, request,
800 decode_params_control, G_N_ELEMENTS (decode_params_control))) {
801 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
802 ("Driver did not accept the bitstream parameters."), (NULL));
803 goto fail;
804 }
805
806 if (!gst_v4l2_request_queue (request, 0)) {
807 GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE,
808 ("Driver did not accept the decode request."), (NULL));
809 goto fail;
810 }
811
812 gst_vp9_picture_set_user_data (picture, request,
813 (GDestroyNotify) gst_v4l2_request_unref);
814 gst_v4l2_codec_vp9_dec_reset_picture (self);
815 return GST_FLOW_OK;
816
817 fail:
818 if (request)
819 gst_v4l2_request_unref (request);
820
821 gst_v4l2_codec_vp9_dec_reset_picture (self);
822 return GST_FLOW_ERROR;
823 }
824
825 static gboolean
gst_v4l2_codec_vp9_dec_copy_output_buffer(GstV4l2CodecVp9Dec * self,GstVideoCodecFrame * codec_frame)826 gst_v4l2_codec_vp9_dec_copy_output_buffer (GstV4l2CodecVp9Dec * self,
827 GstVideoCodecFrame * codec_frame)
828 {
829 GstVideoFrame src_frame;
830 GstVideoFrame dest_frame;
831 GstVideoInfo dest_vinfo;
832 GstBuffer *buffer;
833
834 gst_video_info_set_format (&dest_vinfo, GST_VIDEO_INFO_FORMAT (&self->vinfo),
835 self->width, self->height);
836
837 buffer = gst_video_decoder_allocate_output_buffer (GST_VIDEO_DECODER (self));
838 if (!buffer)
839 goto fail;
840
841 if (!gst_video_frame_map (&src_frame, &self->vinfo,
842 codec_frame->output_buffer, GST_MAP_READ))
843 goto fail;
844
845 if (!gst_video_frame_map (&dest_frame, &dest_vinfo, buffer, GST_MAP_WRITE)) {
846 gst_video_frame_unmap (&dest_frame);
847 goto fail;
848 }
849
850 /* gst_video_frame_copy can crop this, but does not know, so let make it
851 * think it's all right */
852 GST_VIDEO_INFO_WIDTH (&src_frame.info) = self->width;
853 GST_VIDEO_INFO_HEIGHT (&src_frame.info) = self->height;
854
855 if (!gst_video_frame_copy (&dest_frame, &src_frame)) {
856 gst_video_frame_unmap (&src_frame);
857 gst_video_frame_unmap (&dest_frame);
858 goto fail;
859 }
860
861 gst_video_frame_unmap (&src_frame);
862 gst_video_frame_unmap (&dest_frame);
863 gst_buffer_replace (&codec_frame->output_buffer, buffer);
864 gst_buffer_unref (buffer);
865
866 return TRUE;
867
868 fail:
869 GST_ERROR_OBJECT (self, "Failed copy output buffer.");
870 return FALSE;
871 }
872
873
874 static GstVp9Picture *
gst_v4l2_codec_vp9_dec_duplicate_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)875 gst_v4l2_codec_vp9_dec_duplicate_picture (GstVp9Decoder * decoder,
876 GstVideoCodecFrame * frame, GstVp9Picture * picture)
877 {
878 GstVp9Picture *new_picture;
879
880 GST_DEBUG_OBJECT (decoder, "Duplicate picture %u",
881 picture->system_frame_number);
882
883 new_picture = gst_vp9_picture_new ();
884 new_picture->frame_hdr = picture->frame_hdr;
885 new_picture->system_frame_number = frame->system_frame_number;
886
887 if (GST_MINI_OBJECT_FLAG_IS_SET (picture, FLAG_PICTURE_OUTPUTED)) {
888 GstBuffer *output_buffer = gst_vp9_picture_get_user_data (picture);
889 if (output_buffer)
890 frame->output_buffer = gst_buffer_ref (output_buffer);
891 } else {
892 GstV4l2Request *request = gst_vp9_picture_get_user_data (picture);
893 gst_vp9_picture_set_user_data (new_picture, gst_v4l2_request_ref (request),
894 (GDestroyNotify) gst_v4l2_request_unref);
895 frame->output_buffer = gst_v4l2_request_dup_pic_buf (request);
896 }
897
898 return new_picture;
899 }
900
901 static GstFlowReturn
gst_v4l2_codec_vp9_dec_output_picture(GstVp9Decoder * decoder,GstVideoCodecFrame * frame,GstVp9Picture * picture)902 gst_v4l2_codec_vp9_dec_output_picture (GstVp9Decoder * decoder,
903 GstVideoCodecFrame * frame, GstVp9Picture * picture)
904 {
905 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
906 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
907 GstV4l2Request *request = gst_vp9_picture_get_user_data (picture);
908 gint ret;
909
910 GST_DEBUG_OBJECT (self, "Output picture %u", picture->system_frame_number);
911
912 if (request) {
913 ret = gst_v4l2_request_set_done (request);
914 if (ret == 0) {
915 GST_ELEMENT_ERROR (self, STREAM, DECODE,
916 ("Decoding frame took too long"), (NULL));
917 goto error;
918 } else if (ret < 0) {
919 GST_ELEMENT_ERROR (self, STREAM, DECODE,
920 ("Decoding request failed: %s", g_strerror (errno)), (NULL));
921 goto error;
922 }
923 g_return_val_if_fail (frame->output_buffer, GST_FLOW_ERROR);
924
925 if (gst_v4l2_request_failed (request)) {
926 GST_ELEMENT_ERROR (self, STREAM, DECODE,
927 ("Failed to decode frame %u", picture->system_frame_number), (NULL));
928 goto error;
929 }
930
931 /* Hold on reference buffers for the rest of the picture lifetime */
932 gst_vp9_picture_set_user_data (picture,
933 gst_buffer_ref (frame->output_buffer),
934 (GDestroyNotify) gst_buffer_unref);
935
936 GST_MINI_OBJECT_FLAG_SET (picture, FLAG_PICTURE_OUTPUTED);
937 }
938
939 /* This may happen if we duplicate a picture witch failed to decode */
940 if (!frame->output_buffer) {
941 GST_ELEMENT_ERROR (self, STREAM, DECODE,
942 ("Failed to decode frame %u", picture->system_frame_number), (NULL));
943 goto error;
944 }
945
946 if (self->copy_frames)
947 gst_v4l2_codec_vp9_dec_copy_output_buffer (self, frame);
948
949 gst_vp9_picture_unref (picture);
950
951 return gst_video_decoder_finish_frame (vdec, frame);
952
953 error:
954 gst_video_decoder_drop_frame (vdec, frame);
955 gst_vp9_picture_unref (picture);
956
957 return GST_FLOW_ERROR;
958 }
959
960 static void
gst_v4l2_codec_vp9_dec_set_flushing(GstV4l2CodecVp9Dec * self,gboolean flushing)961 gst_v4l2_codec_vp9_dec_set_flushing (GstV4l2CodecVp9Dec * self,
962 gboolean flushing)
963 {
964 if (self->sink_allocator)
965 gst_v4l2_codec_allocator_set_flushing (self->sink_allocator, flushing);
966 if (self->src_allocator)
967 gst_v4l2_codec_allocator_set_flushing (self->src_allocator, flushing);
968 }
969
970 static gboolean
gst_v4l2_codec_vp9_dec_flush(GstVideoDecoder * decoder)971 gst_v4l2_codec_vp9_dec_flush (GstVideoDecoder * decoder)
972 {
973 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
974
975 GST_DEBUG_OBJECT (self, "Flushing decoder state.");
976
977 gst_v4l2_decoder_flush (self->decoder);
978 gst_v4l2_codec_vp9_dec_set_flushing (self, FALSE);
979
980 return GST_VIDEO_DECODER_CLASS (parent_class)->flush (decoder);
981 }
982
983 static gboolean
gst_v4l2_codec_vp9_dec_sink_event(GstVideoDecoder * decoder,GstEvent * event)984 gst_v4l2_codec_vp9_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
985 {
986 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (decoder);
987
988 switch (GST_EVENT_TYPE (event)) {
989 case GST_EVENT_FLUSH_START:
990 GST_DEBUG_OBJECT (self, "flush start");
991 gst_v4l2_codec_vp9_dec_set_flushing (self, TRUE);
992 break;
993 default:
994 break;
995 }
996
997 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
998 }
999
1000 static GstStateChangeReturn
gst_v4l2_codec_vp9_dec_change_state(GstElement * element,GstStateChange transition)1001 gst_v4l2_codec_vp9_dec_change_state (GstElement * element,
1002 GstStateChange transition)
1003 {
1004 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (element);
1005
1006 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1007 gst_v4l2_codec_vp9_dec_set_flushing (self, TRUE);
1008
1009 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1010 }
1011
1012 static void
gst_v4l2_codec_vp9_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1013 gst_v4l2_codec_vp9_dec_set_property (GObject * object, guint prop_id,
1014 const GValue * value, GParamSpec * pspec)
1015 {
1016 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (object);
1017 GObject *dec = G_OBJECT (self->decoder);
1018
1019 switch (prop_id) {
1020 default:
1021 gst_v4l2_decoder_set_property (dec, prop_id - PROP_LAST, value, pspec);
1022 break;
1023 }
1024 }
1025
1026 static void
gst_v4l2_codec_vp9_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1027 gst_v4l2_codec_vp9_dec_get_property (GObject * object, guint prop_id,
1028 GValue * value, GParamSpec * pspec)
1029 {
1030 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (object);
1031 GObject *dec = G_OBJECT (self->decoder);
1032
1033 switch (prop_id) {
1034 default:
1035 gst_v4l2_decoder_get_property (dec, prop_id - PROP_LAST, value, pspec);
1036 break;
1037 }
1038 }
1039
1040 static void
gst_v4l2_codec_vp9_dec_init(GstV4l2CodecVp9Dec * self)1041 gst_v4l2_codec_vp9_dec_init (GstV4l2CodecVp9Dec * self)
1042 {
1043 GstVp9Decoder *parent = GST_VP9_DECODER (self);
1044 parent->parse_compressed_headers = TRUE;
1045 }
1046
1047 static void
gst_v4l2_codec_vp9_dec_subinit(GstV4l2CodecVp9Dec * self,GstV4l2CodecVp9DecClass * klass)1048 gst_v4l2_codec_vp9_dec_subinit (GstV4l2CodecVp9Dec * self,
1049 GstV4l2CodecVp9DecClass * klass)
1050 {
1051 self->decoder = gst_v4l2_decoder_new (klass->device);
1052 gst_video_info_init (&self->vinfo);
1053 }
1054
1055 static void
gst_v4l2_codec_vp9_dec_dispose(GObject * object)1056 gst_v4l2_codec_vp9_dec_dispose (GObject * object)
1057 {
1058 GstV4l2CodecVp9Dec *self = GST_V4L2_CODEC_VP9_DEC (object);
1059
1060 g_clear_object (&self->decoder);
1061
1062 G_OBJECT_CLASS (parent_class)->dispose (object);
1063 }
1064
1065 static void
gst_v4l2_codec_vp9_dec_class_init(GstV4l2CodecVp9DecClass * klass)1066 gst_v4l2_codec_vp9_dec_class_init (GstV4l2CodecVp9DecClass * klass)
1067 {
1068 }
1069
1070 static void
gst_v4l2_codec_vp9_dec_subclass_init(GstV4l2CodecVp9DecClass * klass,GstV4l2CodecDevice * device)1071 gst_v4l2_codec_vp9_dec_subclass_init (GstV4l2CodecVp9DecClass * klass,
1072 GstV4l2CodecDevice * device)
1073 {
1074 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1075 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
1076 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
1077 GstVp9DecoderClass *vp9decoder_class = GST_VP9_DECODER_CLASS (klass);
1078
1079 gobject_class->set_property = gst_v4l2_codec_vp9_dec_set_property;
1080 gobject_class->get_property = gst_v4l2_codec_vp9_dec_get_property;
1081 gobject_class->dispose = gst_v4l2_codec_vp9_dec_dispose;
1082
1083 gst_element_class_set_static_metadata (element_class,
1084 "V4L2 Stateless VP9 Video Decoder",
1085 "Codec/Decoder/Video/Hardware",
1086 "A V4L2 based VP9 video decoder",
1087 "Daniel Almeida <daniel.almeida@collabora.com>");
1088
1089 gst_element_class_add_static_pad_template (element_class, &sink_template);
1090 gst_element_class_add_static_pad_template (element_class, &src_template);
1091 element_class->change_state =
1092 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_change_state);
1093
1094 decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_open);
1095 decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_close);
1096 decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_stop);
1097 decoder_class->negotiate =
1098 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_negotiate);
1099 decoder_class->decide_allocation =
1100 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_decide_allocation);
1101 decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_flush);
1102 decoder_class->sink_event =
1103 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_sink_event);
1104
1105 vp9decoder_class->new_sequence =
1106 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_new_sequence);
1107 vp9decoder_class->start_picture =
1108 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_start_picture);
1109 vp9decoder_class->decode_picture =
1110 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_decode_picture);
1111 vp9decoder_class->end_picture =
1112 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_end_picture);
1113 vp9decoder_class->output_picture =
1114 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_output_picture);
1115 vp9decoder_class->duplicate_picture =
1116 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_duplicate_picture);
1117 vp9decoder_class->get_preferred_output_delay =
1118 GST_DEBUG_FUNCPTR (gst_v4l2_codec_vp9_dec_get_preferred_output_delay);
1119
1120 klass->device = device;
1121 gst_v4l2_decoder_install_properties (gobject_class, PROP_LAST, device);
1122 }
1123
gst_v4l2_codec_vp9_alpha_decode_bin_subclass_init(GstV4l2CodecAlphaDecodeBinClass * klass,gchar * decoder_name)1124 static void gst_v4l2_codec_vp9_alpha_decode_bin_subclass_init
1125 (GstV4l2CodecAlphaDecodeBinClass * klass, gchar * decoder_name)
1126 {
1127 GstV4l2CodecAlphaDecodeBinClass *adbin_class =
1128 (GstV4l2CodecAlphaDecodeBinClass *) klass;
1129 GstElementClass *element_class = (GstElementClass *) klass;
1130
1131 adbin_class->decoder_name = decoder_name;
1132 gst_element_class_add_static_pad_template (element_class, &alpha_template);
1133
1134 gst_element_class_set_static_metadata (element_class,
1135 "VP9 Alpha Decoder", "Codec/Decoder/Video",
1136 "Wrapper bin to decode VP9 with alpha stream.",
1137 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1138 }
1139
1140 void
gst_v4l2_codec_vp9_dec_register(GstPlugin * plugin,GstV4l2Decoder * decoder,GstV4l2CodecDevice * device,guint rank)1141 gst_v4l2_codec_vp9_dec_register (GstPlugin * plugin, GstV4l2Decoder * decoder,
1142 GstV4l2CodecDevice * device, guint rank)
1143 {
1144 gchar *element_name;
1145 GstCaps *src_caps, *alpha_caps;
1146
1147 GST_DEBUG_CATEGORY_INIT (v4l2_vp9dec_debug, "v4l2codecs-vp9dec", 0,
1148 "V4L2 stateless VP9 decoder");
1149
1150 if (!gst_v4l2_decoder_set_sink_fmt (decoder, V4L2_PIX_FMT_VP9_FRAME,
1151 320, 240, 8))
1152 return;
1153 src_caps = gst_v4l2_decoder_enum_src_formats (decoder);
1154
1155 if (gst_caps_is_empty (src_caps)) {
1156 GST_WARNING ("Not registering VP9 decoder since it produces no "
1157 "supported format");
1158 goto done;
1159 }
1160
1161 gst_v4l2_decoder_register (plugin, GST_TYPE_V4L2_CODEC_VP9_DEC,
1162 (GClassInitFunc) gst_v4l2_codec_vp9_dec_subclass_init,
1163 gst_mini_object_ref (GST_MINI_OBJECT (device)),
1164 (GInstanceInitFunc) gst_v4l2_codec_vp9_dec_subinit,
1165 "v4l2sl%svp9dec", device, rank, &element_name);
1166
1167 if (!element_name)
1168 goto done;
1169
1170 alpha_caps = gst_caps_from_string ("video/x-raw,format={I420, NV12}");
1171
1172 if (gst_caps_can_intersect (src_caps, alpha_caps))
1173 gst_v4l2_codec_alpha_decode_bin_register (plugin,
1174 (GClassInitFunc) gst_v4l2_codec_vp9_alpha_decode_bin_subclass_init,
1175 element_name, "v4l2slvp9%salphadecodebin", device, rank);
1176
1177 gst_caps_unref (alpha_caps);
1178
1179 done:
1180 gst_caps_unref (src_caps);
1181 }
1182