Coverage Report

Created: 2025-10-28 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavc/encoder/ih264e_cavlc.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/**
22
*******************************************************************************
23
* @file
24
*  ih264e_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_cavlc
39
*  - ih264e_write_pslice_mb_cavlc
40
*  - ih264e_write_bslice_mb_cavlc
41
*
42
* @remarks
43
*  none
44
*
45
*******************************************************************************
46
*/
47
48
/*****************************************************************************/
49
/* File Includes                                                             */
50
/*****************************************************************************/
51
52
/* System Include Files */
53
#include <stdio.h>
54
#include <assert.h>
55
#include <limits.h>
56
57
/* User Include Files */
58
#include "ih264e_config.h"
59
#include "ih264_typedefs.h"
60
#include "iv2.h"
61
#include "ive2.h"
62
63
#include "ih264_debug.h"
64
#include "ih264_macros.h"
65
#include "ih264_error.h"
66
#include "ih264_defs.h"
67
#include "ih264_mem_fns.h"
68
#include "ih264_padding.h"
69
#include "ih264_structs.h"
70
#include "ih264_trans_quant_itrans_iquant.h"
71
#include "ih264_inter_pred_filters.h"
72
#include "ih264_intra_pred_filters.h"
73
#include "ih264_deblk_edge_filters.h"
74
#include "ih264_cavlc_tables.h"
75
#include "ih264_cabac_tables.h"
76
77
#include "ime_defs.h"
78
#include "ime_distortion_metrics.h"
79
#include "ime_structs.h"
80
81
#include "irc_cntrl_param.h"
82
#include "irc_frame_info_collector.h"
83
84
#include "ih264e_error.h"
85
#include "ih264e_defs.h"
86
#include "ih264e_bitstream.h"
87
#include "ih264e_cabac_structs.h"
88
#include "ih264e_structs.h"
89
#include "ih264e_encode_header.h"
90
#include "ih264e_cavlc.h"
91
#include "ih264e_statistics.h"
92
#include "ih264e_trace.h"
93
94
/*****************************************************************************/
95
/* Function Definitions                                                      */
96
/*****************************************************************************/
97
98
/**
99
*******************************************************************************
100
*
101
* @brief
102
*  This function computes run of zero, number of trailing ones and sign of
103
*  trailing ones basing on the significant coeff map, residual block and
104
*  total nnz.
105
*
106
* @param[in] pi2_res_block
107
*  Pointer to residual block containing levels in scan order
108
*
109
* @param[in] u4_total_coeff
110
*  Total non-zero coefficients in that sub block
111
*
112
* @param[in] pu1_zero_run
113
*  Pointer to array to store run of zeros
114
*
115
* @param[in] u4_sig_coeff_map
116
*  significant coefficient map
117
*
118
* @returns u4_totzero_sign_trailone
119
*  Bits 0-8 contains number of trailing ones.
120
*  Bits 8-16 contains bitwise sign information of trailing one
121
*  Bits 16-24 contains total number of zeros.
122
*
123
* @remarks
124
*  none
125
*
126
*******************************************************************************
127
*/
128
static UWORD32 ih264e_compute_zeroruns_and_trailingones(WORD16 *pi2_res_block,
129
                                                        UWORD32 u4_total_coeff,
130
                                                        UWORD8 *pu1_zero_run,
131
                                                        UWORD32 u4_sig_coeff_map)
132
1.24M
{
133
1.24M
    UWORD32 i = 0;
134
1.24M
    UWORD32 u4_nnz_coeff = 0;
135
1.24M
    WORD32  i4_run = -1;
136
1.24M
    UWORD32 u4_sign = 0;
137
1.24M
    UWORD32 u4_tot_zero = 0;
138
1.24M
    UWORD32 u4_trailing1 = 0;
139
1.24M
    WORD32 i4_val;
140
1.24M
    UWORD32 u4_totzero_sign_trailone;
141
1.24M
    UWORD32 *pu4_zero_run;
142
143
1.24M
    pu4_zero_run = (void *)pu1_zero_run;
144
1.24M
    pu4_zero_run[0] = 0;
145
1.24M
    pu4_zero_run[1] = 0;
146
1.24M
    pu4_zero_run[2] = 0;
147
1.24M
    pu4_zero_run[3] = 0;
148
149
    /* Compute Runs of zeros for all nnz coefficients except the last 3 */
150
1.24M
    if (u4_total_coeff > 3)
151
836k
    {
152
9.47M
        for (i = 0; u4_nnz_coeff < (u4_total_coeff-3); i++)
153
8.64M
        {
154
8.64M
            i4_run++;
155
156
8.64M
            i4_val = (u4_sig_coeff_map & 0x1);
157
8.64M
            u4_sig_coeff_map >>= 1;
158
159
8.64M
            if (i4_val != 0)
160
8.04M
            {
161
8.04M
                pu1_zero_run[u4_nnz_coeff++] = i4_run;
162
8.04M
                i4_run = -1;
163
8.04M
            }
164
8.64M
        }
165
836k
    }
166
167
    /* Compute T1's, Signof(T1's) and Runs of zeros for the last 3 */
168
6.57M
    while (u4_nnz_coeff != u4_total_coeff)
169
5.32M
    {
170
5.32M
        i4_run++;
171
172
5.32M
        i4_val = (u4_sig_coeff_map & 0x1);
173
5.32M
        u4_sig_coeff_map >>= 1;
174
175
5.32M
        if (i4_val != 0)
176
3.35M
        {
177
3.35M
            if (pi2_res_block[u4_nnz_coeff] == 1)
178
342k
            {
179
342k
                pu1_zero_run[u4_nnz_coeff] = i4_run;
180
342k
                u4_trailing1++;
181
342k
            }
182
3.00M
            else
183
3.00M
            {
184
3.00M
                if (pi2_res_block[u4_nnz_coeff] == -1)
185
384k
                {
186
384k
                    pu1_zero_run[u4_nnz_coeff] = i4_run;
187
384k
                    u4_sign |= 1 << u4_trailing1;
188
384k
                    u4_trailing1++;
189
384k
                }
190
2.62M
                else
191
2.62M
                {
192
2.62M
                    pu1_zero_run[u4_nnz_coeff] = i4_run;
193
2.62M
                    u4_trailing1 = 0;
194
2.62M
                    u4_sign = 0;
195
2.62M
                }
196
3.00M
            }
197
3.35M
            i4_run = -1;
198
3.35M
            u4_nnz_coeff++;
199
3.35M
        }
200
5.32M
        i++;
201
5.32M
    }
202
203
1.24M
    u4_tot_zero = i - u4_total_coeff;
204
1.24M
    u4_totzero_sign_trailone = (u4_tot_zero << 16)|(u4_sign << 8)|u4_trailing1;
205
206
1.24M
    return (u4_totzero_sign_trailone);
207
1.24M
}
208
209
/**
210
*******************************************************************************
211
*
212
* @brief
213
*  This function generates CAVLC coded bit stream for the given residual block
214
*
215
* @param[in] pi2_res_block
216
*  Pointer to residual block containing levels in scan order
217
*
218
* @param[in] u4_total_coeff
219
*  Total non-zero coefficients in the sub block
220
*
221
* @param[in] u4_block_type
222
*  block type
223
*
224
* @param[in] pu1_zero_run
225
*  Pointer to array to store run of zeros
226
*
227
* @param[in] u4_nc
228
*  average of non zero coeff from top and left blocks (when available)
229
*
230
* @param[in, out] ps_bit_stream
231
*  structure pointing to a buffer holding output bit stream
232
*
233
* @param[in] u4_sig_coeff_map
234
*  significant coefficient map of the residual block
235
*
236
* @returns
237
*  error code
238
*
239
* @remarks
240
*  If the block type is CAVLC_CHROMA_4x4_DC, then u4_nc is non-significant
241
*
242
*******************************************************************************
243
*/
244
static IH264E_ERROR_T ih264e_write_coeff4x4_cavlc(WORD16 *pi2_res_block,
245
                                                  UWORD32 u4_total_coeff,
246
                                                  ENTROPY_BLK_TYPE u4_block_type,
247
                                                  UWORD8 *pu1_zero_run,
248
                                                  UWORD32 u4_nc,
249
                                                  bitstrm_t *ps_bit_stream,
250
                                                  UWORD32 u4_sig_coeff_map)
