• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /*!
22 ******************************************************************************
23 * \file ihevce_frame_process_utils.c
24 *
25 * \brief
26 *    This file contains definitions of top level functions related to frame
27 *    processing
28 *
29 * \date
30 *    18/09/2012
31 *
32 * \author
33 *    Ittiam
34 *
35 * List of Functions
36 *
37 *
38 ******************************************************************************
39 */
40 
41 /*****************************************************************************/
42 /* File Includes                                                             */
43 /*****************************************************************************/
44 /* System include files */
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <assert.h>
49 #include <stdarg.h>
50 #include <math.h>
51 
52 /* User include files */
53 #include "ihevc_typedefs.h"
54 #include "itt_video_api.h"
55 #include "ihevce_api.h"
56 
57 #include "rc_cntrl_param.h"
58 #include "rc_frame_info_collector.h"
59 #include "rc_look_ahead_params.h"
60 
61 #include "ihevc_defs.h"
62 #include "ihevc_debug.h"
63 #include "ihevc_macros.h"
64 #include "ihevc_structs.h"
65 #include "ihevc_platform_macros.h"
66 #include "ihevc_deblk.h"
67 #include "ihevc_itrans_recon.h"
68 #include "ihevc_chroma_itrans_recon.h"
69 #include "ihevc_chroma_intra_pred.h"
70 #include "ihevc_intra_pred.h"
71 #include "ihevc_inter_pred.h"
72 #include "ihevc_mem_fns.h"
73 #include "ihevc_padding.h"
74 #include "ihevc_weighted_pred.h"
75 #include "ihevc_sao.h"
76 #include "ihevc_resi_trans.h"
77 #include "ihevc_quant_iquant_ssd.h"
78 #include "ihevc_cabac_tables.h"
79 #include "ihevc_common_tables.h"
80 
81 #include "ihevce_defs.h"
82 #include "ihevce_hle_interface.h"
83 #include "ihevce_hle_q_func.h"
84 #include "ihevce_lap_enc_structs.h"
85 #include "ihevce_multi_thrd_structs.h"
86 #include "ihevce_multi_thrd_funcs.h"
87 #include "ihevce_me_common_defs.h"
88 #include "ihevce_had_satd.h"
89 #include "ihevce_error_checks.h"
90 #include "ihevce_error_codes.h"
91 #include "ihevce_bitstream.h"
92 #include "ihevce_cabac.h"
93 #include "ihevce_function_selector.h"
94 #include "ihevce_enc_structs.h"
95 #include "ihevce_global_tables.h"
96 #include "ihevce_rc_enc_structs.h"
97 #include "ihevce_rc_interface.h"
98 #include "ihevce_frame_process_utils.h"
99 
100 #include "cast_types.h"
101 #include "osal.h"
102 #include "osal_defaults.h"
103 
104 /*****************************************************************************/
105 /* Globals                                                                   */
106 /*****************************************************************************/
107 
108 /************** Version Number string *******************/
109 UWORD8 gau1_version_string[] = "i265-v4.13-218 Build ";
110 
111 /*****************************************************************************/
112 /* Function Definitions                                                      */
113 /*****************************************************************************/
114 
115 /*!
116 ******************************************************************************
117 *
118 * @brief
119 *    API to return frame qp in constant qp mode based on init I frame qp,
120 *    slice type and current temporal layer.
121 *
122 *      I picture is given the same qp as the init qp configure in static params
123 *      P picture is set equal to I frame qp + 1
124 *      B picture is set equal to P frame qp + temporal layer
125 *
126 * @param[in] static_params_frame_qp
127 *   frame level qp set for I frames in create time params
128 *
129 * @param[in] slice_type
130 *   slice type for current frame (I/P/B)
131 *
132 * @param[in] temporal_id
133 *   temoporal layer ID of the current frame. This is associalted with B frame.
134 *   temporal layer ID. I and P frames have temporal_id set to 0.
135 *
136 * @param[in] min_qp
137 *   minimum qp to be allocated for this frame.
138 *
139 * @param[in] max_qp
140 *   maximum qp to be allocated for this frame
141 *
142 * @return
143 *    current frame qp
144 *
145 * @author
146 *  Ittiam
147 *
148 * @remarks
149 *  This is right place to plug in frame level RC call for current frame qp
150 *  allocation later when RC support is added
151 *
152 *****************************************************************************
153 */
ihevce_get_cur_frame_qp(WORD32 static_params_frame_qp,WORD32 slice_type,WORD32 temporal_id,WORD32 min_qp,WORD32 max_qp,rc_quant_t * ps_rc_quant_ctxt)154 WORD32 ihevce_get_cur_frame_qp(
155     WORD32 static_params_frame_qp,
156     WORD32 slice_type,
157     WORD32 temporal_id,
158     WORD32 min_qp,
159     WORD32 max_qp,
160     rc_quant_t *ps_rc_quant_ctxt)
161 {
162     WORD32 i4_curr_qp = static_params_frame_qp;
163 
164     /* sanity checks */
165     ASSERT(max_qp >= min_qp);
166     ASSERT((min_qp >= ps_rc_quant_ctxt->i2_min_qp) && (min_qp <= ps_rc_quant_ctxt->i2_max_qp));
167     ASSERT(
168         (static_params_frame_qp >= ps_rc_quant_ctxt->i2_min_qp) &&
169         (static_params_frame_qp <= ps_rc_quant_ctxt->i2_max_qp));
170     if(ISLICE == slice_type)
171     {
172         /* I frame qp is same as init qp in static params   */
173         i4_curr_qp = static_params_frame_qp;
174     }
175     else if(PSLICE == slice_type)
176     {
177         /* P frame qp is I frame qp + 1                     */
178         i4_curr_qp = static_params_frame_qp + 1;
179     }
180     else if(BSLICE == slice_type)
181     {
182         /* B frame qp is I frame qp + 1 + temporal layer id */
183         i4_curr_qp = static_params_frame_qp + temporal_id + 1;
184     }
185     else
186     {
187         /* illegal slice type */
188         ASSERT(0);
189     }
190 
191     i4_curr_qp = CLIP3(i4_curr_qp, min_qp, max_qp);
192 
193     return (i4_curr_qp);
194 }
195 
196 /*!
197 ******************************************************************************
198 * \if Function name : calc_block_ssim \endif
199 *
200 * \brief
201 *    Calc Block SSIM
202 *
203 * \return
204 *    None
205 *
206 * \author
207 *  Ittiam
208 *****************************************************************************
209 */
calc_block_ssim(unsigned char * pu1_ref,unsigned char * pu1_tst,unsigned char * pu1_win,WORD32 i4_horz_jump,unsigned short u2_ref_stride,unsigned short u2_tst_stride,unsigned char u1_win_size,unsigned char u1_win_q_shift)210 unsigned int calc_block_ssim(
211     unsigned char *pu1_ref,
212     unsigned char *pu1_tst,
213     unsigned char *pu1_win,
214     WORD32 i4_horz_jump,
215     unsigned short u2_ref_stride,
216     unsigned short u2_tst_stride,
217     unsigned char u1_win_size,
218     unsigned char u1_win_q_shift)
219 {
220     unsigned int u4_wtd_ref_mean, u4_wtd_tst_mean, u4_wtd_ref_sq, u4_wtd_tst_sq, u4_wtd_ref_tst;
221     unsigned int u4_wtd_ref_mean_sq, u4_wtd_tst_mean_sq, u4_wtd_ref_tst_mean_prod;
222     unsigned char u1_wt, u1_ref_smpl, u1_tst_smpl;
223     unsigned short u2_wtd_ref_smpl, u2_wtd_tst_smpl, u2_win_q_rounding;
224     int i4_row, i4_col;
225 
226     u4_wtd_ref_mean = 0;
227     u4_wtd_tst_mean = 0;
228     u4_wtd_ref_sq = 0;
229     u4_wtd_tst_sq = 0;
230     u4_wtd_ref_tst = 0;
231 
232     for(i4_row = 0; i4_row < u1_win_size; i4_row++)
233     {
234         for(i4_col = 0; i4_col < u1_win_size; i4_col++)
235         {
236             u1_wt = *pu1_win++;
237             u1_ref_smpl = pu1_ref[i4_col * i4_horz_jump];
238             u1_tst_smpl = pu1_tst[i4_col * i4_horz_jump];
239 
240             u2_wtd_ref_smpl = u1_wt * u1_ref_smpl;
241             u2_wtd_tst_smpl = u1_wt * u1_tst_smpl;
242 
243             u4_wtd_ref_mean += u2_wtd_ref_smpl;
244             u4_wtd_tst_mean += u2_wtd_tst_smpl;
245 
246             u4_wtd_ref_sq += u2_wtd_ref_smpl * u1_ref_smpl;
247             u4_wtd_tst_sq += u2_wtd_tst_smpl * u1_tst_smpl;
248             u4_wtd_ref_tst += u2_wtd_ref_smpl * u1_tst_smpl;
249         }
250         pu1_ref += u2_ref_stride;
251         pu1_tst += u2_tst_stride;
252     }
253 
254     {
255         unsigned int u4_num, u4_den, u4_term1;
256 
257         u2_win_q_rounding = (1 << u1_win_q_shift) >> 1;
258         u4_wtd_ref_mean += (u2_win_q_rounding >> 8);
259         u4_wtd_tst_mean += (u2_win_q_rounding >> 8);
260 
261         /* Keep the mean terms within 16-bits before squaring */
262         u4_wtd_ref_mean >>= (u1_win_q_shift - 8);
263         u4_wtd_tst_mean >>= (u1_win_q_shift - 8);
264 
265         /* Bring down the square of sum terms to same Q format as the sum of square terms */
266         u4_wtd_ref_mean_sq = (u4_wtd_ref_mean * u4_wtd_ref_mean + 16) >> (16 - u1_win_q_shift);
267         u4_wtd_tst_mean_sq = (u4_wtd_tst_mean * u4_wtd_tst_mean + 16) >> (16 - u1_win_q_shift);
268         u4_wtd_ref_tst_mean_prod = (u4_wtd_ref_mean * u4_wtd_tst_mean + 16) >>
269                                    (16 - u1_win_q_shift);
270 
271         /* Compute self and cross variances */
272         if(u4_wtd_ref_sq > u4_wtd_ref_mean_sq)
273             u4_wtd_ref_sq -= u4_wtd_ref_mean_sq;
274         else
275             u4_wtd_ref_sq = 0;
276 
277         if(u4_wtd_tst_sq > u4_wtd_tst_mean_sq)
278             u4_wtd_tst_sq -= u4_wtd_tst_mean_sq;
279         else
280             u4_wtd_tst_sq = 0;
281 
282         if(u4_wtd_ref_tst > u4_wtd_ref_tst_mean_prod)
283             u4_wtd_ref_tst -= u4_wtd_ref_tst_mean_prod;
284         else
285             u4_wtd_ref_tst = 0;
286 
287         /* Keep the numerator in Q12 format before division */
288         u4_num = ((u4_wtd_ref_tst_mean_prod << 1) + C1) << (12 - u1_win_q_shift);
289         u4_den = ((u4_wtd_ref_mean_sq + u4_wtd_tst_mean_sq) + C1 + u2_win_q_rounding) >>
290                  u1_win_q_shift;
291         u4_term1 = (u4_num) / u4_den;
292 
293         u4_num = (u4_wtd_ref_tst << 1) + C2;
294         u4_den = (u4_wtd_ref_sq + u4_wtd_tst_sq) + C2;
295         /* If numerator takes less than 20-bits, product would not overflow; so no need to normalize */
296         if(u4_num < 1048576)
297         {
298             return ((u4_num * u4_term1) / u4_den);
299         }
300 
301         /* While the above should be done really with getRange calculation, for simplicity,
302         the other cases go through a less accurate calculation */
303         u4_num = (u4_num + u2_win_q_rounding) >> u1_win_q_shift;
304         u4_den = (u4_den + u2_win_q_rounding) >> u1_win_q_shift;
305 
306         /* What is returned is SSIM in 1Q12 */
307         return ((u4_term1 * u4_num) / u4_den);
308     }
309 }
310 
311 /*!
312 ******************************************************************************
313 * \if Function name : ihevce_fill_sei_payload \endif
314 *
315 * \brief
316 *    Fills SEI Payload
317 *
318 * \param[in]    ps_enc_ctxt
319 * Encoder Context
320 *
321 * \param[in]    ps_curr_inp
322 * Current Input pointer
323 *
324 * \param[in]    ps_curr_out
325 * Current Output pointer
326 *
327 * \return
328 *    None
329 *
330 * \author
331 *  Ittiam
332 *
333 *****************************************************************************
334 */
ihevce_fill_sei_payload(enc_ctxt_t * ps_enc_ctxt,ihevce_lap_enc_buf_t * ps_curr_inp,frm_proc_ent_cod_ctxt_t * ps_curr_out)335 void ihevce_fill_sei_payload(
336     enc_ctxt_t *ps_enc_ctxt,
337     ihevce_lap_enc_buf_t *ps_curr_inp,
338     frm_proc_ent_cod_ctxt_t *ps_curr_out)
339 {
340     UWORD32 *pu4_length, i4_cmd_len;
341     UWORD32 *pu4_tag, i4_pic_type;
342     UWORD8 *pu1_user_data;
343 
344     pu4_tag = ((UWORD32 *)(ps_curr_inp->s_input_buf.pv_synch_ctrl_bufs));
345     ps_curr_out->u4_num_sei_payload = 0;
346     i4_pic_type = ps_curr_inp->s_lap_out.i4_pic_type;
347     (void)ps_enc_ctxt;
348     while(1)
349     {
350         if(((*pu4_tag) & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_END_TAG)
351             break;
352 
353         pu4_length = pu4_tag + 1;
354         pu1_user_data = (UWORD8 *)(pu4_length + 1);
355         i4_cmd_len = *pu4_length;
356 
357         if((*pu4_tag & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_REG_KEYFRAME_SEI_TAG)
358         {
359             if(i4_pic_type == IV_IDR_FRAME)
360             {
361                 memcpy(
362                     (void *)((ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload]
363                                   .pu1_sei_payload)),
364                     (void *)pu1_user_data,
365                     i4_cmd_len);
366                 ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_length =
367                     (i4_cmd_len);
368                 ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_type =
369                     ((*pu4_tag & IHEVCE_PAYLOAD_TYPE_MASK) >> IHEVCE_PAYLOAD_TYPE_SHIFT);
370                 ps_curr_out->u4_num_sei_payload++;
371             }
372         }
373         else if((*pu4_tag & IHEVCE_COMMANDS_TAG_MASK) == IHEVCE_SYNCH_API_REG_ALLFRAME_SEI_TAG)
374         {
375             memcpy(
376                 (void *)((
377                     ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].pu1_sei_payload)),
378                 (void *)pu1_user_data,
379                 i4_cmd_len);
380             ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_length =
381                 (i4_cmd_len);
382             ps_curr_out->as_sei_payload[ps_curr_out->u4_num_sei_payload].u4_payload_type =
383                 ((*pu4_tag & IHEVCE_PAYLOAD_TYPE_MASK) >> IHEVCE_PAYLOAD_TYPE_SHIFT);
384             ps_curr_out->u4_num_sei_payload++;
385         }
386 
387         //The formula (((x-1)>>2)+1) gives us the ceiling of (x mod 4). Hence this will take the pointer to the next address boundary divisible by 4.
388         //And then we add 2 bytes for the tag and the payload length.
389         if(i4_cmd_len)
390             pu4_tag += (((i4_cmd_len - 1) >> 2) + 1 + 2);
391         else
392             pu4_tag += 2;
393     }
394 }
395 
396 /*!
397 ******************************************************************************
398 * \if Function name : ihevce_dyn_bitrate \endif
399 *
400 * \brief
401 *    Call back function to be called for changing the bitrate
402 *
403 *
404 * \return
405 *    None
406 *
407 * \author
408 *  Ittiam
409 *
410 *****************************************************************************
411 */
ihevce_dyn_bitrate(void * pv_hle_ctxt,void * pv_dyn_bitrate_prms)412 void ihevce_dyn_bitrate(void *pv_hle_ctxt, void *pv_dyn_bitrate_prms)
413 {
414     ihevce_hle_ctxt_t *ps_hle_ctxt = (ihevce_hle_ctxt_t *)pv_hle_ctxt;
415     ihevce_dyn_config_prms_t *ps_dyn_bitrate_prms = (ihevce_dyn_config_prms_t *)pv_dyn_bitrate_prms;
416     enc_ctxt_t *ps_enc_ctxt =
417         (enc_ctxt_t *)ps_hle_ctxt->apv_enc_hdl[ps_dyn_bitrate_prms->i4_tgt_res_id];
418     ihevce_static_cfg_params_t *ps_static_cfg_params = ps_hle_ctxt->ps_static_cfg_prms;
419 
420     if(ps_enc_ctxt->ps_stat_prms->i4_log_dump_level > 0)
421     {
422         ps_static_cfg_params->s_sys_api.ihevce_printf(
423             ps_static_cfg_params->s_sys_api.pv_cb_handle,
424             "\n Average Bitrate changed to %d",
425             ps_dyn_bitrate_prms->i4_new_tgt_bitrate);
426         ps_static_cfg_params->s_sys_api.ihevce_printf(
427             ps_static_cfg_params->s_sys_api.pv_cb_handle,
428             "\n Peak    Bitrate changed to %d",
429             ps_dyn_bitrate_prms->i4_new_peak_bitrate);
430     }
431 
432 
433     /* acquire mutex lock for rate control calls */
434     osal_mutex_lock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
435 
436     ihevce_rc_register_dyn_change_bitrate(
437         ps_enc_ctxt->s_module_ctxt.apv_rc_ctxt[ps_dyn_bitrate_prms->i4_tgt_br_id],
438         (LWORD64)ps_dyn_bitrate_prms->i4_new_tgt_bitrate,
439         (LWORD64)ps_dyn_bitrate_prms->i4_new_peak_bitrate);
440 
441     /*unlock rate control context*/
442     osal_mutex_unlock(ps_enc_ctxt->pv_rc_mutex_lock_hdl);
443     return;
444 }
445 
446 /*!
447 ******************************************************************************
448 * \if Function name : ihevce_validate_encoder_parameters \endif
449 *
450 * \brief
451 *    Call back function to be called for changing the bitrate
452 *
453 * \return
454 *    None
455 *
456 * \author
457 *  Ittiam
458 *****************************************************************************
459 */
ihevce_validate_encoder_parameters(ihevce_static_cfg_params_t * ps_static_cfg_prms)460 WORD32 ihevce_validate_encoder_parameters(ihevce_static_cfg_params_t *ps_static_cfg_prms)
461 {
462     return (ihevce_hle_validate_static_params(ps_static_cfg_prms));
463 }
464 
465 /*!
466 ******************************************************************************
467 * \if Function name : ihevce_get_encoder_version \endif
468 *
469 * \brief
470 *    Call back function to be called for changing the bitrate
471 *
472 * \return
473 *    None
474 *
475 * \author
476 *  Ittiam
477 *****************************************************************************
478 */
ihevce_get_encoder_version()479 const char *ihevce_get_encoder_version()
480 {
481     return ((const char *)gau1_version_string);
482 }
483