3 * Copyright @ 2017 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWAR OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Pengfei Qu <Pengfei.Qu@intel.com>
30 #include "i965_avc_encoder_common.h"
32 i965_avc_get_max_mbps(int level_idc)
37 case INTEL_AVC_LEVEL_2:
41 case INTEL_AVC_LEVEL_21:
45 case INTEL_AVC_LEVEL_22:
49 case INTEL_AVC_LEVEL_3:
53 case INTEL_AVC_LEVEL_31:
57 case INTEL_AVC_LEVEL_32:
61 case INTEL_AVC_LEVEL_4:
62 case INTEL_AVC_LEVEL_41:
66 case INTEL_AVC_LEVEL_42:
70 case INTEL_AVC_LEVEL_5:
74 case INTEL_AVC_LEVEL_51:
78 case INTEL_AVC_LEVEL_52:
90 i965_avc_get_profile_level_max_frame(struct avc_param * param,
93 double bits_per_mb, tmpf;
94 int max_mbps, num_mb_per_frame;
95 uint64_t max_byte_per_frame0, max_byte_per_frame1;
97 unsigned int scale_factor = 4;
100 if (level_idc >= INTEL_AVC_LEVEL_31 && level_idc <= INTEL_AVC_LEVEL_4)
108 max_mbps = i965_avc_get_max_mbps(level_idc);
109 num_mb_per_frame = param->frame_width_in_mbs * param->frame_height_in_mbs;
111 tmpf = (double)num_mb_per_frame;
113 if (tmpf < max_mbps / 172.0)
114 tmpf = max_mbps / 172.0;
116 max_byte_per_frame0 = (uint64_t)(tmpf * bits_per_mb);
117 max_byte_per_frame1 = (uint64_t)(((double)max_mbps * 100) / param->frames_per_100s * bits_per_mb);
119 /* TODO: check VAEncMiscParameterTypeMaxFrameSize */
120 ret = (unsigned int)MIN(max_byte_per_frame0, max_byte_per_frame1);
121 ret = (unsigned int)MIN(ret, param->frame_width_in_pixel * param->frame_height_in_pixel * 3 / (2 * scale_factor));
127 i965_avc_calculate_initial_qp(struct avc_param * param)
129 float x0 = 0, y0 = 1.19f, x1 = 1.75f, y1 = 1.75f;
133 frame_size = (param->frame_width_in_pixel * param->frame_height_in_pixel * 3 / 2);
134 qp = (int)(1.0 / 1.2 * pow(10.0,
135 (log10(frame_size * 2.0 / 3.0 * ((float)param->frames_per_100s) /
136 ((float)(param->target_bit_rate * 1000) * 100)) - x0) *
137 (y1 - y0) / (x1 - x0) + y0) + 0.5);
139 delat_qp = (int)(9 - (param->vbv_buffer_size_in_bit * ((float)param->frames_per_100s) /
140 ((float)(param->target_bit_rate * 1000) * 100)));
144 qp = CLAMP(1, 51, qp);
154 i965_avc_get_max_v_mv_r(int level_idc)
156 int max_v_mv_r = 128 * 4;
158 // See JVT Spec Annex A Table A-1 Level limits for below mapping
159 // MaxVmvR is in luma quarter pel unit
161 case INTEL_AVC_LEVEL_1:
164 case INTEL_AVC_LEVEL_11:
165 case INTEL_AVC_LEVEL_12:
166 case INTEL_AVC_LEVEL_13:
167 case INTEL_AVC_LEVEL_2:
168 max_v_mv_r = 128 * 4;
170 case INTEL_AVC_LEVEL_21:
171 case INTEL_AVC_LEVEL_22:
172 case INTEL_AVC_LEVEL_3:
173 max_v_mv_r = 256 * 4;
175 case INTEL_AVC_LEVEL_31:
176 case INTEL_AVC_LEVEL_32:
177 case INTEL_AVC_LEVEL_4:
178 case INTEL_AVC_LEVEL_41:
179 case INTEL_AVC_LEVEL_42:
180 case INTEL_AVC_LEVEL_5:
181 case INTEL_AVC_LEVEL_51:
182 case INTEL_AVC_LEVEL_52:
183 max_v_mv_r = 512 * 4;
194 i965_avc_get_max_mv_len(int level_idc)
196 int max_mv_len = 127;
198 // See JVT Spec Annex A Table A-1 Level limits for below mapping
199 // MaxVmvR is in luma quarter pel unit
201 case INTEL_AVC_LEVEL_1:
204 case INTEL_AVC_LEVEL_11:
205 case INTEL_AVC_LEVEL_12:
206 case INTEL_AVC_LEVEL_13:
207 case INTEL_AVC_LEVEL_2:
210 case INTEL_AVC_LEVEL_21:
211 case INTEL_AVC_LEVEL_22:
212 case INTEL_AVC_LEVEL_3:
215 case INTEL_AVC_LEVEL_31:
216 case INTEL_AVC_LEVEL_32:
217 case INTEL_AVC_LEVEL_4:
218 case INTEL_AVC_LEVEL_41:
219 case INTEL_AVC_LEVEL_42:
220 case INTEL_AVC_LEVEL_5:
221 case INTEL_AVC_LEVEL_51:
222 case INTEL_AVC_LEVEL_52:
234 i965_avc_get_max_mv_per_2mb(int level_idc)
236 unsigned int max_mv_per_2mb = 32;
238 // See JVT Spec Annex A Table A-1 Level limits for below mapping
240 case INTEL_AVC_LEVEL_3:
243 case INTEL_AVC_LEVEL_31:
244 case INTEL_AVC_LEVEL_32:
245 case INTEL_AVC_LEVEL_4:
246 case INTEL_AVC_LEVEL_41:
247 case INTEL_AVC_LEVEL_42:
248 case INTEL_AVC_LEVEL_5:
249 case INTEL_AVC_LEVEL_51:
250 case INTEL_AVC_LEVEL_52:
257 return max_mv_per_2mb;
261 i965_avc_calc_skip_value(unsigned int enc_block_based_sip_en, unsigned int transform_8x8_flag, unsigned short skip_value)
263 if (!enc_block_based_sip_en) {
265 } else if (!transform_8x8_flag) {
272 unsigned short i965_avc_get_maxnum_slices_num(int profile_idc, int level_idc, unsigned int frames_per_100s)
274 unsigned int slice_num = 0;
276 if ((profile_idc == VAProfileH264Main) ||
277 (profile_idc == VAProfileH264High)) {
279 case INTEL_AVC_LEVEL_3:
280 slice_num = (unsigned int)(40500.0 * 100 / 22.0 / frames_per_100s);
282 case INTEL_AVC_LEVEL_31:
283 slice_num = (unsigned int)(108000.0 * 100 / 60.0 / frames_per_100s);
285 case INTEL_AVC_LEVEL_32:
286 slice_num = (unsigned int)(216000.0 * 100 / 60.0 / frames_per_100s);
288 case INTEL_AVC_LEVEL_4:
289 case INTEL_AVC_LEVEL_41:
290 slice_num = (unsigned int)(245760.0 * 100 / 24.0 / frames_per_100s);
292 case INTEL_AVC_LEVEL_42:
293 slice_num = (unsigned int)(522240.0 * 100 / 24.0 / frames_per_100s);
295 case INTEL_AVC_LEVEL_5:
296 slice_num = (unsigned int)(589824.0 * 100 / 24.0 / frames_per_100s);
298 case INTEL_AVC_LEVEL_51:
299 slice_num = (unsigned int)(983040.0 * 100 / 24.0 / frames_per_100s);
301 case INTEL_AVC_LEVEL_52:
302 slice_num = (unsigned int)(2073600.0 * 100 / 24.0 / frames_per_100s);