251
3.74M
{
252
3.74M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
253
3.74M
    UWORD32 u4_totzero_sign_trailone = 0;
254
3.74M
    UWORD32 u4_trailing_ones = 0;
255
3.74M
    UWORD32 u4_tot_zeros = 0;
256
3.74M
    UWORD32 u4_remaining_coeff = 0;
257
3.74M
    UWORD32 u4_sign1 = 0;
258
3.74M
    UWORD32 u4_max_num_coeff = 0;
259
3.74M
    const UWORD32 au4_max_num_nnz_coeff[] = {16, 15, 16, 4, 15};
260
261
    /* validate inputs */
262
3.74M
    ASSERT(u4_block_type <= CAVLC_CHROMA_4x4_AC);
263
264
3.74M
    u4_max_num_coeff = au4_max_num_nnz_coeff[u4_block_type];
265
266
3.74M
    ASSERT(u4_total_coeff <= u4_max_num_coeff);
267
268
3.74M
    if (!u4_total_coeff)
269
2.50M
    {
270
2.50M
        UWORD32 u4_codeword = 15;
271
2.50M
        UWORD32 u4_codesize = 1;
272
2.50M
        if (u4_block_type == CAVLC_CHROMA_4x4_DC)
273
9.82k
        {
274
9.82k
            u4_codeword = 1;
275
9.82k
            u4_codesize = 2;
276
9.82k
            DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, 0);
277
9.82k
            ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
278
9.82k
            ENTROPY_TRACE("\tnumber of trailing ones ",0);
279
9.82k
        }
280
2.49M
        else
281
2.49M
        {
282
2.49M
            UWORD32 u4_vlcnum = u4_nc >> 1;
283
284
            /* write coeff_token */
285
2.49M
            if (u4_vlcnum > 3)
286
63.6k
            {
287
                /* Num-FLC */
288
63.6k
                u4_codeword = 3;
289
63.6k
                u4_codesize = 6;
290
63.6k
            }
291
2.42M
            else
292
2.42M
            {
293
                /* Num-VLC 0, 1, 2 */
294
2.42M
                if (u4_vlcnum > 1)
295
28.9k
                {
296
28.9k
                    u4_vlcnum = 2;
297
28.9k
                }
298
2.42M
                u4_codesize <<= u4_vlcnum;
299
2.42M
                u4_codeword >>= (4 - u4_codesize);
300
2.42M
            }
301
302
2.49M
            DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, 0, u4_nc);
303
2.49M
            ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
304
2.49M
            ENTROPY_TRACE("\tnC ",u4_nc);
305
2.49M
        }
306
307
308
2.50M
        DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
309
2.50M
        ENTROPY_TRACE("\tcodeword ",u4_codeword);
310
2.50M
        ENTROPY_TRACE("\tcodesize ",u4_codesize);
311
312
2.50M
        error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
313
314
2.50M
        return error_status;
315
2.50M
    }
316
1.24M
    else
317
1.24M
    {
318
        /* Compute zero run, number of trailing ones and their sign. */
319
1.24M
        u4_totzero_sign_trailone =
320
1.24M
                ih264e_compute_zeroruns_and_trailingones(pi2_res_block,
321
1.24M
                        u4_total_coeff,
322
1.24M
                        pu1_zero_run,
323
1.24M
                        u4_sig_coeff_map);
324
1.24M
        u4_trailing_ones = u4_totzero_sign_trailone & 0xFF;
325
1.24M
        u4_sign1 = (u4_totzero_sign_trailone >> 8)& 0xFF;
326
1.24M
        u4_tot_zeros = (u4_totzero_sign_trailone >> 16) & 0xFF;
327
1.24M
        u4_remaining_coeff = u4_total_coeff - u4_trailing_ones;
328
329
        /* write coeff_token */
330
1.24M
        {
331
1.24M
            UWORD32 u4_codeword;
332
1.24M
            UWORD32 u4_codesize;
333
1.24M
            if (u4_block_type == CAVLC_CHROMA_4x4_DC)
334
126k
            {
335
126k
                u4_codeword = gu1_code_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
336
126k
                u4_codesize = gu1_size_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
337
338
126k
                DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, u4_trailing_ones);
339
126k
                ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
340
126k
                ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
341
126k
            }
342
1.11M
            else
343
1.11M
            {
344
1.11M
                UWORD32 u4_vlcnum = u4_nc >> 1;
345
346
1.11M
                if (u4_vlcnum > 3)
347
690k
                {
348
                    /* Num-FLC */
349
690k
                    u4_codeword = ((u4_total_coeff-1) << 2 ) + u4_trailing_ones;
350
690k
                    u4_codesize = 6;
351
690k
                }
352
425k
                else
353
425k
                {
354
                    /* Num-VLC 0, 1, 2 */
355
425k
                    if (u4_vlcnum > 1)
356
104k
                    {
357
104k
                        u4_vlcnum = 2;
358
104k
                    }
359
425k
                    u4_codeword = gu1_code_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
360
425k
                    u4_codesize = gu1_size_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
361
425k
                }
362
363
1.11M
                DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, u4_trailing_ones, u4_nc);
364
1.11M
                ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
365
1.11M
                ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
366
1.11M
                ENTROPY_TRACE("\tnC ",u4_nc);
367
1.11M
            }
368
369
1.24M
            DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
370
1.24M
            ENTROPY_TRACE("\tcodeword ",u4_codeword);
371
1.24M
            ENTROPY_TRACE("\tcodesize ",u4_codesize);
372
373
1.24M
            error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
374
1.24M
        }
375
376
        /* write sign of trailing ones */
377
1.24M
        if (u4_trailing_ones)
378
328k
        {
379
328k
            DEBUG("\nT1's: %d u4_codeword, %d u4_codesize",u4_sign1, u4_trailing_ones);
380
328k
            error_status = ih264e_put_bits(ps_bit_stream, u4_sign1, u4_trailing_ones);
381
328k
            ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
382
328k
            ENTROPY_TRACE("\tsign of trailing ones ",u4_sign1);
383
328k
        }
384
385
        /* write level codes */
386
1.24M
        if (u4_remaining_coeff)
387
1.11M
        {
388
1.11M
            WORD32 i4_level = pi2_res_block[u4_remaining_coeff-1];
389
1.11M
            UWORD32 u4_escape;
390
1.11M
            UWORD32 u4_suffix_length = 0; // Level-VLC[N]
391
1.11M
            UWORD32 u4_abs_level, u4_abs_level_actual = 0;
392
1.11M
            WORD32 i4_sign;
393
1.11M
            const UWORD32 u4_rndfactor[] = {0, 0, 1, 3, 7, 15, 31};
394
395
1.11M
            DEBUG("\n \t%d coeff,",i4_level);
396
1.11M
            ENTROPY_TRACE("\tcoeff ",i4_level);
397
398
1.11M
            if (u4_trailing_ones < 3)
399
1.04M
            {
400
                /* If there are less than 3 T1s, then the first non-T1 level is incremented if negative (decremented if positive)*/
401
1.04M
                if (i4_level < 0)
402
535k
                {
403
535k
                    i4_level += 1;
404
535k
                }
405
505k
                else
406
505k
                {
407
505k
                    i4_level -= 1;
408
505k
                }
409
410
1.04M
                u4_abs_level_actual = 1;
411
412
                /* Initialize VLC table (Suffix Length) to encode the level */
413
1.04M
                if (u4_total_coeff > 10)
414
609k
                {
415
609k
                    u4_suffix_length = 1;
416
609k
                }
417
1.04M
            }
418
419
1.11M
            i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
420
1.11M
            u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
421
422
1.11M
            u4_abs_level_actual += u4_abs_level;
423
424
1.11M
            u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
425
426
10.8M
            while (1)
427
10.8M
            {
428
10.8M
                WORD32 i4_codesize;
429
10.8M
                WORD32 i4_codeword;
430
10.8M
                WORD32 i4_codeval;
431
432
10.8M
                u4_remaining_coeff--;
433
434
10.8M
GATHER_CAVLC_STATS1();
435
436
10.8M
                {
437
10.8M
                    i4_codeval = u4_abs_level << 1;
438
10.8M
                    i4_codeval = i4_codeval - 2 - i4_sign;
439
440
10.8M
                    if ((!u4_suffix_length) && (u4_escape > 7) && (u4_abs_level < 16))
441
49.6k
                    {
442
49.6k
                        i4_codeword = (1 << 4) + (i4_codeval - 14);
443
49.6k
                        i4_codesize = 19;
444
49.6k
                    }
445
10.7M
                    else if (u4_escape > 7)
446
1.05M
                    {
447
1.05M
                        i4_codeword = (1 << 12) + (i4_codeval - (15 << u4_suffix_length));
448
1.05M
                        i4_codesize = 28;
449
1.05M
                        if (!u4_suffix_length)
450
145k
                        {
451
145k
                            i4_codeword -= 15;
452
145k
                        }
453
1.05M
                    }
454
9.71M
                    else
455
9.71M
                    {
456
9.71M
                        i4_codeword = (1 << u4_suffix_length) + (i4_codeval & ((1 << u4_suffix_length)-1));
457
9.71M
                        i4_codesize = (i4_codeval >> u4_suffix_length) + 1 + u4_suffix_length;
458
9.71M
                    }
459
10.8M
                }
460
461
                /*put the level code in bitstream*/
462
10.8M
                DEBUG("\nLEVEL: %d i4_codeword, %d i4_codesize",i4_codeword, i4_codesize);
463
10.8M
                ENTROPY_TRACE("\tcodeword ",i4_codeword);
464
10.8M
                ENTROPY_TRACE("\tcodesize ",i4_codesize);
465
10.8M
                error_status = ih264e_put_bits(ps_bit_stream, i4_codeword, i4_codesize);
466
467
10.8M
                if (u4_remaining_coeff == 0) break;
468
469
                /*update suffix length for next level*/
470
9.70M
                if (u4_suffix_length == 0)
471
407k
                {
472
407k
                    u4_suffix_length++;
473
407k
                }
474
9.70M
                if (u4_suffix_length < 6)
475
7.17M
                {
476
7.17M
                    if (u4_abs_level_actual > gu1_threshold_vlc_level[u4_suffix_length])
477
2.99M
                    {
478
2.99M
                        u4_suffix_length++;
479
2.99M
                    }
480
7.17M
                }
481
482
                /* next level */
483
9.70M
                i4_level      = pi2_res_block[u4_remaining_coeff-1];
484
485
9.70M
                DEBUG("\n \t%d coeff,",i4_level);
486
9.70M
                ENTROPY_TRACE("\tcoeff ",i4_level);
487
488
9.70M
                i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
489
9.70M
                u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
490
491
9.70M
                u4_abs_level_actual = u4_abs_level;
492
493
9.70M
                u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
494
9.70M
            }
495
1.11M
        }
