blob: 1341dcd20d23a795321051d7124cd2c9064dfc36 [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_cavlc.c
25*
26* @brief
27* Contains all the routines to code syntax elements and residuals when entropy
28* coding chosen is CAVLC
29*
30* @author
31* ittiam
32*
33* @par List of Functions:
34* - ih264e_compute_zeroruns_and_trailingones()
35* - ih264e_write_coeff4x4_cavlc()
36* - ih264e_write_coeff8x8_cavlc()
37* - ih264e_encode_residue()
38* - ih264e_write_islice_mb()
39* - ih264e_write_pslice_mb()
40*
41* @remarks
42* None
43*
44*******************************************************************************
45*/
46
47/*****************************************************************************/
48/* File Includes */
49/*****************************************************************************/
50
51/* System include files */
52#include <stdio.h>
53#include <assert.h>
54#include <limits.h>
55
56/* User include files */
57#include "ih264e_config.h"
58#include "ih264_typedefs.h"
59#include "iv2.h"
60#include "ive2.h"
61#include "ih264_debug.h"
62#include "ih264_defs.h"
63#include "ih264e_defs.h"
64#include "ih264e_error.h"
65#include "ih264e_bitstream.h"
66#include "ime_distortion_metrics.h"
67#include "ime_structs.h"
68#include "ih264_defs.h"
69#include "ih264_error.h"
70#include "ih264_structs.h"
71#include "ih264_trans_quant_itrans_iquant.h"
72#include "ih264_inter_pred_filters.h"
73#include "ih264_mem_fns.h"
74#include "ih264_padding.h"
75#include "ih264_intra_pred_filters.h"
76#include "ih264_deblk_edge_filters.h"
77#include "irc_cntrl_param.h"
78#include "irc_frame_info_collector.h"
79#include "ih264e_rate_control.h"
80#include "ih264e_structs.h"
81#include "ih264e_encode_header.h"
82#include "ih264_cavlc_tables.h"
83#include "ih264e_cavlc.h"
84#include "ih264e_statistics.h"
85#include "ih264e_trace.h"
86
87/*****************************************************************************/
88/* Function Definitions */
89/*****************************************************************************/
90
91/**
92*******************************************************************************
93*
94* @brief
95* This function computes run of zero, number of trailing ones and sign of
96* trailing ones basing on the significant coeff map, residual block and
97* total nnz.
98*
99* @param[in] pi2_res_block
100* Pointer to residual block containing levels in scan order
101*
102* @param[in] u4_total_coeff
103* Total non-zero coefficients in that sub block
104*
105* @param[in] pu1_zero_run
106* Pointer to array to store run of zeros
107*
108* @param[in] u4_sig_coeff_map
109* significant coefficient map
110*
111* @returns u4_totzero_sign_trailone
112* Bits 0-8 contains number of trailing ones.
113* Bits 8-16 contains bitwise sign information of trailing one
114* Bits 16-24 contains total number of zeros.
115*
116* @remarks
117* None
118*
119*******************************************************************************
120*/
121static UWORD32 ih264e_compute_zeroruns_and_trailingones(WORD16 *pi2_res_block,
122 UWORD32 u4_total_coeff,
123 UWORD8 *pu1_zero_run,
124 UWORD32 u4_sig_coeff_map)
125{
126 UWORD32 i = 0;
127 UWORD32 u4_nnz_coeff = 0;
128 WORD32 i4_run = -1;
129 UWORD32 u4_sign = 0;
130 UWORD32 u4_tot_zero = 0;
131 UWORD32 u4_trailing1 = 0;
132 WORD32 i4_val;
133 UWORD32 u4_totzero_sign_trailone;
134 UWORD32 *pu4_zero_run;
135
136 pu4_zero_run = (void *)pu1_zero_run;
137 pu4_zero_run[0] = 0;
138 pu4_zero_run[1] = 0;
139 pu4_zero_run[2] = 0;
140 pu4_zero_run[3] = 0;
141
142 /* Compute Runs of zeros for all nnz coefficients except the last 3 */
143 if (u4_total_coeff > 3)
144 {
145 for (i = 0; u4_nnz_coeff < (u4_total_coeff-3); i++)
146 {
147 i4_run++;
148
149 i4_val = (u4_sig_coeff_map & 0x1);
150 u4_sig_coeff_map >>= 1;
151
152 if (i4_val != 0)
153 {
154 pu1_zero_run[u4_nnz_coeff++] = i4_run;
155 i4_run = -1;
156 }
157 }
158 }
159
160 /* Compute T1's, Signof(T1's) and Runs of zeros for the last 3 */
161 while (u4_nnz_coeff != u4_total_coeff)
162 {
163 i4_run++;
164
165 i4_val = (u4_sig_coeff_map & 0x1);
166 u4_sig_coeff_map >>= 1;
167
168 if (i4_val != 0)
169 {
170 if (pi2_res_block[u4_nnz_coeff] == 1)
171 {
172 pu1_zero_run[u4_nnz_coeff] = i4_run;
173 u4_trailing1++;
174 }
175 else
176 {
177 if (pi2_res_block[u4_nnz_coeff] == -1)
178 {
179 pu1_zero_run[u4_nnz_coeff] = i4_run;
180 u4_sign |= 1 << u4_trailing1;
181 u4_trailing1++;
182 }
183 else
184 {
185 pu1_zero_run[u4_nnz_coeff] = i4_run;
186 u4_trailing1 = 0;
187 u4_sign = 0;
188 }
189 }
190 i4_run = -1;
191 u4_nnz_coeff++;
192 }
193 i++;
194 }
195
196 u4_tot_zero = i - u4_total_coeff;
197 u4_totzero_sign_trailone = (u4_tot_zero << 16)|(u4_sign << 8)|u4_trailing1;
198
199 return (u4_totzero_sign_trailone);
200}
201
202/**
203*******************************************************************************
204*
205* @brief
206* This function generates CAVLC coded bit stream for the given residual block
207*
208* @param[in] pi2_res_block
209* Pointer to residual block containing levels in scan order
210*
211* @param[in] u4_total_coeff
212* Total non-zero coefficients in the sub block
213*
214* @param[in] u4_block_type
215* block type
216*
217* @param[in] pu1_zero_run
218* Pointer to array to store run of zeros
219*
220* @param[in] u4_nc
221* average of non zero coeff from top and left blocks (when available)
222*
223* @param[in, out] ps_bit_stream
224* structure pointing to a buffer holding output bit stream
225*
226* @param[in] u4_sig_coeff_map
227* significant coefficient map of the residual block
228*
229* @returns
230* error code
231*
232* @remarks
233* If the block type is CAVLC_CHROMA_4x4_DC, then u4_nc is non-significant
234*
235*******************************************************************************
236*/
237static IH264E_ERROR_T ih264e_write_coeff4x4_cavlc(WORD16 *pi2_res_block,
238 UWORD32 u4_total_coeff,
239 ENTROPY_BLK_TYPE u4_block_type,
240 UWORD8 *pu1_zero_run,
241 UWORD32 u4_nc,
242 bitstrm_t *ps_bit_stream,
243 UWORD32 u4_sig_coeff_map)
244{
245 IH264E_ERROR_T error_status = IH264E_SUCCESS;
246 UWORD32 u4_totzero_sign_trailone = 0;
247 UWORD32 u4_trailing_ones = 0;
248 UWORD32 u4_tot_zeros = 0;
249 UWORD32 u4_remaining_coeff = 0;
250 UWORD32 u4_sign1 = 0;
251 UWORD32 u4_max_num_coeff = 0;
252 const UWORD32 au4_max_num_nnz_coeff[] = {16, 15, 16, 4, 15};
253
254 /* validate inputs */
255 ASSERT(u4_block_type <= CAVLC_CHROMA_4x4_AC);
256
257 u4_max_num_coeff = au4_max_num_nnz_coeff[u4_block_type];
258
259 ASSERT(u4_total_coeff <= u4_max_num_coeff);
260
261 if (!u4_total_coeff)
262 {
263 UWORD32 u4_codeword = 15;
264 UWORD32 u4_codesize = 1;
265 if (u4_block_type == CAVLC_CHROMA_4x4_DC)
266 {
267 u4_codeword = 1;
268 u4_codesize = 2;
269 DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, 0);
270 ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
271 ENTROPY_TRACE("\tnumber of trailing ones ",0);
272 }
273 else
274 {
275 UWORD32 u4_vlcnum = u4_nc >> 1;
276
277 /* write coeff_token */
278 if (u4_vlcnum > 3)
279 {
280 /* Num-FLC */
281 u4_codeword = 3;
282 u4_codesize = 6;
283 }
284 else
285 {
286 /* Num-VLC 0, 1, 2 */
287 if (u4_vlcnum > 1)
288 {
289 u4_vlcnum = 2;
290 }
291 u4_codesize <<= u4_vlcnum;
292 u4_codeword >>= (4 - u4_codesize);
293 }
294
295 DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, 0, u4_nc);
296 ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
297 ENTROPY_TRACE("\tnC ",u4_nc);
298 }
299
300
301 DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
302 ENTROPY_TRACE("\tcodeword ",u4_codeword);
303 ENTROPY_TRACE("\tcodesize ",u4_codesize);
304
305 error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
306
307 return error_status;
308 }
309 else
310 {
311 /* Compute zero run, number of trailing ones and their sign. */
312 u4_totzero_sign_trailone =
313 ih264e_compute_zeroruns_and_trailingones(pi2_res_block,
314 u4_total_coeff,
315 pu1_zero_run,
316 u4_sig_coeff_map);
317 u4_trailing_ones = u4_totzero_sign_trailone & 0xFF;
318 u4_sign1 = (u4_totzero_sign_trailone >> 8)& 0xFF;
319 u4_tot_zeros = (u4_totzero_sign_trailone >> 16) & 0xFF;
320 u4_remaining_coeff = u4_total_coeff - u4_trailing_ones;
321
322 /* write coeff_token */
323 {
324 UWORD32 u4_codeword;
325 UWORD32 u4_codesize;
326 if (u4_block_type == CAVLC_CHROMA_4x4_DC)
327 {
328 u4_codeword = gu1_code_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
329 u4_codesize = gu1_size_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
330
331 DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, u4_trailing_ones);
332 ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
333 ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
334 }
335 else
336 {
337 UWORD32 u4_vlcnum = u4_nc >> 1;
338
339 if (u4_vlcnum > 3)
340 {
341 /* Num-FLC */
342 u4_codeword = ((u4_total_coeff-1) << 2 ) + u4_trailing_ones;
343 u4_codesize = 6;
344 }
345 else
346 {
347 /* Num-VLC 0, 1, 2 */
348 if (u4_vlcnum > 1)
349 {
350 u4_vlcnum = 2;
351 }
352 u4_codeword = gu1_code_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
353 u4_codesize = gu1_size_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
354 }
355
356 DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, u4_trailing_ones, u4_nc);
357 ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
358 ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
359 ENTROPY_TRACE("\tnC ",u4_nc);
360 }
361
362 DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
363 ENTROPY_TRACE("\tcodeword ",u4_codeword);
364 ENTROPY_TRACE("\tcodesize ",u4_codesize);
365
366 error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
367 }
368
369 /* write sign of trailing ones */
370 if (u4_trailing_ones)
371 {
372 DEBUG("\nT1's: %d u4_codeword, %d u4_codesize",u4_sign1, u4_trailing_ones);
373 error_status = ih264e_put_bits(ps_bit_stream, u4_sign1, u4_trailing_ones);
374 ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
375 ENTROPY_TRACE("\tsign of trailing ones ",u4_sign1);
376 }
377
378 /* write level codes */
379 if (u4_remaining_coeff)
380 {
381 WORD32 i4_level = pi2_res_block[u4_remaining_coeff-1];
382 UWORD32 u4_escape;
383 UWORD32 u4_suffix_length = 0; // Level-VLC[N]
384 UWORD32 u4_abs_level, u4_abs_level_actual = 0;
385 WORD32 i4_sign;
386 const UWORD32 u4_rndfactor[] = {0, 0, 1, 3, 7, 15, 31};
387
388 DEBUG("\n \t%d coeff,",i4_level);
389 ENTROPY_TRACE("\tcoeff ",i4_level);
390
391 if (u4_trailing_ones < 3)
392 {
393 /* If there are less than 3 T1s, then the first non-T1 level is incremented if negative (decremented if positive)*/
394 if (i4_level < 0)
395 {
396 i4_level += 1;
397 }
398 else
399 {
400 i4_level -= 1;
401 }
402
403 u4_abs_level_actual = 1;
404
405 /* Initialize VLC table (Suffix Length) to encode the level */
406 if (u4_total_coeff > 10)
407 {
408 u4_suffix_length = 1;
409 }
410 }
411
412 i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
413 u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
414
415 u4_abs_level_actual += u4_abs_level;
416
417 u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
418
419 while (1)
420 {
421 UWORD32 u4_codesize;
422 UWORD32 u4_codeword;
423 UWORD32 u4_codeval;
424
425 u4_remaining_coeff--;
426
427GATHER_CAVLC_STATS1();
428
429 {
430 u4_codeval = u4_abs_level << 1;
431 u4_codeval = u4_codeval - 2 - i4_sign;
432
433 if ((!u4_suffix_length) && (u4_escape > 7) && (u4_abs_level < 16))
434 {
435 u4_codeword = (1 << 4) + (u4_codeval - 14);
436 u4_codesize = 19;
437 }
438 else if (u4_escape > 7)
439 {
440 u4_codeword = (1 << 12) + (u4_codeval - (15 << u4_suffix_length));
441 u4_codesize = 28;
442 if (!u4_suffix_length)
443 {
444 u4_codeword -= 15;
445 }
446 }
447 else
448 {
449 u4_codeword = (1 << u4_suffix_length) + (u4_codeval & ((1 << u4_suffix_length)-1));
450 u4_codesize = (u4_codeval >> u4_suffix_length) + 1 + u4_suffix_length;
451 }
452 }
453
454 /*put the level code in bitstream*/
455 DEBUG("\nLEVEL: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
456 ENTROPY_TRACE("\tcodeword ",u4_codeword);
457 ENTROPY_TRACE("\tcodesize ",u4_codesize);
458 error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
459
460 if (u4_remaining_coeff == 0) break;
461
462 /*update suffix length for next level*/
463 if (u4_suffix_length == 0)
464 {
465 u4_suffix_length++;
466 }
467 if (u4_suffix_length < 6)
468 {
469 if (u4_abs_level_actual > gu1_threshold_vlc_level[u4_suffix_length])
470 {
471 u4_suffix_length++;
472 }
473 }
474
475 /* next level */
476 i4_level = pi2_res_block[u4_remaining_coeff-1];
477
478 DEBUG("\n \t%d coeff,",i4_level);
479 ENTROPY_TRACE("\tcoeff ",i4_level);
480
481 i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
482 u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
483
484 u4_abs_level_actual = u4_abs_level;
485
486 u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
487 }
488 }
489
490 DEBUG("\n \t %d totalzeros",u4_tot_zeros);
491 ENTROPY_TRACE("\ttotal zeros ",u4_tot_zeros);
492
493 /* Write Total Zeros */
494 if (u4_total_coeff < u4_max_num_coeff)
495 {
496 WORD32 index;
497 UWORD32 u4_codeword;
498 UWORD32 u4_codesize;
499
500 if (u4_block_type == CAVLC_CHROMA_4x4_DC)
501 {
502 UWORD8 gu1_index_zero_table_chroma[] = {0, 4, 7};
503 index = gu1_index_zero_table_chroma[u4_total_coeff-1] + u4_tot_zeros;
504 u4_codesize = gu1_size_zero_table_chroma[index];
505 u4_codeword = gu1_code_zero_table_chroma[index];
506 }
507 else
508 {
509 index = gu1_index_zero_table[u4_total_coeff-1] + u4_tot_zeros;
510 u4_codesize = gu1_size_zero_table[index];
511 u4_codeword = gu1_code_zero_table[index];
512 }
513
514 DEBUG("\nTOTAL ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
515 ENTROPY_TRACE("\tcodeword ",u4_codeword);
516 ENTROPY_TRACE("\tcodesize ",u4_codesize);
517 error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
518 }
519
520 /* Write Run Before */
521 if (u4_tot_zeros)
522 {
523 UWORD32 u4_max_num_coef = u4_total_coeff-1;
524 UWORD32 u4_codeword;
525 UWORD32 u4_codesize;
526 UWORD32 u4_zeros_left = u4_tot_zeros;
527
528 while (u4_max_num_coef)
529 {
530 UWORD32 u4_run_before = pu1_zero_run[u4_max_num_coef];
531 UWORD32 u4_index;
532
533 if (u4_zeros_left > MAX_ZERO_LEFT)
534 {
535 u4_index = gu1_index_run_table[MAX_ZERO_LEFT];
536 }
537 else
538 {
539 u4_index = gu1_index_run_table[u4_zeros_left - 1];
540 }
541
542 u4_codesize = gu1_size_run_table[u4_index + u4_run_before];
543 u4_codeword = gu1_code_run_table[u4_index + u4_run_before];
544
545 DEBUG("\nRUN BEFORE ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
546 ENTROPY_TRACE("\tcodeword ",u4_codeword);
547 ENTROPY_TRACE("\tcodesize ",u4_codesize);
548 error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
549
550 u4_zeros_left -= u4_run_before;
551 if (!u4_zeros_left)
552 {
553 break;
554 }
555 u4_max_num_coef--;
556 }
557 }
558 }
559
560 return error_status;
561}
562
563/**
564*******************************************************************************
565*
566* @brief
567* This function generates CAVLC coded bit stream for the given subblock
568*
569* @param[in] ps_ent_ctxt
570* Pointer to entropy context
571*
572* @param[in] pi2_res_block
573* Pointers to residual blocks of all the partitions for the current subblk
574* (containing levels in scan order)
575*
576* @param[in] pu1_nnz
577* Total non-zero coefficients of all the partitions for the current subblk
578*
579* @param[in] pu2_sig_coeff_map
580* Significant coefficient map of all the partitions for the current subblk
581*
582* @param[in] u4_block_type
583* entropy coding block type
584*
585* @param[in] u4_ngbr_avbl
586* top and left availability of all the partitions for the current subblk
587* (packed)
588*
589* @param[in] pu1_top_nnz
590* pointer to the buffer containing nnz of all the subblks to the top
591*
592* @param[in] pu1_left_nnz
593* pointer to the buffer containing nnz of all the subblks to the left
594*
595* @returns error status
596*
597* @remarks none
598*
599*******************************************************************************
600*/
601static IH264E_ERROR_T ih264e_write_coeff8x8_cavlc(entropy_ctxt_t *ps_ent_ctxt,
602 WORD16 **pi2_res_block,
603 UWORD8 *pu1_nnz,
604 UWORD16 *pu2_sig_coeff_map,
605 ENTROPY_BLK_TYPE u4_block_type,
606 UWORD32 u4_ngbr_avlb,
607 UWORD8 *pu1_top_nnz,
608 UWORD8 *pu1_left_nnz)
609{
610 IH264E_ERROR_T error_status = IH264E_SUCCESS;
611 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
612 UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run, *pu1_ngbr_avbl;
613 UWORD32 u4_nC;
614 UWORD8 u1_mb_a, u1_mb_b;
615
616 pu1_ngbr_avbl = (void *)(&u4_ngbr_avlb);
617
618 /* encode ac block index 4x4 = 0*/
619 u1_mb_a = pu1_ngbr_avbl[0] & 0x0F;
620 u1_mb_b = pu1_ngbr_avbl[0] & 0xF0;
621 u4_nC = 0;
622 if (u1_mb_a)
623 u4_nC += pu1_left_nnz[0];
624 if (u1_mb_b)
625 u4_nC += pu1_top_nnz[0];
626 if (u1_mb_a && u1_mb_b)
627 u4_nC = (u4_nC + 1) >> 1;
628 pu1_left_nnz[0] = pu1_top_nnz[0] = pu1_nnz[0];
629 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[0], pu1_nnz[0], u4_block_type, pu1_zero_run, u4_nC, ps_bitstream, pu2_sig_coeff_map[0]);
630
631 /* encode ac block index 4x4 = 1*/
632 u1_mb_a = pu1_ngbr_avbl[1] & 0x0F;
633 u1_mb_b = pu1_ngbr_avbl[1] & 0xF0;
634 u4_nC = 0;
635 if (u1_mb_a)
636 u4_nC += pu1_left_nnz[0];
637 if (u1_mb_b)
638 u4_nC += pu1_top_nnz[1];
639 if (u1_mb_a && u1_mb_b)
640 u4_nC = (u4_nC + 1) >> 1;
641 pu1_left_nnz[0] = pu1_top_nnz[1] = pu1_nnz[1];
642 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[1], pu1_nnz[1], u4_block_type, pu1_zero_run, u4_nC, ps_bitstream, pu2_sig_coeff_map[1]);
643
644 /* encode ac block index 4x4 = 2*/
645 u1_mb_a = pu1_ngbr_avbl[2] & 0x0F;
646 u1_mb_b = pu1_ngbr_avbl[2] & 0xF0;
647 u4_nC = 0;
648 if (u1_mb_a)
649 u4_nC += pu1_left_nnz[1];
650 if (u1_mb_b)
651 u4_nC += pu1_top_nnz[0];
652 if (u1_mb_a && u1_mb_b)
653 u4_nC = (u4_nC + 1) >> 1;
654 pu1_left_nnz[1] = pu1_top_nnz[0] = pu1_nnz[2];
655 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[2], pu1_nnz[2], u4_block_type, pu1_zero_run, u4_nC, ps_bitstream, pu2_sig_coeff_map[2]);
656
657 /* encode ac block index 4x4 = 0*/
658 u1_mb_a = pu1_ngbr_avbl[3] & 0x0F;
659 u1_mb_b = pu1_ngbr_avbl[3] & 0xF0;
660 u4_nC = 0;
661 if (u1_mb_a)
662 u4_nC += pu1_left_nnz[1];
663 if (u1_mb_b)
664 u4_nC += pu1_top_nnz[1];
665 if (u1_mb_a && u1_mb_b)
666 u4_nC = (u4_nC + 1) >> 1;
667 pu1_left_nnz[1] = pu1_top_nnz[1] = pu1_nnz[3];
668 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[3], pu1_nnz[3], u4_block_type, pu1_zero_run, u4_nC, ps_bitstream, pu2_sig_coeff_map[3]);
669
670 return error_status;
671}
672
673/**
674*******************************************************************************
675*
676* @brief
677* This function encodes luma and chroma residues of a macro block when
678* the entropy coding mode chosen is cavlc.
679*
680* @param[in] ps_ent_ctxt
681* Pointer to entropy context
682*
683* @param[in] u4_mb_type
684* current mb type
685*
686* @param[in] u4_cbp
687* coded block pattern for the current mb
688*
689* @returns error code
690*
691* @remarks none
692*
693*******************************************************************************
694*/
695static IH264E_ERROR_T ih264e_encode_residue(entropy_ctxt_t *ps_ent_ctxt,
696 UWORD32 u4_mb_type,
697 UWORD32 u4_cbp)
698{
699 /* error status */
700 IH264E_ERROR_T error_status = IH264E_SUCCESS;
701
702 /* packed residue */
703 void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
704
705 /* bit stream buffer */
706 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
707
708 /* zero run */
709 UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run;
710
711 /* temp var */
712 UWORD32 u4_nC, u4_ngbr_avlb;
713 UWORD8 au1_nnz[4], *pu1_ngbr_avlb, *pu1_top_nnz, *pu1_left_nnz;
714 UWORD16 au2_sig_coeff_map[4];
715 WORD16 *pi2_res_block[4];
716 UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
717 tu_sblk_coeff_data_t *ps_mb_coeff_data;
718 ENTROPY_BLK_TYPE e_entropy_blk_type = CAVLC_LUMA_4x4;
719
720 /* ngbr availability */
721 UWORD8 u1_mb_a, u1_mb_b;
722
723 /* cbp */
724 UWORD32 u4_cbp_luma = u4_cbp & 0xF, u4_cbp_chroma = u4_cbp >> 4;
725
726 /* mb indices */
727 WORD32 i4_mb_x, i4_mb_y;
728
729 /* derive neighbor availability */
730 i4_mb_x = ps_ent_ctxt->i4_mb_x;
731 i4_mb_y = ps_ent_ctxt->i4_mb_y;
732 pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
733 /* left macroblock availability */
734 u1_mb_a = (i4_mb_x == 0 ||
735 (pu1_slice_idx[i4_mb_x - 1 ] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
736 /* top macroblock availability */
737 u1_mb_b = (i4_mb_y == 0 ||
738 (pu1_slice_idx[i4_mb_x-ps_ent_ctxt->i4_wd_mbs] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
739
740 pu1_ngbr_avlb = (void *)(&u4_ngbr_avlb);
741 pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
742 pu1_left_nnz = (UWORD8 *)&ps_ent_ctxt->u4_left_nnz_luma;
743
744 /* encode luma residue */
745
746 /* mb type intra 16x16 */
747 if (u4_mb_type == I16x16)
748 {
749 /* parse packed coeff data structure for residual data */
750 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
751 /* estimate nnz for the current mb */
752 u4_nC = 0;
753 if (u1_mb_a)
754 u4_nC += pu1_left_nnz[0];
755 if (u1_mb_b)
756 u4_nC += pu1_top_nnz[0];
757 if (u1_mb_a && u1_mb_b)
758 u4_nC = (u4_nC + 1) >> 1;
759
760 /* encode dc block */
761 ENTROPY_TRACE("Luma DC blk idx %d",0);
762 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[0], au1_nnz[0], CAVLC_LUMA_4x4_DC, pu1_zero_run, u4_nC, ps_bitstream, au2_sig_coeff_map[0]);
763
764 e_entropy_blk_type = CAVLC_LUMA_4x4_AC;
765 }
766
767 if (u4_cbp_luma & 1)
768 {
769 /* encode ac block index 8x8 = 0*/
770 /* parse packed coeff data structure for residual data */
771 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
772 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
773 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
774 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
775 /* derive sub block neighbor availability */
776
777 pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
778 pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
779 pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
780 pu1_ngbr_avlb[3] = 0x11;
781 /* encode sub blk */
782 ENTROPY_TRACE("Luma blk idx %d",0);
783 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, e_entropy_blk_type, u4_ngbr_avlb, pu1_top_nnz, pu1_left_nnz);
784 }
785 else
786 {
787 pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
788 pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
789 }
790
791 if (u4_cbp_luma & 2)
792 {
793 /* encode ac block index 8x8 = 1*/
794 /* parse packed coeff data structure for residual data */
795 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
796 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
797 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
798 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
799
800 /* derive sub block neighbor availability */
801 pu1_ngbr_avlb[1] = pu1_ngbr_avlb[0] = (u1_mb_b << 4) | 1;
802 pu1_ngbr_avlb[3] = pu1_ngbr_avlb[2] = 0x11;
803 /* encode sub blk */
804 ENTROPY_TRACE("Luma blk idx %d",1);
805 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, e_entropy_blk_type, u4_ngbr_avlb, pu1_top_nnz+2, pu1_left_nnz);
806 }
807 else
808 {
809 (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
810 pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
811 }
812
813 if (u4_cbp_luma & 0x4)
814 {
815 /* encode ac block index 8x8 = 2*/
816 /* parse packed coeff data structure for residual data */
817 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
818 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
819 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
820 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
821
822 /* derive sub block neighbor availability */
823 pu1_ngbr_avlb[2] = pu1_ngbr_avlb[0] = (1 << 4) | u1_mb_a;
824 pu1_ngbr_avlb[1] = pu1_ngbr_avlb[3] = 0x11;
825 /* encode sub blk */
826 ENTROPY_TRACE("Luma blk idx %d",2);
827 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, e_entropy_blk_type, u4_ngbr_avlb, pu1_top_nnz, (pu1_left_nnz+2));
828 }
829 else
830 {
831 pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
832 (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
833 }
834
835 if (u4_cbp_luma & 0x8)
836 {
837 /* encode ac block index 8x8 = 3*/
838 /* parse packed coeff data structure for residual data */
839 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
840 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
841 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
842 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
843
844 /* derive sub block neighbor availability */
845 u4_ngbr_avlb = 0x11111111;
846 /* encode sub blk */
847 ENTROPY_TRACE("Luma blk idx %d",3);
848 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, e_entropy_blk_type, u4_ngbr_avlb, pu1_top_nnz+2, pu1_left_nnz+2);
849 }
850 else
851 {
852 (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
853 (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
854 }
855
856 /* encode chroma residue */
857 if (u4_cbp_chroma & 3)
858 {
859 /* parse packed coeff data structure for residual data */
860 /* cb, cr */
861 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
862 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
863
864 /* encode dc block */
865 /* cb, cr */
866 ENTROPY_TRACE("Chroma DC blk idx %d",0);
867 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[0], au1_nnz[0], CAVLC_CHROMA_4x4_DC, pu1_zero_run, 0, ps_bitstream, au2_sig_coeff_map[0]);
868 ENTROPY_TRACE("Chroma DC blk idx %d",1);
869 error_status = ih264e_write_coeff4x4_cavlc(pi2_res_block[1], au1_nnz[1], CAVLC_CHROMA_4x4_DC, pu1_zero_run, 0, ps_bitstream, au2_sig_coeff_map[1]);
870 }
871
872 pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
873 pu1_left_nnz = (UWORD8 *) &ps_ent_ctxt->u4_left_nnz_cbcr;
874
875 /* encode sub blk */
876 if (u4_cbp_chroma & 0x2)
877 {
878 /* encode ac block index 8x8 = 0*/
879 /* derive sub block neighbor availability */
880 pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
881 pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
882 pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
883 pu1_ngbr_avlb[3] = 0x11;
884
885 /* parse packed coeff data structure for residual data */
886 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
887 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
888 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
889 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
890
891 ENTROPY_TRACE("Chroma AC blk idx %d",0);
892 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, CAVLC_CHROMA_4x4_AC, u4_ngbr_avlb, pu1_top_nnz, pu1_left_nnz);
893 }
894 else
895 {
896 pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
897 pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
898 }
899
900 pu1_top_nnz += 2;
901 pu1_left_nnz += 2;
902
903 /* encode sub blk */
904 if (u4_cbp_chroma & 0x2)
905 {
906 /* parse packed coeff data structure for residual data */
907 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[0], au2_sig_coeff_map[0], pi2_res_block[0]);
908 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[1], au2_sig_coeff_map[1], pi2_res_block[1]);
909 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[2], au2_sig_coeff_map[2], pi2_res_block[2]);
910 PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, au1_nnz[3], au2_sig_coeff_map[3], pi2_res_block[3]);
911
912 ENTROPY_TRACE("Chroma AC blk idx %d",1);
913 error_status = ih264e_write_coeff8x8_cavlc(ps_ent_ctxt, pi2_res_block, au1_nnz, au2_sig_coeff_map, CAVLC_CHROMA_4x4_AC, u4_ngbr_avlb, pu1_top_nnz, pu1_left_nnz);
914 }
915 else
916 {
917 pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
918 pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
919 }
920
921 /* store the index of the next mb coeff data */
922 ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
923
924 return error_status;
925}
926
927#define GET_NUM_BITS(ps_bitstream) ((ps_bitstream->u4_strm_buf_offset << 3) + 32 - ps_bitstream->i4_bits_left_in_cw)
928
929/**
930*******************************************************************************
931*
932* @brief
933* This function generates CAVLC coded bit stream for an Intra Slice.
934*
935* @description
936* The mb syntax layer for intra slices constitutes luma mb mode, luma sub modes
937* (if present), mb qp delta, coded block pattern, chroma mb mode and
938* luma/chroma residue. These syntax elements are written as directed by table
939* 7.3.5 of h264 specification.
940*
941* @param[in] ps_ent_ctxt
942* pointer to entropy context
943*
944* @returns error code
945*
946* @remarks none
947*
948*******************************************************************************
949*/
950IH264E_ERROR_T ih264e_write_islice_mb(entropy_ctxt_t *ps_ent_ctxt)
951{
952 /* error status */
953 IH264E_ERROR_T error_status = IH264E_SUCCESS;
954
955 /* bit stream ptr */
956 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
957
958 /* packed header data */
959 UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
960
961 /* mb header info */
962 /*
963 * mb_tpm : mb type plus mode
964 * mb_type : luma mb type and chroma mb type are packed
965 * cbp : coded block pattern
966 * mb_qp_delta : mb qp delta
967 * chroma_intra_mode : chroma intra mode
968 * luma_intra_mode : luma intra mode
969 */
970 WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
971 WORD8 mb_qp_delta;
972
973 /* temp var */
974 WORD32 i, mb_type_stream;
975
976 WORD32 bitstream_start_offset, bitstream_end_offset;
977
978 /* Starting bitstream offset for header in bits */
979 bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
980
981
982 /********************************************************************/
983 /* BEGIN HEADER GENERATION */
984 /********************************************************************/
985
986 /* mb header info */
987 mb_tpm = *pu1_byte++;
988 cbp = *pu1_byte++;
989 mb_qp_delta = *pu1_byte++;
990
991 /* mb type */
992 mb_type = mb_tpm & 0xF;
993 /* is intra ? */
994 if (mb_type == I16x16)
995 {
996 UWORD32 u4_cbp_l, u4_cbp_c;
997
998 u4_cbp_c = (cbp >> 4);
999 u4_cbp_l = (cbp & 0xF);
1000 luma_intra_mode = (mb_tpm >> 4) & 3;
1001 chroma_intra_mode = (mb_tpm >> 6);
1002
1003 mb_type_stream = luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1004
1005 /* write mb type */
1006 PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1007
1008 /* intra_chroma_pred_mode */
1009 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1010 }
1011 else if (mb_type == I4x4)
1012 {
1013 /* mb sub blk modes */
1014 WORD32 intra_pred_mode_flag, rem_intra_mode;
1015 WORD32 byte;
1016
1017 chroma_intra_mode = (mb_tpm >> 6);
1018
1019 /* write mb type */
1020 PUT_BITS_UEV(ps_bitstream, 0, error_status, "mb type");
1021
1022 for (i = 0; i < 16; i += 2)
1023 {
1024 /* sub blk idx 1 */
1025 byte = *pu1_byte++;
1026
1027 intra_pred_mode_flag = byte & 0x1;
1028
1029 /* prev_intra4x4_pred_mode_flag */
1030 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1031
1032 /* rem_intra4x4_pred_mode */
1033 if (!intra_pred_mode_flag)
1034 {
1035 rem_intra_mode = (byte & 0xF) >> 1;
1036 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1037 }
1038
1039 /* sub blk idx 2 */
1040 byte >>= 4;
1041
1042 intra_pred_mode_flag = byte & 0x1;
1043
1044 /* prev_intra4x4_pred_mode_flag */
1045 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1046
1047 /* rem_intra4x4_pred_mode */
1048 if (!intra_pred_mode_flag)
1049 {
1050 rem_intra_mode = (byte & 0xF) >> 1;
1051 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1052 }
1053 }
1054
1055 /* intra_chroma_pred_mode */
1056 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1057 }
1058 else if (mb_type == I8x8)
1059 {
1060 /* transform 8x8 flag */
1061 UWORD32 u4_transform_size_8x8_flag = ps_ent_ctxt->i1_transform_8x8_mode_flag;
1062
1063 /* mb sub blk modes */
1064 WORD32 intra_pred_mode_flag, rem_intra_mode;
1065 WORD32 byte;
1066
1067 chroma_intra_mode = (mb_tpm >> 6);
1068
1069 ASSERT(0);
1070
1071 /* write mb type */
1072 PUT_BITS_UEV(ps_bitstream, 0, error_status, "mb type");
1073
1074 /* u4_transform_size_8x8_flag */
1075 PUT_BITS(ps_bitstream, u4_transform_size_8x8_flag, 1, error_status, "u4_transform_size_8x8_flag");
1076
1077 /* write sub block modes */
1078 for (i = 0; i < 4; i++)
1079 {
1080 /* sub blk idx 1 */
1081 byte = *pu1_byte++;
1082
1083 intra_pred_mode_flag = byte & 0x1;
1084
1085 /* prev_intra4x4_pred_mode_flag */
1086 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1087
1088 /* rem_intra4x4_pred_mode */
1089 if (!intra_pred_mode_flag)
1090 {
1091 rem_intra_mode = (byte & 0xF) >> 1;
1092 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1093 }
1094
1095 /* sub blk idx 2 */
1096 byte >>= 4;
1097
1098 intra_pred_mode_flag = byte & 0x1;
1099
1100 /* prev_intra4x4_pred_mode_flag */
1101 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1102
1103 /* rem_intra4x4_pred_mode */
1104 if (!intra_pred_mode_flag)
1105 {
1106 rem_intra_mode = (byte & 0xF) >> 1;
1107 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1108 }
1109 }
1110
1111 /* intra_chroma_pred_mode */
1112 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1113 }
1114 else
1115 {
1116 }
1117
1118 /* coded_block_pattern */
1119 if (mb_type != I16x16)
1120 {
1121 PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][0], error_status, "coded_block_pattern");
1122 }
1123
1124 if (cbp || mb_type == I16x16)
1125 {
1126 /* mb_qp_delta */
1127 PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1128 }
1129
1130 /* Ending bitstream offset for header in bits */
1131 bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1132
1133 ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1134
1135 /* Starting bitstream offset for residue */
1136 bitstream_start_offset = bitstream_end_offset;
1137
1138 /* residual */
1139 error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1140
1141 /* Ending bitstream offset for reside in bits */
1142 bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1143 ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset - bitstream_start_offset;
1144
1145 /* store the index of the next mb syntax layer */
1146 ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1147
1148 return error_status;
1149}
1150
1151/**
1152*******************************************************************************
1153*
1154* @brief
1155* This function generates CAVLC coded bit stream for Inter slices
1156*
1157* @description
1158* The mb syntax layer for inter slices constitutes luma mb mode, luma sub modes
1159* (if present), mb qp delta, coded block pattern, chroma mb mode and
1160* luma/chroma residue. These syntax elements are written as directed by table
1161* 7.3.5 of h264 specification
1162*
1163* @param[in] ps_ent_ctxt
1164* pointer to entropy context
1165*
1166* @returns error code
1167*
1168* @remarks none
1169*
1170*******************************************************************************
1171*/
1172IH264E_ERROR_T ih264e_write_pslice_mb(entropy_ctxt_t *ps_ent_ctxt)
1173{
1174 /* error status */
1175 IH264E_ERROR_T error_status = IH264E_SUCCESS;
1176
1177 /* bit stream ptr */
1178 bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1179
1180 /* packed header data */
1181 UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1182
1183 /* mb header info */
1184 /*
1185 * mb_tpm : mb type plus mode
1186 * mb_type : luma mb type and chroma mb type are packed
1187 * cbp : coded block pattern
1188 * mb_qp_delta : mb qp delta
1189 * chroma_intra_mode : chroma intra mode
1190 * luma_intra_mode : luma intra mode
1191 * ps_pu : Pointer to the array of structures having motion vectors, size
1192 * and position of sub partitions
1193 */
1194 WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
1195 WORD8 mb_qp_delta;
1196
1197 /* temp var */
1198 WORD32 i, mb_type_stream, cbptable = 1;
1199
1200 WORD32 is_inter = 0;
1201
1202 WORD32 bitstream_start_offset, bitstream_end_offset;
1203
1204 /* Starting bitstream offset for header in bits */
1205 bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
1206
1207 /********************************************************************/
1208 /* BEGIN HEADER GENERATION */
1209 /********************************************************************/
1210
1211 /* mb header info */
1212 mb_tpm = *pu1_byte++;
1213
1214 /* mb type */
1215 mb_type = mb_tpm & 0xF;
1216
1217 /* check for skip */
1218 if (mb_type == PSKIP)
1219 {
1220 UWORD32 *nnz;
1221
1222 is_inter = 1;
1223
1224 /* increment skip counter */
1225 (*ps_ent_ctxt->pi4_mb_skip_run)++;
1226
1227 /* store the index of the next mb syntax layer */
1228 ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1229
1230 /* set nnz to zero */
1231 ps_ent_ctxt->u4_left_nnz_luma = 0;
1232 nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
1233 *nnz = 0;
1234 ps_ent_ctxt->u4_left_nnz_cbcr = 0;
1235 nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
1236 *nnz = 0;
1237
1238 /* residual */
1239 error_status = ih264e_encode_residue(ps_ent_ctxt, P16x16, 0);
1240
1241 bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1242
1243 ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1244
1245 return error_status;
1246 }
1247
1248 /* remaining mb header info */
1249 cbp = *pu1_byte++;
1250 mb_qp_delta = *pu1_byte++;
1251
1252 /* mb skip run */
1253 PUT_BITS_UEV(ps_bitstream, *ps_ent_ctxt->pi4_mb_skip_run, error_status, "mb skip run");
1254
1255 /* reset skip counter */
1256 *ps_ent_ctxt->pi4_mb_skip_run = 0;
1257
1258 /* is intra ? */
1259 if (mb_type == I16x16)
1260 {
1261 UWORD32 u4_cbp_l, u4_cbp_c;
1262
1263 is_inter = 0;
1264
1265 u4_cbp_c = (cbp >> 4);
1266 u4_cbp_l = (cbp & 0xF);
1267 luma_intra_mode = (mb_tpm >> 4) & 3;
1268 chroma_intra_mode = (mb_tpm >> 6);
1269
1270 mb_type_stream = luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1271
1272 mb_type_stream += 5;
1273
1274 /* write mb type */
1275 PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1276
1277 /* intra_chroma_pred_mode */
1278 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1279 }
1280 else if (mb_type == I4x4)
1281 {
1282 /* mb sub blk modes */
1283 WORD32 intra_pred_mode_flag, rem_intra_mode;
1284 WORD32 byte;
1285
1286 is_inter = 0;
1287
1288 chroma_intra_mode = (mb_tpm >> 6);
1289 cbptable = 0;
1290
1291 /* write mb type */
1292 PUT_BITS_UEV(ps_bitstream, 5, error_status, "mb type");
1293
1294 for (i = 0; i < 16; i += 2)
1295 {
1296 /* sub blk idx 1 */
1297 byte = *pu1_byte++;
1298
1299 intra_pred_mode_flag = byte & 0x1;
1300
1301 /* prev_intra4x4_pred_mode_flag */
1302 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1303
1304 /* rem_intra4x4_pred_mode */
1305 if (!intra_pred_mode_flag)
1306 {
1307 rem_intra_mode = (byte & 0xF) >> 1;
1308 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1309 }
1310
1311 /* sub blk idx 2 */
1312 byte >>= 4;
1313
1314 intra_pred_mode_flag = byte & 0x1;
1315
1316 /* prev_intra4x4_pred_mode_flag */
1317 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1318
1319 /* rem_intra4x4_pred_mode */
1320 if (!intra_pred_mode_flag)
1321 {
1322 rem_intra_mode = (byte & 0xF) >> 1;
1323 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1324 }
1325 }
1326
1327 /* intra_chroma_pred_mode */
1328 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1329 }
1330 else if (mb_type == I8x8)
1331 {
1332 /* transform 8x8 flag */
1333 UWORD32 u4_transform_size_8x8_flag = ps_ent_ctxt->i1_transform_8x8_mode_flag;
1334
1335 /* mb sub blk modes */
1336 WORD32 intra_pred_mode_flag, rem_intra_mode;
1337 WORD32 byte;
1338
1339 is_inter = 0;
1340
1341 chroma_intra_mode = (mb_tpm >> 6);
1342 cbptable = 0;
1343
1344 ASSERT(0);
1345
1346 /* write mb type */
1347 PUT_BITS_UEV(ps_bitstream, 5, error_status, "mb type");
1348
1349 /* u4_transform_size_8x8_flag */
1350 PUT_BITS(ps_bitstream, u4_transform_size_8x8_flag, 1, error_status, "u4_transform_size_8x8_flag");
1351
1352 /* write sub block modes */
1353 for (i = 0; i < 4; i++)
1354 {
1355 /* sub blk idx 1 */
1356 byte = *pu1_byte++;
1357
1358 intra_pred_mode_flag = byte & 0x1;
1359
1360 /* prev_intra4x4_pred_mode_flag */
1361 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1362
1363 /* rem_intra4x4_pred_mode */
1364 if (!intra_pred_mode_flag)
1365 {
1366 rem_intra_mode = (byte & 0xF) >> 1;
1367 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1368 }
1369
1370 /* sub blk idx 2 */
1371 byte >>= 4;
1372
1373 intra_pred_mode_flag = byte & 0x1;
1374
1375 /* prev_intra4x4_pred_mode_flag */
1376 PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1377
1378 /* rem_intra4x4_pred_mode */
1379 if (!intra_pred_mode_flag)
1380 {
1381 rem_intra_mode = (byte & 0xF) >> 1;
1382 PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1383 }
1384 }
1385
1386 /* intra_chroma_pred_mode */
1387 PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1388 }
1389 else
1390 {
1391 /* inter macro block partition cnt */
1392 const UWORD8 au1_part_cnt[] = { 1, 2, 2, 4 };
1393
1394 /* mv ptr */
1395 WORD16 *pi2_mv_ptr = (WORD16 *)pu1_byte;
1396
1397 /* number of partitions for the current mb */
1398 UWORD32 u4_part_cnt = au1_part_cnt[mb_type - 3];
1399
1400 is_inter = 1;
1401
1402 /* write mb type */
1403 PUT_BITS_UEV(ps_bitstream, mb_type - 3, error_status, "mb type");
1404
1405 for (i = 0; i < (WORD32)u4_part_cnt; i++)
1406 {
1407 PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv x");
1408
1409 PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv y");
1410 }
1411
1412 pu1_byte = (UWORD8 *)pi2_mv_ptr;
1413 }
1414
1415 /* coded_block_pattern */
1416 if (mb_type != I16x16)
1417 {
1418 PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][cbptable], error_status, "coded_block_pattern");
1419 }
1420
1421 if (cbp || mb_type == I16x16)
1422 {
1423 /* mb_qp_delta */
1424 PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1425 }
1426
1427
1428 /* Ending bitstream offset for header in bits */
1429 bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1430
1431 ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1432
1433 /* start bitstream offset for residue in bits */
1434 bitstream_start_offset = bitstream_end_offset;
1435
1436 /* residual */
1437 error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1438
1439 /* Ending bitstream offset for residue in bits */
1440 bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1441
1442 ps_ent_ctxt->u4_residue_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1443
1444 /* store the index of the next mb syntax layer */
1445 ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1446
1447 return error_status;
1448}