vavp9dec: Use gst_va_base_dec_set_output_state().
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / va / gstvaav1dec.c
1 /* GStreamer
2  *  Copyright (C) 2020 Intel Corporation
3  *     Author: He Junyan <junyan.he@intel.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 the0
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:element-vaav1dec
23  * @title: vaav1dec
24  * @short_description: A VA-API based AV1 video decoder
25  *
26  * vaav1dec decodes AV1 bitstreams to VA surfaces using the
27  * installed and chosen [VA-API](https://01.org/linuxmedia/vaapi)
28  * driver.
29  *
30  * The decoding surfaces can be mapped onto main memory as video
31  * frames.
32  *
33  * ## Example launch line
34  * ```
35  * gst-launch-1.0 filesrc location=sample.av1 ! ivfparse ! av1parse ! vaav1dec ! autovideosink
36  * ```
37  *
38  * Since: 1.20
39  *
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <gst/va/gstva.h>
47
48 #include "gstvaav1dec.h"
49 #include "gstvabasedec.h"
50
51 GST_DEBUG_CATEGORY_STATIC (gst_va_av1dec_debug);
52 #ifndef GST_DISABLE_GST_DEBUG
53 #define GST_CAT_DEFAULT gst_va_av1dec_debug
54 #else
55 #define GST_CAT_DEFAULT NULL
56 #endif
57
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)
61
62 typedef struct _GstVaAV1Dec GstVaAV1Dec;
63 typedef struct _GstVaAV1DecClass GstVaAV1DecClass;
64
65 struct _GstVaAV1DecClass
66 {
67   GstVaBaseDecClass parent_class;
68 };
69
70 struct _GstVaAV1Dec
71 {
72   GstVaBaseDec parent;
73
74   GstAV1SequenceHeaderOBU seq;
75   gint max_width;
76   gint max_height;
77   GstVideoFormat preferred_format;
78   /* Used for layers not output. */
79   GstBufferPool *internal_pool;
80 };
81
82 static GstElementClass *parent_class = NULL;
83
84 /* *INDENT-OFF* */
85 static const gchar *src_caps_str =
86     GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_VA,
87         "{ NV12, P010_10LE }") " ;"
88     GST_VIDEO_CAPS_MAKE ("{ NV12, P010_10LE }");
89 /* *INDENT-ON* */
90
91 static const gchar *sink_caps_str = "video/x-av1";
92
93 static gboolean
94 gst_va_av1_dec_negotiate (GstVideoDecoder * decoder)
95 {
96   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
97   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
98   GstAV1Decoder *av1dec = GST_AV1_DECODER (decoder);
99   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
100   GstCapsFeatures *capsfeatures = NULL;
101
102   /* Ignore downstream renegotiation request. */
103   if (!base->need_negotiation)
104     return TRUE;
105
106   base->need_negotiation = FALSE;
107
108   /* Do not re-create the context if only the frame size changes */
109   if (!gst_va_decoder_config_is_equal (base->decoder, base->profile,
110           base->rt_format, self->max_width, self->max_height)) {
111     if (gst_va_decoder_is_open (base->decoder)
112         && !gst_va_decoder_close (base->decoder))
113       return FALSE;
114
115     if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
116       return FALSE;
117
118     if (!gst_va_decoder_set_frame_size (base->decoder, self->max_width,
119             self->max_height))
120       return FALSE;
121   }
122
123   if (base->output_state)
124     gst_video_codec_state_unref (base->output_state);
125
126   gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
127       &capsfeatures);
128   if (format == GST_VIDEO_FORMAT_UNKNOWN)
129     return FALSE;
130
131   if (self->preferred_format != GST_VIDEO_FORMAT_UNKNOWN &&
132       self->preferred_format != format) {
133     GST_WARNING_OBJECT (self, "The preferred_format is different from"
134         " the last result");
135     return FALSE;
136   }
137   self->preferred_format = format;
138
139   base->output_state = gst_video_decoder_set_output_state (decoder, format,
140       base->width, base->height, av1dec->input_state);
141
142   base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
143   if (capsfeatures)
144     gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
145
146   GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
147       base->output_state->caps);
148
149   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
150 }
151
152 static GstCaps *
153 _complete_sink_caps (GstCaps * sinkcaps)
154 {
155   GstCaps *caps = gst_caps_copy (sinkcaps);
156   GValue val = G_VALUE_INIT;
157
158   g_value_init (&val, G_TYPE_STRING);
159   g_value_set_string (&val, "frame");
160   gst_caps_set_value (caps, "alignment", &val);
161   g_value_unset (&val);
162
163   return caps;
164 }
165
166 static VAProfile
167 _get_profile (GstVaAV1Dec * self, const GstAV1SequenceHeaderOBU * seq_hdr)
168 {
169   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
170   VAProfile profile = VAProfileNone;
171
172   switch (seq_hdr->seq_profile) {
173     case GST_AV1_PROFILE_0:
174       profile = VAProfileAV1Profile0;
175       break;
176     case GST_AV1_PROFILE_1:
177       profile = VAProfileAV1Profile1;
178       break;
179     default:
180       GST_ERROR_OBJECT (self, "Unsupported av1 profile value %d",
181           seq_hdr->seq_profile);
182       return VAProfileNone;
183   }
184
185   if (!gst_va_decoder_has_profile (base->decoder, profile)) {
186     GST_ERROR_OBJECT (self, "Profile %s is not supported by HW",
187         gst_va_profile_name (profile));
188     return VAProfileNone;
189   }
190
191   return profile;
192 }
193
194 static guint
195 _get_rtformat (GstVaAV1Dec * self, VAProfile profile,
196     const GstAV1SequenceHeaderOBU * seq_header)
197 {
198   /* 6.4.1:
199      seq_profile  Bit depth  Monochrome support  Chroma subsampling
200      0            8 or 10    Yes                 YUV 4:2:0
201      1            8 or 10    No                  YUV 4:4:4
202      2            8 or 10    Yes                 YUV 4:2:2
203      2            12         Yes                 YUV 4:2:0,YUV 4:2:2,YUV 4:4:4
204    */
205
206   /* TODO: consider Monochrome case. Just return 4:2:0 for Monochrome now. */
207   switch (profile) {
208     case VAProfileAV1Profile0:
209       if (seq_header->bit_depth == 8) {
210         return VA_RT_FORMAT_YUV420;
211       } else if (seq_header->bit_depth == 10) {
212         return VA_RT_FORMAT_YUV420_10;
213       }
214       break;
215     case VAProfileAV1Profile1:
216       if (seq_header->bit_depth == 8) {
217         return VA_RT_FORMAT_YUV444;
218       } else if (seq_header->bit_depth == 10) {
219         return VA_RT_FORMAT_YUV444_10;
220       }
221       break;
222     default:
223       break;
224   }
225
226   GST_ERROR_OBJECT (self, "Fail to find rtformat for profile:%s, bit_depth:%d",
227       gst_va_profile_name (profile), seq_header->bit_depth);
228   return 0;
229 }
230
231 static GstCaps *
232 gst_va_av1_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
233 {
234   GstCaps *sinkcaps, *caps = NULL, *tmp;
235   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
236
237   if (base->decoder)
238     caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
239
240   if (caps) {
241     sinkcaps = _complete_sink_caps (caps);
242     gst_caps_unref (caps);
243     if (filter) {
244       tmp = gst_caps_intersect_full (filter, sinkcaps,
245           GST_CAPS_INTERSECT_FIRST);
246       gst_caps_unref (sinkcaps);
247       caps = tmp;
248     } else {
249       caps = sinkcaps;
250     }
251     GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
252   } else if (!caps) {
253     caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
254   }
255
256   return caps;
257 }
258
259 static void
260 _clear_internal_pool (GstVaAV1Dec * self)
261 {
262   if (self->internal_pool)
263     gst_buffer_pool_set_active (self->internal_pool, FALSE);
264
265   gst_clear_object (&self->internal_pool);
266 }
267
268 static GstBufferPool *
269 _create_internal_pool (GstVaAV1Dec * self, gint width, gint height)
270 {
271   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
272   GstVideoInfo info;
273   GArray *surface_formats;
274   GstAllocator *allocator;
275   GstCaps *caps = NULL;
276   GstBufferPool *pool;
277   GstAllocationParams params = { 0, };
278
279   gst_allocation_params_init (&params);
280
281   /* We may come here before the negotiation, make sure all pools
282      use the same video format. */
283   if (self->preferred_format == GST_VIDEO_FORMAT_UNKNOWN) {
284     GstVideoFormat format;
285
286     gst_va_base_dec_get_preferred_format_and_caps_features (base,
287         &format, NULL);
288     if (format == GST_VIDEO_FORMAT_UNKNOWN) {
289       GST_WARNING_OBJECT (self, "Failed to get format for internal pool");
290       return NULL;
291     }
292
293     self->preferred_format = format;
294   }
295
296   gst_video_info_set_format (&info, self->preferred_format, width, height);
297
298   caps = gst_video_info_to_caps (&info);
299   if (caps == NULL) {
300     GST_WARNING_OBJECT (self, "Failed to create caps for internal pool");
301     return NULL;
302   }
303
304   gst_caps_set_features_simple (caps,
305       gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
306
307   surface_formats = gst_va_decoder_get_surface_formats (base->decoder);
308   allocator = gst_va_allocator_new (base->display, surface_formats);
309
310   pool = gst_va_pool_new_with_config (caps, GST_VIDEO_INFO_SIZE (&info),
311       1, 0, VA_SURFACE_ATTRIB_USAGE_HINT_DECODER, GST_VA_FEATURE_AUTO,
312       allocator, &params);
313   gst_clear_caps (&caps);
314
315   if (!pool) {
316     GST_WARNING_OBJECT (self, "Failed to create internal pool");
317     gst_object_unref (allocator);
318     return NULL;
319   }
320
321   gst_object_unref (allocator);
322
323   gst_buffer_pool_set_active (pool, TRUE);
324
325   return pool;
326 }
327
328 static GstFlowReturn
329 gst_va_av1_dec_new_sequence (GstAV1Decoder * decoder,
330     const GstAV1SequenceHeaderOBU * seq_hdr, gint max_dpb_size)
331 {
332   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
333   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
334   VAProfile profile;
335   guint rt_format;
336
337   GST_LOG_OBJECT (self, "new sequence");
338
339   profile = _get_profile (self, seq_hdr);
340   if (profile == VAProfileNone)
341     return GST_FLOW_NOT_NEGOTIATED;
342
343   rt_format = _get_rtformat (self, profile, seq_hdr);
344   if (!rt_format)
345     return GST_FLOW_NOT_NEGOTIATED;
346
347   self->seq = *seq_hdr;
348
349   if (!gst_va_decoder_config_is_equal (base->decoder, profile,
350           rt_format, seq_hdr->max_frame_width_minus_1 + 1,
351           seq_hdr->max_frame_height_minus_1 + 1)) {
352     _clear_internal_pool (self);
353     self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
354
355     base->profile = profile;
356     base->rt_format = rt_format;
357     self->max_width = seq_hdr->max_frame_width_minus_1 + 1;
358     self->max_height = seq_hdr->max_frame_height_minus_1 + 1;
359     base->need_negotiation = TRUE;
360
361     base->min_buffers = 7 + 4;  /* dpb size + scratch surfaces */
362
363     /* May be changed by frame header */
364     base->width = self->max_width;
365     base->height = self->max_height;
366     base->need_valign = FALSE;
367   }
368
369   return GST_FLOW_OK;
370 }
371
372 static inline GstFlowReturn
373 _acquire_internal_buffer (GstVaAV1Dec * self, GstVideoCodecFrame * frame)
374 {
375   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
376   GstFlowReturn ret;
377
378   if (!self->internal_pool) {
379     self->internal_pool =
380         _create_internal_pool (self, self->max_width, self->max_height);
381     if (!self->internal_pool)
382       return GST_FLOW_ERROR;
383   }
384
385   if (base->need_negotiation) {
386     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self)))
387       return GST_FLOW_NOT_NEGOTIATED;
388   }
389
390   ret = gst_buffer_pool_acquire_buffer (self->internal_pool,
391       &frame->output_buffer, NULL);
392   if (ret != GST_FLOW_OK) {
393     GST_WARNING_OBJECT (self,
394         "Failed to allocated output buffer from internal pool, return %s",
395         gst_flow_get_name (ret));
396   }
397
398   return ret;
399 }
400
401 static GstFlowReturn
402 gst_va_av1_dec_new_picture (GstAV1Decoder * decoder,
403     GstVideoCodecFrame * frame, GstAV1Picture * picture)
404 {
405   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
406   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
407   GstVaDecodePicture *pic;
408   GstAV1FrameHeaderOBU *frame_hdr = &picture->frame_hdr;
409   GstFlowReturn ret;
410
411   /* Only output the highest spatial layer. For non output pictures,
412      we just use internal pool, then no negotiation needed. */
413   if (picture->spatial_id < decoder->highest_spatial_layer) {
414     ret = _acquire_internal_buffer (self, frame);
415     if (ret != GST_FLOW_OK)
416       return ret;
417   } else {
418     if (frame_hdr->upscaled_width != base->width
419         || frame_hdr->frame_height != base->height) {
420       base->width = frame_hdr->upscaled_width;
421       base->height = frame_hdr->frame_height;
422
423       if (base->width < self->max_width || base->height < self->max_height) {
424         base->need_valign = TRUE;
425         /* *INDENT-OFF* */
426         base->valign = (GstVideoAlignment){
427           .padding_bottom = self->max_height - base->height,
428           .padding_right = self->max_width - base->width,
429         };
430         /* *INDENT-ON* */
431       }
432
433       base->need_negotiation = TRUE;
434     }
435
436     ret = gst_va_base_dec_prepare_output_frame (base, frame);
437     if (ret != GST_FLOW_OK) {
438       GST_WARNING_OBJECT (self, "Failed to allocated output buffer, return %s",
439           gst_flow_get_name (ret));
440       return ret;
441     }
442   }
443
444   if (picture->apply_grain) {
445     if (!gst_va_buffer_create_aux_surface (frame->output_buffer)) {
446       GST_WARNING_OBJECT (self,
447           "Failed to allocated aux surface for buffer %p",
448           frame->output_buffer);
449       return GST_FLOW_ERROR;
450     }
451   }
452
453   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
454
455   gst_av1_picture_set_user_data (picture, pic,
456       (GDestroyNotify) gst_va_decode_picture_free);
457
458   if (picture->apply_grain) {
459     GST_LOG_OBJECT (self, "New va decode picture %p - %#x(aux: %#x)", pic,
460         gst_va_decode_picture_get_surface (pic),
461         gst_va_decode_picture_get_aux_surface (pic));
462   } else {
463     GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
464         gst_va_decode_picture_get_surface (pic));
465   }
466
467   return GST_FLOW_OK;
468 }
469
470 static GstAV1Picture *
471 gst_va_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
472     GstVideoCodecFrame * frame, GstAV1Picture * picture)
473 {
474   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
475   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
476   GstVaDecodePicture *pic;
477   GstVaDecodePicture *new_pic;
478   GstAV1Picture *new_picture;
479
480   pic = gst_av1_picture_get_user_data (picture);
481   if (!pic) {
482     GST_ERROR_OBJECT (self, "Parent picture does not have a va picture");
483     return NULL;
484   }
485
486   new_picture = gst_av1_picture_new ();
487   g_assert (pic->gstbuffer);
488   new_pic = gst_va_decode_picture_new (base->decoder, pic->gstbuffer);
489
490   GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT
491       " (surface %#x)", pic, gst_va_decode_picture_get_surface (pic));
492
493   gst_av1_picture_set_user_data (new_picture, new_pic,
494       (GDestroyNotify) gst_va_decode_picture_free);
495
496   return new_picture;
497 }
498
499 static void
500 _setup_segment_info (VADecPictureParameterBufferAV1 * pic_param,
501     GstAV1FrameHeaderOBU * frame_header)
502 {
503   guint i, j;
504   uint8_t feature_mask;
505
506   for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++)
507     for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++)
508       pic_param->seg_info.feature_data[i][j] =
509           frame_header->segmentation_params.feature_data[i][j];
510
511   for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
512     feature_mask = 0;
513     for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
514       if (frame_header->segmentation_params.feature_enabled[i][j])
515         feature_mask |= 1 << j;
516     }
517     pic_param->seg_info.feature_mask[i] = feature_mask;
518   }
519 }
520
521 static void
522 _setup_film_grain_info (VADecPictureParameterBufferAV1 * pic_param,
523     GstAV1FrameHeaderOBU * frame_header)
524 {
525   guint i;
526
527   if (!frame_header->film_grain_params.apply_grain)
528     return;
529
530   pic_param->film_grain_info.num_y_points =
531       frame_header->film_grain_params.num_y_points;
532   for (i = 0; i < frame_header->film_grain_params.num_y_points; i++) {
533     pic_param->film_grain_info.point_y_value[i] =
534         frame_header->film_grain_params.point_y_value[i];
535     pic_param->film_grain_info.point_y_scaling[i] =
536         frame_header->film_grain_params.point_y_scaling[i];
537   }
538
539   pic_param->film_grain_info.num_cb_points =
540       frame_header->film_grain_params.num_cb_points;
541   for (i = 0; i < frame_header->film_grain_params.num_cb_points; i++) {
542     pic_param->film_grain_info.point_cb_value[i] =
543         frame_header->film_grain_params.point_cb_value[i];
544     pic_param->film_grain_info.point_cb_scaling[i] =
545         frame_header->film_grain_params.point_cb_scaling[i];
546   }
547
548   pic_param->film_grain_info.num_cr_points =
549       frame_header->film_grain_params.num_cr_points;
550   for (i = 0; i < frame_header->film_grain_params.num_cr_points; i++) {
551     pic_param->film_grain_info.point_cr_value[i] =
552         frame_header->film_grain_params.point_cr_value[i];
553     pic_param->film_grain_info.point_cr_scaling[i] =
554         frame_header->film_grain_params.point_cr_scaling[i];
555   }
556
557
558   if (pic_param->film_grain_info.num_y_points) {
559     for (i = 0; i < 24; i++) {
560       pic_param->film_grain_info.ar_coeffs_y[i] =
561           frame_header->film_grain_params.ar_coeffs_y_plus_128[i] - 128;
562     }
563   }
564   if (frame_header->film_grain_params.chroma_scaling_from_luma
565       || pic_param->film_grain_info.num_cb_points) {
566     for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
567       pic_param->film_grain_info.ar_coeffs_cb[i] =
568           frame_header->film_grain_params.ar_coeffs_cb_plus_128[i] - 128;
569     }
570   }
571   if (frame_header->film_grain_params.chroma_scaling_from_luma
572       || pic_param->film_grain_info.num_cr_points) {
573     for (i = 0; i < GST_AV1_MAX_NUM_POS_LUMA; i++) {
574       pic_param->film_grain_info.ar_coeffs_cr[i] =
575           frame_header->film_grain_params.ar_coeffs_cr_plus_128[i] - 128;
576     }
577   }
578 }
579
580 static void
581 _setup_loop_filter_info (VADecPictureParameterBufferAV1 * pic_param,
582     GstAV1FrameHeaderOBU * frame_header)
583 {
584   guint i;
585
586   pic_param->filter_level[0] =
587       frame_header->loop_filter_params.loop_filter_level[0];
588   pic_param->filter_level[1] =
589       frame_header->loop_filter_params.loop_filter_level[1];
590   pic_param->filter_level_u =
591       frame_header->loop_filter_params.loop_filter_level[2];
592   pic_param->filter_level_v =
593       frame_header->loop_filter_params.loop_filter_level[3];
594
595   for (i = 0; i < GST_AV1_TOTAL_REFS_PER_FRAME; i++)
596     pic_param->ref_deltas[i] =
597         frame_header->loop_filter_params.loop_filter_ref_deltas[i];
598   for (i = 0; i < 2; i++)
599     pic_param->mode_deltas[i] =
600         frame_header->loop_filter_params.loop_filter_mode_deltas[i];
601 }
602
603 static void
604 _setup_quantization_info (VADecPictureParameterBufferAV1 * pic_param,
605     GstAV1FrameHeaderOBU * frame_header)
606 {
607   pic_param->qmatrix_fields.bits.using_qmatrix =
608       frame_header->quantization_params.using_qmatrix;
609   if (frame_header->quantization_params.using_qmatrix) {
610     pic_param->qmatrix_fields.bits.qm_y =
611         frame_header->quantization_params.qm_y;
612     pic_param->qmatrix_fields.bits.qm_u =
613         frame_header->quantization_params.qm_u;
614     pic_param->qmatrix_fields.bits.qm_v =
615         frame_header->quantization_params.qm_v;
616   } else {
617     pic_param->qmatrix_fields.bits.qm_y = 0;
618     pic_param->qmatrix_fields.bits.qm_u = 0;
619     pic_param->qmatrix_fields.bits.qm_v = 0;
620   }
621 }
622
623 static void
624 _setup_cdef_info (VADecPictureParameterBufferAV1 * pic_param,
625     GstAV1FrameHeaderOBU * frame_header, guint8 num_planes)
626 {
627   guint8 sec_strength;
628   guint i;
629
630   pic_param->cdef_damping_minus_3 = frame_header->cdef_params.cdef_damping - 3;
631   pic_param->cdef_bits = frame_header->cdef_params.cdef_bits;
632   for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
633     sec_strength = frame_header->cdef_params.cdef_y_sec_strength[i];
634     g_assert (sec_strength <= 4);
635     /* may need to minus 1 in order to merge with primary value. */
636     if (sec_strength == 4)
637       sec_strength--;
638
639     pic_param->cdef_y_strengths[i] =
640         ((frame_header->cdef_params.cdef_y_pri_strength[i] & 0xf) << 2) |
641         (sec_strength & 0x03);
642   }
643   if (num_planes > 1) {
644     for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
645       sec_strength = frame_header->cdef_params.cdef_uv_sec_strength[i];
646       g_assert (sec_strength <= 4);
647       /* may need to minus 1 in order to merge with primary value. */
648       if (sec_strength == 4)
649         sec_strength--;
650
651       pic_param->cdef_uv_strengths[i] =
652           ((frame_header->cdef_params.cdef_uv_pri_strength[i] & 0xf) << 2) |
653           (sec_strength & 0x03);
654     }
655   } else {
656     for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
657       pic_param->cdef_uv_strengths[i] = 0;
658     }
659   }
660 }
661
662 static void
663 _setup_global_motion_info (VADecPictureParameterBufferAV1 * pic_param,
664     GstAV1FrameHeaderOBU * frame_header)
665 {
666   guint i, j;
667
668   for (i = 0; i < 7; i++) {
669     /* assuming VAAV1TransformationType and GstAV1WarpModelType are
670      * equivalent */
671     pic_param->wm[i].wmtype = (VAAV1TransformationType)
672         frame_header->global_motion_params.gm_type[GST_AV1_REF_LAST_FRAME + i];
673
674     for (j = 0; j < 6; j++)
675       pic_param->wm[i].wmmat[j] =
676           frame_header->global_motion_params.gm_params
677           [GST_AV1_REF_LAST_FRAME + i][j];
678
679     pic_param->wm[i].wmmat[6] = 0;
680     pic_param->wm[i].wmmat[7] = 0;
681
682     pic_param->wm[i].invalid =
683         frame_header->global_motion_params.invalid[GST_AV1_REF_LAST_FRAME + i];
684   }
685 }
686
687 static GstFlowReturn
688 gst_va_av1_dec_start_picture (GstAV1Decoder * decoder, GstAV1Picture * picture,
689     GstAV1Dpb * dpb)
690 {
691   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
692   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
693   GstAV1FrameHeaderOBU *frame_header = &picture->frame_hdr;
694   GstAV1SequenceHeaderOBU *seq_header = &self->seq;
695   VADecPictureParameterBufferAV1 pic_param = { };
696   GstVaDecodePicture *va_pic;
697   guint i;
698
699   va_pic = gst_av1_picture_get_user_data (picture);
700   g_assert (va_pic);
701
702   /* *INDENT-OFF* */
703   pic_param = (VADecPictureParameterBufferAV1){
704     .profile = seq_header->seq_profile,
705     .order_hint_bits_minus_1 = seq_header->order_hint_bits_minus_1,
706     .matrix_coefficients = seq_header->color_config.matrix_coefficients,
707     .seq_info_fields.fields = {
708       .still_picture = seq_header->still_picture,
709       .use_128x128_superblock = seq_header->use_128x128_superblock,
710       .enable_filter_intra = seq_header->enable_filter_intra,
711       .enable_intra_edge_filter = seq_header->enable_intra_edge_filter,
712       .enable_interintra_compound = seq_header->enable_interintra_compound,
713       .enable_masked_compound = seq_header->enable_masked_compound,
714       .enable_dual_filter = seq_header->enable_dual_filter,
715       .enable_order_hint = seq_header->enable_order_hint,
716       .enable_jnt_comp = seq_header->enable_jnt_comp,
717       .enable_cdef = seq_header->enable_cdef,
718       .mono_chrome = seq_header->color_config.mono_chrome,
719       .color_range = seq_header->color_config.color_range,
720       .subsampling_x = seq_header->color_config.subsampling_x,
721       .subsampling_y = seq_header->color_config.subsampling_y,
722       .film_grain_params_present = seq_header->film_grain_params_present,
723     },
724     .anchor_frames_num = 0,
725     .anchor_frames_list = NULL,
726     .frame_width_minus1 = frame_header->upscaled_width - 1,
727     .frame_height_minus1 = frame_header->frame_height - 1,
728     .output_frame_width_in_tiles_minus_1 = 0,
729     .output_frame_height_in_tiles_minus_1 = 0,
730     .order_hint = frame_header->order_hint,
731     /* Segmentation */
732     .seg_info.segment_info_fields.bits = {
733       .enabled = frame_header->segmentation_params.segmentation_enabled,
734       .update_map = frame_header->segmentation_params.segmentation_update_map,
735       .temporal_update =
736         frame_header->segmentation_params.segmentation_temporal_update,
737       .update_data =
738         frame_header->segmentation_params.segmentation_update_data,
739     },
740     /* FilmGrain */
741     .film_grain_info = {
742       .film_grain_info_fields.bits = {
743         .apply_grain = frame_header->film_grain_params.apply_grain,
744         .chroma_scaling_from_luma =
745           frame_header->film_grain_params.chroma_scaling_from_luma,
746         .grain_scaling_minus_8 =
747           frame_header->film_grain_params.grain_scaling_minus_8,
748         .ar_coeff_lag = frame_header->film_grain_params.ar_coeff_lag,
749         .ar_coeff_shift_minus_6 =
750           frame_header->film_grain_params.ar_coeff_shift_minus_6,
751         .grain_scale_shift = frame_header->film_grain_params.grain_scale_shift,
752         .overlap_flag = frame_header->film_grain_params.overlap_flag,
753         .clip_to_restricted_range =
754           frame_header->film_grain_params.clip_to_restricted_range,
755       },
756       .grain_seed = frame_header->film_grain_params.grain_seed,
757       .cb_mult = frame_header->film_grain_params.cb_mult,
758       .cb_luma_mult = frame_header->film_grain_params.cb_luma_mult,
759       .cb_offset = frame_header->film_grain_params.cb_offset,
760       .cr_mult = frame_header->film_grain_params.cr_mult,
761       .cr_luma_mult = frame_header->film_grain_params.cr_luma_mult,
762       .cr_offset = frame_header->film_grain_params.cr_offset,
763     },
764     .tile_cols = frame_header->tile_info.tile_cols,
765     .tile_rows = frame_header->tile_info.tile_rows,
766     .context_update_tile_id = frame_header->tile_info.context_update_tile_id,
767     .pic_info_fields.bits = {
768       .frame_type = frame_header->frame_type,
769       .show_frame = frame_header->show_frame,
770       .showable_frame = frame_header->showable_frame,
771       .error_resilient_mode = frame_header->error_resilient_mode,
772       .disable_cdf_update = frame_header->disable_cdf_update,
773       .allow_screen_content_tools = frame_header->allow_screen_content_tools,
774       .force_integer_mv = frame_header->force_integer_mv,
775       .allow_intrabc = frame_header->allow_intrabc,
776       .use_superres = frame_header->use_superres,
777       .allow_high_precision_mv = frame_header->allow_high_precision_mv,
778       .is_motion_mode_switchable = frame_header->is_motion_mode_switchable,
779       .use_ref_frame_mvs = frame_header->use_ref_frame_mvs,
780       .disable_frame_end_update_cdf =
781         frame_header->disable_frame_end_update_cdf,
782       .uniform_tile_spacing_flag =
783         frame_header->tile_info.uniform_tile_spacing_flag,
784       .allow_warped_motion = frame_header->allow_warped_motion,
785     },
786     .superres_scale_denominator = frame_header->superres_denom,
787     .interp_filter = frame_header->interpolation_filter,
788     /* loop filter */
789     .loop_filter_info_fields.bits = {
790       .sharpness_level =
791         frame_header->loop_filter_params.loop_filter_sharpness,
792       .mode_ref_delta_enabled =
793         frame_header->loop_filter_params.loop_filter_delta_enabled,
794       .mode_ref_delta_update =
795         frame_header->loop_filter_params.loop_filter_delta_update,
796     },
797     .mode_control_fields.bits = {
798       .delta_lf_present_flag =
799         frame_header->loop_filter_params.delta_lf_present,
800       .log2_delta_lf_res = frame_header->loop_filter_params.delta_lf_res,
801       .delta_lf_multi = frame_header->loop_filter_params.delta_lf_multi,
802       .delta_q_present_flag =
803         frame_header->quantization_params.delta_q_present,
804       .log2_delta_q_res = frame_header->quantization_params.delta_q_res,
805       .tx_mode = frame_header->tx_mode,
806       .reference_select = frame_header->reference_select,
807       .reduced_tx_set_used = frame_header->reduced_tx_set,
808       .skip_mode_present = frame_header->skip_mode_present,
809     },
810     /* quantization */
811     .base_qindex = frame_header->quantization_params.base_q_idx,
812     .y_dc_delta_q = frame_header->quantization_params.delta_q_y_dc,
813     .u_dc_delta_q = frame_header->quantization_params.delta_q_u_dc,
814     .u_ac_delta_q = frame_header->quantization_params.delta_q_u_ac,
815     .v_dc_delta_q = frame_header->quantization_params.delta_q_v_dc,
816     .v_ac_delta_q = frame_header->quantization_params.delta_q_v_ac,
817     /* loop restoration */
818     .loop_restoration_fields.bits = {
819       .yframe_restoration_type =
820         frame_header->loop_restoration_params.frame_restoration_type[0],
821       .cbframe_restoration_type =
822         frame_header->loop_restoration_params.frame_restoration_type[1],
823       .crframe_restoration_type =
824         frame_header->loop_restoration_params.frame_restoration_type[2],
825       .lr_unit_shift = frame_header->loop_restoration_params.lr_unit_shift,
826       .lr_uv_shift = frame_header->loop_restoration_params.lr_uv_shift,
827     },
828   };
829   /* *INDENT-ON* */
830
831   if (seq_header->bit_depth == 8) {
832     pic_param.bit_depth_idx = 0;
833   } else if (seq_header->bit_depth == 10) {
834     pic_param.bit_depth_idx = 1;
835   } else if (seq_header->bit_depth == 12) {
836     pic_param.bit_depth_idx = 2;
837   } else {
838     g_assert_not_reached ();
839   }
840
841   if (frame_header->film_grain_params.apply_grain) {
842     pic_param.current_frame = gst_va_decode_picture_get_aux_surface (va_pic);
843     pic_param.current_display_picture =
844         gst_va_decode_picture_get_surface (va_pic);
845   } else {
846     pic_param.current_frame = gst_va_decode_picture_get_surface (va_pic);
847     pic_param.current_display_picture = VA_INVALID_SURFACE;
848   }
849
850   for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
851     if (dpb->pic_list[i]) {
852       if (dpb->pic_list[i]->apply_grain) {
853         pic_param.ref_frame_map[i] = gst_va_decode_picture_get_aux_surface
854             (gst_av1_picture_get_user_data (dpb->pic_list[i]));
855       } else {
856         pic_param.ref_frame_map[i] = gst_va_decode_picture_get_surface
857             (gst_av1_picture_get_user_data (dpb->pic_list[i]));
858       }
859     } else {
860       pic_param.ref_frame_map[i] = VA_INVALID_SURFACE;
861     }
862   }
863   for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
864     pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i];
865   }
866   pic_param.primary_ref_frame = frame_header->primary_ref_frame;
867
868   _setup_segment_info (&pic_param, frame_header);
869   _setup_film_grain_info (&pic_param, frame_header);
870
871   for (i = 0; i < 63; i++) {
872     pic_param.width_in_sbs_minus_1[i] =
873         frame_header->tile_info.width_in_sbs_minus_1[i];
874     pic_param.height_in_sbs_minus_1[i] =
875         frame_header->tile_info.height_in_sbs_minus_1[i];
876   }
877
878   _setup_loop_filter_info (&pic_param, frame_header);
879   _setup_quantization_info (&pic_param, frame_header);
880   _setup_cdef_info (&pic_param, frame_header, seq_header->num_planes);
881   _setup_global_motion_info (&pic_param, frame_header);
882
883   if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
884           VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
885     return GST_FLOW_ERROR;
886
887   return GST_FLOW_OK;
888 }
889
890 static GstFlowReturn
891 gst_va_av1_dec_decode_tile (GstAV1Decoder * decoder, GstAV1Picture * picture,
892     GstAV1Tile * tile)
893 {
894   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
895   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
896   GstAV1TileGroupOBU *tile_group = &tile->tile_group;
897   GstVaDecodePicture *va_pic;
898   guint i;
899   VASliceParameterBufferAV1 slice_param[GST_AV1_MAX_TILE_COUNT];
900
901   GST_TRACE_OBJECT (self, "-");
902
903   for (i = 0; i < tile_group->tg_end - tile_group->tg_start + 1; i++) {
904     slice_param[i] = (VASliceParameterBufferAV1) {
905     };
906     slice_param[i].slice_data_size =
907         tile_group->entry[tile_group->tg_start + i].tile_size;
908     slice_param[i].slice_data_offset =
909         tile_group->entry[tile_group->tg_start + i].tile_offset;
910     slice_param[i].tile_row =
911         tile_group->entry[tile_group->tg_start + i].tile_row;
912     slice_param[i].tile_column =
913         tile_group->entry[tile_group->tg_start + i].tile_col;
914     slice_param[i].slice_data_flag = 0;
915   }
916
917   va_pic = gst_av1_picture_get_user_data (picture);
918
919   if (!gst_va_decoder_add_slice_buffer_with_n_params (base->decoder, va_pic,
920           slice_param, sizeof (VASliceParameterBufferAV1), i, tile->obu.data,
921           tile->obu.obu_size)) {
922     return GST_FLOW_ERROR;
923   }
924
925   return GST_FLOW_OK;
926 }
927
928 static GstFlowReturn
929 gst_va_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
930 {
931   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
932   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
933   GstVaDecodePicture *va_pic;
934
935   GST_LOG_OBJECT (self, "end picture %p, (system_frame_number %d)",
936       picture, picture->system_frame_number);
937
938   va_pic = gst_av1_picture_get_user_data (picture);
939
940   if (!gst_va_decoder_decode_with_aux_surface (base->decoder, va_pic,
941           picture->apply_grain)) {
942     return GST_FLOW_ERROR;
943   }
944
945   return GST_FLOW_OK;
946 }
947
948 static GstFlowReturn
949 gst_va_av1_dec_output_picture (GstAV1Decoder * decoder,
950     GstVideoCodecFrame * frame, GstAV1Picture * picture)
951 {
952   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
953   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
954   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
955   gboolean ret;
956
957   g_assert (picture->frame_hdr.show_frame ||
958       picture->frame_hdr.show_existing_frame);
959
960   GST_LOG_OBJECT (self,
961       "Outputting picture %p (system_frame_number %d)",
962       picture, picture->system_frame_number);
963
964   if (picture->frame_hdr.show_existing_frame) {
965     GstVaDecodePicture *pic;
966
967     g_assert (!frame->output_buffer);
968     pic = gst_av1_picture_get_user_data (picture);
969     frame->output_buffer = gst_buffer_ref (pic->gstbuffer);
970   }
971
972   ret = gst_va_base_dec_process_output (base, frame, 0);
973   gst_av1_picture_unref (picture);
974
975   if (ret)
976     return gst_video_decoder_finish_frame (vdec, frame);
977   return GST_FLOW_ERROR;
978 }
979
980 static gboolean
981 gst_va_av1_dec_start (GstVideoDecoder * decoder)
982 {
983   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
984
985   self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
986
987   return GST_VIDEO_DECODER_CLASS (parent_class)->start (decoder);
988 }
989
990 static gboolean
991 gst_va_av1_dec_close (GstVideoDecoder * decoder)
992 {
993   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
994
995   _clear_internal_pool (self);
996
997   return gst_va_base_dec_close (GST_VIDEO_DECODER (decoder));
998 }
999
1000 static void
1001 gst_va_av1_dec_init (GTypeInstance * instance, gpointer g_class)
1002 {
1003   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
1004 }
1005
1006 static void
1007 gst_va_av1_dec_dispose (GObject * object)
1008 {
1009   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
1010   G_OBJECT_CLASS (parent_class)->dispose (object);
1011 }
1012
1013 static void
1014 gst_va_av1_dec_class_init (gpointer g_class, gpointer class_data)
1015 {
1016   GstCaps *src_doc_caps, *sink_doc_caps;
1017   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
1018   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1019   GstAV1DecoderClass *av1decoder_class = GST_AV1_DECODER_CLASS (g_class);
1020   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (g_class);
1021   struct CData *cdata = class_data;
1022   gchar *long_name;
1023
1024   if (cdata->description) {
1025     long_name = g_strdup_printf ("VA-API AV1 Decoder in %s",
1026         cdata->description);
1027   } else {
1028     long_name = g_strdup ("VA-API AV1 Decoder");
1029   }
1030
1031   gst_element_class_set_metadata (element_class, long_name,
1032       "Codec/Decoder/Video/Hardware",
1033       "VA-API based AV1 video decoder", "He Junyan <junyan.he@intel.com>");
1034
1035   sink_doc_caps = gst_caps_from_string (sink_caps_str);
1036   src_doc_caps = gst_caps_from_string (src_caps_str);
1037
1038   parent_class = g_type_class_peek_parent (g_class);
1039
1040   /**
1041    * GstVaAV1Dec:device-path:
1042    *
1043    * It shows the DRM device path used for the VA operation, if any.
1044    *
1045    * Since: 1.22
1046    */
1047   gst_va_base_dec_class_init (GST_VA_BASE_DEC_CLASS (g_class), AV1,
1048       cdata->render_device_path, cdata->sink_caps, cdata->src_caps,
1049       src_doc_caps, sink_doc_caps);
1050
1051   gobject_class->dispose = gst_va_av1_dec_dispose;
1052
1053   decoder_class->getcaps = GST_DEBUG_FUNCPTR (gst_va_av1_dec_getcaps);
1054   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_va_av1_dec_negotiate);
1055   decoder_class->close = GST_DEBUG_FUNCPTR (gst_va_av1_dec_close);
1056   decoder_class->start = GST_DEBUG_FUNCPTR (gst_va_av1_dec_start);
1057
1058   av1decoder_class->new_sequence =
1059       GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_sequence);
1060   av1decoder_class->new_picture =
1061       GST_DEBUG_FUNCPTR (gst_va_av1_dec_new_picture);
1062   av1decoder_class->duplicate_picture =
1063       GST_DEBUG_FUNCPTR (gst_va_av1_dec_duplicate_picture);
1064   av1decoder_class->start_picture =
1065       GST_DEBUG_FUNCPTR (gst_va_av1_dec_start_picture);
1066   av1decoder_class->decode_tile =
1067       GST_DEBUG_FUNCPTR (gst_va_av1_dec_decode_tile);
1068   av1decoder_class->end_picture =
1069       GST_DEBUG_FUNCPTR (gst_va_av1_dec_end_picture);
1070   av1decoder_class->output_picture =
1071       GST_DEBUG_FUNCPTR (gst_va_av1_dec_output_picture);
1072
1073   g_free (long_name);
1074   g_free (cdata->description);
1075   g_free (cdata->render_device_path);
1076   gst_caps_unref (cdata->src_caps);
1077   gst_caps_unref (cdata->sink_caps);
1078   g_free (cdata);
1079 }
1080
1081 static gpointer
1082 _register_debug_category (gpointer data)
1083 {
1084   GST_DEBUG_CATEGORY_INIT (gst_va_av1dec_debug, "vaav1dec", 0,
1085       "VA AV1 decoder");
1086
1087   return NULL;
1088 }
1089
1090 gboolean
1091 gst_va_av1_dec_register (GstPlugin * plugin, GstVaDevice * device,
1092     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
1093 {
1094   static GOnce debug_once = G_ONCE_INIT;
1095   GType type;
1096   GTypeInfo type_info = {
1097     .class_size = sizeof (GstVaAV1DecClass),
1098     .class_init = gst_va_av1_dec_class_init,
1099     .instance_size = sizeof (GstVaAV1Dec),
1100     .instance_init = gst_va_av1_dec_init,
1101   };
1102   struct CData *cdata;
1103   gboolean ret;
1104   gchar *type_name, *feature_name;
1105
1106   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
1107   g_return_val_if_fail (GST_IS_VA_DEVICE (device), FALSE);
1108   g_return_val_if_fail (GST_IS_CAPS (sink_caps), FALSE);
1109   g_return_val_if_fail (GST_IS_CAPS (src_caps), FALSE);
1110
1111   cdata = g_new (struct CData, 1);
1112   cdata->description = NULL;
1113   cdata->render_device_path = g_strdup (device->render_device_path);
1114   cdata->sink_caps = _complete_sink_caps (sink_caps);
1115   cdata->src_caps = gst_caps_ref (src_caps);
1116
1117   /* class data will be leaked if the element never gets instantiated */
1118   GST_MINI_OBJECT_FLAG_SET (cdata->sink_caps,
1119       GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1120   GST_MINI_OBJECT_FLAG_SET (src_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1121
1122   type_info.class_data = cdata;
1123
1124   /* The first decoder to be registered should use a constant name,
1125    * like vaav1dec, for any additional decoders, we create unique
1126    * names, using inserting the render device name. */
1127   if (device->index == 0) {
1128     type_name = g_strdup ("GstVaAV1Dec");
1129     feature_name = g_strdup ("vaav1dec");
1130   } else {
1131     gchar *basename = g_path_get_basename (device->render_device_path);
1132     type_name = g_strdup_printf ("GstVa%sAV1Dec", basename);
1133     feature_name = g_strdup_printf ("va%sav1dec", basename);
1134     cdata->description = basename;
1135
1136     /* lower rank for non-first device */
1137     if (rank > 0)
1138       rank--;
1139   }
1140
1141   g_once (&debug_once, _register_debug_category, NULL);
1142
1143   type = g_type_register_static (GST_TYPE_AV1_DECODER,
1144       type_name, &type_info, 0);
1145
1146   ret = gst_element_register (plugin, feature_name, rank, type);
1147
1148   g_free (type_name);
1149   g_free (feature_name);
1150
1151   return ret;
1152 }