496
497
1.24M
        DEBUG("\n \t %d totalzeros",u4_tot_zeros);
498
1.24M
        ENTROPY_TRACE("\ttotal zeros ",u4_tot_zeros);
499
500
        /* Write Total Zeros */
501
1.24M
        if (u4_total_coeff < u4_max_num_coeff)
502
772k
        {
503
772k
            WORD32 index;
504
772k
            UWORD32 u4_codeword;
505
772k
            UWORD32 u4_codesize;
506
507
772k
            if (u4_block_type == CAVLC_CHROMA_4x4_DC)
508
75.4k
            {
509
75.4k
                UWORD8 gu1_index_zero_table_chroma[] = {0, 4, 7};
510
75.4k
                index = gu1_index_zero_table_chroma[u4_total_coeff-1] + u4_tot_zeros;
511
75.4k
                u4_codesize = gu1_size_zero_table_chroma[index];
512
75.4k
                u4_codeword = gu1_code_zero_table_chroma[index];
513
75.4k
            }
514
696k
            else
515
696k
            {
516
696k
                index = gu1_index_zero_table[u4_total_coeff-1] + u4_tot_zeros;
517
696k
                u4_codesize = gu1_size_zero_table[index];
518
696k
                u4_codeword = gu1_code_zero_table[index];
519
696k
            }
520
521
772k
            DEBUG("\nTOTAL ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
522
772k
            ENTROPY_TRACE("\tcodeword ",u4_codeword);
523
772k
            ENTROPY_TRACE("\tcodesize ",u4_codesize);
524
772k
            error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
525
772k
        }
526
527
        /* Write Run Before */
528
1.24M
        if (u4_tot_zeros)
529
634k
        {
530
634k
            UWORD32 u4_max_num_coef = u4_total_coeff-1;
531
634k
            UWORD32 u4_codeword;
532
634k
            UWORD32 u4_codesize;
533
634k
            UWORD32 u4_zeros_left = u4_tot_zeros;
534
535
2.86M
            while (u4_max_num_coef)
536
2.65M
            {
537
2.65M
                UWORD32 u4_run_before = pu1_zero_run[u4_max_num_coef];
538
2.65M
                UWORD32 u4_index;
539
540
2.65M
                if (u4_zeros_left > MAX_ZERO_LEFT)
541
131k
                {
542
131k
                    u4_index = gu1_index_run_table[MAX_ZERO_LEFT];
543
131k
                }
544
2.52M
                else
545
2.52M
                {
546
2.52M
                    u4_index = gu1_index_run_table[u4_zeros_left - 1];
547
2.52M
                }
548
549
2.65M
                u4_codesize = gu1_size_run_table[u4_index + u4_run_before];
550
2.65M
                u4_codeword = gu1_code_run_table[u4_index + u4_run_before];
551
552
2.65M
                DEBUG("\nRUN BEFORE ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
553
2.65M
                ENTROPY_TRACE("\tcodeword ",u4_codeword);
554
2.65M
                ENTROPY_TRACE("\tcodesize ",u4_codesize);
555
2.65M
                error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
556
557
2.65M
                u4_zeros_left -= u4_run_before;
558
2.65M
                if (!u4_zeros_left)
559
432k
                {
560
432k
                    break;
561
432k
                }
562
2.22M
                u4_max_num_coef--;
563
2.22M
            }
564
634k
        }
565
1.24M
    }
566
567
1.24M
    return error_status;
568
3.74M
}
569
570
/**
571
*******************************************************************************
572
*
573
* @brief
574
*  This function generates CAVLC coded bit stream for the given subblock
575
*
576
* @param[in] ps_ent_ctxt
577
*  Pointer to entropy context
578
*
579
* @param[in] pi2_res_block
580
*  Pointers to residual blocks of all the partitions for the current subblk
581
*  (containing levels in scan order)
582
*
583
* @param[in] pu1_nnz
584
*  Total non-zero coefficients of all the partitions for the current subblk
585
*
586
* @param[in] pu2_sig_coeff_map
587
*  Significant coefficient map of all the partitions for the current subblk
588
*
589
* @param[in] u4_block_type
590
*  entropy coding block type
591
*
592
* @param[in] u4_ngbr_avbl
593
*  top and left availability of all the partitions for the current subblk
594
*  (packed)
595
*
596
* @param[in] pu1_top_nnz
597
*  pointer to the buffer containing nnz of all the subblks to the top
598
*
599
* @param[in] pu1_left_nnz
600
*  pointer to the buffer containing nnz of all the subblks to the left
601
*
602
* @returns error status
603
*
604
* @remarks none
605
*
606
*******************************************************************************
607
*/
608
static IH264E_ERROR_T ih264e_write_coeff8x8_cavlc(entropy_ctxt_t *ps_ent_ctxt,
609
                                                  WORD16 **pi2_res_block,
610
                                                  UWORD8 *pu1_nnz,
611
                                                  UWORD16 *pu2_sig_coeff_map,
612
                                                  ENTROPY_BLK_TYPE u4_block_type,
613
                                                  UWORD32 u4_ngbr_avlb,
614
                                                  UWORD8 *pu1_top_nnz,
615
                                                  UWORD8 *pu1_left_nnz)
616
351k
{
617
351k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
618
351k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
619
351k
    UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run, *pu1_ngbr_avbl;
620
351k
    UWORD32 u4_nC;
621
351k
    UWORD8 u1_mb_a, u1_mb_b;
622
623
351k
    pu1_ngbr_avbl = (void *)(&u4_ngbr_avlb);
624
625
    /* encode ac block index 4x4 = 0*/
626
351k
    u1_mb_a = pu1_ngbr_avbl[0] & 0x0F;
627
351k
    u1_mb_b = pu1_ngbr_avbl[0] & 0xF0;
628
351k
    u4_nC = 0;
629
351k
    if (u1_mb_a)
630
216k
        u4_nC += pu1_left_nnz[0];
631
351k
    if (u1_mb_b)
632
211k
        u4_nC += pu1_top_nnz[0];
633
351k
    if (u1_mb_a && u1_mb_b)
634
139k
        u4_nC = (u4_nC + 1) >> 1;
635
351k
    pu1_left_nnz[0] = pu1_top_nnz[0] = pu1_nnz[0];
636
351k
    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]);
637
638
    /* encode ac block index 4x4 = 1*/
639
351k
    u1_mb_a = pu1_ngbr_avbl[1] & 0x0F;
640
351k
    u1_mb_b = pu1_ngbr_avbl[1] & 0xF0;
641
351k
    u4_nC = 0;
642
351k
    if (u1_mb_a)
643
351k
        u4_nC += pu1_left_nnz[0];
644
351k
    if (u1_mb_b)
645
211k
        u4_nC += pu1_top_nnz[1];
646
351k
    if (u1_mb_a && u1_mb_b)
647
211k
        u4_nC = (u4_nC + 1) >> 1;
