• 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    context->desc.h264enc.frame_num = h264->frame_num;
40    context->desc.h264enc.not_referenced = false;
41    context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
42    if (context->desc.h264enc.gop_cnt == 0)
43       context->desc.h264enc.i_remain = context->gop_coeff;
44    else if (context->desc.h264enc.frame_num == 1)
45       context->desc.h264enc.i_remain--;
46 
47    context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
48 
49    coded_buf = handle_table_get(drv->htab, h264->coded_buf);
50    if (!coded_buf->derived_surface.resource)
51       coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
52                                             PIPE_USAGE_STREAM, coded_buf->size);
53    context->coded_buf = coded_buf;
54 
55    _mesa_hash_table_insert(context->desc.h264enc.frame_idx,
56 		       UINT_TO_PTR(h264->CurrPic.picture_id + 1),
57 		       UINT_TO_PTR(h264->frame_num));
58 
59    if (h264->pic_fields.bits.idr_pic_flag == 1)
60       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_IDR;
61    else
62       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
63 
64    context->desc.h264enc.quant_i_frames = h264->pic_init_qp;
65    context->desc.h264enc.quant_b_frames = h264->pic_init_qp;
66    context->desc.h264enc.quant_p_frames = h264->pic_init_qp;
67    context->desc.h264enc.gop_cnt++;
68    if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
69       context->desc.h264enc.gop_cnt = 0;
70 
71    return VA_STATUS_SUCCESS;
72 }
73 
74 VAStatus
vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)75 vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
76 {
77    VAEncSliceParameterBufferH264 *h264;
78 
79    h264 = buf->data;
80    context->desc.h264enc.ref_idx_l0 = VA_INVALID_ID;
81    context->desc.h264enc.ref_idx_l1 = VA_INVALID_ID;
82 
83    for (int i = 0; i < 32; i++) {
84       if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
85          if (context->desc.h264enc.ref_idx_l0 == VA_INVALID_ID)
86             context->desc.h264enc.ref_idx_l0 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
87 									       UINT_TO_PTR(h264->RefPicList0[i].picture_id + 1)));
88       }
89       if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
90          if (context->desc.h264enc.ref_idx_l1 == VA_INVALID_ID)
91             context->desc.h264enc.ref_idx_l1 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
92 									       UINT_TO_PTR(h264->RefPicList1[i].picture_id + 1)));
93       }
94    }
95 
96    if (h264->slice_type == 1)
97       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_B;
98    else if (h264->slice_type == 0)
99       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
100    else if (h264->slice_type == 2) {
101       if (context->desc.h264enc.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
102          context->desc.h264enc.idr_pic_id++;
103 	   else
104          context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_I;
105    } else
106       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP;
107 
108    return VA_STATUS_SUCCESS;
109 }
110 
111 VAStatus
vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)112 vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
113 {
114    VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;
115    if (!context->decoder) {
116       context->templat.max_references = h264->max_num_ref_frames;
117       context->templat.level = h264->level_idc;
118       context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);
119       if (!context->decoder)
120          return VA_STATUS_ERROR_ALLOCATION_FAILED;
121    }
122 
123    context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;
124    if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
125       context->gop_coeff = VL_VA_ENC_GOP_COEFF;
126    context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;
127    context->desc.h264enc.rate_ctrl[0].frame_rate_num = h264->time_scale / 2;
128    context->desc.h264enc.rate_ctrl[0].frame_rate_den = h264->num_units_in_tick;
129    context->desc.h264enc.pic_order_cnt_type = h264->seq_fields.bits.pic_order_cnt_type;
130 
131    if (h264->frame_cropping_flag) {
132       context->desc.h264enc.pic_ctrl.enc_frame_cropping_flag = h264->frame_cropping_flag;
133       context->desc.h264enc.pic_ctrl.enc_frame_crop_left_offset = h264->frame_crop_left_offset;
134       context->desc.h264enc.pic_ctrl.enc_frame_crop_right_offset = h264->frame_crop_right_offset;
135       context->desc.h264enc.pic_ctrl.enc_frame_crop_top_offset = h264->frame_crop_top_offset;
136       context->desc.h264enc.pic_ctrl.enc_frame_crop_bottom_offset = h264->frame_crop_bottom_offset;
137    }
138    return VA_STATUS_SUCCESS;
139 }
140 
141 VAStatus
vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)142 vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
143 {
144    unsigned temporal_id;
145    VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
146 
147    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
148                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
149                  rc->rc_flags.bits.temporal_id :
150                  0;
151 
152    if (context->desc.h264enc.rate_ctrl[0].rate_ctrl_method ==
153        PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT)
154       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
155          rc->bits_per_second;
156    else
157       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
158          rc->bits_per_second * (rc->target_percentage / 100.0);
159 
160    if (context->desc.h264enc.num_temporal_layers > 0 &&
161        temporal_id >= context->desc.h264enc.num_temporal_layers)
162       return VA_STATUS_ERROR_INVALID_PARAMETER;
163 
164    context->desc.h264enc.rate_ctrl[temporal_id].peak_bitrate = rc->bits_per_second;
165    if (context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate < 2000000)
166        context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
167          MIN2((context->desc.h264enc.rate_ctrl[0].target_bitrate * 2.75), 2000000);
168    else
169       context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
170          context->desc.h264enc.rate_ctrl[0].target_bitrate;
171 
172    return VA_STATUS_SUCCESS;
173 }
174 
175 VAStatus
vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)176 vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
177 {
178    unsigned temporal_id;
179    VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
180 
181    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
182                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
183                  fr->framerate_flags.bits.temporal_id :
184                  0;
185 
186    if (context->desc.h264enc.num_temporal_layers > 0 &&
187        temporal_id >= context->desc.h264enc.num_temporal_layers)
188       return VA_STATUS_ERROR_INVALID_PARAMETER;
189 
190    if (fr->framerate & 0xffff0000) {
191       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate       & 0xffff;
192       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = fr->framerate >> 16 & 0xffff;
193    } else {
194       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate;
195       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = 1;
196    }
197 
198    return VA_STATUS_SUCCESS;
199 }
200 
201 VAStatus
vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)202 vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
203 {
204    VAEncMiscParameterTemporalLayerStructure *tl = (VAEncMiscParameterTemporalLayerStructure *)misc->data;
205 
206    context->desc.h264enc.num_temporal_layers = tl->number_of_layers;
207 
208    return VA_STATUS_SUCCESS;
209 }
210 
getEncParamPresetH264(vlVaContext * context)211 void getEncParamPresetH264(vlVaContext *context)
212 {
213    //motion estimation preset
214    context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0x00000001;
215    context->desc.h264enc.motion_est.lsmvert = 0x00000002;
216    context->desc.h264enc.motion_est.enc_disable_sub_mode = 0x00000078;
217    context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0x00000001;
218    context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0x00000001;
219    context->desc.h264enc.motion_est.enc_ime2_search_range_x = 0x00000004;
220    context->desc.h264enc.motion_est.enc_ime2_search_range_y = 0x00000004;
221 
222    //pic control preset
223    context->desc.h264enc.pic_ctrl.enc_cabac_enable = 0x00000001;
224    context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;
225 
226    //rate control
227    context->desc.h264enc.rate_ctrl[0].vbv_buffer_size = 20000000;
228    context->desc.h264enc.rate_ctrl[0].vbv_buf_lv = 48;
229    context->desc.h264enc.rate_ctrl[0].fill_data_enable = 1;
230    context->desc.h264enc.rate_ctrl[0].enforce_hrd = 1;
231    context->desc.h264enc.enable_vui = false;
232    if (context->desc.h264enc.rate_ctrl[0].frame_rate_num == 0 ||
233        context->desc.h264enc.rate_ctrl[0].frame_rate_den == 0) {
234          context->desc.h264enc.rate_ctrl[0].frame_rate_num = 30;
235          context->desc.h264enc.rate_ctrl[0].frame_rate_den = 1;
236    }
237    context->desc.h264enc.rate_ctrl[0].target_bits_picture =
238       context->desc.h264enc.rate_ctrl[0].target_bitrate *
239       ((float)context->desc.h264enc.rate_ctrl[0].frame_rate_den /
240       context->desc.h264enc.rate_ctrl[0].frame_rate_num);
241    context->desc.h264enc.rate_ctrl[0].peak_bits_picture_integer =
242       context->desc.h264enc.rate_ctrl[0].peak_bitrate *
243       ((float)context->desc.h264enc.rate_ctrl[0].frame_rate_den /
244       context->desc.h264enc.rate_ctrl[0].frame_rate_num);
245 
246    context->desc.h264enc.rate_ctrl[0].peak_bits_picture_fraction = 0;
247    context->desc.h264enc.ref_pic_mode = 0x00000201;
248 }
249