d3d11av1dec: Fix typo in debug message
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / d3d11 / gstd3d11av1dec.cpp
1 /* GStreamer
2  * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-d3d11av1dec
22  * @title: d3d11av1dec
23  *
24  * A Direct3D11/DXVA based AV1 video decoder
25  *
26  * ## Example launch line
27  * ```
28  * gst-launch-1.0 filesrc location=/path/to/av1/file ! parsebin ! d3d11av1dec ! d3d11videosink
29  * ```
30  *
31  * Since: 1.20
32  *
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "gstd3d11av1dec.h"
40
41 #include <gst/codecs/gstav1decoder.h>
42 #include <string.h>
43 #include <vector>
44
45 /* HACK: to expose dxva data structure on UWP */
46 #ifdef WINAPI_PARTITION_DESKTOP
47 #undef WINAPI_PARTITION_DESKTOP
48 #endif
49 #define WINAPI_PARTITION_DESKTOP 1
50 #include <d3d9.h>
51 #include <dxva.h>
52
53 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_av1_dec_debug);
54 #define GST_CAT_DEFAULT gst_d3d11_av1_dec_debug
55
56 /* Might not be defined in dxva.h, copied from DXVA AV1 spec available at
57  * https://www.microsoft.com/en-us/download/confirmation.aspx?id=101577
58  * and modified with "GST_" prefix
59  */
60 #pragma pack(push, 1)
61
62 /* AV1 picture entry data structure */
63 typedef struct _GST_DXVA_PicEntry_AV1
64 {
65   UINT width;
66   UINT height;
67
68   // Global motion parameters
69   INT wmmat[6];
70   union
71   {
72     struct
73     {
74       UCHAR wminvalid:1;
75       UCHAR wmtype:2;
76       UCHAR Reserved:5;
77     };
78     UCHAR GlobalMotionFlags;
79   } DUMMYUNIONNAME;
80
81   UCHAR Index;
82   UINT16 Reserved16Bits;
83
84 } GST_DXVA_PicEntry_AV1;
85
86 /* AV1 picture parameters structure */
87 typedef struct _GST_DXVA_PicParams_AV1
88 {
89   UINT width;
90   UINT height;
91
92   UINT max_width;
93   UINT max_height;
94
95   UCHAR CurrPicTextureIndex;
96   UCHAR superres_denom;
97   UCHAR bitdepth;
98   UCHAR seq_profile;
99
100   // Tiles:
101   struct
102   {
103     UCHAR cols;
104     UCHAR rows;
105     USHORT context_update_id;
106     USHORT widths[64];
107     USHORT heights[64];
108   } tiles;
109
110   // Coding Tools
111   union
112   {
113     struct
114     {
115       UINT use_128x128_superblock:1;
116       UINT intra_edge_filter:1;
117       UINT interintra_compound:1;
118       UINT masked_compound:1;
119       UINT warped_motion:1;
120       UINT dual_filter:1;
121       UINT jnt_comp:1;
122       UINT screen_content_tools:1;
123       UINT integer_mv:1;
124       UINT cdef:1;
125       UINT restoration:1;
126       UINT film_grain:1;
127       UINT intrabc:1;
128       UINT high_precision_mv:1;
129       UINT switchable_motion_mode:1;
130       UINT filter_intra:1;
131       UINT disable_frame_end_update_cdf:1;
132       UINT disable_cdf_update:1;
133       UINT reference_mode:1;
134       UINT skip_mode:1;
135       UINT reduced_tx_set:1;
136       UINT superres:1;
137       UINT tx_mode:2;
138       UINT use_ref_frame_mvs:1;
139       UINT enable_ref_frame_mvs:1;
140       UINT reference_frame_update:1;
141       UINT Reserved:5;
142     };
143     UINT32 CodingParamToolFlags;
144   } coding;
145
146   // Format & Picture Info flags
147   union
148   {
149     struct
150     {
151       UCHAR frame_type:2;
152       UCHAR show_frame:1;
153       UCHAR showable_frame:1;
154       UCHAR subsampling_x:1;
155       UCHAR subsampling_y:1;
156       UCHAR mono_chrome:1;
157       UCHAR Reserved:1;
158     };
159     UCHAR FormatAndPictureInfoFlags;
160   } format;
161
162   // References
163   UCHAR primary_ref_frame;
164   UCHAR order_hint;
165   UCHAR order_hint_bits;
166
167   GST_DXVA_PicEntry_AV1 frame_refs[7];
168   UCHAR RefFrameMapTextureIndex[8];
169
170   // Loop filter parameters
171   struct
172   {
173     UCHAR filter_level[2];
174     UCHAR filter_level_u;
175     UCHAR filter_level_v;
176
177     UCHAR sharpness_level;
178     union
179     {
180       struct
181       {
182         UCHAR mode_ref_delta_enabled:1;
183         UCHAR mode_ref_delta_update:1;
184         UCHAR delta_lf_multi:1;
185         UCHAR delta_lf_present:1;
186         UCHAR Reserved:4;
187       };
188       UCHAR ControlFlags;
189     } DUMMYUNIONNAME;
190     CHAR ref_deltas[8];
191     CHAR mode_deltas[2];
192     UCHAR delta_lf_res;
193     UCHAR frame_restoration_type[3];
194     USHORT log2_restoration_unit_size[3];
195     UINT16 Reserved16Bits;
196   } loop_filter;
197
198   // Quantization
199   struct
200   {
201     union
202     {
203       struct
204       {
205         UCHAR delta_q_present:1;
206         UCHAR delta_q_res:2;
207         UCHAR Reserved:5;
208       };
209       UCHAR ControlFlags;
210     } DUMMYUNIONNAME;
211
212     UCHAR base_qindex;
213     CHAR y_dc_delta_q;
214     CHAR u_dc_delta_q;
215     CHAR v_dc_delta_q;
216     CHAR u_ac_delta_q;
217     CHAR v_ac_delta_q;
218     // using_qmatrix:
219     UCHAR qm_y;
220     UCHAR qm_u;
221     UCHAR qm_v;
222     UINT16 Reserved16Bits;
223   } quantization;
224
225   // Cdef parameters
226   struct
227   {
228     union
229     {
230       struct
231       {
232         UCHAR damping:2;
233         UCHAR bits:2;
234         UCHAR Reserved:4;
235       };
236       UCHAR ControlFlags;
237     } DUMMYUNIONNAME;
238
239     union
240     {
241       struct
242       {
243         UCHAR primary:6;
244         UCHAR secondary:2;
245       };
246       UCHAR combined;
247     } y_strengths[8];
248
249     union
250     {
251       struct
252       {
253         UCHAR primary:6;
254         UCHAR secondary:2;
255       };
256       UCHAR combined;
257     } uv_strengths[8];
258
259   } cdef;
260
261   UCHAR interp_filter;
262
263   // Segmentation
264   struct
265   {
266     union
267     {
268       struct
269       {
270         UCHAR enabled:1;
271         UCHAR update_map:1;
272         UCHAR update_data:1;
273         UCHAR temporal_update:1;
274         UCHAR Reserved:4;
275       };
276       UCHAR ControlFlags;
277     } DUMMYUNIONNAME;
278     UCHAR Reserved24Bits[3];
279
280     union
281     {
282       struct
283       {
284         UCHAR alt_q:1;
285         UCHAR alt_lf_y_v:1;
286         UCHAR alt_lf_y_h:1;
287         UCHAR alt_lf_u:1;
288         UCHAR alt_lf_v:1;
289         UCHAR ref_frame:1;
290         UCHAR skip:1;
291         UCHAR globalmv:1;
292       };
293       UCHAR mask;
294     } feature_mask[8];
295
296     SHORT feature_data[8][8];
297
298   } segmentation;
299
300   struct
301   {
302     union
303     {
304       struct
305       {
306         USHORT apply_grain:1;
307         USHORT scaling_shift_minus8:2;
308         USHORT chroma_scaling_from_luma:1;
309         USHORT ar_coeff_lag:2;
310         USHORT ar_coeff_shift_minus6:2;
311         USHORT grain_scale_shift:2;
312         USHORT overlap_flag:1;
313         USHORT clip_to_restricted_range:1;
314         USHORT matrix_coeff_is_identity:1;
315         USHORT Reserved:3;
316       };
317       USHORT ControlFlags;
318     } DUMMYUNIONNAME;
319
320     USHORT grain_seed;
321     UCHAR scaling_points_y[14][2];
322     UCHAR num_y_points;
323     UCHAR scaling_points_cb[10][2];
324     UCHAR num_cb_points;
325     UCHAR scaling_points_cr[10][2];
326     UCHAR num_cr_points;
327     UCHAR ar_coeffs_y[24];
328     UCHAR ar_coeffs_cb[25];
329     UCHAR ar_coeffs_cr[25];
330     UCHAR cb_mult;
331     UCHAR cb_luma_mult;
332     UCHAR cr_mult;
333     UCHAR cr_luma_mult;
334     UCHAR Reserved8Bits;
335     SHORT cb_offset;
336     SHORT cr_offset;
337   } film_grain;
338
339   UINT Reserved32Bits;
340   UINT StatusReportFeedbackNumber;
341 } GST_DXVA_PicParams_AV1;
342
343 /* AV1 tile structure */
344 typedef struct _GST_DXVA_Tile_AV1
345 {
346   UINT DataOffset;
347   UINT DataSize;
348   USHORT row;
349   USHORT column;
350   UINT16 Reserved16Bits;
351   UCHAR anchor_frame;
352   UCHAR Reserved8Bits;
353 } GST_DXVA_Tile_AV1;
354
355 /* AV1 status reporting data structure */
356 typedef struct _GST_DXVA_Status_AV1
357 {
358   UINT StatusReportFeedbackNumber;
359   GST_DXVA_PicEntry_AV1 CurrPic;
360   UCHAR BufType;
361   UCHAR Status;
362   UCHAR Reserved8Bits;
363   USHORT NumMbsAffected;
364 } GST_DXVA_Status_AV1;
365
366 #pragma pack(pop)
367
368 /* reference list 8 + 4 margin */
369 #define NUM_OUTPUT_VIEW 12
370
371 /* *INDENT-OFF* */
372 typedef struct _GstD3D11AV1DecInner
373 {
374   GstD3D11Device *device = nullptr;
375   GstD3D11Decoder *d3d11_decoder = nullptr;
376
377   GstAV1SequenceHeaderOBU seq_hdr;
378   GST_DXVA_PicParams_AV1 pic_params;
379
380   std::vector<GST_DXVA_Tile_AV1> tile_list;
381   std::vector<guint8> bitstream_buffer;
382
383   guint max_width = 0;
384   guint max_height = 0;
385   guint bitdepth = 0;
386 } GstD3D11AV1DecInner;
387 /* *INDENT-ON* */
388
389 typedef struct _GstD3D11AV1Dec
390 {
391   GstAV1Decoder parent;
392   GstD3D11AV1DecInner *inner;
393 } GstD3D11AV1Dec;
394
395 typedef struct _GstD3D11AV1DecClass
396 {
397   GstAV1DecoderClass parent_class;
398   GstD3D11DecoderSubClassData class_data;
399 } GstD3D11AV1DecClass;
400
401 static GstElementClass *parent_class = NULL;
402
403 #define GST_D3D11_AV1_DEC(object) ((GstD3D11AV1Dec *) (object))
404 #define GST_D3D11_AV1_DEC_GET_CLASS(object) \
405     (G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object),GstD3D11AV1DecClass))
406
407 static void gst_d3d11_av1_dec_get_property (GObject * object,
408     guint prop_id, GValue * value, GParamSpec * pspec);
409 static void gst_d3d11_av1_dec_finalize (GObject * object);
410
411 static void gst_d3d11_av1_dec_set_context (GstElement * element,
412     GstContext * context);
413
414 static gboolean gst_d3d11_av1_dec_open (GstVideoDecoder * decoder);
415 static gboolean gst_d3d11_av1_dec_close (GstVideoDecoder * decoder);
416 static gboolean gst_d3d11_av1_dec_negotiate (GstVideoDecoder * decoder);
417 static gboolean gst_d3d11_av1_dec_decide_allocation (GstVideoDecoder *
418     decoder, GstQuery * query);
419 static gboolean gst_d3d11_av1_dec_src_query (GstVideoDecoder * decoder,
420     GstQuery * query);
421 static gboolean gst_d3d11_av1_dec_sink_event (GstVideoDecoder * decoder,
422     GstEvent * event);
423
424 /* GstAV1Decoder */
425 static GstFlowReturn gst_d3d11_av1_dec_new_sequence (GstAV1Decoder * decoder,
426     const GstAV1SequenceHeaderOBU * seq_hdr);
427 static GstFlowReturn gst_d3d11_av1_dec_new_picture (GstAV1Decoder * decoder,
428     GstVideoCodecFrame * frame, GstAV1Picture * picture);
429 static GstAV1Picture *gst_d3d11_av1_dec_duplicate_picture (GstAV1Decoder *
430     decoder, GstAV1Picture * picture);
431 static GstFlowReturn gst_d3d11_av1_dec_start_picture (GstAV1Decoder * decoder,
432     GstAV1Picture * picture, GstAV1Dpb * dpb);
433 static GstFlowReturn gst_d3d11_av1_dec_decode_tile (GstAV1Decoder * decoder,
434     GstAV1Picture * picture, GstAV1Tile * tile);
435 static GstFlowReturn gst_d3d11_av1_dec_end_picture (GstAV1Decoder * decoder,
436     GstAV1Picture * picture);
437 static GstFlowReturn gst_d3d11_av1_dec_output_picture (GstAV1Decoder *
438     decoder, GstVideoCodecFrame * frame, GstAV1Picture * picture);
439
440 static void
441 gst_d3d11_av1_dec_class_init (GstD3D11AV1DecClass * klass, gpointer data)
442 {
443   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
444   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
445   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
446   GstAV1DecoderClass *av1decoder_class = GST_AV1_DECODER_CLASS (klass);
447   GstD3D11DecoderClassData *cdata = (GstD3D11DecoderClassData *) data;
448
449   gobject_class->get_property = gst_d3d11_av1_dec_get_property;
450   gobject_class->finalize = gst_d3d11_av1_dec_finalize;
451
452   element_class->set_context =
453       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_set_context);
454
455   parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
456   gst_d3d11_decoder_class_data_fill_subclass_data (cdata, &klass->class_data);
457
458   /**
459    * GstD3D11AV1Dec:adapter-luid:
460    *
461    * DXGI Adapter LUID for this elemenet
462    *
463    * Since: 1.20
464    */
465   gst_d3d11_decoder_proxy_class_init (element_class, cdata,
466       "Seungha Yang <seungha@centricular.com>");
467
468   decoder_class->open = GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_open);
469   decoder_class->close = GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_close);
470   decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_negotiate);
471   decoder_class->decide_allocation =
472       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_decide_allocation);
473   decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_src_query);
474   decoder_class->sink_event = GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_sink_event);
475
476   av1decoder_class->new_sequence =
477       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_new_sequence);
478   av1decoder_class->new_picture =
479       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_new_picture);
480   av1decoder_class->duplicate_picture =
481       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_duplicate_picture);
482   av1decoder_class->start_picture =
483       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_start_picture);
484   av1decoder_class->decode_tile =
485       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_decode_tile);
486   av1decoder_class->end_picture =
487       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_end_picture);
488   av1decoder_class->output_picture =
489       GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_output_picture);
490 }
491
492 static void
493 gst_d3d11_av1_dec_init (GstD3D11AV1Dec * self)
494 {
495   self->inner = new GstD3D11AV1DecInner ();
496 }
497
498 static void
499 gst_d3d11_av1_dec_get_property (GObject * object, guint prop_id,
500     GValue * value, GParamSpec * pspec)
501 {
502   GstD3D11AV1DecClass *klass = GST_D3D11_AV1_DEC_GET_CLASS (object);
503   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
504
505   gst_d3d11_decoder_proxy_get_property (object, prop_id, value, pspec, cdata);
506 }
507
508 static void
509 gst_d3d11_av1_dec_finalize (GObject * object)
510 {
511   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (object);
512
513   delete self->inner;
514
515   G_OBJECT_CLASS (parent_class)->finalize (object);
516 }
517
518 static void
519 gst_d3d11_av1_dec_set_context (GstElement * element, GstContext * context)
520 {
521   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (element);
522   GstD3D11AV1DecInner *inner = self->inner;
523   GstD3D11AV1DecClass *klass = GST_D3D11_AV1_DEC_GET_CLASS (self);
524   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
525
526   gst_d3d11_handle_set_context_for_adapter_luid (element,
527       context, cdata->adapter_luid, &inner->device);
528
529   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
530 }
531
532 static gboolean
533 gst_d3d11_av1_dec_open (GstVideoDecoder * decoder)
534 {
535   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
536   GstD3D11AV1DecInner *inner = self->inner;
537   GstD3D11AV1DecClass *klass = GST_D3D11_AV1_DEC_GET_CLASS (self);
538   GstD3D11DecoderSubClassData *cdata = &klass->class_data;
539
540   if (!gst_d3d11_decoder_proxy_open (decoder,
541           cdata, &inner->device, &inner->d3d11_decoder)) {
542     GST_ERROR_OBJECT (self, "Failed to open decoder");
543     return FALSE;
544   }
545
546   return TRUE;
547 }
548
549 static gboolean
550 gst_d3d11_av1_dec_close (GstVideoDecoder * decoder)
551 {
552   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
553   GstD3D11AV1DecInner *inner = self->inner;
554
555   gst_clear_object (&inner->d3d11_decoder);
556   gst_clear_object (&inner->device);
557
558   return TRUE;
559 }
560
561 static gboolean
562 gst_d3d11_av1_dec_negotiate (GstVideoDecoder * decoder)
563 {
564   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
565   GstD3D11AV1DecInner *inner = self->inner;
566
567   if (!gst_d3d11_decoder_negotiate (inner->d3d11_decoder, decoder))
568     return FALSE;
569
570   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
571 }
572
573 static gboolean
574 gst_d3d11_av1_dec_decide_allocation (GstVideoDecoder * decoder,
575     GstQuery * query)
576 {
577   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
578   GstD3D11AV1DecInner *inner = self->inner;
579
580   if (!gst_d3d11_decoder_decide_allocation (inner->d3d11_decoder,
581           decoder, query)) {
582     return FALSE;
583   }
584
585   return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
586       (decoder, query);
587 }
588
589 static gboolean
590 gst_d3d11_av1_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
591 {
592   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
593   GstD3D11AV1DecInner *inner = self->inner;
594
595   switch (GST_QUERY_TYPE (query)) {
596     case GST_QUERY_CONTEXT:
597       if (gst_d3d11_handle_context_query (GST_ELEMENT (decoder),
598               query, inner->device)) {
599         return TRUE;
600       }
601       break;
602     default:
603       break;
604   }
605
606   return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
607 }
608
609 static gboolean
610 gst_d3d11_av1_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
611 {
612   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
613   GstD3D11AV1DecInner *inner = self->inner;
614
615   switch (GST_EVENT_TYPE (event)) {
616     case GST_EVENT_FLUSH_START:
617       if (inner->d3d11_decoder)
618         gst_d3d11_decoder_set_flushing (inner->d3d11_decoder, decoder, TRUE);
619       break;
620     case GST_EVENT_FLUSH_STOP:
621       if (inner->d3d11_decoder)
622         gst_d3d11_decoder_set_flushing (inner->d3d11_decoder, decoder, FALSE);
623       break;
624     default:
625       break;
626   }
627
628   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
629 }
630
631 static GstFlowReturn
632 gst_d3d11_av1_dec_new_sequence (GstAV1Decoder * decoder,
633     const GstAV1SequenceHeaderOBU * seq_hdr)
634 {
635   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
636   GstD3D11AV1DecInner *inner = self->inner;
637   gboolean modified = FALSE;
638   guint max_width, max_height;
639
640   GST_LOG_OBJECT (self, "new sequence");
641
642   if (seq_hdr->seq_profile != GST_AV1_PROFILE_0) {
643     GST_WARNING_OBJECT (self, "Unsupported profile %d", seq_hdr->seq_profile);
644     return GST_FLOW_NOT_NEGOTIATED;
645   }
646
647   if (seq_hdr->num_planes != 3) {
648     GST_WARNING_OBJECT (self, "Monochrome is not supported");
649     return GST_FLOW_NOT_NEGOTIATED;
650   }
651
652   inner->seq_hdr = *seq_hdr;
653
654   if (inner->bitdepth != seq_hdr->bit_depth) {
655     GST_INFO_OBJECT (self, "Bitdepth changed %d -> %d", inner->bitdepth,
656         seq_hdr->bit_depth);
657     inner->bitdepth = seq_hdr->bit_depth;
658     modified = TRUE;
659   }
660
661   max_width = seq_hdr->max_frame_width_minus_1 + 1;
662   max_height = seq_hdr->max_frame_height_minus_1 + 1;
663
664   if (inner->max_width != max_width || inner->max_height != max_height) {
665     GST_INFO_OBJECT (self, "Resolution changed %dx%d -> %dx%d",
666         inner->max_width, inner->max_height, max_width, max_height);
667     inner->max_width = max_width;
668     inner->max_height = max_height;
669     modified = TRUE;
670   }
671
672   if (modified || !gst_d3d11_decoder_is_configured (inner->d3d11_decoder)) {
673     GstVideoInfo info;
674     GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
675
676     if (inner->bitdepth == 8) {
677       out_format = GST_VIDEO_FORMAT_NV12;
678     } else if (inner->bitdepth == 10) {
679       out_format = GST_VIDEO_FORMAT_P010_10LE;
680     } else {
681       GST_WARNING_OBJECT (self, "Invalid bit-depth %d", seq_hdr->bit_depth);
682       return GST_FLOW_NOT_NEGOTIATED;
683     }
684
685     gst_video_info_set_format (&info,
686         out_format, inner->max_width, inner->max_height);
687
688     if (!gst_d3d11_decoder_configure (inner->d3d11_decoder,
689             decoder->input_state, &info, (gint) inner->max_width,
690             (gint) inner->max_height, NUM_OUTPUT_VIEW)) {
691       GST_ERROR_OBJECT (self, "Failed to create decoder");
692       return GST_FLOW_NOT_NEGOTIATED;
693     }
694
695     if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (self))) {
696       GST_ERROR_OBJECT (self, "Failed to negotiate with downstream");
697       return GST_FLOW_NOT_NEGOTIATED;
698     }
699   }
700
701   return GST_FLOW_OK;
702 }
703
704 static GstFlowReturn
705 gst_d3d11_av1_dec_new_picture (GstAV1Decoder * decoder,
706     GstVideoCodecFrame * frame, GstAV1Picture * picture)
707 {
708   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
709   GstD3D11AV1DecInner *inner = self->inner;
710   GstBuffer *view_buffer;
711
712   view_buffer = gst_d3d11_decoder_get_output_view_buffer (inner->d3d11_decoder,
713       GST_VIDEO_DECODER (decoder));
714   if (!view_buffer) {
715     GST_DEBUG_OBJECT (self, "No available output view buffer");
716     return GST_FLOW_FLUSHING;
717   }
718
719   GST_LOG_OBJECT (self, "New output view buffer %" GST_PTR_FORMAT, view_buffer);
720
721   gst_av1_picture_set_user_data (picture,
722       view_buffer, (GDestroyNotify) gst_buffer_unref);
723
724   GST_LOG_OBJECT (self, "New AV1 picture %p", picture);
725
726   return GST_FLOW_OK;
727 }
728
729 static GstAV1Picture *
730 gst_d3d11_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
731     GstAV1Picture * picture)
732 {
733   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
734   GstBuffer *view_buffer;
735   GstAV1Picture *new_picture;
736
737   view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
738
739   if (!view_buffer) {
740     GST_ERROR_OBJECT (self, "Parent picture does not have output view buffer");
741     return NULL;
742   }
743
744   new_picture = gst_av1_picture_new ();
745
746   GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT,
747       view_buffer);
748
749   gst_av1_picture_set_user_data (new_picture,
750       gst_buffer_ref (view_buffer), (GDestroyNotify) gst_buffer_unref);
751
752   return new_picture;
753 }
754
755 static ID3D11VideoDecoderOutputView *
756 gst_d3d11_av1_dec_get_output_view_from_picture (GstD3D11AV1Dec * self,
757     GstAV1Picture * picture, guint8 * view_id)
758 {
759   GstD3D11AV1DecInner *inner = self->inner;
760   GstBuffer *view_buffer;
761   ID3D11VideoDecoderOutputView *view;
762
763   view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
764   if (!view_buffer) {
765     GST_DEBUG_OBJECT (self, "current picture does not have output view buffer");
766     return NULL;
767   }
768
769   view =
770       gst_d3d11_decoder_get_output_view_from_buffer (inner->d3d11_decoder,
771       view_buffer, view_id);
772   if (!view) {
773     GST_DEBUG_OBJECT (self, "current picture does not have output view handle");
774     return NULL;
775   }
776
777   return view;
778 }
779
780 static GstFlowReturn
781 gst_d3d11_av1_dec_start_picture (GstAV1Decoder * decoder,
782     GstAV1Picture * picture, GstAV1Dpb * dpb)
783 {
784   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
785   GstD3D11AV1DecInner *inner = self->inner;
786   const GstAV1SequenceHeaderOBU *seq_hdr = &inner->seq_hdr;
787   const GstAV1FrameHeaderOBU *frame_hdr = &picture->frame_hdr;
788   ID3D11VideoDecoderOutputView *view;
789   GST_DXVA_PicParams_AV1 *pic_params = &inner->pic_params;
790   guint8 view_id = 0xff;
791   guint i, j;
792
793   view = gst_d3d11_av1_dec_get_output_view_from_picture (self, picture,
794       &view_id);
795   if (!view) {
796     GST_ERROR_OBJECT (self, "current picture does not have output view handle");
797     return GST_FLOW_OK;
798   }
799
800   memset (pic_params, 0, sizeof (GST_DXVA_PicParams_AV1));
801
802   pic_params->width = frame_hdr->frame_width;
803   pic_params->height = frame_hdr->frame_height;
804
805   pic_params->max_width = seq_hdr->max_frame_width_minus_1 + 1;
806   pic_params->max_height = seq_hdr->max_frame_height_minus_1 + 1;
807
808   pic_params->CurrPicTextureIndex = view_id;
809   pic_params->superres_denom = frame_hdr->superres_denom;
810   pic_params->bitdepth = seq_hdr->bit_depth;
811   pic_params->seq_profile = seq_hdr->seq_profile;
812
813   /* TILES */
814   pic_params->tiles.cols = frame_hdr->tile_info.tile_cols;
815   pic_params->tiles.rows = frame_hdr->tile_info.tile_rows;
816   pic_params->tiles.context_update_id =
817       frame_hdr->tile_info.context_update_tile_id;
818
819   for (i = 0; i < pic_params->tiles.cols; i++) {
820     pic_params->tiles.widths[i] =
821         frame_hdr->tile_info.width_in_sbs_minus_1[i] + 1;
822   }
823
824   for (i = 0; i < pic_params->tiles.rows; i++) {
825     pic_params->tiles.heights[i] =
826         frame_hdr->tile_info.height_in_sbs_minus_1[i] + 1;
827   }
828
829   /* CODING TOOLS */
830   pic_params->coding.use_128x128_superblock = seq_hdr->use_128x128_superblock;
831   pic_params->coding.intra_edge_filter = seq_hdr->enable_filter_intra;
832   pic_params->coding.interintra_compound = seq_hdr->enable_interintra_compound;
833   pic_params->coding.masked_compound = seq_hdr->enable_masked_compound;
834   pic_params->coding.warped_motion = frame_hdr->allow_warped_motion;
835   pic_params->coding.dual_filter = seq_hdr->enable_dual_filter;
836   pic_params->coding.jnt_comp = seq_hdr->enable_jnt_comp;
837   pic_params->coding.screen_content_tools =
838       frame_hdr->allow_screen_content_tools;
839   pic_params->coding.integer_mv = frame_hdr->force_integer_mv;
840   pic_params->coding.cdef = seq_hdr->enable_cdef;
841   pic_params->coding.restoration = seq_hdr->enable_restoration;
842   pic_params->coding.film_grain = seq_hdr->film_grain_params_present;
843   pic_params->coding.intrabc = frame_hdr->allow_intrabc;
844   pic_params->coding.high_precision_mv = frame_hdr->allow_high_precision_mv;
845   pic_params->coding.switchable_motion_mode =
846       frame_hdr->is_motion_mode_switchable;
847   pic_params->coding.filter_intra = seq_hdr->enable_filter_intra;
848   pic_params->coding.disable_frame_end_update_cdf =
849       frame_hdr->disable_frame_end_update_cdf;
850   pic_params->coding.disable_cdf_update = frame_hdr->disable_cdf_update;
851   pic_params->coding.reference_mode = frame_hdr->reference_select;
852   pic_params->coding.skip_mode = frame_hdr->skip_mode_present;
853   pic_params->coding.reduced_tx_set = frame_hdr->reduced_tx_set;
854   pic_params->coding.superres = frame_hdr->use_superres;
855   pic_params->coding.tx_mode = frame_hdr->tx_mode;
856   pic_params->coding.use_ref_frame_mvs = frame_hdr->use_ref_frame_mvs;
857   pic_params->coding.enable_ref_frame_mvs = seq_hdr->enable_ref_frame_mvs;
858   pic_params->coding.reference_frame_update = 1;
859
860   /* FORMAT */
861   pic_params->format.frame_type = frame_hdr->frame_type;
862   pic_params->format.show_frame = frame_hdr->show_frame;
863   pic_params->format.showable_frame = frame_hdr->showable_frame;
864   pic_params->format.subsampling_x = seq_hdr->color_config.subsampling_x;
865   pic_params->format.subsampling_y = seq_hdr->color_config.subsampling_y;
866   pic_params->format.mono_chrome = seq_hdr->color_config.mono_chrome;
867
868   /* REFERENCES */
869   pic_params->primary_ref_frame = frame_hdr->primary_ref_frame;
870   pic_params->order_hint = frame_hdr->order_hint;
871   if (seq_hdr->enable_order_hint) {
872     pic_params->order_hint_bits = seq_hdr->order_hint_bits_minus_1 + 1;
873   } else {
874     pic_params->order_hint_bits = 0;
875   }
876
877   for (i = 0; i < GST_AV1_REFS_PER_FRAME; i++) {
878     if (dpb->pic_list[i]) {
879       GstAV1Picture *other_pic = dpb->pic_list[i];
880       const GstAV1GlobalMotionParams *gmp = &frame_hdr->global_motion_params;
881
882       pic_params->frame_refs[i].width = other_pic->frame_hdr.frame_width;
883       pic_params->frame_refs[i].height = other_pic->frame_hdr.frame_height;
884       for (j = 0; j < 6; j++) {
885         pic_params->frame_refs[i].wmmat[j] =
886             gmp->gm_params[GST_AV1_REF_LAST_FRAME + i][j];
887       }
888       pic_params->frame_refs[i].wminvalid =
889           (gmp->gm_type[GST_AV1_REF_LAST_FRAME + i] ==
890           GST_AV1_WARP_MODEL_IDENTITY);
891       pic_params->frame_refs[i].wmtype =
892           gmp->gm_type[GST_AV1_REF_LAST_FRAME + i];
893       pic_params->frame_refs[i].Index = frame_hdr->ref_frame_idx[i];
894     } else {
895       pic_params->frame_refs[i].Index = 0xff;
896     }
897   }
898
899   for (i = 0; i < GST_AV1_NUM_REF_FRAMES; i++) {
900     if (dpb->pic_list[i]) {
901       GstAV1Picture *other_pic = dpb->pic_list[i];
902       ID3D11VideoDecoderOutputView *other_view;
903       guint8 other_view_id = 0xff;
904
905       other_view = gst_d3d11_av1_dec_get_output_view_from_picture (self,
906           other_pic, &other_view_id);
907       if (!other_view) {
908         GST_ERROR_OBJECT (self,
909             "current picture does not have output view handle");
910         return GST_FLOW_ERROR;
911       }
912
913       pic_params->RefFrameMapTextureIndex[i] = other_view_id;
914     } else {
915       pic_params->RefFrameMapTextureIndex[i] = 0xff;
916     }
917   }
918
919   /* LOOP FILTER PARAMS */
920   pic_params->loop_filter.filter_level[0] =
921       frame_hdr->loop_filter_params.loop_filter_level[0];
922   pic_params->loop_filter.filter_level[1] =
923       frame_hdr->loop_filter_params.loop_filter_level[1];
924   pic_params->loop_filter.filter_level_u =
925       frame_hdr->loop_filter_params.loop_filter_level[2];
926   pic_params->loop_filter.filter_level_v =
927       frame_hdr->loop_filter_params.loop_filter_level[3];
928   pic_params->loop_filter.sharpness_level =
929       frame_hdr->loop_filter_params.loop_filter_sharpness;
930   pic_params->loop_filter.mode_ref_delta_enabled =
931       frame_hdr->loop_filter_params.loop_filter_delta_enabled;
932   pic_params->loop_filter.mode_ref_delta_update =
933       frame_hdr->loop_filter_params.loop_filter_delta_update;
934   pic_params->loop_filter.delta_lf_multi =
935       frame_hdr->loop_filter_params.delta_lf_multi;
936   pic_params->loop_filter.delta_lf_present =
937       frame_hdr->loop_filter_params.delta_lf_present;
938
939   for (i = 0; i < GST_AV1_TOTAL_REFS_PER_FRAME; i++) {
940     pic_params->loop_filter.ref_deltas[i] =
941         frame_hdr->loop_filter_params.loop_filter_ref_deltas[i];
942   }
943
944   for (i = 0; i < 2; i++) {
945     pic_params->loop_filter.mode_deltas[i] =
946         frame_hdr->loop_filter_params.loop_filter_mode_deltas[i];
947   }
948
949   pic_params->loop_filter.delta_lf_res =
950       frame_hdr->loop_filter_params.delta_lf_res;
951
952   for (i = 0; i < GST_AV1_MAX_NUM_PLANES; i++) {
953     pic_params->loop_filter.frame_restoration_type[i] =
954         frame_hdr->loop_restoration_params.frame_restoration_type[i];
955   }
956
957   if (frame_hdr->loop_restoration_params.uses_lr) {
958     pic_params->loop_filter.log2_restoration_unit_size[0] =
959         (6 + frame_hdr->loop_restoration_params.lr_unit_shift);
960     pic_params->loop_filter.log2_restoration_unit_size[1] =
961         pic_params->loop_filter.log2_restoration_unit_size[2] =
962         (6 + frame_hdr->loop_restoration_params.lr_unit_shift -
963         frame_hdr->loop_restoration_params.lr_uv_shift);
964   } else {
965     pic_params->loop_filter.log2_restoration_unit_size[0] =
966         pic_params->loop_filter.log2_restoration_unit_size[1] =
967         pic_params->loop_filter.log2_restoration_unit_size[2] = 8;
968   }
969
970   /* QUANTIZATION */
971   pic_params->quantization.delta_q_present =
972       frame_hdr->quantization_params.delta_q_present;
973   pic_params->quantization.delta_q_res =
974       frame_hdr->quantization_params.delta_q_res;
975   pic_params->quantization.base_qindex =
976       frame_hdr->quantization_params.base_q_idx;
977   pic_params->quantization.y_dc_delta_q =
978       frame_hdr->quantization_params.delta_q_y_dc;
979   pic_params->quantization.u_dc_delta_q =
980       frame_hdr->quantization_params.delta_q_u_dc;
981   pic_params->quantization.v_dc_delta_q =
982       frame_hdr->quantization_params.delta_q_v_dc;
983   pic_params->quantization.u_ac_delta_q =
984       frame_hdr->quantization_params.delta_q_u_ac;
985   pic_params->quantization.v_ac_delta_q =
986       frame_hdr->quantization_params.delta_q_v_ac;
987   if (frame_hdr->quantization_params.using_qmatrix) {
988     pic_params->quantization.qm_y = frame_hdr->quantization_params.qm_y;
989     pic_params->quantization.qm_u = frame_hdr->quantization_params.qm_u;
990     pic_params->quantization.qm_v = frame_hdr->quantization_params.qm_v;
991   } else {
992     pic_params->quantization.qm_y = 0xff;
993     pic_params->quantization.qm_u = 0xff;
994     pic_params->quantization.qm_v = 0xff;
995   }
996
997   /* Cdef params */
998   pic_params->cdef.damping = frame_hdr->cdef_params.cdef_damping - 3;
999   pic_params->cdef.bits = frame_hdr->cdef_params.cdef_bits;
1000
1001   for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
1002     guint8 secondary;
1003
1004     pic_params->cdef.y_strengths[i].primary =
1005         frame_hdr->cdef_params.cdef_y_pri_strength[i];
1006     secondary = frame_hdr->cdef_params.cdef_y_sec_strength[i];
1007     if (secondary == 4)
1008       secondary--;
1009     pic_params->cdef.y_strengths[i].secondary = secondary;
1010
1011     pic_params->cdef.uv_strengths[i].primary =
1012         frame_hdr->cdef_params.cdef_uv_pri_strength[i];
1013     secondary = frame_hdr->cdef_params.cdef_uv_sec_strength[i];
1014     if (secondary == 4)
1015       secondary--;
1016     pic_params->cdef.uv_strengths[i].secondary = secondary;
1017   }
1018
1019   pic_params->interp_filter = frame_hdr->interpolation_filter;
1020
1021   /* SEGMENTATION */
1022   pic_params->segmentation.enabled =
1023       frame_hdr->segmentation_params.segmentation_enabled;
1024   pic_params->segmentation.update_map =
1025       frame_hdr->segmentation_params.segmentation_update_map;
1026   pic_params->segmentation.update_data =
1027       frame_hdr->segmentation_params.segmentation_update_data;
1028   pic_params->segmentation.temporal_update =
1029       frame_hdr->segmentation_params.segmentation_temporal_update;
1030
1031   for (i = 0; i < GST_AV1_MAX_SEGMENTS; i++) {
1032     for (j = 0; j < GST_AV1_SEG_LVL_MAX; j++) {
1033       pic_params->segmentation.feature_mask[i].mask |=
1034           (frame_hdr->segmentation_params.feature_enabled[i][j] << j);
1035       pic_params->segmentation.feature_data[i][j] =
1036           frame_hdr->segmentation_params.feature_data[i][j];
1037     }
1038   }
1039
1040   /* FILM GRAIN */
1041   if (frame_hdr->film_grain_params.apply_grain) {
1042     pic_params->film_grain.apply_grain = 1;
1043     pic_params->film_grain.scaling_shift_minus8 =
1044         frame_hdr->film_grain_params.grain_scaling_minus_8;
1045     pic_params->film_grain.chroma_scaling_from_luma =
1046         frame_hdr->film_grain_params.chroma_scaling_from_luma;
1047     pic_params->film_grain.ar_coeff_lag =
1048         frame_hdr->film_grain_params.ar_coeff_lag;
1049     pic_params->film_grain.ar_coeff_shift_minus6 =
1050         frame_hdr->film_grain_params.ar_coeff_shift_minus_6;
1051     pic_params->film_grain.grain_scale_shift =
1052         frame_hdr->film_grain_params.grain_scale_shift;
1053     pic_params->film_grain.overlap_flag =
1054         frame_hdr->film_grain_params.overlap_flag;
1055     pic_params->film_grain.clip_to_restricted_range =
1056         frame_hdr->film_grain_params.clip_to_restricted_range;
1057     pic_params->film_grain.matrix_coeff_is_identity =
1058         (seq_hdr->color_config.matrix_coefficients == GST_AV1_MC_IDENTITY);
1059     pic_params->film_grain.grain_seed = frame_hdr->film_grain_params.grain_seed;
1060     for (i = 0; i < frame_hdr->film_grain_params.num_y_points && i < 14; i++) {
1061       pic_params->film_grain.scaling_points_y[i][0] =
1062           frame_hdr->film_grain_params.point_y_value[i];
1063       pic_params->film_grain.scaling_points_y[i][1] =
1064           frame_hdr->film_grain_params.point_y_scaling[i];
1065     }
1066     pic_params->film_grain.num_y_points =
1067         frame_hdr->film_grain_params.num_y_points;
1068
1069     for (i = 0; i < frame_hdr->film_grain_params.num_cb_points && i < 10; i++) {
1070       pic_params->film_grain.scaling_points_cb[i][0] =
1071           frame_hdr->film_grain_params.point_cb_value[i];
1072       pic_params->film_grain.scaling_points_cb[i][1] =
1073           frame_hdr->film_grain_params.point_cb_scaling[i];
1074     }
1075     pic_params->film_grain.num_cb_points =
1076         frame_hdr->film_grain_params.num_cb_points;
1077
1078     for (i = 0; i < frame_hdr->film_grain_params.num_cr_points && i < 10; i++) {
1079       pic_params->film_grain.scaling_points_cr[i][0] =
1080           frame_hdr->film_grain_params.point_cr_value[i];
1081       pic_params->film_grain.scaling_points_cr[i][1] =
1082           frame_hdr->film_grain_params.point_cr_scaling[i];
1083     }
1084     pic_params->film_grain.num_cr_points =
1085         frame_hdr->film_grain_params.num_cr_points;
1086
1087     for (i = 0; i < 24; i++) {
1088       pic_params->film_grain.ar_coeffs_y[i] =
1089           frame_hdr->film_grain_params.ar_coeffs_y_plus_128[i];
1090     }
1091
1092     for (i = 0; i < 25; i++) {
1093       pic_params->film_grain.ar_coeffs_cb[i] =
1094           frame_hdr->film_grain_params.ar_coeffs_cb_plus_128[i];
1095       pic_params->film_grain.ar_coeffs_cr[i] =
1096           frame_hdr->film_grain_params.ar_coeffs_cr_plus_128[i];
1097     }
1098
1099     pic_params->film_grain.cb_mult = frame_hdr->film_grain_params.cb_mult;
1100     pic_params->film_grain.cb_luma_mult =
1101         frame_hdr->film_grain_params.cb_luma_mult;
1102     pic_params->film_grain.cr_mult = frame_hdr->film_grain_params.cr_mult;
1103     pic_params->film_grain.cr_luma_mult =
1104         frame_hdr->film_grain_params.cr_luma_mult;
1105     pic_params->film_grain.cb_offset = frame_hdr->film_grain_params.cb_offset;
1106     pic_params->film_grain.cr_offset = frame_hdr->film_grain_params.cr_offset;
1107   }
1108
1109   inner->bitstream_buffer.resize (0);
1110   inner->tile_list.resize (0);
1111
1112   return GST_FLOW_OK;
1113 }
1114
1115 static GstFlowReturn
1116 gst_d3d11_av1_dec_decode_tile (GstAV1Decoder * decoder,
1117     GstAV1Picture * picture, GstAV1Tile * tile)
1118 {
1119   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
1120   GstD3D11AV1DecInner *inner = self->inner;
1121   GstAV1TileGroupOBU *tile_group = &tile->tile_group;
1122
1123   if (tile_group->num_tiles > inner->tile_list.size ())
1124     inner->tile_list.resize (tile_group->num_tiles);
1125
1126   g_assert (tile_group->tg_end < inner->tile_list.size ());
1127
1128   GST_LOG_OBJECT (self, "Decode tile, tile count %d (start: %d - end: %d)",
1129       tile_group->num_tiles, tile_group->tg_start, tile_group->tg_end);
1130
1131   for (guint i = tile_group->tg_start; i <= tile_group->tg_end; i++) {
1132     GST_DXVA_Tile_AV1 *dxva_tile = &inner->tile_list[i];
1133
1134     GST_TRACE_OBJECT (self,
1135         "Tile offset %d, size %d, row %d, col %d",
1136         tile_group->entry[i].tile_offset, tile_group->entry[i].tile_size,
1137         tile_group->entry[i].tile_row, tile_group->entry[i].tile_col);
1138
1139     dxva_tile->DataOffset = inner->bitstream_buffer.size () +
1140         tile_group->entry[i].tile_offset;
1141     dxva_tile->DataSize = tile_group->entry[i].tile_size;
1142     dxva_tile->row = tile_group->entry[i].tile_row;
1143     dxva_tile->column = tile_group->entry[i].tile_col;
1144     /* TODO: used for tile list OBU */
1145     dxva_tile->anchor_frame = 0xff;
1146   }
1147
1148   GST_TRACE_OBJECT (self, "OBU size %d", tile->obu.obu_size);
1149
1150   size_t pos = inner->bitstream_buffer.size ();
1151   inner->bitstream_buffer.resize (pos + tile->obu.obu_size);
1152
1153   memcpy (&inner->bitstream_buffer[0] + pos,
1154       tile->obu.data, tile->obu.obu_size);
1155
1156   return GST_FLOW_OK;
1157 }
1158
1159 static GstFlowReturn
1160 gst_d3d11_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
1161 {
1162   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
1163   GstD3D11AV1DecInner *inner = self->inner;
1164   ID3D11VideoDecoderOutputView *view;
1165   guint8 view_id = 0xff;
1166   size_t bitstream_buffer_size;
1167   size_t bitstream_pos;
1168   GstD3D11DecodeInputStreamArgs input_args;
1169
1170   if (inner->bitstream_buffer.empty () || inner->tile_list.empty ()) {
1171     GST_ERROR_OBJECT (self, "No bitstream buffer to submit");
1172     return GST_FLOW_ERROR;
1173   }
1174
1175   view = gst_d3d11_av1_dec_get_output_view_from_picture (self, picture,
1176       &view_id);
1177   if (!view) {
1178     GST_ERROR_OBJECT (self, "current picture does not have output view handle");
1179     return GST_FLOW_ERROR;
1180   }
1181
1182   memset (&input_args, 0, sizeof (GstD3D11DecodeInputStreamArgs));
1183
1184   bitstream_pos = inner->bitstream_buffer.size ();
1185   bitstream_buffer_size = GST_ROUND_UP_128 (bitstream_pos);
1186
1187   if (bitstream_buffer_size > bitstream_pos) {
1188     size_t padding = bitstream_buffer_size - bitstream_pos;
1189
1190     /* As per DXVA spec, total amount of bitstream buffer size should be
1191      * 128 bytes aligned. If actual data is not multiple of 128 bytes,
1192      * the last slice data needs to be zero-padded */
1193     inner->bitstream_buffer.resize (bitstream_buffer_size, 0);
1194
1195     GST_DXVA_Tile_AV1 & tile = inner->tile_list.back ();
1196     tile.DataSize += padding;
1197   }
1198
1199   input_args.picture_params = &inner->pic_params;
1200   input_args.picture_params_size = sizeof (GST_DXVA_PicParams_AV1);
1201   input_args.slice_control = &inner->tile_list[0];
1202   input_args.slice_control_size =
1203       sizeof (GST_DXVA_Tile_AV1) * inner->tile_list.size ();
1204   input_args.bitstream = &inner->bitstream_buffer[0];
1205   input_args.bitstream_size = inner->bitstream_buffer.size ();
1206
1207   if (!gst_d3d11_decoder_decode_frame (inner->d3d11_decoder, view, &input_args))
1208     return GST_FLOW_ERROR;
1209
1210   return GST_FLOW_OK;
1211 }
1212
1213 static GstFlowReturn
1214 gst_d3d11_av1_dec_output_picture (GstAV1Decoder * decoder,
1215     GstVideoCodecFrame * frame, GstAV1Picture * picture)
1216 {
1217   GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
1218   GstD3D11AV1DecInner *inner = self->inner;
1219   GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
1220   GstBuffer *view_buffer;
1221
1222   GST_LOG_OBJECT (self, "Outputting picture %p, %dx%d", picture,
1223       picture->frame_hdr.render_width, picture->frame_hdr.render_height);
1224
1225   view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
1226
1227   if (!view_buffer) {
1228     GST_ERROR_OBJECT (self, "Could not get output view");
1229     goto error;
1230   }
1231
1232   if (!gst_d3d11_decoder_process_output (inner->d3d11_decoder, vdec,
1233           picture->frame_hdr.render_width, picture->frame_hdr.render_height,
1234           view_buffer, &frame->output_buffer)) {
1235     GST_ERROR_OBJECT (self, "Failed to copy buffer");
1236     goto error;
1237   }
1238
1239   gst_av1_picture_unref (picture);
1240
1241   return gst_video_decoder_finish_frame (vdec, frame);
1242
1243 error:
1244   gst_av1_picture_unref (picture);
1245   gst_video_decoder_release_frame (vdec, frame);
1246
1247   return GST_FLOW_ERROR;
1248 }
1249
1250 void
1251 gst_d3d11_av1_dec_register (GstPlugin * plugin, GstD3D11Device * device,
1252     guint rank)
1253 {
1254   GType type;
1255   gchar *type_name;
1256   gchar *feature_name;
1257   guint index = 0;
1258   guint i;
1259   GTypeInfo type_info = {
1260     sizeof (GstD3D11AV1DecClass),
1261     NULL,
1262     NULL,
1263     (GClassInitFunc) gst_d3d11_av1_dec_class_init,
1264     NULL,
1265     NULL,
1266     sizeof (GstD3D11AV1Dec),
1267     0,
1268     (GInstanceInitFunc) gst_d3d11_av1_dec_init,
1269   };
1270   const GUID *profile_guid = NULL;
1271   GstCaps *sink_caps = NULL;
1272   GstCaps *src_caps = NULL;
1273   guint max_width = 0;
1274   guint max_height = 0;
1275   guint resolution;
1276   gboolean have_p010 = FALSE;
1277   gboolean have_gray = FALSE;
1278   gboolean have_gray10 = FALSE;
1279
1280   if (!gst_d3d11_decoder_get_supported_decoder_profile (device,
1281           GST_DXVA_CODEC_AV1, GST_VIDEO_FORMAT_NV12, &profile_guid)) {
1282     GST_INFO_OBJECT (device, "device does not support AV1 decoding");
1283     return;
1284   }
1285
1286   have_p010 = gst_d3d11_decoder_supports_format (device,
1287       profile_guid, DXGI_FORMAT_P010);
1288   have_gray = gst_d3d11_decoder_supports_format (device,
1289       profile_guid, DXGI_FORMAT_R8_UNORM);
1290   have_gray10 = gst_d3d11_decoder_supports_format (device,
1291       profile_guid, DXGI_FORMAT_R16_UNORM);
1292
1293   GST_INFO_OBJECT (device, "Decoder support P010: %d, R8: %d, R16: %d",
1294       have_p010, have_gray, have_gray10);
1295
1296   /* TODO: add test monochrome formats */
1297   for (i = 0; i < G_N_ELEMENTS (gst_dxva_resolutions); i++) {
1298     if (gst_d3d11_decoder_supports_resolution (device, profile_guid,
1299             DXGI_FORMAT_NV12, gst_dxva_resolutions[i].width,
1300             gst_dxva_resolutions[i].height)) {
1301       max_width = gst_dxva_resolutions[i].width;
1302       max_height = gst_dxva_resolutions[i].height;
1303
1304       GST_DEBUG_OBJECT (device,
1305           "device support resolution %dx%d", max_width, max_height);
1306     } else {
1307       break;
1308     }
1309   }
1310
1311   if (max_width == 0 || max_height == 0) {
1312     GST_WARNING_OBJECT (device, "Couldn't query supported resolution");
1313     return;
1314   }
1315
1316   sink_caps =
1317       gst_caps_from_string ("video/x-av1, "
1318       "alignment = (string) frame, profile = (string) main");
1319   src_caps = gst_caps_from_string ("video/x-raw("
1320       GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY "); video/x-raw");
1321
1322   if (have_p010) {
1323     GValue format_list = G_VALUE_INIT;
1324     GValue format_value = G_VALUE_INIT;
1325
1326     g_value_init (&format_list, GST_TYPE_LIST);
1327
1328     g_value_init (&format_value, G_TYPE_STRING);
1329     g_value_set_string (&format_value, "NV12");
1330     gst_value_list_append_and_take_value (&format_list, &format_value);
1331
1332     g_value_init (&format_value, G_TYPE_STRING);
1333     g_value_set_string (&format_value, "P010_10LE");
1334     gst_value_list_append_and_take_value (&format_list, &format_value);
1335
1336     gst_caps_set_value (src_caps, "format", &format_list);
1337     g_value_unset (&format_list);
1338   } else {
1339     gst_caps_set_simple (src_caps, "format", G_TYPE_STRING, "NV12", NULL);
1340   }
1341
1342   /* To cover both landscape and portrait, select max value */
1343   resolution = MAX (max_width, max_height);
1344   gst_caps_set_simple (sink_caps,
1345       "width", GST_TYPE_INT_RANGE, 1, resolution,
1346       "height", GST_TYPE_INT_RANGE, 1, resolution, NULL);
1347   gst_caps_set_simple (src_caps,
1348       "width", GST_TYPE_INT_RANGE, 1, resolution,
1349       "height", GST_TYPE_INT_RANGE, 1, resolution, NULL);
1350
1351   type_info.class_data =
1352       gst_d3d11_decoder_class_data_new (device, GST_DXVA_CODEC_AV1,
1353       sink_caps, src_caps);
1354
1355   type_name = g_strdup ("GstD3D11AV1Dec");
1356   feature_name = g_strdup ("d3d11av1dec");
1357
1358   while (g_type_from_name (type_name)) {
1359     index++;
1360     g_free (type_name);
1361     g_free (feature_name);
1362     type_name = g_strdup_printf ("GstD3D11AV1Device%dDec", index);
1363     feature_name = g_strdup_printf ("d3d11av1device%ddec", index);
1364   }
1365
1366   type = g_type_register_static (GST_TYPE_AV1_DECODER,
1367       type_name, &type_info, (GTypeFlags) 0);
1368
1369   /* make lower rank than default device */
1370   if (rank > 0 && index != 0)
1371     rank--;
1372
1373   if (index != 0)
1374     gst_element_type_set_skip_documentation (type);
1375
1376   if (!gst_element_register (plugin, feature_name, rank, type))
1377     GST_WARNING ("Failed to register plugin '%s'", type_name);
1378
1379   g_free (type_name);
1380   g_free (feature_name);
1381 }