648
351k
    pu1_left_nnz[0] = pu1_top_nnz[1] = pu1_nnz[1];
649
351k
    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]);
650
651
    /* encode ac block index 4x4 = 2*/
652
351k
    u1_mb_a = pu1_ngbr_avbl[2] & 0x0F;
653
351k
    u1_mb_b = pu1_ngbr_avbl[2] & 0xF0;
654
351k
    u4_nC = 0;
655
351k
    if (u1_mb_a)
656
216k
        u4_nC += pu1_left_nnz[1];
657
351k
    if (u1_mb_b)
658
351k
        u4_nC += pu1_top_nnz[0];
659
351k
    if (u1_mb_a && u1_mb_b)
660
216k
        u4_nC = (u4_nC + 1) >> 1;
661
351k
    pu1_left_nnz[1] = pu1_top_nnz[0] = pu1_nnz[2];
662
351k
    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]);
663
664
    /* encode ac block index 4x4 = 0*/
665
351k
    u1_mb_a = pu1_ngbr_avbl[3] & 0x0F;
666
351k
    u1_mb_b = pu1_ngbr_avbl[3] & 0xF0;
667
351k
    u4_nC = 0;
668
351k
    if (u1_mb_a)
669
351k
        u4_nC += pu1_left_nnz[1];
670
351k
    if (u1_mb_b)
671
351k
        u4_nC += pu1_top_nnz[1];
672
351k
    if (u1_mb_a && u1_mb_b)
673
351k
        u4_nC = (u4_nC + 1) >> 1;
674
351k
    pu1_left_nnz[1] = pu1_top_nnz[1] = pu1_nnz[3];
675
351k
    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]);
676
677
351k
    return error_status;
678
351k
}
679
680
/**
681
*******************************************************************************
682
*
683
* @brief
684
*  This function encodes luma and chroma residues of a macro block when
685
*  the entropy coding mode chosen is cavlc.
686
*
687
* @param[in] ps_ent_ctxt
688
*  Pointer to entropy context
689
*
690
* @param[in] u4_mb_type
691
*  current mb type
692
*
693
* @param[in] u4_cbp
694
*  coded block pattern for the current mb
695
*
696
* @returns error code
697
*
698
* @remarks none
699
*
700
*******************************************************************************
701
*/
702
static IH264E_ERROR_T ih264e_encode_residue(entropy_ctxt_t *ps_ent_ctxt,
703
                                            UWORD32 u4_mb_type,
704
                                            UWORD32 u4_cbp)
705
3.59M
{
706
    /* error status */
707
3.59M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
708
709
    /* packed residue */
710
3.59M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
711
712
    /* bit stream buffer */
713
3.59M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
714
715
    /* zero run */
716
3.59M
    UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run;
717
718
    /* temp var */
719
3.59M
    UWORD32 u4_nC, u4_ngbr_avlb;
720
3.59M
    UWORD8 au1_nnz[4], *pu1_ngbr_avlb, *pu1_top_nnz, *pu1_left_nnz;
721
3.59M
    UWORD16 au2_sig_coeff_map[4] = {0};
722
3.59M
    WORD16 *pi2_res_block[4] = {NULL};
723
3.59M
    UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
724
3.59M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
725
3.59M
    ENTROPY_BLK_TYPE e_entropy_blk_type = CAVLC_LUMA_4x4;
726
727
    /* ngbr availability */
728
3.59M
    UWORD8 u1_mb_a, u1_mb_b;
729
730
    /* cbp */
731
3.59M
    UWORD32 u4_cbp_luma = u4_cbp & 0xF, u4_cbp_chroma = u4_cbp >> 4;
732
733
    /* mb indices */
734
3.59M
    WORD32 i4_mb_x, i4_mb_y;
735
736
    /* derive neighbor availability */
737
3.59M
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
738
3.59M
    i4_mb_y = ps_ent_ctxt->i4_mb_y;
739
3.59M
    pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
740
    /* left macroblock availability */
741
3.59M
    u1_mb_a = (i4_mb_x == 0 ||
742
3.50M
                    (pu1_slice_idx[i4_mb_x - 1 ] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
743
    /* top macroblock availability */
744
3.59M
    u1_mb_b = (i4_mb_y == 0 ||
745
3.50M
                    (pu1_slice_idx[i4_mb_x-ps_ent_ctxt->i4_wd_mbs] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
746
747
3.59M
    pu1_ngbr_avlb = (void *)(&u4_ngbr_avlb);
748
3.59M
    pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
749
3.59M
    pu1_left_nnz = (UWORD8 *)&ps_ent_ctxt->u4_left_nnz_luma;
750
751
    /* encode luma residue */
752
753
    /* mb type intra 16x16 */
754
3.59M
    if (u4_mb_type == I16x16)
755
2.19M
    {
756
        /* parse packed coeff data structure for residual data */
757
2.19M
        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]);
758
        /* estimate nnz for the current mb */
759
2.19M
        u4_nC = 0;
760
2.19M
        if (u1_mb_a)
761
2.16M
            u4_nC += pu1_left_nnz[0];
762
2.19M
        if (u1_mb_b)
763
2.16M
            u4_nC += pu1_top_nnz[0];
764
2.19M
        if (u1_mb_a && u1_mb_b)
765
2.13M
            u4_nC = (u4_nC + 1) >> 1;
766
767
        /* encode dc block */
768
2.19M
        ENTROPY_TRACE("Luma DC blk idx %d",0);
769
2.19M
        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]);
770
771
2.19M
        e_entropy_blk_type = CAVLC_LUMA_4x4_AC;
772
2.19M
    }
773
774
3.59M
    if (u4_cbp_luma & 1)
775
67.9k
    {
776
        /* encode ac block index 8x8 = 0*/
777
        /* parse packed coeff data structure for residual data */
778
67.9k
        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]);
779
67.9k
        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]);
780
67.9k
        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]);
781
67.9k
        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]);
782
        /* derive sub block neighbor availability */
783
784
67.9k
        pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
785
67.9k
        pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
786
67.9k
        pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
787
67.9k
        pu1_ngbr_avlb[3] = 0x11;
788
        /* encode sub blk */
789
67.9k
        ENTROPY_TRACE("Luma blk idx %d",0);
790
67.9k
        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);
791
67.9k
    }
792
3.52M
    else
793
3.52M
    {
794
3.52M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
795
3.52M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
796
3.52M
    }
797
798
3.59M
    if (u4_cbp_luma & 2)
799
59.3k
    {
800
        /* encode ac block index 8x8 = 1*/
801
        /* parse packed coeff data structure for residual data */
802
59.3k
        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]);
803
59.3k
        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]);
804
59.3k
        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]);
805
59.3k
        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]);
806
807
        /* derive sub block neighbor availability */
808
59.3k
        pu1_ngbr_avlb[1] = pu1_ngbr_avlb[0] = (u1_mb_b << 4) | 1;
809
59.3k
        pu1_ngbr_avlb[3] = pu1_ngbr_avlb[2] = 0x11;
810
        /* encode sub blk */
811
59.3k
        ENTROPY_TRACE("Luma blk idx %d",1);
812
59.3k
        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);
813
59.3k
    }
814
3.53M
    else
815
3.53M
    {
816
3.53M
        (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
817
3.53M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
818
3.53M
    }
819
820
3.59M
    if (u4_cbp_luma & 0x4)
821
59.7k
    {
822
        /* encode ac block index 8x8 = 2*/
823
        /* parse packed coeff data structure for residual data */
824
59.7k
        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]);
825
59.7k
        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]);
826
59.7k
        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]);
827
59.7k
        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]);
828
829
        /* derive sub block neighbor availability */
830
59.7k
        pu1_ngbr_avlb[2] = pu1_ngbr_avlb[0] = (1 << 4) | u1_mb_a;
831
59.7k
        pu1_ngbr_avlb[1] = pu1_ngbr_avlb[3] = 0x11;
832
        /* encode sub blk */
833
59.7k
        ENTROPY_TRACE("Luma blk idx %d",2);
834
59.7k
        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));
835
59.7k
    }
836
3.53M
    else
837
3.53M
    {
838
3.53M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
839
3.53M
        (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
840
3.53M
    }
841
842
3.59M
    if (u4_cbp_luma & 0x8)
843
54.6k
    {
844
        /* encode ac block index 8x8 = 3*/
845
        /* parse packed coeff data structure for residual data */
846
54.6k
        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]);
847
54.6k
        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]);
848
54.6k
        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]);
849
54.6k
        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]);
850
851
        /* derive sub block neighbor availability */
852
54.6k
        u4_ngbr_avlb = 0x11111111;
853
        /* encode sub blk */
854
54.6k
        ENTROPY_TRACE("Luma blk idx %d",3);
