• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef D3D12_VIDEO_TYPES_H
25 #define D3D12_VIDEO_TYPES_H
26 
27 #include <stdarg.h>
28 #include <memory>
29 #include <vector>
30 #include <functional>
31 
32 #include "pipe/p_context.h"
33 #include "pipe/p_video_codec.h"
34 #include "d3d12_fence.h"
35 #include "d3d12_debug.h"
36 
37 #include <directx/d3d12video.h>
38 #include <dxguids/dxguids.h>
39 
40 #include <wrl/client.h>
41 using Microsoft::WRL::ComPtr;
42 
43 #define D3D12_VIDEO_ANY_DECODER_ENABLED (VIDEO_CODEC_H264DEC || VIDEO_CODEC_H265DEC || VIDEO_CODEC_AV1DEC || VIDEO_CODEC_VP9DEC)
44 
45 #if !defined(_WIN32) || defined(_MSC_VER)
46 inline D3D12_VIDEO_DECODER_HEAP_DESC
GetDesc(ID3D12VideoDecoderHeap * heap)47 GetDesc(ID3D12VideoDecoderHeap *heap)
48 {
49    return heap->GetDesc();
50 }
51 #else
52 inline D3D12_VIDEO_DECODER_HEAP_DESC
GetDesc(ID3D12VideoDecoderHeap * heap)53 GetDesc(ID3D12VideoDecoderHeap *heap)
54 {
55    D3D12_VIDEO_DECODER_HEAP_DESC ret;
56    heap->GetDesc(&ret);
57    return ret;
58 }
59 #endif
60 
61 /* For CBR mode, to guarantee bitrate of generated stream complies with
62 * target bitrate (e.g. no over +/-10%), vbv_buffer_size should be same
63 * as target bitrate. Controlled by OS env var D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE
64 */
65 const bool D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE = debug_get_bool_option("D3D12_VIDEO_ENC_CBR_FORCE_VBV_EQUAL_BITRATE", false);
66 
67 /**
68  * This indicates how many in-flight encode commands can happen before blocking on the next request
69  */
70 const size_t D3D12_VIDEO_ENC_ASYNC_DEPTH = static_cast<size_t>(debug_get_num_option("D3D12_VIDEO_ENC_ASYNC_DEPTH", 8));
71 
72 const size_t D3D12_VIDEO_ENC_METADATA_BUFFERS_COUNT = static_cast<size_t>(debug_get_num_option("D3D12_VIDEO_ENC_METADATA_BUFFERS_COUNT", 2 * D3D12_VIDEO_ENC_ASYNC_DEPTH));
73 
74 constexpr unsigned int D3D12_VIDEO_H264_MB_IN_PIXELS = 16;
75 
76 constexpr size_t D3D12_DEFAULT_COMPBIT_STAGING_SIZE = (1024 /*1K*/ * 1024/*1MB*/) * 8/*8 MB*/; // 8MB
77 
78 /* If enabled, the D3D12 AV1 encoder will use always ...CONFIGURABLE_GRID_PARTITION mode */
79 /* If disabled, the D3D12 AV1 encoder will try to use ...UNIFORM_GRID_PARTITION first and then fallback to ...CONFIGURABLE_GRID_PARTITION if not possible */
80 const bool D3D12_VIDEO_FORCE_TILE_MODE = debug_get_bool_option("D3D12_VIDEO_FORCE_TILE_MODE", false);
81 
82 /**
83  * If enabled, the D3D12 AV1 encoder will insert a OBU_FRAME_HEADER with show_existing_frame = 1 after the current frame to show a previous
84  * show_frame = 0 encoded frame as reference and used by the current frame
85  */
86 const bool D3D12_VIDEO_AV1_INSERT_SHOW_EXISTING_FRAME_HEADER = debug_get_bool_option("D3D12_VIDEO_AV1_INSERT_SHOW_EXISTING_FRAME_HEADER", false);
87 
88 enum d3d12_video_decode_config_specific_flags
89 {
90    d3d12_video_decode_config_specific_flag_none              = 0,
91    d3d12_video_decode_config_specific_flag_alignment_height  = 1 << 12,   // set by accelerator
92    d3d12_video_decode_config_specific_flag_array_of_textures = 1 << 14,   // set by accelerator
93    d3d12_video_decode_config_specific_flag_reuse_decoder =
94       1 << 15,   // set by accelerator - This bit means that the decoder can be re-used with resolution change and bit
95                  // depth change (including profile GUID change from 8bit to 10bit and vice versa).
96    d3d12_video_decode_config_specific_flag_reference_only_textures_required = 1 << 30,   // custom created for WSL
97 };
98 
99 enum d3d12_video_decode_profile_type
100 {
101    d3d12_video_decode_profile_type_none,
102    d3d12_video_decode_profile_type_h264,
103    d3d12_video_decode_profile_type_hevc,
104    d3d12_video_decode_profile_type_av1,
105    d3d12_video_decode_profile_type_vp9,
106    d3d12_video_decode_profile_type_max_valid
107 };
108 
109 struct d3d12_video_decode_dpb_descriptor
110 {
111    DXGI_FORMAT Format          = DXGI_FORMAT_UNKNOWN;
112    uint64_t    Width           = 0;
113    uint32_t    Height          = 0;
114    bool        fArrayOfTexture = false;
115    bool        fReferenceOnly  = false;
116    uint16_t    dpbSize         = 0;
117    uint32_t    m_NodeMask      = 0;
118 };
119 
120 struct d3d12_video_decode_output_conversion_arguments
121 {
122    BOOL                  Enable;
123    DXGI_COLOR_SPACE_TYPE OutputColorSpace;
124    D3D12_VIDEO_SAMPLE    ReferenceInfo;
125    uint32_t              ReferenceFrameCount;
126 };
127 
128 void
129 d3d12_video_encoder_convert_from_d3d12_level_h264(D3D12_VIDEO_ENCODER_LEVELS_H264 level12,
130                                                   uint32_t &                      specLevel);
131 void
132 d3d12_video_encoder_convert_from_d3d12_level_hevc(D3D12_VIDEO_ENCODER_LEVELS_HEVC level12,
133                                                   uint32_t &                      specLevel);
134 void
135 d3d12_video_encoder_convert_d3d12_to_spec_level_av1(D3D12_VIDEO_ENCODER_AV1_LEVELS   level12,
136                                                     uint32_t &                      specLevel);
137 void
138 d3d12_video_encoder_convert_spec_to_d3d12_level_av1(uint32_t specLevel,
139                                                     D3D12_VIDEO_ENCODER_AV1_LEVELS& level12);
140 void
141 d3d12_video_encoder_convert_spec_to_d3d12_tier_av1(uint32_t specTier,
142                                                    D3D12_VIDEO_ENCODER_AV1_TIER & tier12);
143 D3D12_VIDEO_ENCODER_PROFILE_H264
144 d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_h264(enum pipe_video_profile profile);
145 D3D12_VIDEO_ENCODER_PROFILE_HEVC
146 d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_hevc(enum pipe_video_profile profile);
147 D3D12_VIDEO_ENCODER_AV1_PROFILE
148 d3d12_video_encoder_convert_profile_to_d3d12_enc_profile_av1(enum pipe_video_profile profile);
149 D3D12_VIDEO_ENCODER_CODEC
150 d3d12_video_encoder_convert_codec_to_d3d12_enc_codec(enum pipe_video_profile profile);
151 GUID
152 d3d12_video_decoder_convert_pipe_video_profile_to_d3d12_profile(enum pipe_video_profile profile);
153 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE
154 d3d12_video_encoder_convert_pixel_size_hevc_to_12tusize(const uint32_t& TUSize);
155 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE
156 d3d12_video_encoder_convert_pixel_size_hevc_to_12cusize(const uint32_t& cuSize);
157 uint8_t
158 d3d12_video_encoder_convert_12cusize_to_pixel_size_hevc(const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_CUSIZE& cuSize);
159 uint8_t
160 d3d12_video_encoder_convert_12tusize_to_pixel_size_hevc(const D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_HEVC_TUSIZE& TUSize);
161 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT
162 ConvertHEVCSupportFromProfile(D3D12_VIDEO_ENCODER_PROFILE_HEVC profile, D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_HEVC1* pSupport1);
163 D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA
164 ConvertHEVCPicParamsFromProfile(D3D12_VIDEO_ENCODER_PROFILE_HEVC profile, D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_HEVC1* pPictureParams1);
165 
166 DEFINE_ENUM_FLAG_OPERATORS(pipe_enc_feature);
167 DEFINE_ENUM_FLAG_OPERATORS(pipe_h265_enc_pred_direction);
168 DEFINE_ENUM_FLAG_OPERATORS(codec_unit_location_flags);
169 DEFINE_ENUM_FLAG_OPERATORS(pipe_video_feedback_encode_result_flags);
170 DEFINE_ENUM_FLAG_OPERATORS(pipe_video_feedback_metadata_type);
171 
172 #define D3D12_VIDEO_ENC_H264_MAX_TEMPORAL_LAYERS 4
173 #define D3D12_VIDEO_ENC_HEVC_MAX_TEMPORAL_LAYERS 4
174 #define D3D12_VIDEO_ENC_AV1_MAX_TEMPORAL_LAYERS 1
175 #define D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS 4
176 
177 static_assert(D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS >= D3D12_VIDEO_ENC_H264_MAX_TEMPORAL_LAYERS, "Increase size of D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS");
178 static_assert(D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS >= D3D12_VIDEO_ENC_HEVC_MAX_TEMPORAL_LAYERS, "Increase size of D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS");
179 static_assert(D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS >= D3D12_VIDEO_ENC_AV1_MAX_TEMPORAL_LAYERS, "Increase size of D3D12_VIDEO_ENC_MAX_RATE_CONTROL_TEMPORAL_LAYERS");
180 
181 static_assert(ARRAY_SIZE(pipe_h264_enc_picture_desc::rate_ctrl) >= D3D12_VIDEO_ENC_H264_MAX_TEMPORAL_LAYERS, "Increase size of pipe_h264_enc_picture_desc::rate_ctrl[] array");
182 static_assert(ARRAY_SIZE(pipe_h265_enc_picture_desc::rc) >= D3D12_VIDEO_ENC_HEVC_MAX_TEMPORAL_LAYERS, "Increase size of pipe_h265_enc_picture_desc::rc[] array");
183 static_assert(ARRAY_SIZE(pipe_av1_enc_picture_desc::rc) >= D3D12_VIDEO_ENC_AV1_MAX_TEMPORAL_LAYERS, "Increase size of pipe_h265_enc_picture_desc::rc[] array");
184 #endif
185