1
2 /*
3 * Copyright @ 2017 Intel Corporation
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 * Authors:
26 * Pengfei Qu <Pengfei.Qu@intel.com>
27 *
28 */
29
30 #include "i965_avc_encoder_common.h"
31
32 // H.264 table A-1 - level limits.
33 static const struct avc_level_limits {
34 int level_idc;
35 int max_mbps;
36 int max_fs;
37 int max_dpb_mbs;
38 int max_v_mv_r;
39 int max_mvs_per_2mb;
40 } avc_level_limits[] = {
41 { INTEL_AVC_LEVEL_1, 1485, 99, 396, 64, 0 },
42 { INTEL_AVC_LEVEL_11, 3000, 396, 900, 128, 0 },
43 { INTEL_AVC_LEVEL_12, 6000, 396, 2376, 128, 0 },
44 { INTEL_AVC_LEVEL_13, 11880, 396, 2376, 128, 0 },
45 { INTEL_AVC_LEVEL_2, 11880, 396, 2376, 128, 0 },
46 { INTEL_AVC_LEVEL_21, 19800, 792, 4752, 256, 0 },
47 { INTEL_AVC_LEVEL_22, 20250, 1620, 8100, 256, 0 },
48 { INTEL_AVC_LEVEL_3, 40500, 1620, 8100, 256, 32 },
49 { INTEL_AVC_LEVEL_31, 108000, 3600, 18000, 512, 16 },
50 { INTEL_AVC_LEVEL_32, 216000, 5120, 20480, 512, 16 },
51 { INTEL_AVC_LEVEL_4, 245760, 8192, 32768, 512, 16 },
52 { INTEL_AVC_LEVEL_41, 245760, 8192, 32768, 512, 16 },
53 { INTEL_AVC_LEVEL_42, 522240, 8704, 34816, 512, 16 },
54 { INTEL_AVC_LEVEL_5, 589824, 22080, 110400, 512, 16 },
55 { INTEL_AVC_LEVEL_51, 983040, 36864, 184320, 512, 16 },
56 { INTEL_AVC_LEVEL_52, 2073600, 36864, 184320, 512, 16 },
57 { INTEL_AVC_LEVEL_6, 4177920, 139264, 696320, 8192, 16 },
58 { INTEL_AVC_LEVEL_61, 8355840, 139264, 696320, 8192, 16 },
59 { INTEL_AVC_LEVEL_62, 16711680, 139264, 696320, 8192, 16 },
60 };
61
62 static const struct avc_level_limits*
get_level_limits(int level_idc)63 get_level_limits(int level_idc)
64 {
65 int i;
66 for (i = 1; i < ARRAY_ELEMS(avc_level_limits); i++) {
67 if (level_idc < avc_level_limits[i].level_idc)
68 break;
69 }
70 return &avc_level_limits[i - 1];
71 }
72
73 int
i965_avc_level_is_valid(int level_idc)74 i965_avc_level_is_valid(int level_idc)
75 {
76 return get_level_limits(level_idc)->level_idc == level_idc;
77 }
78
79 int
i965_avc_get_max_mbps(int level_idc)80 i965_avc_get_max_mbps(int level_idc)
81 {
82 return get_level_limits(level_idc)->max_mbps;
83 };
84
85 unsigned int
i965_avc_get_profile_level_max_frame(struct avc_param * param,int level_idc)86 i965_avc_get_profile_level_max_frame(struct avc_param * param,
87 int level_idc)
88 {
89 double bits_per_mb, tmpf;
90 int max_mbps, num_mb_per_frame;
91 uint64_t max_byte_per_frame0, max_byte_per_frame1;
92 unsigned int ret;
93 unsigned int scale_factor = 4;
94
95
96 if (level_idc >= INTEL_AVC_LEVEL_31 && level_idc <= INTEL_AVC_LEVEL_4)
97 bits_per_mb = 96.0;
98 else {
99 bits_per_mb = 192.0;
100 scale_factor = 2;
101
102 }
103
104 max_mbps = i965_avc_get_max_mbps(level_idc);
105 num_mb_per_frame = param->frame_width_in_mbs * param->frame_height_in_mbs;
106
107 tmpf = (double)num_mb_per_frame;
108
109 if (tmpf < max_mbps / 172.0)
110 tmpf = max_mbps / 172.0;
111
112 max_byte_per_frame0 = (uint64_t)(tmpf * bits_per_mb);
113 max_byte_per_frame1 = (uint64_t)(((double)max_mbps * 100) / param->frames_per_100s * bits_per_mb);
114
115 /* TODO: check VAEncMiscParameterTypeMaxFrameSize */
116 ret = (unsigned int)MIN(max_byte_per_frame0, max_byte_per_frame1);
117 ret = (unsigned int)MIN(ret, param->frame_width_in_pixel * param->frame_height_in_pixel * 3 / (2 * scale_factor));
118
119 return ret;
120 }
121
122 int
i965_avc_calculate_initial_qp(struct avc_param * param)123 i965_avc_calculate_initial_qp(struct avc_param * param)
124 {
125 float x0 = 0, y0 = 1.19f, x1 = 1.75f, y1 = 1.75f;
126 unsigned frame_size;
127 int qp, delat_qp;
128
129 frame_size = (param->frame_width_in_pixel * param->frame_height_in_pixel * 3 / 2);
130 qp = (int)(1.0 / 1.2 * pow(10.0,
131 (log10(frame_size * 2.0 / 3.0 * ((float)param->frames_per_100s) /
132 ((float)(param->target_bit_rate * 1000) * 100)) - x0) *
133 (y1 - y0) / (x1 - x0) + y0) + 0.5);
134 qp += 2;
135 delat_qp = (int)(9 - (param->vbv_buffer_size_in_bit * ((float)param->frames_per_100s) /
136 ((float)(param->target_bit_rate * 1000) * 100)));
137 if (delat_qp > 0)
138 qp += delat_qp;
139
140 qp = CLAMP(1, 51, qp);
141 qp--;
142
143 return qp;
144 }
145
146
147 int
i965_avc_get_max_mv_len(int level_idc)148 i965_avc_get_max_mv_len(int level_idc)
149 {
150 return get_level_limits(level_idc)->max_v_mv_r - 1;
151 }
152
153 int
i965_avc_get_max_mv_per_2mb(int level_idc)154 i965_avc_get_max_mv_per_2mb(int level_idc)
155 {
156 return get_level_limits(level_idc)->max_mvs_per_2mb;
157 }
158
159 unsigned short
i965_avc_calc_skip_value(unsigned int enc_block_based_sip_en,unsigned int transform_8x8_flag,unsigned short skip_value)160 i965_avc_calc_skip_value(unsigned int enc_block_based_sip_en, unsigned int transform_8x8_flag, unsigned short skip_value)
161 {
162 if (!enc_block_based_sip_en) {
163 skip_value *= 3;
164 } else if (!transform_8x8_flag) {
165 skip_value /= 2;
166 }
167
168 return skip_value;
169 }
170