855
54.6k
        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);
856
54.6k
    }
857
3.54M
    else
858
3.54M
    {
859
3.54M
        (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
860
3.54M
        (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
861
3.54M
    }
862
863
    /* encode chroma residue */
864
3.59M
    if (u4_cbp_chroma & 3)
865
68.1k
    {
866
        /* parse packed coeff data structure for residual data */
867
        /* cb, cr */
868
68.1k
        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]);
869
68.1k
        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]);
870
871
        /* encode dc block */
872
        /* cb, cr */
873
68.1k
        ENTROPY_TRACE("Chroma DC blk idx %d",0);
874
68.1k
        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]);
875
68.1k
        ENTROPY_TRACE("Chroma DC blk idx %d",1);
876
68.1k
        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]);
877
68.1k
    }
878
879
3.59M
    pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
880
3.59M
    pu1_left_nnz = (UWORD8 *) &ps_ent_ctxt->u4_left_nnz_cbcr;
881
882
    /* encode sub blk */
883
3.59M
    if (u4_cbp_chroma & 0x2)
884
55.1k
    {
885
        /* encode ac block index 8x8 = 0*/
886
        /* derive sub block neighbor availability */
887
55.1k
        pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
888
55.1k
        pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
889
55.1k
        pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
890
55.1k
        pu1_ngbr_avlb[3] = 0x11;
891
892
        /* parse packed coeff data structure for residual data */
893
55.1k
        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]);
894
55.1k
        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]);
895
55.1k
        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]);
896
55.1k
        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]);
897
898
55.1k
        ENTROPY_TRACE("Chroma AC blk idx %d",0);
899
55.1k
        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);
900
55.1k
    }
901
3.54M
    else
902
3.54M
    {
903
3.54M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
904
3.54M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
905
3.54M
    }
906
907
3.59M
    pu1_top_nnz += 2;
908
3.59M
    pu1_left_nnz += 2;
909
910
    /* encode sub blk */
911
3.59M
    if (u4_cbp_chroma & 0x2)
912
55.1k
    {
913
        /* parse packed coeff data structure for residual data */
914
55.1k
        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]);
915
55.1k
        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]);
916
55.1k
        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]);
917
55.1k
        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]);
918
919
55.1k
        ENTROPY_TRACE("Chroma AC blk idx %d",1);
920
55.1k
        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);
921
55.1k
    }
922
3.54M
    else
923
3.54M
    {
924
3.54M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
925
3.54M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
926
3.54M
    }
927
928
    /* store the index of the next mb coeff data */
929
3.59M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
930
931
3.59M
    return error_status;
932
3.59M
}
933
934
935
/**
936
*******************************************************************************
937
*
938
* @brief
939
*  This function generates CAVLC coded bit stream for an Intra Slice.
940
*
941
* @description
942
*  The mb syntax layer for intra slices constitutes luma mb mode, luma sub modes
943
*  (if present), mb qp delta, coded block pattern, chroma mb mode and
944
*  luma/chroma residue. These syntax elements are written as directed by table
945
*  7.3.5 of h264 specification.
946
*
947
* @param[in] ps_ent_ctxt
948
*  pointer to entropy context
949
*
950
* @returns error code
951
*
952
* @remarks none
953
*
954
*******************************************************************************
955
*/
956
IH264E_ERROR_T ih264e_write_islice_mb_cavlc(entropy_ctxt_t *ps_ent_ctxt)
957
3.53M
{
958
    /* error status */
959
3.53M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
960
961
    /* bit stream ptr */
962
3.53M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
963
964
    /* packed header data */
965
3.53M
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
966
3.53M
    mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data;
967
968
    /* mb header info */
969
    /*
970
     * mb_tpm : mb type plus mode
971
     * mb_type : luma mb type and chroma mb type are packed
972
     * cbp : coded block pattern
973
     * mb_qp_delta : mb qp delta
974
     * chroma_intra_mode : chroma intra mode
975
     * luma_intra_mode : luma intra mode
976
     */
977
3.53M
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
978
3.53M
    WORD8 mb_qp_delta;
979
980
    /* temp var */
981
3.53M
    WORD32 i, mb_type_stream;
982
983
3.53M
    WORD32 bitstream_start_offset, bitstream_end_offset;
984
985
    /* Starting bitstream offset for header in bits */
986
3.53M
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
987
988
989
    /********************************************************************/
990
    /*                    BEGIN HEADER GENERATION                       */
991
    /********************************************************************/
992
993
    /* mb header info */
994
3.53M
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
995
3.53M
    cbp = ps_mb_hdr->u1_cbp;
996
3.53M
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
997
998
    /* mb type */
999
3.53M
    mb_type = mb_tpm & 0xF;
1000
    /* is intra ? */
1001
3.53M
    if (mb_type == I16x16)
1002
2.19M
    {
1003
2.19M
        UWORD32 u4_cbp_l, u4_cbp_c;
1004
1005
2.19M
        u4_cbp_c = (cbp >> 4);
1006
2.19M
        u4_cbp_l = (cbp & 0xF);
1007
2.19M
        luma_intra_mode = (mb_tpm >> 4) & 3;
1008
2.19M
        chroma_intra_mode = (mb_tpm >> 6);
1009
1010
2.19M
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1011
1012
        /* write mb type */
1013
2.19M
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1014
1015
        /* intra_chroma_pred_mode */
1016
2.19M
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1017
1018
2.19M
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1019
2.19M
    }
1020
1.34M
    else if (mb_type == I4x4)
1021
1.34M
    {
1022
1.34M
        mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data;
1023
1024
        /* mb sub blk modes */
1025
1.34M
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1026
1.34M
        WORD32 byte;
1027
1028
1.34M
        chroma_intra_mode = (mb_tpm >> 6);
1029
1030
        /* write mb type */
1031
1.34M
        PUT_BITS_UEV(ps_bitstream, 0, error_status, "mb type");
1032
1033
12.0M
        for (i = 0; i < 16; i += 2)
1034
10.7M
        {
1035
            /* sub blk idx 1 */
1036
10.7M
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1037
1038
10.7M
            intra_pred_mode_flag = byte & 0x1;
1039
1040
            /* prev_intra4x4_pred_mode_flag */
1041
10.7M
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1042
1043
            /* rem_intra4x4_pred_mode */
1044
10.7M
            if (!intra_pred_mode_flag)
1045
148k
            {
1046
148k
                rem_intra_mode = (byte & 0xF) >> 1;
1047
148k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1048
148k
            }
1049
1050
            /* sub blk idx 2 */
1051
10.7M
            byte >>= 4;
1052
1053
10.7M
            intra_pred_mode_flag = byte & 0x1;
1054
1055
            /* prev_intra4x4_pred_mode_flag */
1056
10.7M
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1057
1058
            /* rem_intra4x4_pred_mode */
1059
10.7M
            if (!intra_pred_mode_flag)
1060
97.3k
            {
1061
97.3k
                rem_intra_mode = (byte & 0xF) >> 1;
1062
97.3k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1063
97.2k
            }
1064
10.7M
        }
1065
1066
        /* intra_chroma_pred_mode */
1067
1.34M
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1068
1069
1.34M
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1070
1.34M
    }
1071
0
    else if (mb_type == I8x8)
1072
0
    {
1073
        /* transform 8x8 flag */
1074
0
        UWORD32 u4_transform_size_8x8_flag = ps_ent_ctxt->i1_transform_8x8_mode_flag;
1075
0
        mb_hdr_i8x8_t *ps_mb_hdr_i8x8 = (mb_hdr_i8x8_t *)ps_ent_ctxt->pv_mb_header_data;
1076
1077
        /* mb sub blk modes */
1078
0
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1079
0
        WORD32 byte;
1080
1081
0
        chroma_intra_mode = (mb_tpm >> 6);
1082
1083
0
        ASSERT(0);
1084
1085
        /* write mb type */
1086
0
        PUT_BITS_UEV(ps_bitstream, 0, error_status, "mb type");
1087
1088
        /* u4_transform_size_8x8_flag */
1089
0
        PUT_BITS(ps_bitstream, u4_transform_size_8x8_flag, 1, error_status, "u4_transform_size_8x8_flag");
1090
1091
        /* write sub block modes */
1092
0
        for (i = 0; i < 4; i++)
1093
0
        {
1094
            /* sub blk idx 1 */
1095
0
            byte = ps_mb_hdr_i8x8->au1_sub_blk_modes[i >> 1];
1096
1097
0
            intra_pred_mode_flag = byte & 0x1;
1098
1099
            /* prev_intra4x4_pred_mode_flag */
1100
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1101
1102
            /* rem_intra4x4_pred_mode */
1103
0
            if (!intra_pred_mode_flag)
1104
0
            {
1105
0
                rem_intra_mode = (byte & 0xF) >> 1;
1106
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1107
0
            }
1108
1109
            /* sub blk idx 2 */
1110
0
            byte >>= 4;
1111
1112
0
            intra_pred_mode_flag = byte & 0x1;
1113
1114
            /* prev_intra4x4_pred_mode_flag */
1115
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1116
1117
            /* rem_intra4x4_pred_mode */
1118
0
            if (!intra_pred_mode_flag)
1119
0
            {
1120
0
                rem_intra_mode = (byte & 0xF) >> 1;
1121
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1122
0
            }
1123
0
        }
1124
1125
        /* intra_chroma_pred_mode */
1126
0
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1127
1128
0
        pu1_byte += sizeof(mb_hdr_i8x8_t);
1129
0
    }
1130
0
    else
1131
0
    {
1132
0
    }
1133
1134
    /* coded_block_pattern */
1135
3.53M
    if (mb_type != I16x16)
1136
1.34M
    {
1137
1.34M
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][0], error_status, "coded_block_pattern");
1138
1.34M
    }
