2c1bb79b92b4a786c37f297a84b663c107bf3210
[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   GstFlowReturn last_ret;
75
76   GstAV1SequenceHeaderOBU seq;
77   gint max_width;
78   gint max_height;
79   GstVideoFormat preferred_format;
80   /* Used for layers not output. */
81   GstBufferPool *internal_pool;
82 };
83
84 static GstElementClass *parent_class = NULL;
85
86 /* *INDENT-OFF* */
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 }");
91 /* *INDENT-ON* */
92
93 static const gchar *sink_caps_str = "video/x-av1";
94
95 static gboolean
96 gst_va_av1_dec_negotiate (GstVideoDecoder * decoder)
97 {
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;
103
104   /* Ignore downstream renegotiation request. */
105   if (!base->need_negotiation)
106     return TRUE;
107
108   base->need_negotiation = FALSE;
109
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))
115       return FALSE;
116
117     if (!gst_va_decoder_open (base->decoder, base->profile, base->rt_format))
118       return FALSE;
119
120     if (!gst_va_decoder_set_frame_size (base->decoder, self->max_width,
121             self->max_height))
122       return FALSE;
123   }
124
125   if (base->output_state)
126     gst_video_codec_state_unref (base->output_state);
127
128   gst_va_base_dec_get_preferred_format_and_caps_features (base, &format,
129       &capsfeatures);
130   if (format == GST_VIDEO_FORMAT_UNKNOWN)
131     return FALSE;
132
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"
136         " the last result");
137     return FALSE;
138   }
139   self->preferred_format = format;
140
141   base->output_state = gst_video_decoder_set_output_state (decoder, format,
142       base->width, base->height, av1dec->input_state);
143
144   base->output_state->caps = gst_video_info_to_caps (&base->output_state->info);
145   if (capsfeatures)
146     gst_caps_set_features_simple (base->output_state->caps, capsfeatures);
147
148   GST_INFO_OBJECT (self, "Negotiated caps %" GST_PTR_FORMAT,
149       base->output_state->caps);
150
151   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
152 }
153
154 static GstCaps *
155 _complete_sink_caps (GstCaps * sinkcaps)
156 {
157   GstCaps *caps = gst_caps_copy (sinkcaps);
158   GValue val = G_VALUE_INIT;
159
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);
164
165   return caps;
166 }
167
168 static VAProfile
169 _get_profile (GstVaAV1Dec * self, const GstAV1SequenceHeaderOBU * seq_hdr)
170 {
171   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
172   VAProfile profile = VAProfileNone;
173
174   switch (seq_hdr->seq_profile) {
175     case GST_AV1_PROFILE_0:
176       profile = VAProfileAV1Profile0;
177       break;
178     case GST_AV1_PROFILE_1:
179       profile = VAProfileAV1Profile1;
180       break;
181     default:
182       GST_ERROR_OBJECT (self, "Unsupported av1 profile value %d",
183           seq_hdr->seq_profile);
184       return VAProfileNone;
185   }
186
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;
191   }
192
193   return profile;
194 }
195
196 static guint
197 _get_rtformat (GstVaAV1Dec * self, VAProfile profile,
198     const GstAV1SequenceHeaderOBU * seq_header)
199 {
200   /* 6.4.1:
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
206    */
207
208   /* TODO: consider Monochrome case. Just return 4:2:0 for Monochrome now. */
209   switch (profile) {
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;
215       }
216       break;
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;
222       }
223       break;
224     default:
225       break;
226   }
227
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);
230   return 0;
231 }
232
233 static GstCaps *
234 gst_va_av1_dec_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
235 {
236   GstCaps *sinkcaps, *caps = NULL, *tmp;
237   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
238
239   if (base->decoder)
240     caps = gst_va_decoder_get_sinkpad_caps (base->decoder);
241
242   if (caps) {
243     sinkcaps = _complete_sink_caps (caps);
244     gst_caps_unref (caps);
245     if (filter) {
246       tmp = gst_caps_intersect_full (filter, sinkcaps,
247           GST_CAPS_INTERSECT_FIRST);
248       gst_caps_unref (sinkcaps);
249       caps = tmp;
250     } else {
251       caps = sinkcaps;
252     }
253     GST_LOG_OBJECT (base, "Returning caps %" GST_PTR_FORMAT, caps);
254   } else if (!caps) {
255     caps = gst_video_decoder_proxy_getcaps (decoder, NULL, filter);
256   }
257
258   return caps;
259 }
260
261 static void
262 _clear_internal_pool (GstVaAV1Dec * self)
263 {
264   if (self->internal_pool)
265     gst_buffer_pool_set_active (self->internal_pool, FALSE);
266
267   gst_clear_object (&self->internal_pool);
268 }
269
270 static GstBufferPool *
271 _create_internal_pool (GstVaAV1Dec * self, gint width, gint height)
272 {
273   GstVaBaseDec *base = GST_VA_BASE_DEC (self);
274   GstVideoInfo info;
275   GArray *surface_formats;
276   GstAllocator *allocator;
277   GstCaps *caps = NULL;
278   GstBufferPool *pool;
279   GstAllocationParams params = { 0, };
280
281   gst_allocation_params_init (&params);
282
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;
287
288     gst_va_base_dec_get_preferred_format_and_caps_features (base,
289         &format, NULL);
290     if (format == GST_VIDEO_FORMAT_UNKNOWN) {
291       GST_WARNING_OBJECT (self, "Failed to get format for internal pool");
292       return NULL;
293     }
294
295     self->preferred_format = format;
296   }
297
298   gst_video_info_set_format (&info, self->preferred_format, width, height);
299
300   caps = gst_video_info_to_caps (&info);
301   if (caps == NULL) {
302     GST_WARNING_OBJECT (self, "Failed to create caps for internal pool");
303     return NULL;
304   }
305
306   gst_caps_set_features_simple (caps,
307       gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VA));
308
309   surface_formats = gst_va_decoder_get_surface_formats (base->decoder);
310   allocator = gst_va_allocator_new (base->display, surface_formats);
311
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,
314       allocator, &params);
315   gst_clear_caps (&caps);
316
317   if (!pool) {
318     GST_WARNING_OBJECT (self, "Failed to create internal pool");
319     gst_object_unref (allocator);
320     return NULL;
321   }
322
323   gst_object_unref (allocator);
324
325   gst_buffer_pool_set_active (pool, TRUE);
326
327   return pool;
328 }
329
330 static GstFlowReturn
331 gst_va_av1_dec_new_sequence (GstAV1Decoder * decoder,
332     const GstAV1SequenceHeaderOBU * seq_hdr, gint max_dpb_size)
333 {
334   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
335   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
336   VAProfile profile;
337   guint rt_format;
338
339   GST_LOG_OBJECT (self, "new sequence");
340
341   profile = _get_profile (self, seq_hdr);
342   if (profile == VAProfileNone)
343     return GST_FLOW_NOT_NEGOTIATED;
344
345   rt_format = _get_rtformat (self, profile, seq_hdr);
346   if (!rt_format)
347     return GST_FLOW_NOT_NEGOTIATED;
348
349   self->seq = *seq_hdr;
350
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;
356
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;
362
363     base->min_buffers = 7 + 4;  /* dpb size + scratch surfaces */
364
365     /* May be changed by frame header */
366     base->width = self->max_width;
367     base->height = self->max_height;
368     base->need_valign = FALSE;
369   }
370
371   return GST_FLOW_OK;
372 }
373
374 static GstFlowReturn
375 gst_va_av1_dec_new_picture (GstAV1Decoder * decoder,
376     GstVideoCodecFrame * frame, GstAV1Picture * picture)
377 {
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;
383
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;
392     }
393   } else {
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;
398
399       if (base->width < self->max_width || base->height < self->max_height) {
400         base->need_valign = TRUE;
401         /* *INDENT-OFF* */
402         base->valign = (GstVideoAlignment){
403           .padding_bottom = self->max_height - base->height,
404           .padding_right = self->max_width - base->width,
405         };
406         /* *INDENT-ON* */
407       }
408
409       base->need_negotiation = TRUE;
410     }
411   }
412
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;
417     }
418   }
419
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;
428     }
429   } else {
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;
436     }
437   }
438
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;
445     }
446   }
447
448   pic = gst_va_decode_picture_new (base->decoder, frame->output_buffer);
449
450   gst_av1_picture_set_user_data (picture, pic,
451       (GDestroyNotify) gst_va_decode_picture_free);
452
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));
457   } else {
458     GST_LOG_OBJECT (self, "New va decode picture %p - %#x", pic,
459         gst_va_decode_picture_get_surface (pic));
460   }
461
462   return GST_FLOW_OK;
463 }
464
465 static GstAV1Picture *
466 gst_va_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
467     GstVideoCodecFrame * frame, GstAV1Picture * picture)
468 {
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;
474
475   pic = gst_av1_picture_get_user_data (picture);
476   if (!pic) {
477     GST_ERROR_OBJECT (self, "Parent picture does not have a va picture");
478     return NULL;
479   }
480
481   new_picture = gst_av1_picture_new ();
482   g_assert (pic->gstbuffer);
483   new_pic = gst_va_decode_picture_new (base->decoder, pic->gstbuffer);
484
485   GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT
486       " (surface %#x)", pic, gst_va_decode_picture_get_surface (pic));
487
488   gst_av1_picture_set_user_data (new_picture, new_pic,
489       (GDestroyNotify) gst_va_decode_picture_free);
490
491   return new_picture;
492 }
493
494 static void
495 _setup_segment_info (VADecPictureParameterBufferAV1 * pic_param,
496     GstAV1FrameHeaderOBU * frame_header)
497 {
498   guint i, j;
499   uint8_t feature_mask;
500
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];
505
506   for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
507     feature_mask = 0;
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;
511     }
512     pic_param->seg_info.feature_mask[i] = feature_mask;
513   }
514 }
515
516 static void
517 _setup_film_grain_info (VADecPictureParameterBufferAV1 * pic_param,
518     GstAV1FrameHeaderOBU * frame_header)
519 {
520   guint i;
521
522   if (!frame_header->film_grain_params.apply_grain)
523     return;
524
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];
532   }
533
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];
541   }
542
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];
550   }
551
552
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;
557     }
558   }
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;
564     }
565   }
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;
571     }
572   }
573 }
574
575 static void
576 _setup_loop_filter_info (VADecPictureParameterBufferAV1 * pic_param,
577     GstAV1FrameHeaderOBU * frame_header)
578 {
579   guint i;
580
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];
589
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];
596 }
597
598 static void
599 _setup_quantization_info (VADecPictureParameterBufferAV1 * pic_param,
600     GstAV1FrameHeaderOBU * frame_header)
601 {
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;
611   } else {
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;
615   }
616 }
617
618 static void
619 _setup_cdef_info (VADecPictureParameterBufferAV1 * pic_param,
620     GstAV1FrameHeaderOBU * frame_header, guint8 num_planes)
621 {
622   guint8 sec_strength;
623   guint i;
624
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)
632       sec_strength--;
633
634     pic_param->cdef_y_strengths[i] =
635         ((frame_header->cdef_params.cdef_y_pri_strength[i] & 0xf) << 2) |
636         (sec_strength & 0x03);
637   }
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)
644         sec_strength--;
645
646       pic_param->cdef_uv_strengths[i] =
647           ((frame_header->cdef_params.cdef_uv_pri_strength[i] & 0xf) << 2) |
648           (sec_strength & 0x03);
649     }
650   } else {
651     for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
652       pic_param->cdef_uv_strengths[i] = 0;
653     }
654   }
655 }
656
657 static void
658 _setup_global_motion_info (VADecPictureParameterBufferAV1 * pic_param,
659     GstAV1FrameHeaderOBU * frame_header)
660 {
661   guint i, j;
662
663   for (i = 0; i < 7; i++) {
664     /* assuming VAAV1TransformationType and GstAV1WarpModelType are
665      * equivalent */
666     pic_param->wm[i].wmtype = (VAAV1TransformationType)
667         frame_header->global_motion_params.gm_type[GST_AV1_REF_LAST_FRAME + i];
668
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];
673
674     pic_param->wm[i].wmmat[6] = 0;
675     pic_param->wm[i].wmmat[7] = 0;
676
677     pic_param->wm[i].invalid =
678         frame_header->global_motion_params.invalid[GST_AV1_REF_LAST_FRAME + i];
679   }
680 }
681
682 static GstFlowReturn
683 gst_va_av1_dec_start_picture (GstAV1Decoder * decoder, GstAV1Picture * picture,
684     GstAV1Dpb * dpb)
685 {
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;
692   guint i;
693
694   va_pic = gst_av1_picture_get_user_data (picture);
695   g_assert (va_pic);
696
697   /* *INDENT-OFF* */
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,
718     },
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,
726     /* Segmentation */
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,
730       .temporal_update =
731         frame_header->segmentation_params.segmentation_temporal_update,
732       .update_data =
733         frame_header->segmentation_params.segmentation_update_data,
734     },
735     /* FilmGrain */
736     .film_grain_info = {
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,
750       },
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,
758     },
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,
780     },
781     .superres_scale_denominator = frame_header->superres_denom,
782     .interp_filter = frame_header->interpolation_filter,
783     /* loop filter */
784     .loop_filter_info_fields.bits = {
785       .sharpness_level =
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,
791     },
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,
804     },
805     /* quantization */
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,
822     },
823   };
824   /* *INDENT-ON* */
825
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;
832   } else {
833     g_assert_not_reached ();
834   }
835
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);
840   } else {
841     pic_param.current_frame = gst_va_decode_picture_get_surface (va_pic);
842     pic_param.current_display_picture = VA_INVALID_SURFACE;
843   }
844
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]));
850       } else {
851         pic_param.ref_frame_map[i] = gst_va_decode_picture_get_surface
852             (gst_av1_picture_get_user_data (dpb->pic_list[i]));
853       }
854     } else {
855       pic_param.ref_frame_map[i] = VA_INVALID_SURFACE;
856     }
857   }
858   for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
859     pic_param.ref_frame_idx[i] = frame_header->ref_frame_idx[i];
860   }
861   pic_param.primary_ref_frame = frame_header->primary_ref_frame;
862
863   _setup_segment_info (&pic_param, frame_header);
864   _setup_film_grain_info (&pic_param, frame_header);
865
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];
871   }
872
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);
877
878   if (!gst_va_decoder_add_param_buffer (base->decoder, va_pic,
879           VAPictureParameterBufferType, &pic_param, sizeof (pic_param)))
880     return GST_FLOW_ERROR;
881
882   return GST_FLOW_OK;
883 }
884
885 static GstFlowReturn
886 gst_va_av1_dec_decode_tile (GstAV1Decoder * decoder, GstAV1Picture * picture,
887     GstAV1Tile * tile)
888 {
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;
893   guint i;
894   VASliceParameterBufferAV1 slice_param[GST_AV1_MAX_TILE_COUNT];
895
896   GST_TRACE_OBJECT (self, "-");
897
898   for (i = 0; i < tile_group->tg_end - tile_group->tg_start + 1; i++) {
899     slice_param[i] = (VASliceParameterBufferAV1) {
900     };
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;
910   }
911
912   va_pic = gst_av1_picture_get_user_data (picture);
913
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;
918   }
919
920   return GST_FLOW_OK;
921 }
922
923 static GstFlowReturn
924 gst_va_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
925 {
926   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
927   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
928   GstVaDecodePicture *va_pic;
929
930   GST_LOG_OBJECT (self, "end picture %p, (system_frame_number %d)",
931       picture, picture->system_frame_number);
932
933   va_pic = gst_av1_picture_get_user_data (picture);
934
935   if (!gst_va_decoder_decode_with_aux_surface (base->decoder, va_pic,
936           picture->apply_grain)) {
937     return GST_FLOW_ERROR;
938   }
939
940   return GST_FLOW_OK;
941 }
942
943 static GstFlowReturn
944 gst_va_av1_dec_output_picture (GstAV1Decoder * decoder,
945     GstVideoCodecFrame * frame, GstAV1Picture * picture)
946 {
947   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
948   GstVaBaseDec *base = GST_VA_BASE_DEC (decoder);
949
950   g_assert (picture->frame_hdr.show_frame ||
951       picture->frame_hdr.show_existing_frame);
952
953   GST_LOG_OBJECT (self,
954       "Outputting picture %p (system_frame_number %d)",
955       picture, picture->system_frame_number);
956
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;
961   }
962
963   if (picture->frame_hdr.show_existing_frame) {
964     GstVaDecodePicture *pic;
965
966     g_assert (!frame->output_buffer);
967     pic = gst_av1_picture_get_user_data (picture);
968     frame->output_buffer = gst_buffer_ref (pic->gstbuffer);
969   }
970
971   if (base->copy_frames)
972     gst_va_base_dec_copy_output_buffer (base, frame);
973
974   gst_av1_picture_unref (picture);
975
976   return gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
977 }
978
979 static gboolean
980 gst_va_av1_dec_start (GstVideoDecoder * decoder)
981 {
982   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
983
984   self->preferred_format = GST_VIDEO_FORMAT_UNKNOWN;
985
986   return GST_VIDEO_DECODER_CLASS (parent_class)->start (decoder);
987 }
988
989 static gboolean
990 gst_va_av1_dec_close (GstVideoDecoder * decoder)
991 {
992   GstVaAV1Dec *self = GST_VA_AV1_DEC (decoder);
993
994   _clear_internal_pool (self);
995
996   return gst_va_base_dec_close (GST_VIDEO_DECODER (decoder));
997 }
998
999 static void
1000 gst_va_av1_dec_init (GTypeInstance * instance, gpointer g_class)
1001 {
1002   gst_va_base_dec_init (GST_VA_BASE_DEC (instance), GST_CAT_DEFAULT);
1003 }
1004
1005 static void
1006 gst_va_av1_dec_dispose (GObject * object)
1007 {
1008   gst_va_base_dec_close (GST_VIDEO_DECODER (object));
1009   G_OBJECT_CLASS (parent_class)->dispose (object);
1010 }
1011
1012 static void
1013 gst_va_av1_dec_class_init (gpointer g_class, gpointer class_data)
1014 {
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;
1021   gchar *long_name;
1022
1023   if (cdata->description) {
1024     long_name = g_strdup_printf ("VA-API AV1 Decoder in %s",
1025         cdata->description);
1026   } else {
1027     long_name = g_strdup ("VA-API AV1 Decoder");
1028   }
1029
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>");
1033
1034   sink_doc_caps = gst_caps_from_string (sink_caps_str);
1035   src_doc_caps = gst_caps_from_string (src_caps_str);
1036
1037   parent_class = g_type_class_peek_parent (g_class);
1038
1039   /**
1040    * GstVaAV1Dec:device-path:
1041    *
1042    * It shows the DRM device path used for the VA operation, if any.
1043    *
1044    * Since: 1.22
1045    */
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);
1049
1050   gobject_class->dispose = gst_va_av1_dec_dispose;
1051
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);
1056
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);
1071
1072   g_free (long_name);
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);
1077   g_free (cdata);
1078 }
1079
1080 static gpointer
1081 _register_debug_category (gpointer data)
1082 {
1083   GST_DEBUG_CATEGORY_INIT (gst_va_av1dec_debug, "vaav1dec", 0,
1084       "VA AV1 decoder");
1085
1086   return NULL;
1087 }
1088
1089 gboolean
1090 gst_va_av1_dec_register (GstPlugin * plugin, GstVaDevice * device,
1091     GstCaps * sink_caps, GstCaps * src_caps, guint rank)
1092 {
1093   static GOnce debug_once = G_ONCE_INIT;
1094   GType type;
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,
1100   };
1101   struct CData *cdata;
1102   gboolean ret;
1103   gchar *type_name, *feature_name;
1104
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);
1109
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);
1115
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);
1120
1121   type_info.class_data = cdata;
1122
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");
1129   } else {
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;
1134
1135     /* lower rank for non-first device */
1136     if (rank > 0)
1137       rank--;
1138   }
1139
1140   g_once (&debug_once, _register_debug_category, NULL);
1141
1142   type = g_type_register_static (GST_TYPE_AV1_DECODER,
1143       type_name, &type_info, 0);
1144
1145   ret = gst_element_register (plugin, feature_name, rank, type);
1146
1147   g_free (type_name);
1148   g_free (feature_name);
1149
1150   return ret;
1151 }