xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/va/picture_h264_enc.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/vl_rbsp.h"
33 
34 VAStatus
vlVaHandleVAEncPictureParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)35 vlVaHandleVAEncPictureParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
36 {
37    VAEncPictureParameterBufferH264 *h264;
38    vlVaBuffer *coded_buf;
39    vlVaSurface *surf;
40    unsigned i;
41 
42    h264 = buf->data;
43    if (h264->pic_fields.bits.idr_pic_flag == 1)
44       context->desc.h264enc.frame_num = 0;
45    context->desc.h264enc.not_referenced = !h264->pic_fields.bits.reference_pic_flag;
46    context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
47    context->desc.h264enc.is_ltr = h264->CurrPic.flags & VA_PICTURE_H264_LONG_TERM_REFERENCE;
48    if (context->desc.h264enc.is_ltr)
49       context->desc.h264enc.ltr_index = h264->CurrPic.frame_idx;
50    if (context->desc.h264enc.gop_cnt == 0)
51       context->desc.h264enc.i_remain = context->gop_coeff;
52    else if (context->desc.h264enc.frame_num == 1)
53       context->desc.h264enc.i_remain--;
54 
55    surf = handle_table_get(drv->htab, h264->CurrPic.picture_id);
56    if (!surf)
57       return VA_STATUS_ERROR_INVALID_PARAMETER;
58 
59    for (i = 0; i < ARRAY_SIZE(context->desc.h264enc.dpb); i++) {
60       if (context->desc.h264enc.dpb[i].id == h264->CurrPic.picture_id) {
61          assert(surf->is_dpb);
62          break;
63       }
64       if (!context->desc.h264enc.dpb[i].id) {
65          assert(!surf->is_dpb);
66          surf->is_dpb = true;
67          if (surf->buffer) {
68             surf->buffer->destroy(surf->buffer);
69             surf->buffer = NULL;
70          }
71          if (context->decoder && context->decoder->create_dpb_buffer)
72             surf->buffer = context->decoder->create_dpb_buffer(context->decoder, &context->desc.base, &surf->templat);
73          vlVaSetSurfaceContext(drv, surf, context);
74          context->desc.h264enc.dpb_size++;
75          break;
76       }
77    }
78    if (i == ARRAY_SIZE(context->desc.h264enc.dpb))
79       return VA_STATUS_ERROR_INVALID_PARAMETER;
80    context->desc.h264enc.dpb_curr_pic = i;
81    context->desc.h264enc.dpb[i].id = h264->CurrPic.picture_id;
82    context->desc.h264enc.dpb[i].frame_idx = h264->CurrPic.frame_idx;
83    context->desc.h264enc.dpb[i].pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;
84    context->desc.h264enc.dpb[i].is_ltr = h264->CurrPic.flags & VA_PICTURE_H264_LONG_TERM_REFERENCE;
85    context->desc.h264enc.dpb[i].buffer = surf->buffer;
86 
87    context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;
88 
89    coded_buf = handle_table_get(drv->htab, h264->coded_buf);
90    if (!coded_buf)
91       return VA_STATUS_ERROR_INVALID_BUFFER;
92 
93    if (!coded_buf->derived_surface.resource)
94       coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,
95                                             PIPE_USAGE_STAGING, coded_buf->size);
96    context->coded_buf = coded_buf;
97 
98    if (context->desc.h264enc.is_ltr)
99       _mesa_hash_table_insert(context->desc.h264enc.frame_idx,
100 		       UINT_TO_PTR(h264->CurrPic.picture_id + 1),
101 		       UINT_TO_PTR(context->desc.h264enc.ltr_index));
102    else
103       _mesa_hash_table_insert(context->desc.h264enc.frame_idx,
104 		       UINT_TO_PTR(h264->CurrPic.picture_id + 1),
105 		       UINT_TO_PTR(context->desc.h264enc.frame_num));
106 
107    if (h264->pic_fields.bits.idr_pic_flag == 1)
108       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_IDR;
109    else
110       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
111 
112    /* Initialize slice descriptors for this picture */
113    context->desc.h264enc.num_slice_descriptors = 0;
114    memset(&context->desc.h264enc.slices_descriptors, 0, sizeof(context->desc.h264enc.slices_descriptors));
115 
116    context->desc.h264enc.init_qp = h264->pic_init_qp;
117    context->desc.h264enc.gop_cnt++;
118    if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)
119       context->desc.h264enc.gop_cnt = 0;
120 
121    context->desc.h264enc.pic_ctrl.enc_cabac_enable = h264->pic_fields.bits.entropy_coding_mode_flag;
122    context->desc.h264enc.num_ref_idx_l0_active_minus1 = h264->num_ref_idx_l0_active_minus1;
123    context->desc.h264enc.num_ref_idx_l1_active_minus1 = h264->num_ref_idx_l1_active_minus1;
124    context->desc.h264enc.pic_ctrl.deblocking_filter_control_present_flag
125       = h264->pic_fields.bits.deblocking_filter_control_present_flag;
126    context->desc.h264enc.pic_ctrl.redundant_pic_cnt_present_flag
127       = h264->pic_fields.bits.redundant_pic_cnt_present_flag;
128    context->desc.h264enc.pic_ctrl.chroma_qp_index_offset = h264->chroma_qp_index_offset;
129    context->desc.h264enc.pic_ctrl.second_chroma_qp_index_offset
130       = h264->second_chroma_qp_index_offset;
131    context->desc.h264enc.pic_ctrl.constrained_intra_pred_flag =
132       h264->pic_fields.bits.constrained_intra_pred_flag;
133    context->desc.h264enc.pic_ctrl.transform_8x8_mode_flag =
134       h264->pic_fields.bits.transform_8x8_mode_flag;
135 
136    return VA_STATUS_SUCCESS;
137 }
138 
139 static uint8_t
vlVaDpbIndex(vlVaContext * context,VASurfaceID id)140 vlVaDpbIndex(vlVaContext *context, VASurfaceID id)
141 {
142    for (uint8_t i = 0; i < context->desc.h264enc.dpb_size; i++) {
143       if (context->desc.h264enc.dpb[i].id == id)
144          return i;
145    }
146    return PIPE_H2645_LIST_REF_INVALID_ENTRY;
147 }
148 
149 VAStatus
vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)150 vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
151 {
152    VAEncSliceParameterBufferH264 *h264;
153    unsigned slice_qp;
154 
155    h264 = buf->data;
156 
157    /* Handle the slice control parameters */
158    struct h264_slice_descriptor slice_descriptor;
159    memset(&slice_descriptor, 0, sizeof(slice_descriptor));
160    slice_descriptor.macroblock_address = h264->macroblock_address;
161    slice_descriptor.num_macroblocks = h264->num_macroblocks;
162    slice_descriptor.slice_type = h264->slice_type;
163    assert(slice_descriptor.slice_type <= PIPE_H264_SLICE_TYPE_I);
164 
165    if (context->desc.h264enc.num_slice_descriptors < ARRAY_SIZE(context->desc.h264enc.slices_descriptors))
166       context->desc.h264enc.slices_descriptors[context->desc.h264enc.num_slice_descriptors++] = slice_descriptor;
167    else
168       return VA_STATUS_ERROR_NOT_ENOUGH_BUFFER;
169 
170    /* Only use parameters for first slice */
171    if (h264->macroblock_address)
172       return VA_STATUS_SUCCESS;
173 
174    memset(&context->desc.h264enc.ref_idx_l0_list, VA_INVALID_ID, sizeof(context->desc.h264enc.ref_idx_l0_list));
175    memset(&context->desc.h264enc.ref_idx_l1_list, VA_INVALID_ID, sizeof(context->desc.h264enc.ref_idx_l1_list));
176    memset(&context->desc.h264enc.ref_list0, PIPE_H2645_LIST_REF_INVALID_ENTRY, sizeof(context->desc.h264enc.ref_list0));
177    memset(&context->desc.h264enc.ref_list1, PIPE_H2645_LIST_REF_INVALID_ENTRY, sizeof(context->desc.h264enc.ref_list1));
178 
179    if(h264->num_ref_idx_active_override_flag) {
180       context->desc.h264enc.num_ref_idx_l0_active_minus1 = h264->num_ref_idx_l0_active_minus1;
181       context->desc.h264enc.num_ref_idx_l1_active_minus1 = h264->num_ref_idx_l1_active_minus1;
182    }
183 
184    for (int i = 0; i < 32; i++) {
185       if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {
186          context->desc.h264enc.ref_list0[i] = vlVaDpbIndex(context, h264->RefPicList0[i].picture_id);
187                context->desc.h264enc.ref_idx_l0_list[i] = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
188                                  UINT_TO_PTR(h264->RefPicList0[i].picture_id + 1)));
189                context->desc.h264enc.l0_is_long_term[i] = h264->RefPicList0[i].flags &
190 		       					  VA_PICTURE_H264_LONG_TERM_REFERENCE;
191       }
192       if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {
193          context->desc.h264enc.ref_list1[i] = vlVaDpbIndex(context, h264->RefPicList1[i].picture_id);
194             context->desc.h264enc.ref_idx_l1_list[i] = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,
195                			 UINT_TO_PTR(h264->RefPicList1[i].picture_id + 1)));
196             context->desc.h264enc.l1_is_long_term[i] = h264->RefPicList1[i].flags &
197 		    				       VA_PICTURE_H264_LONG_TERM_REFERENCE;
198       }
199    }
200 
201    slice_qp = context->desc.h264enc.init_qp + h264->slice_qp_delta;
202 
203    if ((h264->slice_type == 1) || (h264->slice_type == 6)) {
204       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_B;
205       context->desc.h264enc.quant_b_frames = slice_qp;
206    } else if ((h264->slice_type == 0) || (h264->slice_type == 5)) {
207       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;
208       context->desc.h264enc.quant_p_frames = slice_qp;
209    } else if ((h264->slice_type == 2) || (h264->slice_type == 7)) {
210       if (context->desc.h264enc.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
211          context->desc.h264enc.idr_pic_id++;
212       else
213          context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_I;
214       context->desc.h264enc.quant_i_frames = slice_qp;
215    } else {
216       context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP;
217    }
218 
219    context->desc.h264enc.pic_ctrl.enc_cabac_init_idc = h264->cabac_init_idc;
220    context->desc.h264enc.dbk.disable_deblocking_filter_idc = h264->disable_deblocking_filter_idc;
221    context->desc.h264enc.dbk.alpha_c0_offset_div2 = h264->slice_alpha_c0_offset_div2;
222    context->desc.h264enc.dbk.beta_offset_div2 = h264->slice_beta_offset_div2;
223 
224    return VA_STATUS_SUCCESS;
225 }
226 
227 VAStatus
vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)228 vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
229 {
230    VAEncSequenceParameterBufferH264 *h264 = buf->data;
231    uint32_t num_units_in_tick = 0, time_scale  = 0;
232 
233    context->desc.h264enc.ip_period = h264->ip_period;
234    context->desc.h264enc.intra_idr_period =
235       h264->intra_idr_period != 0 ? h264->intra_idr_period : PIPE_DEFAULT_INTRA_IDR_PERIOD;
236    context->gop_coeff = ((1024 + context->desc.h264enc.intra_idr_period - 1) /
237                         context->desc.h264enc.intra_idr_period + 1) / 2 * 2;
238    if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)
239       context->gop_coeff = VL_VA_ENC_GOP_COEFF;
240    context->desc.h264enc.gop_size = context->desc.h264enc.intra_idr_period * context->gop_coeff;
241    context->desc.h264enc.seq.pic_order_cnt_type = h264->seq_fields.bits.pic_order_cnt_type;
242    context->desc.h264enc.seq.log2_max_frame_num_minus4 = h264->seq_fields.bits.log2_max_frame_num_minus4;
243    context->desc.h264enc.seq.log2_max_pic_order_cnt_lsb_minus4 = h264->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4;
244    context->desc.h264enc.seq.vui_parameters_present_flag = h264->vui_parameters_present_flag;
245    if (h264->vui_parameters_present_flag) {
246       context->desc.h264enc.seq.vui_flags.aspect_ratio_info_present_flag =
247          h264->vui_fields.bits.aspect_ratio_info_present_flag;
248       context->desc.h264enc.seq.aspect_ratio_idc = h264->aspect_ratio_idc;
249       context->desc.h264enc.seq.sar_width = h264->sar_width;
250       context->desc.h264enc.seq.sar_height = h264->sar_height;
251       context->desc.h264enc.seq.vui_flags.timing_info_present_flag =
252          h264->vui_fields.bits.timing_info_present_flag;
253       num_units_in_tick = h264->num_units_in_tick;
254       time_scale = h264->time_scale;
255       context->desc.h264enc.seq.vui_flags.fixed_frame_rate_flag =
256          h264->vui_fields.bits.fixed_frame_rate_flag;
257       context->desc.h264enc.seq.vui_flags.low_delay_hrd_flag =
258          h264->vui_fields.bits.low_delay_hrd_flag;
259       context->desc.h264enc.seq.vui_flags.bitstream_restriction_flag =
260          h264->vui_fields.bits.bitstream_restriction_flag;
261       context->desc.h264enc.seq.vui_flags.motion_vectors_over_pic_boundaries_flag =
262          h264->vui_fields.bits.motion_vectors_over_pic_boundaries_flag;
263       context->desc.h264enc.seq.log2_max_mv_length_vertical =
264             h264->vui_fields.bits.log2_max_mv_length_vertical;
265       context->desc.h264enc.seq.log2_max_mv_length_horizontal =
266             h264->vui_fields.bits.log2_max_mv_length_horizontal;
267    } else {
268       context->desc.h264enc.seq.vui_flags.timing_info_present_flag = 0;
269       context->desc.h264enc.seq.vui_flags.fixed_frame_rate_flag = 0;
270       context->desc.h264enc.seq.vui_flags.low_delay_hrd_flag = 0;
271       context->desc.h264enc.seq.vui_flags.bitstream_restriction_flag = 0;
272       context->desc.h264enc.seq.vui_flags.motion_vectors_over_pic_boundaries_flag = 0;
273       context->desc.h264enc.seq.log2_max_mv_length_vertical = 0;
274       context->desc.h264enc.seq.log2_max_mv_length_horizontal = 0;
275    }
276 
277    if (!context->desc.h264enc.seq.vui_flags.timing_info_present_flag) {
278       /* if not present, set default value */
279       num_units_in_tick = PIPE_DEFAULT_FRAME_RATE_DEN;
280       time_scale = PIPE_DEFAULT_FRAME_RATE_NUM * 2;
281    }
282 
283    context->desc.h264enc.seq.num_units_in_tick = num_units_in_tick;
284    context->desc.h264enc.seq.time_scale = time_scale;
285    context->desc.h264enc.rate_ctrl[0].frame_rate_num = time_scale / 2;
286    context->desc.h264enc.rate_ctrl[0].frame_rate_den = num_units_in_tick;
287 
288    if (h264->frame_cropping_flag) {
289       context->desc.h264enc.seq.enc_frame_cropping_flag = h264->frame_cropping_flag;
290       context->desc.h264enc.seq.enc_frame_crop_left_offset = h264->frame_crop_left_offset;
291       context->desc.h264enc.seq.enc_frame_crop_right_offset = h264->frame_crop_right_offset;
292       context->desc.h264enc.seq.enc_frame_crop_top_offset = h264->frame_crop_top_offset;
293       context->desc.h264enc.seq.enc_frame_crop_bottom_offset = h264->frame_crop_bottom_offset;
294    }
295 
296    return VA_STATUS_SUCCESS;
297 }
298 
299 VAStatus
vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)300 vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
301 {
302    unsigned temporal_id;
303    VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;
304 
305    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
306                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
307                  rc->rc_flags.bits.temporal_id :
308                  0;
309 
310    if (context->desc.h264enc.rate_ctrl[0].rate_ctrl_method ==
311        PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT)
312       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
313          rc->bits_per_second;
314    else
315       context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate =
316          rc->bits_per_second * (rc->target_percentage / 100.0);
317 
318    if (context->desc.h264enc.seq.num_temporal_layers > 0 &&
319        temporal_id >= context->desc.h264enc.seq.num_temporal_layers)
320       return VA_STATUS_ERROR_INVALID_PARAMETER;
321 
322    context->desc.h264enc.rate_ctrl[temporal_id].fill_data_enable = !(rc->rc_flags.bits.disable_bit_stuffing);
323    /* context->desc.h264enc.rate_ctrl[temporal_id].skip_frame_enable = !(rc->rc_flags.bits.disable_frame_skip); */
324    context->desc.h264enc.rate_ctrl[temporal_id].skip_frame_enable = 0;
325    context->desc.h264enc.rate_ctrl[temporal_id].peak_bitrate = rc->bits_per_second;
326 
327    if ((context->desc.h264enc.rate_ctrl[0].rate_ctrl_method == PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT) ||
328        (context->desc.h264enc.rate_ctrl[0].rate_ctrl_method == PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT_SKIP))
329       context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
330          context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate;
331    else if (context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate < 2000000)
332       context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
333          MIN2((context->desc.h264enc.rate_ctrl[0].target_bitrate * 2.75), 2000000);
334    else
335       context->desc.h264enc.rate_ctrl[temporal_id].vbv_buffer_size =
336          context->desc.h264enc.rate_ctrl[temporal_id].target_bitrate;
337 
338    context->desc.h264enc.rate_ctrl[temporal_id].max_qp = rc->max_qp;
339    context->desc.h264enc.rate_ctrl[temporal_id].min_qp = rc->min_qp;
340    /* Distinguishes from the default params set for these values in other
341       functions and app specific params passed down */
342    context->desc.h264enc.rate_ctrl[temporal_id].app_requested_qp_range = ((rc->max_qp > 0) || (rc->min_qp > 0));
343 
344    if (context->desc.h264enc.rate_ctrl[0].rate_ctrl_method ==
345        PIPE_H2645_ENC_RATE_CONTROL_METHOD_QUALITY_VARIABLE)
346       context->desc.h264enc.rate_ctrl[temporal_id].vbr_quality_factor =
347          rc->quality_factor;
348 
349    return VA_STATUS_SUCCESS;
350 }
351 
352 VAStatus
vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)353 vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
354 {
355    unsigned temporal_id;
356    VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;
357 
358    temporal_id = context->desc.h264enc.rate_ctrl[0].rate_ctrl_method !=
359                  PIPE_H2645_ENC_RATE_CONTROL_METHOD_DISABLE ?
360                  fr->framerate_flags.bits.temporal_id :
361                  0;
362 
363    if (context->desc.h264enc.seq.num_temporal_layers > 0 &&
364        temporal_id >= context->desc.h264enc.seq.num_temporal_layers)
365       return VA_STATUS_ERROR_INVALID_PARAMETER;
366 
367    if (fr->framerate & 0xffff0000) {
368       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate       & 0xffff;
369       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = fr->framerate >> 16 & 0xffff;
370    } else {
371       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_num = fr->framerate;
372       context->desc.h264enc.rate_ctrl[temporal_id].frame_rate_den = 1;
373    }
374 
375    return VA_STATUS_SUCCESS;
376 }
377 
parseEncSliceParamsH264(vlVaContext * context,struct vl_rbsp * rbsp,unsigned nal_ref_idc,unsigned nal_unit_type)378 static void parseEncSliceParamsH264(vlVaContext *context,
379                                     struct vl_rbsp *rbsp,
380                                     unsigned nal_ref_idc,
381                                     unsigned nal_unit_type)
382 {
383    struct pipe_h264_enc_seq_param *seq = &context->desc.h264enc.seq;
384    struct pipe_h264_enc_pic_control *pic = &context->desc.h264enc.pic_ctrl;
385    struct pipe_h264_enc_slice_param *slice = &context->desc.h264enc.slice;
386    unsigned modification_of_pic_nums_idc, memory_management_control_operation;
387 
388    /* Only parse first slice */
389    if (vl_rbsp_ue(rbsp) != 0) /* first_mb_in_slice */
390       return;
391 
392    pic->nal_ref_idc = nal_ref_idc;
393    pic->nal_unit_type = nal_unit_type;
394 
395    slice->slice_type = vl_rbsp_ue(rbsp) % 5;
396    vl_rbsp_ue(rbsp); /* pic_parameter_set_id */
397    slice->frame_num = vl_rbsp_u(rbsp, seq->log2_max_frame_num_minus4 + 4);
398 
399    if (context->desc.h264enc.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
400       slice->idr_pic_id = vl_rbsp_ue(rbsp);
401 
402    if (seq->pic_order_cnt_type == 0)
403       slice->pic_order_cnt_lsb = vl_rbsp_u(rbsp, seq->log2_max_pic_order_cnt_lsb_minus4 + 4);
404 
405    if (pic->redundant_pic_cnt_present_flag)
406       slice->redundant_pic_cnt = vl_rbsp_ue(rbsp);
407 
408    if (slice->slice_type == PIPE_H264_SLICE_TYPE_B)
409       slice->direct_spatial_mv_pred_flag = vl_rbsp_u(rbsp, 1);
410 
411    if (slice->slice_type == PIPE_H264_SLICE_TYPE_P ||
412        slice->slice_type == PIPE_H264_SLICE_TYPE_SP ||
413        slice->slice_type == PIPE_H264_SLICE_TYPE_B) {
414       slice->num_ref_idx_active_override_flag = vl_rbsp_u(rbsp, 1);
415       if (slice->num_ref_idx_active_override_flag) {
416          slice->num_ref_idx_l0_active_minus1 = vl_rbsp_ue(rbsp);
417          if (slice->slice_type == PIPE_H264_SLICE_TYPE_B)
418             slice->num_ref_idx_l1_active_minus1 = vl_rbsp_ue(rbsp);
419       }
420    }
421 
422    if (slice->slice_type != PIPE_H264_SLICE_TYPE_I &&
423        slice->slice_type != PIPE_H264_SLICE_TYPE_SI) {
424       slice->ref_pic_list_modification_flag_l0 = vl_rbsp_u(rbsp, 1);
425       if (slice->ref_pic_list_modification_flag_l0) {
426          slice->num_ref_list0_mod_operations = 0;
427          while (true) {
428             modification_of_pic_nums_idc = vl_rbsp_ue(rbsp);
429             if (modification_of_pic_nums_idc == 3)
430                break;
431             struct pipe_h264_ref_list_mod_entry *op =
432                &slice->ref_list0_mod_operations[slice->num_ref_list0_mod_operations++];
433             op->modification_of_pic_nums_idc = modification_of_pic_nums_idc;
434             if (op->modification_of_pic_nums_idc == 0 ||
435                 op->modification_of_pic_nums_idc == 1)
436                op->abs_diff_pic_num_minus1 = vl_rbsp_ue(rbsp);
437             else if (op->modification_of_pic_nums_idc == 2)
438                op->long_term_pic_num = vl_rbsp_ue(rbsp);
439          }
440       }
441    }
442 
443    if (slice->slice_type == PIPE_H264_SLICE_TYPE_B) {
444       slice->ref_pic_list_modification_flag_l1 = vl_rbsp_u(rbsp, 1);
445       if (slice->ref_pic_list_modification_flag_l1) {
446          slice->num_ref_list1_mod_operations = 0;
447          while (true) {
448             modification_of_pic_nums_idc = vl_rbsp_ue(rbsp);
449             if (modification_of_pic_nums_idc == 3)
450                break;
451             struct pipe_h264_ref_list_mod_entry *op =
452                &slice->ref_list1_mod_operations[slice->num_ref_list1_mod_operations++];
453             op->modification_of_pic_nums_idc = modification_of_pic_nums_idc;
454             if (op->modification_of_pic_nums_idc == 0 ||
455                 op->modification_of_pic_nums_idc == 1)
456                op->abs_diff_pic_num_minus1 = vl_rbsp_ue(rbsp);
457             else if (op->modification_of_pic_nums_idc == 2)
458                op->long_term_pic_num = vl_rbsp_ue(rbsp);
459          }
460       }
461    }
462 
463    if (nal_ref_idc != 0) {
464       if (nal_unit_type == PIPE_H264_NAL_IDR_SLICE) {
465          slice->no_output_of_prior_pics_flag = vl_rbsp_u(rbsp, 1);
466          slice->long_term_reference_flag = vl_rbsp_u(rbsp, 1);
467       } else {
468          slice->adaptive_ref_pic_marking_mode_flag = vl_rbsp_u(rbsp, 1);
469          if (slice->adaptive_ref_pic_marking_mode_flag) {
470             slice->num_ref_pic_marking_operations = 0;
471             while (true) {
472                memory_management_control_operation = vl_rbsp_ue(rbsp);
473                if (memory_management_control_operation == 0)
474                   break;
475                struct pipe_h264_ref_pic_marking_entry *op =
476                   &slice->ref_pic_marking_operations[slice->num_ref_pic_marking_operations++];
477                op->memory_management_control_operation = memory_management_control_operation;
478                if (memory_management_control_operation == 1 ||
479                    memory_management_control_operation == 3)
480                   op->difference_of_pic_nums_minus1 = vl_rbsp_ue(rbsp);
481                if (memory_management_control_operation == 2)
482                   op->long_term_pic_num = vl_rbsp_ue(rbsp);
483                if (memory_management_control_operation == 3 ||
484                    memory_management_control_operation == 6)
485                   op->long_term_frame_idx = vl_rbsp_ue(rbsp);
486                if (memory_management_control_operation == 4)
487                   op->max_long_term_frame_idx_plus1 = vl_rbsp_ue(rbsp);
488             }
489          }
490       }
491    }
492 
493    if (pic->entropy_coding_mode_flag &&
494        slice->slice_type != PIPE_H264_SLICE_TYPE_I &&
495        slice->slice_type != PIPE_H264_SLICE_TYPE_SI)
496       slice->cabac_init_idc = vl_rbsp_ue(rbsp);
497 
498    slice->slice_qp_delta = vl_rbsp_se(rbsp);
499 
500    if (slice->slice_type == PIPE_H264_SLICE_TYPE_SP ||
501        slice->slice_type == PIPE_H264_SLICE_TYPE_SI) {
502       if (slice->slice_type == PIPE_H264_SLICE_TYPE_SP)
503          vl_rbsp_u(rbsp, 1); /* sp_for_switch_flag */
504       vl_rbsp_se(rbsp); /* slice_qs_delta */
505    }
506 
507    if (pic->deblocking_filter_control_present_flag) {
508       slice->disable_deblocking_filter_idc = vl_rbsp_ue(rbsp);
509       if (slice->disable_deblocking_filter_idc != 1) {
510          slice->slice_alpha_c0_offset_div2 = vl_rbsp_se(rbsp);
511          slice->slice_beta_offset_div2 = vl_rbsp_se(rbsp);
512       }
513    }
514 }
515 
parseEncHrdParamsH264(struct vl_rbsp * rbsp,pipe_h264_enc_hrd_params * hrd_params)516 static void parseEncHrdParamsH264(struct vl_rbsp *rbsp, pipe_h264_enc_hrd_params* hrd_params)
517 {
518    unsigned i;
519 
520    hrd_params->cpb_cnt_minus1 = vl_rbsp_ue(rbsp);
521    hrd_params->bit_rate_scale = vl_rbsp_u(rbsp, 4);
522    hrd_params->cpb_size_scale = vl_rbsp_u(rbsp, 4);
523    for (i = 0; i <= hrd_params->cpb_cnt_minus1; ++i) {
524       hrd_params->bit_rate_value_minus1[i] = vl_rbsp_ue(rbsp);
525       hrd_params->cpb_size_value_minus1[i] = vl_rbsp_ue(rbsp);
526       hrd_params->cbr_flag[i] = vl_rbsp_u(rbsp, 1);
527    }
528    hrd_params->initial_cpb_removal_delay_length_minus1 = vl_rbsp_u(rbsp, 5);
529    hrd_params->cpb_removal_delay_length_minus1 = vl_rbsp_u(rbsp, 5);
530    hrd_params->dpb_output_delay_length_minus1 = vl_rbsp_u(rbsp, 5);
531    hrd_params->time_offset_length = vl_rbsp_u(rbsp, 5);
532 }
533 
parseEncSpsParamsH264(vlVaContext * context,struct vl_rbsp * rbsp)534 static void parseEncSpsParamsH264(vlVaContext *context, struct vl_rbsp *rbsp)
535 {
536    unsigned i, profile_idc, num_ref_frames_in_pic_order_cnt_cycle;
537 
538    context->desc.h264enc.seq.profile_idc = vl_rbsp_u(rbsp, 8);
539    context->desc.h264enc.seq.enc_constraint_set_flags = vl_rbsp_u(rbsp, 6);
540    vl_rbsp_u(rbsp, 2); /* reserved_zero_2bits */
541    context->desc.h264enc.seq.level_idc = vl_rbsp_u(rbsp, 8);
542 
543    vl_rbsp_ue(rbsp); /* seq_parameter_set_id */
544 
545    profile_idc = context->desc.h264enc.seq.profile_idc;
546    if (profile_idc == 100 || profile_idc == 110 ||
547        profile_idc == 122 || profile_idc == 244 || profile_idc == 44 ||
548        profile_idc == 83 || profile_idc == 86 || profile_idc == 118 ||
549        profile_idc == 128 || profile_idc == 138 || profile_idc == 139 ||
550        profile_idc == 134 || profile_idc == 135) {
551 
552       if (vl_rbsp_ue(rbsp) == 3) /* chroma_format_idc */
553          vl_rbsp_u(rbsp, 1); /* separate_colour_plane_flag */
554 
555       context->desc.h264enc.seq.bit_depth_luma_minus8 = vl_rbsp_ue(rbsp);
556       context->desc.h264enc.seq.bit_depth_chroma_minus8 = vl_rbsp_ue(rbsp);
557       vl_rbsp_u(rbsp, 1); /* qpprime_y_zero_transform_bypass_flag */
558 
559       if (vl_rbsp_u(rbsp, 1)) { /* seq_scaling_matrix_present_flag */
560          debug_error("SPS scaling matrix not supported");
561          return;
562       }
563    }
564 
565    context->desc.h264enc.seq.log2_max_frame_num_minus4 = vl_rbsp_ue(rbsp);
566    context->desc.h264enc.seq.pic_order_cnt_type = vl_rbsp_ue(rbsp);
567 
568    if (context->desc.h264enc.seq.pic_order_cnt_type == 0)
569       context->desc.h264enc.seq.log2_max_pic_order_cnt_lsb_minus4 = vl_rbsp_ue(rbsp);
570    else if (context->desc.h264enc.seq.pic_order_cnt_type == 1) {
571       vl_rbsp_u(rbsp, 1); /* delta_pic_order_always_zero_flag */
572       vl_rbsp_se(rbsp); /* offset_for_non_ref_pic */
573       vl_rbsp_se(rbsp); /* offset_for_top_to_bottom_field */
574       num_ref_frames_in_pic_order_cnt_cycle = vl_rbsp_ue(rbsp);
575       for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i)
576          vl_rbsp_se(rbsp); /* offset_for_ref_frame[i] */
577    }
578 
579    context->desc.h264enc.seq.max_num_ref_frames = vl_rbsp_ue(rbsp);
580    context->desc.h264enc.seq.gaps_in_frame_num_value_allowed_flag = vl_rbsp_u(rbsp, 1);
581    context->desc.h264enc.seq.pic_width_in_mbs_minus1 = vl_rbsp_ue(rbsp);
582    context->desc.h264enc.seq.pic_height_in_map_units_minus1 = vl_rbsp_ue(rbsp);
583    if (!vl_rbsp_u(rbsp, 1)) /* frame_mbs_only_flag */
584       vl_rbsp_u(rbsp, 1); /* mb_adaptive_frame_field_flag */
585 
586    context->desc.h264enc.seq.direct_8x8_inference_flag = vl_rbsp_u(rbsp, 1);
587    context->desc.h264enc.seq.enc_frame_cropping_flag = vl_rbsp_u(rbsp, 1);
588    if (context->desc.h264enc.seq.enc_frame_cropping_flag) {
589       context->desc.h264enc.seq.enc_frame_crop_left_offset = vl_rbsp_ue(rbsp);
590       context->desc.h264enc.seq.enc_frame_crop_right_offset = vl_rbsp_ue(rbsp);
591       context->desc.h264enc.seq.enc_frame_crop_top_offset = vl_rbsp_ue(rbsp);
592       context->desc.h264enc.seq.enc_frame_crop_bottom_offset = vl_rbsp_ue(rbsp);
593    }
594 
595    context->desc.h264enc.seq.vui_parameters_present_flag = vl_rbsp_u(rbsp, 1);
596    if (context->desc.h264enc.seq.vui_parameters_present_flag) {
597       context->desc.h264enc.seq.vui_flags.aspect_ratio_info_present_flag = vl_rbsp_u(rbsp, 1);
598       if (context->desc.h264enc.seq.vui_flags.aspect_ratio_info_present_flag) {
599          context->desc.h264enc.seq.aspect_ratio_idc = vl_rbsp_u(rbsp, 8);
600          if (context->desc.h264enc.seq.aspect_ratio_idc == PIPE_H2645_EXTENDED_SAR) {
601             context->desc.h264enc.seq.sar_width = vl_rbsp_u(rbsp, 16);
602             context->desc.h264enc.seq.sar_height = vl_rbsp_u(rbsp, 16);
603          }
604       }
605 
606       context->desc.h264enc.seq.vui_flags.overscan_info_present_flag = vl_rbsp_u(rbsp, 1);
607       if (context->desc.h264enc.seq.vui_flags.overscan_info_present_flag)
608          context->desc.h264enc.seq.vui_flags.overscan_appropriate_flag = vl_rbsp_u(rbsp, 1);
609 
610       context->desc.h264enc.seq.vui_flags.video_signal_type_present_flag = vl_rbsp_u(rbsp, 1);
611       if (context->desc.h264enc.seq.vui_flags.video_signal_type_present_flag) {
612          context->desc.h264enc.seq.video_format = vl_rbsp_u(rbsp, 3);
613          context->desc.h264enc.seq.video_full_range_flag = vl_rbsp_u(rbsp, 1);
614          context->desc.h264enc.seq.vui_flags.colour_description_present_flag = vl_rbsp_u(rbsp, 1);
615          if (context->desc.h264enc.seq.vui_flags.colour_description_present_flag) {
616             context->desc.h264enc.seq.colour_primaries = vl_rbsp_u(rbsp, 8);
617             context->desc.h264enc.seq.transfer_characteristics = vl_rbsp_u(rbsp, 8);
618             context->desc.h264enc.seq.matrix_coefficients = vl_rbsp_u(rbsp, 8);
619          }
620       }
621 
622       context->desc.h264enc.seq.vui_flags.chroma_loc_info_present_flag = vl_rbsp_u(rbsp, 1);
623       if (context->desc.h264enc.seq.vui_flags.chroma_loc_info_present_flag) {
624          context->desc.h264enc.seq.chroma_sample_loc_type_top_field = vl_rbsp_ue(rbsp);
625          context->desc.h264enc.seq.chroma_sample_loc_type_bottom_field = vl_rbsp_ue(rbsp);
626       }
627 
628       context->desc.h264enc.seq.vui_flags.timing_info_present_flag = vl_rbsp_u(rbsp, 1);
629       if (context->desc.h264enc.seq.vui_flags.timing_info_present_flag) {
630          context->desc.h264enc.seq.num_units_in_tick = vl_rbsp_u(rbsp, 32);
631          context->desc.h264enc.seq.time_scale = vl_rbsp_u(rbsp, 32);
632          context->desc.h264enc.seq.vui_flags.fixed_frame_rate_flag = vl_rbsp_u(rbsp, 1);
633       }
634 
635       context->desc.h264enc.seq.vui_flags.nal_hrd_parameters_present_flag = vl_rbsp_u(rbsp, 1);
636       if (context->desc.h264enc.seq.vui_flags.nal_hrd_parameters_present_flag)
637          parseEncHrdParamsH264(rbsp, &context->desc.h264enc.seq.nal_hrd_parameters);
638 
639       context->desc.h264enc.seq.vui_flags.vcl_hrd_parameters_present_flag = vl_rbsp_u(rbsp, 1);
640       if (context->desc.h264enc.seq.vui_flags.vcl_hrd_parameters_present_flag)
641          parseEncHrdParamsH264(rbsp, &context->desc.h264enc.seq.vcl_hrd_parameters);
642 
643       if (context->desc.h264enc.seq.vui_flags.nal_hrd_parameters_present_flag ||
644           context->desc.h264enc.seq.vui_flags.vcl_hrd_parameters_present_flag)
645          context->desc.h264enc.seq.vui_flags.low_delay_hrd_flag = vl_rbsp_u(rbsp, 1);
646 
647       context->desc.h264enc.seq.vui_flags.pic_struct_present_flag = vl_rbsp_u(rbsp, 1);
648 
649       context->desc.h264enc.seq.vui_flags.bitstream_restriction_flag = vl_rbsp_u(rbsp, 1);
650       if (context->desc.h264enc.seq.vui_flags.bitstream_restriction_flag) {
651          context->desc.h264enc.seq.vui_flags.motion_vectors_over_pic_boundaries_flag = vl_rbsp_u(rbsp, 1);
652          context->desc.h264enc.seq.max_bytes_per_pic_denom = vl_rbsp_ue(rbsp);
653          context->desc.h264enc.seq.max_bits_per_mb_denom = vl_rbsp_ue(rbsp);
654          context->desc.h264enc.seq.log2_max_mv_length_horizontal = vl_rbsp_ue(rbsp);
655          context->desc.h264enc.seq.log2_max_mv_length_vertical = vl_rbsp_ue(rbsp);
656          context->desc.h264enc.seq.max_num_reorder_frames = vl_rbsp_ue(rbsp);
657          context->desc.h264enc.seq.max_dec_frame_buffering = vl_rbsp_ue(rbsp);
658       }
659    }
660 }
661 
slice_group_map(struct vl_rbsp * rbsp,unsigned num_slice_groups_minus1)662 static void slice_group_map(struct vl_rbsp *rbsp, unsigned num_slice_groups_minus1)
663 {
664    unsigned slice_group_map_type = vl_rbsp_ue(rbsp);
665    if (slice_group_map_type == 0) {
666       for (unsigned i = 0; i <= num_slice_groups_minus1; i++)
667          vl_rbsp_ue(rbsp); /* run_length_minus1[i] */
668    } else if (slice_group_map_type == 2) {
669       for (unsigned i = 0; i <= num_slice_groups_minus1; i++) {
670          vl_rbsp_ue(rbsp); /* top_left[i] */
671          vl_rbsp_ue(rbsp); /* bottom_right[i] */
672       }
673    } else if (slice_group_map_type == 3 ||
674               slice_group_map_type == 4 ||
675               slice_group_map_type == 5) {
676       vl_rbsp_u(rbsp, 1); /* slice_group_change_direction_flag */
677       vl_rbsp_ue(rbsp); /* slice_group_change_rate_minus1 */
678    } else if (slice_group_map_type == 6) {
679       unsigned pic_size_in_map_units_minus1 = vl_rbsp_ue(rbsp);
680       for (unsigned i = 0; i <= pic_size_in_map_units_minus1; i++)
681          vl_rbsp_u(rbsp, util_logbase2_ceil(num_slice_groups_minus1 + 1)); /* slice_group_id[i] */
682    }
683 }
684 
parseEncPpsParamsH264(vlVaContext * context,struct vl_rbsp * rbsp)685 static void parseEncPpsParamsH264(vlVaContext *context, struct vl_rbsp *rbsp)
686 {
687    struct pipe_h264_enc_pic_control *pic = &context->desc.h264enc.pic_ctrl;
688 
689    vl_rbsp_ue(rbsp); /* pic_parameter_set_id */
690    vl_rbsp_ue(rbsp); /* seq_parameter_set_id */
691    pic->entropy_coding_mode_flag = vl_rbsp_u(rbsp, 1);
692    vl_rbsp_u(rbsp, 1); /* bottom_field_pic_order_in_frame_present_flag */
693    unsigned num_slice_groups_minus1 = vl_rbsp_ue(rbsp);
694    if (num_slice_groups_minus1 > 0)
695       slice_group_map(rbsp, num_slice_groups_minus1);
696    pic->num_ref_idx_l0_default_active_minus1 = vl_rbsp_ue(rbsp);
697    pic->num_ref_idx_l1_default_active_minus1 = vl_rbsp_ue(rbsp);
698    pic->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
699    pic->weighted_bipred_idc = vl_rbsp_u(rbsp, 2);
700    pic->pic_init_qp_minus26 = vl_rbsp_se(rbsp);
701    pic->pic_init_qs_minus26 = vl_rbsp_se(rbsp);
702    pic->chroma_qp_index_offset = vl_rbsp_se(rbsp);
703    pic->deblocking_filter_control_present_flag = vl_rbsp_u(rbsp, 1);
704    pic->constrained_intra_pred_flag = vl_rbsp_u(rbsp, 1);
705    pic->redundant_pic_cnt_present_flag = vl_rbsp_u(rbsp, 1);
706    if (vl_rbsp_more_data(rbsp)) {
707       pic->transform_8x8_mode_flag = vl_rbsp_u(rbsp, 1);
708       if (vl_rbsp_u(rbsp, 1)) { /* pic_scaling_matrix_present_flag */
709          debug_error("PPS scaling matrix not supported");
710          return;
711       }
712       pic->second_chroma_qp_index_offset = vl_rbsp_se(rbsp);
713    } else {
714       pic->transform_8x8_mode_flag = 0;
715       pic->second_chroma_qp_index_offset = pic->chroma_qp_index_offset;
716    }
717 }
718 
719 VAStatus
vlVaHandleVAEncPackedHeaderDataBufferTypeH264(vlVaContext * context,vlVaBuffer * buf)720 vlVaHandleVAEncPackedHeaderDataBufferTypeH264(vlVaContext *context, vlVaBuffer *buf)
721 {
722    struct vl_vlc vlc = {0};
723    uint8_t *data = buf->data;
724    int nal_start = -1;
725    unsigned nal_unit_type = 0, emulation_bytes_start = 0;
726    bool is_slice = false;
727 
728    vl_vlc_init(&vlc, 1, (const void * const*)&data, &buf->size);
729 
730    while (vl_vlc_bits_left(&vlc) > 0) {
731       /* search the first 64 bytes for a startcode */
732       for (int i = 0; i < 64 && vl_vlc_bits_left(&vlc) >= 24; ++i) {
733          if (vl_vlc_peekbits(&vlc, 24) == 0x000001)
734             break;
735          vl_vlc_eatbits(&vlc, 8);
736          vl_vlc_fillbits(&vlc);
737       }
738 
739       unsigned start = vlc.data - data - vl_vlc_valid_bits(&vlc) / 8;
740       emulation_bytes_start = 4; /* 3 bytes startcode + 1 byte header */
741       /* handle 4 bytes startcode */
742       if (start > 0 && data[start - 1] == 0x00) {
743          start--;
744          emulation_bytes_start++;
745       }
746       if (nal_start >= 0) {
747          vlVaAddRawHeader(&context->desc.h264enc.raw_headers, nal_unit_type,
748                           start - nal_start, data + nal_start, is_slice, 0);
749       }
750       nal_start = start;
751       is_slice = false;
752 
753       vl_vlc_eatbits(&vlc, 24); /* eat the startcode */
754 
755       if (vl_vlc_valid_bits(&vlc) < 15)
756          vl_vlc_fillbits(&vlc);
757 
758       vl_vlc_eatbits(&vlc, 1);
759       unsigned nal_ref_idc = vl_vlc_get_uimsbf(&vlc, 2);
760       nal_unit_type = vl_vlc_get_uimsbf(&vlc, 5);
761 
762       struct vl_rbsp rbsp;
763       vl_rbsp_init(&rbsp, &vlc, ~0, context->packed_header_emulation_bytes);
764 
765       switch (nal_unit_type) {
766       case PIPE_H264_NAL_SLICE:
767       case PIPE_H264_NAL_IDR_SLICE:
768          is_slice = true;
769          parseEncSliceParamsH264(context, &rbsp, nal_ref_idc, nal_unit_type);
770          break;
771       case PIPE_H264_NAL_SPS:
772          parseEncSpsParamsH264(context, &rbsp);
773          break;
774       case PIPE_H264_NAL_PPS:
775          parseEncPpsParamsH264(context, &rbsp);
776          break;
777       default:
778          break;
779       }
780 
781       if (!context->packed_header_emulation_bytes)
782          break;
783    }
784 
785    if (nal_start >= 0) {
786       vlVaAddRawHeader(&context->desc.h264enc.raw_headers, nal_unit_type,
787                        buf->size - nal_start, data + nal_start, is_slice,
788                        context->packed_header_emulation_bytes ? 0 : emulation_bytes_start);
789    }
790 
791    return VA_STATUS_SUCCESS;
792 }
793 
794 VAStatus
vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)795 vlVaHandleVAEncMiscParameterTypeTemporalLayerH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
796 {
797    VAEncMiscParameterTemporalLayerStructure *tl = (VAEncMiscParameterTemporalLayerStructure *)misc->data;
798 
799    context->desc.h264enc.seq.num_temporal_layers = tl->number_of_layers;
800 
801    return VA_STATUS_SUCCESS;
802 }
803 
804 VAStatus
vlVaHandleVAEncMiscParameterTypeQualityLevelH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)805 vlVaHandleVAEncMiscParameterTypeQualityLevelH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
806 {
807    VAEncMiscParameterBufferQualityLevel *ql = (VAEncMiscParameterBufferQualityLevel *)misc->data;
808    vlVaHandleVAEncMiscParameterTypeQualityLevel(&context->desc.h264enc.quality_modes,
809                                (vlVaQualityBits *)&ql->quality_level);
810 
811    return VA_STATUS_SUCCESS;
812 }
813 
814 VAStatus
vlVaHandleVAEncMiscParameterTypeMaxFrameSizeH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)815 vlVaHandleVAEncMiscParameterTypeMaxFrameSizeH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
816 {
817    VAEncMiscParameterBufferMaxFrameSize *ms = (VAEncMiscParameterBufferMaxFrameSize *)misc->data;
818    context->desc.h264enc.rate_ctrl[0].max_au_size = ms->max_frame_size;
819    return VA_STATUS_SUCCESS;
820 }
821 
822 VAStatus
vlVaHandleVAEncMiscParameterTypeHRDH264(vlVaContext * context,VAEncMiscParameterBuffer * misc)823 vlVaHandleVAEncMiscParameterTypeHRDH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)
824 {
825    VAEncMiscParameterHRD *ms = (VAEncMiscParameterHRD *)misc->data;
826 
827    if (ms->buffer_size) {
828       context->desc.h264enc.rate_ctrl[0].vbv_buffer_size = ms->buffer_size;
829       context->desc.h264enc.rate_ctrl[0].vbv_buf_lv = (ms->initial_buffer_fullness << 6 ) / ms->buffer_size;
830       context->desc.h264enc.rate_ctrl[0].vbv_buf_initial_size = ms->initial_buffer_fullness;
831       /* Distinguishes from the default params set for these values in other
832       functions and app specific params passed down via HRD buffer */
833       context->desc.h264enc.rate_ctrl[0].app_requested_hrd_buffer = true;
834    }
835 
836    return VA_STATUS_SUCCESS;
837 }
838