1139
1140
3.53M
    if (cbp || mb_type == I16x16)
1141
2.21M
    {
1142
        /* mb_qp_delta */
1143
2.21M
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1144
2.21M
    }
1145
1146
    /* Ending bitstream offset for header in bits */
1147
3.53M
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1148
1149
3.53M
    ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1150
1151
    /* Starting bitstream offset for residue */
1152
3.53M
    bitstream_start_offset = bitstream_end_offset;
1153
1154
    /* residual */
1155
3.53M
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1156
1157
    /* Ending bitstream offset for reside in bits */
1158
3.53M
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1159
3.53M
    ps_ent_ctxt->u4_residue_bits[0] += bitstream_end_offset - bitstream_start_offset;
1160
1161
    /* store the index of the next mb syntax layer */
1162
3.53M
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1163
1164
3.53M
    return error_status;
1165
3.53M
}
1166
1167
/**
1168
*******************************************************************************
1169
*
1170
* @brief
1171
*  This function generates CAVLC coded bit stream for Inter slices
1172
*
1173
* @description
1174
*  The mb syntax layer for inter slices constitutes luma mb mode, luma sub modes
1175
*  (if present), mb qp delta, coded block pattern, chroma mb mode and
1176
*  luma/chroma residue. These syntax elements are written as directed by table
1177
*  7.3.5 of h264 specification
1178
*
1179
* @param[in] ps_ent_ctxt
1180
*  pointer to entropy context
1181
*
1182
* @returns error code
1183
*
1184
* @remarks none
1185
*
1186
*******************************************************************************
1187
*/
1188
IH264E_ERROR_T ih264e_write_pslice_mb_cavlc(entropy_ctxt_t *ps_ent_ctxt)
1189
32.2k
{
1190
    /* error status */
1191
32.2k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
1192
1193
    /* bit stream ptr */
1194
32.2k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1195
1196
    /* packed header data */
1197
32.2k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1198
32.2k
    mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data;
1199
1200
    /* mb header info */
1201
    /*
1202
     * mb_tpm : mb type plus mode
1203
     * mb_type : luma mb type and chroma mb type are packed
1204
     * cbp : coded block pattern
1205
     * mb_qp_delta : mb qp delta
1206
     * chroma_intra_mode : chroma intra mode
1207
     * luma_intra_mode : luma intra mode
1208
     * ps_pu :  Pointer to the array of structures having motion vectors, size
1209
     * and position of sub partitions
1210
     */
1211
32.2k
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
1212
32.2k
    WORD8 mb_qp_delta;
1213
1214
    /* temp var */
1215
32.2k
    WORD32 i, mb_type_stream, cbptable = 1;
1216
1217
32.2k
    WORD32 is_inter = 0;
1218
1219
32.2k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1220
1221
    /* Starting bitstream offset for header in bits */
1222
32.2k
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
1223
1224
    /********************************************************************/
1225
    /*                    BEGIN HEADER GENERATION                       */
1226
    /********************************************************************/
1227
1228
    /* mb header info */
1229
32.2k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1230
1231
    /* mb type */
1232
32.2k
    mb_type = mb_tpm & 0xF;
1233
1234
    /* check for skip */
1235
32.2k
    if (mb_type == PSKIP)
1236
6.02k
    {
1237
6.02k
        UWORD32 *nnz;
1238
1239
6.02k
        is_inter = 1;
1240
1241
        /* increment skip counter */
1242
6.02k
        (*ps_ent_ctxt->pi4_mb_skip_run)++;
1243
1244
        /* store the index of the next mb syntax layer */
1245
6.02k
        pu1_byte += sizeof(mb_hdr_pskip_t);
1246
6.02k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1247
1248
        /* set nnz to zero */
1249
6.02k
        ps_ent_ctxt->u4_left_nnz_luma = 0;
1250
6.02k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
1251
6.02k
        *nnz = 0;
1252
6.02k
        ps_ent_ctxt->u4_left_nnz_cbcr = 0;
1253
6.02k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
1254
6.02k
        *nnz = 0;
1255
1256
        /* residual */
1257
6.02k
        error_status = ih264e_encode_residue(ps_ent_ctxt, P16x16, 0);
1258
1259
6.02k
        bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1260
1261
6.02k
        ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1262
1263
6.02k
        return error_status;
1264
6.02k
    }
1265
1266
    /* remaining mb header info */
1267
26.2k
    cbp = ps_mb_hdr->u1_cbp;
1268
26.2k
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
1269
1270
    /* mb skip run */
1271
26.2k
    PUT_BITS_UEV(ps_bitstream, *ps_ent_ctxt->pi4_mb_skip_run, error_status, "mb skip run");
1272
1273
    /* reset skip counter */
1274
25.1k
    *ps_ent_ctxt->pi4_mb_skip_run = 0;
1275
1276
    /* is intra ? */
1277
25.1k
    if (mb_type == I16x16)
1278
4.16k
    {
1279
4.16k
        UWORD32 u4_cbp_l, u4_cbp_c;
1280
1281
4.16k
        is_inter = 0;
1282
1283
4.16k
        u4_cbp_c = (cbp >> 4);
1284
4.16k
        u4_cbp_l = (cbp & 0xF);
1285
4.16k
        luma_intra_mode = (mb_tpm >> 4) & 3;
1286
4.16k
        chroma_intra_mode = (mb_tpm >> 6);
1287
1288
4.16k
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1289
1290
4.16k
        mb_type_stream += 5;
1291
1292
        /* write mb type */
1293
4.16k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1294
1295
        /* intra_chroma_pred_mode */
1296
4.11k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1297
4.08k
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1298
4.08k
    }
1299
21.0k
    else if (mb_type == I4x4)
1300
9.48k
    {
1301
9.48k
        mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data;
1302
1303
        /* mb sub blk modes */
1304
9.48k
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1305
9.48k
        WORD32 byte;
1306
1307
9.48k
        is_inter = 0;
1308
1309
9.48k
        chroma_intra_mode = (mb_tpm >> 6);
1310
9.48k
        cbptable = 0;
1311
1312
        /* write mb type */
1313
9.48k
        PUT_BITS_UEV(ps_bitstream, 5, error_status, "mb type");
1314
1315
80.8k
        for (i = 0; i < 16; i += 2)
1316
72.0k
        {
1317
            /* sub blk idx 1 */
1318
72.0k
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1319
1320
72.0k
            intra_pred_mode_flag = byte & 0x1;
1321
1322
            /* prev_intra4x4_pred_mode_flag */
1323
72.0k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1324
1325
            /* rem_intra4x4_pred_mode */
1326
71.9k
            if (!intra_pred_mode_flag)
1327
34.2k
            {
1328
34.2k
                rem_intra_mode = (byte & 0xF) >> 1;
1329
34.2k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1330
34.1k
            }
1331
1332
            /* sub blk idx 2 */
1333
71.8k
            byte >>= 4;
1334
1335
71.8k
            intra_pred_mode_flag = byte & 0x1;
1336
1337
            /* prev_intra4x4_pred_mode_flag */
1338
71.8k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1339
1340
            /* rem_intra4x4_pred_mode */
1341
71.7k
            if (!intra_pred_mode_flag)
1342
28.6k
            {
1343
28.6k
                rem_intra_mode = (byte & 0xF) >> 1;
1344
28.6k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1345
28.5k
            }
1346
71.7k
        }
1347
1348
        /* intra_chroma_pred_mode */
1349
8.82k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1350
1351
8.73k
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1352
8.73k
    }
1353
11.5k
    else if (mb_type == I8x8)
1354
0
    {
1355
0
        mb_hdr_i8x8_t *ps_mb_hdr_i8x8 = (mb_hdr_i8x8_t *)ps_ent_ctxt->pv_mb_header_data;
1356
1357
        /* transform 8x8 flag */
1358
0
        UWORD32 u4_transform_size_8x8_flag = ps_ent_ctxt->i1_transform_8x8_mode_flag;
1359
1360
        /* mb sub blk modes */
1361
0
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1362
0
        WORD32 byte;
1363
1364
0
        is_inter = 0;
1365
1366
0
        chroma_intra_mode = (mb_tpm >> 6);
1367
0
        cbptable = 0;
1368
1369
0
        ASSERT(0);
1370
1371
        /* write mb type */
1372
0
        PUT_BITS_UEV(ps_bitstream, 5, error_status, "mb type");
1373
1374
        /* u4_transform_size_8x8_flag */
1375
0
        PUT_BITS(ps_bitstream, u4_transform_size_8x8_flag, 1, error_status, "u4_transform_size_8x8_flag");
1376
1377
        /* write sub block modes */
1378
0
        for (i = 0; i < 4; i++)
1379
0
        {
1380
            /* sub blk idx 1 */
1381
0
            byte = ps_mb_hdr_i8x8->au1_sub_blk_modes[i >> 1];
1382
1383
0
            intra_pred_mode_flag = byte & 0x1;
1384
1385
            /* prev_intra4x4_pred_mode_flag */
1386
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1387
1388
            /* rem_intra4x4_pred_mode */
1389
0
            if (!intra_pred_mode_flag)
1390
0
            {
1391
0
                rem_intra_mode = (byte & 0xF) >> 1;
1392
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1393
0
            }
1394
1395
            /* sub blk idx 2 */
1396
0
            byte >>= 4;
1397
1398
0
            intra_pred_mode_flag = byte & 0x1;
1399
1400
            /* prev_intra4x4_pred_mode_flag */
1401
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1402
1403
            /* rem_intra4x4_pred_mode */
1404
0
            if (!intra_pred_mode_flag)
1405
0
            {
1406
0
                rem_intra_mode = (byte & 0xF) >> 1;
1407
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1408
0
            }
1409
0
        }
1410
1411
        /* intra_chroma_pred_mode */
1412
0
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1413
1414
0
        pu1_byte += sizeof(mb_hdr_i8x8_t);
1415
0
    }
1416
11.5k
    else
1417
11.5k
    {
1418
11.5k
        mb_hdr_p16x16_t *ps_mb_hdr_p16x16 = (mb_hdr_p16x16_t *)ps_ent_ctxt->pv_mb_header_data;
1419
1420
        /* inter macro block partition cnt */
1421
11.5k
        const UWORD8 au1_part_cnt[] = { 1, 2, 2, 4 };
1422
1423
        /* mv ptr */
1424
11.5k
        WORD16 *pi2_mv_ptr = (WORD16 *)ps_mb_hdr_p16x16->ai2_mv;
1425
1426
        /* number of partitions for the current mb */
1427
11.5k
        UWORD32 u4_part_cnt = au1_part_cnt[mb_type - 3];
1428
1429
11.5k
        is_inter = 1;
1430
1431
        /* write mb type */
1432
11.5k
        PUT_BITS_UEV(ps_bitstream, mb_type - 3, error_status, "mb type");
1433
1434
22.6k
        for (i = 0; i < (WORD32)u4_part_cnt; i++)
1435
11.4k
        {
1436
11.4k
            PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv x");
1437
11.3k
            PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv y");
1438
11.2k
        }
1439
1440
11.2k
        pu1_byte += sizeof(mb_hdr_p16x16_t);
1441
1442
11.2k
    }
1443
1444
    /* coded_block_pattern */
1445
24.0k
    if (mb_type != I16x16)
1446
19.9k
    {
1447
19.9k
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][cbptable], error_status, "coded_block_pattern");
1448
19.8k
    }
