• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 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
24 *  ih264e_utils.c
25 *
26 * @brief
27 *  Contains miscellaneous utility functions used by the encoder
28 *
29 * @author
30 *  ittiam
31 *
32 * @par List of Functions:
33 *  - ih264e_get_min_level()
34 *  - ih264e_get_lvl_idx()
35 *  - ih264e_get_dpb_size()
36 *  - ih264e_get_total_pic_buf_size()
37 *  - ih264e_get_pic_mv_bank_size()
38 *  - ih264e_pic_buf_mgr_add_bufs()
39 *  - ih264e_mv_buf_mgr_add_bufs()
40 *  - ih264e_init_quant_params()
41 *  - ih264e_init_air_map()
42 *  - ih264e_codec_init()
43 *  - ih264e_pic_init()
44 *
45 * @remarks
46 *  None
47 *
48 *******************************************************************************
49 */
50 
51 /*****************************************************************************/
52 /* File Includes                                                             */
53 /*****************************************************************************/
54 
55 /* system include files */
56 #include <stdio.h>
57 #include <stddef.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <assert.h>
61 
62 /* user include files */
63 #include "ih264_typedefs.h"
64 #include "iv2.h"
65 #include "ive2.h"
66 #include "ih264e.h"
67 #include "ithread.h"
68 #include "ih264_defs.h"
69 #include "ih264_size_defs.h"
70 #include "ime_distortion_metrics.h"
71 #include "ime_defs.h"
72 #include "ime_structs.h"
73 #include "psnr.h"
74 #include "ih264_error.h"
75 #include "ih264_structs.h"
76 #include "ih264_trans_quant_itrans_iquant.h"
77 #include "ih264_inter_pred_filters.h"
78 #include "ih264_mem_fns.h"
79 #include "ih264_padding.h"
80 #include "ih264_intra_pred_filters.h"
81 #include "ih264_deblk_edge_filters.h"
82 #include "ih264_cabac_tables.h"
83 #include "ih264_macros.h"
84 #include "ih264_common_tables.h"
85 #include "ih264_debug.h"
86 #include "ih264_trans_data.h"
87 #include "ih264e_defs.h"
88 #include "ih264e_globals.h"
89 #include "ih264_buf_mgr.h"
90 #include "ih264_dpb_mgr.h"
91 #include "ih264e_error.h"
92 #include "ih264e_bitstream.h"
93 #include "irc_cntrl_param.h"
94 #include "irc_frame_info_collector.h"
95 #include "ih264e_rate_control.h"
96 #include "ih264e_cabac_structs.h"
97 #include "ih264e_structs.h"
98 #include "ih264e_cabac.h"
99 #include "ih264e_utils.h"
100 #include "ih264e_config.h"
101 #include "ih264e_statistics.h"
102 #include "ih264e_trace.h"
103 #include "ih264_list.h"
104 #include "ih264e_encode_header.h"
105 #include "ih264e_me.h"
106 #include "ime.h"
107 #include "ih264e_core_coding.h"
108 #include "ih264e_rc_mem_interface.h"
109 #include "ih264e_time_stamp.h"
110 #include "ih264e_debug.h"
111 #include "ih264e_process.h"
112 #include "ih264e_master.h"
113 #include "irc_rate_control_api.h"
114 #include "ime_statistics.h"
115 
116 /*****************************************************************************/
117 /* Function Definitions                                                      */
118 /*****************************************************************************/
119 
120 /**
121  *******************************************************************************
122  *
123  * @brief
124  *  Queues the current buffer, gets back a another buffer for encoding with corrent
125  *  picture type
126  *
127  * @par Description:
128  *      This function performs 3 distinct but related functions.
129  *      1) Maintains an input queue [Note the the term queue donot imply a
130  *         first-in first-out logic here] that queues input and dequeues them so
131  *         that input frames can be encoded at any predetermined encoding order
132  *      2) Uses RC library to decide which frame must be encoded in current pass
133  *         and which picture type it must be encoded to.
134  *      3) Uses RC library to decide the QP at which current frame has to be
135  *         encoded
136  *      4) Determines if the current picture must be encoded or not based on
137  *         PRE-ENC skip
138  *
139  *     Input queue is used for storing input buffers till they are used for
140  *     encoding. This queue is maintained at ps_codec->as_inp_list. Whenever a
141  *     valid input comes, it is added to the end of queue. This same input is
142  *     added to RC queue using the identifier as ps_codec->i4_pic_cnt. Hence any
143  *     pic from RC can be located in the input queue easily.
144  *
145  *     The dequeue operation does not start till we have ps_codec->s_cfg.u4_max_num_bframes
146  *     frames in the queue. THis is done in order to ensure that once output starts
147  *     we will have a constant stream of output with no gaps.
148  *
149  *     THe output frame order is governed by RC library. When ever we dequeue a
150  *     buffer from RC library, it ensures that we will get them in encoding order
151  *     With the output of RC library, we can use the picture id to dequeue the
152  *     corresponding buffer from input queue and encode it.
153  *
154  *     Condition at the end of stream.
155  *     -------------------------------
156  *      At the last valid buffer from the app, we will get ps_ive_ip->u4_is_last
157  *      to be set. This will the given to lib when appropriate input buffer is
158  *      given to encoding.
159  *
160  *      Since we have to output is not in sync with input, we will have frames to
161  *      encode even after we recive the last vaild input buffer. Hence we have to
162  *      make sure that we donot queue any new buffers once we get the flag [It may
163  *      mess up GOP ?]. This is acheived by setting ps_codec->i4_last_inp_buff_received
164  *      to act as a permenent marker for last frame recived [This may not be needed,
165  *      because in our current app, all buffers after the last are marked as last.
166  *      But can we rely on that?] . Hence after this flgag is set no new buffers are
167  *      queued.
168  *
169  * @param[in] ps_codec
170  *   Pointer to codec descriptor
171  *
172  * @param[in] ps_ive_ip
173  *   Current input buffer to the encoder
174  *
175  * @param[out] ps_inp
176  *   Buffer to be encoded in the current pass
177  *
178  * @returns
179  *   Flag indicating if we have a pre-enc skip or not
180  *
181  * @remarks
182  * TODO (bpic)
183  *  The check for null ans is last is redudent.
184  *  Need to see if we can remove it
185  *
186  *******************************************************************************
187  */
ih264e_input_queue_update(codec_t * ps_codec,ive_video_encode_ip_t * ps_ive_ip,inp_buf_t * ps_enc_buff)188 WORD32 ih264e_input_queue_update(codec_t *ps_codec,
189                                  ive_video_encode_ip_t *ps_ive_ip,
190                                  inp_buf_t *ps_enc_buff)
191 {
192 
193     inp_buf_t *ps_inp_buf;
194     picture_type_e e_pictype;
195     WORD32 i4_skip;
196     UWORD32 ctxt_sel, u4_pic_id, u4_pic_disp_id;
197     UWORD8 u1_frame_qp, i;
198     UWORD32 max_frame_bits = 0x7FFFFFFF;
199 
200     /*  Mark that the last input frame has been received */
201     if (ps_ive_ip->u4_is_last == 1)
202     {
203         ps_codec->i4_last_inp_buff_received = 1;
204     }
205 
206     if (ps_ive_ip->s_inp_buf.apv_bufs[0] == NULL
207                     && !ps_codec->i4_last_inp_buff_received)
208     {
209         ps_enc_buff->s_raw_buf.apv_bufs[0] = NULL;
210         ps_enc_buff->u4_is_last = ps_ive_ip->u4_is_last;
211         return 0;
212     }
213 
214     /***************************************************************************
215      * Check for pre enc skip
216      *   When src and target frame rates donot match, we skip some frames to
217      *   maintain the relation ship between them
218      **************************************************************************/
219     {
220         WORD32 skip_src;
221 
222         skip_src = ih264e_update_rc_framerates(
223                         ps_codec->s_rate_control.pps_rate_control_api,
224                         ps_codec->s_rate_control.pps_pd_frm_rate,
225                         ps_codec->s_rate_control.pps_time_stamp,
226                         ps_codec->s_rate_control.pps_frame_time);
227 
228         if (skip_src)
229         {
230             ps_enc_buff->u4_is_last = ps_ive_ip->u4_is_last;
231             return 1;
232         }
233     }
234 
235     /***************************************************************************
236      *Queue the input to the queue
237      **************************************************************************/
238     ps_inp_buf = &(ps_codec->as_inp_list[ps_codec->i4_pic_cnt
239                                          % MAX_NUM_INP_FRAMES]);
240 
241     /* copy input info. to internal structure */
242     ps_inp_buf->s_raw_buf = ps_ive_ip->s_inp_buf;
243     ps_inp_buf->u4_timestamp_low = ps_ive_ip->u4_timestamp_low;
244     ps_inp_buf->u4_timestamp_high = ps_ive_ip->u4_timestamp_high;
245     ps_inp_buf->u4_is_last = ps_ive_ip->u4_is_last;
246     ps_inp_buf->pv_mb_info = ps_ive_ip->pv_mb_info;
247     ps_inp_buf->u4_mb_info_type = ps_ive_ip->u4_mb_info_type;
248     ps_inp_buf->pv_pic_info = ps_ive_ip->pv_pic_info;
249     ps_inp_buf->u4_pic_info_type = ps_ive_ip->u4_pic_info_type;
250 
251     ps_inp_buf->u1_sei_ccv_params_present_flag =
252                 ps_codec->s_cfg.s_sei.u1_sei_ccv_params_present_flag;
253     ps_inp_buf->s_sei_ccv = ps_codec->s_cfg.s_sei.s_sei_ccv_params;
254 
255     ps_inp_buf->u1_sei_sii_params_present_flag =
256         ps_codec->s_cfg.s_sei.u1_sei_sii_params_present_flag;
257     ps_inp_buf->s_sei_sii = ps_codec->s_cfg.s_sei.s_sei_sii_params;
258 
259     /***************************************************************************
260      * Now we should add the picture to RC stack here
261      **************************************************************************/
262     /*
263      * If an I frame has been requested, ask  RC to force it
264      * For IDR requests, we have to ask RC to force I and set IDR by our selves
265      * since RC Donot know about IDR. For forcing an IDR at dequeue stage we
266      * should record that an IDR has been requested some where. Hence we will
267      * store it in the u4_idr_inp_list at a position same as that of input frame
268      */
269     {
270         WORD32 i4_force_idr, i4_force_i;
271 
272         i4_force_idr = (ps_codec->force_curr_frame_type == IV_IDR_FRAME);
273         i4_force_idr |= !(ps_codec->i4_pic_cnt % ps_codec->s_cfg.u4_idr_frm_interval);
274 
275         i4_force_i = (ps_codec->force_curr_frame_type == IV_I_FRAME);
276 
277         ps_codec->i4_pending_idr_flag |= i4_force_idr;
278 
279         if ((ps_codec->i4_pic_cnt > 0) && (i4_force_idr || i4_force_i))
280         {
281             irc_force_I_frame(ps_codec->s_rate_control.pps_rate_control_api);
282         }
283         ps_codec->force_curr_frame_type = IV_NA_FRAME;
284     }
285 
286     irc_add_picture_to_stack(ps_codec->s_rate_control.pps_rate_control_api,
287                              ps_codec->i4_pic_cnt);
288 
289 
290     /* Delay */
291     if (ps_codec->i4_encode_api_call_cnt
292                     < (WORD32)(ps_codec->s_cfg.u4_num_bframes))
293     {
294         ps_enc_buff->s_raw_buf.apv_bufs[0] = NULL;
295         ps_enc_buff->u4_is_last = 0;
296         return 0;
297     }
298 
299     /***************************************************************************
300      * Get a new pic to encode
301      **************************************************************************/
302     /* Query the picture_type */
303     e_pictype = ih264e_rc_get_picture_details(
304                     ps_codec->s_rate_control.pps_rate_control_api, (WORD32 *)(&u4_pic_id),
305                     (WORD32 *)(&u4_pic_disp_id));
306 
307     switch (e_pictype)
308     {
309         case I_PIC:
310             ps_codec->pic_type = PIC_I;
311             break;
312         case P_PIC:
313             ps_codec->pic_type = PIC_P;
314             break;
315         case B_PIC:
316             ps_codec->pic_type = PIC_B;
317             break;
318         default:
319             ps_codec->pic_type = PIC_NA;
320             ps_enc_buff->s_raw_buf.apv_bufs[0] = NULL;
321             return 0;
322     }
323 
324     /* Set IDR if it has been requested */
325     if (ps_codec->pic_type == PIC_I)
326     {
327         ps_codec->pic_type = ps_codec->i4_pending_idr_flag ?
328                                     PIC_IDR : ps_codec->pic_type;
329         ps_codec->i4_pending_idr_flag = 0;
330     }
331 
332     /* Get current frame Qp */
333     u1_frame_qp = (UWORD8)irc_get_frame_level_qp(
334                     ps_codec->s_rate_control.pps_rate_control_api, e_pictype,
335                     max_frame_bits);
336     ps_codec->u4_frame_qp = gau1_mpeg2_to_h264_qmap[u1_frame_qp];
337 
338     /*
339      * copy the pic id to poc because the display order is assumed to be same
340      * as input order
341      */
342     ps_codec->i4_poc = u4_pic_id;
343 
344     /***************************************************************************
345      * Now retrieve the correct picture from the queue
346      **************************************************************************/
347 
348     /* Mark the skip flag   */
349     i4_skip = 0;
350     ctxt_sel = ps_codec->i4_encode_api_call_cnt % MAX_CTXT_SETS;
351     ps_codec->s_rate_control.pre_encode_skip[ctxt_sel] = i4_skip;
352 
353     /* Get a buffer to encode */
354     ps_inp_buf = &(ps_codec->as_inp_list[u4_pic_id % MAX_NUM_INP_FRAMES]);
355 
356     /* copy dequeued input to output */
357     ps_enc_buff->s_raw_buf = ps_inp_buf->s_raw_buf;
358     ps_enc_buff->u4_timestamp_low = ps_inp_buf->u4_timestamp_low;
359     ps_enc_buff->u4_timestamp_high = ps_inp_buf->u4_timestamp_high;
360     ps_enc_buff->u4_is_last = ps_inp_buf->u4_is_last;
361     ps_enc_buff->pv_mb_info = ps_inp_buf->pv_mb_info;
362     ps_enc_buff->u4_mb_info_type = ps_inp_buf->u4_mb_info_type;
363     ps_enc_buff->pv_pic_info = ps_inp_buf->pv_pic_info;
364     ps_enc_buff->u4_pic_info_type = ps_inp_buf->u4_pic_info_type;
365 
366     ps_enc_buff->u1_sei_ccv_params_present_flag = ps_inp_buf->u1_sei_ccv_params_present_flag;
367     ps_enc_buff->s_sei_ccv = ps_inp_buf->s_sei_ccv;
368     ps_enc_buff->u1_sei_sii_params_present_flag = ps_inp_buf->u1_sei_sii_params_present_flag;
369     ps_enc_buff->s_sei_sii = ps_inp_buf->s_sei_sii;
370 
371     /* Special case for encoding trailing B frames
372      *
373      * In encoding streams with B frames it may happen that we have a B frame
374      * at the end without a P/I frame after it. Hence when we are dequeing from
375      * the RC, it will return the P frame [next in display order but before in
376      * encoding order] first. Since the dequeue happens for an invalid frame we
377      * will get a frame with null buff and set u4_is_last. Hence lib with return
378      * last frame flag at this point and will stop encoding.
379      *
380      * Since for the last B frame, we does not have the forward ref frame
381      * it makes sense to force it into P.
382      *
383      * To solve this, in case the current frame is P and if the last frame flag
384      * is set, we need to see if there is and pending B frames. If there are any,
385      * we should just encode that picture as the current P frame and set
386      * that B frame as the last frame. Hence the encoder will terminate naturally
387      * once that B-frame is encoded after all the in between frames.
388      *
389      * Since we cannot touch RC stack directly, the option of actually swapping
390      * frames in RC is ruled out. We have to modify the as_inp_list to simulate
391      * such a behavior by RC. We can do that by
392      *  1) Search through as_inp_list to locate the largest u4_timestamp_low less
393      *     than current u4_timestamp_low. This will give us the last B frame before
394      *     the current P frame. Note that this will handle pre encode skip too since
395      *     queue happens after pre enc skip.
396      *  2) Swap the position in as_inp_list. Hence now the last B frame is
397      *     encoded as P frame. And the new last B frame will have u4_is_last
398      *     set so that encoder will end naturally once we reached that B frame
399      *     or any subsequent frame. Also the current GOP will have 1 less B frame
400      *     Since we are swapping, the poc will also be in-order.
401      *  3) In case we have an IPP stream, the result of our search will be an
402      *     I/P frame which is already encoded. Thus swap and encode will result
403      *     in encoding of duplicate frames. Hence to avoid this we will only
404      *     have this work around in case of u4_num_bframes > 0.
405      *
406      *     In case we have forced an I/IDR frame In between this P frame and
407      *     the last B frame -> This cannot happen as the current P frame is
408      *     supposed to have u4_is_last set. Thus forcing an I/ IDR after this
409      *     is illogical.
410      *
411      *     In cae if we have forced an I such that the frame just before last frame
412      *     in is I/P -> This case will never arise. Since we have a closed GOP now,
413      *     once we force an I, the gop gets reset, hence there will be a B between
414      *     I/P and I/P.
415      */
416     if (ps_enc_buff->u4_is_last && (ps_codec->pic_type == PIC_P)
417                     && ps_codec->s_cfg.u4_num_bframes)
418     {
419         WORD32 cntr;
420         WORD32 lst_bframe = -1;
421         UWORD32 u4_timestamp_low = 0;
422         UWORD32 u4_timestamp_high = 0;
423         inp_buf_t *ps_swap_buff, *ps_inp_list;
424 
425         ps_inp_list = &ps_codec->as_inp_list[0];
426 
427         /* Now search the inp list for highest timestamp */
428         for(cntr = 0; cntr < MAX_NUM_INP_FRAMES; cntr++)
429         {
430             if(ps_inp_list[cntr].s_raw_buf.apv_bufs[0] != NULL)
431             {
432                 if ((ps_inp_list[cntr].u4_timestamp_high  > u4_timestamp_high) ||
433                     (ps_inp_list[cntr].u4_timestamp_high  == u4_timestamp_high &&
434                      ps_inp_list[cntr].u4_timestamp_low  > u4_timestamp_low))
435                 {
436                     u4_timestamp_low = ps_inp_list[cntr].u4_timestamp_low;
437                     u4_timestamp_high = ps_inp_list[cntr].u4_timestamp_high;
438                     lst_bframe = cntr;
439                 }
440             }
441         }
442 
443         if(lst_bframe != -1)
444         {
445             ps_swap_buff = &(ps_codec->as_inp_list[lst_bframe]);
446 
447             /* copy the last B buffer to output */
448             *ps_enc_buff = *ps_swap_buff;
449 
450             /* Store the current buf into the queue in place of last B buf */
451             *ps_swap_buff = *ps_inp_buf;
452         }
453     }
454 
455     /* The buffer in the queue is set to NULL to specify that encoding is done for that frame */
456     for(i = 0; i < 3; i++)
457     {
458         ps_inp_buf->s_raw_buf.apv_bufs[i] = NULL;
459     }
460 
461     /* Return the buffer status */
462     return (0);
463 }
464 
465 /**
466 *******************************************************************************
467 *
468 * @brief
469 *  Used to get minimum level index for a given picture size
470 *
471 * @par Description:
472 *  Gets the minimum level index and then gets corresponding level.
473 *  Also used to ignore invalid levels like 2.3, 3.3 etc
474 *
475 * @param[in] level
476 *  Level of the stream
477 *
478 * @returns  Level index for a given level
479 *
480 * @remarks
481 *
482 *******************************************************************************
483 */
ih264e_get_min_level(WORD32 wd,WORD32 ht)484 WORD32 ih264e_get_min_level(WORD32 wd, WORD32 ht)
485 {
486     WORD32 lvl_idx = MAX_LEVEL, i;
487     WORD32 pic_size = wd * ht;
488     WORD32 max = MAX(wd, ht);
489     for (i = 0; i < MAX_LEVEL; i++)
490     {
491         if ((pic_size <= gai4_ih264_max_luma_pic_size[i]) &&
492             (max <= gai4_ih264_max_wd_ht[i]))
493         {
494             lvl_idx = i;
495             break;
496         }
497     }
498 
499     return gai4_ih264_levels[lvl_idx];
500 }
501 
502 /**
503 *******************************************************************************
504 *
505 * @brief
506 *  Used to get level index for a given level
507 *
508 * @par Description:
509 *  Converts from level_idc (which is multiplied by 30) to an index that can be
510 *  used as a lookup. Also used to ignore invalid levels like 2.2 , 3.2 etc
511 *
512 * @param[in] level
513 *  Level of the stream
514 *
515 * @returns  Level index for a given level
516 *
517 * @remarks
518 *
519 *******************************************************************************
520 */
ih264e_get_lvl_idx(WORD32 level)521 WORD32 ih264e_get_lvl_idx(WORD32 level)
522 {
523     WORD32 lvl_idx = 0;
524 
525     if (level < IH264_LEVEL_11)
526     {
527         lvl_idx = 0;
528     }
529     else if (level < IH264_LEVEL_12)
530     {
531         lvl_idx = 1;
532     }
533     else if (level < IH264_LEVEL_13)
534     {
535         lvl_idx = 2;
536     }
537     else if (level < IH264_LEVEL_20)
538     {
539         lvl_idx = 3;
540     }
541     else if (level < IH264_LEVEL_21)
542     {
543         lvl_idx = 4;
544     }
545     else if (level < IH264_LEVEL_22)
546     {
547         lvl_idx = 5;
548     }
549     else if (level < IH264_LEVEL_30)
550     {
551         lvl_idx = 6;
552     }
553     else if (level < IH264_LEVEL_31)
554     {
555         lvl_idx = 7;
556     }
557     else if (level < IH264_LEVEL_32)
558     {
559         lvl_idx = 8;
560     }
561     else if (level < IH264_LEVEL_40)
562     {
563         lvl_idx = 9;
564     }
565     else if (level < IH264_LEVEL_41)
566     {
567         lvl_idx = 10;
568     }
569     else if (level < IH264_LEVEL_42)
570     {
571         lvl_idx = 11;
572     }
573     else if (level < IH264_LEVEL_50)
574     {
575         lvl_idx = 12;
576     }
577     else if (level < IH264_LEVEL_51)
578     {
579         lvl_idx = 13;
580     }
581     else
582     {
583         lvl_idx = 14;
584     }
585 
586     return (lvl_idx);
587 }
588 
589 /**
590 *******************************************************************************
591 *
592 * @brief returns maximum number of pictures allowed in dpb for a given level
593 *
594 * @par Description:
595 *  For given width, height and level, number of pictures allowed in decoder
596 *  picture buffer is computed as per Annex A.3.1
597 *
598 * @param[in] level
599 *  level of the bit-stream
600 *
601 * @param[in] pic_size
602 *  width * height
603 *
604 * @returns  Number of buffers in DPB
605 *
606 * @remarks
607 *  From annexure A.3.1 of H264 specification,
608 *  max_dec_frame_buffering <= MaxDpbSize, where MaxDpbSize is equal to
609 *  Min( 1024 * MaxDPB / ( PicWidthInMbs * FrameHeightInMbs * 384 ), 16 ) and
610 *  MaxDPB is given in Table A-1 in units of 1024 bytes. However the MaxDPB size
611 *  presented in the look up table gas_ih264_lvl_tbl is in units of 512
612 *  bytes. Hence the expression is modified accordingly.
613 *
614 *******************************************************************************
615 */
ih264e_get_dpb_size(WORD32 level,WORD32 pic_size)616 WORD32 ih264e_get_dpb_size(WORD32 level, WORD32 pic_size)
617 {
618     /* dpb size */
619     WORD32 max_dpb_size_bytes = 0;
620 
621     /* dec frame buffering */
622     WORD32 max_dpb_size_frames = 0;
623 
624     /* temp var */
625     WORD32 i;
626 
627     /* determine max luma samples */
628     for (i = 0; i < 16; i++)
629         if (level == (WORD32)gas_ih264_lvl_tbl[i].u4_level_idc)
630             max_dpb_size_bytes = gas_ih264_lvl_tbl[i].u4_max_dpb_size;
631 
632     /* from Annexure A.3.1 h264 specification */
633     max_dpb_size_frames =
634                     MIN( 1024 * max_dpb_size_bytes / ( pic_size * 3 ), MAX_DPB_SIZE );
635 
636     return max_dpb_size_frames;
637 }
638 
639 /**
640 *******************************************************************************
641 *
642 * @brief
643 *  Used to get reference picture buffer size for a given level and
644 *  and padding used
645 *
646 * @par Description:
647 *  Used to get reference picture buffer size for a given level and padding used
648 *  Each picture is padded on all four sides
649 *
650 * @param[in] pic_size
651 *  Number of luma samples (Width * Height)
652 *
653 * @param[in] level
654 *  Level
655 *
656 * @param[in] horz_pad
657 *  Total padding used in horizontal direction
658 *
659 * @param[in] vert_pad
660 *  Total padding used in vertical direction
661 *
662 * @returns  Total picture buffer size
663 *
664 * @remarks
665 *
666 *
667 *******************************************************************************
668 */
ih264e_get_total_pic_buf_size(WORD32 pic_size,WORD32 level,WORD32 horz_pad,WORD32 vert_pad,WORD32 num_ref_frames,WORD32 num_reorder_frames)669 WORD32 ih264e_get_total_pic_buf_size(WORD32 pic_size,
670                                      WORD32 level,
671                                      WORD32 horz_pad,
672                                      WORD32 vert_pad,
673                                      WORD32 num_ref_frames,
674                                      WORD32 num_reorder_frames)
675 {
676     WORD32 size;
677     WORD32 num_luma_samples;
678     WORD32 lvl_idx;
679     WORD32 max_wd, min_ht;
680     WORD32 num_samples;
681     WORD32 max_num_bufs;
682     WORD32 pad = MAX(horz_pad, vert_pad);
683 
684     /*
685      * If num_ref_frames and num_reorder_frmaes is specified
686      * Use minimum value
687      */
688     max_num_bufs = (num_ref_frames + num_reorder_frames + MAX_CTXT_SETS);
689 
690     /* Get level index */
691     lvl_idx = ih264e_get_lvl_idx(level);
692 
693     /* Maximum number of luma samples in a picture at given level */
694     num_luma_samples = gai4_ih264_max_luma_pic_size[lvl_idx];
695     num_luma_samples = MAX(num_luma_samples, pic_size);
696 
697     /* Account for chroma */
698     num_samples = num_luma_samples * 3 / 2;
699 
700     /* Maximum width of luma samples in a picture at given level */
701     max_wd = gai4_ih264_max_wd_ht[lvl_idx];
702 
703     /* Minimum height of luma samples in a picture at given level */
704     min_ht = gai4_ih264_min_wd_ht[lvl_idx];
705 
706     /* Allocation is required for
707      * (Wd + horz_pad) * (Ht + vert_pad) * (2 * max_dpb_size + 1)
708      *
709      * Above expanded as
710      * ((Wd * Ht) + (horz_pad * vert_pad) + Wd * vert_pad + Ht * horz_pad) * (2 * max_dpb_size + 1)
711      * (Wd * Ht) * (2 * max_dpb_size + 1) + ((horz_pad * vert_pad) + Wd * vert_pad + Ht * horz_pad) * (2 * max_dpb_size + 1)
712      * Now  max_dpb_size increases with smaller Wd and Ht, but Wd * ht * max_dpb_size will still be lesser or equal to max_wd * max_ht * dpb_size
713      *
714      * In the above equation (Wd * Ht) * (2 * max_dpb_size + 1) is accounted by using num_samples * (2 * max_dpb_size + 1) below
715      *
716      * For the padded area use MAX(horz_pad, vert_pad) as pad
717      * ((pad * pad) + pad * (Wd + Ht)) * (2 * max_dpb_size + 1) has to accounted from the above for padding
718      *
719      * Since Width and Height can change worst Wd + Ht is when One of the dimensions is max and other is min
720      * So use max_wd and min_ht
721      */
722 
723     /* Number of bytes in reference pictures */
724     size = num_samples * max_num_bufs;
725 
726     /* Account for padding area */
727     size += ((pad * pad) + pad * (max_wd + min_ht)) * 3 / 2 * max_num_bufs;
728 
729     return size;
730 }
731 
732 /**
733 *******************************************************************************
734 *
735 * @brief Returns MV bank buffer size for a given number of luma samples
736 *
737 * @par Description:
738 *  For given number of luma samples  one MV bank size is computed.
739 *  Each MV bank includes pu_map and enc_pu_t for all the min PUs(4x4) in a picture
740 *
741 * @param[in] num_luma_samples
742 *  Max number of luma pixels in the frame
743 *
744 * @returns  Total MV Bank size
745 *
746 * @remarks
747 *
748 *******************************************************************************
749 */
ih264e_get_pic_mv_bank_size(WORD32 num_luma_samples)750 WORD32 ih264e_get_pic_mv_bank_size(WORD32 num_luma_samples)
751 {
752     /* mv bank buffer size */
753     WORD32 mv_bank_size = 0;
754 
755     /* number of sub mb partitions possible */
756     WORD32 num_pu = num_luma_samples / (ENC_MIN_PU_SIZE * ENC_MIN_PU_SIZE);
757 
758     /* number of mbs */
759     WORD32 num_mb = num_luma_samples / (MB_SIZE * MB_SIZE);
760 
761     /* Size for storing enc_pu_t start index each MB */
762     /* One extra entry is needed to compute number of PUs in the last MB */
763     mv_bank_size += num_mb * sizeof(WORD32);
764 
765     /* Size for pu_map */
766     mv_bank_size += ALIGN4(num_pu);
767 
768     /* Size for storing enc_pu_t for each PU */
769     mv_bank_size += ALIGN4(num_pu * sizeof(enc_pu_t));
770 
771     return mv_bank_size;
772 }
773 
774 /**
775 *******************************************************************************
776 *
777 * @brief
778 *  Function to initialize ps_pic_buf structs add pic buffers to
779 *  buffer manager in case of non-shared mode
780 *
781 * @par Description:
782 *  Function to initialize ps_pic_buf structs add pic buffers to
783 *  buffer manager in case of non-shared mode
784 *  To be called once per stream or for every reset
785 *
786 * @param[in] ps_codec
787 *  Pointer to codec context
788 *
789 * @returns  error status
790 *
791 * @remarks
792 *
793 *******************************************************************************
794 */
ih264e_pic_buf_mgr_add_bufs(codec_t * ps_codec)795 IH264E_ERROR_T ih264e_pic_buf_mgr_add_bufs(codec_t *ps_codec)
796 {
797     /* error status */
798     IH264E_ERROR_T ret = IH264E_SUCCESS;
799 
800     /* max ref buffer cnt */
801     WORD32 max_num_bufs = ps_codec->i4_ref_buf_cnt;
802 
803     /* total size for pic buffers */
804     WORD32 pic_buf_size_allocated = ps_codec->i4_total_pic_buf_size
805                     - BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
806 
807     /* temp var */
808     UWORD8 *pu1_buf = (UWORD8 *) ps_codec->ps_pic_buf;
809     pic_buf_t *ps_pic_buf = (pic_buf_t *) ps_codec->ps_pic_buf;
810     WORD32 i;
811 
812     pu1_buf += BUF_MGR_MAX_CNT * sizeof(pic_buf_t);
813 
814     /* In case of non-shared mode, add picture buffers to buffer manager
815      * In case of shared mode, buffers are added in the run-time
816      */
817     {
818         WORD32 buf_ret;
819 
820         WORD32 luma_samples = (ps_codec->i4_rec_strd)
821                         * (ps_codec->s_cfg.u4_ht + PAD_HT);
822 
823         WORD32 chroma_samples = luma_samples >> 1;
824 
825         /* Try and add as many buffers as possible for the memory that is allocated */
826         /* If the number of buffers that can be added is less than max_num_bufs
827          * return with an error */
828         for (i = 0; i < max_num_bufs; i++)
829         {
830             pic_buf_size_allocated -= (luma_samples + chroma_samples);
831 
832             if (pic_buf_size_allocated < 0)
833             {
834                 ps_codec->i4_error_code = IH264E_INSUFFICIENT_MEM_PICBUF;
835                 return IH264E_INSUFFICIENT_MEM_PICBUF;
836             }
837 
838             ps_pic_buf->pu1_luma = pu1_buf + ps_codec->i4_rec_strd * PAD_TOP
839                             + PAD_LEFT;
840             pu1_buf += luma_samples;
841 
842             ps_pic_buf->pu1_chroma = pu1_buf
843                             + ps_codec->i4_rec_strd * (PAD_TOP / 2)+ PAD_LEFT;
844             pu1_buf += chroma_samples;
845 
846             buf_ret = ih264_buf_mgr_add((buf_mgr_t *) ps_codec->pv_ref_buf_mgr,
847                                         ps_pic_buf, i);
848 
849             if (0 != buf_ret)
850             {
851                 ps_codec->i4_error_code = IH264E_BUF_MGR_ERROR;
852                 return IH264E_BUF_MGR_ERROR;
853             }
854             pu1_buf += (HPEL_PLANES_CNT - 1) * (chroma_samples + luma_samples);
855             ps_pic_buf++;
856         }
857     }
858 
859     return ret;
860 }
861 
862 /**
863 *******************************************************************************
864 *
865 * @brief Function to add buffers to MV Bank buffer manager
866 *
867 * @par Description:
868 *  Function to add buffers to MV Bank buffer manager.  To be called once per
869 *  stream or for every reset
870 *
871 * @param[in] ps_codec
872 *  Pointer to codec context
873 *
874 * @returns  error status
875 *
876 * @remarks
877 *
878 *******************************************************************************
879 */
ih264e_mv_buf_mgr_add_bufs(codec_t * ps_codec)880 IH264E_ERROR_T ih264e_mv_buf_mgr_add_bufs(codec_t *ps_codec)
881 {
882     /* error status */
883     IH264E_ERROR_T error_status = IH264E_SUCCESS;
884     IH264_ERROR_T ret;
885 
886     /* max dpb size in frames */
887     WORD32 max_dpb_size = 0;
888 
889     /* mv bank size for the entire dpb */
890     WORD32 mv_bank_size_allocated = 0;
891 
892     /* mv bank size per pic */
893     WORD32 pic_mv_bank_size = 0;
894 
895     /* mv buffer ptr */
896     mv_buf_t *ps_mv_buf = NULL;
897 
898     /* num of luma samples */
899     WORD32 num_luma_samples = ALIGN16(ps_codec->s_cfg.u4_wd)
900                             * ALIGN16(ps_codec->s_cfg.u4_ht);
901 
902     /* number of mb's & frame partitions */
903     WORD32 num_pu, num_mb;
904 
905     /* temp var */
906     UWORD8 *pu1_buf = NULL;
907     WORD32 i;
908 
909     /* Compute the number of MB Bank buffers needed */
910     max_dpb_size = ps_codec->i4_ref_buf_cnt;
911 
912     /* allocate memory for mv buffer array */
913     ps_codec->ps_mv_buf = ps_codec->pv_mv_bank_buf_base;
914     pu1_buf = ps_codec->pv_mv_bank_buf_base;
915     pu1_buf += BUF_MGR_MAX_CNT * sizeof(mv_buf_t);
916 
917     /********************************************************************/
918     /* allocate memory for individual elements of mv buffer ptr         */
919     /********************************************************************/
920     mv_bank_size_allocated = ps_codec->i4_total_mv_bank_size
921                     - (BUF_MGR_MAX_CNT * sizeof(mv_buf_t));
922 
923     /* compute MV bank size per picture */
924     pic_mv_bank_size = ih264e_get_pic_mv_bank_size(num_luma_samples);
925 
926     num_pu = num_luma_samples / (ENC_MIN_PU_SIZE * ENC_MIN_PU_SIZE);
927     num_mb = num_luma_samples / (MB_SIZE * MB_SIZE);
928     i = 0;
929     ps_mv_buf = ps_codec->pv_mv_bank_buf_base;
930 
931     while (i < max_dpb_size)
932     {
933         mv_bank_size_allocated -= pic_mv_bank_size;
934 
935         if (mv_bank_size_allocated < 0)
936         {
937             ps_codec->i4_error_code = IH264E_INSUFFICIENT_MEM_MVBANK;
938 
939             error_status = IH264E_INSUFFICIENT_MEM_MVBANK;
940 
941             return error_status;
942         }
943 
944         ps_mv_buf->pu4_mb_pu_cnt = (UWORD32 *) pu1_buf;
945         pu1_buf += num_mb * sizeof(WORD32);
946 
947         ps_mv_buf->pu1_pic_pu_map = pu1_buf;
948         pu1_buf += ALIGN4(num_pu);
949 
950         ps_mv_buf->ps_pic_pu = (enc_pu_t *) (pu1_buf);
951         pu1_buf += ALIGN4(num_pu * sizeof(enc_pu_t));
952 
953         ret = ih264_buf_mgr_add((buf_mgr_t *) ps_codec->pv_mv_buf_mgr,
954                                 ps_mv_buf, i);
955 
956         if (IH264_SUCCESS != ret)
957         {
958             ps_codec->i4_error_code = IH264E_BUF_MGR_ERROR;
959             error_status = IH264E_BUF_MGR_ERROR;
960             return error_status;
961         }
962 
963         ps_mv_buf++;
964         i++;
965     }
966 
967     return error_status;
968 }
969 
970 /**
971 *******************************************************************************
972 *
973 * @brief Function to initialize quant params structure
974 *
975 * @par Description:
976 *  The forward quantization modules depends on qp/6, qp mod 6, forward scale
977 *  matrix, forward threshold matrix, weight list. The inverse quantization
978 *  modules depends on qp/6, qp mod 6, inverse scale matrix, weight list.
979 *  These params are initialized in this function.
980 *
981 * @param[in] ps_proc
982 *  pointer to process context
983 *
984 * @param[in] qp
985 *  quantization parameter
986 *
987 * @returns none
988 *
989 * @remarks
990 *
991 *******************************************************************************
992 */
ih264e_init_quant_params(process_ctxt_t * ps_proc,int qp)993 void ih264e_init_quant_params(process_ctxt_t *ps_proc, int qp)
994 {
995     /* quant params */
996     quant_params_t *ps_qp_params;
997 
998     /* ptr to forward quant threshold matrix */
999     const UWORD16 *pu2_thres_mat = NULL;
1000 
1001     /* ptr to forward scale matrix */
1002     const UWORD16 *pu2_scale_mat = gu2_quant_scale_matrix_4x4;
1003 
1004     /* ptr to inverse scale matrix */
1005     const UWORD16 *pu2_iscale_mat = gau2_ih264_iquant_scale_matrix_4x4;
1006 
1007     /* temp var */
1008     UWORD32 u4_qp[3], u4_qp_div6, u4_qp_mod6;
1009     COMPONENT_TYPE plane;
1010     WORD32 i;
1011     UWORD32 u4_satdq_t;
1012     const UWORD16 *pu2_smat;
1013 
1014     /********************************************************************/
1015     /* init quant params for all planes Y, U and V                      */
1016     /********************************************************************/
1017     /* luma qp */
1018     u4_qp[Y] = qp;
1019 
1020     /* chroma qp
1021      * TODO_LATER : just in case if the chroma planes use different qp's this
1022      * needs to be corrected accordingly.
1023      */
1024     u4_qp[U] = gu1_qpc_fqpi[qp];
1025     u4_qp[V] = gu1_qpc_fqpi[qp];
1026 
1027     plane = Y;
1028     while (plane <= V)
1029     {
1030         u4_qp_div6 = (u4_qp[plane] / 6);
1031         u4_qp_mod6 = (u4_qp[plane] % 6);
1032 
1033         ps_qp_params = ps_proc->ps_qp_params[plane];
1034 
1035         /* mb qp */
1036         ps_qp_params->u1_mb_qp = u4_qp[plane];
1037 
1038         /* mb qp / 6 */
1039         ps_qp_params->u1_qp_div = u4_qp_div6;
1040 
1041         /* mb qp % 6 */
1042         ps_qp_params->u1_qp_rem = u4_qp_mod6;
1043 
1044         /* QP bits */
1045         ps_qp_params->u1_qbits = QP_BITS_h264_4x4 + u4_qp_div6;
1046 
1047         /* forward scale matrix */
1048         ps_qp_params->pu2_scale_mat = pu2_scale_mat + (u4_qp_mod6 * 16);
1049 
1050         /* threshold matrix & weight for quantization */
1051         pu2_thres_mat = gu2_forward_quant_threshold_4x4 + (u4_qp_mod6 * 16);
1052         for (i = 0; i < 16; i++)
1053         {
1054             ps_qp_params->pu2_thres_mat[i] = pu2_thres_mat[i]
1055                             >> (8 - u4_qp_div6);
1056             ps_qp_params->pu2_weigh_mat[i] = 16;
1057         }
1058 
1059         /* qp dependent rounding constant */
1060         ps_qp_params->u4_dead_zone =
1061                         gu4_forward_quant_round_factor_4x4[u4_qp_div6];
1062 
1063         /* slice dependent rounding constant */
1064         if (ps_proc->i4_slice_type != ISLICE
1065                         && ps_proc->i4_slice_type != SISLICE)
1066         {
1067             ps_qp_params->u4_dead_zone >>= 1;
1068         }
1069 
1070         /* SATQD threshold for zero block prediction */
1071         if (ps_proc->ps_codec->s_cfg.u4_enable_satqd)
1072         {
1073             pu2_smat = ps_qp_params->pu2_scale_mat;
1074 
1075             u4_satdq_t = ((1 << (ps_qp_params->u1_qbits)) - ps_qp_params->u4_dead_zone);
1076 
1077             ps_qp_params->pu2_sad_thrsh[0] = u4_satdq_t / MAX(pu2_smat[3], pu2_smat[11]);
1078             ps_qp_params->pu2_sad_thrsh[1] = u4_satdq_t / MAX(pu2_smat[1], pu2_smat[9]);
1079             ps_qp_params->pu2_sad_thrsh[2] = u4_satdq_t / pu2_smat[15];
1080             ps_qp_params->pu2_sad_thrsh[3] = u4_satdq_t / pu2_smat[7];
1081             ps_qp_params->pu2_sad_thrsh[4] = u4_satdq_t / MAX(pu2_smat[12], pu2_smat[14]);
1082             ps_qp_params->pu2_sad_thrsh[5] = u4_satdq_t / MAX(pu2_smat[4], pu2_smat[6]);
1083             ps_qp_params->pu2_sad_thrsh[6] = u4_satdq_t / pu2_smat[13];
1084             ps_qp_params->pu2_sad_thrsh[7] = u4_satdq_t / pu2_smat[5];
1085             ps_qp_params->pu2_sad_thrsh[8] = u4_satdq_t / MAX(MAX3(pu2_smat[0], pu2_smat[2], pu2_smat[8]), pu2_smat[10]);
1086         }
1087 
1088         /* inverse scale matrix */
1089         ps_qp_params->pu2_iscale_mat = pu2_iscale_mat + (u4_qp_mod6 * 16);
1090 
1091         plane += 1;
1092     }
1093     return ;
1094 }
1095 
1096 /**
1097 *******************************************************************************
1098 *
1099 * @brief
1100 *  Initialize AIR mb frame Map
1101 *
1102 * @par Description:
1103 *  Initialize AIR mb frame map
1104 *  MB frame map indicates which frame an Mb should be coded as intra according to AIR
1105 *
1106 * @param[in] ps_codec
1107 *  Pointer to codec context
1108 *
1109 * @returns  error_status
1110 *
1111 * @remarks
1112 *
1113 *
1114 *******************************************************************************
1115 */
ih264e_init_air_map(codec_t * ps_codec)1116 IH264E_ERROR_T ih264e_init_air_map(codec_t *ps_codec)
1117 {
1118     /* intra refresh map */
1119     UWORD16 *pu2_intr_rfrsh_map = ps_codec->pu2_intr_rfrsh_map;
1120 
1121     /* air mode */
1122     IVE_AIR_MODE_T air_mode = ps_codec->s_cfg.e_air_mode;
1123 
1124     /* refresh period */
1125     UWORD32 air_period = ps_codec->s_cfg.u4_air_refresh_period;
1126 
1127     /* mb cnt */
1128     UWORD32 u4_mb_cnt = ps_codec->s_cfg.i4_wd_mbs * ps_codec->s_cfg.i4_ht_mbs;
1129 
1130     /* temp var */
1131     UWORD32 curr_mb, seed_rand = 1;
1132 
1133     switch (air_mode)
1134     {
1135         case IVE_AIR_MODE_CYCLIC:
1136 
1137             for (curr_mb = 0; curr_mb < u4_mb_cnt; curr_mb++)
1138             {
1139                 pu2_intr_rfrsh_map[curr_mb] = curr_mb % air_period;
1140             }
1141             break;
1142 
1143         case IVE_AIR_MODE_RANDOM:
1144 
1145             for (curr_mb = 0; curr_mb < u4_mb_cnt; curr_mb++)
1146             {
1147                 seed_rand = (seed_rand * 32719 + 3) % 32749;
1148                 pu2_intr_rfrsh_map[curr_mb] = seed_rand % air_period;
1149             }
1150             break;
1151 
1152         default:
1153 
1154             break;
1155     }
1156 
1157     return IH264E_SUCCESS;
1158 }
1159 
1160 /**
1161 *******************************************************************************
1162 *
1163 * @brief
1164 *  Codec level initializations
1165 *
1166 * @par Description:
1167 *  Initializes the codec with parameters that needs to be set before encoding
1168 *  first frame
1169 *
1170 * @param[in] ps_codec
1171 *  Pointer to codec context
1172 *
1173 * @param[in] ps_inp_buf
1174 *  Pointer to input buffer context
1175 *
1176 * @returns  error_status
1177 *
1178 * @remarks
1179 *
1180 *
1181 *******************************************************************************
1182 */
ih264e_codec_init(codec_t * ps_codec)1183 IH264E_ERROR_T ih264e_codec_init(codec_t *ps_codec)
1184 {
1185     /********************************************************************
1186      *                     INITIALIZE CODEC CONTEXT                     *
1187      ********************************************************************/
1188     /* encoder presets */
1189     if (ps_codec->s_cfg.u4_enc_speed_preset != IVE_CONFIG)
1190     {
1191         if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_SLOWEST)
1192         {/* high quality */
1193             /* enable diamond search */
1194             ps_codec->s_cfg.u4_me_speed_preset = DMND_SRCH;
1195             ps_codec->s_cfg.u4_enable_fast_sad = 0;
1196 
1197             /* disable intra 4x4 */
1198             ps_codec->s_cfg.u4_enable_intra_4x4 = 1;
1199             ps_codec->luma_energy_compaction[1] =
1200                             ih264e_code_luma_intra_macroblock_4x4_rdopt_on;
1201 
1202             /* sub pel off */
1203             ps_codec->s_cfg.u4_enable_hpel = 1;
1204 
1205             /* deblocking off */
1206             ps_codec->s_cfg.u4_disable_deblock_level = DISABLE_DEBLK_LEVEL_0;
1207 
1208             /* disabled intra inter gating in Inter slices */
1209             ps_codec->u4_inter_gate = 0;
1210         }
1211         else if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_NORMAL)
1212         {/* normal */
1213             /* enable diamond search */
1214             ps_codec->s_cfg.u4_me_speed_preset = DMND_SRCH;
1215             ps_codec->s_cfg.u4_enable_fast_sad = 0;
1216 
1217             /* disable intra 4x4 */
1218             ps_codec->s_cfg.u4_enable_intra_4x4 = 1;
1219 
1220             /* sub pel off */
1221             ps_codec->s_cfg.u4_enable_hpel = 1;
1222 
1223             /* deblocking off */
1224             ps_codec->s_cfg.u4_disable_deblock_level = DISABLE_DEBLK_LEVEL_0;
1225 
1226             /* disabled intra inter gating in Inter slices */
1227             ps_codec->u4_inter_gate = 0;
1228         }
1229         else if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_FAST)
1230          {/* normal */
1231              /* enable diamond search */
1232              ps_codec->s_cfg.u4_me_speed_preset = DMND_SRCH;
1233              ps_codec->s_cfg.u4_enable_fast_sad = 0;
1234 
1235              /* disable intra 4x4 */
1236              ps_codec->s_cfg.u4_enable_intra_4x4 = 0;
1237 
1238              /* sub pel off */
1239              ps_codec->s_cfg.u4_enable_hpel = 1;
1240 
1241              /* deblocking off */
1242              ps_codec->s_cfg.u4_disable_deblock_level = DISABLE_DEBLK_LEVEL_0;
1243 
1244              /* disabled intra inter gating in Inter slices */
1245              ps_codec->u4_inter_gate = 1;
1246          }
1247         else if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_HIGH_SPEED)
1248         {/* fast */
1249             /* enable diamond search */
1250             ps_codec->s_cfg.u4_me_speed_preset = DMND_SRCH;
1251             ps_codec->s_cfg.u4_enable_fast_sad = 0;
1252 
1253             /* disable intra 4x4 */
1254             ps_codec->s_cfg.u4_enable_intra_4x4 = 0;
1255 
1256             /* sub pel off */
1257             ps_codec->s_cfg.u4_enable_hpel = 0;
1258 
1259             /* deblocking off */
1260             ps_codec->s_cfg.u4_disable_deblock_level = DISABLE_DEBLK_LEVEL_4;
1261 
1262             /* disabled intra inter gating in Inter slices */
1263             ps_codec->u4_inter_gate = 0;
1264         }
1265         else if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_FASTEST)
1266         {/* fastest */
1267             /* enable diamond search */
1268             ps_codec->s_cfg.u4_me_speed_preset = DMND_SRCH;
1269 
1270             /* disable intra 4x4 */
1271             ps_codec->s_cfg.u4_enable_intra_4x4 = 0;
1272 
1273             /* sub pel off */
1274             ps_codec->s_cfg.u4_enable_hpel = 0;
1275 
1276             /* deblocking off */
1277             ps_codec->s_cfg.u4_disable_deblock_level = DISABLE_DEBLK_LEVEL_4;
1278 
1279             /* disabled intra inter gating in Inter slices */
1280             ps_codec->u4_inter_gate = 1;
1281         }
1282     }
1283 
1284     /*****************************************************************
1285      * Initialize AIR inside codec
1286      *****************************************************************/
1287     if (IVE_AIR_MODE_NONE != ps_codec->s_cfg.e_air_mode)
1288     {
1289         ih264e_init_air_map(ps_codec);
1290 
1291         ps_codec->i4_air_pic_cnt = -1;
1292     }
1293 
1294     /****************************************************/
1295     /*           INITIALIZE RATE CONTROL                */
1296     /****************************************************/
1297     {
1298         /* init qp */
1299         UWORD8 au1_init_qp[MAX_PIC_TYPE];
1300 
1301         /* min max qp */
1302         UWORD8 au1_min_max_qp[2 * MAX_PIC_TYPE];
1303 
1304         /* init i,p,b qp */
1305         au1_init_qp[0] = gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_i_qp];
1306         au1_init_qp[1] = gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_p_qp];
1307         au1_init_qp[2] = gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_b_qp];
1308 
1309         /* init min max qp */
1310         au1_min_max_qp[2 * I_PIC] =
1311                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_i_qp_min];
1312         au1_min_max_qp[2 * I_PIC + 1] =
1313                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_i_qp_max];
1314 
1315         au1_min_max_qp[2 * P_PIC] =
1316                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_p_qp_min];
1317         au1_min_max_qp[2 * P_PIC + 1] =
1318                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_p_qp_max];
1319 
1320         au1_min_max_qp[2 * B_PIC] =
1321                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_b_qp_min];
1322         au1_min_max_qp[2 * B_PIC + 1] =
1323                         gau1_h264_to_mpeg2_qmap[ps_codec->s_cfg.u4_b_qp_max];
1324 
1325         /* get rc mode */
1326         switch (ps_codec->s_cfg.e_rc_mode)
1327         {
1328             case IVE_RC_STORAGE:
1329                 ps_codec->s_rate_control.e_rc_type = VBR_STORAGE;
1330                 break;
1331             case IVE_RC_CBR_NON_LOW_DELAY:
1332                 ps_codec->s_rate_control.e_rc_type = CBR_NLDRC;
1333                 break;
1334             case IVE_RC_CBR_LOW_DELAY:
1335                 ps_codec->s_rate_control.e_rc_type = CBR_LDRC;
1336                 break;
1337             case IVE_RC_NONE:
1338                 ps_codec->s_rate_control.e_rc_type = CONST_QP;
1339                 break;
1340             default:
1341                 break;
1342         }
1343 
1344         /* init rate control */
1345         ih264e_rc_init(ps_codec->s_rate_control.pps_rate_control_api,
1346                        ps_codec->s_rate_control.pps_frame_time,
1347                        ps_codec->s_rate_control.pps_time_stamp,
1348                        ps_codec->s_rate_control.pps_pd_frm_rate,
1349                        ps_codec->s_cfg.u4_max_framerate,
1350                        ps_codec->s_cfg.u4_src_frame_rate,
1351                        ps_codec->s_cfg.u4_tgt_frame_rate,
1352                        ps_codec->s_rate_control.e_rc_type,
1353                        ps_codec->s_cfg.u4_target_bitrate,
1354                        ps_codec->s_cfg.u4_max_bitrate,
1355                        ps_codec->s_cfg.u4_vbv_buffer_delay,
1356                        ps_codec->s_cfg.u4_i_frm_interval,
1357                        ps_codec->s_cfg.u4_num_bframes + 1, au1_init_qp,
1358                        ps_codec->s_cfg.u4_num_bframes + 2 , au1_min_max_qp,
1359                        MAX(ps_codec->s_cfg.u4_max_level,
1360                                (UWORD32)ih264e_get_min_level(ps_codec->s_cfg.u4_max_wd, ps_codec->s_cfg.u4_max_ht)));
1361     }
1362 
1363     /* recon stride */
1364     ps_codec->i4_rec_strd = ALIGN16(ps_codec->s_cfg.u4_max_wd) + PAD_WD;
1365 
1366     /* max ref and reorder cnt */
1367     ps_codec->i4_ref_buf_cnt = ps_codec->s_cfg.u4_max_ref_cnt
1368                     + ps_codec->s_cfg.u4_max_reorder_cnt;
1369     ps_codec->i4_ref_buf_cnt += MAX_CTXT_SETS;
1370 
1371     DEBUG_HISTOGRAM_INIT();
1372 
1373 
1374     /* Init dependecy vars */
1375     ps_codec->i4_last_inp_buff_received = 0;
1376 
1377     /* At codec start no IDR is pending */
1378     ps_codec->i4_pending_idr_flag = 0;
1379 
1380     return IH264E_SUCCESS;
1381 }
1382 
1383 /**
1384 *******************************************************************************
1385 *
1386 * @brief
1387 *  Picture level initializations
1388 *
1389 * @par Description:
1390 *  Before beginning to encode the frame, the current function initializes all
1391 *  the ctxts (proc, entropy, me, ...) basing on the input configured params.
1392 *  It locates space for storing recon in the encoder picture buffer set, fetches
1393 *  reference frame from encoder picture buffer set. Calls RC pre-enc to get
1394 *  qp and pic type for the current frame. Queues proc jobs so that
1395 *  the other threads can begin encoding. In brief, this function sets up the
1396 *  tone for the entire encoder.
1397 *
1398 * @param[in] ps_codec
1399 *  Pointer to codec context
1400 *
1401 * @param[in] ps_inp_buf
1402 *  Pointer to input buffer context
1403 *
1404 * @returns  error_status
1405 *
1406 * @remarks
1407 *
1408 *
1409 *******************************************************************************
1410 */
ih264e_pic_init(codec_t * ps_codec,inp_buf_t * ps_inp_buf)1411 IH264E_ERROR_T ih264e_pic_init(codec_t *ps_codec, inp_buf_t *ps_inp_buf)
1412 {
1413     /* error status */
1414     IH264E_ERROR_T error_status = IH264E_SUCCESS;
1415     IH264_ERROR_T ret = IH264_SUCCESS;
1416 
1417     /* mv buff bank */
1418     mv_buf_t *ps_mv_buf = NULL;
1419     WORD32 cur_mv_bank_buf_id;
1420 
1421     /* recon buffer set */
1422     pic_buf_t *ps_cur_pic;
1423     WORD32 cur_pic_buf_id;
1424     UWORD8 *pu1_cur_pic_luma, *pu1_cur_pic_chroma;
1425 
1426     /* ref buffer set */
1427     pic_buf_t *aps_ref_pic[MAX_REF_PIC_CNT] = {NULL, NULL};
1428     mv_buf_t *aps_mv_buf[MAX_REF_PIC_CNT] = {NULL, NULL};
1429     WORD32 ref_set_id;
1430 
1431     /* pic time stamp */
1432     UWORD32 u4_timestamp_high = ps_inp_buf->u4_timestamp_high;
1433     UWORD32 u4_timestamp_low = ps_inp_buf->u4_timestamp_low;
1434 
1435     /* indices to access curr/prev frame info */
1436     WORD32 ctxt_sel = ps_codec->i4_encode_api_call_cnt % MAX_CTXT_SETS;
1437 
1438     /* curr pic type */
1439     PIC_TYPE_T *pic_type = &ps_codec->pic_type;
1440 
1441     /* Diamond search Iteration Max Cnt */
1442     UWORD32 u4_num_layers =
1443                     (ps_codec->s_cfg.u4_enc_speed_preset == IVE_FASTEST) ?
1444                                     (NUM_LAYERS >> 2) : NUM_LAYERS;
1445 
1446     /* enable fast sad */
1447     UWORD32 u4_enable_fast_sad = ps_codec->s_cfg.u4_enable_fast_sad;
1448 
1449     /********************************************************************/
1450     /*                     INITIALIZE CODEC CONTEXT                     */
1451     /********************************************************************/
1452     /* slice_type */
1453     if ((PIC_I == *pic_type) || (PIC_IDR == *pic_type))
1454     {
1455         ps_codec->i4_slice_type = ISLICE;
1456     }
1457     else if (PIC_P == *pic_type)
1458     {
1459         ps_codec->i4_slice_type = PSLICE;
1460     }
1461     else if(PIC_B == *pic_type)
1462     {
1463         ps_codec->i4_slice_type = BSLICE;
1464     }
1465 
1466 
1467     /***************************************************************************
1468      * Set up variables for sending frame number, poc and reference
1469      *   a) Set up alt ref too
1470      **************************************************************************/
1471 
1472     /* Check and set if the current frame is reference or not */
1473     ps_codec->u4_is_curr_frm_ref = 0;
1474 
1475     /* This frame is reference if its not a B pic, pending approval from alt ref */
1476     ps_codec->u4_is_curr_frm_ref = (*pic_type != PIC_B);
1477 
1478     /* In case if its a P pic, we will decide according to alt ref also */
1479     if (ps_codec->s_cfg.u4_enable_alt_ref && (*pic_type == PIC_P)
1480                     && (ps_codec->i4_pic_cnt
1481                                     % (ps_codec->s_cfg.u4_enable_alt_ref + 1)))
1482     {
1483         ps_codec->u4_is_curr_frm_ref = 0;
1484     }
1485 
1486     /*
1487      * Override everything in case of IDR
1488      * Note that in case of IDR, at this point ps_codec->u4_is_curr_frm_ref must
1489      * be 1
1490      */
1491 
1492     /* is this an IDR pic */
1493     ps_codec->u4_is_idr = 0;
1494 
1495     if (PIC_IDR == *pic_type)
1496     {
1497         /* set idr flag */
1498         ps_codec->u4_is_idr = 1;
1499 
1500         /* reset frame num */
1501         ps_codec->i4_frame_num = 0;
1502 
1503         /* idr_pic_id */
1504         ps_codec->i4_idr_pic_id++;
1505     }
1506 
1507     /***************************************************************************
1508      * Set up Deblock
1509      **************************************************************************/
1510 
1511     /* set deblock disable flags based on disable deblock level */
1512     ps_codec->i4_disable_deblk_pic = 1;
1513 
1514     if (ps_codec->s_cfg.u4_disable_deblock_level == DISABLE_DEBLK_LEVEL_0)
1515     {
1516         /* enable deblocking */
1517         ps_codec->i4_disable_deblk_pic = 0;
1518     }
1519     else if (ps_codec->s_cfg.u4_disable_deblock_level == DISABLE_DEBLK_LEVEL_2)
1520     {
1521         /* enable deblocking after a period of frames */
1522         if (ps_codec->i4_disable_deblk_pic_cnt == DISABLE_DEBLOCK_INTERVAL
1523                         || ps_codec->i4_slice_type == ISLICE)
1524         {
1525             ps_codec->i4_disable_deblk_pic = 0;
1526         }
1527     }
1528     else if (ps_codec->s_cfg.u4_disable_deblock_level == DISABLE_DEBLK_LEVEL_3)
1529     {
1530         if (ps_codec->i4_slice_type == ISLICE)
1531         {
1532             ps_codec->i4_disable_deblk_pic = 0;
1533         }
1534     }
1535 
1536     if (ps_codec->i4_disable_deblk_pic)
1537     {
1538         ps_codec->i4_disable_deblk_pic_cnt++;
1539     }
1540     else
1541     {
1542         ps_codec->i4_disable_deblk_pic_cnt = 0;
1543     }
1544 
1545     /* In slice mode - lets not deblk mb edges that lie along slice boundaries */
1546     if (ps_codec->i4_disable_deblk_pic == 0)
1547     {
1548         if (ps_codec->s_cfg.e_slice_mode != IVE_SLICE_MODE_NONE)
1549         {
1550             ps_codec->i4_disable_deblk_pic = 2;
1551         }
1552     }
1553 
1554     /* error status */
1555     ps_codec->i4_error_code = IH264E_SUCCESS;
1556 
1557     /* populate header */
1558     if (ps_codec->i4_gen_header)
1559     {
1560         /* sps */
1561         sps_t *ps_sps = NULL;
1562 
1563         /* pps */
1564         pps_t *ps_pps = NULL;
1565 
1566         /*ps_codec->i4_pps_id ++;*/
1567         ps_codec->i4_pps_id %= MAX_PPS_CNT;
1568 
1569         /*ps_codec->i4_sps_id ++;*/
1570         ps_codec->i4_sps_id %= MAX_SPS_CNT;
1571 
1572         /* populate sps header */
1573         ps_sps = ps_codec->ps_sps_base + ps_codec->i4_sps_id;
1574         ih264e_populate_sps(ps_codec, ps_sps);
1575 
1576         /* populate pps header */
1577         ps_pps = ps_codec->ps_pps_base + ps_codec->i4_pps_id;
1578         ih264e_populate_pps(ps_codec, ps_pps);
1579     }
1580 
1581     /***************************************************************************
1582      *  Reference and MV bank Buffer Manager
1583      *  Here we will
1584      *      1) Find the correct ref pics for the current frame
1585      *      2) Free the ref pic that is not going to be used anywhere
1586      *      3) Find a free buff from the list and assign it as the recon of
1587      *         current frame
1588      *
1589      *  1) Finding correct ref pic
1590      *      All pics needed for future are arranged in a picture list called
1591      *      ps_codec->as_ref_set. Each picture in this will have a pic buffer and
1592      *      MV buffer that is marked appropriately as BUF_MGR_REF, BUF_MGR_IO or
1593      *      BUF_MGR_CODEC. Also the pic_cnt and poc will also be present.
1594      *      Hence to find the ref pic we will loop through the list and find
1595      *      2 pictures with maximum i4_pic_cnt .
1596      *
1597      *      note that i4_pic_cnt == -1 is used to filter uninit ref pics.
1598      *      Now since we only have max two ref pics, we will always find max 2
1599      *      ref pics.
1600 
1601      *
1602      *  2) 3) Self explanatory
1603      ***************************************************************************/
1604     {
1605         /* Search for buffs with maximum pic cnt */
1606 
1607         WORD32 max_pic_cnt[] = { -1, -1 };
1608 
1609         mv_buf_t *ps_mv_buf_to_free[] = { NULL, NULL };
1610 
1611         /* temp var */
1612         WORD32 i, buf_status;
1613 
1614         for (i = 0; i < ps_codec->i4_ref_buf_cnt; i++)
1615         {
1616             if (ps_codec->as_ref_set[i].i4_pic_cnt == -1)
1617                 continue;
1618 
1619             buf_status = ih264_buf_mgr_get_status(
1620                             ps_codec->pv_ref_buf_mgr,
1621                             ps_codec->as_ref_set[i].ps_pic_buf->i4_buf_id);
1622 
1623             /* Ideally we should look for buffer status of MV BUFF also. But since
1624              * the correponding MV buffs also will be at the same state. It dosent
1625              * matter as of now. But the check will make the logic better */
1626             if ((max_pic_cnt[0] < ps_codec->as_ref_set[i].i4_pic_cnt)
1627                             && (buf_status & BUF_MGR_REF))
1628             {
1629                 if (max_pic_cnt[1] < ps_codec->as_ref_set[i].i4_pic_cnt)
1630                 {
1631                     max_pic_cnt[0] = max_pic_cnt[1];
1632                     aps_ref_pic[0] = aps_ref_pic[1];
1633                     aps_mv_buf[0] = aps_mv_buf[1];
1634 
1635                     ps_mv_buf_to_free[0] = ps_mv_buf_to_free[1];
1636 
1637                     max_pic_cnt[1] = ps_codec->as_ref_set[i].i4_pic_cnt;
1638                     aps_ref_pic[1] = ps_codec->as_ref_set[i].ps_pic_buf;
1639                     aps_mv_buf[1] = ps_codec->as_ref_set[i].ps_mv_buf;
1640                     ps_mv_buf_to_free[1] = ps_codec->as_ref_set[i].ps_mv_buf;
1641 
1642                 }
1643                 else
1644                 {
1645                     max_pic_cnt[0] = ps_codec->as_ref_set[i].i4_pic_cnt;
1646                     aps_ref_pic[0] = ps_codec->as_ref_set[i].ps_pic_buf;
1647                     aps_mv_buf[0] = ps_codec->as_ref_set[i].ps_mv_buf;
1648                     ps_mv_buf_to_free[0] = ps_codec->as_ref_set[i].ps_mv_buf;
1649                 }
1650             }
1651         }
1652 
1653         /*
1654          * Now if the current picture is I or P, we discard the back ref pic and
1655          * assign forward ref as backward ref
1656          */
1657         if (*pic_type != PIC_B)
1658         {
1659             if (ps_mv_buf_to_free[0])
1660             {
1661                 /* release this frame from reference list */
1662                 ih264_buf_mgr_release(ps_codec->pv_mv_buf_mgr,
1663                                       ps_mv_buf_to_free[0]->i4_buf_id,
1664                                       BUF_MGR_REF);
1665 
1666                 ih264_buf_mgr_release(ps_codec->pv_ref_buf_mgr,
1667                                       aps_ref_pic[0]->i4_buf_id, BUF_MGR_REF);
1668             }
1669 
1670             max_pic_cnt[0] = max_pic_cnt[1];
1671             aps_ref_pic[0] = aps_ref_pic[1];
1672             aps_mv_buf[0] = aps_mv_buf[1];
1673 
1674             /* Dummy */
1675             max_pic_cnt[1] = -1;
1676         }
1677 
1678         /*
1679          * Mark all reference pic with unused buffers to be free
1680          * We need this step since each one, ie ref, recon io etc only unset their
1681          * respective flags. Hence we need to combine togather and mark the ref set
1682          * accordingly
1683          */
1684         ref_set_id = -1;
1685         for (i = 0; i < ps_codec->i4_ref_buf_cnt; i++)
1686         {
1687             if (ps_codec->as_ref_set[i].i4_pic_cnt == -1)
1688             {
1689                 ref_set_id = i;
1690                 continue;
1691             }
1692 
1693             buf_status = ih264_buf_mgr_get_status(
1694                             ps_codec->pv_ref_buf_mgr,
1695                             ps_codec->as_ref_set[i].ps_pic_buf->i4_buf_id);
1696 
1697             if ((buf_status & (BUF_MGR_REF | BUF_MGR_CODEC | BUF_MGR_IO)) == 0)
1698             {
1699                 ps_codec->as_ref_set[i].i4_pic_cnt = -1;
1700                 ps_codec->as_ref_set[i].i4_poc = 32768;
1701 
1702                 ref_set_id = i;
1703             }
1704         }
1705         /* An asssert failure here means we donot have any free buffs */
1706         ASSERT(ref_set_id >= 0);
1707     }
1708 
1709     {
1710         /*****************************************************************/
1711         /* Get free MV Bank to hold current picture's motion vector data */
1712         /* If there are no free buffers then return with an error code.  */
1713         /* If the buffer is to be freed by another thread, change the    */
1714         /* following to call thread yield and wait for buffer to be freed*/
1715         /*****************************************************************/
1716         ps_mv_buf = (mv_buf_t *) ih264_buf_mgr_get_next_free(
1717                         (buf_mgr_t *) ps_codec->pv_mv_buf_mgr,
1718                         &cur_mv_bank_buf_id);
1719 
1720         if (NULL == ps_mv_buf)
1721         {
1722             ps_codec->i4_error_code = IH264E_NO_FREE_MVBANK;
1723             return IH264E_NO_FREE_MVBANK;
1724         }
1725 
1726         /* mark the buffer as needed for reference if the curr pic is available for ref */
1727         if (ps_codec->u4_is_curr_frm_ref)
1728         {
1729             ih264_buf_mgr_set_status(ps_codec->pv_mv_buf_mgr,
1730                                      cur_mv_bank_buf_id, BUF_MGR_REF);
1731         }
1732 
1733         /* Set current ABS poc to ps_mv_buf, so that while freeing a reference buffer
1734          * corresponding mv buffer can be found by looping through ps_codec->ps_mv_buf array
1735          * and getting a buffer id to free
1736          */
1737         ps_mv_buf->i4_abs_poc = ps_codec->i4_abs_pic_order_cnt;
1738         ps_mv_buf->i4_buf_id = cur_mv_bank_buf_id;
1739     }
1740 
1741     {
1742         /*****************************************************************/
1743         /* Get free pic buf to hold current picture's recon data         */
1744         /* If there are no free buffers then return with an error code.  */
1745         /* If the buffer is to be freed by another thread, change the    */
1746         /* following to call thread yield and wait for buffer to be freed*/
1747         /*****************************************************************/
1748         ps_cur_pic = (pic_buf_t *) ih264_buf_mgr_get_next_free(
1749                         (buf_mgr_t *) ps_codec->pv_ref_buf_mgr,
1750                         &cur_pic_buf_id);
1751 
1752         if (NULL == ps_cur_pic)
1753         {
1754             ps_codec->i4_error_code = IH264E_NO_FREE_PICBUF;
1755             return IH264E_NO_FREE_PICBUF;
1756         }
1757 
1758         /* mark the buffer as needed for reference if the curr pic is available for ref */
1759         if (ps_codec->u4_is_curr_frm_ref)
1760         {
1761             ih264_buf_mgr_set_status(ps_codec->pv_ref_buf_mgr, cur_pic_buf_id,
1762                                      BUF_MGR_REF);
1763         }
1764 
1765         /* Mark the current buffer as needed for IO if recon is enabled */
1766         if (1 == ps_codec->s_cfg.u4_enable_recon)
1767         {
1768             ih264_buf_mgr_set_status(ps_codec->pv_ref_buf_mgr, cur_pic_buf_id,
1769                                      BUF_MGR_IO);
1770         }
1771 
1772         /* Associate input timestamp with current buffer */
1773         ps_cur_pic->u4_timestamp_high = ps_inp_buf->u4_timestamp_high;
1774         ps_cur_pic->u4_timestamp_low = ps_inp_buf->u4_timestamp_low;
1775 
1776         ps_cur_pic->i4_abs_poc = ps_codec->i4_poc;
1777         ps_cur_pic->i4_poc_lsb = ps_codec->i4_pic_order_cnt_lsb;
1778 
1779         ps_cur_pic->i4_buf_id = cur_pic_buf_id;
1780 
1781         pu1_cur_pic_luma = ps_cur_pic->pu1_luma;
1782         pu1_cur_pic_chroma = ps_cur_pic->pu1_chroma;
1783     }
1784 
1785     /*
1786      * Add the current picture to ref list independent of the fact that it is used
1787      * as reference or not. This is because, now recon is not in sync with output
1788      * hence we may need the current recon after some delay. By adding it to ref list
1789      * we can retrieve the recon any time we want. The information that it is used
1790      * for ref can still be found by checking the buffer status of pic buf.
1791      */
1792     {
1793         ps_codec->as_ref_set[ref_set_id].i4_pic_cnt = ps_codec->i4_pic_cnt;
1794         ps_codec->as_ref_set[ref_set_id].i4_poc = ps_codec->i4_poc;
1795         ps_codec->as_ref_set[ref_set_id].ps_mv_buf = ps_mv_buf;
1796         ps_codec->as_ref_set[ref_set_id].ps_pic_buf = ps_cur_pic;
1797     }
1798 
1799     /********************************************************************/
1800     /*                     INITIALIZE PROCESS CONTEXT                   */
1801     /********************************************************************/
1802     {
1803         /* temp var */
1804         WORD32 i, j = 0;
1805 
1806         /* curr proc ctxt */
1807         process_ctxt_t *ps_proc = NULL;
1808 
1809         j = ctxt_sel * MAX_PROCESS_THREADS;
1810 
1811         /* begin init */
1812         for (i = j; i < (j + MAX_PROCESS_THREADS); i++)
1813         {
1814             ps_proc = &ps_codec->as_process[i];
1815 
1816             /* luma src buffer */
1817             if (ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_422ILE)
1818             {
1819                 ps_proc->pu1_src_buf_luma_base = ps_codec->pu1_y_csc_buf_base;
1820             }
1821             else
1822             {
1823                 ps_proc->pu1_src_buf_luma_base =
1824                                 ps_inp_buf->s_raw_buf.apv_bufs[0];
1825             }
1826 
1827             /* chroma src buffer */
1828             if (ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_422ILE
1829                             || ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420P)
1830             {
1831                 ps_proc->pu1_src_buf_chroma_base =
1832                                 ps_codec->pu1_uv_csc_buf_base;
1833             }
1834             else
1835             {
1836                 ps_proc->pu1_src_buf_chroma_base =
1837                                 ps_inp_buf->s_raw_buf.apv_bufs[1];
1838             }
1839 
1840             /* luma rec buffer */
1841             ps_proc->pu1_rec_buf_luma_base = pu1_cur_pic_luma;
1842 
1843             /* chroma rec buffer */
1844             ps_proc->pu1_rec_buf_chroma_base = pu1_cur_pic_chroma;
1845 
1846             /* rec stride */
1847             ps_proc->i4_rec_strd = ps_codec->i4_rec_strd;
1848 
1849             /* frame num */
1850             ps_proc->i4_frame_num = ps_codec->i4_frame_num;
1851 
1852             /* is idr */
1853             ps_proc->u4_is_idr = ps_codec->u4_is_idr;
1854 
1855             /* idr pic id */
1856             ps_proc->u4_idr_pic_id = ps_codec->i4_idr_pic_id;
1857 
1858             /* slice_type */
1859             ps_proc->i4_slice_type = ps_codec->i4_slice_type;
1860 
1861             /* Input width in mbs */
1862             ps_proc->i4_wd_mbs = ps_codec->s_cfg.i4_wd_mbs;
1863 
1864             /* Input height in mbs */
1865             ps_proc->i4_ht_mbs = ps_codec->s_cfg.i4_ht_mbs;
1866 
1867             /* Half x plane offset from pic buf */
1868             ps_proc->u4_half_x_offset = 0;
1869 
1870             /* Half y plane offset from half x plane */
1871             ps_proc->u4_half_y_offset = 0;
1872 
1873             /* Half x plane offset from half y plane */
1874             ps_proc->u4_half_xy_offset = 0;
1875 
1876             /* top row syntax elements */
1877             ps_proc->ps_top_row_mb_syntax_ele =
1878                             ps_proc->ps_top_row_mb_syntax_ele_base;
1879 
1880             ps_proc->pu1_top_mb_intra_modes =
1881                             ps_proc->pu1_top_mb_intra_modes_base;
1882 
1883             ps_proc->ps_top_row_pu = ps_proc->ps_top_row_pu_base;
1884 
1885             /* initialize quant params */
1886             ps_proc->u4_frame_qp = ps_codec->u4_frame_qp;
1887             ps_proc->u4_mb_qp = ps_codec->u4_frame_qp;
1888             ih264e_init_quant_params(ps_proc, ps_proc->u4_frame_qp);
1889 
1890             /* previous mb qp*/
1891             ps_proc->u4_mb_qp_prev = ps_proc->u4_frame_qp;
1892 
1893             /* Reset frame info */
1894             memset(&ps_proc->s_frame_info, 0, sizeof(frame_info_t));
1895 
1896             /* initialize proc, deblk and ME map */
1897             if (i == j)
1898             {
1899                 /* row '-1' */
1900                 memset(ps_proc->pu1_proc_map - ps_proc->i4_wd_mbs, 1, ps_proc->i4_wd_mbs);
1901                 /* row 0 to ht in mbs */
1902                 memset(ps_proc->pu1_proc_map, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
1903 
1904                 /* row '-1' */
1905                 memset(ps_proc->pu1_deblk_map - ps_proc->i4_wd_mbs, 1, ps_proc->i4_wd_mbs);
1906                 /* row 0 to ht in mbs */
1907                 memset(ps_proc->pu1_deblk_map, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
1908 
1909                 /* row '-1' */
1910                 memset(ps_proc->pu1_me_map - ps_proc->i4_wd_mbs, 1, ps_proc->i4_wd_mbs);
1911                 /* row 0 to ht in mbs */
1912                 memset(ps_proc->pu1_me_map, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
1913 
1914                 /* at the start of air refresh period, reset intra coded map */
1915                 if (IVE_AIR_MODE_NONE != ps_codec->s_cfg.e_air_mode)
1916                 {
1917                     ps_codec->i4_air_pic_cnt = (ps_codec->i4_air_pic_cnt + 1)
1918                                     % ps_codec->s_cfg.u4_air_refresh_period;
1919 
1920                     if (!ps_codec->i4_air_pic_cnt)
1921                     {
1922                         memset(ps_proc->pu1_is_intra_coded, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
1923                     }
1924                 }
1925             }
1926 
1927             /* deblock level */
1928             ps_proc->u4_disable_deblock_level = ps_codec->i4_disable_deblk_pic;
1929 
1930             /* slice index map */
1931             /* no slice */
1932             if (ps_codec->s_cfg.e_slice_mode == IVE_SLICE_MODE_NONE)
1933             {
1934                 memset(ps_proc->pu1_slice_idx, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
1935             }
1936             /* generate slices for every 'n' rows, 'n' is given through slice param */
1937             else if (ps_codec->s_cfg.e_slice_mode == IVE_SLICE_MODE_BLOCKS)
1938             {
1939                 /* slice idx map */
1940                 UWORD8 *pu1_slice_idx = ps_proc->pu1_slice_idx;
1941 
1942                 /* temp var */
1943                 WORD32 i4_mb_y = 0, slice_idx = 0, cnt;
1944 
1945                 while (i4_mb_y < ps_proc->i4_ht_mbs)
1946                 {
1947                     if (i4_mb_y +(WORD32)ps_codec->s_cfg.u4_slice_param < ps_proc->i4_ht_mbs)
1948                     {
1949                         cnt = ps_codec->s_cfg.u4_slice_param * ps_proc->i4_wd_mbs;
1950                         i4_mb_y += ps_codec->s_cfg.u4_slice_param;
1951                     }
1952                     else
1953                     {
1954                         cnt = (ps_proc->i4_ht_mbs - i4_mb_y) * ps_proc->i4_wd_mbs;
1955                         i4_mb_y += (ps_proc->i4_ht_mbs - i4_mb_y);
1956                     }
1957                     memset(pu1_slice_idx, slice_idx, cnt);
1958                     slice_idx++;
1959                     pu1_slice_idx += cnt;
1960                 }
1961             }
1962 
1963             /* Current MV Bank's buffer ID */
1964             ps_proc->i4_cur_mv_bank_buf_id = cur_mv_bank_buf_id;
1965 
1966             /* Pointer to current picture buffer structure */
1967             ps_proc->ps_cur_pic = ps_cur_pic;
1968 
1969             /* Pointer to current pictures mv buffers */
1970             ps_proc->ps_cur_mv_buf = ps_mv_buf;
1971 
1972             /*
1973              * pointer to ref picture
1974              * 0    : Temporal back reference
1975              * 1    : Temporal forward reference
1976              */
1977             ps_proc->aps_ref_pic[PRED_L0] = aps_ref_pic[PRED_L0];
1978             ps_proc->aps_ref_pic[PRED_L1] = aps_ref_pic[PRED_L1];
1979             if (ps_codec->pic_type == PIC_B)
1980             {
1981                 ps_proc->aps_mv_buf[PRED_L0] = aps_mv_buf[PRED_L0];
1982                 ps_proc->aps_mv_buf[PRED_L1] = aps_mv_buf[PRED_L1];
1983             }
1984             else
1985             {
1986                 /*
1987                  * Else is dummy since for non B pic we does not need this
1988                  * But an assignment here will help in not having a segfault
1989                  * when we calcualte colpic in P slices
1990                  */
1991                 ps_proc->aps_mv_buf[PRED_L0] = ps_mv_buf;
1992                 ps_proc->aps_mv_buf[PRED_L1] = ps_mv_buf;
1993             }
1994 
1995             if ((*pic_type != PIC_IDR) && (*pic_type != PIC_I))
1996             {
1997                 /* temporal back an forward  ref pointer luma and chroma */
1998                 ps_proc->apu1_ref_buf_luma_base[PRED_L0] = aps_ref_pic[PRED_L0]->pu1_luma;
1999                 ps_proc->apu1_ref_buf_chroma_base[PRED_L0] = aps_ref_pic[PRED_L0]->pu1_chroma;
2000 
2001                 ps_proc->apu1_ref_buf_luma_base[PRED_L1] = aps_ref_pic[PRED_L1]->pu1_luma;
2002                 ps_proc->apu1_ref_buf_chroma_base[PRED_L1] = aps_ref_pic[PRED_L1]->pu1_chroma;
2003             }
2004 
2005             /* Structure for current input buffer */
2006             ps_proc->s_inp_buf = *ps_inp_buf;
2007 
2008             /* Number of encode frame API calls made */
2009             ps_proc->i4_encode_api_call_cnt = ps_codec->i4_encode_api_call_cnt;
2010 
2011             /* Current Picture count */
2012             ps_proc->i4_pic_cnt = ps_codec->i4_pic_cnt;
2013 
2014             /* error status */
2015             ps_proc->i4_error_code = 0;
2016 
2017             /********************************************************************/
2018             /*                     INITIALIZE ENTROPY CONTEXT                   */
2019             /********************************************************************/
2020             {
2021                 entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
2022 
2023                 /* start of frame */
2024                 ps_entropy->i4_sof = 0;
2025 
2026                 /* end of frame */
2027                 ps_entropy->i4_eof = 0;
2028 
2029                 /* generate header */
2030                 ps_entropy->i4_gen_header = ps_codec->i4_gen_header;
2031 
2032                 /* sps ref_set_id */
2033                 ps_entropy->u4_sps_id = ps_codec->i4_sps_id;
2034 
2035                 /* sps base */
2036                 ps_entropy->ps_sps_base = ps_codec->ps_sps_base;
2037 
2038                 /* sps id */
2039                 ps_entropy->u4_pps_id = ps_codec->i4_pps_id;
2040 
2041                 /* sps base */
2042                 ps_entropy->ps_pps_base = ps_codec->ps_pps_base;
2043 
2044                 /* slice map */
2045                 ps_entropy->pu1_slice_idx = ps_proc->pu1_slice_idx;
2046 
2047                 /* slice hdr base */
2048                 ps_entropy->ps_slice_hdr_base = ps_proc->ps_slice_hdr_base;
2049 
2050                 /* Abs poc */
2051                 ps_entropy->i4_abs_pic_order_cnt = ps_proc->ps_codec->i4_poc;
2052 
2053                 /* initialize entropy map */
2054                 if (i == j)
2055                 {
2056                     /* row '-1' */
2057                     memset(ps_entropy->pu1_entropy_map - ps_proc->i4_wd_mbs, 1, ps_proc->i4_wd_mbs);
2058                     /* row 0 to ht in mbs */
2059                     memset(ps_entropy->pu1_entropy_map, 0, ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs);
2060 
2061                     /* intialize cabac tables */
2062                     ih264e_init_cabac_table(ps_entropy);
2063                 }
2064 
2065                 /* wd in mbs */
2066                 ps_entropy->i4_wd_mbs = ps_proc->i4_wd_mbs;
2067 
2068                 /* ht in mbs */
2069                 ps_entropy->i4_ht_mbs = ps_proc->i4_ht_mbs;
2070 
2071                 /* transform_8x8_mode_flag */
2072                 ps_entropy->i1_transform_8x8_mode_flag = 0;
2073 
2074                 /* entropy_coding_mode_flag */
2075                 ps_entropy->u1_entropy_coding_mode_flag =
2076                                 ps_codec->s_cfg.u4_entropy_coding_mode;
2077 
2078                 /* error code */
2079                 ps_entropy->i4_error_code = IH264E_SUCCESS;
2080 
2081                 /* mb skip run */
2082                 *(ps_proc->s_entropy.pi4_mb_skip_run) = 0;
2083 
2084                 /* last frame to encode */
2085                 ps_proc->s_entropy.u4_is_last = ps_inp_buf->u4_is_last;
2086 
2087                 /* Current Picture count */
2088                 ps_proc->s_entropy.i4_pic_cnt = ps_codec->i4_pic_cnt;
2089 
2090                 /* time stamps */
2091                 ps_entropy->u4_timestamp_low = u4_timestamp_low;
2092                 ps_entropy->u4_timestamp_high = u4_timestamp_high;
2093 
2094                 /* init frame statistics */
2095                 ps_entropy->u4_header_bits[MB_TYPE_INTRA] = 0;
2096                 ps_entropy->u4_header_bits[MB_TYPE_INTER] = 0;
2097                 ps_entropy->u4_residue_bits[MB_TYPE_INTRA] = 0;
2098                 ps_entropy->u4_residue_bits[MB_TYPE_INTER] = 0;
2099             }
2100 
2101             /********************************************************************/
2102             /*                     INITIALIZE DEBLOCK CONTEXT                   */
2103             /********************************************************************/
2104             {
2105                 /* deblk ctxt */
2106                 deblk_ctxt_t *ps_deblk = &ps_proc->s_deblk_ctxt;
2107 
2108                 /* slice idx map */
2109                 ps_deblk->pu1_slice_idx = ps_proc->pu1_slice_idx;
2110             }
2111 
2112             /********************************************************************/
2113             /*                     INITIALIZE ME CONTEXT                        */
2114             /********************************************************************/
2115             {
2116                 /* me ctxt */
2117                 me_ctxt_t *ps_me_ctxt = &ps_proc->s_me_ctxt;
2118 
2119                 /* srch range x */
2120                 ps_me_ctxt->ai2_srch_boundaries[0] =
2121                                 ps_codec->s_cfg.u4_srch_rng_x;
2122 
2123                 /* srch range y */
2124                 ps_me_ctxt->ai2_srch_boundaries[1] =
2125                                 ps_codec->s_cfg.u4_srch_rng_y;
2126 
2127                 /* rec stride */
2128                 ps_me_ctxt->i4_rec_strd = ps_codec->i4_rec_strd;
2129 
2130                 /* Half x plane offset from pic buf */
2131                 ps_me_ctxt->u4_half_x_offset = ps_proc->u4_half_x_offset;
2132 
2133                 /* Half y plane offset from half x plane */
2134                 ps_me_ctxt->u4_half_y_offset = ps_proc->u4_half_y_offset;
2135 
2136                 /* Half x plane offset from half y plane */
2137                 ps_me_ctxt->u4_half_xy_offset = ps_proc->u4_half_xy_offset;
2138 
2139                 /* enable fast sad */
2140                 ps_me_ctxt->u4_enable_fast_sad = u4_enable_fast_sad;
2141 
2142                 /* half pel */
2143                 ps_me_ctxt->u4_enable_hpel = ps_codec->s_cfg.u4_enable_hpel;
2144 
2145                 /* Diamond search Iteration Max Cnt */
2146                 ps_me_ctxt->u4_num_layers = u4_num_layers;
2147 
2148                 /* me speed preset */
2149                 ps_me_ctxt->u4_me_speed_preset =
2150                                 ps_codec->s_cfg.u4_me_speed_preset;
2151 
2152                 /* qp */
2153                 ps_me_ctxt->u1_mb_qp = ps_codec->u4_frame_qp;
2154 
2155                 if ((i == j) && (0 == ps_codec->i4_poc))
2156                 {
2157                     /* init mv bits tables */
2158                     ih264e_init_mv_bits(ps_me_ctxt);
2159                 }
2160             }
2161 
2162             ps_proc->ps_ngbr_avbl = &(ps_proc->s_ngbr_avbl);
2163 
2164         }
2165 
2166         /* reset encoder header */
2167         ps_codec->i4_gen_header = 0;
2168     }
2169 
2170     /********************************************************************/
2171     /*                       ADD JOBS TO THE QUEUE                      */
2172     /********************************************************************/
2173     {
2174         /* job structures */
2175         job_t s_job;
2176 
2177         /* temp var */
2178         WORD32 i;
2179 
2180         /* job class */
2181         s_job.i4_cmd = CMD_PROCESS;
2182 
2183         /* number of mbs to be processed in the current job */
2184         s_job.i2_mb_cnt = ps_codec->s_cfg.i4_wd_mbs;
2185 
2186         /* job start index x */
2187         s_job.i2_mb_x = 0;
2188 
2189         /* proc base idx */
2190         s_job.i2_proc_base_idx = ctxt_sel ? (MAX_PROCESS_CTXT / 2) : 0;
2191 
2192         for (i = 0; i < (WORD32)ps_codec->s_cfg.i4_ht_mbs; i++)
2193         {
2194             /* job start index y */
2195             s_job.i2_mb_y = i;
2196 
2197             /* queue the job */
2198             ret = ih264_list_queue(ps_codec->pv_proc_jobq, &s_job, 1);
2199             if (ret != IH264_SUCCESS)
2200             {
2201                 ps_codec->i4_error_code = ret;
2202                 return IH264E_FAIL;
2203             }
2204         }
2205 
2206         /* Once all the jobs are queued, terminate the queue */
2207         /* Since the threads are created and deleted in each call, terminating
2208         here is not an issue */
2209         ih264_list_terminate(ps_codec->pv_proc_jobq);
2210     }
2211 
2212     return error_status;
2213 }
2214 
2215 /**
2216 *******************************************************************************
2217 *
2218 * @brief
2219 *  Calculate the per-pic and global PSNR
2220 *
2221 * @par Description:
2222 *  This function takes the source and recon luma/chroma buffer pointers from the
2223 *  codec context and calculates the per-pic and global PSNR for the current encoding
2224 *  frame.
2225 *
2226 * @param[in] ps_codec
2227 *  Pointer to process context
2228 *
2229 * @returns  none
2230 *
2231 * @remarks
2232 *
2233 *
2234 *******************************************************************************
2235 */
ih264e_compute_quality_stats(process_ctxt_t * ps_proc)2236 void ih264e_compute_quality_stats(process_ctxt_t *ps_proc)
2237 {
2238     codec_t *ps_codec = ps_proc->ps_codec;
2239     WORD32 wd = ps_codec->s_cfg.u4_wd;
2240     WORD32 ht = ps_codec->s_cfg.u4_ht;
2241     WORD32 disp_wd = ps_codec->s_cfg.u4_disp_wd;
2242     WORD32 disp_ht = ps_codec->s_cfg.u4_disp_ht;
2243     WORD32 src_strds = ps_proc->i4_src_strd;
2244     WORD32 rec_strds = ps_proc->i4_rec_strd;
2245     quality_stats_t *ps_pic_quality_stats = NULL;
2246     double sum_squared_error[3] = {0.0, 0.0, 0.0};
2247     double total_samples[3];
2248     WORD32 i;
2249     for (i = 0; i < ps_codec->i4_ref_buf_cnt; i++)
2250     {
2251         if (ps_codec->as_ref_set[i].i4_pic_cnt != -1 &&
2252             ps_codec->as_ref_set[i].i4_poc == ps_codec->i4_poc)
2253         {
2254             ps_pic_quality_stats = &ps_codec->as_ref_set[i].s_pic_quality_stats;
2255             break;
2256         }
2257     }
2258 
2259     if(ps_pic_quality_stats == NULL) return;
2260 
2261     get_sse(
2262         ps_proc->pu1_src_buf_luma_base, ps_proc->pu1_rec_buf_luma_base,
2263         ps_proc->pu1_src_buf_chroma_base, ps_proc->pu1_rec_buf_chroma_base,
2264         src_strds, rec_strds, wd, ht, sum_squared_error);
2265 
2266     total_samples[0] = disp_wd * disp_ht;
2267     total_samples[1] = total_samples[2] = total_samples[0] / 4;
2268 
2269     ps_pic_quality_stats->total_frames = 1;
2270     ps_codec->s_global_quality_stats.total_frames += 1;
2271     for (i = 0; i < 3; i++)
2272     {
2273         double psnr = sse_to_psnr(total_samples[i], sum_squared_error[i]);
2274         ps_pic_quality_stats->total_samples[i] = total_samples[i];
2275         ps_pic_quality_stats->total_sse[i] = sum_squared_error[i];
2276         ps_pic_quality_stats->global_psnr[i] = ps_pic_quality_stats->avg_psnr[i] =
2277             ps_pic_quality_stats->total_psnr[i] = psnr;
2278         ps_codec->s_global_quality_stats.total_sse[i] += sum_squared_error[i];
2279         ps_codec->s_global_quality_stats.global_psnr[i] =
2280             sse_to_psnr(ps_codec->s_global_quality_stats.total_samples[i],
2281                 ps_codec->s_global_quality_stats.total_sse[i]);
2282         ps_codec->s_global_quality_stats.total_psnr[i] += psnr;
2283         ps_codec->s_global_quality_stats.avg_psnr[i] =
2284             ps_codec->s_global_quality_stats.total_psnr[i] /
2285                 ps_codec->s_global_quality_stats.total_frames;
2286     }
2287 }
2288