2 * Copyright (C) 2020 Intel Corporation
3 * Author: He Junyan <junyan.he@intel.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-vaav1dec
24 * @short_description: A VA-API based AV1 video decoder
26 * vaav1dec decodes AV1 bitstreams to VA surfaces using the
27 * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
30 * The decoding surfaces can be mapped onto main memory as video
33 * ## Example launch line
35 * gst-launch-1.0 filesrc location=sample.av1 ! ivfparse ! av1parse ! vaav1dec ! autovideosink
46 #include <gst/va/gstva.h>
48 #include "gstvaav1dec.h"
49 #include "gstvabasedec.h"
51 GST_DEBUG_CATEGORY_STATIC (gst_va_av1dec_debug);
52 #ifndef GST_DISABLE_GST_DEBUG
53 #define GST_CAT_DEFAULT gst_va_av1dec_debug
55 #define GST_CAT_DEFAULT NULL
58 #define GST_VA_AV1_DEC(obj) ((GstVaAV1Dec *) obj)
59 #define GST_VA_AV1_DEC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_FROM_INSTANCE (obj), GstVaAV1DecClass))
60 #define GST_VA_AV1_DEC_CLASS(klass) ((GstVaAV1DecClass *) klass)
62 typedef struct _GstVaAV1Dec GstVaAV1Dec;
63 typedef struct _GstVaAV1DecClass GstVaAV1DecClass;
65 struct _GstVaAV1DecClass
67 GstVaBaseDecClass parent_class;
74 GstFlowReturn last_ret;
76 GstAV1SequenceHeaderOBU seq;
79 GstVideoFormat preferred_format;
80 /* Used for layers not output. */
81 GstBufferPool *internal_pool;
84 static GstElementClass *parent_class = NULL;
87 static const gchar *src_caps_str =
88 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
89 "{ NV12, P010_10LE }") " ;"
90 GST_VIDEO_CAPS_MAKE ("{ NV12, P010_10LE }");
93 static const gchar *sink_caps_str = "video/x-av1";
96 gst_va_av1_dec_negotiate (GstVideoDecoder * decoder)
98 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
99 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
100 GstAV1Decoder *av1dec = GST_AV1_DECODER (decoder);
101 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
102 GstCapsFeatures *capsfeatures = NULL;
104 /* Ignore downstream renegotiation request. */
105 if (!base->need_negotiation)
108 base->need_negotiation = FALSE;
110 /* Do not re-create the context if only the frame size changes */
111 if (!gst_va_decoder_config_is_equal (base->decoder, base->profile,
112 base->rt_format, self->max_width, self->max_height)) {
113 if (gst_va_decoder_is_open (base->decoder)
114 && !gst_va_decoder_close (base->decoder))
117 if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
120 if (!gst_va_decoder_set_frame_size (base->decoder, self->max_width,
125 if (base->output_state)
126 gst_video_codec_state_unref (base->output_state);
128 gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
130 if (format == GST_VIDEO_FORMAT_UNKNOWN)
133 if (self->preferred_format != GST_VIDEO_FORMAT_UNKNOWN &&
134 self->preferred_format != format) {
135 GST_WARNING_OBJECT (self, "The preferred_format is different from"
139 self->preferred_format = format;
141 base->output_state = gst_video_decoder_set_output_state (decoder, format,
142 base->width, base->height, av1dec->input_state);
144 base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
146 gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
148 GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
149 base->output_state->caps);
151 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
155 _complete_sink_caps (GstCaps * sinkcaps)
157 GstCaps *caps = gst_caps_copy (sinkcaps);
158 GValue val = G_VALUE_INIT;
160 g_value_init (&val, G_TYPE_STRING);
161 g_value_set_string (&val, "frame");
162 gst_caps_set_value (caps, "alignment", &val);
163 g_value_unset (&val);
169 _get_profile (GstVaAV1Dec * self, const GstAV1SequenceHeaderOBU * seq_hdr)
171 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
172 VAProfile profile = VAProfileNone;
174 switch (seq_hdr->seq_profile) {
175 case GST_AV1_PROFILE_0:
176 profile = VAProfileAV1Profile0;
178 case GST_AV1_PROFILE_1:
179 profile = VAProfileAV1Profile1;
182 GST_ERROR_OBJECT (self, "Unsupported av1 profile value %d",
183 seq_hdr->seq_profile);
184 return VAProfileNone;
187 if (!gst_va_decoder_has_profile (base->decoder, profile)) {
188 GST_ERROR_OBJECT (self, "Profile %s is not supported by HW",
189 gst_va_profile_name (profile));
190 return VAProfileNone;
197 _get_rtformat (GstVaAV1Dec * self, VAProfile profile,
198 const GstAV1SequenceHeaderOBU * seq_header)
201 seq_profile Bit depth Monochrome support Chroma subsampling
202 0 8 or 10 Yes YUV 4:2:0
203 1 8 or 10 No YUV 4:4:4
204 2 8 or 10 Yes YUV 4:2:2
205 2 12 Yes YUV 4:2:0,YUV 4:2:2,YUV 4:4:4
208 /* TODO: consider Monochrome case. Just return 4:2:0 for Monochrome now. */
210 case VAProfileAV1Profile0:
211 if (seq_header->bit_depth == 8) {
212 return VA_RT_FORMAT_YUV420;
213 } else if (seq_header->bit_depth == 10) {
214 return VA_RT_FORMAT_YUV420_10;
217 case VAProfileAV1Profile1:
218 if (seq_header->bit_depth == 8) {
219 return VA_RT_FORMAT_YUV444;
220 } else if (seq_header->bit_depth == 10) {
221 return VA_RT_FORMAT_YUV444_10;
228 GST_ERROR_OBJECT (self, "Fail to find rtformat for profile:%s, bit_depth:%d",
229 gst_va_profile_name (profile), seq_header->bit_depth);
234 gst_va_av1_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
236 GstCaps *sinkcaps, *caps = NULL, *tmp;
237 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
240 caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
243 sinkcaps = _complete_sink_caps (caps);
244 gst_caps_unref (caps);
246 tmp = gst_caps_intersect_full (filter, sinkcaps,
247 GST_CAPS_INTERSECT_FIRST);
248 gst_caps_unref (sinkcaps);
253 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
255 caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
262 _clear_internal_pool (GstVaAV1Dec * self)
264 if (self->internal_pool)
265 gst_buffer_pool_set_active (self->internal_pool, FALSE);
267 gst_clear_object (&self->internal_pool);
270 static GstBufferPool *
271 _create_internal_pool (GstVaAV1Dec * self, gint width, gint height)
273 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
275 GArray *surface_formats;
276 GstAllocator *allocator;
277 GstCaps *caps = NULL;
279 GstAllocationParams params = { 0, };
281 gst_allocation_params_init (¶ms);
283 /* We may come here before the negotiation, make sure all pools
284 use the same video format. */
285 if (self->preferred_format == GST_VIDEO_FORMAT_UNKNOWN) {
286 GstVideoFormat format;
288 gst_va_base_dec_get_preferred_format_and_caps_features (base,
290 if (format == GST_VIDEO_FORMAT_UNKNOWN) {
291 GST_WARNING_OBJECT (self, "Failed to get format for internal pool");
295 self->preferred_format = format;
298 gst_video_info_set_format (&info, self->preferred_format, width, height);
300 caps = gst_video_info_to_caps (&info);
302 GST_WARNING_OBJECT (self, "Failed to create caps for internal pool");
306 gst_caps_set_features_simple (caps,
307 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
309 surface_formats = gst_va_decoder_get_surface_formats (base->decoder);
310 allocator = gst_va_allocator_new (base->display, surface_formats);
312 pool = gst_va_pool_new_with_config (caps, GST_VIDEO_INFO_SIZE (&info),
313 1, 0, VA_SURFACE_ATTRIB_USAGE_HINT_DECODER, GST_VA_FEATURE_AUTO,
315 gst_clear_caps (&caps);
318 GST_WARNING_OBJECT (self, "Failed to create internal pool");
319 gst_object_unref (allocator);
323 gst_object_unref (allocator);
325 gst_buffer_pool_set_active (pool, TRUE);
331 gst_va_av1_dec_new_sequence (GstAV1Decoder * decoder,
332 const GstAV1SequenceHeaderOBU * seq_hdr, gint max_dpb_size)
334 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
335 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
339 GST_LOG_OBJECT (self, "new sequence");
341 profile = _get_profile (self, seq_hdr);
342 if (profile == VAProfileNone)
343 return GST_FLOW_NOT_NEGOTIATED;
345 rt_format = _get_rtformat (self, profile, seq_hdr);
347 return GST_FLOW_NOT_NEGOTIATED;
349 self->seq = *seq_hdr;
351 if (!gst_va_decoder_config_is_equal (base->decoder, profile,
352 rt_format, seq_hdr->max_frame_width_minus_1 + 1,
353 seq_hdr->max_frame_height_minus_1 + 1)) {
354 _clear_internal_pool (self);
355 self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
357 base->profile = profile;
358 base->rt_format = rt_format;
359 self->max_width = seq_hdr->max_frame_width_minus_1 + 1;
360 self->max_height = seq_hdr->max_frame_height_minus_1 + 1;
361 base->need_negotiation = TRUE;
363 base->min_buffers = 7 + 4; /* dpb size + scratch surfaces */
365 /* May be changed by frame header */
366 base->width = self->max_width;
367 base->height = self->max_height;
368 base->need_valign = FALSE;
375 gst_va_av1_dec_new_picture (GstAV1Decoder * decoder,
376 GstVideoCodecFrame * frame, GstAV1Picture * picture)
378 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
379 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
380 GstVaDecodePicture *pic;
381 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
382 GstAV1FrameHeaderOBU *frame_hdr = &picture->frame_hdr;
384 /* Only output the highest spatial layer. For non output pictures,
385 we just use internal pool, then no negotiation needed. */
386 if (picture->spatial_id < decoder->highest_spatial_layer) {
387 if (!self->internal_pool) {
388 self->internal_pool =
389 _create_internal_pool (self, self->max_width, self->max_height);
390 if (!self->internal_pool)
391 return GST_FLOW_ERROR;
394 if (frame_hdr->upscaled_width != base->width
395 || frame_hdr->frame_height != base->height) {
396 base->width = frame_hdr->upscaled_width;
397 base->height = frame_hdr->frame_height;
399 if (base->width < self->max_width || base->height < self->max_height) {
400 base->need_valign = TRUE;
402 base->valign = (GstVideoAlignment){
403 .padding_bottom = self->max_height - base->height,
404 .padding_right = self->max_width - base->width,
409 base->need_negotiation = TRUE;
413 if (base->need_negotiation) {
414 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
415 GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
416 return GST_FLOW_NOT_NEGOTIATED;
420 if (picture->spatial_id < decoder->highest_spatial_layer) {
421 self->last_ret = gst_buffer_pool_acquire_buffer (self->internal_pool,
422 &frame->output_buffer, NULL);
423 if (self->last_ret != GST_FLOW_OK) {
424 GST_WARNING_OBJECT (self,
425 "Failed to allocated output buffer from internal pool, return %s",
426 gst_flow_get_name (self->last_ret));
427 return self->last_ret;
430 self->last_ret = gst_video_decoder_allocate_output_frame (vdec, frame);
431 if (self->last_ret != GST_FLOW_OK) {
432 GST_WARNING_OBJECT (self,
433 "Failed to allocated output buffer, return %s",
434 gst_flow_get_name (self->last_ret));
435 return self->last_ret;
439 if (picture->apply_grain) {
440 if (!gst_va_buffer_create_aux_surface (frame->output_buffer)) {
441 GST_WARNING_OBJECT (self,
442 "Failed to allocated aux surface for buffer %p",
443 frame->output_buffer);
444 return GST_FLOW_ERROR;
448 pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
450 gst_av1_picture_set_user_data (picture, pic,
451 (GDestroyNotify) gst_va_decode_picture_free);
453 if (picture->apply_grain) {
454 GST_LOG_OBJECT (self, "New va decode picture %p - %#x(aux: %#x)", pic,
455 gst_va_decode_picture_get_surface (pic),
456 gst_va_decode_picture_get_aux_surface (pic));
458 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
459 gst_va_decode_picture_get_surface (pic));
465 static GstAV1Picture *
466 gst_va_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
467 GstVideoCodecFrame * frame, GstAV1Picture * picture)
469 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
470 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
471 GstVaDecodePicture *pic;
472 GstVaDecodePicture *new_pic;
473 GstAV1Picture *new_picture;
475 pic = gst_av1_picture_get_user_data (picture);
477 GST_ERROR_OBJECT (self, "Parent picture does not have a va picture");
481 new_picture = gst_av1_picture_new ();
482 g_assert (pic->gstbuffer);
483 new_pic = gst_va_decode_picture_new (base->decoder, pic->gstbuffer);
485 GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT
486 " (surface %#x)", pic, gst_va_decode_picture_get_surface (pic));
488 gst_av1_picture_set_user_data (new_picture, new_pic,
489 (GDestroyNotify) gst_va_decode_picture_free);
495 _setup_segment_info (VADecPictureParameterBufferAV1 * pic_param,
496 GstAV1FrameHeaderOBU * frame_header)
499 uint8_t feature_mask;
501 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++)
502 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++)
503 pic_param->seg_info.feature_data[i][j] =
504 frame_header->segmentation_params.feature_data[i][j];
506 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
508 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
509 if (frame_header->segmentation_params.feature_enabled[i][j])
510 feature_mask |= 1 << j;
512 pic_param->seg_info.feature_mask[i] = feature_mask;
517 _setup_film_grain_info (VADecPictureParameterBufferAV1 * pic_param,
518 GstAV1FrameHeaderOBU * frame_header)
522 if (!frame_header->film_grain_params.apply_grain)
525 pic_param->film_grain_info.num_y_points =
526 frame_header->film_grain_params.num_y_points;
527 for (i = 0; i < frame_header->film_grain_params.num_y_points; i++) {
528 pic_param->film_grain_info.point_y_value[i] =
529 frame_header->film_grain_params.point_y_value[i];
530 pic_param->film_grain_info.point_y_scaling[i] =
531 frame_header->film_grain_params.point_y_scaling[i];
534 pic_param->film_grain_info.num_cb_points =
535 frame_header->film_grain_params.num_cb_points;
536 for (i = 0; i < frame_header->film_grain_params.num_cb_points; i++) {
537 pic_param->film_grain_info.point_cb_value[i] =
538 frame_header->film_grain_params.point_cb_value[i];
539 pic_param->film_grain_info.point_cb_scaling[i] =
540 frame_header->film_grain_params.point_cb_scaling[i];
543 pic_param->film_grain_info.num_cr_points =
544 frame_header->film_grain_params.num_cr_points;
545 for (i = 0; i < frame_header->film_grain_params.num_cr_points; i++) {
546 pic_param->film_grain_info.point_cr_value[i] =
547 frame_header->film_grain_params.point_cr_value[i];
548 pic_param->film_grain_info.point_cr_scaling[i] =
549 frame_header->film_grain_params.point_cr_scaling[i];
553 if (pic_param->film_grain_info.num_y_points) {
554 for (i = 0; i < 24; i++) {
555 pic_param->film_grain_info.ar_coeffs_y[i] =
556 frame_header->film_grain_params.ar_coeffs_y_plus_128[i] - 128;
559 if (frame_header->film_grain_params.chroma_scaling_from_luma
560 || pic_param->film_grain_info.num_cb_points) {
561 for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
562 pic_param->film_grain_info.ar_coeffs_cb[i] =
563 frame_header->film_grain_params.ar_coeffs_cb_plus_128[i] - 128;
566 if (frame_header->film_grain_params.chroma_scaling_from_luma
567 || pic_param->film_grain_info.num_cr_points) {
568 for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
569 pic_param->film_grain_info.ar_coeffs_cr[i] =
570 frame_header->film_grain_params.ar_coeffs_cr_plus_128[i] - 128;
576 _setup_loop_filter_info (VADecPictureParameterBufferAV1 * pic_param,
577 GstAV1FrameHeaderOBU * frame_header)
581 pic_param->filter_level[0] =
582 frame_header->loop_filter_params.loop_filter_level[0];
583 pic_param->filter_level[1] =
584 frame_header->loop_filter_params.loop_filter_level[1];
585 pic_param->filter_level_u =
586 frame_header->loop_filter_params.loop_filter_level[2];
587 pic_param->filter_level_v =
588 frame_header->loop_filter_params.loop_filter_level[3];
590 for (i = 0; i < GST_AV1_TOTAL_REFS_PER_FRAME; i++)
591 pic_param->ref_deltas[i] =
592 frame_header->loop_filter_params.loop_filter_ref_deltas[i];
593 for (i = 0; i < 2; i++)
594 pic_param->mode_deltas[i] =
595 frame_header->loop_filter_params.loop_filter_mode_deltas[i];
599 _setup_quantization_info (VADecPictureParameterBufferAV1 * pic_param,
600 GstAV1FrameHeaderOBU * frame_header)
602 pic_param->qmatrix_fields.bits.using_qmatrix =
603 frame_header->quantization_params.using_qmatrix;
604 if (frame_header->quantization_params.using_qmatrix) {
605 pic_param->qmatrix_fields.bits.qm_y =
606 frame_header->quantization_params.qm_y;
607 pic_param->qmatrix_fields.bits.qm_u =
608 frame_header->quantization_params.qm_u;
609 pic_param->qmatrix_fields.bits.qm_v =
610 frame_header->quantization_params.qm_v;
612 pic_param->qmatrix_fields.bits.qm_y = 0;
613 pic_param->qmatrix_fields.bits.qm_u = 0;
614 pic_param->qmatrix_fields.bits.qm_v = 0;
619 _setup_cdef_info (VADecPictureParameterBufferAV1 * pic_param,
620 GstAV1FrameHeaderOBU * frame_header, guint8 num_planes)
625 pic_param->cdef_damping_minus_3 = frame_header->cdef_params.cdef_damping - 3;
626 pic_param->cdef_bits = frame_header->cdef_params.cdef_bits;
627 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
628 sec_strength = frame_header->cdef_params.cdef_y_sec_strength[i];
629 g_assert (sec_strength <= 4);
630 /* may need to minus 1 in order to merge with primary value. */
631 if (sec_strength == 4)
634 pic_param->cdef_y_strengths[i] =
635 ((frame_header->cdef_params.cdef_y_pri_strength[i] & 0xf) << 2) |
636 (sec_strength & 0x03);
638 if (num_planes > 1) {
639 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
640 sec_strength = frame_header->cdef_params.cdef_uv_sec_strength[i];
641 g_assert (sec_strength <= 4);
642 /* may need to minus 1 in order to merge with primary value. */
643 if (sec_strength == 4)
646 pic_param->cdef_uv_strengths[i] =
647 ((frame_header->cdef_params.cdef_uv_pri_strength[i] & 0xf) << 2) |
648 (sec_strength & 0x03);
651 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
652 pic_param->cdef_uv_strengths[i] = 0;
658 _setup_global_motion_info (VADecPictureParameterBufferAV1 * pic_param,
659 GstAV1FrameHeaderOBU * frame_header)
663 for (i = 0; i < 7; i++) {
664 /* assuming VAAV1TransformationType and GstAV1WarpModelType are
666 pic_param->wm[i].wmtype = (VAAV1TransformationType)
667 frame_header->global_motion_params.gm_type[GST_AV1_REF_LAST_FRAME + i];
669 for (j = 0; j < 6; j++)
670 pic_param->wm[i].wmmat[j] =
671 frame_header->global_motion_params.gm_params
672 [GST_AV1_REF_LAST_FRAME + i][j];
674 pic_param->wm[i].wmmat[6] = 0;
675 pic_param->wm[i].wmmat[7] = 0;
677 pic_param->wm[i].invalid =
678 frame_header->global_motion_params.invalid[GST_AV1_REF_LAST_FRAME + i];
683 gst_va_av1_dec_start_picture (GstAV1Decoder * decoder, GstAV1Picture * picture,
686 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
687 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
688 GstAV1FrameHeaderOBU *frame_header = &picture->frame_hdr;
689 GstAV1SequenceHeaderOBU *seq_header = &self->seq;
690 VADecPictureParameterBufferAV1 pic_param = { };
691 GstVaDecodePicture *va_pic;
694 va_pic = gst_av1_picture_get_user_data (picture);
698 pic_param = (VADecPictureParameterBufferAV1){
699 .profile = seq_header->seq_profile,
700 .order_hint_bits_minus_1 = seq_header->order_hint_bits_minus_1,
701 .matrix_coefficients = seq_header->color_config.matrix_coefficients,
702 .seq_info_fields.fields = {
703 .still_picture = seq_header->still_picture,
704 .use_128x128_superblock = seq_header->use_128x128_superblock,
705 .enable_filter_intra = seq_header->enable_filter_intra,
706 .enable_intra_edge_filter = seq_header->enable_intra_edge_filter,
707 .enable_interintra_compound = seq_header->enable_interintra_compound,
708 .enable_masked_compound = seq_header->enable_masked_compound,
709 .enable_dual_filter = seq_header->enable_dual_filter,
710 .enable_order_hint = seq_header->enable_order_hint,
711 .enable_jnt_comp = seq_header->enable_jnt_comp,
712 .enable_cdef = seq_header->enable_cdef,
713 .mono_chrome = seq_header->color_config.mono_chrome,
714 .color_range = seq_header->color_config.color_range,
715 .subsampling_x = seq_header->color_config.subsampling_x,
716 .subsampling_y = seq_header->color_config.subsampling_y,
717 .film_grain_params_present = seq_header->film_grain_params_present,
719 .anchor_frames_num = 0,
720 .anchor_frames_list = NULL,
721 .frame_width_minus1 = frame_header->upscaled_width - 1,
722 .frame_height_minus1 = frame_header->frame_height - 1,
723 .output_frame_width_in_tiles_minus_1 = 0,
724 .output_frame_height_in_tiles_minus_1 = 0,
725 .order_hint = frame_header->order_hint,
727 .seg_info.segment_info_fields.bits = {
728 .enabled = frame_header->segmentation_params.segmentation_enabled,
729 .update_map = frame_header->segmentation_params.segmentation_update_map,
731 frame_header->segmentation_params.segmentation_temporal_update,
733 frame_header->segmentation_params.segmentation_update_data,
737 .film_grain_info_fields.bits = {
738 .apply_grain = frame_header->film_grain_params.apply_grain,
739 .chroma_scaling_from_luma =
740 frame_header->film_grain_params.chroma_scaling_from_luma,
741 .grain_scaling_minus_8 =
742 frame_header->film_grain_params.grain_scaling_minus_8,
743 .ar_coeff_lag = frame_header->film_grain_params.ar_coeff_lag,
744 .ar_coeff_shift_minus_6 =
745 frame_header->film_grain_params.ar_coeff_shift_minus_6,
746 .grain_scale_shift = frame_header->film_grain_params.grain_scale_shift,
747 .overlap_flag = frame_header->film_grain_params.overlap_flag,
748 .clip_to_restricted_range =
749 frame_header->film_grain_params.clip_to_restricted_range,
751 .grain_seed = frame_header->film_grain_params.grain_seed,
752 .cb_mult = frame_header->film_grain_params.cb_mult,
753 .cb_luma_mult = frame_header->film_grain_params.cb_luma_mult,
754 .cb_offset = frame_header->film_grain_params.cb_offset,
755 .cr_mult = frame_header->film_grain_params.cr_mult,
756 .cr_luma_mult = frame_header->film_grain_params.cr_luma_mult,
757 .cr_offset = frame_header->film_grain_params.cr_offset,
759 .tile_cols = frame_header->tile_info.tile_cols,
760 .tile_rows = frame_header->tile_info.tile_rows,
761 .context_update_tile_id = frame_header->tile_info.context_update_tile_id,
762 .pic_info_fields.bits = {
763 .frame_type = frame_header->frame_type,
764 .show_frame = frame_header->show_frame,
765 .showable_frame = frame_header->showable_frame,
766 .error_resilient_mode = frame_header->error_resilient_mode,
767 .disable_cdf_update = frame_header->disable_cdf_update,
768 .allow_screen_content_tools = frame_header->allow_screen_content_tools,
769 .force_integer_mv = frame_header->force_integer_mv,
770 .allow_intrabc = frame_header->allow_intrabc,
771 .use_superres = frame_header->use_superres,
772 .allow_high_precision_mv = frame_header->allow_high_precision_mv,
773 .is_motion_mode_switchable = frame_header->is_motion_mode_switchable,
774 .use_ref_frame_mvs = frame_header->use_ref_frame_mvs,
775 .disable_frame_end_update_cdf =
776 frame_header->disable_frame_end_update_cdf,
777 .uniform_tile_spacing_flag =
778 frame_header->tile_info.uniform_tile_spacing_flag,
779 .allow_warped_motion = frame_header->allow_warped_motion,
781 .superres_scale_denominator = frame_header->superres_denom,
782 .interp_filter = frame_header->interpolation_filter,
784 .loop_filter_info_fields.bits = {
786 frame_header->loop_filter_params.loop_filter_sharpness,
787 .mode_ref_delta_enabled =
788 frame_header->loop_filter_params.loop_filter_delta_enabled,
789 .mode_ref_delta_update =
790 frame_header->loop_filter_params.loop_filter_delta_update,
792 .mode_control_fields.bits = {
793 .delta_lf_present_flag =
794 frame_header->loop_filter_params.delta_lf_present,
795 .log2_delta_lf_res = frame_header->loop_filter_params.delta_lf_res,
796 .delta_lf_multi = frame_header->loop_filter_params.delta_lf_multi,
797 .delta_q_present_flag =
798 frame_header->quantization_params.delta_q_present,
799 .log2_delta_q_res = frame_header->quantization_params.delta_q_res,
800 .tx_mode = frame_header->tx_mode,
801 .reference_select = frame_header->reference_select,
802 .reduced_tx_set_used = frame_header->reduced_tx_set,
803 .skip_mode_present = frame_header->skip_mode_present,
806 .base_qindex = frame_header->quantization_params.base_q_idx,
807 .y_dc_delta_q = frame_header->quantization_params.delta_q_y_dc,
808 .u_dc_delta_q = frame_header->quantization_params.delta_q_u_dc,
809 .u_ac_delta_q = frame_header->quantization_params.delta_q_u_ac,
810 .v_dc_delta_q = frame_header->quantization_params.delta_q_v_dc,
811 .v_ac_delta_q = frame_header->quantization_params.delta_q_v_ac,
812 /* loop restoration */
813 .loop_restoration_fields.bits = {
814 .yframe_restoration_type =
815 frame_header->loop_restoration_params.frame_restoration_type[0],
816 .cbframe_restoration_type =
817 frame_header->loop_restoration_params.frame_restoration_type[1],
818 .crframe_restoration_type =
819 frame_header->loop_restoration_params.frame_restoration_type[2],
820 .lr_unit_shift = frame_header->loop_restoration_params.lr_unit_shift,
821 .lr_uv_shift = frame_header->loop_restoration_params.lr_uv_shift,
826 if (seq_header->bit_depth == 8) {
827 pic_param.bit_depth_idx = 0;
828 } else if (seq_header->bit_depth == 10) {
829 pic_param.bit_depth_idx = 1;
830 } else if (seq_header->bit_depth == 12) {
831 pic_param.bit_depth_idx = 2;
833 g_assert_not_reached ();
836 if (frame_header->film_grain_params.apply_grain) {
837 pic_param.current_frame = gst_va_decode_picture_get_aux_surface (va_pic);
838 pic_param.current_display_picture =
839 gst_va_decode_picture_get_surface (va_pic);
841 pic_param.current_frame = gst_va_decode_picture_get_surface (va_pic);
842 pic_param.current_display_picture = VA_INVALID_SURFACE;
845 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
846 if (dpb->pic_list[i]) {
847 if (dpb->pic_list[i]->apply_grain) {
848 pic_param.ref_frame_map[i] = gst_va_decode_picture_get_aux_surface
849 (gst_av1_picture_get_user_data (dpb->pic_list[i]));
851 pic_param.ref_frame_map[i] = gst_va_decode_picture_get_surface
852 (gst_av1_picture_get_user_data (dpb->pic_list[i]));
855 pic_param.ref_frame_map[i] = VA_INVALID_SURFACE;
858 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
859 pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i];
861 pic_param.primary_ref_frame = frame_header->primary_ref_frame;
863 _setup_segment_info (&pic_param, frame_header);
864 _setup_film_grain_info (&pic_param, frame_header);
866 for (i = 0; i < 63; i++) {
867 pic_param.width_in_sbs_minus_1[i] =
868 frame_header->tile_info.width_in_sbs_minus_1[i];
869 pic_param.height_in_sbs_minus_1[i] =
870 frame_header->tile_info.height_in_sbs_minus_1[i];
873 _setup_loop_filter_info (&pic_param, frame_header);
874 _setup_quantization_info (&pic_param, frame_header);
875 _setup_cdef_info (&pic_param, frame_header, seq_header->num_planes);
876 _setup_global_motion_info (&pic_param, frame_header);
878 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
879 VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
880 return GST_FLOW_ERROR;
886 gst_va_av1_dec_decode_tile (GstAV1Decoder * decoder, GstAV1Picture * picture,
889 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
890 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
891 GstAV1TileGroupOBU *tile_group = &tile->tile_group;
892 GstVaDecodePicture *va_pic;
894 VASliceParameterBufferAV1 slice_param[GST_AV1_MAX_TILE_COUNT];
896 GST_TRACE_OBJECT (self, "-");
898 for (i = 0; i < tile_group->tg_end - tile_group->tg_start + 1; i++) {
899 slice_param[i] = (VASliceParameterBufferAV1) {
901 slice_param[i].slice_data_size =
902 tile_group->entry[tile_group->tg_start + i].tile_size;
903 slice_param[i].slice_data_offset =
904 tile_group->entry[tile_group->tg_start + i].tile_offset;
905 slice_param[i].tile_row =
906 tile_group->entry[tile_group->tg_start + i].tile_row;
907 slice_param[i].tile_column =
908 tile_group->entry[tile_group->tg_start + i].tile_col;
909 slice_param[i].slice_data_flag = 0;
912 va_pic = gst_av1_picture_get_user_data (picture);
914 if (!gst_va_decoder_add_slice_buffer_with_n_params (base->decoder, va_pic,
915 slice_param, sizeof (VASliceParameterBufferAV1), i, tile->obu.data,
916 tile->obu.obu_size)) {
917 return GST_FLOW_ERROR;
924 gst_va_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
926 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
927 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
928 GstVaDecodePicture *va_pic;
930 GST_LOG_OBJECT (self, "end picture %p, (system_frame_number %d)",
931 picture, picture->system_frame_number);
933 va_pic = gst_av1_picture_get_user_data (picture);
935 if (!gst_va_decoder_decode_with_aux_surface (base->decoder, va_pic,
936 picture->apply_grain)) {
937 return GST_FLOW_ERROR;
944 gst_va_av1_dec_output_picture (GstAV1Decoder * decoder,
945 GstVideoCodecFrame * frame, GstAV1Picture * picture)
947 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
948 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
950 g_assert (picture->frame_hdr.show_frame ||
951 picture->frame_hdr.show_existing_frame);
953 GST_LOG_OBJECT (self,
954 "Outputting picture %p (system_frame_number %d)",
955 picture, picture->system_frame_number);
957 if (self->last_ret != GST_FLOW_OK) {
958 gst_av1_picture_unref (picture);
959 gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self), frame);
960 return self->last_ret;
963 if (picture->frame_hdr.show_existing_frame) {
964 GstVaDecodePicture *pic;
966 g_assert (!frame->output_buffer);
967 pic = gst_av1_picture_get_user_data (picture);
968 frame->output_buffer = gst_buffer_ref (pic->gstbuffer);
971 if (base->copy_frames)
972 gst_va_base_dec_copy_output_buffer (base, frame);
974 gst_av1_picture_unref (picture);
976 return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
980 gst_va_av1_dec_start (GstVideoDecoder * decoder)
982 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
984 self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
986 return GST_VIDEO_DECODER_CLASS (parent_class)->start (decoder);
990 gst_va_av1_dec_close (GstVideoDecoder * decoder)
992 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
994 _clear_internal_pool (self);
996 return gst_va_base_dec_close (GST_VIDEO_DECODER (decoder));
1000 gst_va_av1_dec_init (GTypeInstance * instance, gpointer g_class)
1002 gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
1006 gst_va_av1_dec_dispose (GObject * object)
1008 gst_va_base_dec_close (GST_VIDEO_DECODER (object));
1009 G_OBJECT_CLASS (parent_class)->dispose (object);
1013 gst_va_av1_dec_class_init (gpointer g_class, gpointer class_data)
1015 GstCaps *src_doc_caps, *sink_doc_caps;
1016 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
1017 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1018 GstAV1DecoderClass *av1decoder_class = GST_AV1_DECODER_CLASS (g_class);
1019 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
1020 struct CData *cdata = class_data;
1023 if (cdata->description) {
1024 long_name = g_strdup_printf ("VA-API AV1 Decoder in %s",
1025 cdata->description);
1027 long_name = g_strdup ("VA-API AV1 Decoder");
1030 gst_element_class_set_metadata (element_class, long_name,
1031 "Codec/Decoder/Video/Hardware",
1032 "VA-API based AV1 video decoder", "He Junyan <junyan.he@intel.com>");
1034 sink_doc_caps = gst_caps_from_string (sink_caps_str);
1035 src_doc_caps = gst_caps_from_string (src_caps_str);
1037 parent_class = g_type_class_peek_parent (g_class);
1040 * GstVaAV1Dec:device-path:
1042 * It shows the DRM device path used for the VA operation, if any.
1046 gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), AV1,
1047 cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
1048 src_doc_caps, sink_doc_caps);
1050 gobject_class->dispose = gst_va_av1_dec_dispose;
1052 decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_av1_dec_getcaps);
1053 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_av1_dec_negotiate);
1054 decoder_class->close = GST_DEBUG_FUNCPTR (gst_va_av1_dec_close);
1055 decoder_class->start = GST_DEBUG_FUNCPTR (gst_va_av1_dec_start);
1057 av1decoder_class->new_sequence =
1058 GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_sequence);
1059 av1decoder_class->new_picture =
1060 GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_picture);
1061 av1decoder_class->duplicate_picture =
1062 GST_DEBUG_FUNCPTR (gst_va_av1_dec_duplicate_picture);
1063 av1decoder_class->start_picture =
1064 GST_DEBUG_FUNCPTR (gst_va_av1_dec_start_picture);
1065 av1decoder_class->decode_tile =
1066 GST_DEBUG_FUNCPTR (gst_va_av1_dec_decode_tile);
1067 av1decoder_class->end_picture =
1068 GST_DEBUG_FUNCPTR (gst_va_av1_dec_end_picture);
1069 av1decoder_class->output_picture =
1070 GST_DEBUG_FUNCPTR (gst_va_av1_dec_output_picture);
1073 g_free (cdata->description);
1074 g_free (cdata->render_device_path);
1075 gst_caps_unref (cdata->src_caps);
1076 gst_caps_unref (cdata->sink_caps);
1081 _register_debug_category (gpointer data)
1083 GST_DEBUG_CATEGORY_INIT (gst_va_av1dec_debug, "vaav1dec", 0,
1090 gst_va_av1_dec_register (GstPlugin * plugin, GstVaDevice * device,
1091 GstCaps * sink_caps, GstCaps * src_caps, guint rank)
1093 static GOnce debug_once = G_ONCE_INIT;
1095 GTypeInfo type_info = {
1096 .class_size = sizeof (GstVaAV1DecClass),
1097 .class_init = gst_va_av1_dec_class_init,
1098 .instance_size = sizeof (GstVaAV1Dec),
1099 .instance_init = gst_va_av1_dec_init,
1101 struct CData *cdata;
1103 gchar *type_name, *feature_name;
1105 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
1106 g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
1107 g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
1108 g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
1110 cdata = g_new (struct CData, 1);
1111 cdata->description = NULL;
1112 cdata->render_device_path = g_strdup (device->render_device_path);
1113 cdata->sink_caps = _complete_sink_caps (sink_caps);
1114 cdata->src_caps = gst_caps_ref (src_caps);
1116 /* class data will be leaked if the element never gets instantiated */
1117 GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
1118 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1119 GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1121 type_info.class_data = cdata;
1123 /* The first decoder to be registered should use a constant name,
1124 * like vaav1dec, for any additional decoders, we create unique
1125 * names, using inserting the render device name. */
1126 if (device->index == 0) {
1127 type_name = g_strdup ("GstVaAV1Dec");
1128 feature_name = g_strdup ("vaav1dec");
1130 gchar *basename = g_path_get_basename (device->render_device_path);
1131 type_name = g_strdup_printf ("GstVa%sAV1Dec", basename);
1132 feature_name = g_strdup_printf ("va%sav1dec", basename);
1133 cdata->description = basename;
1135 /* lower rank for non-first device */
1140 g_once (&debug_once, _register_debug_category, NULL);
1142 type = g_type_register_static (GST_TYPE_AV1_DECODER,
1143 type_name, &type_info, 0);
1145 ret = gst_element_register (plugin, feature_name, rank, type);
1148 g_free (feature_name);