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 GstAV1SequenceHeaderOBU seq;
75 GstVideoFormat preferred_format;
76 /* Used for layers not output. */
77 GstBufferPool *internal_pool;
80 static GstElementClass *parent_class = NULL;
83 static const gchar *src_caps_str =
84 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
85 "{ NV12, P010_10LE }") " ;"
86 GST_VIDEO_CAPS_MAKE ("{ NV12, P010_10LE }");
89 static const gchar *sink_caps_str = "video/x-av1";
92 gst_va_av1_dec_negotiate (GstVideoDecoder * decoder)
94 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
95 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
97 /* Ignore downstream renegotiation request. */
98 if (!base->need_negotiation)
101 base->need_negotiation = FALSE;
103 /* Do not re-create the context if only the frame size changes */
104 if (!gst_va_decoder_config_is_equal (base->decoder, base->profile,
105 base->rt_format, base->width, base->height)) {
106 if (gst_va_decoder_is_open (base->decoder)
107 && !gst_va_decoder_close (base->decoder))
110 if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
113 if (!gst_va_decoder_set_frame_size (base->decoder, base->width,
118 if (!gst_va_base_dec_set_output_state (base))
121 if (self->preferred_format != GST_VIDEO_FORMAT_UNKNOWN &&
122 self->preferred_format !=
123 GST_VIDEO_INFO_FORMAT (&base->output_state->info)) {
124 GST_WARNING_OBJECT (self, "The preferred_format is different from"
128 self->preferred_format = GST_VIDEO_INFO_FORMAT (&base->output_state->info);
130 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
134 _complete_sink_caps (GstCaps * sinkcaps)
136 GstCaps *caps = gst_caps_copy (sinkcaps);
137 GValue val = G_VALUE_INIT;
139 g_value_init (&val, G_TYPE_STRING);
140 g_value_set_string (&val, "frame");
141 gst_caps_set_value (caps, "alignment", &val);
142 g_value_unset (&val);
148 _get_profile (GstVaAV1Dec * self, const GstAV1SequenceHeaderOBU * seq_hdr)
150 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
151 VAProfile profile = VAProfileNone;
153 switch (seq_hdr->seq_profile) {
154 case GST_AV1_PROFILE_0:
155 profile = VAProfileAV1Profile0;
157 case GST_AV1_PROFILE_1:
158 profile = VAProfileAV1Profile1;
161 GST_ERROR_OBJECT (self, "Unsupported av1 profile value %d",
162 seq_hdr->seq_profile);
163 return VAProfileNone;
166 if (!gst_va_decoder_has_profile (base->decoder, profile)) {
167 GST_ERROR_OBJECT (self, "Profile %s is not supported by HW",
168 gst_va_profile_name (profile));
169 return VAProfileNone;
176 _get_rtformat (GstVaAV1Dec * self, VAProfile profile,
177 const GstAV1SequenceHeaderOBU * seq_header)
180 seq_profile Bit depth Monochrome support Chroma subsampling
181 0 8 or 10 Yes YUV 4:2:0
182 1 8 or 10 No YUV 4:4:4
183 2 8 or 10 Yes YUV 4:2:2
184 2 12 Yes YUV 4:2:0,YUV 4:2:2,YUV 4:4:4
187 /* TODO: consider Monochrome case. Just return 4:2:0 for Monochrome now. */
189 case VAProfileAV1Profile0:
190 if (seq_header->bit_depth == 8) {
191 return VA_RT_FORMAT_YUV420;
192 } else if (seq_header->bit_depth == 10) {
193 return VA_RT_FORMAT_YUV420_10;
196 case VAProfileAV1Profile1:
197 if (seq_header->bit_depth == 8) {
198 return VA_RT_FORMAT_YUV444;
199 } else if (seq_header->bit_depth == 10) {
200 return VA_RT_FORMAT_YUV444_10;
207 GST_ERROR_OBJECT (self, "Fail to find rtformat for profile:%s, bit_depth:%d",
208 gst_va_profile_name (profile), seq_header->bit_depth);
213 gst_va_av1_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
215 GstCaps *sinkcaps, *caps = NULL, *tmp;
216 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
219 caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
222 sinkcaps = _complete_sink_caps (caps);
223 gst_caps_unref (caps);
225 tmp = gst_caps_intersect_full (filter, sinkcaps,
226 GST_CAPS_INTERSECT_FIRST);
227 gst_caps_unref (sinkcaps);
232 GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
234 caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
241 _clear_internal_pool (GstVaAV1Dec * self)
243 if (self->internal_pool)
244 gst_buffer_pool_set_active (self->internal_pool, FALSE);
246 gst_clear_object (&self->internal_pool);
249 static GstBufferPool *
250 _create_internal_pool (GstVaAV1Dec * self, gint width, gint height)
252 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
254 GArray *surface_formats;
255 GstAllocator *allocator;
256 GstCaps *caps = NULL;
258 GstAllocationParams params = { 0, };
260 gst_allocation_params_init (¶ms);
262 /* We may come here before the negotiation, make sure all pools
263 use the same video format. */
264 if (self->preferred_format == GST_VIDEO_FORMAT_UNKNOWN) {
265 GstVideoFormat format;
267 gst_va_base_dec_get_preferred_format_and_caps_features (base,
269 if (format == GST_VIDEO_FORMAT_UNKNOWN) {
270 GST_WARNING_OBJECT (self, "Failed to get format for internal pool");
274 self->preferred_format = format;
277 gst_video_info_set_format (&info, self->preferred_format, width, height);
279 caps = gst_video_info_to_caps (&info);
281 GST_WARNING_OBJECT (self, "Failed to create caps for internal pool");
285 gst_caps_set_features_simple (caps,
286 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
288 surface_formats = gst_va_decoder_get_surface_formats (base->decoder);
289 allocator = gst_va_allocator_new (base->display, surface_formats);
291 pool = gst_va_pool_new_with_config (caps, GST_VIDEO_INFO_SIZE (&info),
292 1, 0, VA_SURFACE_ATTRIB_USAGE_HINT_DECODER, GST_VA_FEATURE_AUTO,
295 gst_clear_caps (&caps);
296 gst_object_unref (allocator);
299 GST_WARNING_OBJECT (self, "Failed to create internal pool");
303 gst_buffer_pool_set_active (pool, TRUE);
309 gst_va_av1_dec_new_sequence (GstAV1Decoder * decoder,
310 const GstAV1SequenceHeaderOBU * seq_hdr, gint max_dpb_size)
312 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
313 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
314 GstVideoInfo *info = &base->output_info;
319 GST_LOG_OBJECT (self, "new sequence");
321 profile = _get_profile (self, seq_hdr);
322 if (profile == VAProfileNone)
323 return GST_FLOW_NOT_NEGOTIATED;
325 rt_format = _get_rtformat (self, profile, seq_hdr);
327 return GST_FLOW_NOT_NEGOTIATED;
329 self->seq = *seq_hdr;
331 width = seq_hdr->max_frame_width_minus_1 + 1;
332 height = seq_hdr->max_frame_height_minus_1 + 1;
334 if (!gst_va_decoder_config_is_equal (base->decoder, profile,
335 rt_format, width, height)) {
336 _clear_internal_pool (self);
337 self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
339 base->profile = profile;
340 base->rt_format = rt_format;
341 GST_VIDEO_INFO_WIDTH (info) = base->width = width;
342 GST_VIDEO_INFO_HEIGHT (info) = base->height = height;
343 base->need_negotiation = TRUE;
344 base->min_buffers = 7 + 4; /* dpb size + scratch surfaces */
345 base->need_valign = FALSE;
348 g_clear_pointer (&base->input_state, gst_video_codec_state_unref);
349 base->input_state = gst_video_codec_state_ref (decoder->input_state);
354 static inline GstFlowReturn
355 _acquire_internal_buffer (GstVaAV1Dec * self, GstVideoCodecFrame * frame)
357 GstVaBaseDec *base = GST_VA_BASE_DEC (self);
360 if (!self->internal_pool) {
361 self->internal_pool =
362 _create_internal_pool (self, base->width, base->height);
363 if (!self->internal_pool)
364 return GST_FLOW_ERROR;
367 if (base->need_negotiation) {
368 if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self)))
369 return GST_FLOW_NOT_NEGOTIATED;
372 ret = gst_buffer_pool_acquire_buffer (self->internal_pool,
373 &frame->output_buffer, NULL);
374 if (ret != GST_FLOW_OK) {
375 GST_WARNING_OBJECT (self,
376 "Failed to allocated output buffer from internal pool, return %s",
377 gst_flow_get_name (ret));
384 gst_va_av1_dec_new_picture (GstAV1Decoder * decoder,
385 GstVideoCodecFrame * frame, GstAV1Picture * picture)
387 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
388 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
389 GstAV1FrameHeaderOBU *frame_hdr = &picture->frame_hdr;
390 GstVaDecodePicture *pic;
391 GstVideoInfo *info = &base->output_info;
394 /* Only output the highest spatial layer. For non output pictures,
395 we just use internal pool, then no negotiation needed. */
396 if (picture->spatial_id < decoder->highest_spatial_layer) {
397 ret = _acquire_internal_buffer (self, frame);
398 if (ret != GST_FLOW_OK)
401 if (frame_hdr->upscaled_width != GST_VIDEO_INFO_WIDTH (info)
402 || frame_hdr->frame_height != GST_VIDEO_INFO_HEIGHT (info)) {
403 GST_VIDEO_INFO_WIDTH (info) = frame_hdr->upscaled_width;
404 GST_VIDEO_INFO_HEIGHT (info) = frame_hdr->frame_height;
406 if (GST_VIDEO_INFO_WIDTH (info) < base->width
407 || GST_VIDEO_INFO_HEIGHT (info) < base->height) {
408 base->need_valign = TRUE;
410 base->valign = (GstVideoAlignment) {
411 .padding_bottom = base->height - GST_VIDEO_INFO_HEIGHT (info),
412 .padding_right = base->width - GST_VIDEO_INFO_WIDTH (info),
417 base->need_negotiation = TRUE;
420 ret = gst_va_base_dec_prepare_output_frame (base, frame);
421 if (ret != GST_FLOW_OK) {
422 GST_WARNING_OBJECT (self, "Failed to allocated output buffer, return %s",
423 gst_flow_get_name (ret));
428 if (picture->apply_grain) {
429 if (!gst_va_buffer_create_aux_surface (frame->output_buffer)) {
430 GST_WARNING_OBJECT (self,
431 "Failed to allocated aux surface for buffer %p",
432 frame->output_buffer);
433 return GST_FLOW_ERROR;
437 pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
439 gst_av1_picture_set_user_data (picture, pic,
440 (GDestroyNotify) gst_va_decode_picture_free);
442 if (picture->apply_grain) {
443 GST_LOG_OBJECT (self, "New va decode picture %p - %#x(aux: %#x)", pic,
444 gst_va_decode_picture_get_surface (pic),
445 gst_va_decode_picture_get_aux_surface (pic));
447 GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
448 gst_va_decode_picture_get_surface (pic));
454 static GstAV1Picture *
455 gst_va_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
456 GstVideoCodecFrame * frame, GstAV1Picture * picture)
458 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
459 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
460 GstVaDecodePicture *pic;
461 GstVaDecodePicture *new_pic;
462 GstAV1Picture *new_picture;
464 pic = gst_av1_picture_get_user_data (picture);
466 GST_ERROR_OBJECT (self, "Parent picture does not have a va picture");
470 new_picture = gst_av1_picture_new ();
471 g_assert (pic->gstbuffer);
472 new_pic = gst_va_decode_picture_new (base->decoder, pic->gstbuffer);
474 GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT
475 " (surface %#x)", pic, gst_va_decode_picture_get_surface (pic));
477 gst_av1_picture_set_user_data (new_picture, new_pic,
478 (GDestroyNotify) gst_va_decode_picture_free);
484 _setup_segment_info (VADecPictureParameterBufferAV1 * pic_param,
485 GstAV1FrameHeaderOBU * frame_header)
488 uint8_t feature_mask;
490 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++)
491 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++)
492 pic_param->seg_info.feature_data[i][j] =
493 frame_header->segmentation_params.feature_data[i][j];
495 for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
497 for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
498 if (frame_header->segmentation_params.feature_enabled[i][j])
499 feature_mask |= 1 << j;
501 pic_param->seg_info.feature_mask[i] = feature_mask;
506 _setup_film_grain_info (VADecPictureParameterBufferAV1 * pic_param,
507 GstAV1FrameHeaderOBU * frame_header)
511 if (!frame_header->film_grain_params.apply_grain)
514 pic_param->film_grain_info.num_y_points =
515 frame_header->film_grain_params.num_y_points;
516 for (i = 0; i < frame_header->film_grain_params.num_y_points; i++) {
517 pic_param->film_grain_info.point_y_value[i] =
518 frame_header->film_grain_params.point_y_value[i];
519 pic_param->film_grain_info.point_y_scaling[i] =
520 frame_header->film_grain_params.point_y_scaling[i];
523 pic_param->film_grain_info.num_cb_points =
524 frame_header->film_grain_params.num_cb_points;
525 for (i = 0; i < frame_header->film_grain_params.num_cb_points; i++) {
526 pic_param->film_grain_info.point_cb_value[i] =
527 frame_header->film_grain_params.point_cb_value[i];
528 pic_param->film_grain_info.point_cb_scaling[i] =
529 frame_header->film_grain_params.point_cb_scaling[i];
532 pic_param->film_grain_info.num_cr_points =
533 frame_header->film_grain_params.num_cr_points;
534 for (i = 0; i < frame_header->film_grain_params.num_cr_points; i++) {
535 pic_param->film_grain_info.point_cr_value[i] =
536 frame_header->film_grain_params.point_cr_value[i];
537 pic_param->film_grain_info.point_cr_scaling[i] =
538 frame_header->film_grain_params.point_cr_scaling[i];
542 if (pic_param->film_grain_info.num_y_points) {
543 for (i = 0; i < 24; i++) {
544 pic_param->film_grain_info.ar_coeffs_y[i] =
545 frame_header->film_grain_params.ar_coeffs_y_plus_128[i] - 128;
548 if (frame_header->film_grain_params.chroma_scaling_from_luma
549 || pic_param->film_grain_info.num_cb_points) {
550 for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
551 pic_param->film_grain_info.ar_coeffs_cb[i] =
552 frame_header->film_grain_params.ar_coeffs_cb_plus_128[i] - 128;
555 if (frame_header->film_grain_params.chroma_scaling_from_luma
556 || pic_param->film_grain_info.num_cr_points) {
557 for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
558 pic_param->film_grain_info.ar_coeffs_cr[i] =
559 frame_header->film_grain_params.ar_coeffs_cr_plus_128[i] - 128;
565 _setup_loop_filter_info (VADecPictureParameterBufferAV1 * pic_param,
566 GstAV1FrameHeaderOBU * frame_header)
570 pic_param->filter_level[0] =
571 frame_header->loop_filter_params.loop_filter_level[0];
572 pic_param->filter_level[1] =
573 frame_header->loop_filter_params.loop_filter_level[1];
574 pic_param->filter_level_u =
575 frame_header->loop_filter_params.loop_filter_level[2];
576 pic_param->filter_level_v =
577 frame_header->loop_filter_params.loop_filter_level[3];
579 for (i = 0; i < GST_AV1_TOTAL_REFS_PER_FRAME; i++)
580 pic_param->ref_deltas[i] =
581 frame_header->loop_filter_params.loop_filter_ref_deltas[i];
582 for (i = 0; i < 2; i++)
583 pic_param->mode_deltas[i] =
584 frame_header->loop_filter_params.loop_filter_mode_deltas[i];
588 _setup_quantization_info (VADecPictureParameterBufferAV1 * pic_param,
589 GstAV1FrameHeaderOBU * frame_header)
591 pic_param->qmatrix_fields.bits.using_qmatrix =
592 frame_header->quantization_params.using_qmatrix;
593 if (frame_header->quantization_params.using_qmatrix) {
594 pic_param->qmatrix_fields.bits.qm_y =
595 frame_header->quantization_params.qm_y;
596 pic_param->qmatrix_fields.bits.qm_u =
597 frame_header->quantization_params.qm_u;
598 pic_param->qmatrix_fields.bits.qm_v =
599 frame_header->quantization_params.qm_v;
601 pic_param->qmatrix_fields.bits.qm_y = 0;
602 pic_param->qmatrix_fields.bits.qm_u = 0;
603 pic_param->qmatrix_fields.bits.qm_v = 0;
608 _setup_cdef_info (VADecPictureParameterBufferAV1 * pic_param,
609 GstAV1FrameHeaderOBU * frame_header, guint8 num_planes)
614 pic_param->cdef_damping_minus_3 = frame_header->cdef_params.cdef_damping - 3;
615 pic_param->cdef_bits = frame_header->cdef_params.cdef_bits;
616 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
617 sec_strength = frame_header->cdef_params.cdef_y_sec_strength[i];
618 g_assert (sec_strength <= 4);
619 /* may need to minus 1 in order to merge with primary value. */
620 if (sec_strength == 4)
623 pic_param->cdef_y_strengths[i] =
624 ((frame_header->cdef_params.cdef_y_pri_strength[i] & 0xf) << 2) |
625 (sec_strength & 0x03);
627 if (num_planes > 1) {
628 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
629 sec_strength = frame_header->cdef_params.cdef_uv_sec_strength[i];
630 g_assert (sec_strength <= 4);
631 /* may need to minus 1 in order to merge with primary value. */
632 if (sec_strength == 4)
635 pic_param->cdef_uv_strengths[i] =
636 ((frame_header->cdef_params.cdef_uv_pri_strength[i] & 0xf) << 2) |
637 (sec_strength & 0x03);
640 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
641 pic_param->cdef_uv_strengths[i] = 0;
647 _setup_global_motion_info (VADecPictureParameterBufferAV1 * pic_param,
648 GstAV1FrameHeaderOBU * frame_header)
652 for (i = 0; i < 7; i++) {
653 /* assuming VAAV1TransformationType and GstAV1WarpModelType are
655 pic_param->wm[i].wmtype = (VAAV1TransformationType)
656 frame_header->global_motion_params.gm_type[GST_AV1_REF_LAST_FRAME + i];
658 for (j = 0; j < 6; j++)
659 pic_param->wm[i].wmmat[j] =
660 frame_header->global_motion_params.gm_params
661 [GST_AV1_REF_LAST_FRAME + i][j];
663 pic_param->wm[i].wmmat[6] = 0;
664 pic_param->wm[i].wmmat[7] = 0;
666 pic_param->wm[i].invalid =
667 frame_header->global_motion_params.invalid[GST_AV1_REF_LAST_FRAME + i];
672 gst_va_av1_dec_start_picture (GstAV1Decoder * decoder, GstAV1Picture * picture,
675 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
676 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
677 GstAV1FrameHeaderOBU *frame_header = &picture->frame_hdr;
678 GstAV1SequenceHeaderOBU *seq_header = &self->seq;
679 VADecPictureParameterBufferAV1 pic_param = { };
680 GstVaDecodePicture *va_pic;
683 va_pic = gst_av1_picture_get_user_data (picture);
687 pic_param = (VADecPictureParameterBufferAV1){
688 .profile = seq_header->seq_profile,
689 .order_hint_bits_minus_1 = seq_header->order_hint_bits_minus_1,
690 .matrix_coefficients = seq_header->color_config.matrix_coefficients,
691 .seq_info_fields.fields = {
692 .still_picture = seq_header->still_picture,
693 .use_128x128_superblock = seq_header->use_128x128_superblock,
694 .enable_filter_intra = seq_header->enable_filter_intra,
695 .enable_intra_edge_filter = seq_header->enable_intra_edge_filter,
696 .enable_interintra_compound = seq_header->enable_interintra_compound,
697 .enable_masked_compound = seq_header->enable_masked_compound,
698 .enable_dual_filter = seq_header->enable_dual_filter,
699 .enable_order_hint = seq_header->enable_order_hint,
700 .enable_jnt_comp = seq_header->enable_jnt_comp,
701 .enable_cdef = seq_header->enable_cdef,
702 .mono_chrome = seq_header->color_config.mono_chrome,
703 .color_range = seq_header->color_config.color_range,
704 .subsampling_x = seq_header->color_config.subsampling_x,
705 .subsampling_y = seq_header->color_config.subsampling_y,
706 .film_grain_params_present = seq_header->film_grain_params_present,
708 .anchor_frames_num = 0,
709 .anchor_frames_list = NULL,
710 .frame_width_minus1 = frame_header->upscaled_width - 1,
711 .frame_height_minus1 = frame_header->frame_height - 1,
712 .output_frame_width_in_tiles_minus_1 = 0,
713 .output_frame_height_in_tiles_minus_1 = 0,
714 .order_hint = frame_header->order_hint,
716 .seg_info.segment_info_fields.bits = {
717 .enabled = frame_header->segmentation_params.segmentation_enabled,
718 .update_map = frame_header->segmentation_params.segmentation_update_map,
720 frame_header->segmentation_params.segmentation_temporal_update,
722 frame_header->segmentation_params.segmentation_update_data,
726 .film_grain_info_fields.bits = {
727 .apply_grain = frame_header->film_grain_params.apply_grain,
728 .chroma_scaling_from_luma =
729 frame_header->film_grain_params.chroma_scaling_from_luma,
730 .grain_scaling_minus_8 =
731 frame_header->film_grain_params.grain_scaling_minus_8,
732 .ar_coeff_lag = frame_header->film_grain_params.ar_coeff_lag,
733 .ar_coeff_shift_minus_6 =
734 frame_header->film_grain_params.ar_coeff_shift_minus_6,
735 .grain_scale_shift = frame_header->film_grain_params.grain_scale_shift,
736 .overlap_flag = frame_header->film_grain_params.overlap_flag,
737 .clip_to_restricted_range =
738 frame_header->film_grain_params.clip_to_restricted_range,
740 .grain_seed = frame_header->film_grain_params.grain_seed,
741 .cb_mult = frame_header->film_grain_params.cb_mult,
742 .cb_luma_mult = frame_header->film_grain_params.cb_luma_mult,
743 .cb_offset = frame_header->film_grain_params.cb_offset,
744 .cr_mult = frame_header->film_grain_params.cr_mult,
745 .cr_luma_mult = frame_header->film_grain_params.cr_luma_mult,
746 .cr_offset = frame_header->film_grain_params.cr_offset,
748 .tile_cols = frame_header->tile_info.tile_cols,
749 .tile_rows = frame_header->tile_info.tile_rows,
750 .context_update_tile_id = frame_header->tile_info.context_update_tile_id,
751 .pic_info_fields.bits = {
752 .frame_type = frame_header->frame_type,
753 .show_frame = frame_header->show_frame,
754 .showable_frame = frame_header->showable_frame,
755 .error_resilient_mode = frame_header->error_resilient_mode,
756 .disable_cdf_update = frame_header->disable_cdf_update,
757 .allow_screen_content_tools = frame_header->allow_screen_content_tools,
758 .force_integer_mv = frame_header->force_integer_mv,
759 .allow_intrabc = frame_header->allow_intrabc,
760 .use_superres = frame_header->use_superres,
761 .allow_high_precision_mv = frame_header->allow_high_precision_mv,
762 .is_motion_mode_switchable = frame_header->is_motion_mode_switchable,
763 .use_ref_frame_mvs = frame_header->use_ref_frame_mvs,
764 .disable_frame_end_update_cdf =
765 frame_header->disable_frame_end_update_cdf,
766 .uniform_tile_spacing_flag =
767 frame_header->tile_info.uniform_tile_spacing_flag,
768 .allow_warped_motion = frame_header->allow_warped_motion,
770 .superres_scale_denominator = frame_header->superres_denom,
771 .interp_filter = frame_header->interpolation_filter,
773 .loop_filter_info_fields.bits = {
775 frame_header->loop_filter_params.loop_filter_sharpness,
776 .mode_ref_delta_enabled =
777 frame_header->loop_filter_params.loop_filter_delta_enabled,
778 .mode_ref_delta_update =
779 frame_header->loop_filter_params.loop_filter_delta_update,
781 .mode_control_fields.bits = {
782 .delta_lf_present_flag =
783 frame_header->loop_filter_params.delta_lf_present,
784 .log2_delta_lf_res = frame_header->loop_filter_params.delta_lf_res,
785 .delta_lf_multi = frame_header->loop_filter_params.delta_lf_multi,
786 .delta_q_present_flag =
787 frame_header->quantization_params.delta_q_present,
788 .log2_delta_q_res = frame_header->quantization_params.delta_q_res,
789 .tx_mode = frame_header->tx_mode,
790 .reference_select = frame_header->reference_select,
791 .reduced_tx_set_used = frame_header->reduced_tx_set,
792 .skip_mode_present = frame_header->skip_mode_present,
795 .base_qindex = frame_header->quantization_params.base_q_idx,
796 .y_dc_delta_q = frame_header->quantization_params.delta_q_y_dc,
797 .u_dc_delta_q = frame_header->quantization_params.delta_q_u_dc,
798 .u_ac_delta_q = frame_header->quantization_params.delta_q_u_ac,
799 .v_dc_delta_q = frame_header->quantization_params.delta_q_v_dc,
800 .v_ac_delta_q = frame_header->quantization_params.delta_q_v_ac,
801 /* loop restoration */
802 .loop_restoration_fields.bits = {
803 .yframe_restoration_type =
804 frame_header->loop_restoration_params.frame_restoration_type[0],
805 .cbframe_restoration_type =
806 frame_header->loop_restoration_params.frame_restoration_type[1],
807 .crframe_restoration_type =
808 frame_header->loop_restoration_params.frame_restoration_type[2],
809 .lr_unit_shift = frame_header->loop_restoration_params.lr_unit_shift,
810 .lr_uv_shift = frame_header->loop_restoration_params.lr_uv_shift,
815 if (seq_header->bit_depth == 8) {
816 pic_param.bit_depth_idx = 0;
817 } else if (seq_header->bit_depth == 10) {
818 pic_param.bit_depth_idx = 1;
819 } else if (seq_header->bit_depth == 12) {
820 pic_param.bit_depth_idx = 2;
822 g_assert_not_reached ();
825 if (frame_header->film_grain_params.apply_grain) {
826 pic_param.current_frame = gst_va_decode_picture_get_aux_surface (va_pic);
827 pic_param.current_display_picture =
828 gst_va_decode_picture_get_surface (va_pic);
830 pic_param.current_frame = gst_va_decode_picture_get_surface (va_pic);
831 pic_param.current_display_picture = VA_INVALID_SURFACE;
834 for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
835 if (dpb->pic_list[i]) {
836 if (dpb->pic_list[i]->apply_grain) {
837 pic_param.ref_frame_map[i] = gst_va_decode_picture_get_aux_surface
838 (gst_av1_picture_get_user_data (dpb->pic_list[i]));
840 pic_param.ref_frame_map[i] = gst_va_decode_picture_get_surface
841 (gst_av1_picture_get_user_data (dpb->pic_list[i]));
844 pic_param.ref_frame_map[i] = VA_INVALID_SURFACE;
847 for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
848 pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i];
850 pic_param.primary_ref_frame = frame_header->primary_ref_frame;
852 _setup_segment_info (&pic_param, frame_header);
853 _setup_film_grain_info (&pic_param, frame_header);
855 for (i = 0; i < 63; i++) {
856 pic_param.width_in_sbs_minus_1[i] =
857 frame_header->tile_info.width_in_sbs_minus_1[i];
858 pic_param.height_in_sbs_minus_1[i] =
859 frame_header->tile_info.height_in_sbs_minus_1[i];
862 _setup_loop_filter_info (&pic_param, frame_header);
863 _setup_quantization_info (&pic_param, frame_header);
864 _setup_cdef_info (&pic_param, frame_header, seq_header->num_planes);
865 _setup_global_motion_info (&pic_param, frame_header);
867 if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
868 VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
869 return GST_FLOW_ERROR;
875 gst_va_av1_dec_decode_tile (GstAV1Decoder * decoder, GstAV1Picture * picture,
878 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
879 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
880 GstAV1TileGroupOBU *tile_group = &tile->tile_group;
881 GstVaDecodePicture *va_pic;
883 VASliceParameterBufferAV1 slice_param[GST_AV1_MAX_TILE_COUNT];
885 GST_TRACE_OBJECT (self, "-");
887 for (i = 0; i < tile_group->tg_end - tile_group->tg_start + 1; i++) {
888 slice_param[i] = (VASliceParameterBufferAV1) {
890 slice_param[i].slice_data_size =
891 tile_group->entry[tile_group->tg_start + i].tile_size;
892 slice_param[i].slice_data_offset =
893 tile_group->entry[tile_group->tg_start + i].tile_offset;
894 slice_param[i].tile_row =
895 tile_group->entry[tile_group->tg_start + i].tile_row;
896 slice_param[i].tile_column =
897 tile_group->entry[tile_group->tg_start + i].tile_col;
898 slice_param[i].slice_data_flag = 0;
901 va_pic = gst_av1_picture_get_user_data (picture);
903 if (!gst_va_decoder_add_slice_buffer_with_n_params (base->decoder, va_pic,
904 slice_param, sizeof (VASliceParameterBufferAV1), i, tile->obu.data,
905 tile->obu.obu_size)) {
906 return GST_FLOW_ERROR;
913 gst_va_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
915 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
916 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
917 GstVaDecodePicture *va_pic;
919 GST_LOG_OBJECT (self, "end picture %p, (system_frame_number %d)",
920 picture, picture->system_frame_number);
922 va_pic = gst_av1_picture_get_user_data (picture);
924 if (!gst_va_decoder_decode_with_aux_surface (base->decoder, va_pic,
925 picture->apply_grain)) {
926 return GST_FLOW_ERROR;
933 gst_va_av1_dec_output_picture (GstAV1Decoder * decoder,
934 GstVideoCodecFrame * frame, GstAV1Picture * picture)
936 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
937 GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
938 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
941 g_assert (picture->frame_hdr.show_frame ||
942 picture->frame_hdr.show_existing_frame);
944 GST_LOG_OBJECT (self,
945 "Outputting picture %p (system_frame_number %d)",
946 picture, picture->system_frame_number);
948 if (picture->frame_hdr.show_existing_frame) {
949 GstVaDecodePicture *pic;
951 g_assert (!frame->output_buffer);
952 pic = gst_av1_picture_get_user_data (picture);
953 frame->output_buffer = gst_buffer_ref (pic->gstbuffer);
956 ret = gst_va_base_dec_process_output (base, frame, picture->discont_state, 0);
957 gst_av1_picture_unref (picture);
960 return gst_video_decoder_finish_frame (vdec, frame);
961 return GST_FLOW_ERROR;
965 gst_va_av1_dec_start (GstVideoDecoder * decoder)
967 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
969 self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
971 return GST_VIDEO_DECODER_CLASS (parent_class)->start (decoder);
975 gst_va_av1_dec_close (GstVideoDecoder * decoder)
977 GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
979 _clear_internal_pool (self);
981 return gst_va_base_dec_close (GST_VIDEO_DECODER (decoder));
985 gst_va_av1_dec_init (GTypeInstance * instance, gpointer g_class)
987 gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
991 gst_va_av1_dec_dispose (GObject * object)
993 gst_va_base_dec_close (GST_VIDEO_DECODER (object));
994 G_OBJECT_CLASS (parent_class)->dispose (object);
998 gst_va_av1_dec_class_init (gpointer g_class, gpointer class_data)
1000 GstCaps *src_doc_caps, *sink_doc_caps;
1001 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
1002 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1003 GstAV1DecoderClass *av1decoder_class = GST_AV1_DECODER_CLASS (g_class);
1004 GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
1005 struct CData *cdata = class_data;
1008 if (cdata->description) {
1009 long_name = g_strdup_printf ("VA-API AV1 Decoder in %s",
1010 cdata->description);
1012 long_name = g_strdup ("VA-API AV1 Decoder");
1015 gst_element_class_set_metadata (element_class, long_name,
1016 "Codec/Decoder/Video/Hardware",
1017 "VA-API based AV1 video decoder", "He Junyan <junyan.he@intel.com>");
1019 sink_doc_caps = gst_caps_from_string (sink_caps_str);
1020 src_doc_caps = gst_caps_from_string (src_caps_str);
1022 parent_class = g_type_class_peek_parent (g_class);
1025 * GstVaAV1Dec:device-path:
1027 * It shows the DRM device path used for the VA operation, if any.
1031 gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), AV1,
1032 cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
1033 src_doc_caps, sink_doc_caps);
1035 gobject_class->dispose = gst_va_av1_dec_dispose;
1037 decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_av1_dec_getcaps);
1038 decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_av1_dec_negotiate);
1039 decoder_class->close = GST_DEBUG_FUNCPTR (gst_va_av1_dec_close);
1040 decoder_class->start = GST_DEBUG_FUNCPTR (gst_va_av1_dec_start);
1042 av1decoder_class->new_sequence =
1043 GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_sequence);
1044 av1decoder_class->new_picture =
1045 GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_picture);
1046 av1decoder_class->duplicate_picture =
1047 GST_DEBUG_FUNCPTR (gst_va_av1_dec_duplicate_picture);
1048 av1decoder_class->start_picture =
1049 GST_DEBUG_FUNCPTR (gst_va_av1_dec_start_picture);
1050 av1decoder_class->decode_tile =
1051 GST_DEBUG_FUNCPTR (gst_va_av1_dec_decode_tile);
1052 av1decoder_class->end_picture =
1053 GST_DEBUG_FUNCPTR (gst_va_av1_dec_end_picture);
1054 av1decoder_class->output_picture =
1055 GST_DEBUG_FUNCPTR (gst_va_av1_dec_output_picture);
1058 g_free (cdata->description);
1059 g_free (cdata->render_device_path);
1060 gst_caps_unref (cdata->src_caps);
1061 gst_caps_unref (cdata->sink_caps);
1066 _register_debug_category (gpointer data)
1068 GST_DEBUG_CATEGORY_INIT (gst_va_av1dec_debug, "vaav1dec", 0,
1075 gst_va_av1_dec_register (GstPlugin * plugin, GstVaDevice * device,
1076 GstCaps * sink_caps, GstCaps * src_caps, guint rank)
1078 static GOnce debug_once = G_ONCE_INIT;
1080 GTypeInfo type_info = {
1081 .class_size = sizeof (GstVaAV1DecClass),
1082 .class_init = gst_va_av1_dec_class_init,
1083 .instance_size = sizeof (GstVaAV1Dec),
1084 .instance_init = gst_va_av1_dec_init,
1086 struct CData *cdata;
1088 gchar *type_name, *feature_name;
1090 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
1091 g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
1092 g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
1093 g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
1095 cdata = g_new (struct CData, 1);
1096 cdata->description = NULL;
1097 cdata->render_device_path = g_strdup (device->render_device_path);
1098 cdata->sink_caps = _complete_sink_caps (sink_caps);
1099 cdata->src_caps = gst_caps_ref (src_caps);
1101 /* class data will be leaked if the element never gets instantiated */
1102 GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
1103 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1104 GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1106 type_info.class_data = cdata;
1108 /* The first decoder to be registered should use a constant name,
1109 * like vaav1dec, for any additional decoders, we create unique
1110 * names, using inserting the render device name. */
1111 if (device->index == 0) {
1112 type_name = g_strdup ("GstVaAV1Dec");
1113 feature_name = g_strdup ("vaav1dec");
1115 gchar *basename = g_path_get_basename (device->render_device_path);
1116 type_name = g_strdup_printf ("GstVa%sAV1Dec", basename);
1117 feature_name = g_strdup_printf ("va%sav1dec", basename);
1118 cdata->description = basename;
1120 /* lower rank for non-first device */
1125 g_once (&debug_once, _register_debug_category, NULL);
1127 type = g_type_register_static (GST_TYPE_AV1_DECODER,
1128 type_name, &type_info, 0);
1130 ret = gst_element_register (plugin, feature_name, rank, type);
1133 g_free (feature_name);