• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 VAStatus
vlVaHandleVAEncPictureParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)33 vlVaHandleVAEncPictureParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
34 {
35    VAEncPictureParameterBufferH264 *h264;
36    vlVaBuffer *coded_buf;
37 
38    h264 = buf->data;
39    if (h264->pic_fields.bits.idr_pic_flag == 1)
40       context->desc.h264enc.frame_num = 0;
41    context->desc.h264enc.not_referenced = !h264->pic_fields.bits.reference_pic_flag;
42    context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
43    if (context->desc.h264enc.gop_cnt == 0)
44       context->desc.h264enc.i_remain = context->gop_coeff;
45    else if (context->desc.h264enc.frame_num == 1)
46       context->desc.h264enc.i_remain--;
47 
48    context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
49 
50    coded_buf = handle_table_get(drv->htab, h264->coded_buf);
51    if (!coded_buf->derived_surface.resource)
52       coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
53                                             PIPE_USAGE_STREAM, coded_buf->size);
54    context->coded_buf = coded_buf;
55 
56    _mesa_hash_table_insert(context->desc.h264enc.frame_idx,
57 		       UINT_TO_PTR(h264->CurrPic.picture_id + 1),
58 		       UINT_TO_PTR(context->desc.h264enc.frame_num));
59 
60    if (h264->pic_fields.bits.idr_pic_flag == 1)
61       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_IDR;
62    else
63       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
64 
65    /* Initialize slice descriptors for this picture */
66    context->desc.h264enc.num_slice_descriptors = 0;
67    memset(&context->desc.h264enc.slices_descriptors, 0, sizeof(context->desc.h264enc.slices_descriptors));
68 
69    context->desc.h264enc.quant_i_frames = h264->pic_init_qp;
70    context->desc.h264enc.quant_b_frames = h264->pic_init_qp;
71    context->desc.h264enc.quant_p_frames = h264->pic_init_qp;
72    context->desc.h264enc.gop_cnt++;
73    if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
74       context->desc.h264enc.gop_cnt = 0;
75 
76    context->desc.h264enc.pic_ctrl.enc_cabac_enable = h264->pic_fields.bits.entropy_coding_mode_flag;
77    context->desc.h264enc.num_ref_idx_l0_active_minus1 = h264->num_ref_idx_l0_active_minus1;
78    context->desc.h264enc.num_ref_idx_l1_active_minus1 = h264->num_ref_idx_l1_active_minus1;
79 
80    return VA_STATUS_SUCCESS;
81 }
82 
83 VAStatus
vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)84 vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
85 {
86    VAEncSliceParameterBufferH264 *h264;
87 
88    h264 = buf->data;
89    memset(&context->desc.h264enc.ref_idx_l0_list, VA_INVALID_ID, sizeof(context->desc.h264enc.ref_idx_l0_list));
90    memset(&context->desc.h264enc.ref_idx_l1_list, VA_INVALID_ID, sizeof(context->desc.h264enc.ref_idx_l1_list));
91 
92    if(h264->num_ref_idx_active_override_flag) {
93       context->desc.h264enc.num_ref_idx_l0_active_minus1 = h264->num_ref_idx_l0_active_minus1;
94       context->desc.h264enc.num_ref_idx_l1_active_minus1 = h264->num_ref_idx_l1_active_minus1;
95    }
96 
97    for (int i = 0; i < 32; i++) {
98       if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
99                context->desc.h264enc.ref_idx_l0_list[i] = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
100                                  UINT_TO_PTR(h264->RefPicList0[i].picture_id + 1)));
101       }
102       if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
103             context->desc.h264enc.ref_idx_l1_list[i] = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
104                                  UINT_TO_PTR(h264->RefPicList1[i].picture_id + 1)));
105       }
106    }
107 
108    /**
109     *  VAEncSliceParameterBufferH264.slice_type
110     *  Slice type.
111     *  Range: 0..2, 5..7, i.e. no switching slices.
112    */
113    struct h264_slice_descriptor slice_descriptor = { };
114    slice_descriptor.macroblock_address = h264->macroblock_address;
115    slice_descriptor.num_macroblocks = h264->num_macroblocks;
116 
117    if ((h264->slice_type == 1) || (h264->slice_type == 6)) {
118       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_B;
119       slice_descriptor.slice_type = PIPE_H264_SLICE_TYPE_B;
120    } else if ((h264->slice_type == 0) || (h264->slice_type == 5)) {
121       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
122       slice_descriptor.slice_type = PIPE_H264_SLICE_TYPE_P;
123    } else if ((h264->slice_type == 2) || (h264->slice_type == 7)) {
124       if (context->desc.h264enc.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR) {
125          if (slice_descriptor.macroblock_address == 0) {
126             /* Increment it only for the first slice of the IDR frame */
127             context->desc.h264enc.idr_pic_id++;
128          }
129          slice_descriptor.slice_type = PIPE_H264_SLICE_TYPE_I;
130       } else {
131          context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_I;
132          slice_descriptor.slice_type = PIPE_H264_SLICE_TYPE_I;
133       }
134    } else {
135       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP;
136    }
137 
138    context->desc.h264enc.pic_ctrl.enc_cabac_init_idc = h264->cabac_init_idc;
139 
140    /* Handle the slice control parameters */
141    if (context->desc.h264enc.num_slice_descriptors < ARRAY_SIZE(context->desc.h264enc.slices_descriptors)) {
142       context->desc.h264enc.slices_descriptors[context->desc.h264enc.num_slice_descriptors++] = slice_descriptor;
143    } else {
144       return VA_STATUS_ERROR_NOT_ENOUGH_BUFFER;
145    }
146 
147    return VA_STATUS_SUCCESS;
148 }
149 
150 VAStatus
vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)151 vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
152 {
153    VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;
154    if (!context->decoder) {
155       context->templat.max_references = h264->max_num_ref_frames;
156       context->templat.level = h264->level_idc;
157       context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
158       if (!context->decoder)
159          return VA_STATUS_ERROR_ALLOCATION_FAILED;
160    }
161 
162    context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;
163    if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
164       context->gop_coeff = VL_VA_ENC_GOP_COEFF;
165    context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;
166    context->desc.h264enc.rate_ctrl[0].frame_rate_num = h264->time_scale / 2;
167    context->desc.h264enc.rate_ctrl[0].frame_rate_den = h264->num_units_in_tick;
168    context->desc.h264enc.pic_order_cnt_type = h264->seq_fields.bits.pic_order_cnt_type;
169 
170    if (h264->frame_cropping_flag) {
171       context->desc.h264enc.pic_ctrl.enc_frame_cropping_flag = h264->frame_cropping_flag;
172       context->desc.h264enc.pic_ctrl.enc_frame_crop_left_offset = h264->frame_crop_left_offset;
173       context->desc.h264enc.pic_ctrl.enc_frame_crop_right_offset = h264->frame_crop_right_offset;
174       context->desc.h264enc.pic_ctrl.enc_frame_crop_top_offset = h264->frame_crop_top_offset;
175       context->desc.h264enc.pic_ctrl.enc_frame_crop_bottom_offset = h264->frame_crop_bottom_offset;
176    }
177    return VA_STATUS_SUCCESS;
178 }
179 
180 VAStatus
vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)181 vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
182 {
183    unsigned temporal_id;
184    VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
185 
186    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
187                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
188                  rc->rc_flags.bits.temporal_id :
189                  0;
190 
191    if (context->desc.h264enc.rate_ctrl[0].rate_ctrl_method ==
192        PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT)
193       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
194          rc->bits_per_second;
195    else
196       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
197          rc->bits_per_second * (rc->target_percentage / 100.0);
198 
199    if (context->desc.h264enc.num_temporal_layers > 0 &&
200        temporal_id >= context->desc.h264enc.num_temporal_layers)
201       return VA_STATUS_ERROR_INVALID_PARAMETER;
202 
203    context->desc.h264enc.rate_ctrl[temporal_id].peak_bitrate = rc->bits_per_second;
204    if (context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate < 2000000)
205        context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
206          MIN2((context->desc.h264enc.rate_ctrl[0].target_bitrate * 2.75), 2000000);
207    else
208       context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
209          context->desc.h264enc.rate_ctrl[0].target_bitrate;
210 
211    return VA_STATUS_SUCCESS;
212 }
213 
214 VAStatus
vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)215 vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
216 {
217    unsigned temporal_id;
218    VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
219 
220    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
221                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
222                  fr->framerate_flags.bits.temporal_id :
223                  0;
224 
225    if (context->desc.h264enc.num_temporal_layers > 0 &&
226        temporal_id >= context->desc.h264enc.num_temporal_layers)
227       return VA_STATUS_ERROR_INVALID_PARAMETER;
228 
229    if (fr->framerate & 0xffff0000) {
230       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate       & 0xffff;
231       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = fr->framerate >> 16 & 0xffff;
232    } else {
233       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate;
234       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = 1;
235    }
236 
237    return VA_STATUS_SUCCESS;
238 }
239 
240 VAStatus
vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)241 vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
242 {
243    VAEncMiscParameterTemporalLayerStructure *tl = (VAEncMiscParameterTemporalLayerStructure *)misc->data;
244 
245    context->desc.h264enc.num_temporal_layers = tl->number_of_layers;
246 
247    return VA_STATUS_SUCCESS;
248 }
249 
getEncParamPresetH264(vlVaContext * context)250 void getEncParamPresetH264(vlVaContext *context)
251 {
252    //motion estimation preset
253    context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0;
254    context->desc.h264enc.motion_est.lsmvert = 0;
255    context->desc.h264enc.motion_est.enc_disable_sub_mode = 254;
256    context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0;
257    context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0;
258    context->desc.h264enc.motion_est.enc_ime2_search_range_x = 1;
259    context->desc.h264enc.motion_est.enc_ime2_search_range_y = 1;
260 
261    //pic control preset
262    context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;
263 
264    //rate control
265    context->desc.h264enc.rate_ctrl[0].vbv_buffer_size = 20000000;
266    context->desc.h264enc.rate_ctrl[0].vbv_buf_lv = 48;
267    context->desc.h264enc.rate_ctrl[0].fill_data_enable = 1;
268    context->desc.h264enc.rate_ctrl[0].enforce_hrd = 1;
269    context->desc.h264enc.enable_vui = false;
270    if (context->desc.h264enc.rate_ctrl[0].frame_rate_num == 0 ||
271        context->desc.h264enc.rate_ctrl[0].frame_rate_den == 0) {
272          context->desc.h264enc.rate_ctrl[0].frame_rate_num = 30;
273          context->desc.h264enc.rate_ctrl[0].frame_rate_den = 1;
274    }
275    context->desc.h264enc.rate_ctrl[0].target_bits_picture =
276       context->desc.h264enc.rate_ctrl[0].target_bitrate *
277       ((float)context->desc.h264enc.rate_ctrl[0].frame_rate_den /
278       context->desc.h264enc.rate_ctrl[0].frame_rate_num);
279    context->desc.h264enc.rate_ctrl[0].peak_bits_picture_integer =
280       context->desc.h264enc.rate_ctrl[0].peak_bitrate *
281       ((float)context->desc.h264enc.rate_ctrl[0].frame_rate_den /
282       context->desc.h264enc.rate_ctrl[0].frame_rate_num);
283 
284    context->desc.h264enc.rate_ctrl[0].peak_bits_picture_fraction = 0;
285    context->desc.h264enc.ref_pic_mode = 0x00000201;
286 }
287