blob: 7acd2391ae0984ef8979c566d1c3dc1b8c6789ed [file] [log] [blame]
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301/******************************************************************************
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_process.c
25*
26* @brief
27* Contains functions for codec thread
28*
29* @author
30* Harish
31*
32* @par List of Functions:
33* - ih264e_generate_sps_pps()
34* - ih264e_init_entropy_ctxt()
35* - ih264e_entropy()
36* - ih264e_pack_header_data()
37* - ih264e_update_proc_ctxt()
38* - ih264e_init_proc_ctxt()
39* - ih264e_pad_recon_buffer()
40* - ih264e_dblk_pad_hpel_processing_n_mbs()
41* - ih264e_process()
42* - ih264e_set_rc_pic_params()
43* - ih264e_update_rc_post_enc()
44* - ih264e_process_thread()
45*
46* @remarks
47* None
48*
49*******************************************************************************
50*/
51
52/*****************************************************************************/
53/* File Includes */
54/*****************************************************************************/
55
56/* System include files */
57#include <stdio.h>
58#include <stddef.h>
59#include <stdlib.h>
60#include <string.h>
61#include <limits.h>
62#include <assert.h>
63
64/* User include files */
65#include "ih264_typedefs.h"
66#include "iv2.h"
67#include "ive2.h"
68#include "ih264_defs.h"
69#include "ih264_debug.h"
70#include "ime_distortion_metrics.h"
Harinarayanan K K134291e2015-06-18 16:03:38 +053071#include "ime_defs.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053072#include "ime_structs.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053073#include "ih264_error.h"
74#include "ih264_structs.h"
75#include "ih264_trans_quant_itrans_iquant.h"
76#include "ih264_inter_pred_filters.h"
77#include "ih264_mem_fns.h"
78#include "ih264_padding.h"
79#include "ih264_intra_pred_filters.h"
80#include "ih264_deblk_edge_filters.h"
Harinarayanan K K134291e2015-06-18 16:03:38 +053081#include "ih264_cabac_tables.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053082#include "ih264_platform_macros.h"
83#include "ih264_macros.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053084#include "ih264_buf_mgr.h"
85#include "ih264e_error.h"
86#include "ih264e_bitstream.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053087#include "ih264_common_tables.h"
88#include "ih264_list.h"
89#include "ih264e_defs.h"
90#include "irc_cntrl_param.h"
91#include "irc_frame_info_collector.h"
92#include "ih264e_rate_control.h"
Harinarayanan K K134291e2015-06-18 16:03:38 +053093#include "ih264e_cabac_structs.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053094#include "ih264e_structs.h"
Harinarayanan K K134291e2015-06-18 16:03:38 +053095#include "ih264e_cabac.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +053096#include "ih264e_process.h"
97#include "ithread.h"
98#include "ih264e_intra_modes_eval.h"
99#include "ih264e_encode_header.h"
100#include "ih264e_globals.h"
101#include "ih264e_config.h"
102#include "ih264e_trace.h"
103#include "ih264e_statistics.h"
104#include "ih264_cavlc_tables.h"
105#include "ih264e_cavlc.h"
106#include "ih264e_deblk.h"
107#include "ih264e_me.h"
108#include "ih264e_debug.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530109#include "ih264e_master.h"
110#include "ih264e_utils.h"
111#include "irc_mem_req_and_acq.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530112#include "irc_rate_control_api.h"
113#include "ih264e_platform_macros.h"
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530114#include "ime_statistics.h"
115
116
117/*****************************************************************************/
118/* Function Definitions */
119/*****************************************************************************/
120
121/**
122******************************************************************************
123*
124* @brief This function generates sps, pps set on request
125*
126* @par Description
127* When the encoder is set in header generation mode, the following function
128* is called. This generates sps and pps headers and returns the control back
129* to caller.
130*
131* @param[in] ps_codec
132* pointer to codec context
133*
134* @return success or failure error code
135*
136******************************************************************************
137*/
138IH264E_ERROR_T ih264e_generate_sps_pps(codec_t *ps_codec)
139{
140 /* choose between ping-pong process buffer set */
Harinarayanan K K6cb67722015-06-19 14:44:42 +0530141 WORD32 ctxt_sel = ps_codec->i4_encode_api_call_cnt % MAX_CTXT_SETS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530142
143 /* entropy ctxt */
144 entropy_ctxt_t *ps_entropy = &ps_codec->as_process[ctxt_sel * MAX_PROCESS_THREADS].s_entropy;
145
146 /* Bitstream structure */
147 bitstrm_t *ps_bitstrm = ps_entropy->ps_bitstrm;
148
149 /* sps */
150 sps_t *ps_sps = NULL;
151
152 /* pps */
153 pps_t *ps_pps = NULL;
154
155 /* output buff */
156 out_buf_t *ps_out_buf = &ps_codec->as_out_buf[ctxt_sel];
157
158
159 /********************************************************************/
160 /* initialize the bit stream buffer */
161 /********************************************************************/
162 ih264e_bitstrm_init(ps_bitstrm, ps_out_buf->s_bits_buf.pv_buf, ps_out_buf->s_bits_buf.u4_bufsize);
163
164 /********************************************************************/
165 /* BEGIN HEADER GENERATION */
166 /********************************************************************/
167 /*ps_codec->i4_pps_id ++;*/
168 ps_codec->i4_pps_id %= MAX_PPS_CNT;
169
170 /*ps_codec->i4_sps_id ++;*/
171 ps_codec->i4_sps_id %= MAX_SPS_CNT;
172
173 /* populate sps header */
174 ps_sps = ps_codec->ps_sps_base + ps_codec->i4_sps_id;
175 ih264e_populate_sps(ps_codec, ps_sps);
176
177 /* populate pps header */
178 ps_pps = ps_codec->ps_pps_base + ps_codec->i4_pps_id;
179 ih264e_populate_pps(ps_codec, ps_pps);
180
181 ps_entropy->i4_error_code = IH264E_SUCCESS;
182
183 /* generate sps */
Doney Alex983e1ae2016-03-30 17:31:26 +0530184 ps_entropy->i4_error_code |= ih264e_generate_sps(ps_bitstrm, ps_sps,
185 &ps_codec->s_cfg.s_vui);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530186
187 /* generate pps */
188 ps_entropy->i4_error_code |= ih264e_generate_pps(ps_bitstrm, ps_pps, ps_sps);
189
190 /* queue output buffer */
191 ps_out_buf->s_bits_buf.u4_bytes = ps_bitstrm->u4_strm_buf_offset;
192
193 return ps_entropy->i4_error_code;
194}
195
196/**
197*******************************************************************************
198*
199* @brief initialize entropy context.
200*
201* @par Description:
202* Before invoking the call to perform to entropy coding the entropy context
203* associated with the job needs to be initialized. This involves the start
204* mb address, end mb address, slice index and the pointer to location at
205* which the mb residue info and mb header info are packed.
206*
207* @param[in] ps_proc
208* Pointer to the current process context
209*
210* @returns error status
211*
212* @remarks none
213*
214*******************************************************************************
215*/
216IH264E_ERROR_T ih264e_init_entropy_ctxt(process_ctxt_t *ps_proc)
217{
218 /* codec context */
219 codec_t *ps_codec = ps_proc->ps_codec;
220
221 /* entropy ctxt */
222 entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
223
224 /* start address */
225 ps_entropy->i4_mb_start_add = ps_entropy->i4_mb_y * ps_entropy->i4_wd_mbs + ps_entropy->i4_mb_x;
226
227 /* end address */
228 ps_entropy->i4_mb_end_add = ps_entropy->i4_mb_start_add + ps_entropy->i4_mb_cnt;
229
230 /* slice index */
231 ps_entropy->i4_cur_slice_idx = ps_proc->pu1_slice_idx[ps_entropy->i4_mb_start_add];
232
233 /* sof */
234 /* @ start of frame or start of a new slice, set sof flag */
235 if (ps_entropy->i4_mb_start_add == 0)
236 {
237 ps_entropy->i4_sof = 1;
238 }
239
240 if (ps_entropy->i4_mb_x == 0)
241 {
242 /* packed mb coeff data */
243 ps_entropy->pv_mb_coeff_data = ((UWORD8 *)ps_entropy->pv_pic_mb_coeff_data) +
244 ps_entropy->i4_mb_y * ps_codec->u4_size_coeff_data;
245
246 /* packed mb header data */
247 ps_entropy->pv_mb_header_data = ((UWORD8 *)ps_entropy->pv_pic_mb_header_data) +
248 ps_entropy->i4_mb_y * ps_codec->u4_size_header_data;
249 }
250
251 return IH264E_SUCCESS;
252}
253
254/**
255*******************************************************************************
256*
257* @brief entry point for entropy coding
258*
259* @par Description
260* This function calls lower level functions to perform entropy coding for a
261* group (n rows) of mb's. After encoding 1 row of mb's, the function takes
262* back the control, updates the ctxt and calls lower level functions again.
263* This process is repeated till all the rows or group of mb's (which ever is
264* minimum) are coded
265*
266* @param[in] ps_proc
267* process context
268*
269* @returns error status
270*
271* @remarks
272*
273*******************************************************************************
274*/
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530275
276IH264E_ERROR_T ih264e_entropy(process_ctxt_t *ps_proc)
277{
278 /* codec context */
279 codec_t *ps_codec = ps_proc->ps_codec;
280
281 /* entropy context */
282 entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
283
Harinarayanan K K134291e2015-06-18 16:03:38 +0530284 /* cabac context */
285 cabac_ctxt_t *ps_cabac_ctxt = ps_entropy->ps_cabac;
286
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530287 /* sps */
288 sps_t *ps_sps = ps_entropy->ps_sps_base + (ps_entropy->u4_sps_id % MAX_SPS_CNT);
289
290 /* pps */
291 pps_t *ps_pps = ps_entropy->ps_pps_base + (ps_entropy->u4_pps_id % MAX_PPS_CNT);
292
293 /* slice header */
294 slice_header_t *ps_slice_hdr = ps_entropy->ps_slice_hdr_base + (ps_entropy->i4_cur_slice_idx % MAX_SLICE_HDR_CNT);
295
296 /* slice type */
297 WORD32 i4_slice_type = ps_proc->i4_slice_type;
298
299 /* Bitstream structure */
300 bitstrm_t *ps_bitstrm = ps_entropy->ps_bitstrm;
301
302 /* output buff */
303 out_buf_t s_out_buf;
304
305 /* proc map */
306 UWORD8 *pu1_proc_map;
307
308 /* entropy map */
309 UWORD8 *pu1_entropy_map_curr;
310
311 /* proc base idx */
Harinarayanan K K6cb67722015-06-19 14:44:42 +0530312 WORD32 ctxt_sel = ps_proc->i4_encode_api_call_cnt % MAX_CTXT_SETS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530313
314 /* temp var */
315 WORD32 i4_wd_mbs, i4_ht_mbs;
316 UWORD32 u4_mb_cnt, u4_mb_idx, u4_mb_end_idx;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530317 WORD32 bitstream_start_offset, bitstream_end_offset;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530318 /********************************************************************/
319 /* BEGIN INIT */
320 /********************************************************************/
321
322 /* entropy encode start address */
323 u4_mb_idx = ps_entropy->i4_mb_start_add;
324
325 /* entropy encode end address */
326 u4_mb_end_idx = ps_entropy->i4_mb_end_add;
327
328 /* width in mbs */
329 i4_wd_mbs = ps_entropy->i4_wd_mbs;
330
331 /* height in mbs */
332 i4_ht_mbs = ps_entropy->i4_ht_mbs;
333
334 /* total mb cnt */
335 u4_mb_cnt = i4_wd_mbs * i4_ht_mbs;
336
337 /* proc map */
338 pu1_proc_map = ps_proc->pu1_proc_map + ps_entropy->i4_mb_y * i4_wd_mbs;
339
340 /* entropy map */
341 pu1_entropy_map_curr = ps_entropy->pu1_entropy_map + ps_entropy->i4_mb_y * i4_wd_mbs;
342
343 /********************************************************************/
344 /* @ start of frame / slice, */
345 /* initialize the output buffer, */
346 /* initialize the bit stream buffer, */
347 /* check if sps and pps headers have to be generated, */
348 /* populate and generate slice header */
349 /********************************************************************/
350 if (ps_entropy->i4_sof)
351 {
352 /********************************************************************/
353 /* initialize the output buffer */
354 /********************************************************************/
355 s_out_buf = ps_codec->as_out_buf[ctxt_sel];
356
357 /* is last frame to encode */
358 s_out_buf.u4_is_last = ps_entropy->u4_is_last;
359
360 /* frame idx */
361 s_out_buf.u4_timestamp_high = ps_entropy->u4_timestamp_high;
362 s_out_buf.u4_timestamp_low = ps_entropy->u4_timestamp_low;
363
364 /********************************************************************/
365 /* initialize the bit stream buffer */
366 /********************************************************************/
367 ih264e_bitstrm_init(ps_bitstrm, s_out_buf.s_bits_buf.pv_buf, s_out_buf.s_bits_buf.u4_bufsize);
368
369 /********************************************************************/
370 /* BEGIN HEADER GENERATION */
371 /********************************************************************/
372 if (1 == ps_entropy->i4_gen_header)
373 {
374 /* generate sps */
Doney Alex983e1ae2016-03-30 17:31:26 +0530375 ps_entropy->i4_error_code |= ih264e_generate_sps(ps_bitstrm, ps_sps,
376 &ps_codec->s_cfg.s_vui);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530377 /* generate pps */
378 ps_entropy->i4_error_code |= ih264e_generate_pps(ps_bitstrm, ps_pps, ps_sps);
379
380 /* reset i4_gen_header */
381 ps_entropy->i4_gen_header = 0;
382 }
383
384 /* populate slice header */
385 ih264e_populate_slice_header(ps_proc, ps_slice_hdr, ps_pps, ps_sps);
386
387 /* generate slice header */
388 ps_entropy->i4_error_code |= ih264e_generate_slice_header(ps_bitstrm, ps_slice_hdr,
389 ps_pps, ps_sps);
390
391 /* once start of frame / slice is done, you can reset it */
392 /* it is the responsibility of the caller to set this flag */
393 ps_entropy->i4_sof = 0;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530394
395 if (CABAC == ps_entropy->u1_entropy_coding_mode_flag)
396 {
397 BITSTREAM_BYTE_ALIGN(ps_bitstrm);
398 BITSTREAM_FLUSH(ps_bitstrm);
399 ih264e_init_cabac_ctxt(ps_entropy);
400 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530401 }
402
403 /* begin entropy coding for the mb set */
404 while (u4_mb_idx < u4_mb_end_idx)
405 {
406 /* init ptrs/indices */
407 if (ps_entropy->i4_mb_x == i4_wd_mbs)
408 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530409 ps_entropy->i4_mb_y++;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530410 ps_entropy->i4_mb_x = 0;
411
412 /* packed mb coeff data */
413 ps_entropy->pv_mb_coeff_data = ((UWORD8 *)ps_entropy->pv_pic_mb_coeff_data) +
414 ps_entropy->i4_mb_y * ps_codec->u4_size_coeff_data;
415
416 /* packed mb header data */
417 ps_entropy->pv_mb_header_data = ((UWORD8 *)ps_entropy->pv_pic_mb_header_data) +
418 ps_entropy->i4_mb_y * ps_codec->u4_size_header_data;
419
420 /* proc map */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530421 pu1_proc_map = ps_proc->pu1_proc_map + ps_entropy->i4_mb_y * i4_wd_mbs;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530422
423 /* entropy map */
424 pu1_entropy_map_curr = ps_entropy->pu1_entropy_map + ps_entropy->i4_mb_y * i4_wd_mbs;
425 }
426
427 DEBUG("\nmb indices x, y %d, %d", ps_entropy->i4_mb_x, ps_entropy->i4_mb_y);
428 ENTROPY_TRACE("mb index x %d", ps_entropy->i4_mb_x);
429 ENTROPY_TRACE("mb index y %d", ps_entropy->i4_mb_y);
430
431 /* wait until the curr mb is core coded */
432 /* The wait for curr mb to be core coded is essential when entropy is launched
433 * as a separate job
434 */
435 while (1)
436 {
437 volatile UWORD8 *pu1_buf1;
438 WORD32 idx = ps_entropy->i4_mb_x;
439
Harinarayanan K K134291e2015-06-18 16:03:38 +0530440 pu1_buf1 = pu1_proc_map + idx;
441 if (*pu1_buf1)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530442 break;
443 ithread_yield();
444 }
445
Harinarayanan K K134291e2015-06-18 16:03:38 +0530446
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530447 /* write mb layer */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530448 ps_entropy->i4_error_code |= ps_codec->pf_write_mb_syntax_layer[ps_entropy->u1_entropy_coding_mode_flag][i4_slice_type](ps_entropy);
449 /* Starting bitstream offset for header in bits */
450 bitstream_start_offset = GET_NUM_BITS(ps_bitstrm);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530451
452 /* set entropy map */
453 pu1_entropy_map_curr[ps_entropy->i4_mb_x] = 1;
454
Harinarayanan K K134291e2015-06-18 16:03:38 +0530455 u4_mb_idx++;
456 ps_entropy->i4_mb_x++;
457 /* check for eof */
458 if (CABAC == ps_entropy->u1_entropy_coding_mode_flag)
459 {
460 if (ps_entropy->i4_mb_x < i4_wd_mbs)
461 {
462 ih264e_cabac_encode_terminate(ps_cabac_ctxt, 0);
463 }
464 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530465
466 if (ps_entropy->i4_mb_x == i4_wd_mbs)
467 {
468 /* if slices are enabled */
469 if (ps_codec->s_cfg.e_slice_mode == IVE_SLICE_MODE_BLOCKS)
470 {
471 /* current slice index */
472 WORD32 i4_curr_slice_idx = ps_entropy->i4_cur_slice_idx;
473
474 /* slice map */
475 UWORD8 *pu1_slice_idx = ps_entropy->pu1_slice_idx;
476
477 /* No need to open a slice at end of frame. The current slice can be closed at the time
478 * of signaling eof flag.
479 */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530480 if ((u4_mb_idx != u4_mb_cnt) && (i4_curr_slice_idx
481 != pu1_slice_idx[u4_mb_idx]))
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530482 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530483 if (CAVLC == ps_entropy->u1_entropy_coding_mode_flag)
484 { /* mb skip run */
485 if ((i4_slice_type != ISLICE)
486 && *ps_entropy->pi4_mb_skip_run)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530487 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530488 if (*ps_entropy->pi4_mb_skip_run)
489 {
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530490 PUT_BITS_UEV(ps_bitstrm, *ps_entropy->pi4_mb_skip_run, ps_entropy->i4_error_code, "mb skip run");
Harinarayanan K K134291e2015-06-18 16:03:38 +0530491 *ps_entropy->pi4_mb_skip_run = 0;
492 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530493 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530494 /* put rbsp trailing bits for the previous slice */
495 ps_entropy->i4_error_code |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530496 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530497 else
498 {
499 ih264e_cabac_encode_terminate(ps_cabac_ctxt, 1);
500 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530501
502 /* update slice header pointer */
503 i4_curr_slice_idx = pu1_slice_idx[u4_mb_idx];
504 ps_entropy->i4_cur_slice_idx = i4_curr_slice_idx;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530505 ps_slice_hdr = ps_entropy->ps_slice_hdr_base+ (i4_curr_slice_idx % MAX_SLICE_HDR_CNT);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530506
507 /* populate slice header */
508 ps_entropy->i4_mb_start_add = u4_mb_idx;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530509 ih264e_populate_slice_header(ps_proc, ps_slice_hdr, ps_pps,
510 ps_sps);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530511
512 /* generate slice header */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530513 ps_entropy->i4_error_code |= ih264e_generate_slice_header(
514 ps_bitstrm, ps_slice_hdr, ps_pps, ps_sps);
515 if (CABAC == ps_entropy->u1_entropy_coding_mode_flag)
516 {
517 BITSTREAM_BYTE_ALIGN(ps_bitstrm);
518 BITSTREAM_FLUSH(ps_bitstrm);
519 ih264e_init_cabac_ctxt(ps_entropy);
520 }
521 }
522 else
523 {
524 if (CABAC == ps_entropy->u1_entropy_coding_mode_flag
525 && u4_mb_idx != u4_mb_cnt)
526 {
527 ih264e_cabac_encode_terminate(ps_cabac_ctxt, 0);
528 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530529 }
530 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530531 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530532
533 /* Ending bitstream offset for header in bits */
534 bitstream_end_offset = GET_NUM_BITS(ps_bitstrm);
535 ps_entropy->u4_header_bits[i4_slice_type == PSLICE] +=
536 bitstream_end_offset - bitstream_start_offset;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530537 }
538
539 /* check for eof */
540 if (u4_mb_idx == u4_mb_cnt)
541 {
542 /* set end of frame flag */
543 ps_entropy->i4_eof = 1;
544 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530545 else
546 {
547 if (CABAC == ps_entropy->u1_entropy_coding_mode_flag
548 && ps_codec->s_cfg.e_slice_mode
549 != IVE_SLICE_MODE_BLOCKS)
550 {
551 ih264e_cabac_encode_terminate(ps_cabac_ctxt, 0);
552 }
553 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530554
555 if (ps_entropy->i4_eof)
556 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530557 if (CAVLC == ps_entropy->u1_entropy_coding_mode_flag)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530558 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530559 /* mb skip run */
560 if ((i4_slice_type != ISLICE) && *ps_entropy->pi4_mb_skip_run)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530561 {
Harinarayanan K K134291e2015-06-18 16:03:38 +0530562 if (*ps_entropy->pi4_mb_skip_run)
563 {
564 PUT_BITS_UEV(ps_bitstrm, *ps_entropy->pi4_mb_skip_run,
565 ps_entropy->i4_error_code, "mb skip run");
566 *ps_entropy->pi4_mb_skip_run = 0;
567 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530568 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530569 /* put rbsp trailing bits */
570 ps_entropy->i4_error_code |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
571 }
572 else
573 {
574 ih264e_cabac_encode_terminate(ps_cabac_ctxt, 1);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530575 }
576
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530577 /* update current frame stats to rc library */
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530578 {
579 /* number of bytes to stuff */
580 WORD32 i4_stuff_bytes;
581
582 /* update */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530583 i4_stuff_bytes = ih264e_update_rc_post_enc(
584 ps_codec, ctxt_sel,
585 (ps_proc->ps_codec->i4_poc == 0));
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530586
587 /* cbr rc - house keeping */
588 if (ps_codec->s_rate_control.post_encode_skip[ctxt_sel])
589 {
590 ps_entropy->ps_bitstrm->u4_strm_buf_offset = 0;
591 }
592 else if (i4_stuff_bytes)
593 {
594 /* add filler nal units */
595 ps_entropy->i4_error_code |= ih264e_add_filler_nal_unit(ps_bitstrm, i4_stuff_bytes);
596 }
597 }
598
Harinarayanan K K134291e2015-06-18 16:03:38 +0530599 /*
600 *Frame number is to be incremented only if the current frame is a
601 * reference frame. After each successful frame encode, we increment
602 * frame number by 1
603 */
604 if (!ps_codec->s_rate_control.post_encode_skip[ctxt_sel]
605 && ps_codec->u4_is_curr_frm_ref)
606 {
607 ps_codec->i4_frame_num++;
608 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530609 /********************************************************************/
610 /* signal the output */
611 /********************************************************************/
Harinarayanan K K134291e2015-06-18 16:03:38 +0530612 ps_codec->as_out_buf[ctxt_sel].s_bits_buf.u4_bytes =
613 ps_entropy->ps_bitstrm->u4_strm_buf_offset;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530614
615 DEBUG("entropy status %x", ps_entropy->i4_error_code);
616 }
617
Ram Mohan5df81832019-05-10 19:55:26 +0530618 /* Dont execute any further instructions until store synchronization took place */
619 DATA_SYNC();
620
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530621 /* allow threads to dequeue entropy jobs */
622 ps_codec->au4_entropy_thread_active[ctxt_sel] = 0;
623
624 return ps_entropy->i4_error_code;
625}
626
627/**
628*******************************************************************************
629*
630* @brief Packs header information of a mb in to a buffer
631*
632* @par Description:
633* After the deciding the mode info of a macroblock, the syntax elements
634* associated with the mb are packed and stored. The entropy thread unpacks
635* this buffer and generates the end bit stream.
636*
637* @param[in] ps_proc
638* Pointer to the current process context
639*
640* @returns error status
641*
642* @remarks none
643*
644*******************************************************************************
645*/
646IH264E_ERROR_T ih264e_pack_header_data(process_ctxt_t *ps_proc)
647{
648 /* curr mb type */
649 UWORD32 u4_mb_type = ps_proc->u4_mb_type;
650
651 /* pack mb syntax layer of curr mb (used for entropy coding) */
652 if (u4_mb_type == I4x4)
653 {
654 /* pointer to mb header storage space */
655 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530656 mb_hdr_i4x4_t *ps_mb_hdr = (mb_hdr_i4x4_t *)ps_proc->pv_mb_header_data;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530657
658 /* temp var */
659 WORD32 i4, byte;
660
661 /* mb type plus mode */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530662 ps_mb_hdr->common.u1_mb_type_mode = (ps_proc->u1_c_i8_mode << 6) + u4_mb_type;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530663
664 /* cbp */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530665 ps_mb_hdr->common.u1_cbp = ps_proc->u4_cbp;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530666
667 /* mb qp delta */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530668 ps_mb_hdr->common.u1_mb_qp_delta = ps_proc->u4_mb_qp - ps_proc->u4_mb_qp_prev;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530669
670 /* sub mb modes */
671 for (i4 = 0; i4 < 16; i4 ++)
672 {
673 byte = 0;
674
675 if (ps_proc->au1_predicted_intra_luma_mb_4x4_modes[i4] ==
676 ps_proc->au1_intra_luma_mb_4x4_modes[i4])
677 {
678 byte |= 1;
679 }
680 else
681 {
682
683 if (ps_proc->au1_intra_luma_mb_4x4_modes[i4] <
684 ps_proc->au1_predicted_intra_luma_mb_4x4_modes[i4])
685 {
686 byte |= (ps_proc->au1_intra_luma_mb_4x4_modes[i4] << 1);
687 }
688 else
689 {
690 byte |= (ps_proc->au1_intra_luma_mb_4x4_modes[i4] - 1) << 1;
691 }
692 }
693
694 i4++;
695
696 if (ps_proc->au1_predicted_intra_luma_mb_4x4_modes[i4] ==
697 ps_proc->au1_intra_luma_mb_4x4_modes[i4])
698 {
699 byte |= 16;
700 }
701 else
702 {
703
704 if (ps_proc->au1_intra_luma_mb_4x4_modes[i4] <
705 ps_proc->au1_predicted_intra_luma_mb_4x4_modes[i4])
706 {
707 byte |= (ps_proc->au1_intra_luma_mb_4x4_modes[i4] << 5);
708 }
709 else
710 {
711 byte |= (ps_proc->au1_intra_luma_mb_4x4_modes[i4] - 1) << 5;
712 }
713 }
714
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530715 ps_mb_hdr->au1_sub_blk_modes[i4 >> 1] = byte;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530716 }
717
718 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530719 pu1_ptr += sizeof(mb_hdr_i4x4_t);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530720 ps_proc->pv_mb_header_data = pu1_ptr;
721 }
722 else if (u4_mb_type == I16x16)
723 {
724 /* pointer to mb header storage space */
725 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530726 mb_hdr_i16x16_t *ps_mb_hdr = (mb_hdr_i16x16_t *)ps_proc->pv_mb_header_data;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530727
728 /* mb type plus mode */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530729 ps_mb_hdr->common.u1_mb_type_mode = (ps_proc->u1_c_i8_mode << 6) + (ps_proc->u1_l_i16_mode << 4) + u4_mb_type;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530730
731 /* cbp */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530732 ps_mb_hdr->common.u1_cbp = ps_proc->u4_cbp;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530733
734 /* mb qp delta */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530735 ps_mb_hdr->common.u1_mb_qp_delta = ps_proc->u4_mb_qp - ps_proc->u4_mb_qp_prev;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530736
737 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530738 pu1_ptr += sizeof(mb_hdr_i16x16_t);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530739 ps_proc->pv_mb_header_data = pu1_ptr;
740 }
741 else if (u4_mb_type == P16x16)
742 {
743 /* pointer to mb header storage space */
744 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530745 mb_hdr_p16x16_t *ps_mb_hdr = (mb_hdr_p16x16_t *)ps_proc->pv_mb_header_data;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530746
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530747 /* mb type */
748 ps_mb_hdr->common.u1_mb_type_mode = u4_mb_type;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530749
750 /* cbp */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530751 ps_mb_hdr->common.u1_cbp = ps_proc->u4_cbp;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530752
753 /* mb qp delta */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530754 ps_mb_hdr->common.u1_mb_qp_delta = ps_proc->u4_mb_qp - ps_proc->u4_mb_qp_prev;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530755
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530756 ps_mb_hdr->ai2_mv[0] = ps_proc->ps_pu->s_me_info[0].s_mv.i2_mvx - ps_proc->ps_pred_mv[0].s_mv.i2_mvx;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530757
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530758 ps_mb_hdr->ai2_mv[1] = ps_proc->ps_pu->s_me_info[0].s_mv.i2_mvy - ps_proc->ps_pred_mv[0].s_mv.i2_mvy;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530759
760 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530761 pu1_ptr += sizeof(mb_hdr_p16x16_t);
762 ps_proc->pv_mb_header_data = pu1_ptr;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530763 }
764 else if (u4_mb_type == PSKIP)
765 {
766 /* pointer to mb header storage space */
767 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530768 mb_hdr_pskip_t *ps_mb_hdr = (mb_hdr_pskip_t *)ps_proc->pv_mb_header_data;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530769
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530770 /* mb type */
771 ps_mb_hdr->common.u1_mb_type_mode = u4_mb_type;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530772
773 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530774 pu1_ptr += sizeof(mb_hdr_pskip_t);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530775 ps_proc->pv_mb_header_data = pu1_ptr;
776 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530777 else if(u4_mb_type == B16x16)
778 {
779
780 /* pointer to mb header storage space */
781 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530782 mb_hdr_b16x16_t *ps_mb_hdr = (mb_hdr_b16x16_t *)ps_proc->pv_mb_header_data;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530783
784 UWORD32 u4_pred_mode = ps_proc->ps_pu->b2_pred_mode;
785
786 /* mb type plus mode */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530787 ps_mb_hdr->common.u1_mb_type_mode = (u4_pred_mode << 4) + u4_mb_type;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530788
789 /* cbp */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530790 ps_mb_hdr->common.u1_cbp = ps_proc->u4_cbp;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530791
792 /* mb qp delta */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530793 ps_mb_hdr->common.u1_mb_qp_delta = ps_proc->u4_mb_qp - ps_proc->u4_mb_qp_prev;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530794
795 /* l0 & l1 me data */
Harinarayanan K K134291e2015-06-18 16:03:38 +0530796 if (u4_pred_mode != PRED_L1)
797 {
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530798 ps_mb_hdr->ai2_mv[0][0] = ps_proc->ps_pu->s_me_info[0].s_mv.i2_mvx
Harinarayanan K K134291e2015-06-18 16:03:38 +0530799 - ps_proc->ps_pred_mv[0].s_mv.i2_mvx;
800
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530801 ps_mb_hdr->ai2_mv[0][1] = ps_proc->ps_pu->s_me_info[0].s_mv.i2_mvy
Harinarayanan K K134291e2015-06-18 16:03:38 +0530802 - ps_proc->ps_pred_mv[0].s_mv.i2_mvy;
803 }
804 if (u4_pred_mode != PRED_L0)
805 {
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530806 ps_mb_hdr->ai2_mv[1][0] = ps_proc->ps_pu->s_me_info[1].s_mv.i2_mvx
Harinarayanan K K134291e2015-06-18 16:03:38 +0530807 - ps_proc->ps_pred_mv[1].s_mv.i2_mvx;
808
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530809 ps_mb_hdr->ai2_mv[1][1] = ps_proc->ps_pu->s_me_info[1].s_mv.i2_mvy
Harinarayanan K K134291e2015-06-18 16:03:38 +0530810 - ps_proc->ps_pred_mv[1].s_mv.i2_mvy;
811 }
812
813 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530814 pu1_ptr += sizeof(mb_hdr_b16x16_t);
815 ps_proc->pv_mb_header_data = pu1_ptr;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530816
817 }
818 else if(u4_mb_type == BDIRECT)
819 {
820 /* pointer to mb header storage space */
821 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530822 mb_hdr_bdirect_t *ps_mb_hdr = (mb_hdr_bdirect_t *)ps_proc->pv_mb_header_data;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530823
824 /* mb type plus mode */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530825 ps_mb_hdr->common.u1_mb_type_mode = u4_mb_type;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530826
827 /* cbp */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530828 ps_mb_hdr->common.u1_cbp = ps_proc->u4_cbp;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530829
830 /* mb qp delta */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530831 ps_mb_hdr->common.u1_mb_qp_delta = ps_proc->u4_mb_qp - ps_proc->u4_mb_qp_prev;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530832
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530833 /* end of mb layer */
834 pu1_ptr += sizeof(mb_hdr_bdirect_t);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530835 ps_proc->pv_mb_header_data = pu1_ptr;
836
837 }
838 else if(u4_mb_type == BSKIP)
839 {
840 UWORD32 u4_pred_mode = ps_proc->ps_pu->b2_pred_mode;
841
842 /* pointer to mb header storage space */
843 UWORD8 *pu1_ptr = ps_proc->pv_mb_header_data;
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530844 mb_hdr_bskip_t *ps_mb_hdr = (mb_hdr_bskip_t *)ps_proc->pv_mb_header_data;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530845
846 /* mb type plus mode */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530847 ps_mb_hdr->common.u1_mb_type_mode = (u4_pred_mode << 4) + u4_mb_type;
Harinarayanan K K134291e2015-06-18 16:03:38 +0530848
849 /* end of mb layer */
Harish Mahendrakarc7d9c912016-11-23 13:30:39 +0530850 pu1_ptr += sizeof(mb_hdr_bskip_t);
Harinarayanan K K134291e2015-06-18 16:03:38 +0530851 ps_proc->pv_mb_header_data = pu1_ptr;
852 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530853
854 return IH264E_SUCCESS;
855}
856
857/**
858*******************************************************************************
859*
860* @brief update process context after encoding an mb. This involves preserving
861* the current mb information for later use, initialize the proc ctxt elements to
862* encode next mb.
863*
864* @par Description:
865* This function performs house keeping tasks after encoding an mb.
866* After encoding an mb, various elements of the process context needs to be
867* updated to encode the next mb. For instance, the source, recon and reference
868* pointers, mb indices have to be adjusted to the next mb. The slice index of
869* the current mb needs to be updated. If mb qp modulation is enabled, then if
870* the qp changes the quant param structure needs to be updated. Also to encoding
871* the next mb, the current mb info is used as part of mode prediction or mv
872* prediction. Hence the current mb info has to preserved at top/top left/left
873* locations.
874*
875* @param[in] ps_proc
876* Pointer to the current process context
877*
878* @returns none
879*
880* @remarks none
881*
882*******************************************************************************
883*/
884WORD32 ih264e_update_proc_ctxt(process_ctxt_t *ps_proc)
885{
886 /* error status */
887 WORD32 error_status = IH264_SUCCESS;
888
889 /* codec context */
890 codec_t *ps_codec = ps_proc->ps_codec;
891
892 /* curr mb indices */
893 WORD32 i4_mb_x = ps_proc->i4_mb_x;
894 WORD32 i4_mb_y = ps_proc->i4_mb_y;
895
896 /* mb syntax elements of neighbors */
897 mb_info_t *ps_left_syn = &ps_proc->s_left_mb_syntax_ele;
898 mb_info_t *ps_top_syn = ps_proc->ps_top_row_mb_syntax_ele + i4_mb_x;
899 mb_info_t *ps_top_left_syn = &ps_proc->s_top_left_mb_syntax_ele;
900
901 /* curr mb type */
902 UWORD32 u4_mb_type = ps_proc->u4_mb_type;
903
904 /* curr mb type */
905 UWORD32 u4_is_intra = ps_proc->u4_is_intra;
906
907 /* width in mbs */
908 WORD32 i4_wd_mbs = ps_proc->i4_wd_mbs;
909
910 /*height in mbs*/
911 WORD32 i4_ht_mbs = ps_proc->i4_ht_mbs;
912
913 /* proc map */
914 UWORD8 *pu1_proc_map = ps_proc->pu1_proc_map + (i4_mb_y * i4_wd_mbs);
915
916 /* deblk context */
917 deblk_ctxt_t *ps_deblk = &ps_proc->s_deblk_ctxt;
918
919 /* deblk bs context */
920 bs_ctxt_t *ps_bs = &(ps_deblk->s_bs_ctxt);
921
922 /* top row motion vector info */
923 enc_pu_t *ps_top_row_pu = ps_proc->ps_top_row_pu + i4_mb_x;
924
925 /* top left mb motion vector */
926 enc_pu_t *ps_top_left_mb_pu = &ps_proc->s_top_left_mb_pu;
927
928 /* left mb motion vector */
929 enc_pu_t *ps_left_mb_pu = &ps_proc->s_left_mb_pu;
930
931 /* sub mb modes */
932 UWORD8 *pu1_top_mb_intra_modes = ps_proc->pu1_top_mb_intra_modes + (i4_mb_x << 4);
933
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530934 /*************************************************************/
935 /* During MV prediction, when top right mb is not available, */
936 /* top left mb info. is used for prediction. Hence the curr */
937 /* top, which will be top left for the next mb needs to be */
938 /* preserved before updating it with curr mb info. */
939 /*************************************************************/
940
941 /* mb type, mb class, csbp */
942 *ps_top_left_syn = *ps_top_syn;
943
Harinarayanan K K134291e2015-06-18 16:03:38 +0530944 if (ps_proc->i4_slice_type != ISLICE)
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530945 {
946 /*****************************************/
947 /* update top left with top info results */
948 /*****************************************/
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530949 /* mv */
950 *ps_top_left_mb_pu = *ps_top_row_pu;
951 }
952
953 /*************************************************/
954 /* update top and left with curr mb info results */
955 /*************************************************/
956
957 /* mb type */
958 ps_left_syn->u2_mb_type = ps_top_syn->u2_mb_type = u4_mb_type;
959
960 /* mb class */
961 ps_left_syn->u2_is_intra = ps_top_syn->u2_is_intra = u4_is_intra;
962
963 /* csbp */
964 ps_left_syn->u4_csbp = ps_top_syn->u4_csbp = ps_proc->u4_csbp;
965
966 /* distortion */
967 ps_left_syn->i4_mb_distortion = ps_top_syn->i4_mb_distortion = ps_proc->i4_mb_distortion;
968
969 if (u4_is_intra)
970 {
971 /* mb / sub mb modes */
972 if (I16x16 == u4_mb_type)
973 {
974 pu1_top_mb_intra_modes[0] = ps_proc->au1_left_mb_intra_modes[0] = ps_proc->u1_l_i16_mode;
975 }
976 else if (I4x4 == u4_mb_type)
977 {
978 ps_codec->pf_mem_cpy_mul8(ps_proc->au1_left_mb_intra_modes, ps_proc->au1_intra_luma_mb_4x4_modes, 16);
979 ps_codec->pf_mem_cpy_mul8(pu1_top_mb_intra_modes, ps_proc->au1_intra_luma_mb_4x4_modes, 16);
980 }
981 else if (I8x8 == u4_mb_type)
982 {
983 memcpy(ps_proc->au1_left_mb_intra_modes, ps_proc->au1_intra_luma_mb_8x8_modes, 4);
984 memcpy(pu1_top_mb_intra_modes, ps_proc->au1_intra_luma_mb_8x8_modes, 4);
985 }
986
Harinarayanan K K134291e2015-06-18 16:03:38 +0530987 if ((ps_proc->i4_slice_type == PSLICE) ||(ps_proc->i4_slice_type == BSLICE))
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530988 {
989 /* mv */
990 *ps_left_mb_pu = *ps_top_row_pu = *(ps_proc->ps_pu);
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530991 }
Harinarayanan K K134291e2015-06-18 16:03:38 +0530992
993 *ps_proc->pu4_mb_pu_cnt = 1;
Hamsalekha S8d3d3032015-03-13 21:24:58 +0530994 }
995 else
996 {
997 /* mv */
998 *ps_left_mb_pu = *ps_top_row_pu = *(ps_proc->ps_pu);
999 }
1000
1001 /*
1002 * Mark that the MB has been coded intra
1003 * So that future AIRs can skip it
1004 */
1005 ps_proc->pu1_is_intra_coded[i4_mb_x + (i4_mb_y * i4_wd_mbs)] = u4_is_intra;
1006
1007 /**************************************************/
1008 /* pack mb header info. for entropy coding */
1009 /**************************************************/
1010 ih264e_pack_header_data(ps_proc);
1011
1012 /* update previous mb qp */
1013 ps_proc->u4_mb_qp_prev = ps_proc->u4_mb_qp;
1014
1015 /* store qp */
1016 ps_proc->s_deblk_ctxt.s_bs_ctxt.pu1_pic_qp[(i4_mb_y * i4_wd_mbs) + i4_mb_x] = ps_proc->u4_mb_qp;
1017
1018 /*
1019 * We need to sync the cache to make sure that the nmv content of proc
1020 * is updated to cache properly
1021 */
1022 DATA_SYNC();
1023
1024 /* Just before finishing the row, enqueue the job in to entropy queue.
1025 * The master thread depending on its convenience shall dequeue it and
1026 * performs entropy.
1027 *
1028 * WARN !! Placing this block post proc map update can cause queuing of
1029 * entropy jobs in out of order.
1030 */
1031 if (i4_mb_x == i4_wd_mbs - 1)
1032 {
1033 /* job structures */
1034 job_t s_job;
1035
1036 /* job class */
1037 s_job.i4_cmd = CMD_ENTROPY;
1038
1039 /* number of mbs to be processed in the current job */
1040 s_job.i2_mb_cnt = ps_codec->s_cfg.i4_wd_mbs;
1041
1042 /* job start index x */
1043 s_job.i2_mb_x = 0;
1044
1045 /* job start index y */
1046 s_job.i2_mb_y = ps_proc->i4_mb_y;
1047
1048 /* proc base idx */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301049 s_job.i2_proc_base_idx = (ps_codec->i4_encode_api_call_cnt % MAX_CTXT_SETS) ? (MAX_PROCESS_CTXT / 2) : 0;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301050
1051 /* queue the job */
1052 error_status |= ih264_list_queue(ps_proc->pv_entropy_jobq, &s_job, 1);
1053
1054 if(ps_proc->i4_mb_y == (i4_ht_mbs - 1))
1055 ih264_list_terminate(ps_codec->pv_entropy_jobq);
1056 }
1057
1058 /* update proc map */
1059 pu1_proc_map[i4_mb_x] = 1;
1060
1061 /**************************************************/
1062 /* update proc ctxt elements for encoding next mb */
1063 /**************************************************/
1064 /* update indices */
1065 i4_mb_x ++;
1066 ps_proc->i4_mb_x = i4_mb_x;
1067
1068 if (ps_proc->i4_mb_x == i4_wd_mbs)
1069 {
1070 ps_proc->i4_mb_y++;
1071 ps_proc->i4_mb_x = 0;
1072 }
1073
1074 /* update slice index */
1075 ps_proc->i4_cur_slice_idx = ps_proc->pu1_slice_idx[ps_proc->i4_mb_y * i4_wd_mbs + ps_proc->i4_mb_x];
1076
1077 /* update buffers pointers */
1078 ps_proc->pu1_src_buf_luma += MB_SIZE;
1079 ps_proc->pu1_rec_buf_luma += MB_SIZE;
Harinarayanan K K134291e2015-06-18 16:03:38 +05301080 ps_proc->apu1_ref_buf_luma[0] += MB_SIZE;
1081 ps_proc->apu1_ref_buf_luma[1] += MB_SIZE;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301082
1083 /*
1084 * Note: Although chroma mb size is 8, as the chroma buffers are interleaved,
1085 * the stride per MB is MB_SIZE
1086 */
1087 ps_proc->pu1_src_buf_chroma += MB_SIZE;
1088 ps_proc->pu1_rec_buf_chroma += MB_SIZE;
Harinarayanan K K134291e2015-06-18 16:03:38 +05301089 ps_proc->apu1_ref_buf_chroma[0] += MB_SIZE;
1090 ps_proc->apu1_ref_buf_chroma[1] += MB_SIZE;
1091
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301092
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301093
1094 /* Reset cost, distortion params */
1095 ps_proc->i4_mb_cost = INT_MAX;
1096 ps_proc->i4_mb_distortion = SHRT_MAX;
1097
1098 ps_proc->ps_pu += *ps_proc->pu4_mb_pu_cnt;
1099
1100 ps_proc->pu4_mb_pu_cnt += 1;
1101
Harinarayanan K K134291e2015-06-18 16:03:38 +05301102 /* Update colocated pu */
1103 if (ps_proc->i4_slice_type == BSLICE)
1104 ps_proc->ps_colpu += *(ps_proc->aps_mv_buf[1]->pu4_mb_pu_cnt + (i4_mb_y * ps_proc->i4_wd_mbs) + i4_mb_x);
1105
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301106 /* deblk ctxts */
1107 if (ps_proc->u4_disable_deblock_level != 1)
1108 {
1109 /* indices */
1110 ps_bs->i4_mb_x = ps_proc->i4_mb_x;
1111 ps_bs->i4_mb_y = ps_proc->i4_mb_y;
1112
1113#ifndef N_MB_ENABLE /* For N MB processing update take place inside deblocking function */
1114 ps_deblk->i4_mb_x ++;
1115
1116 ps_deblk->pu1_cur_pic_luma += MB_SIZE;
1117 /*
1118 * Note: Although chroma mb size is 8, as the chroma buffers are interleaved,
1119 * the stride per MB is MB_SIZE
1120 */
1121 ps_deblk->pu1_cur_pic_chroma += MB_SIZE;
1122#endif
1123 }
1124
1125 return error_status;
1126}
1127
1128/**
1129*******************************************************************************
1130*
1131* @brief initialize process context.
1132*
1133* @par Description:
1134* Before dispatching the current job to process thread, the process context
1135* associated with the job is initialized. Usually every job aims to encode one
1136* row of mb's. Basing on the row indices provided by the job, the process
1137* context's buffer ptrs, slice indices and other elements that are necessary
1138* during core-coding are initialized.
1139*
1140* @param[in] ps_proc
1141* Pointer to the current process context
1142*
1143* @returns error status
1144*
1145* @remarks none
1146*
1147*******************************************************************************
1148*/
1149IH264E_ERROR_T ih264e_init_proc_ctxt(process_ctxt_t *ps_proc)
1150{
1151 /* codec context */
1152 codec_t *ps_codec = ps_proc->ps_codec;
1153
1154 /* nmb processing context*/
1155 n_mb_process_ctxt_t *ps_n_mb_ctxt = &ps_proc->s_n_mb_ctxt;
1156
1157 /* indices */
1158 WORD32 i4_mb_x, i4_mb_y;
1159
1160 /* strides */
1161 WORD32 i4_src_strd = ps_proc->i4_src_strd;
Martin Storsjo53c68782015-06-09 16:25:51 +03001162 WORD32 i4_src_chroma_strd = ps_proc->i4_src_chroma_strd;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301163 WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
1164
1165 /* quant params */
1166 quant_params_t *ps_qp_params = ps_proc->ps_qp_params[0];
1167
1168 /* deblk ctxt */
1169 deblk_ctxt_t *ps_deblk = &ps_proc->s_deblk_ctxt;
1170
1171 /* deblk bs context */
1172 bs_ctxt_t *ps_bs = &(ps_deblk->s_bs_ctxt);
1173
1174 /* Pointer to mv_buffer of current frame */
1175 mv_buf_t *ps_cur_mv_buf = ps_proc->ps_cur_mv_buf;
1176
1177 /* Pointers for color space conversion */
1178 UWORD8 *pu1_y_buf_base, *pu1_u_buf_base, *pu1_v_buf_base;
1179
1180 /* Pad the MB to support non standard sizes */
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301181 UWORD32 u4_pad_right_sz = ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301182 UWORD32 u4_pad_bottom_sz = ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht;
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301183 UWORD16 u2_num_rows = MB_SIZE;
1184 WORD32 convert_uv_only;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301185
1186 /********************************************************************/
1187 /* BEGIN INIT */
1188 /********************************************************************/
1189
1190 i4_mb_x = ps_proc->i4_mb_x;
1191 i4_mb_y = ps_proc->i4_mb_y;
1192
1193 /* Number of mbs processed in one loop of process function */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301194 ps_proc->i4_nmb_ntrpy = ps_proc->i4_wd_mbs;
1195 ps_proc->u4_nmb_me = ps_proc->i4_wd_mbs;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301196
Harinarayanan K K134291e2015-06-18 16:03:38 +05301197 /* init buffer pointers */
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301198 convert_uv_only = 1;
Martin Storsjof7682972015-06-16 21:30:36 +02001199 if (u4_pad_bottom_sz || u4_pad_right_sz ||
Martin Storsjo90a39042015-06-09 16:38:50 +03001200 ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_422ILE)
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301201 {
Martin Storsjo90a39042015-06-09 16:38:50 +03001202 if (ps_proc->i4_mb_y == ps_proc->i4_ht_mbs - 1)
1203 u2_num_rows = (UWORD16) MB_SIZE - u4_pad_bottom_sz;
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301204 ps_proc->pu1_src_buf_luma_base = ps_codec->pu1_y_csc_buf_base;
Martin Storsjo90a39042015-06-09 16:38:50 +03001205 i4_src_strd = ps_proc->i4_src_strd = ps_codec->s_cfg.u4_max_wd;
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301206 ps_proc->pu1_src_buf_luma = ps_proc->pu1_src_buf_luma_base + (i4_mb_x * MB_SIZE) + ps_codec->s_cfg.u4_max_wd * (i4_mb_y * MB_SIZE);
1207 convert_uv_only = 0;
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301208 }
1209 else
Martin Storsjo90a39042015-06-09 16:38:50 +03001210 {
1211 i4_src_strd = ps_proc->i4_src_strd = ps_proc->s_inp_buf.s_raw_buf.au4_strd[0];
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301212 ps_proc->pu1_src_buf_luma = ps_proc->pu1_src_buf_luma_base + (i4_mb_x * MB_SIZE) + i4_src_strd * (i4_mb_y * MB_SIZE);
Martin Storsjo90a39042015-06-09 16:38:50 +03001213 }
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301214
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301215
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301216 if (ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_422ILE ||
1217 ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420P ||
Martin Storsjof7682972015-06-16 21:30:36 +02001218 ps_proc->i4_mb_y == (ps_proc->i4_ht_mbs - 1) ||
1219 u4_pad_bottom_sz || u4_pad_right_sz)
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301220 {
1221 if ((ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420SP_UV) ||
1222 (ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420SP_VU))
1223 ps_proc->pu1_src_buf_chroma_base = ps_codec->pu1_uv_csc_buf_base;
1224
1225 ps_proc->pu1_src_buf_chroma = ps_proc->pu1_src_buf_chroma_base + (i4_mb_x * MB_SIZE) + ps_codec->s_cfg.u4_max_wd * (i4_mb_y * BLK8x8SIZE);
Martin Storsjo90a39042015-06-09 16:38:50 +03001226 i4_src_chroma_strd = ps_proc->i4_src_chroma_strd = ps_codec->s_cfg.u4_max_wd;
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301227 }
1228 else
1229 {
Martin Storsjo90a39042015-06-09 16:38:50 +03001230 i4_src_chroma_strd = ps_proc->i4_src_chroma_strd = ps_proc->s_inp_buf.s_raw_buf.au4_strd[1];
Martin Storsjo53c68782015-06-09 16:25:51 +03001231 ps_proc->pu1_src_buf_chroma = ps_proc->pu1_src_buf_chroma_base + (i4_mb_x * MB_SIZE) + i4_src_chroma_strd * (i4_mb_y * BLK8x8SIZE);
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301232 }
1233
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301234 ps_proc->pu1_rec_buf_luma = ps_proc->pu1_rec_buf_luma_base + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * MB_SIZE);
1235 ps_proc->pu1_rec_buf_chroma = ps_proc->pu1_rec_buf_chroma_base + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * BLK8x8SIZE);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301236
Harinarayanan K K134291e2015-06-18 16:03:38 +05301237 /* Tempral back and forward reference buffer */
1238 ps_proc->apu1_ref_buf_luma[0] = ps_proc->apu1_ref_buf_luma_base[0] + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * MB_SIZE);
1239 ps_proc->apu1_ref_buf_chroma[0] = ps_proc->apu1_ref_buf_chroma_base[0] + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * BLK8x8SIZE);
1240 ps_proc->apu1_ref_buf_luma[1] = ps_proc->apu1_ref_buf_luma_base[1] + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * MB_SIZE);
1241 ps_proc->apu1_ref_buf_chroma[1] = ps_proc->apu1_ref_buf_chroma_base[1] + (i4_mb_x * MB_SIZE) + i4_rec_strd * (i4_mb_y * BLK8x8SIZE);
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301242
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301243 /*
1244 * Do color space conversion
1245 * NOTE : We assume there that the number of MB's to process will not span multiple rows
1246 */
1247 switch (ps_codec->s_cfg.e_inp_color_fmt)
1248 {
1249 case IV_YUV_420SP_UV:
1250 case IV_YUV_420SP_VU:
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301251 /* In case of 420 semi-planar input, copy last few rows to intermediate
1252 buffer as chroma trans functions access one extra byte due to interleaved input.
1253 This data will be padded if required */
Martin Storsjof7682972015-06-16 21:30:36 +02001254 if (ps_proc->i4_mb_y == (ps_proc->i4_ht_mbs - 1) || u4_pad_bottom_sz || u4_pad_right_sz)
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301255 {
Martin Storsjof7682972015-06-16 21:30:36 +02001256 WORD32 num_rows = MB_SIZE;
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301257 UWORD8 *pu1_src;
1258 UWORD8 *pu1_dst;
1259 WORD32 i;
1260 pu1_src = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[0] + (i4_mb_x * MB_SIZE) +
1261 ps_proc->s_inp_buf.s_raw_buf.au4_strd[0] * (i4_mb_y * MB_SIZE);
1262
1263 pu1_dst = ps_proc->pu1_src_buf_luma;
1264
Martin Storsjof7682972015-06-16 21:30:36 +02001265 /* If padding is required, we always copy luma, if padding isn't required we never copy luma. */
1266 if (u4_pad_bottom_sz || u4_pad_right_sz) {
1267 if (ps_proc->i4_mb_y == (ps_proc->i4_ht_mbs - 1))
1268 num_rows = MB_SIZE - u4_pad_bottom_sz;
1269 for (i = 0; i < num_rows; i++)
1270 {
1271 memcpy(pu1_dst, pu1_src, ps_codec->s_cfg.u4_wd);
1272 pu1_src += ps_proc->s_inp_buf.s_raw_buf.au4_strd[0];
1273 pu1_dst += ps_proc->i4_src_strd;
1274 }
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301275 }
1276 pu1_src = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[1] + (i4_mb_x * BLK8x8SIZE) +
1277 ps_proc->s_inp_buf.s_raw_buf.au4_strd[1] * (i4_mb_y * BLK8x8SIZE);
1278 pu1_dst = ps_proc->pu1_src_buf_chroma;
1279
1280 /* Last MB row of chroma is copied unconditionally, since trans functions access an extra byte
1281 * due to interleaved input
1282 */
Martin Storsjof7682972015-06-16 21:30:36 +02001283 if (ps_proc->i4_mb_y == (ps_proc->i4_ht_mbs - 1))
1284 num_rows = (ps_codec->s_cfg.u4_disp_ht >> 1) - (ps_proc->i4_mb_y * BLK8x8SIZE);
1285 else
1286 num_rows = BLK8x8SIZE;
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301287 for (i = 0; i < num_rows; i++)
1288 {
1289 memcpy(pu1_dst, pu1_src, ps_codec->s_cfg.u4_wd);
1290 pu1_src += ps_proc->s_inp_buf.s_raw_buf.au4_strd[1];
Martin Storsjo53c68782015-06-09 16:25:51 +03001291 pu1_dst += ps_proc->i4_src_chroma_strd;
Harish Mahendrakar5cdb8882015-06-09 17:48:53 +05301292 }
1293
1294 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301295 break;
1296
1297 case IV_YUV_420P :
1298 pu1_y_buf_base = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[0] + (i4_mb_x * MB_SIZE) +
1299 ps_proc->s_inp_buf.s_raw_buf.au4_strd[0] * (i4_mb_y * MB_SIZE);
1300
1301 pu1_u_buf_base = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[1] + (i4_mb_x * BLK8x8SIZE) +
1302 ps_proc->s_inp_buf.s_raw_buf.au4_strd[1] * (i4_mb_y * BLK8x8SIZE);
1303
1304 pu1_v_buf_base = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[2] + (i4_mb_x * BLK8x8SIZE) +
1305 ps_proc->s_inp_buf.s_raw_buf.au4_strd[2] * (i4_mb_y * BLK8x8SIZE);
1306
1307 ps_codec->pf_ih264e_conv_420p_to_420sp(
1308 pu1_y_buf_base, pu1_u_buf_base, pu1_v_buf_base,
1309 ps_proc->pu1_src_buf_luma,
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301310 ps_proc->pu1_src_buf_chroma, u2_num_rows,
1311 ps_codec->s_cfg.u4_disp_wd,
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301312 ps_proc->s_inp_buf.s_raw_buf.au4_strd[0],
1313 ps_proc->s_inp_buf.s_raw_buf.au4_strd[1],
1314 ps_proc->s_inp_buf.s_raw_buf.au4_strd[2],
Martin Storsjo53c68782015-06-09 16:25:51 +03001315 ps_proc->i4_src_strd, ps_proc->i4_src_chroma_strd,
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301316 convert_uv_only);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301317 break;
1318
1319 case IV_YUV_422ILE :
1320 pu1_y_buf_base = (UWORD8 *)ps_proc->s_inp_buf.s_raw_buf.apv_bufs[0] + (i4_mb_x * MB_SIZE * 2)
1321 + ps_proc->s_inp_buf.s_raw_buf.au4_strd[0] * (i4_mb_y * MB_SIZE);
1322
1323 ps_codec->pf_ih264e_fmt_conv_422i_to_420sp(
1324 ps_proc->pu1_src_buf_luma,
1325 ps_proc->pu1_src_buf_chroma,
1326 ps_proc->pu1_src_buf_chroma + 1, pu1_y_buf_base,
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301327 ps_codec->s_cfg.u4_disp_wd, u2_num_rows,
Martin Storsjo53c68782015-06-09 16:25:51 +03001328 ps_proc->i4_src_strd, ps_proc->i4_src_chroma_strd,
1329 ps_proc->i4_src_chroma_strd,
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301330 ps_proc->s_inp_buf.s_raw_buf.au4_strd[0] >> 1);
1331 break;
1332
1333 default:
1334 break;
1335 }
1336
Martin Storsjof7682972015-06-16 21:30:36 +02001337 if (u4_pad_right_sz && (ps_proc->i4_mb_x == 0))
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301338 {
1339 UWORD32 u4_pad_wd, u4_pad_ht;
1340 u4_pad_wd = (UWORD32)(ps_proc->i4_src_strd - ps_codec->s_cfg.u4_disp_wd);
1341 u4_pad_wd = MIN(u4_pad_right_sz, u4_pad_wd);
1342 u4_pad_ht = MB_SIZE;
1343 if(ps_proc->i4_mb_y == ps_proc->i4_ht_mbs - 1)
1344 u4_pad_ht = MIN(MB_SIZE, (MB_SIZE - u4_pad_bottom_sz));
1345
1346 ih264_pad_right_luma(
1347 ps_proc->pu1_src_buf_luma + ps_codec->s_cfg.u4_disp_wd,
1348 ps_proc->i4_src_strd, u4_pad_ht, u4_pad_wd);
1349
1350 ih264_pad_right_chroma(
1351 ps_proc->pu1_src_buf_chroma + ps_codec->s_cfg.u4_disp_wd,
Martin Storsjo53c68782015-06-09 16:25:51 +03001352 ps_proc->i4_src_chroma_strd, u4_pad_ht / 2, u4_pad_wd);
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301353 }
1354
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301355 /* pad bottom edge */
1356 if (u4_pad_bottom_sz && (ps_proc->i4_mb_y == ps_proc->i4_ht_mbs - 1) && ps_proc->i4_mb_x == 0)
1357 {
1358 ih264_pad_bottom(ps_proc->pu1_src_buf_luma + (MB_SIZE - u4_pad_bottom_sz) * ps_proc->i4_src_strd,
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301359 ps_proc->i4_src_strd, ps_proc->i4_src_strd, u4_pad_bottom_sz);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301360
Martin Storsjo53c68782015-06-09 16:25:51 +03001361 ih264_pad_bottom(ps_proc->pu1_src_buf_chroma + (MB_SIZE - u4_pad_bottom_sz) * ps_proc->i4_src_chroma_strd / 2,
1362 ps_proc->i4_src_chroma_strd, ps_proc->i4_src_chroma_strd, (u4_pad_bottom_sz / 2));
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301363 }
1364
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301365
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301366 /* packed mb coeff data */
1367 ps_proc->pv_mb_coeff_data = ((UWORD8 *)ps_proc->pv_pic_mb_coeff_data) + i4_mb_y * ps_codec->u4_size_coeff_data;
1368
1369 /* packed mb header data */
1370 ps_proc->pv_mb_header_data = ((UWORD8 *)ps_proc->pv_pic_mb_header_data) + i4_mb_y * ps_codec->u4_size_header_data;
1371
1372 /* slice index */
1373 ps_proc->i4_cur_slice_idx = ps_proc->pu1_slice_idx[i4_mb_y * ps_proc->i4_wd_mbs + i4_mb_x];
1374
1375 /*********************************************************************/
1376 /* ih264e_init_quant_params() routine is called at the pic init level*/
1377 /* this would have initialized the qp. */
1378 /* TODO_LATER: currently it is assumed that quant params donot change*/
1379 /* across mb's. When they do calculate update ps_qp_params accordingly*/
1380 /*********************************************************************/
1381
1382 /* init mv buffer ptr */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301383 ps_proc->ps_pu = ps_cur_mv_buf->ps_pic_pu + (i4_mb_y * ps_proc->i4_wd_mbs *
1384 ((MB_SIZE * MB_SIZE) / (ENC_MIN_PU_SIZE * ENC_MIN_PU_SIZE)));
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301385
Harinarayanan K K134291e2015-06-18 16:03:38 +05301386 /* Init co-located mv buffer */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301387 ps_proc->ps_colpu = ps_proc->aps_mv_buf[1]->ps_pic_pu + (i4_mb_y * ps_proc->i4_wd_mbs *
1388 ((MB_SIZE * MB_SIZE) / (ENC_MIN_PU_SIZE * ENC_MIN_PU_SIZE)));
Harinarayanan K K134291e2015-06-18 16:03:38 +05301389
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301390 if (i4_mb_y == 0)
1391 {
1392 ps_proc->ps_top_row_pu_ME = ps_cur_mv_buf->ps_pic_pu;
1393 }
1394 else
1395 {
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301396 ps_proc->ps_top_row_pu_ME = ps_cur_mv_buf->ps_pic_pu + ((i4_mb_y - 1) * ps_proc->i4_wd_mbs *
1397 ((MB_SIZE * MB_SIZE) / (ENC_MIN_PU_SIZE * ENC_MIN_PU_SIZE)));
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301398 }
1399
1400 ps_proc->pu4_mb_pu_cnt = ps_cur_mv_buf->pu4_mb_pu_cnt + (i4_mb_y * ps_proc->i4_wd_mbs);
1401
1402 /* mb type */
1403 ps_proc->u4_mb_type = I16x16;
1404
1405 /* lambda */
1406 ps_proc->u4_lambda = gu1_qp0[ps_qp_params->u1_mb_qp];
1407
1408 /* mb distortion */
1409 ps_proc->i4_mb_distortion = SHRT_MAX;
1410
1411 if (i4_mb_x == 0)
1412 {
1413 ps_proc->s_left_mb_syntax_ele.i4_mb_distortion = 0;
1414
1415 ps_proc->s_top_left_mb_syntax_ele.i4_mb_distortion = 0;
1416
1417 ps_proc->s_top_left_mb_syntax_ME.i4_mb_distortion = 0;
1418
1419 if (i4_mb_y == 0)
1420 {
1421 memset(ps_proc->ps_top_row_mb_syntax_ele, 0, (ps_proc->i4_wd_mbs + 1)*sizeof(mb_info_t));
1422 }
1423 }
1424
1425 /* mb cost */
1426 ps_proc->i4_mb_cost = INT_MAX;
1427
1428 /**********************/
1429 /* init deblk context */
1430 /**********************/
1431 ps_deblk->i4_mb_x = ps_proc->i4_mb_x;
1432 /* deblk lags the current mb proc by 1 row */
1433 /* NOTE: Intra prediction has to happen with non deblocked samples used as reference */
1434 /* Hence to deblk MB 0 of row 0, you have wait till MB 0 of row 1 is encoded. */
1435 /* For simplicity, we chose to lag deblking by 1 Row wrt to proc */
1436 ps_deblk->i4_mb_y = ps_proc->i4_mb_y - 1;
1437
1438 /* buffer ptrs */
1439 ps_deblk->pu1_cur_pic_luma = ps_proc->pu1_rec_buf_luma_base + i4_rec_strd * (ps_deblk->i4_mb_y * MB_SIZE);
1440 ps_deblk->pu1_cur_pic_chroma = ps_proc->pu1_rec_buf_chroma_base + i4_rec_strd * (ps_deblk->i4_mb_y * BLK8x8SIZE);
1441
1442 /* init deblk bs context */
1443 /* mb indices */
1444 ps_bs->i4_mb_x = ps_proc->i4_mb_x;
1445 ps_bs->i4_mb_y = ps_proc->i4_mb_y;
1446
1447 /* init n_mb_process context */
1448 ps_n_mb_ctxt->i4_mb_x = 0;
1449 ps_n_mb_ctxt->i4_mb_y = ps_deblk->i4_mb_y;
1450 ps_n_mb_ctxt->i4_n_mbs = ps_proc->i4_nmb_ntrpy;
1451
1452 return IH264E_SUCCESS;
1453}
1454
1455/**
1456*******************************************************************************
1457*
1458* @brief This function performs luma & chroma padding
1459*
1460* @par Description:
1461*
1462* @param[in] ps_proc
1463* Process context corresponding to the job
1464*
1465* @param[in] pu1_curr_pic_luma
1466* Pointer to luma buffer
1467*
1468* @param[in] pu1_curr_pic_chroma
1469* Pointer to chroma buffer
1470*
1471* @param[in] i4_mb_x
1472* mb index x
1473*
1474* @param[in] i4_mb_y
1475* mb index y
1476*
1477* @param[in] i4_pad_ht
1478* number of rows to be padded
1479*
1480* @returns error status
1481*
1482* @remarks none
1483*
1484*******************************************************************************
1485*/
1486IH264E_ERROR_T ih264e_pad_recon_buffer(process_ctxt_t *ps_proc,
1487 UWORD8 *pu1_curr_pic_luma,
1488 UWORD8 *pu1_curr_pic_chroma,
1489 WORD32 i4_mb_x,
1490 WORD32 i4_mb_y,
1491 WORD32 i4_pad_ht)
1492{
1493 /* codec context */
1494 codec_t *ps_codec = ps_proc->ps_codec;
1495
1496 /* strides */
1497 WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
1498
1499 if (i4_mb_x == 0)
1500 {
1501 /* padding left luma */
1502 ps_codec->pf_pad_left_luma(pu1_curr_pic_luma, i4_rec_strd, i4_pad_ht, PAD_LEFT);
1503
1504 /* padding left chroma */
1505 ps_codec->pf_pad_left_chroma(pu1_curr_pic_chroma, i4_rec_strd, i4_pad_ht >> 1, PAD_LEFT);
1506 }
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301507 if (i4_mb_x == ps_proc->i4_wd_mbs - 1)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301508 {
1509 /* padding right luma */
1510 ps_codec->pf_pad_right_luma(pu1_curr_pic_luma + MB_SIZE, i4_rec_strd, i4_pad_ht, PAD_RIGHT);
1511
1512 /* padding right chroma */
1513 ps_codec->pf_pad_right_chroma(pu1_curr_pic_chroma + MB_SIZE, i4_rec_strd, i4_pad_ht >> 1, PAD_RIGHT);
1514
1515 if (i4_mb_y == ps_proc->i4_ht_mbs - 1)
1516 {
1517 UWORD8 *pu1_rec_luma = pu1_curr_pic_luma + MB_SIZE + PAD_RIGHT + ((i4_pad_ht - 1) * i4_rec_strd);
1518 UWORD8 *pu1_rec_chroma = pu1_curr_pic_chroma + MB_SIZE + PAD_RIGHT + (((i4_pad_ht >> 1) - 1) * i4_rec_strd);
1519
1520 /* padding bottom luma */
1521 ps_codec->pf_pad_bottom(pu1_rec_luma, i4_rec_strd, i4_rec_strd, PAD_BOT);
1522
1523 /* padding bottom chroma */
1524 ps_codec->pf_pad_bottom(pu1_rec_chroma, i4_rec_strd, i4_rec_strd, (PAD_BOT >> 1));
1525 }
1526 }
1527
1528 if (i4_mb_y == 0)
1529 {
1530 UWORD8 *pu1_rec_luma = pu1_curr_pic_luma;
1531 UWORD8 *pu1_rec_chroma = pu1_curr_pic_chroma;
1532 WORD32 wd = MB_SIZE;
1533
1534 if (i4_mb_x == 0)
1535 {
1536 pu1_rec_luma -= PAD_LEFT;
1537 pu1_rec_chroma -= PAD_LEFT;
1538
1539 wd += PAD_LEFT;
1540 }
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301541 if (i4_mb_x == ps_proc->i4_wd_mbs - 1)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301542 {
1543 wd += PAD_RIGHT;
1544 }
1545
1546 /* padding top luma */
1547 ps_codec->pf_pad_top(pu1_rec_luma, i4_rec_strd, wd, PAD_TOP);
1548
1549 /* padding top chroma */
1550 ps_codec->pf_pad_top(pu1_rec_chroma, i4_rec_strd, wd, (PAD_TOP >> 1));
1551 }
1552
1553 return IH264E_SUCCESS;
1554}
1555
1556
1557
1558
1559/**
1560*******************************************************************************
1561*
1562* @brief This function performs deblocking, padding and halfpel generation for
1563* 'n' MBs
1564*
1565* @par Description:
1566*
1567* @param[in] ps_proc
1568* Process context corresponding to the job
1569*
1570* @param[in] pu1_curr_pic_luma
1571* Current MB being processed(Luma)
1572*
1573* @param[in] pu1_curr_pic_chroma
1574* Current MB being processed(Chroma)
1575*
1576* @param[in] i4_mb_x
1577* Column value of current MB processed
1578*
1579* @param[in] i4_mb_y
1580* Curent row processed
1581*
1582* @returns error status
1583*
1584* @remarks none
1585*
1586*******************************************************************************
1587*/
1588IH264E_ERROR_T ih264e_dblk_pad_hpel_processing_n_mbs(process_ctxt_t *ps_proc,
1589 UWORD8 *pu1_curr_pic_luma,
1590 UWORD8 *pu1_curr_pic_chroma,
1591 WORD32 i4_mb_x,
1592 WORD32 i4_mb_y)
1593{
1594 /* codec context */
1595 codec_t *ps_codec = ps_proc->ps_codec;
1596
1597 /* n_mb processing context */
1598 n_mb_process_ctxt_t *ps_n_mb_ctxt = &ps_proc->s_n_mb_ctxt;
1599
1600 /* deblk context */
1601 deblk_ctxt_t *ps_deblk = &ps_proc->s_deblk_ctxt;
1602
1603 /* strides */
1604 WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
1605
1606 /* loop variables */
1607 WORD32 row, i, j, col;
1608
1609 /* Padding Width */
1610 UWORD32 u4_pad_wd;
1611
1612 /* deblk_map of the row being deblocked */
1613 UWORD8 *pu1_deblk_map = ps_proc->pu1_deblk_map + ps_deblk->i4_mb_y * ps_proc->i4_wd_mbs;
1614
1615 /* deblk_map_previous row */
1616 UWORD8 *pu1_deblk_map_prev_row = pu1_deblk_map - ps_proc->i4_wd_mbs;
1617
1618 WORD32 u4_pad_top = 0;
1619
1620 WORD32 u4_deblk_prev_row = 0;
1621
1622 /* Number of mbs to be processed */
1623 WORD32 i4_n_mbs = ps_n_mb_ctxt->i4_n_mbs;
1624
1625 /* Number of mbs actually processed
1626 * (at the end of a row, when remaining number of MBs are less than i4_n_mbs) */
1627 WORD32 i4_n_mb_process_count = 0;
1628
1629 UWORD8 *pu1_pad_bottom_src = NULL;
1630
1631 UWORD8 *pu1_pad_src_luma = NULL;
1632 UWORD8 *pu1_pad_src_chroma = NULL;
1633
1634 if (ps_proc->u4_disable_deblock_level == 1)
1635 {
1636 /* If left most MB is processed, then pad left */
1637 if (i4_mb_x == 0)
1638 {
1639 /* padding left luma */
1640 ps_codec->pf_pad_left_luma(pu1_curr_pic_luma, i4_rec_strd, MB_SIZE, PAD_LEFT);
1641
1642 /* padding left chroma */
1643 ps_codec->pf_pad_left_chroma(pu1_curr_pic_chroma, i4_rec_strd, MB_SIZE >> 1, PAD_LEFT);
1644 }
1645 /*last col*/
1646 if (i4_mb_x == (ps_proc->i4_wd_mbs - 1))
1647 {
1648 /* padding right luma */
1649 ps_codec->pf_pad_right_luma(pu1_curr_pic_luma + MB_SIZE, i4_rec_strd, MB_SIZE, PAD_RIGHT);
1650
1651 /* padding right chroma */
1652 ps_codec->pf_pad_right_chroma(pu1_curr_pic_chroma + MB_SIZE, i4_rec_strd, MB_SIZE >> 1, PAD_RIGHT);
1653 }
1654 }
1655
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301656 if ((i4_mb_y > 0) || (i4_mb_y == (ps_proc->i4_ht_mbs - 1)))
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301657 {
1658 /* if number of mb's to be processed are less than 'N', go back.
1659 * exception to the above clause is end of row */
1660 if ( ((i4_mb_x - (ps_n_mb_ctxt->i4_mb_x - 1)) < i4_n_mbs) && (i4_mb_x < (ps_proc->i4_wd_mbs - 1)) )
1661 {
1662 return IH264E_SUCCESS;
1663 }
1664 else
1665 {
1666 i4_n_mb_process_count = MIN(i4_mb_x - (ps_n_mb_ctxt->i4_mb_x - 1), i4_n_mbs);
1667
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301668 /* performing deblocking for required number of MBs */
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301669 if ((i4_mb_y > 0) && (ps_proc->u4_disable_deblock_level != 1))
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301670 {
Martin Storsjo1bc67422015-05-25 15:31:26 +03001671 u4_deblk_prev_row = 1;
1672
1673 /* checking whether the top rows are deblocked */
1674 for (col = 0; col < i4_n_mb_process_count; col++)
1675 {
1676 u4_deblk_prev_row &= pu1_deblk_map_prev_row[ps_deblk->i4_mb_x + col];
1677 }
1678
1679 /* checking whether the top right MB is deblocked */
1680 if ((ps_deblk->i4_mb_x + i4_n_mb_process_count) != ps_proc->i4_wd_mbs)
1681 {
1682 u4_deblk_prev_row &= pu1_deblk_map_prev_row[ps_deblk->i4_mb_x + i4_n_mb_process_count];
1683 }
1684
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301685 /* Top or Top right MBs not deblocked */
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301686 if ((u4_deblk_prev_row != 1) && (i4_mb_y > 0))
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301687 {
1688 return IH264E_SUCCESS;
1689 }
1690
1691 for (row = 0; row < i4_n_mb_process_count; row++)
1692 {
1693 ih264e_deblock_mb(ps_proc, ps_deblk);
1694
1695 pu1_deblk_map[ps_deblk->i4_mb_x] = 1;
1696
1697 if (ps_deblk->i4_mb_y > 0)
1698 {
1699 if (ps_deblk->i4_mb_x == 0)/* If left most MB is processed, then pad left*/
1700 {
1701 /* padding left luma */
1702 ps_codec->pf_pad_left_luma(ps_deblk->pu1_cur_pic_luma - i4_rec_strd * MB_SIZE, i4_rec_strd, MB_SIZE, PAD_LEFT);
1703
1704 /* padding left chroma */
1705 ps_codec->pf_pad_left_chroma(ps_deblk->pu1_cur_pic_chroma - i4_rec_strd * BLK8x8SIZE, i4_rec_strd, MB_SIZE >> 1, PAD_LEFT);
1706 }
1707
1708 if (ps_deblk->i4_mb_x == (ps_proc->i4_wd_mbs - 1))/*last column*/
1709 {
1710 /* padding right luma */
1711 ps_codec->pf_pad_right_luma(ps_deblk->pu1_cur_pic_luma - i4_rec_strd * MB_SIZE + MB_SIZE, i4_rec_strd, MB_SIZE, PAD_RIGHT);
1712
1713 /* padding right chroma */
1714 ps_codec->pf_pad_right_chroma(ps_deblk->pu1_cur_pic_chroma - i4_rec_strd * BLK8x8SIZE + MB_SIZE, i4_rec_strd, MB_SIZE >> 1, PAD_RIGHT);
1715 }
1716 }
1717 ps_deblk->i4_mb_x++;
1718
1719 ps_deblk->pu1_cur_pic_luma += MB_SIZE;
1720 ps_deblk->pu1_cur_pic_chroma += MB_SIZE;
1721
1722 }
1723 }
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301724 else if(i4_mb_y > 0)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301725 {
1726 ps_deblk->i4_mb_x += i4_n_mb_process_count;
1727
1728 ps_deblk->pu1_cur_pic_luma += i4_n_mb_process_count * MB_SIZE;
1729 ps_deblk->pu1_cur_pic_chroma += i4_n_mb_process_count * MB_SIZE;
1730 }
1731
1732 if (i4_mb_y == 2)
1733 {
1734 u4_pad_wd = i4_n_mb_process_count * MB_SIZE;
1735 u4_pad_top = ps_n_mb_ctxt->i4_mb_x * MB_SIZE;
1736
1737 if (ps_n_mb_ctxt->i4_mb_x == 0)
1738 {
1739 u4_pad_wd += PAD_LEFT;
1740 u4_pad_top = -PAD_LEFT;
1741 }
1742
1743 if (i4_mb_x == ps_proc->i4_wd_mbs - 1)
1744 {
1745 u4_pad_wd += PAD_RIGHT;
1746 }
1747
1748 /* padding top luma */
1749 ps_codec->pf_pad_top(ps_proc->pu1_rec_buf_luma_base + u4_pad_top, i4_rec_strd, u4_pad_wd, PAD_TOP);
1750
1751 /* padding top chroma */
1752 ps_codec->pf_pad_top(ps_proc->pu1_rec_buf_chroma_base + u4_pad_top, i4_rec_strd, u4_pad_wd, (PAD_TOP >> 1));
1753 }
1754
1755 ps_n_mb_ctxt->i4_mb_x += i4_n_mb_process_count;
1756
1757 if (i4_mb_x == ps_proc->i4_wd_mbs - 1)
1758 {
1759 if (ps_proc->i4_mb_y == ps_proc->i4_ht_mbs - 1)
1760 {
1761 /* Bottom Padding is done in one stretch for the entire width */
1762 if (ps_proc->u4_disable_deblock_level != 1)
1763 {
1764 ps_deblk->pu1_cur_pic_luma = ps_proc->pu1_rec_buf_luma_base + (ps_proc->i4_ht_mbs - 1) * i4_rec_strd * MB_SIZE;
1765
1766 ps_deblk->pu1_cur_pic_chroma = ps_proc->pu1_rec_buf_chroma_base + (ps_proc->i4_ht_mbs - 1) * i4_rec_strd * BLK8x8SIZE;
1767
1768 ps_n_mb_ctxt->i4_mb_x = 0;
1769 ps_n_mb_ctxt->i4_mb_y = ps_proc->i4_mb_y;
1770 ps_deblk->i4_mb_x = 0;
1771 ps_deblk->i4_mb_y = ps_proc->i4_mb_y;
1772
1773 /* update pic qp map (as update_proc_ctxt is still not called for the last MB) */
1774 ps_proc->s_deblk_ctxt.s_bs_ctxt.pu1_pic_qp[(i4_mb_y * ps_proc->i4_wd_mbs) + i4_mb_x] = ps_proc->u4_mb_qp;
1775
1776 i4_n_mb_process_count = (ps_proc->i4_wd_mbs) % i4_n_mbs;
1777
1778 j = (ps_proc->i4_wd_mbs) / i4_n_mbs;
1779
1780 for (i = 0; i < j; i++)
1781 {
1782 for (col = 0; col < i4_n_mbs; col++)
1783 {
1784 ih264e_deblock_mb(ps_proc, ps_deblk);
1785
1786 pu1_deblk_map[ps_deblk->i4_mb_x] = 1;
1787
1788 ps_deblk->i4_mb_x++;
1789 ps_deblk->pu1_cur_pic_luma += MB_SIZE;
1790 ps_deblk->pu1_cur_pic_chroma += MB_SIZE;
1791 ps_n_mb_ctxt->i4_mb_x++;
1792 }
1793 }
1794
1795 for (col = 0; col < i4_n_mb_process_count; col++)
1796 {
1797 ih264e_deblock_mb(ps_proc, ps_deblk);
1798
1799 pu1_deblk_map[ps_deblk->i4_mb_x] = 1;
1800
1801 ps_deblk->i4_mb_x++;
1802 ps_deblk->pu1_cur_pic_luma += MB_SIZE;
1803 ps_deblk->pu1_cur_pic_chroma += MB_SIZE;
1804 ps_n_mb_ctxt->i4_mb_x++;
1805 }
1806
1807 pu1_pad_src_luma = ps_proc->pu1_rec_buf_luma_base + (ps_proc->i4_ht_mbs - 2) * MB_SIZE * i4_rec_strd;
1808
1809 pu1_pad_src_chroma = ps_proc->pu1_rec_buf_chroma_base + (ps_proc->i4_ht_mbs - 2) * BLK8x8SIZE * i4_rec_strd;
1810
1811 /* padding left luma */
1812 ps_codec->pf_pad_left_luma(pu1_pad_src_luma, i4_rec_strd, MB_SIZE, PAD_LEFT);
1813
1814 /* padding left chroma */
1815 ps_codec->pf_pad_left_chroma(pu1_pad_src_chroma, i4_rec_strd, BLK8x8SIZE, PAD_LEFT);
1816
1817 pu1_pad_src_luma += i4_rec_strd * MB_SIZE;
1818 pu1_pad_src_chroma += i4_rec_strd * BLK8x8SIZE;
1819
1820 /* padding left luma */
1821 ps_codec->pf_pad_left_luma(pu1_pad_src_luma, i4_rec_strd, MB_SIZE, PAD_LEFT);
1822
1823 /* padding left chroma */
1824 ps_codec->pf_pad_left_chroma(pu1_pad_src_chroma, i4_rec_strd, BLK8x8SIZE, PAD_LEFT);
1825
1826 pu1_pad_src_luma = ps_proc->pu1_rec_buf_luma_base + (ps_proc->i4_ht_mbs - 2) * MB_SIZE * i4_rec_strd + (ps_proc->i4_wd_mbs) * MB_SIZE;
1827
1828 pu1_pad_src_chroma = ps_proc->pu1_rec_buf_chroma_base + (ps_proc->i4_ht_mbs - 2) * BLK8x8SIZE * i4_rec_strd + (ps_proc->i4_wd_mbs) * MB_SIZE;
1829
1830 /* padding right luma */
1831 ps_codec->pf_pad_right_luma(pu1_pad_src_luma, i4_rec_strd, MB_SIZE, PAD_RIGHT);
1832
1833 /* padding right chroma */
1834 ps_codec->pf_pad_right_chroma(pu1_pad_src_chroma, i4_rec_strd, BLK8x8SIZE, PAD_RIGHT);
1835
1836 pu1_pad_src_luma += i4_rec_strd * MB_SIZE;
1837 pu1_pad_src_chroma += i4_rec_strd * BLK8x8SIZE;
1838
1839 /* padding right luma */
1840 ps_codec->pf_pad_right_luma(pu1_pad_src_luma, i4_rec_strd, MB_SIZE, PAD_RIGHT);
1841
1842 /* padding right chroma */
1843 ps_codec->pf_pad_right_chroma(pu1_pad_src_chroma, i4_rec_strd, BLK8x8SIZE, PAD_RIGHT);
1844
1845 }
1846
Harish Mahendrakarc72323e2015-04-28 19:07:40 +05301847 /* In case height is less than 2 MBs pad top */
1848 if (ps_proc->i4_ht_mbs <= 2)
1849 {
1850 UWORD8 *pu1_pad_top_src;
1851 /* padding top luma */
1852 pu1_pad_top_src = ps_proc->pu1_rec_buf_luma_base - PAD_LEFT;
1853 ps_codec->pf_pad_top(pu1_pad_top_src, i4_rec_strd, i4_rec_strd, PAD_TOP);
1854
1855 /* padding top chroma */
1856 pu1_pad_top_src = ps_proc->pu1_rec_buf_chroma_base - PAD_LEFT;
1857 ps_codec->pf_pad_top(pu1_pad_top_src, i4_rec_strd, i4_rec_strd, (PAD_TOP >> 1));
1858 }
1859
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301860 /* padding bottom luma */
1861 pu1_pad_bottom_src = ps_proc->pu1_rec_buf_luma_base + ps_proc->i4_ht_mbs * MB_SIZE * i4_rec_strd - PAD_LEFT;
1862 ps_codec->pf_pad_bottom(pu1_pad_bottom_src, i4_rec_strd, i4_rec_strd, PAD_BOT);
1863
1864 /* padding bottom chroma */
1865 pu1_pad_bottom_src = ps_proc->pu1_rec_buf_chroma_base + ps_proc->i4_ht_mbs * (MB_SIZE >> 1) * i4_rec_strd - PAD_LEFT;
1866 ps_codec->pf_pad_bottom(pu1_pad_bottom_src, i4_rec_strd, i4_rec_strd, (PAD_BOT >> 1));
1867 }
1868 }
1869 }
1870 }
1871
1872 return IH264E_SUCCESS;
1873}
1874
1875
1876/**
1877*******************************************************************************
1878*
1879* @brief This function performs luma & chroma core coding for a set of mb's.
1880*
1881* @par Description:
1882* The mb to be coded is taken and is evaluated over a predefined set of modes
1883* (intra (i16, i4, i8)/inter (mv, skip)) for best cost. The mode with least cost
1884* is selected and using intra/inter prediction filters, prediction is carried out.
1885* The deviation between src and pred signal constitutes error signal. This error
1886* signal is transformed (hierarchical transform if necessary) and quantized. The
1887* quantized residue is packed in to entropy buffer for entropy coding. This is
1888* repeated for all the mb's enlisted under the job.
1889*
1890* @param[in] ps_proc
1891* Process context corresponding to the job
1892*
1893* @returns error status
1894*
1895* @remarks none
1896*
1897*******************************************************************************
1898*/
1899WORD32 ih264e_process(process_ctxt_t *ps_proc)
1900{
1901 /* error status */
1902 WORD32 error_status = IH264_SUCCESS;
1903
1904 /* codec context */
1905 codec_t *ps_codec = ps_proc->ps_codec;
1906
1907 /* cbp luma, chroma */
1908 UWORD32 u4_cbp_l, u4_cbp_c;
1909
1910 /* width in mbs */
1911 WORD32 i4_wd_mbs = ps_proc->i4_wd_mbs;
1912
1913 /* loop var */
1914 WORD32 i4_mb_idx, i4_mb_cnt = ps_proc->i4_mb_cnt;
1915
1916 /* valid modes */
1917 UWORD32 u4_valid_modes = 0;
1918
1919 /* gate threshold */
1920 WORD32 i4_gate_threshold = 0;
1921
1922 /* is intra */
1923 WORD32 luma_idx, chroma_idx, is_intra;
1924
1925 /* temp variables */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05301926 WORD32 ctxt_sel = ps_proc->i4_encode_api_call_cnt % MAX_CTXT_SETS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301927
Harinarayanan K K85883032015-06-19 11:39:42 +05301928 /*
1929 * list of modes for evaluation
1930 * -------------------------------------------------------------------------
1931 * Note on enabling I4x4 and I16x16
1932 * At very low QP's the hadamard transform in I16x16 will push up the maximum
1933 * coeff value very high. CAVLC may not be able to represent the value and
1934 * hence the stream may not be decodable in some clips.
1935 * Hence at low QPs, we will enable I4x4 and disable I16x16 irrespective of preset.
1936 */
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301937 if (ps_proc->i4_slice_type == ISLICE)
1938 {
Harinarayanan K K85883032015-06-19 11:39:42 +05301939 if (ps_proc->u4_frame_qp > 10)
1940 {
1941 /* enable intra 16x16 */
1942 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_16x16 ? (1 << I16x16) : 0;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301943
Harinarayanan K K85883032015-06-19 11:39:42 +05301944 /* enable intra 8x8 */
1945 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_8x8 ? (1 << I8x8) : 0;
1946 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301947
1948 /* enable intra 4x4 */
1949 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_4x4 ? (1 << I4x4) : 0;
Harinarayanan K K85883032015-06-19 11:39:42 +05301950 u4_valid_modes |= (ps_proc->u4_frame_qp <= 10) << I4x4;
1951
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301952 }
1953 else if (ps_proc->i4_slice_type == PSLICE)
1954 {
Harinarayanan K K85883032015-06-19 11:39:42 +05301955 if (ps_proc->u4_frame_qp > 10)
1956 {
1957 /* enable intra 16x16 */
1958 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_16x16 ? (1 << I16x16) : 0;
1959 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301960
1961 /* enable intra 4x4 */
1962 if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_SLOWEST)
1963 {
1964 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_4x4 ? (1 << I4x4) : 0;
1965 }
Harinarayanan K K85883032015-06-19 11:39:42 +05301966 u4_valid_modes |= (ps_proc->u4_frame_qp <= 10) << I4x4;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301967
Harinarayanan K K134291e2015-06-18 16:03:38 +05301968 /* enable inter P16x16 */
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301969 u4_valid_modes |= (1 << P16x16);
1970 }
Harinarayanan K K134291e2015-06-18 16:03:38 +05301971 else if (ps_proc->i4_slice_type == BSLICE)
1972 {
Harinarayanan K K85883032015-06-19 11:39:42 +05301973 if (ps_proc->u4_frame_qp > 10)
1974 {
1975 /* enable intra 16x16 */
1976 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_16x16 ? (1 << I16x16) : 0;
1977 }
Harinarayanan K K134291e2015-06-18 16:03:38 +05301978
1979 /* enable intra 4x4 */
1980 if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_SLOWEST)
1981 {
1982 u4_valid_modes |= ps_codec->s_cfg.u4_enable_intra_4x4 ? (1 << I4x4) : 0;
1983 }
Harinarayanan K K85883032015-06-19 11:39:42 +05301984 u4_valid_modes |= (ps_proc->u4_frame_qp <= 10) << I4x4;
Harinarayanan K K134291e2015-06-18 16:03:38 +05301985
1986 /* enable inter B16x16 */
1987 u4_valid_modes |= (1 << B16x16);
1988 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05301989
1990
1991 /* init entropy */
1992 ps_proc->s_entropy.i4_mb_x = ps_proc->i4_mb_x;
1993 ps_proc->s_entropy.i4_mb_y = ps_proc->i4_mb_y;
1994 ps_proc->s_entropy.i4_mb_cnt = MIN(ps_proc->i4_nmb_ntrpy, i4_wd_mbs - ps_proc->i4_mb_x);
1995
1996 /* compute recon when :
1997 * 1. current frame is to be used as a reference
1998 * 2. dump recon for bit stream sanity check
1999 */
2000 ps_proc->u4_compute_recon = ps_codec->u4_is_curr_frm_ref ||
2001 ps_codec->s_cfg.u4_enable_recon;
2002
2003 /* Encode 'n' macroblocks,
2004 * 'n' being the number of mbs dictated by current proc ctxt */
2005 for (i4_mb_idx = 0; i4_mb_idx < i4_mb_cnt; i4_mb_idx ++)
2006 {
2007 /* since we have not yet found sad, we have not yet got min sad */
2008 /* we need to initialize these variables for each MB */
2009 /* TODO how to get the min sad into the codec */
2010 ps_proc->u4_min_sad = ps_codec->s_cfg.i4_min_sad;
2011 ps_proc->u4_min_sad_reached = 0;
2012
2013 /* mb analysis */
2014 {
2015 /* temp var */
2016 WORD32 i4_mb_id = ps_proc->i4_mb_x + ps_proc->i4_mb_y * i4_wd_mbs;
2017
2018 /* force intra refresh ? */
2019 WORD32 i4_air_enable_inter = (ps_codec->s_cfg.e_air_mode == IVE_AIR_MODE_NONE) ||
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302020 (ps_codec->pu2_intr_rfrsh_map[i4_mb_id] != ps_codec->i4_air_pic_cnt);
2021
2022 /* evaluate inter 16x16 modes */
Harinarayanan K K134291e2015-06-18 16:03:38 +05302023 if ((u4_valid_modes & (1 << P16x16)) || (u4_valid_modes & (1 << B16x16)))
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302024 {
2025 /* compute nmb me */
2026 if (ps_proc->i4_mb_x % ps_proc->u4_nmb_me == 0)
2027 {
2028 ih264e_compute_me_nmb(ps_proc, MIN((WORD32)ps_proc->u4_nmb_me,
2029 i4_wd_mbs - ps_proc->i4_mb_x));
2030 }
2031
2032 /* set pointers to ME data appropriately for other modules to use */
2033 {
2034 UWORD32 u4_mb_index = ps_proc->i4_mb_x % ps_proc->u4_nmb_me ;
2035
2036 /* get the min sad condition for current mb */
2037 ps_proc->u4_min_sad_reached = ps_proc->ps_nmb_info[u4_mb_index].u4_min_sad_reached;
2038 ps_proc->u4_min_sad = ps_proc->ps_nmb_info[u4_mb_index].u4_min_sad;
2039
Harinarayanan K K134291e2015-06-18 16:03:38 +05302040 ps_proc->ps_skip_mv = &(ps_proc->ps_nmb_info[u4_mb_index].as_skip_mv[0]);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302041 ps_proc->ps_ngbr_avbl = &(ps_proc->ps_nmb_info[u4_mb_index].s_ngbr_avbl);
Harinarayanan K K134291e2015-06-18 16:03:38 +05302042 ps_proc->ps_pred_mv = &(ps_proc->ps_nmb_info[u4_mb_index].as_pred_mv[0]);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302043
2044 ps_proc->i4_mb_distortion = ps_proc->ps_nmb_info[u4_mb_index].i4_mb_distortion;
2045 ps_proc->i4_mb_cost = ps_proc->ps_nmb_info[u4_mb_index].i4_mb_cost;
2046 ps_proc->u4_min_sad = ps_proc->ps_nmb_info[u4_mb_index].u4_min_sad;
2047 ps_proc->u4_min_sad_reached = ps_proc->ps_nmb_info[u4_mb_index].u4_min_sad_reached;
2048 ps_proc->u4_mb_type = ps_proc->ps_nmb_info[u4_mb_index].u4_mb_type;
2049
2050 /* get the best sub pel buffer */
2051 ps_proc->pu1_best_subpel_buf = ps_proc->ps_nmb_info[u4_mb_index].pu1_best_sub_pel_buf;
2052 ps_proc->u4_bst_spel_buf_strd = ps_proc->ps_nmb_info[u4_mb_index].u4_bst_spel_buf_strd;
2053 }
2054 ih264e_derive_nghbr_avbl_of_mbs(ps_proc);
2055 }
2056 else
2057 {
2058 /* Derive neighbor availability for the current macroblock */
2059 ps_proc->ps_ngbr_avbl = &ps_proc->s_ngbr_avbl;
2060
2061 ih264e_derive_nghbr_avbl_of_mbs(ps_proc);
2062 }
2063
2064 /*
2065 * If air says intra, we need to force the following code path to evaluate intra
2066 * The easy way is just to say that the inter cost is too much
2067 */
2068 if (!i4_air_enable_inter)
2069 {
2070 ps_proc->u4_min_sad_reached = 0;
2071 ps_proc->i4_mb_cost = INT_MAX;
2072 ps_proc->i4_mb_distortion = INT_MAX;
2073 }
2074 else if (ps_proc->u4_mb_type == PSKIP)
2075 {
2076 goto UPDATE_MB_INFO;
2077 }
2078
2079 /* wait until the proc of [top + 1] mb is computed.
2080 * We wait till the proc dependencies are satisfied */
2081 if(ps_proc->i4_mb_y > 0)
2082 {
2083 /* proc map */
2084 UWORD8 *pu1_proc_map_top;
2085
2086 pu1_proc_map_top = ps_proc->pu1_proc_map + ((ps_proc->i4_mb_y - 1) * i4_wd_mbs);
2087
2088 while (1)
2089 {
2090 volatile UWORD8 *pu1_buf;
2091 WORD32 idx = i4_mb_idx + 1;
2092
2093 idx = MIN(idx, ((WORD32)ps_codec->s_cfg.i4_wd_mbs - 1));
2094 pu1_buf = pu1_proc_map_top + idx;
2095 if(*pu1_buf)
2096 break;
2097 ithread_yield();
2098 }
2099 }
2100
2101 /* If we already have the minimum sad, there is no point in searching for sad again */
Ram Mohan82fb2d32017-11-27 17:56:29 +05302102 if (ps_proc->u4_min_sad_reached == 0 || ps_codec->s_cfg.u4_enc_speed_preset != IVE_FASTEST)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302103 {
2104 /* intra gating in inter slices */
2105 /* No need of gating if we want to force intra, we need to find the threshold only if inter is enabled by AIR*/
Harinarayanan K K134291e2015-06-18 16:03:38 +05302106 if (i4_air_enable_inter && ps_proc->i4_slice_type != ISLICE && ps_codec->u4_inter_gate)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302107 {
2108 /* distortion of neighboring blocks */
2109 WORD32 i4_distortion[4];
2110
2111 i4_distortion[0] = ps_proc->s_left_mb_syntax_ele.i4_mb_distortion;
2112
2113 i4_distortion[1] = ps_proc->ps_top_row_mb_syntax_ele[ps_proc->i4_mb_x].i4_mb_distortion;
2114
2115 i4_distortion[2] = ps_proc->ps_top_row_mb_syntax_ele[ps_proc->i4_mb_x + 1].i4_mb_distortion;
2116
2117 i4_distortion[3] = ps_proc->s_top_left_mb_syntax_ele.i4_mb_distortion;
2118
2119 i4_gate_threshold = (i4_distortion[0] + i4_distortion[1] + i4_distortion[2] + i4_distortion[3]) >> 2;
2120
2121 }
2122
Harinarayanan K K134291e2015-06-18 16:03:38 +05302123
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302124 /* If we are going to force intra we need to evaluate intra irrespective of gating */
2125 if ( (!i4_air_enable_inter) || ((i4_gate_threshold + 16 *((WORD32) ps_proc->u4_lambda)) < ps_proc->i4_mb_distortion))
2126 {
2127 /* evaluate intra 4x4 modes */
2128 if (u4_valid_modes & (1 << I4x4))
2129 {
2130 if (ps_codec->s_cfg.u4_enc_speed_preset == IVE_SLOWEST)
2131 {
2132 ih264e_evaluate_intra4x4_modes_for_least_cost_rdopton(ps_proc);
2133 }
2134 else
2135 {
2136 ih264e_evaluate_intra4x4_modes_for_least_cost_rdoptoff(ps_proc);
2137 }
2138 }
2139
2140 /* evaluate intra 16x16 modes */
2141 if (u4_valid_modes & (1 << I16x16))
2142 {
2143 ih264e_evaluate_intra16x16_modes_for_least_cost_rdoptoff(ps_proc);
2144 }
2145
2146 /* evaluate intra 8x8 modes */
2147 if (u4_valid_modes & (1 << I8x8))
2148 {
2149 ih264e_evaluate_intra8x8_modes_for_least_cost_rdoptoff(ps_proc);
2150 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302151
Harinarayanan K K134291e2015-06-18 16:03:38 +05302152 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302153 }
Harinarayanan K K134291e2015-06-18 16:03:38 +05302154 }
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302155
2156 /* is intra */
2157 if (ps_proc->u4_mb_type == I4x4 || ps_proc->u4_mb_type == I16x16 || ps_proc->u4_mb_type == I8x8)
2158 {
2159 luma_idx = ps_proc->u4_mb_type;
2160 chroma_idx = 0;
2161 is_intra = 1;
2162
2163 /* evaluate chroma blocks for intra */
2164 ih264e_evaluate_chroma_intra8x8_modes_for_least_cost_rdoptoff(ps_proc);
2165 }
2166 else
2167 {
2168 luma_idx = 3;
2169 chroma_idx = 1;
2170 is_intra = 0;
2171 }
2172 ps_proc->u4_is_intra = is_intra;
Harinarayanan K K134291e2015-06-18 16:03:38 +05302173 ps_proc->ps_pu->b1_intra_flag = is_intra;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302174
2175 /* redo MV pred of neighbors in the case intra mb */
2176 /* TODO : currently called unconditionally, needs to be called only in the case of intra
2177 * to modify neighbors */
2178 if (ps_proc->i4_slice_type != ISLICE)
2179 {
Harinarayanan K K134291e2015-06-18 16:03:38 +05302180 ih264e_mv_pred(ps_proc, ps_proc->i4_slice_type);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302181 }
2182
2183 /* Perform luma mb core coding */
2184 u4_cbp_l = (ps_codec->luma_energy_compaction)[luma_idx](ps_proc);
2185
2186 /* Perform luma mb core coding */
2187 u4_cbp_c = (ps_codec->chroma_energy_compaction)[chroma_idx](ps_proc);
2188
2189 /* coded block pattern */
2190 ps_proc->u4_cbp = (u4_cbp_c << 4) | u4_cbp_l;
2191
Harinarayanan K K134291e2015-06-18 16:03:38 +05302192 if (!ps_proc->u4_is_intra)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302193 {
Harinarayanan K K134291e2015-06-18 16:03:38 +05302194 if (ps_proc->i4_slice_type == BSLICE)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302195 {
Harinarayanan K K134291e2015-06-18 16:03:38 +05302196 if (ih264e_find_bskip_params(ps_proc, PRED_L0))
2197 {
2198 ps_proc->u4_mb_type = (ps_proc->u4_cbp) ? BDIRECT : BSKIP;
2199 }
2200 }
2201 else if(!ps_proc->u4_cbp)
2202 {
2203 if (ih264e_find_pskip_params(ps_proc, PRED_L0))
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302204 {
2205 ps_proc->u4_mb_type = PSKIP;
2206 }
2207 }
2208 }
2209
2210UPDATE_MB_INFO:
2211
2212 /* Update mb sad, mb qp and intra mb cost. Will be used by rate control */
2213 ih264e_update_rc_mb_info(&ps_proc->s_frame_info, ps_proc);
2214
2215 /**********************************************************************/
2216 /* if disable deblock level is '0' this implies enable deblocking for */
2217 /* all edges of all macroblocks with out any restrictions */
2218 /* */
2219 /* if disable deblock level is '1' this implies disable deblocking for*/
2220 /* all edges of all macroblocks with out any restrictions */
2221 /* */
2222 /* if disable deblock level is '2' this implies enable deblocking for */
2223 /* all edges of all macroblocks except edges overlapping with slice */
2224 /* boundaries. This option is not currently supported by the encoder */
2225 /* hence the slice map should be of no significance to perform debloc */
2226 /* king */
2227 /**********************************************************************/
2228
2229 if (ps_proc->u4_compute_recon)
2230 {
2231 /* deblk context */
2232 /* src pointers */
2233 UWORD8 *pu1_cur_pic_luma = ps_proc->pu1_rec_buf_luma;
2234 UWORD8 *pu1_cur_pic_chroma = ps_proc->pu1_rec_buf_chroma;
2235
2236 /* src indices */
2237 UWORD32 i4_mb_x = ps_proc->i4_mb_x;
2238 UWORD32 i4_mb_y = ps_proc->i4_mb_y;
2239
2240 /* compute blocking strength */
2241 if (ps_proc->u4_disable_deblock_level != 1)
2242 {
2243 ih264e_compute_bs(ps_proc);
2244 }
2245
2246 /* nmb deblocking and hpel and padding */
2247 ih264e_dblk_pad_hpel_processing_n_mbs(ps_proc, pu1_cur_pic_luma,
2248 pu1_cur_pic_chroma, i4_mb_x,
2249 i4_mb_y);
2250 }
2251
2252 /* update the context after for coding next mb */
2253 error_status |= ih264e_update_proc_ctxt(ps_proc);
2254
2255 /* Once the last row is processed, mark the buffer status appropriately */
2256 if (ps_proc->i4_ht_mbs == ps_proc->i4_mb_y)
2257 {
2258 /* Pointer to current picture buffer structure */
2259 pic_buf_t *ps_cur_pic = ps_proc->ps_cur_pic;
2260
2261 /* Pointer to current picture's mv buffer structure */
2262 mv_buf_t *ps_cur_mv_buf = ps_proc->ps_cur_mv_buf;
2263
2264 /**********************************************************************/
2265 /* if disable deblock level is '0' this implies enable deblocking for */
2266 /* all edges of all macroblocks with out any restrictions */
2267 /* */
2268 /* if disable deblock level is '1' this implies disable deblocking for*/
2269 /* all edges of all macroblocks with out any restrictions */
2270 /* */
2271 /* if disable deblock level is '2' this implies enable deblocking for */
2272 /* all edges of all macroblocks except edges overlapping with slice */
2273 /* boundaries. This option is not currently supported by the encoder */
2274 /* hence the slice map should be of no significance to perform debloc */
2275 /* king */
2276 /**********************************************************************/
2277 error_status |= ih264_buf_mgr_release(ps_codec->pv_mv_buf_mgr, ps_cur_mv_buf->i4_buf_id , BUF_MGR_CODEC);
2278
2279 error_status |= ih264_buf_mgr_release(ps_codec->pv_ref_buf_mgr, ps_cur_pic->i4_buf_id , BUF_MGR_CODEC);
2280
2281 if (ps_codec->s_cfg.u4_enable_recon)
2282 {
2283 /* pic cnt */
2284 ps_codec->as_rec_buf[ctxt_sel].i4_pic_cnt = ps_proc->i4_pic_cnt;
2285
2286 /* rec buffers */
2287 ps_codec->as_rec_buf[ctxt_sel].s_pic_buf = *ps_proc->ps_cur_pic;
2288
2289 /* is last? */
2290 ps_codec->as_rec_buf[ctxt_sel].u4_is_last = ps_proc->s_entropy.u4_is_last;
2291
2292 /* frame time stamp */
2293 ps_codec->as_rec_buf[ctxt_sel].u4_timestamp_high = ps_proc->s_entropy.u4_timestamp_high;
2294 ps_codec->as_rec_buf[ctxt_sel].u4_timestamp_low = ps_proc->s_entropy.u4_timestamp_low;
2295 }
2296
2297 }
2298 }
2299
2300 DEBUG_HISTOGRAM_DUMP(ps_codec->s_cfg.i4_ht_mbs == ps_proc->i4_mb_y);
2301
2302 return error_status;
2303}
2304
2305/**
2306*******************************************************************************
2307*
2308* @brief
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302309* Function to update rc context after encoding
2310*
2311* @par Description
2312* This function updates the rate control context after the frame is encoded.
2313* Number of bits consumed by the current frame, frame distortion, frame cost,
2314* number of intra/inter mb's, ... are passed on to rate control context for
2315* updating the rc model.
2316*
2317* @param[in] ps_codec
2318* Handle to codec context
2319*
2320* @param[in] ctxt_sel
2321* frame context selector
2322*
2323* @param[in] pic_cnt
2324* pic count
2325*
2326* @returns i4_stuffing_byte
2327* number of stuffing bytes (if necessary)
2328*
2329* @remarks
2330*
2331*******************************************************************************
2332*/
Harinarayanan K K134291e2015-06-18 16:03:38 +05302333WORD32 ih264e_update_rc_post_enc(codec_t *ps_codec, WORD32 ctxt_sel, WORD32 i4_is_first_frm)
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302334{
2335 /* proc set base idx */
2336 WORD32 i4_proc_ctxt_sel_base = ctxt_sel ? (MAX_PROCESS_CTXT / 2) : 0;
2337
2338 /* proc ctxt */
2339 process_ctxt_t *ps_proc = &ps_codec->as_process[i4_proc_ctxt_sel_base];
2340
2341 /* frame qp */
2342 UWORD8 u1_frame_qp = ps_codec->u4_frame_qp;
2343
2344 /* cbr rc return status */
2345 WORD32 i4_stuffing_byte = 0;
2346
2347 /* current frame stats */
2348 frame_info_t s_frame_info;
2349 picture_type_e rc_pic_type;
2350
2351 /* temp var */
2352 WORD32 i, j;
2353
2354 /********************************************************************/
2355 /* BEGIN INIT */
2356 /********************************************************************/
2357
2358 /* init frame info */
2359 irc_init_frame_info(&s_frame_info);
2360
2361 /* get frame info */
2362 for (i = 0; i < (WORD32)ps_codec->s_cfg.u4_num_cores; i++)
2363 {
2364 /*****************************************************************/
2365 /* One frame can be encoded by max of u4_num_cores threads */
2366 /* Accumulating the num mbs, sad, qp and intra_mb_cost from */
2367 /* u4_num_cores threads */
2368 /*****************************************************************/
2369 for (j = 0; j< MAX_MB_TYPE; j++)
2370 {
2371 s_frame_info.num_mbs[j] += ps_proc[i].s_frame_info.num_mbs[j];
2372
2373 s_frame_info.tot_mb_sad[j] += ps_proc[i].s_frame_info.tot_mb_sad[j];
2374
2375 s_frame_info.qp_sum[j] += ps_proc[i].s_frame_info.qp_sum[j];
2376 }
2377
2378 s_frame_info.intra_mb_cost_sum += ps_proc[i].s_frame_info.intra_mb_cost_sum;
2379
2380 s_frame_info.activity_sum += ps_proc[i].s_frame_info.activity_sum;
2381
2382 /*****************************************************************/
2383 /* gather number of residue and header bits consumed by the frame*/
2384 /*****************************************************************/
2385 ih264e_update_rc_bits_info(&s_frame_info, &ps_proc[i].s_entropy);
2386 }
2387
2388 /* get pic type */
2389 switch (ps_codec->pic_type)
2390 {
2391 case PIC_I:
2392 case PIC_IDR:
2393 rc_pic_type = I_PIC;
2394 break;
2395 case PIC_P:
2396 rc_pic_type = P_PIC;
2397 break;
2398 case PIC_B:
2399 rc_pic_type = B_PIC;
2400 break;
2401 default:
2402 assert(0);
2403 break;
2404 }
2405
2406 /* update rc lib with current frame stats */
2407 i4_stuffing_byte = ih264e_rc_post_enc(ps_codec->s_rate_control.pps_rate_control_api,
2408 &(s_frame_info),
2409 ps_codec->s_rate_control.pps_pd_frm_rate,
2410 ps_codec->s_rate_control.pps_time_stamp,
2411 ps_codec->s_rate_control.pps_frame_time,
2412 (ps_proc->i4_wd_mbs * ps_proc->i4_ht_mbs),
2413 &rc_pic_type,
Harinarayanan K K134291e2015-06-18 16:03:38 +05302414 i4_is_first_frm,
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302415 &ps_codec->s_rate_control.post_encode_skip[ctxt_sel],
2416 u1_frame_qp,
2417 &ps_codec->s_rate_control.num_intra_in_prev_frame,
2418 &ps_codec->s_rate_control.i4_avg_activity);
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302419 return i4_stuffing_byte;
2420}
2421
2422/**
2423*******************************************************************************
2424*
2425* @brief
2426* entry point of a spawned encoder thread
2427*
2428* @par Description:
2429* The encoder thread dequeues a proc/entropy job from the encoder queue and
2430* calls necessary routines.
2431*
2432* @param[in] pv_proc
2433* Process context corresponding to the thread
2434*
2435* @returns error status
2436*
2437* @remarks
2438*
2439*******************************************************************************
2440*/
2441WORD32 ih264e_process_thread(void *pv_proc)
2442{
2443 /* error status */
2444 IH264_ERROR_T ret = IH264_SUCCESS;
2445 WORD32 error_status = IH264_SUCCESS;
2446
2447 /* proc ctxt */
2448 process_ctxt_t *ps_proc = pv_proc;
2449
2450 /* codec ctxt */
2451 codec_t *ps_codec = ps_proc->ps_codec;
2452
2453 /* structure to represent a processing job entry */
2454 job_t s_job;
2455
2456 /* blocking call : entropy dequeue is non-blocking till all
2457 * the proc jobs are processed */
2458 WORD32 is_blocking = 0;
2459
2460 /* set affinity */
2461 ithread_set_affinity(ps_proc->i4_id);
2462
2463 while(1)
2464 {
2465 /* dequeue a job from the entropy queue */
2466 {
2467 int error = ithread_mutex_lock(ps_codec->pv_entropy_mutex);
2468
2469 /* codec context selector */
Harinarayanan K K6cb67722015-06-19 14:44:42 +05302470 WORD32 ctxt_sel = ps_codec->i4_encode_api_call_cnt % MAX_CTXT_SETS;
Hamsalekha S8d3d3032015-03-13 21:24:58 +05302471
2472 volatile UWORD32 *pu4_buf = &ps_codec->au4_entropy_thread_active[ctxt_sel];
2473
2474 /* have the lock */
2475 if (error == 0)
2476 {
2477 if (*pu4_buf == 0)
2478 {
2479 /* no entropy threads are active, try dequeuing a job from the entropy queue */
2480 ret = ih264_list_dequeue(ps_proc->pv_entropy_jobq, &s_job, is_blocking);
2481 if (IH264_SUCCESS == ret)
2482 {
2483 *pu4_buf = 1;
2484 ithread_mutex_unlock(ps_codec->pv_entropy_mutex);
2485 goto WORKER;
2486 }
2487 else if(is_blocking)
2488 {
2489 ithread_mutex_unlock(ps_codec->pv_entropy_mutex);
2490 break;
2491 }
2492 }
2493 ithread_mutex_unlock(ps_codec->pv_entropy_mutex);
2494 }
2495 }
2496
2497 /* dequeue a job from the process queue */
2498 ret = ih264_list_dequeue(ps_proc->pv_proc_jobq, &s_job, 1);
2499 if (IH264_SUCCESS != ret)
2500 {
2501 if(ps_proc->i4_id)
2502 break;
2503 else
2504 {
2505 is_blocking = 1;
2506 continue;
2507 }
2508 }
2509
2510WORKER:
2511 /* choose appropriate proc context based on proc_base_idx */
2512 ps_proc = &ps_codec->as_process[ps_proc->i4_id + s_job.i2_proc_base_idx];
2513
2514 switch (s_job.i4_cmd)
2515 {
2516 case CMD_PROCESS:
2517 ps_proc->i4_mb_cnt = s_job.i2_mb_cnt;
2518 ps_proc->i4_mb_x = s_job.i2_mb_x;
2519 ps_proc->i4_mb_y = s_job.i2_mb_y;
2520
2521 /* init process context */
2522 ih264e_init_proc_ctxt(ps_proc);
2523
2524 /* core code all mbs enlisted under the current job */
2525 error_status |= ih264e_process(ps_proc);
2526 break;
2527
2528 case CMD_ENTROPY:
2529 ps_proc->s_entropy.i4_mb_x = s_job.i2_mb_x;
2530 ps_proc->s_entropy.i4_mb_y = s_job.i2_mb_y;
2531 ps_proc->s_entropy.i4_mb_cnt = s_job.i2_mb_cnt;
2532
2533 /* init entropy */
2534 ih264e_init_entropy_ctxt(ps_proc);
2535
2536 /* entropy code all mbs enlisted under the current job */
2537 error_status |= ih264e_entropy(ps_proc);
2538 break;
2539
2540 default:
2541 error_status |= IH264_FAIL;
2542 break;
2543 }
2544 }
2545
2546 /* send error code */
2547 ps_proc->i4_error_code = error_status;
2548 return ret;
2549}