2 * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
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.
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.
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.
21 * SECTION:element-d3d11av1dec
24 * A Direct3D11/DXVA based AV1 video decoder
26 * ## Example launch line
28 * gst-launch-1.0 filesrc location=/path/to/av1/file ! parsebin ! d3d11av1dec ! d3d11videosink
39 #include "gstd3d11av1dec.h"
41 #include <gst/codecs/gstav1decoder.h>
45 /* HACK: to expose dxva data structure on UWP */
46 #ifdef WINAPI_PARTITION_DESKTOP
47 #undef WINAPI_PARTITION_DESKTOP
49 #define WINAPI_PARTITION_DESKTOP 1
53 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_av1_dec_debug);
54 #define GST_CAT_DEFAULT gst_d3d11_av1_dec_debug
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
62 /* AV1 picture entry data structure */
63 typedef struct _GST_DXVA_PicEntry_AV1
68 // Global motion parameters
78 UCHAR GlobalMotionFlags;
82 UINT16 Reserved16Bits;
84 } GST_DXVA_PicEntry_AV1;
86 /* AV1 picture parameters structure */
87 typedef struct _GST_DXVA_PicParams_AV1
95 UCHAR CurrPicTextureIndex;
105 USHORT context_update_id;
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;
122 UINT screen_content_tools:1;
128 UINT high_precision_mv:1;
129 UINT switchable_motion_mode:1;
131 UINT disable_frame_end_update_cdf:1;
132 UINT disable_cdf_update:1;
133 UINT reference_mode:1;
135 UINT reduced_tx_set:1;
138 UINT use_ref_frame_mvs:1;
139 UINT enable_ref_frame_mvs:1;
140 UINT reference_frame_update:1;
143 UINT32 CodingParamToolFlags;
146 // Format & Picture Info flags
153 UCHAR showable_frame:1;
154 UCHAR subsampling_x:1;
155 UCHAR subsampling_y:1;
159 UCHAR FormatAndPictureInfoFlags;
163 UCHAR primary_ref_frame;
165 UCHAR order_hint_bits;
167 GST_DXVA_PicEntry_AV1 frame_refs[7];
168 UCHAR RefFrameMapTextureIndex[8];
170 // Loop filter parameters
173 UCHAR filter_level[2];
174 UCHAR filter_level_u;
175 UCHAR filter_level_v;
177 UCHAR sharpness_level;
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;
193 UCHAR frame_restoration_type[3];
194 USHORT log2_restoration_unit_size[3];
195 UINT16 Reserved16Bits;
205 UCHAR delta_q_present:1;
222 UINT16 Reserved16Bits;
273 UCHAR temporal_update:1;
278 UCHAR Reserved24Bits[3];
296 SHORT feature_data[8][8];
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;
321 UCHAR scaling_points_y[14][2];
323 UCHAR scaling_points_cb[10][2];
325 UCHAR scaling_points_cr[10][2];
327 UCHAR ar_coeffs_y[24];
328 UCHAR ar_coeffs_cb[25];
329 UCHAR ar_coeffs_cr[25];
340 UINT StatusReportFeedbackNumber;
341 } GST_DXVA_PicParams_AV1;
343 /* AV1 tile structure */
344 typedef struct _GST_DXVA_Tile_AV1
350 UINT16 Reserved16Bits;
355 /* AV1 status reporting data structure */
356 typedef struct _GST_DXVA_Status_AV1
358 UINT StatusReportFeedbackNumber;
359 GST_DXVA_PicEntry_AV1 CurrPic;
363 USHORT NumMbsAffected;
364 } GST_DXVA_Status_AV1;
368 /* reference list 8 + 4 margin */
369 #define NUM_OUTPUT_VIEW 12
372 typedef struct _GstD3D11AV1DecInner
374 GstD3D11Device *device = nullptr;
375 GstD3D11Decoder *d3d11_decoder = nullptr;
377 GstAV1SequenceHeaderOBU seq_hdr;
378 GST_DXVA_PicParams_AV1 pic_params;
380 std::vector<GST_DXVA_Tile_AV1> tile_list;
381 std::vector<guint8> bitstream_buffer;
384 guint max_height = 0;
386 } GstD3D11AV1DecInner;
389 typedef struct _GstD3D11AV1Dec
391 GstAV1Decoder parent;
392 GstD3D11AV1DecInner *inner;
395 typedef struct _GstD3D11AV1DecClass
397 GstAV1DecoderClass parent_class;
398 GstD3D11DecoderSubClassData class_data;
399 } GstD3D11AV1DecClass;
401 static GstElementClass *parent_class = NULL;
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))
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);
411 static void gst_d3d11_av1_dec_set_context (GstElement * element,
412 GstContext * context);
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,
421 static gboolean gst_d3d11_av1_dec_sink_event (GstVideoDecoder * decoder,
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);
441 gst_d3d11_av1_dec_class_init (GstD3D11AV1DecClass * klass, gpointer data)
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;
449 gobject_class->get_property = gst_d3d11_av1_dec_get_property;
450 gobject_class->finalize = gst_d3d11_av1_dec_finalize;
452 element_class->set_context =
453 GST_DEBUG_FUNCPTR (gst_d3d11_av1_dec_set_context);
455 parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
456 gst_d3d11_decoder_class_data_fill_subclass_data (cdata, &klass->class_data);
459 * GstD3D11AV1Dec:adapter-luid:
461 * DXGI Adapter LUID for this elemenet
465 gst_d3d11_decoder_proxy_class_init (element_class, cdata,
466 "Seungha Yang <seungha@centricular.com>");
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);
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);
493 gst_d3d11_av1_dec_init (GstD3D11AV1Dec * self)
495 self->inner = new GstD3D11AV1DecInner ();
499 gst_d3d11_av1_dec_get_property (GObject * object, guint prop_id,
500 GValue * value, GParamSpec * pspec)
502 GstD3D11AV1DecClass *klass = GST_D3D11_AV1_DEC_GET_CLASS (object);
503 GstD3D11DecoderSubClassData *cdata = &klass->class_data;
505 gst_d3d11_decoder_proxy_get_property (object, prop_id, value, pspec, cdata);
509 gst_d3d11_av1_dec_finalize (GObject * object)
511 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (object);
515 G_OBJECT_CLASS (parent_class)->finalize (object);
519 gst_d3d11_av1_dec_set_context (GstElement * element, GstContext * context)
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;
526 gst_d3d11_handle_set_context_for_adapter_luid (element,
527 context, cdata->adapter_luid, &inner->device);
529 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
533 gst_d3d11_av1_dec_open (GstVideoDecoder * decoder)
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;
540 if (!gst_d3d11_decoder_proxy_open (decoder,
541 cdata, &inner->device, &inner->d3d11_decoder)) {
542 GST_ERROR_OBJECT (self, "Failed to open decoder");
550 gst_d3d11_av1_dec_close (GstVideoDecoder * decoder)
552 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
553 GstD3D11AV1DecInner *inner = self->inner;
555 gst_clear_object (&inner->d3d11_decoder);
556 gst_clear_object (&inner->device);
562 gst_d3d11_av1_dec_negotiate (GstVideoDecoder * decoder)
564 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
565 GstD3D11AV1DecInner *inner = self->inner;
567 if (!gst_d3d11_decoder_negotiate (inner->d3d11_decoder, decoder))
570 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
574 gst_d3d11_av1_dec_decide_allocation (GstVideoDecoder * decoder,
577 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
578 GstD3D11AV1DecInner *inner = self->inner;
580 if (!gst_d3d11_decoder_decide_allocation (inner->d3d11_decoder,
585 return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
590 gst_d3d11_av1_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
592 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
593 GstD3D11AV1DecInner *inner = self->inner;
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)) {
606 return GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
610 gst_d3d11_av1_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
612 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
613 GstD3D11AV1DecInner *inner = self->inner;
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);
620 case GST_EVENT_FLUSH_STOP:
621 if (inner->d3d11_decoder)
622 gst_d3d11_decoder_set_flushing (inner->d3d11_decoder, decoder, FALSE);
628 return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
632 gst_d3d11_av1_dec_new_sequence (GstAV1Decoder * decoder,
633 const GstAV1SequenceHeaderOBU * seq_hdr)
635 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
636 GstD3D11AV1DecInner *inner = self->inner;
637 gboolean modified = FALSE;
638 guint max_width, max_height;
640 GST_LOG_OBJECT (self, "new sequence");
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;
647 if (seq_hdr->num_planes != 3) {
648 GST_WARNING_OBJECT (self, "Monochrome is not supported");
649 return GST_FLOW_NOT_NEGOTIATED;
652 inner->seq_hdr = *seq_hdr;
654 if (inner->bitdepth != seq_hdr->bit_depth) {
655 GST_INFO_OBJECT (self, "Bitdepth changed %d -> %d", inner->bitdepth,
657 inner->bitdepth = seq_hdr->bit_depth;
661 max_width = seq_hdr->max_frame_width_minus_1 + 1;
662 max_height = seq_hdr->max_frame_height_minus_1 + 1;
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;
672 if (modified || !gst_d3d11_decoder_is_configured (inner->d3d11_decoder)) {
674 GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
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;
681 GST_WARNING_OBJECT (self, "Invalid bit-depth %d", seq_hdr->bit_depth);
682 return GST_FLOW_NOT_NEGOTIATED;
685 gst_video_info_set_format (&info,
686 out_format, inner->max_width, inner->max_height);
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;
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;
705 gst_d3d11_av1_dec_new_picture (GstAV1Decoder * decoder,
706 GstVideoCodecFrame * frame, GstAV1Picture * picture)
708 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
709 GstD3D11AV1DecInner *inner = self->inner;
710 GstBuffer *view_buffer;
712 view_buffer = gst_d3d11_decoder_get_output_view_buffer (inner->d3d11_decoder,
713 GST_VIDEO_DECODER (decoder));
715 GST_DEBUG_OBJECT (self, "No available output view buffer");
716 return GST_FLOW_FLUSHING;
719 GST_LOG_OBJECT (self, "New output view buffer %" GST_PTR_FORMAT, view_buffer);
721 gst_av1_picture_set_user_data (picture,
722 view_buffer, (GDestroyNotify) gst_buffer_unref);
724 GST_LOG_OBJECT (self, "New AV1 picture %p", picture);
729 static GstAV1Picture *
730 gst_d3d11_av1_dec_duplicate_picture (GstAV1Decoder * decoder,
731 GstAV1Picture * picture)
733 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
734 GstBuffer *view_buffer;
735 GstAV1Picture *new_picture;
737 view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
740 GST_ERROR_OBJECT (self, "Parent picture does not have output view buffer");
744 new_picture = gst_av1_picture_new ();
746 GST_LOG_OBJECT (self, "Duplicate output with buffer %" GST_PTR_FORMAT,
749 gst_av1_picture_set_user_data (new_picture,
750 gst_buffer_ref (view_buffer), (GDestroyNotify) gst_buffer_unref);
755 static ID3D11VideoDecoderOutputView *
756 gst_d3d11_av1_dec_get_output_view_from_picture (GstD3D11AV1Dec * self,
757 GstAV1Picture * picture, guint8 * view_id)
759 GstD3D11AV1DecInner *inner = self->inner;
760 GstBuffer *view_buffer;
761 ID3D11VideoDecoderOutputView *view;
763 view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
765 GST_DEBUG_OBJECT (self, "current picture does not have output view buffer");
770 gst_d3d11_decoder_get_output_view_from_buffer (inner->d3d11_decoder,
771 view_buffer, view_id);
773 GST_DEBUG_OBJECT (self, "current picture does not have output view handle");
781 gst_d3d11_av1_dec_start_picture (GstAV1Decoder * decoder,
782 GstAV1Picture * picture, GstAV1Dpb * dpb)
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;
793 view = gst_d3d11_av1_dec_get_output_view_from_picture (self, picture,
796 GST_ERROR_OBJECT (self, "current picture does not have output view handle");
800 memset (pic_params, 0, sizeof (GST_DXVA_PicParams_AV1));
802 pic_params->width = frame_hdr->frame_width;
803 pic_params->height = frame_hdr->frame_height;
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;
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;
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;
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;
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;
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;
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;
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;
874 pic_params->order_hint_bits = 0;
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;
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];
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];
895 pic_params->frame_refs[i].Index = 0xff;
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;
905 other_view = gst_d3d11_av1_dec_get_output_view_from_picture (self,
906 other_pic, &other_view_id);
908 GST_ERROR_OBJECT (self,
909 "current picture does not have output view handle");
910 return GST_FLOW_ERROR;
913 pic_params->RefFrameMapTextureIndex[i] = other_view_id;
915 pic_params->RefFrameMapTextureIndex[i] = 0xff;
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;
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];
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];
949 pic_params->loop_filter.delta_lf_res =
950 frame_hdr->loop_filter_params.delta_lf_res;
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];
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);
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;
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;
992 pic_params->quantization.qm_y = 0xff;
993 pic_params->quantization.qm_u = 0xff;
994 pic_params->quantization.qm_v = 0xff;
998 pic_params->cdef.damping = frame_hdr->cdef_params.cdef_damping - 3;
999 pic_params->cdef.bits = frame_hdr->cdef_params.cdef_bits;
1001 for (i = 0; i < GST_AV1_CDEF_MAX; i++) {
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];
1009 pic_params->cdef.y_strengths[i].secondary = secondary;
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];
1016 pic_params->cdef.uv_strengths[i].secondary = secondary;
1019 pic_params->interp_filter = frame_hdr->interpolation_filter;
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;
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];
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];
1066 pic_params->film_grain.num_y_points =
1067 frame_hdr->film_grain_params.num_y_points;
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];
1075 pic_params->film_grain.num_cb_points =
1076 frame_hdr->film_grain_params.num_cb_points;
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];
1084 pic_params->film_grain.num_cr_points =
1085 frame_hdr->film_grain_params.num_cr_points;
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];
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];
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;
1109 inner->bitstream_buffer.resize (0);
1110 inner->tile_list.resize (0);
1115 static GstFlowReturn
1116 gst_d3d11_av1_dec_decode_tile (GstAV1Decoder * decoder,
1117 GstAV1Picture * picture, GstAV1Tile * tile)
1119 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
1120 GstD3D11AV1DecInner *inner = self->inner;
1121 GstAV1TileGroupOBU *tile_group = &tile->tile_group;
1123 if (tile_group->num_tiles > inner->tile_list.size ())
1124 inner->tile_list.resize (tile_group->num_tiles);
1126 g_assert (tile_group->tg_end < inner->tile_list.size ());
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);
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];
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);
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;
1148 GST_TRACE_OBJECT (self, "OBU size %d", tile->obu.obu_size);
1150 size_t pos = inner->bitstream_buffer.size ();
1151 inner->bitstream_buffer.resize (pos + tile->obu.obu_size);
1153 memcpy (&inner->bitstream_buffer[0] + pos,
1154 tile->obu.data, tile->obu.obu_size);
1159 static GstFlowReturn
1160 gst_d3d11_av1_dec_end_picture (GstAV1Decoder * decoder, GstAV1Picture * picture)
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;
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;
1175 view = gst_d3d11_av1_dec_get_output_view_from_picture (self, picture,
1178 GST_ERROR_OBJECT (self, "current picture does not have output view handle");
1179 return GST_FLOW_ERROR;
1182 memset (&input_args, 0, sizeof (GstD3D11DecodeInputStreamArgs));
1184 bitstream_pos = inner->bitstream_buffer.size ();
1185 bitstream_buffer_size = GST_ROUND_UP_128 (bitstream_pos);
1187 if (bitstream_buffer_size > bitstream_pos) {
1188 size_t padding = bitstream_buffer_size - bitstream_pos;
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);
1195 GST_DXVA_Tile_AV1 & tile = inner->tile_list.back ();
1196 tile.DataSize += padding;
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 ();
1207 if (!gst_d3d11_decoder_decode_frame (inner->d3d11_decoder, view, &input_args))
1208 return GST_FLOW_ERROR;
1213 static GstFlowReturn
1214 gst_d3d11_av1_dec_output_picture (GstAV1Decoder * decoder,
1215 GstVideoCodecFrame * frame, GstAV1Picture * picture)
1217 GstD3D11AV1Dec *self = GST_D3D11_AV1_DEC (decoder);
1218 GstD3D11AV1DecInner *inner = self->inner;
1219 GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
1220 GstBuffer *view_buffer;
1222 GST_LOG_OBJECT (self, "Outputting picture %p, %dx%d", picture,
1223 picture->frame_hdr.render_width, picture->frame_hdr.render_height);
1225 view_buffer = (GstBuffer *) gst_av1_picture_get_user_data (picture);
1228 GST_ERROR_OBJECT (self, "Could not get output view");
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");
1239 gst_av1_picture_unref (picture);
1241 return gst_video_decoder_finish_frame (vdec, frame);
1244 gst_av1_picture_unref (picture);
1245 gst_video_decoder_release_frame (vdec, frame);
1247 return GST_FLOW_ERROR;
1251 gst_d3d11_av1_dec_register (GstPlugin * plugin, GstD3D11Device * device,
1256 gchar *feature_name;
1259 GTypeInfo type_info = {
1260 sizeof (GstD3D11AV1DecClass),
1263 (GClassInitFunc) gst_d3d11_av1_dec_class_init,
1266 sizeof (GstD3D11AV1Dec),
1268 (GInstanceInitFunc) gst_d3d11_av1_dec_init,
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;
1276 gboolean have_p010 = FALSE;
1277 gboolean have_gray = FALSE;
1278 gboolean have_gray10 = FALSE;
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");
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);
1293 GST_INFO_OBJECT (device, "Decoder support P010: %d, R8: %d, R16: %d",
1294 have_p010, have_gray, have_gray10);
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;
1304 GST_DEBUG_OBJECT (device,
1305 "device support resolution %dx%d", max_width, max_height);
1311 if (max_width == 0 || max_height == 0) {
1312 GST_WARNING_OBJECT (device, "Couldn't query supported resolution");
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");
1323 GValue format_list = G_VALUE_INIT;
1324 GValue format_value = G_VALUE_INIT;
1326 g_value_init (&format_list, GST_TYPE_LIST);
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);
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);
1336 gst_caps_set_value (src_caps, "format", &format_list);
1337 g_value_unset (&format_list);
1339 gst_caps_set_simple (src_caps, "format", G_TYPE_STRING, "NV12", NULL);
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);
1351 type_info.class_data =
1352 gst_d3d11_decoder_class_data_new (device, GST_DXVA_CODEC_AV1,
1353 sink_caps, src_caps);
1355 type_name = g_strdup ("GstD3D11AV1Dec");
1356 feature_name = g_strdup ("d3d11av1dec");
1358 while (g_type_from_name (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);
1366 type = g_type_register_static (GST_TYPE_AV1_DECODER,
1367 type_name, &type_info, (GTypeFlags) 0);
1369 /* make lower rank than default device */
1370 if (rank > 0 && index != 0)
1374 gst_element_type_set_skip_documentation (type);
1376 if (!gst_element_register (plugin, feature_name, rank, type))
1377 GST_WARNING ("Failed to register plugin '%s'", type_name);
1380 g_free (feature_name);