OSDN Git Service

8052d57a1144af231fe074b6cda99b02a49f4aae
[android-x86/hardware-intel-common-vaapi.git] / src / i965_avc_encoder_common.c
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 int
32 i965_avc_get_max_mbps(int level_idc)
33 {
34     int max_mbps = 11880;
35
36     switch (level_idc) {
37     case INTEL_AVC_LEVEL_2:
38         max_mbps = 11880;
39         break;
40
41     case INTEL_AVC_LEVEL_21:
42         max_mbps = 19800;
43         break;
44
45     case INTEL_AVC_LEVEL_22:
46         max_mbps = 20250;
47         break;
48
49     case INTEL_AVC_LEVEL_3:
50         max_mbps = 40500;
51         break;
52
53     case INTEL_AVC_LEVEL_31:
54         max_mbps = 108000;
55         break;
56
57     case INTEL_AVC_LEVEL_32:
58         max_mbps = 216000;
59         break;
60
61     case INTEL_AVC_LEVEL_4:
62     case INTEL_AVC_LEVEL_41:
63         max_mbps = 245760;
64         break;
65
66     case INTEL_AVC_LEVEL_42:
67         max_mbps = 522240;
68         break;
69
70     case INTEL_AVC_LEVEL_5:
71         max_mbps = 589824;
72         break;
73
74     case INTEL_AVC_LEVEL_51:
75         max_mbps = 983040;
76         break;
77
78     case INTEL_AVC_LEVEL_52:
79         max_mbps = 2073600;
80         break;
81
82     default:
83         break;
84     }
85
86     return max_mbps;
87 };
88
89 unsigned int
90 i965_avc_get_profile_level_max_frame(struct avc_param * param,
91                                        int level_idc)
92 {
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;
96     unsigned int ret;
97     unsigned int scale_factor = 4;
98
99
100     if (level_idc >= INTEL_AVC_LEVEL_31 && level_idc <= INTEL_AVC_LEVEL_4)
101         bits_per_mb = 96.0;
102     else
103     {
104         bits_per_mb = 192.0;
105         scale_factor = 2;
106
107     }
108
109     max_mbps = i965_avc_get_max_mbps(level_idc);
110     num_mb_per_frame = param->frame_width_in_mbs * param->frame_height_in_mbs;
111
112     tmpf = (double)num_mb_per_frame;
113
114     if (tmpf < max_mbps / 172.0)
115         tmpf = max_mbps / 172.0;
116
117     max_byte_per_frame0 = (uint64_t)(tmpf * bits_per_mb);
118     max_byte_per_frame1 = (uint64_t)(((double)max_mbps * 100) / param->frames_per_100s *bits_per_mb);
119
120     /* TODO: check VAEncMiscParameterTypeMaxFrameSize */
121     ret = (unsigned int)MIN(max_byte_per_frame0, max_byte_per_frame1);
122     ret = (unsigned int)MIN(ret, param->frame_width_in_pixel * param->frame_height_in_pixel *3 /(2*scale_factor));
123
124     return ret;
125 }
126
127 int
128 i965_avc_calculate_initial_qp(struct avc_param * param)
129 {
130     float x0 = 0, y0 = 1.19f, x1 = 1.75f, y1 = 1.75f;
131     unsigned frame_size;
132     int qp, delat_qp;
133
134     frame_size = (param->frame_width_in_pixel * param->frame_height_in_pixel * 3 / 2);
135     qp = (int)(1.0 / 1.2 * pow(10.0,
136                                (log10(frame_size * 2.0 / 3.0 * ((float)param->frames_per_100s) /
137                                       ((float)(param->target_bit_rate * 1000) * 100)) - x0) *
138                                (y1 - y0) / (x1 - x0) + y0) + 0.5);
139     qp += 2;
140     delat_qp = (int)(9 - (param->vbv_buffer_size_in_bit * ((float)param->frames_per_100s) /
141                           ((float)(param->target_bit_rate * 1000) * 100)));
142     if (delat_qp > 0)
143         qp += delat_qp;
144
145     qp = CLAMP(1, 51, qp);
146     qp--;
147
148     if (qp < 0)
149         qp = 1;
150
151     return qp;
152 }
153
154 int
155 i965_avc_get_max_v_mv_r(int level_idc)
156 {
157     int max_v_mv_r = 128 * 4;
158
159     // See JVT Spec Annex A Table A-1 Level limits for below mapping
160     // MaxVmvR is in luma quarter pel unit
161     switch (level_idc)
162     {
163     case INTEL_AVC_LEVEL_1:
164         max_v_mv_r = 64 * 4;
165         break;
166     case INTEL_AVC_LEVEL_11:
167     case INTEL_AVC_LEVEL_12:
168     case INTEL_AVC_LEVEL_13:
169     case INTEL_AVC_LEVEL_2:
170         max_v_mv_r = 128 * 4;
171         break;
172     case INTEL_AVC_LEVEL_21:
173     case INTEL_AVC_LEVEL_22:
174     case INTEL_AVC_LEVEL_3:
175         max_v_mv_r = 256 * 4;
176         break;
177     case INTEL_AVC_LEVEL_31:
178     case INTEL_AVC_LEVEL_32:
179     case INTEL_AVC_LEVEL_4:
180     case INTEL_AVC_LEVEL_41:
181     case INTEL_AVC_LEVEL_42:
182     case INTEL_AVC_LEVEL_5:
183     case INTEL_AVC_LEVEL_51:
184     case INTEL_AVC_LEVEL_52:
185         max_v_mv_r = 512 * 4;
186         break;
187     default:
188         assert(0);
189         break;
190     }
191
192     return max_v_mv_r;
193 }
194
195 int
196 i965_avc_get_max_mv_len(int level_idc)
197 {
198     int max_mv_len = 127;
199
200     // See JVT Spec Annex A Table A-1 Level limits for below mapping
201     // MaxVmvR is in luma quarter pel unit
202     switch (level_idc)
203     {
204     case INTEL_AVC_LEVEL_1:
205         max_mv_len = 63;
206         break;
207     case INTEL_AVC_LEVEL_11:
208     case INTEL_AVC_LEVEL_12:
209     case INTEL_AVC_LEVEL_13:
210     case INTEL_AVC_LEVEL_2:
211         max_mv_len = 127;
212         break;
213     case INTEL_AVC_LEVEL_21:
214     case INTEL_AVC_LEVEL_22:
215     case INTEL_AVC_LEVEL_3:
216         max_mv_len = 255;
217         break;
218     case INTEL_AVC_LEVEL_31:
219     case INTEL_AVC_LEVEL_32:
220     case INTEL_AVC_LEVEL_4:
221     case INTEL_AVC_LEVEL_41:
222     case INTEL_AVC_LEVEL_42:
223     case INTEL_AVC_LEVEL_5:
224     case INTEL_AVC_LEVEL_51:
225     case INTEL_AVC_LEVEL_52:
226         max_mv_len = 511;
227         break;
228     default:
229         assert(0);
230         break;
231     }
232
233     return max_mv_len;
234 }
235
236 int
237 i965_avc_get_max_mv_per_2mb(int level_idc)
238 {
239     unsigned int max_mv_per_2mb = 32;
240
241     // See JVT Spec Annex A Table A-1 Level limits for below mapping
242     switch (level_idc)
243     {
244     case INTEL_AVC_LEVEL_3:
245         max_mv_per_2mb = 32;
246         break;
247     case INTEL_AVC_LEVEL_31:
248     case INTEL_AVC_LEVEL_32:
249     case INTEL_AVC_LEVEL_4:
250     case INTEL_AVC_LEVEL_41:
251     case INTEL_AVC_LEVEL_42:
252     case INTEL_AVC_LEVEL_5:
253     case INTEL_AVC_LEVEL_51:
254     case INTEL_AVC_LEVEL_52:
255         max_mv_per_2mb = 16;
256         break;
257     default:
258         break;
259     }
260
261     return max_mv_per_2mb;
262 }
263
264 unsigned short
265 i965_avc_calc_skip_value(unsigned int enc_block_based_sip_en, unsigned int transform_8x8_flag, unsigned short skip_value)
266 {
267     if(!enc_block_based_sip_en)
268     {
269         skip_value *= 3;
270     }
271     else if(!transform_8x8_flag)
272     {
273         skip_value /= 2;
274     }
275
276     return skip_value;
277 }
278
279 unsigned short i965_avc_get_maxnum_slices_num(int profile_idc,int level_idc,unsigned int frames_per_100s)
280 {
281     unsigned int  slice_num = 0;
282
283     if ((profile_idc == VAProfileH264Main) ||
284         (profile_idc == VAProfileH264High))
285     {
286         switch (level_idc)
287         {
288         case INTEL_AVC_LEVEL_3:
289             slice_num = (unsigned int)(40500.0 * 100 / 22.0 / frames_per_100s);
290             break;
291         case INTEL_AVC_LEVEL_31:
292             slice_num = (unsigned int)(108000.0 * 100 / 60.0 / frames_per_100s);
293             break;
294         case INTEL_AVC_LEVEL_32:
295             slice_num = (unsigned int)(216000.0 * 100 / 60.0 / frames_per_100s);
296             break;
297         case INTEL_AVC_LEVEL_4:
298         case INTEL_AVC_LEVEL_41:
299             slice_num = (unsigned int)(245760.0 * 100 / 24.0 / frames_per_100s);
300             break;
301         case INTEL_AVC_LEVEL_42:
302             slice_num = (unsigned int)(522240.0 * 100 / 24.0 / frames_per_100s);
303             break;
304         case INTEL_AVC_LEVEL_5:
305             slice_num = (unsigned int)(589824.0 * 100 / 24.0 / frames_per_100s);
306             break;
307         case INTEL_AVC_LEVEL_51:
308             slice_num = (unsigned int)(983040.0 * 100 / 24.0 / frames_per_100s);
309             break;
310         case INTEL_AVC_LEVEL_52:
311             slice_num = (unsigned int)(2073600.0 * 100 / 24.0 / frames_per_100s);
312             break;
313         default:
314             slice_num = 0;
315         }
316     }
317
318     return slice_num;
319 }