Path: blob/21.2-virgl/src/gallium/frontends/va/picture_h264_enc.c
4561 views
/**************************************************************************1*2* Copyright 2018 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_handle_table.h"28#include "util/u_video.h"29#include "va_private.h"3031VAStatus32vlVaHandleVAEncPictureParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)33{34VAEncPictureParameterBufferH264 *h264;35vlVaBuffer *coded_buf;3637h264 = buf->data;38context->desc.h264enc.frame_num = h264->frame_num;39context->desc.h264enc.not_referenced = false;40context->desc.h264enc.pic_order_cnt = h264->CurrPic.TopFieldOrderCnt;41if (context->desc.h264enc.gop_cnt == 0)42context->desc.h264enc.i_remain = context->gop_coeff;43else if (context->desc.h264enc.frame_num == 1)44context->desc.h264enc.i_remain--;4546context->desc.h264enc.p_remain = context->desc.h264enc.gop_size - context->desc.h264enc.gop_cnt - context->desc.h264enc.i_remain;4748coded_buf = handle_table_get(drv->htab, h264->coded_buf);49if (!coded_buf->derived_surface.resource)50coded_buf->derived_surface.resource = pipe_buffer_create(drv->pipe->screen, PIPE_BIND_VERTEX_BUFFER,51PIPE_USAGE_STREAM, coded_buf->size);52context->coded_buf = coded_buf;5354_mesa_hash_table_insert(context->desc.h264enc.frame_idx,55UINT_TO_PTR(h264->CurrPic.picture_id + 1),56UINT_TO_PTR(h264->frame_num));5758if (h264->pic_fields.bits.idr_pic_flag == 1)59context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_IDR;60else61context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;6263context->desc.h264enc.quant_i_frames = h264->pic_init_qp;64context->desc.h264enc.quant_b_frames = h264->pic_init_qp;65context->desc.h264enc.quant_p_frames = h264->pic_init_qp;66context->desc.h264enc.gop_cnt++;67if (context->desc.h264enc.gop_cnt == context->desc.h264enc.gop_size)68context->desc.h264enc.gop_cnt = 0;6970return VA_STATUS_SUCCESS;71}7273VAStatus74vlVaHandleVAEncSliceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)75{76VAEncSliceParameterBufferH264 *h264;7778h264 = buf->data;79context->desc.h264enc.ref_idx_l0 = VA_INVALID_ID;80context->desc.h264enc.ref_idx_l1 = VA_INVALID_ID;8182for (int i = 0; i < 32; i++) {83if (h264->RefPicList0[i].picture_id != VA_INVALID_ID) {84if (context->desc.h264enc.ref_idx_l0 == VA_INVALID_ID)85context->desc.h264enc.ref_idx_l0 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,86UINT_TO_PTR(h264->RefPicList0[i].picture_id + 1)));87}88if (h264->RefPicList1[i].picture_id != VA_INVALID_ID && h264->slice_type == 1) {89if (context->desc.h264enc.ref_idx_l1 == VA_INVALID_ID)90context->desc.h264enc.ref_idx_l1 = PTR_TO_UINT(util_hash_table_get(context->desc.h264enc.frame_idx,91UINT_TO_PTR(h264->RefPicList1[i].picture_id + 1)));92}93}9495if (h264->slice_type == 1)96context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_B;97else if (h264->slice_type == 0)98context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_P;99else if (h264->slice_type == 2) {100if (context->desc.h264enc.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)101context->desc.h264enc.idr_pic_id++;102else103context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_I;104} else105context->desc.h264enc.picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP;106107return VA_STATUS_SUCCESS;108}109110VAStatus111vlVaHandleVAEncSequenceParameterBufferTypeH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)112{113VAEncSequenceParameterBufferH264 *h264 = (VAEncSequenceParameterBufferH264 *)buf->data;114if (!context->decoder) {115context->templat.max_references = h264->max_num_ref_frames;116context->templat.level = h264->level_idc;117context->decoder = drv->pipe->create_video_codec(drv->pipe, &context->templat);118if (!context->decoder)119return VA_STATUS_ERROR_ALLOCATION_FAILED;120}121122context->gop_coeff = ((1024 + h264->intra_idr_period - 1) / h264->intra_idr_period + 1) / 2 * 2;123if (context->gop_coeff > VL_VA_ENC_GOP_COEFF)124context->gop_coeff = VL_VA_ENC_GOP_COEFF;125context->desc.h264enc.gop_size = h264->intra_idr_period * context->gop_coeff;126context->desc.h264enc.rate_ctrl.frame_rate_num = h264->time_scale / 2;127context->desc.h264enc.rate_ctrl.frame_rate_den = h264->num_units_in_tick;128context->desc.h264enc.pic_order_cnt_type = h264->seq_fields.bits.pic_order_cnt_type;129130if (h264->frame_cropping_flag) {131context->desc.h264enc.pic_ctrl.enc_frame_cropping_flag = h264->frame_cropping_flag;132context->desc.h264enc.pic_ctrl.enc_frame_crop_left_offset = h264->frame_crop_left_offset;133context->desc.h264enc.pic_ctrl.enc_frame_crop_right_offset = h264->frame_crop_right_offset;134context->desc.h264enc.pic_ctrl.enc_frame_crop_top_offset = h264->frame_crop_top_offset;135context->desc.h264enc.pic_ctrl.enc_frame_crop_bottom_offset = h264->frame_crop_bottom_offset;136}137return VA_STATUS_SUCCESS;138}139140VAStatus141vlVaHandleVAEncMiscParameterTypeRateControlH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)142{143VAEncMiscParameterRateControl *rc = (VAEncMiscParameterRateControl *)misc->data;144if (context->desc.h264enc.rate_ctrl.rate_ctrl_method ==145PIPE_H2645_ENC_RATE_CONTROL_METHOD_CONSTANT)146context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second;147else148context->desc.h264enc.rate_ctrl.target_bitrate = rc->bits_per_second * (rc->target_percentage / 100.0);149context->desc.h264enc.rate_ctrl.peak_bitrate = rc->bits_per_second;150if (context->desc.h264enc.rate_ctrl.target_bitrate < 2000000)151context->desc.h264enc.rate_ctrl.vbv_buffer_size = MIN2((context->desc.h264enc.rate_ctrl.target_bitrate * 2.75), 2000000);152else153context->desc.h264enc.rate_ctrl.vbv_buffer_size = context->desc.h264enc.rate_ctrl.target_bitrate;154155return VA_STATUS_SUCCESS;156}157158VAStatus159vlVaHandleVAEncMiscParameterTypeFrameRateH264(vlVaContext *context, VAEncMiscParameterBuffer *misc)160{161VAEncMiscParameterFrameRate *fr = (VAEncMiscParameterFrameRate *)misc->data;162if (fr->framerate & 0xffff0000) {163context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate & 0xffff;164context->desc.h264enc.rate_ctrl.frame_rate_den = fr->framerate >> 16 & 0xffff;165} else {166context->desc.h264enc.rate_ctrl.frame_rate_num = fr->framerate;167context->desc.h264enc.rate_ctrl.frame_rate_den = 1;168}169return VA_STATUS_SUCCESS;170}171172void getEncParamPresetH264(vlVaContext *context)173{174//motion estimation preset175context->desc.h264enc.motion_est.motion_est_quarter_pixel = 0x00000001;176context->desc.h264enc.motion_est.lsmvert = 0x00000002;177context->desc.h264enc.motion_est.enc_disable_sub_mode = 0x00000078;178context->desc.h264enc.motion_est.enc_en_ime_overw_dis_subm = 0x00000001;179context->desc.h264enc.motion_est.enc_ime_overw_dis_subm_no = 0x00000001;180context->desc.h264enc.motion_est.enc_ime2_search_range_x = 0x00000004;181context->desc.h264enc.motion_est.enc_ime2_search_range_y = 0x00000004;182183//pic control preset184context->desc.h264enc.pic_ctrl.enc_cabac_enable = 0x00000001;185context->desc.h264enc.pic_ctrl.enc_constraint_set_flags = 0x00000040;186187//rate control188context->desc.h264enc.rate_ctrl.vbv_buffer_size = 20000000;189context->desc.h264enc.rate_ctrl.vbv_buf_lv = 48;190context->desc.h264enc.rate_ctrl.fill_data_enable = 1;191context->desc.h264enc.rate_ctrl.enforce_hrd = 1;192context->desc.h264enc.enable_vui = false;193if (context->desc.h264enc.rate_ctrl.frame_rate_num == 0 ||194context->desc.h264enc.rate_ctrl.frame_rate_den == 0) {195context->desc.h264enc.rate_ctrl.frame_rate_num = 30;196context->desc.h264enc.rate_ctrl.frame_rate_den = 1;197}198context->desc.h264enc.rate_ctrl.target_bits_picture =199context->desc.h264enc.rate_ctrl.target_bitrate *200((float)context->desc.h264enc.rate_ctrl.frame_rate_den /201context->desc.h264enc.rate_ctrl.frame_rate_num);202context->desc.h264enc.rate_ctrl.peak_bits_picture_integer =203context->desc.h264enc.rate_ctrl.peak_bitrate *204((float)context->desc.h264enc.rate_ctrl.frame_rate_den /205context->desc.h264enc.rate_ctrl.frame_rate_num);206207context->desc.h264enc.rate_ctrl.peak_bits_picture_fraction = 0;208context->desc.h264enc.ref_pic_mode = 0x00000201;209}210211212