1449
1450
23.9k
    if (cbp || mb_type == I16x16)
1451
21.8k
    {
1452
        /* mb_qp_delta */
1453
21.8k
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1454
21.8k
    }
1455
1456
    /* Ending bitstream offset for header in bits */
1457
23.8k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1458
1459
23.8k
    ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1460
1461
    /* start bitstream offset for residue in bits */
1462
23.8k
    bitstream_start_offset = bitstream_end_offset;
1463
1464
    /* residual */
1465
23.8k
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1466
1467
    /* Ending bitstream offset for residue in bits */
1468
23.8k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1469
1470
23.8k
    ps_ent_ctxt->u4_residue_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1471
1472
    /* store the index of the next mb syntax layer */
1473
23.8k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1474
1475
23.8k
    return error_status;
1476
23.9k
}
1477
1478
1479
/**
1480
*******************************************************************************
1481
*
1482
* @brief
1483
*  This function generates CAVLC coded bit stream for B slices
1484
*
1485
* @description
1486
*  The mb syntax layer for inter slices constitutes luma mb mode, luma sub modes
1487
*  (if present), mb qp delta, coded block pattern, chroma mb mode and
1488
*  luma/chroma residue. These syntax elements are written as directed by table
1489
*  7.3.5 of h264 specification
1490
*
1491
* @param[in] ps_ent_ctxt
1492
*  pointer to entropy context
1493
*
1494
* @returns error code
1495
*
1496
* @remarks none
1497
*
1498
*******************************************************************************
1499
*/
1500
IH264E_ERROR_T ih264e_write_bslice_mb_cavlc(entropy_ctxt_t *ps_ent_ctxt)
1501
37.3k
{
1502
    /* error status */
1503
37.3k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
1504
1505
    /* bit stream ptr */
1506
37.3k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1507
1508
    /* packed header data */
1509
37.3k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1510
37.3k
    mb_hdr_common_t *ps_mb_hdr = (mb_hdr_common_t *)ps_ent_ctxt->pv_mb_header_data;
1511
1512
    /* mb header info */
1513
    /*
1514
     * mb_tpm : mb type plus mode
1515
     * mb_type : luma mb type and chroma mb type are packed
1516
     * cbp : coded block pattern
1517
     * mb_qp_delta : mb qp delta
1518
     * chroma_intra_mode : chroma intra mode
1519
     * luma_intra_mode : luma intra mode
1520
     * ps_pu :  Pointer to the array of structures having motion vectors, size
1521
     * and position of sub partitions
1522
     */
1523
37.3k
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
1524
37.3k
    WORD8 mb_qp_delta;
1525
1526
    /* temp var */
1527
37.3k
    WORD32 i, mb_type_stream, cbptable = 1;
1528
1529
37.3k
    WORD32 is_inter = 0;
1530
1531
37.3k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1532
1533
    /* Starting bitstream offset for header in bits */
1534
37.3k
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
1535
1536
    /********************************************************************/
1537
    /*                    BEGIN HEADER GENERATION                       */
1538
    /********************************************************************/
1539
1540
37.3k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1541
1542
    /* mb type */
1543
37.3k
    mb_type = mb_tpm & 0xF;
1544
1545
    /* check for skip */
1546
37.3k
    if (mb_type == BSKIP)
1547
3.11k
    {
1548
3.11k
        UWORD32 *nnz;
1549
1550
3.11k
        is_inter = 1;
1551
1552
        /* increment skip counter */
1553
3.11k
        (*ps_ent_ctxt->pi4_mb_skip_run)++;
1554
1555
        /* store the index of the next mb syntax layer */
1556
3.11k
        pu1_byte += sizeof(mb_hdr_bskip_t);
1557
3.11k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1558
1559
        /* set nnz to zero */
1560
3.11k
        ps_ent_ctxt->u4_left_nnz_luma = 0;
1561
3.11k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
1562
3.11k
        *nnz = 0;
1563
3.11k
        ps_ent_ctxt->u4_left_nnz_cbcr = 0;
1564
3.11k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
1565
3.11k
        *nnz = 0;
1566
1567
        /* residual */
1568
3.11k
        error_status = ih264e_encode_residue(ps_ent_ctxt, B16x16, 0);
1569
1570
3.11k
        bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1571
1572
3.11k
        ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset
1573
3.11k
                        - bitstream_start_offset;
1574
1575
3.11k
        return error_status;
1576
3.11k
    }
1577
1578
1579
    /* remaining mb header info */
1580
34.2k
    cbp = ps_mb_hdr->u1_cbp;
1581
34.2k
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
1582
1583
    /* mb skip run */
1584
34.2k
    PUT_BITS_UEV(ps_bitstream, *ps_ent_ctxt->pi4_mb_skip_run, error_status, "mb skip run");
1585
1586
    /* reset skip counter */
1587
32.9k
    *ps_ent_ctxt->pi4_mb_skip_run = 0;
1588
1589
    /* is intra ? */
1590
32.9k
    if (mb_type == I16x16)
1591
4.10k
    {
1592
4.10k
        UWORD32 u4_cbp_l, u4_cbp_c;
1593
1594
4.10k
        is_inter = 0;
1595
1596
4.10k
        u4_cbp_c = (cbp >> 4);
1597
4.10k
        u4_cbp_l = (cbp & 0xF);
1598
4.10k
        luma_intra_mode = (mb_tpm >> 4) & 3;
1599
4.10k
        chroma_intra_mode = (mb_tpm >> 6);
1600
1601
4.10k
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1602
1603
4.10k
        mb_type_stream += 23;
1604
1605
        /* write mb type */
1606
4.10k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1607
1608
        /* intra_chroma_pred_mode */
1609
4.04k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1610
4.02k
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1611
1612
4.02k
    }
1613
28.8k
    else if (mb_type == I4x4)
1614
8.90k
    {
1615
8.90k
        mb_hdr_i4x4_t *ps_mb_hdr_i4x4 = (mb_hdr_i4x4_t *)ps_ent_ctxt->pv_mb_header_data;
1616
1617
        /* mb sub blk modes */
1618
8.90k
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1619
8.90k
        WORD32 byte;
1620
1621
8.90k
        is_inter = 0;
1622
1623
8.90k
        chroma_intra_mode = (mb_tpm >> 6);
1624
8.90k
        cbptable = 0;
1625
1626
        /* write mb type */
1627
8.90k
        PUT_BITS_UEV(ps_bitstream, 23, error_status, "mb type");
1628
1629
76.3k
        for (i = 0; i < 16; i += 2)
1630
68.0k
        {
1631
            /* sub blk idx 1 */
1632
68.0k
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1633
1634
68.0k
            intra_pred_mode_flag = byte & 0x1;
1635
1636
            /* prev_intra4x4_pred_mode_flag */
1637
68.0k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1638
1639
            /* rem_intra4x4_pred_mode */
1640
67.9k
            if (!intra_pred_mode_flag)
1641
18.8k
            {
1642
18.8k
                rem_intra_mode = (byte & 0xF) >> 1;
1643
18.8k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1644
18.7k
            }
1645
1646
            /* sub blk idx 2 */
1647
67.8k
            byte >>= 4;
1648
1649
67.8k
            intra_pred_mode_flag = byte & 0x1;
1650
1651
            /* prev_intra4x4_pred_mode_flag */
1652
67.8k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1653
1654
            /* rem_intra4x4_pred_mode */
1655
67.7k
            if (!intra_pred_mode_flag)
1656
21.0k
            {
1657
21.0k
                rem_intra_mode = (byte & 0xF) >> 1;
1658
21.0k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1659
21.0k
            }
1660
67.7k
        }
1661
1662
        /* intra_chroma_pred_mode */
1663
8.34k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1664
8.32k
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1665
1666
8.32k
    }
1667
19.9k
    else if (mb_type == I8x8)
1668
0
    {
1669
0
        mb_hdr_i8x8_t *ps_mb_hdr_i8x8 = (mb_hdr_i8x8_t *)ps_ent_ctxt->pv_mb_header_data;
1670
1671
        /* transform 8x8 flag */
1672
0
        UWORD32 u4_transform_size_8x8_flag = ps_ent_ctxt->i1_transform_8x8_mode_flag;
1673
1674
        /* mb sub blk modes */
1675
0
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1676
0
        WORD32 byte;
1677
1678
0
        is_inter = 0;
1679
1680
0
        chroma_intra_mode = (mb_tpm >> 6);
1681
0
        cbptable = 0;
1682
1683
0
        ASSERT(0);
1684
1685
        /* write mb type */
1686
0
        PUT_BITS_UEV(ps_bitstream, 23, error_status, "mb type");
1687
1688
        /* u4_transform_size_8x8_flag */
1689
0
        PUT_BITS(ps_bitstream, u4_transform_size_8x8_flag, 1, error_status, "u4_transform_size_8x8_flag");
1690
1691
        /* write sub block modes */
1692
0
        for (i = 0; i < 4; i++)
1693
0
        {
1694
            /* sub blk idx 1 */
1695
0
            byte = ps_mb_hdr_i8x8->au1_sub_blk_modes[i >> 1];
1696
1697
0
            intra_pred_mode_flag = byte & 0x1;
1698
1699
            /* prev_intra4x4_pred_mode_flag */
1700
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1701
1702
            /* rem_intra4x4_pred_mode */
1703
0
            if (!intra_pred_mode_flag)
1704
0
            {
1705
0
                rem_intra_mode = (byte & 0xF) >> 1;
1706
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1707
0
            }
1708
1709
            /* sub blk idx 2 */
1710
0
            byte >>= 4;
1711
1712
0
            intra_pred_mode_flag = byte & 0x1;
1713
1714
            /* prev_intra4x4_pred_mode_flag */
1715
0
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1716
1717
            /* rem_intra4x4_pred_mode */
1718
0
            if (!intra_pred_mode_flag)
1719
0
            {
1720
0
                rem_intra_mode = (byte & 0xF) >> 1;
1721
0
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1722
0
            }
1723
0
        }
1724
1725
        /* intra_chroma_pred_mode */
1726
0
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1727
0
        pu1_byte += sizeof(mb_hdr_i8x8_t);
1728
1729
0
    }
1730
19.9k
    else if(mb_type == BDIRECT)
1731
4.31k
    {
1732
4.31k
        is_inter = 1;
1733
        /* write mb type */
1734
4.31k
        PUT_BITS_UEV(ps_bitstream, B_DIRECT_16x16, error_status, "mb type");
1735
4.29k
        pu1_byte += sizeof(mb_hdr_bdirect_t);
1736
1737
4.29k
    }
1738
15.6k
    else /* if mb_type == B16x16 */
1739
15.6k
    {
1740
15.6k
        mb_hdr_b16x16_t *ps_mb_hdr_b16x16 = (mb_hdr_b16x16_t *)ps_ent_ctxt->pv_mb_header_data;
1741
1742
        /* inter macro block partition cnt for 16x16 16x8 8x16 8x8 */
1743
15.6k
        const UWORD8 au1_part_cnt[] = { 1, 2, 2, 4 };
1744
1745
        /* number of partitions for the current mb */
1746
15.6k
        UWORD32 u4_part_cnt = au1_part_cnt[mb_type - B16x16];
1747
1748
        /* Get the pred modes */
1749
15.6k
        WORD32 i4_mb_part_pred_mode = (mb_tpm >> 4);
1750
1751
15.6k
        is_inter = 1;
1752
1753
15.6k
        mb_type_stream = mb_type - B16x16 + B_L0_16x16 + i4_mb_part_pred_mode;
1754
1755
        /* write mb type */
1756
15.6k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1757
1758
30.7k
        for (i = 0; i < (WORD32)u4_part_cnt; i++)
1759
15.4k
        {
1760
15.4k
            if (i4_mb_part_pred_mode != PRED_L1)/* || PRED_BI */
1761
8.25k
            {
1762
8.25k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[0][0], error_status, "mv l0 x");
1763
8.18k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[0][1], error_status, "mv l0 y");
1764
8.13k
            }
1765
15.3k
            if (i4_mb_part_pred_mode != PRED_L0)/* || PRED_BI */
1766
8.77k
            {
1767
8.77k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[1][0], error_status, "mv l1 x");
1768
8.69k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[1][1], error_status, "mv l1 y");
1769
8.65k
            }
1770
15.3k
        }
1771
1772
15.2k
        pu1_byte += sizeof(mb_hdr_b16x16_t);
1773
15.2k
    }
1774
1775
    /* coded_block_pattern */
1776
31.8k
    if (mb_type != I16x16)
1777
27.8k
    {
1778
27.8k
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][cbptable], error_status, "coded_block_pattern");
1779
27.7k
    }
1780
1781
31.7k
    if (cbp || mb_type == I16x16)
1782
29.1k
    {
1783
        /* mb_qp_delta */
1784
29.1k
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1785
29.1k
    }
1786
1787
    /* Ending bitstream offset for header in bits */
1788
31.7k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1789
1790
31.7k
    ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1791
1792
    /* start bitstream offset for residue in bits */
1793
31.7k
    bitstream_start_offset = bitstream_end_offset;
1794
1795
    /* residual */
1796
31.7k
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1797
1798
    /* Ending bitstream offset for residue in bits */
1799
31.7k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1800
1801
31.7k
    ps_ent_ctxt->u4_residue_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1802
1803
    /* store the index of the next mb syntax layer */
1804
31.7k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1805
1806
31.7k
    return error_status;
1807
31.7k
}