1 /**************************************************************************
2 *
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_handle_table.h"
29 #include "util/u_video.h"
30 #include "va_private.h"
31
32 #include "vl/vl_vlc.h"
33 #include "vl/vl_rbsp.h"
34
35 enum HEVCNALUnitType {
36 HEVC_NAL_VPS = 32,
37 HEVC_NAL_SPS = 33,
38 HEVC_NAL_PPS = 34,
39 };
40
41 VAStatus
vlVaHandleVAEncPictureParameterBufferTypeHEVC(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)42 vlVaHandleVAEncPictureParameterBufferTypeHEVC(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
43 {
44 VAEncPictureParameterBufferHEVC *h265;
45 vlVaBuffer *coded_buf;
46 int i;
47
48 h265 = buf->data;
49 context->desc.h265enc.decoded_curr_pic = h265->decoded_curr_pic.picture_id;
50
51 for (i = 0; i < 15; i++)
52 context->desc.h265enc.reference_frames[i] = h265->reference_frames[i].picture_id;
53
54 context->desc.h265enc.pic_order_cnt = h265->decoded_curr_pic.pic_order_cnt;
55 coded_buf = handle_table_get(drv->htab, h265->coded_buf);
56
57 if (!coded_buf->derived_surface.resource)
58 coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
59 PIPE_USAGE_STREAM, coded_buf->size);
60
61 context->coded_buf = coded_buf;
62 context->desc.h265enc.pic.log2_parallel_merge_level_minus2 = h265->log2_parallel_merge_level_minus2;
63 context->desc.h265enc.pic.nal_unit_type = h265->nal_unit_type;
64 context->desc.h265enc.rc.quant_i_frames = h265->pic_init_qp;
65
66 switch(h265->pic_fields.bits.coding_type) {
67 case 1:
68 if (h265->pic_fields.bits.idr_pic_flag)
69 context->desc.h265enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_IDR;
70 else
71 context->desc.h265enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_I;
72 break;
73 case 2:
74 context->desc.h265enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
75 break;
76 case 3:
77 case 4:
78 case 5:
79 return VA_STATUS_ERROR_UNIMPLEMENTED; //no b frame support
80 break;
81 }
82
83 context->desc.h265enc.pic.constrained_intra_pred_flag = h265->pic_fields.bits.constrained_intra_pred_flag;
84
85 _mesa_hash_table_insert(context->desc.h265enc.frame_idx,
86 UINT_TO_PTR(h265->decoded_curr_pic.picture_id + 1),
87 UINT_TO_PTR(context->desc.h265enc.frame_num));
88
89 return VA_STATUS_SUCCESS;
90 }
91
92 VAStatus
vlVaHandleVAEncSliceParameterBufferTypeHEVC(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)93 vlVaHandleVAEncSliceParameterBufferTypeHEVC(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
94 {
95 VAEncSliceParameterBufferHEVC *h265;
96
97 h265 = buf->data;
98 context->desc.h265enc.ref_idx_l0 = VA_INVALID_ID;
99 context->desc.h265enc.ref_idx_l1 = VA_INVALID_ID;
100
101 for (int i = 0; i < 15; i++) {
102 if (h265->ref_pic_list0[i].picture_id != VA_INVALID_ID) {
103 if (context->desc.h265enc.ref_idx_l0 == VA_INVALID_ID)
104 context->desc.h265enc.ref_idx_l0 = PTR_TO_UINT(util_hash_table_get(context->desc.h265enc.frame_idx,
105 UINT_TO_PTR(h265->ref_pic_list0[i].picture_id + 1)));
106 }
107 if (h265->ref_pic_list1[i].picture_id != VA_INVALID_ID && h265->slice_type == 1) {
108 if (context->desc.h265enc.ref_idx_l1 == VA_INVALID_ID)
109 context->desc.h265enc.ref_idx_l1 = PTR_TO_UINT(util_hash_table_get(context->desc.h265enc.frame_idx,
110 UINT_TO_PTR(h265->ref_pic_list1[i].picture_id + 1)));
111 }
112 }
113
114 context->desc.h265enc.slice.max_num_merge_cand = h265->max_num_merge_cand;
115 context->desc.h265enc.slice.slice_cb_qp_offset = h265->slice_cb_qp_offset;
116 context->desc.h265enc.slice.slice_cr_qp_offset = h265->slice_cr_qp_offset;
117 context->desc.h265enc.slice.slice_beta_offset_div2 = h265->slice_beta_offset_div2;
118 context->desc.h265enc.slice.slice_tc_offset_div2 = h265->slice_tc_offset_div2;
119 context->desc.h265enc.slice.cabac_init_flag = h265->slice_fields.bits.cabac_init_flag;
120 context->desc.h265enc.slice.slice_deblocking_filter_disabled_flag = h265->slice_fields.bits.slice_deblocking_filter_disabled_flag;
121 context->desc.h265enc.slice.slice_loop_filter_across_slices_enabled_flag = h265->slice_fields.bits.slice_loop_filter_across_slices_enabled_flag;
122
123 return VA_STATUS_SUCCESS;
124 }
125
126 VAStatus
vlVaHandleVAEncSequenceParameterBufferTypeHEVC(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)127 vlVaHandleVAEncSequenceParameterBufferTypeHEVC(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
128 {
129 VAEncSequenceParameterBufferHEVC *h265 = (VAEncSequenceParameterBufferHEVC *)buf->data;
130
131 if (!context->decoder) {
132 context->templat.level = h265->general_level_idc;
133 context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
134
135 if (!context->decoder)
136 return VA_STATUS_ERROR_ALLOCATION_FAILED;
137 }
138
139 context->desc.h265enc.seq.general_profile_idc = h265->general_profile_idc;
140 context->desc.h265enc.seq.general_level_idc = h265->general_level_idc;
141 context->desc.h265enc.seq.general_tier_flag = h265->general_tier_flag;
142 context->desc.h265enc.seq.intra_period = h265->intra_period;
143 context->desc.h265enc.seq.pic_width_in_luma_samples = h265->pic_width_in_luma_samples;
144 context->desc.h265enc.seq.pic_height_in_luma_samples = h265->pic_height_in_luma_samples;
145 context->desc.h265enc.seq.chroma_format_idc = h265->seq_fields.bits.chroma_format_idc;
146 context->desc.h265enc.seq.bit_depth_luma_minus8 = h265->seq_fields.bits.bit_depth_luma_minus8;
147 context->desc.h265enc.seq.bit_depth_chroma_minus8 = h265->seq_fields.bits.bit_depth_chroma_minus8;
148 context->desc.h265enc.seq.strong_intra_smoothing_enabled_flag = h265->seq_fields.bits.strong_intra_smoothing_enabled_flag;
149 context->desc.h265enc.seq.amp_enabled_flag = h265->seq_fields.bits.amp_enabled_flag;
150 context->desc.h265enc.seq.sample_adaptive_offset_enabled_flag = h265->seq_fields.bits.sample_adaptive_offset_enabled_flag;
151 context->desc.h265enc.seq.pcm_enabled_flag = h265->seq_fields.bits.pcm_enabled_flag;
152 context->desc.h265enc.seq.sps_temporal_mvp_enabled_flag = h265->seq_fields.bits.sps_temporal_mvp_enabled_flag;
153 context->desc.h265enc.seq.log2_min_luma_coding_block_size_minus3 = h265->log2_min_luma_coding_block_size_minus3;
154 context->desc.h265enc.seq.log2_diff_max_min_luma_coding_block_size = h265->log2_diff_max_min_luma_coding_block_size;
155 context->desc.h265enc.seq.log2_min_transform_block_size_minus2 = h265->log2_min_transform_block_size_minus2;
156 context->desc.h265enc.seq.log2_diff_max_min_transform_block_size = h265->log2_diff_max_min_transform_block_size;
157 context->desc.h265enc.seq.max_transform_hierarchy_depth_inter = h265->max_transform_hierarchy_depth_inter;
158 context->desc.h265enc.seq.max_transform_hierarchy_depth_intra = h265->max_transform_hierarchy_depth_intra;
159 context->desc.h265enc.rc.frame_rate_num = h265->vui_time_scale;
160 context->desc.h265enc.rc.frame_rate_den = h265->vui_num_units_in_tick;
161
162 return VA_STATUS_SUCCESS;
163 }
164
165 VAStatus
vlVaHandleVAEncMiscParameterTypeRateControlHEVC(vlVaContext * context,VAEncMiscParameterBuffer * misc)166 vlVaHandleVAEncMiscParameterTypeRateControlHEVC(vlVaContext *context, VAEncMiscParameterBuffer *misc)
167 {
168 VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
169
170 if (context->desc.h265enc.rc.rate_ctrl_method ==
171 PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT)
172 context->desc.h265enc.rc.target_bitrate = rc->bits_per_second;
173 else
174 context->desc.h265enc.rc.target_bitrate = rc->bits_per_second * (rc->target_percentage / 100.0);
175 context->desc.h265enc.rc.peak_bitrate = rc->bits_per_second;
176 if (context->desc.h265enc.rc.target_bitrate < 2000000)
177 context->desc.h265enc.rc.vbv_buffer_size = MIN2((context->desc.h265enc.rc.target_bitrate * 2.75), 2000000);
178 else
179 context->desc.h265enc.rc.vbv_buffer_size = context->desc.h265enc.rc.target_bitrate;
180
181 return VA_STATUS_SUCCESS;
182 }
183
184 VAStatus
vlVaHandleVAEncMiscParameterTypeFrameRateHEVC(vlVaContext * context,VAEncMiscParameterBuffer * misc)185 vlVaHandleVAEncMiscParameterTypeFrameRateHEVC(vlVaContext *context, VAEncMiscParameterBuffer *misc)
186 {
187 VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
188
189 if (fr->framerate & 0xffff0000) {
190 context->desc.h265enc.rc.frame_rate_num = fr->framerate & 0xffff;
191 context->desc.h265enc.rc.frame_rate_den = fr->framerate >> 16 & 0xffff;
192 } else {
193 context->desc.h265enc.rc.frame_rate_num = fr->framerate;
194 context->desc.h265enc.rc.frame_rate_den = 1;
195 }
196
197 return VA_STATUS_SUCCESS;
198 }
199
profile_tier(struct vl_rbsp * rbsp)200 static void profile_tier(struct vl_rbsp *rbsp)
201 {
202 vl_rbsp_u(rbsp, 2); /* general_profile_space */
203 vl_rbsp_u(rbsp, 1); /* general_tier_flag */
204 vl_rbsp_u(rbsp, 5); /* general_profile_idc */
205
206 /* general_profile_compatibility_flag */
207 for(int i = 0; i < 32; ++i)
208 vl_rbsp_u(rbsp, 1);
209
210 vl_rbsp_u(rbsp, 1); /* general_progressive_source_flag */
211 vl_rbsp_u(rbsp, 1); /* general_interlaced_source_flag */
212 vl_rbsp_u(rbsp, 1); /* general_non_packed_constraint_flag */
213 vl_rbsp_u(rbsp, 1); /* general_frame_only_constraint_flag */
214
215 /* general_reserved_zero_44bits */
216 vl_rbsp_u(rbsp, 16);
217 vl_rbsp_u(rbsp, 16);
218 vl_rbsp_u(rbsp, 12);
219 }
220
profile_tier_level(struct vl_rbsp * rbsp,int max_sublayers_minus1)221 static unsigned profile_tier_level(struct vl_rbsp *rbsp,
222 int max_sublayers_minus1)
223 {
224 bool sub_layer_profile_present_flag[6];
225 bool sub_layer_level_present_flag[6];
226 unsigned level_idc;
227 int i;
228
229 profile_tier(rbsp);
230 level_idc = vl_rbsp_u(rbsp, 8); /* general_level_idc */
231
232 for (i = 0; i < max_sublayers_minus1; ++i) {
233 sub_layer_profile_present_flag[i] = vl_rbsp_u(rbsp, 1);
234 sub_layer_level_present_flag[i] = vl_rbsp_u(rbsp, 1);
235 }
236
237 if (max_sublayers_minus1 > 0)
238 for (i = max_sublayers_minus1; i < 8; ++i)
239 vl_rbsp_u(rbsp, 2); /* reserved_zero_2bits */
240
241 for (i = 0; i < max_sublayers_minus1; ++i) {
242 if (sub_layer_profile_present_flag[i])
243 profile_tier(rbsp);
244
245 if (sub_layer_level_present_flag[i])
246 vl_rbsp_u(rbsp, 8); /* sub_layer_level_idc */
247 }
248
249 return level_idc;
250 }
251
parseEncSpsParamsH265(vlVaContext * context,struct vl_rbsp * rbsp)252 static void parseEncSpsParamsH265(vlVaContext *context, struct vl_rbsp *rbsp)
253 {
254 int sps_max_sub_layers_minus1;
255
256 vl_rbsp_u(rbsp, 4); /* sps_video_parameter_set_id */
257 sps_max_sub_layers_minus1 = vl_rbsp_u(rbsp, 3);
258 vl_rbsp_u(rbsp, 1); /* sps_temporal_id_nesting_flag */
259
260 /* level_idc */
261 profile_tier_level(rbsp, sps_max_sub_layers_minus1);
262
263 vl_rbsp_ue(rbsp); /* id */
264 context->desc.h265enc.seq.chroma_format_idc = vl_rbsp_ue(rbsp);
265 if (context->desc.h265enc.seq.chroma_format_idc == 3)
266 vl_rbsp_u(rbsp, 1); /* separate_colour_plane_flag */
267
268 context->desc.h265enc.seq.pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
269 context->desc.h265enc.seq.pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
270
271 /* conformance_window_flag - used for cropping */
272 context->desc.h265enc.seq.conformance_window_flag = vl_rbsp_u(rbsp, 1);
273 if (context->desc.h265enc.seq.conformance_window_flag) {
274 context->desc.h265enc.seq.conf_win_left_offset = vl_rbsp_ue(rbsp);
275 context->desc.h265enc.seq.conf_win_right_offset = vl_rbsp_ue(rbsp);
276 context->desc.h265enc.seq.conf_win_top_offset = vl_rbsp_ue(rbsp);
277 context->desc.h265enc.seq.conf_win_bottom_offset = vl_rbsp_ue(rbsp);
278 }
279 }
280
281 VAStatus
vlVaHandleVAEncPackedHeaderDataBufferTypeHEVC(vlVaContext * context,vlVaBuffer * buf)282 vlVaHandleVAEncPackedHeaderDataBufferTypeHEVC(vlVaContext *context, vlVaBuffer *buf)
283 {
284 struct vl_vlc vlc = {0};
285 vl_vlc_init(&vlc, 1, (const void * const*)&buf->data, &buf->size);
286
287 while (vl_vlc_bits_left(&vlc) > 0) {
288 /* search the first 64 bytes for a startcode */
289 for (int i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= 24; ++i) {
290 if (vl_vlc_peekbits(&vlc, 24) == 0x000001)
291 break;
292 vl_vlc_eatbits(&vlc, 8);
293 vl_vlc_fillbits(&vlc);
294 }
295 vl_vlc_eatbits(&vlc, 24); /* eat the startcode */
296
297 if (vl_vlc_valid_bits(&vlc) < 15)
298 vl_vlc_fillbits(&vlc);
299
300 vl_vlc_eatbits(&vlc, 1);
301 unsigned nal_unit_type = vl_vlc_get_uimsbf(&vlc, 6);
302 vl_vlc_eatbits(&vlc, 6);
303 vl_vlc_eatbits(&vlc, 3);
304
305 struct vl_rbsp rbsp;
306 vl_rbsp_init(&rbsp, &vlc, ~0);
307
308 switch(nal_unit_type) {
309 case HEVC_NAL_SPS:
310 parseEncSpsParamsH265(context, &rbsp);
311 break;
312 case HEVC_NAL_VPS:
313 case HEVC_NAL_PPS:
314 default:
315 break;
316 }
317 }
318
319 return VA_STATUS_SUCCESS;
320 }
321
getEncParamPresetH265(vlVaContext * context)322 void getEncParamPresetH265(vlVaContext *context)
323 {
324 //rate control
325 context->desc.h265enc.rc.vbv_buffer_size = 20000000;
326 context->desc.h265enc.rc.vbv_buf_lv = 48;
327 context->desc.h265enc.rc.fill_data_enable = 1;
328 context->desc.h265enc.rc.enforce_hrd = 1;
329 if (context->desc.h265enc.rc.frame_rate_num == 0 ||
330 context->desc.h265enc.rc.frame_rate_den == 0) {
331 context->desc.h265enc.rc.frame_rate_num = 30;
332 context->desc.h265enc.rc.frame_rate_den = 1;
333 }
334 context->desc.h265enc.rc.target_bits_picture =
335 context->desc.h265enc.rc.target_bitrate *
336 ((float)context->desc.h265enc.rc.frame_rate_den /
337 context->desc.h265enc.rc.frame_rate_num);
338 context->desc.h265enc.rc.peak_bits_picture_integer =
339 context->desc.h265enc.rc.peak_bitrate *
340 ((float)context->desc.h265enc.rc.frame_rate_den /
341 context->desc.h265enc.rc.frame_rate_num);
342
343 context->desc.h265enc.rc.peak_bits_picture_fraction = 0;
344 }
345