Coverage Report

Created: 2025-07-18 07:04

/src/libavc/encoder/ih264e_cavlc.c
Line
Count
Source (jump to first uncovered line)
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.29M
{
133
1.29M
    UWORD32 i = 0;
134
1.29M
    UWORD32 u4_nnz_coeff = 0;
135
1.29M
    WORD32  i4_run = -1;
136
1.29M
    UWORD32 u4_sign = 0;
137
1.29M
    UWORD32 u4_tot_zero = 0;
138
1.29M
    UWORD32 u4_trailing1 = 0;
139
1.29M
    WORD32 i4_val;
140
1.29M
    UWORD32 u4_totzero_sign_trailone;
141
1.29M
    UWORD32 *pu4_zero_run;
142
143
1.29M
    pu4_zero_run = (void *)pu1_zero_run;
144
1.29M
    pu4_zero_run[0] = 0;
145
1.29M
    pu4_zero_run[1] = 0;
146
1.29M
    pu4_zero_run[2] = 0;
147
1.29M
    pu4_zero_run[3] = 0;
148
149
    /* Compute Runs of zeros for all nnz coefficients except the last 3 */
150
1.29M
    if (u4_total_coeff > 3)
151
878k
    {
152
9.92M
        for (i = 0; u4_nnz_coeff < (u4_total_coeff-3); i++)
153
9.05M
        {
154
9.05M
            i4_run++;
155
156
9.05M
            i4_val = (u4_sig_coeff_map & 0x1);
157
9.05M
            u4_sig_coeff_map >>= 1;
158
159
9.05M
            if (i4_val != 0)
160
8.46M
            {
161
8.46M
                pu1_zero_run[u4_nnz_coeff++] = i4_run;
162
8.46M
                i4_run = -1;
163
8.46M
            }
164
9.05M
        }
165
878k
    }
166
167
    /* Compute T1's, Signof(T1's) and Runs of zeros for the last 3 */
168
6.77M
    while (u4_nnz_coeff != u4_total_coeff)
169
5.48M
    {
170
5.48M
        i4_run++;
171
172
5.48M
        i4_val = (u4_sig_coeff_map & 0x1);
173
5.48M
        u4_sig_coeff_map >>= 1;
174
175
5.48M
        if (i4_val != 0)
176
3.46M
        {
177
3.46M
            if (pi2_res_block[u4_nnz_coeff] == 1)
178
393k
            {
179
393k
                pu1_zero_run[u4_nnz_coeff] = i4_run;
180
393k
                u4_trailing1++;
181
393k
            }
182
3.06M
            else
183
3.06M
            {
184
3.06M
                if (pi2_res_block[u4_nnz_coeff] == -1)
185
430k
                {
186
430k
                    pu1_zero_run[u4_nnz_coeff] = i4_run;
187
430k
                    u4_sign |= 1 << u4_trailing1;
188
430k
                    u4_trailing1++;
189
430k
                }
190
2.63M
                else
191
2.63M
                {
192
2.63M
                    pu1_zero_run[u4_nnz_coeff] = i4_run;
193
2.63M
                    u4_trailing1 = 0;
194
2.63M
                    u4_sign = 0;
195
2.63M
                }
196
3.06M
            }
197
3.46M
            i4_run = -1;
198
3.46M
            u4_nnz_coeff++;
199
3.46M
        }
200
5.48M
        i++;
201
5.48M
    }
202
203
1.29M
    u4_tot_zero = i - u4_total_coeff;
204
1.29M
    u4_totzero_sign_trailone = (u4_tot_zero << 16)|(u4_sign << 8)|u4_trailing1;
205
206
1.29M
    return (u4_totzero_sign_trailone);
207
1.29M
}
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.96M
{
252
3.96M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
253
3.96M
    UWORD32 u4_totzero_sign_trailone = 0;
254
3.96M
    UWORD32 u4_trailing_ones = 0;
255
3.96M
    UWORD32 u4_tot_zeros = 0;
256
3.96M
    UWORD32 u4_remaining_coeff = 0;
257
3.96M
    UWORD32 u4_sign1 = 0;
258
3.96M
    UWORD32 u4_max_num_coeff = 0;
259
3.96M
    const UWORD32 au4_max_num_nnz_coeff[] = {16, 15, 16, 4, 15};
260
261
    /* validate inputs */
262
3.96M
    ASSERT(u4_block_type <= CAVLC_CHROMA_4x4_AC);
263
264
3.96M
    u4_max_num_coeff = au4_max_num_nnz_coeff[u4_block_type];
265
266
3.96M
    ASSERT(u4_total_coeff <= u4_max_num_coeff);
267
268
3.96M
    if (!u4_total_coeff)
269
2.67M
    {
270
2.67M
        UWORD32 u4_codeword = 15;
271
2.67M
        UWORD32 u4_codesize = 1;
272
2.67M
        if (u4_block_type == CAVLC_CHROMA_4x4_DC)
273
11.8k
        {
274
11.8k
            u4_codeword = 1;
275
11.8k
            u4_codesize = 2;
276
11.8k
            DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, 0);
277
11.8k
            ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
278
11.8k
            ENTROPY_TRACE("\tnumber of trailing ones ",0);
279
11.8k
        }
280
2.66M
        else
281
2.66M
        {
282
2.66M
            UWORD32 u4_vlcnum = u4_nc >> 1;
283
284
            /* write coeff_token */
285
2.66M
            if (u4_vlcnum > 3)
286
64.7k
            {
287
                /* Num-FLC */
288
64.7k
                u4_codeword = 3;
289
64.7k
                u4_codesize = 6;
290
64.7k
            }
291
2.59M
            else
292
2.59M
            {
293
                /* Num-VLC 0, 1, 2 */
294
2.59M
                if (u4_vlcnum > 1)
295
34.9k
                {
296
34.9k
                    u4_vlcnum = 2;
297
34.9k
                }
298
2.59M
                u4_codesize <<= u4_vlcnum;
299
2.59M
                u4_codeword >>= (4 - u4_codesize);
300
2.59M
            }
301
302
2.66M
            DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, 0, u4_nc);
303
2.66M
            ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
304
2.66M
            ENTROPY_TRACE("\tnC ",u4_nc);
305
2.66M
        }
306
307
308
2.67M
        DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
309
2.67M
        ENTROPY_TRACE("\tcodeword ",u4_codeword);
310
2.67M
        ENTROPY_TRACE("\tcodesize ",u4_codesize);
311
312
2.67M
        error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
313
314
2.67M
        return error_status;
315
2.67M
    }
316
1.29M
    else
317
1.29M
    {
318
        /* Compute zero run, number of trailing ones and their sign. */
319
1.29M
        u4_totzero_sign_trailone =
320
1.29M
                ih264e_compute_zeroruns_and_trailingones(pi2_res_block,
321
1.29M
                        u4_total_coeff,
322
1.29M
                        pu1_zero_run,
323
1.29M
                        u4_sig_coeff_map);
324
1.29M
        u4_trailing_ones = u4_totzero_sign_trailone & 0xFF;
325
1.29M
        u4_sign1 = (u4_totzero_sign_trailone >> 8)& 0xFF;
326
1.29M
        u4_tot_zeros = (u4_totzero_sign_trailone >> 16) & 0xFF;
327
1.29M
        u4_remaining_coeff = u4_total_coeff - u4_trailing_ones;
328
329
        /* write coeff_token */
330
1.29M
        {
331
1.29M
            UWORD32 u4_codeword;
332
1.29M
            UWORD32 u4_codesize;
333
1.29M
            if (u4_block_type == CAVLC_CHROMA_4x4_DC)
334
122k
            {
335
122k
                u4_codeword = gu1_code_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
336
122k
                u4_codesize = gu1_size_coeff_token_table_chroma[u4_trailing_ones][u4_total_coeff-1];
337
338
122k
                DEBUG("\n[%d numcoeff, %d numtrailing ones]",u4_total_coeff, u4_trailing_ones);
339
122k
                ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
340
122k
                ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
341
122k
            }
342
1.16M
            else
343
1.16M
            {
344
1.16M
                UWORD32 u4_vlcnum = u4_nc >> 1;
345
346
1.16M
                if (u4_vlcnum > 3)
347
705k
                {
348
                    /* Num-FLC */
349
705k
                    u4_codeword = ((u4_total_coeff-1) << 2 ) + u4_trailing_ones;
350
705k
                    u4_codesize = 6;
351
705k
                }
352
463k
                else
353
463k
                {
354
                    /* Num-VLC 0, 1, 2 */
355
463k
                    if (u4_vlcnum > 1)
356
119k
                    {
357
119k
                        u4_vlcnum = 2;
358
119k
                    }
359
463k
                    u4_codeword = gu1_code_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
360
463k
                    u4_codesize = gu1_size_coeff_token_table[u4_vlcnum][u4_trailing_ones][u4_total_coeff-1];
361
463k
                }
362
363
1.16M
                DEBUG("\n[%d numcoeff, %d numtrailing ones, %d nnz]",u4_total_coeff, u4_trailing_ones, u4_nc);
364
1.16M
                ENTROPY_TRACE("\tnumber of non zero coeffs ",u4_total_coeff);
365
1.16M
                ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
366
1.16M
                ENTROPY_TRACE("\tnC ",u4_nc);
367
1.16M
            }
368
369
1.29M
            DEBUG("\nCOEFF TOKEN 0: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
370
1.29M
            ENTROPY_TRACE("\tcodeword ",u4_codeword);
371
1.29M
            ENTROPY_TRACE("\tcodesize ",u4_codesize);
372
373
1.29M
            error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
374
1.29M
        }
375
376
        /* write sign of trailing ones */
377
1.29M
        if (u4_trailing_ones)
378
378k
        {
379
378k
            DEBUG("\nT1's: %d u4_codeword, %d u4_codesize",u4_sign1, u4_trailing_ones);
380
378k
            error_status = ih264e_put_bits(ps_bit_stream, u4_sign1, u4_trailing_ones);
381
378k
            ENTROPY_TRACE("\tnumber of trailing ones ",u4_trailing_ones);
382
378k
            ENTROPY_TRACE("\tsign of trailing ones ",u4_sign1);
383
378k
        }
384
385
        /* write level codes */
386
1.29M
        if (u4_remaining_coeff)
387
1.13M
        {
388
1.13M
            WORD32 i4_level = pi2_res_block[u4_remaining_coeff-1];
389
1.13M
            UWORD32 u4_escape;
390
1.13M
            UWORD32 u4_suffix_length = 0; // Level-VLC[N]
391
1.13M
            UWORD32 u4_abs_level, u4_abs_level_actual = 0;
392
1.13M
            WORD32 i4_sign;
393
1.13M
            const UWORD32 u4_rndfactor[] = {0, 0, 1, 3, 7, 15, 31};
394
395
1.13M
            DEBUG("\n \t%d coeff,",i4_level);
396
1.13M
            ENTROPY_TRACE("\tcoeff ",i4_level);
397
398
1.13M
            if (u4_trailing_ones < 3)
399
1.05M
            {
400
                /* If there are less than 3 T1s, then the first non-T1 level is incremented if negative (decremented if positive)*/
401
1.05M
                if (i4_level < 0)
402
537k
                {
403
537k
                    i4_level += 1;
404
537k
                }
405
515k
                else
406
515k
                {
407
515k
                    i4_level -= 1;
408
515k
                }
409
410
1.05M
                u4_abs_level_actual = 1;
411
412
                /* Initialize VLC table (Suffix Length) to encode the level */
413
1.05M
                if (u4_total_coeff > 10)
414
636k
                {
415
636k
                    u4_suffix_length = 1;
416
636k
                }
417
1.05M
            }
418
419
1.13M
            i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
420
1.13M
            u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
421
422
1.13M
            u4_abs_level_actual += u4_abs_level;
423
424
1.13M
            u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
425
426
11.2M
            while (1)
427
11.2M
            {
428
11.2M
                WORD32 i4_codesize;
429
11.2M
                WORD32 i4_codeword;
430
11.2M
                WORD32 i4_codeval;
431
432
11.2M
                u4_remaining_coeff--;
433
434
11.2M
GATHER_CAVLC_STATS1();
435
436
11.2M
                {
437
11.2M
                    i4_codeval = u4_abs_level << 1;
438
11.2M
                    i4_codeval = i4_codeval - 2 - i4_sign;
439
440
11.2M
                    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
11.1M
                    else if (u4_escape > 7)
446
1.01M
                    {
447
1.01M
                        i4_codeword = (1 << 12) + (i4_codeval - (15 << u4_suffix_length));
448
1.01M
                        i4_codesize = 28;
449
1.01M
                        if (!u4_suffix_length)
450
122k
                        {
451
122k
                            i4_codeword -= 15;
452
122k
                        }
453
1.01M
                    }
454
10.1M
                    else
455
10.1M
                    {
456
10.1M
                        i4_codeword = (1 << u4_suffix_length) + (i4_codeval & ((1 << u4_suffix_length)-1));
457
10.1M
                        i4_codesize = (i4_codeval >> u4_suffix_length) + 1 + u4_suffix_length;
458
10.1M
                    }
459
11.2M
                }
460
461
                /*put the level code in bitstream*/
462
11.2M
                DEBUG("\nLEVEL: %d i4_codeword, %d i4_codesize",i4_codeword, i4_codesize);
463
11.2M
                ENTROPY_TRACE("\tcodeword ",i4_codeword);
464
11.2M
                ENTROPY_TRACE("\tcodesize ",i4_codesize);
465
11.2M
                error_status = ih264e_put_bits(ps_bit_stream, i4_codeword, i4_codesize);
466
467
11.2M
                if (u4_remaining_coeff == 0) break;
468
469
                /*update suffix length for next level*/
470
10.1M
                if (u4_suffix_length == 0)
471
394k
                {
472
394k
                    u4_suffix_length++;
473
394k
                }
474
10.1M
                if (u4_suffix_length < 6)
475
7.46M
                {
476
7.46M
                    if (u4_abs_level_actual > gu1_threshold_vlc_level[u4_suffix_length])
477
3.07M
                    {
478
3.07M
                        u4_suffix_length++;
479
3.07M
                    }
480
7.46M
                }
481
482
                /* next level */
483
10.1M
                i4_level      = pi2_res_block[u4_remaining_coeff-1];
484
485
10.1M
                DEBUG("\n \t%d coeff,",i4_level);
486
10.1M
                ENTROPY_TRACE("\tcoeff ",i4_level);
487
488
10.1M
                i4_sign = (i4_level >> (sizeof(WORD32) * CHAR_BIT - 1));
489
10.1M
                u4_abs_level = ((i4_level + i4_sign) ^ i4_sign);
490
491
10.1M
                u4_abs_level_actual = u4_abs_level;
492
493
10.1M
                u4_escape = (u4_abs_level + u4_rndfactor[u4_suffix_length]) >> u4_suffix_length;
494
10.1M
            }
495
1.13M
        }
496
497
1.29M
        DEBUG("\n \t %d totalzeros",u4_tot_zeros);
498
1.29M
        ENTROPY_TRACE("\ttotal zeros ",u4_tot_zeros);
499
500
        /* Write Total Zeros */
501
1.29M
        if (u4_total_coeff < u4_max_num_coeff)
502
792k
        {
503
792k
            WORD32 index;
504
792k
            UWORD32 u4_codeword;
505
792k
            UWORD32 u4_codesize;
506
507
792k
            if (u4_block_type == CAVLC_CHROMA_4x4_DC)
508
74.4k
            {
509
74.4k
                UWORD8 gu1_index_zero_table_chroma[] = {0, 4, 7};
510
74.4k
                index = gu1_index_zero_table_chroma[u4_total_coeff-1] + u4_tot_zeros;
511
74.4k
                u4_codesize = gu1_size_zero_table_chroma[index];
512
74.4k
                u4_codeword = gu1_code_zero_table_chroma[index];
513
74.4k
            }
514
718k
            else
515
718k
            {
516
718k
                index = gu1_index_zero_table[u4_total_coeff-1] + u4_tot_zeros;
517
718k
                u4_codesize = gu1_size_zero_table[index];
518
718k
                u4_codeword = gu1_code_zero_table[index];
519
718k
            }
520
521
792k
            DEBUG("\nTOTAL ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
522
792k
            ENTROPY_TRACE("\tcodeword ",u4_codeword);
523
792k
            ENTROPY_TRACE("\tcodesize ",u4_codesize);
524
792k
            error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
525
792k
        }
526
527
        /* Write Run Before */
528
1.29M
        if (u4_tot_zeros)
529
645k
        {
530
645k
            UWORD32 u4_max_num_coef = u4_total_coeff-1;
531
645k
            UWORD32 u4_codeword;
532
645k
            UWORD32 u4_codesize;
533
645k
            UWORD32 u4_zeros_left = u4_tot_zeros;
534
535
2.83M
            while (u4_max_num_coef)
536
2.62M
            {
537
2.62M
                UWORD32 u4_run_before = pu1_zero_run[u4_max_num_coef];
538
2.62M
                UWORD32 u4_index;
539
540
2.62M
                if (u4_zeros_left > MAX_ZERO_LEFT)
541
142k
                {
542
142k
                    u4_index = gu1_index_run_table[MAX_ZERO_LEFT];
543
142k
                }
544
2.48M
                else
545
2.48M
                {
546
2.48M
                    u4_index = gu1_index_run_table[u4_zeros_left - 1];
547
2.48M
                }
548
549
2.62M
                u4_codesize = gu1_size_run_table[u4_index + u4_run_before];
550
2.62M
                u4_codeword = gu1_code_run_table[u4_index + u4_run_before];
551
552
2.62M
                DEBUG("\nRUN BEFORE ZEROS: %d u4_codeword, %d u4_codesize",u4_codeword, u4_codesize);
553
2.62M
                ENTROPY_TRACE("\tcodeword ",u4_codeword);
554
2.62M
                ENTROPY_TRACE("\tcodesize ",u4_codesize);
555
2.62M
                error_status = ih264e_put_bits(ps_bit_stream, u4_codeword, u4_codesize);
556
557
2.62M
                u4_zeros_left -= u4_run_before;
558
2.62M
                if (!u4_zeros_left)
559
435k
                {
560
435k
                    break;
561
435k
                }
562
2.19M
                u4_max_num_coef--;
563
2.19M
            }
564
645k
        }
565
1.29M
    }
566
567
1.29M
    return error_status;
568
3.96M
}
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
375k
{
617
375k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
618
375k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
619
375k
    UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run, *pu1_ngbr_avbl;
620
375k
    UWORD32 u4_nC;
621
375k
    UWORD8 u1_mb_a, u1_mb_b;
622
623
375k
    pu1_ngbr_avbl = (void *)(&u4_ngbr_avlb);
624
625
    /* encode ac block index 4x4 = 0*/
626
375k
    u1_mb_a = pu1_ngbr_avbl[0] & 0x0F;
627
375k
    u1_mb_b = pu1_ngbr_avbl[0] & 0xF0;
628
375k
    u4_nC = 0;
629
375k
    if (u1_mb_a)
630
247k
        u4_nC += pu1_left_nnz[0];
631
375k
    if (u1_mb_b)
632
231k
        u4_nC += pu1_top_nnz[0];
633
375k
    if (u1_mb_a && u1_mb_b)
634
168k
        u4_nC = (u4_nC + 1) >> 1;
635
375k
    pu1_left_nnz[0] = pu1_top_nnz[0] = pu1_nnz[0];
636
375k
    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
375k
    u1_mb_a = pu1_ngbr_avbl[1] & 0x0F;
640
375k
    u1_mb_b = pu1_ngbr_avbl[1] & 0xF0;
641
375k
    u4_nC = 0;
642
375k
    if (u1_mb_a)
643
375k
        u4_nC += pu1_left_nnz[0];
644
375k
    if (u1_mb_b)
645
231k
        u4_nC += pu1_top_nnz[1];
646
375k
    if (u1_mb_a && u1_mb_b)
647
231k
        u4_nC = (u4_nC + 1) >> 1;
648
375k
    pu1_left_nnz[0] = pu1_top_nnz[1] = pu1_nnz[1];
649
375k
    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
375k
    u1_mb_a = pu1_ngbr_avbl[2] & 0x0F;
653
375k
    u1_mb_b = pu1_ngbr_avbl[2] & 0xF0;
654
375k
    u4_nC = 0;
655
375k
    if (u1_mb_a)
656
247k
        u4_nC += pu1_left_nnz[1];
657
375k
    if (u1_mb_b)
658
375k
        u4_nC += pu1_top_nnz[0];
659
375k
    if (u1_mb_a && u1_mb_b)
660
247k
        u4_nC = (u4_nC + 1) >> 1;
661
375k
    pu1_left_nnz[1] = pu1_top_nnz[0] = pu1_nnz[2];
662
375k
    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
375k
    u1_mb_a = pu1_ngbr_avbl[3] & 0x0F;
666
375k
    u1_mb_b = pu1_ngbr_avbl[3] & 0xF0;
667
375k
    u4_nC = 0;
668
375k
    if (u1_mb_a)
669
375k
        u4_nC += pu1_left_nnz[1];
670
375k
    if (u1_mb_b)
671
375k
        u4_nC += pu1_top_nnz[1];
672
375k
    if (u1_mb_a && u1_mb_b)
673
375k
        u4_nC = (u4_nC + 1) >> 1;
674
375k
    pu1_left_nnz[1] = pu1_top_nnz[1] = pu1_nnz[3];
675
375k
    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
375k
    return error_status;
678
375k
}
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.91M
{
706
    /* error status */
707
3.91M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
708
709
    /* packed residue */
710
3.91M
    void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
711
712
    /* bit stream buffer */
713
3.91M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
714
715
    /* zero run */
716
3.91M
    UWORD8 *pu1_zero_run = ps_ent_ctxt->au1_zero_run;
717
718
    /* temp var */
719
3.91M
    UWORD32 u4_nC, u4_ngbr_avlb;
720
3.91M
    UWORD8 au1_nnz[4], *pu1_ngbr_avlb, *pu1_top_nnz, *pu1_left_nnz;
721
3.91M
    UWORD16 au2_sig_coeff_map[4] = {0};
722
3.91M
    WORD16 *pi2_res_block[4] = {NULL};
723
3.91M
    UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
724
3.91M
    tu_sblk_coeff_data_t *ps_mb_coeff_data;
725
3.91M
    ENTROPY_BLK_TYPE e_entropy_blk_type = CAVLC_LUMA_4x4;
726
727
    /* ngbr availability */
728
3.91M
    UWORD8 u1_mb_a, u1_mb_b;
729
730
    /* cbp */
731
3.91M
    UWORD32 u4_cbp_luma = u4_cbp & 0xF, u4_cbp_chroma = u4_cbp >> 4;
732
733
    /* mb indices */
734
3.91M
    WORD32 i4_mb_x, i4_mb_y;
735
736
    /* derive neighbor availability */
737
3.91M
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
738
3.91M
    i4_mb_y = ps_ent_ctxt->i4_mb_y;
739
3.91M
    pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
740
    /* left macroblock availability */
741
3.91M
    u1_mb_a = (i4_mb_x == 0 ||
742
3.91M
                    (pu1_slice_idx[i4_mb_x - 1 ] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
743
    /* top macroblock availability */
744
3.91M
    u1_mb_b = (i4_mb_y == 0 ||
745
3.91M
                    (pu1_slice_idx[i4_mb_x-ps_ent_ctxt->i4_wd_mbs] != pu1_slice_idx[i4_mb_x]))? 0 : 1;
746
747
3.91M
    pu1_ngbr_avlb = (void *)(&u4_ngbr_avlb);
748
3.91M
    pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
749
3.91M
    pu1_left_nnz = (UWORD8 *)&ps_ent_ctxt->u4_left_nnz_luma;
750
751
    /* encode luma residue */
752
753
    /* mb type intra 16x16 */
754
3.91M
    if (u4_mb_type == I16x16)
755
2.33M
    {
756
        /* parse packed coeff data structure for residual data */
757
2.33M
        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.33M
        u4_nC = 0;
760
2.33M
        if (u1_mb_a)
761
2.29M
            u4_nC += pu1_left_nnz[0];
762
2.33M
        if (u1_mb_b)
763
2.29M
            u4_nC += pu1_top_nnz[0];
764
2.33M
        if (u1_mb_a && u1_mb_b)
765
2.25M
            u4_nC = (u4_nC + 1) >> 1;
766
767
        /* encode dc block */
768
2.33M
        ENTROPY_TRACE("Luma DC blk idx %d",0);
769
2.33M
        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.33M
        e_entropy_blk_type = CAVLC_LUMA_4x4_AC;
772
2.33M
    }
773
774
3.91M
    if (u4_cbp_luma & 1)
775
74.2k
    {
776
        /* encode ac block index 8x8 = 0*/
777
        /* parse packed coeff data structure for residual data */
778
74.2k
        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
74.2k
        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
74.2k
        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
74.2k
        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
74.2k
        pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
785
74.2k
        pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
786
74.2k
        pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
787
74.2k
        pu1_ngbr_avlb[3] = 0x11;
788
        /* encode sub blk */
789
74.2k
        ENTROPY_TRACE("Luma blk idx %d",0);
790
74.2k
        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
74.2k
    }
792
3.83M
    else
793
3.83M
    {
794
3.83M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
795
3.83M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
796
3.83M
    }
797
798
3.91M
    if (u4_cbp_luma & 2)
799
67.2k
    {
800
        /* encode ac block index 8x8 = 1*/
801
        /* parse packed coeff data structure for residual data */
802
67.2k
        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
67.2k
        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
67.2k
        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
67.2k
        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
67.2k
        pu1_ngbr_avlb[1] = pu1_ngbr_avlb[0] = (u1_mb_b << 4) | 1;
809
67.2k
        pu1_ngbr_avlb[3] = pu1_ngbr_avlb[2] = 0x11;
810
        /* encode sub blk */
811
67.2k
        ENTROPY_TRACE("Luma blk idx %d",1);
812
67.2k
        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
67.2k
    }
814
3.84M
    else
815
3.84M
    {
816
3.84M
        (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
817
3.84M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
818
3.84M
    }
819
820
3.91M
    if (u4_cbp_luma & 0x4)
821
66.1k
    {
822
        /* encode ac block index 8x8 = 2*/
823
        /* parse packed coeff data structure for residual data */
824
66.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]);
825
66.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]);
826
66.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]);
827
66.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]);
828
829
        /* derive sub block neighbor availability */
830
66.1k
        pu1_ngbr_avlb[2] = pu1_ngbr_avlb[0] = (1 << 4) | u1_mb_a;
831
66.1k
        pu1_ngbr_avlb[1] = pu1_ngbr_avlb[3] = 0x11;
832
        /* encode sub blk */
833
66.1k
        ENTROPY_TRACE("Luma blk idx %d",2);
834
66.1k
        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
66.1k
    }
836
3.84M
    else
837
3.84M
    {
838
3.84M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
839
3.84M
        (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
840
3.84M
    }
841
842
3.91M
    if (u4_cbp_luma & 0x8)
843
62.2k
    {
844
        /* encode ac block index 8x8 = 3*/
845
        /* parse packed coeff data structure for residual data */
846
62.2k
        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
62.2k
        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
62.2k
        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
62.2k
        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
62.2k
        u4_ngbr_avlb = 0x11111111;
853
        /* encode sub blk */
854
62.2k
        ENTROPY_TRACE("Luma blk idx %d",3);
855
62.2k
        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
62.2k
    }
857
3.84M
    else
858
3.84M
    {
859
3.84M
        (pu1_top_nnz + 2)[0] = (pu1_top_nnz + 2)[1] = 0;
860
3.84M
        (pu1_left_nnz + 2)[0] = (pu1_left_nnz + 2)[1] = 0;
861
3.84M
    }
862
863
    /* encode chroma residue */
864
3.91M
    if (u4_cbp_chroma & 3)
865
67.1k
    {
866
        /* parse packed coeff data structure for residual data */
867
        /* cb, cr */
868
67.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
67.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
67.1k
        ENTROPY_TRACE("Chroma DC blk idx %d",0);
874
67.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
67.1k
        ENTROPY_TRACE("Chroma DC blk idx %d",1);
876
67.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
67.1k
    }
878
879
3.91M
    pu1_top_nnz = ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
880
3.91M
    pu1_left_nnz = (UWORD8 *) &ps_ent_ctxt->u4_left_nnz_cbcr;
881
882
    /* encode sub blk */
883
3.91M
    if (u4_cbp_chroma & 0x2)
884
52.7k
    {
885
        /* encode ac block index 8x8 = 0*/
886
        /* derive sub block neighbor availability */
887
52.7k
        pu1_ngbr_avlb[0] = (u1_mb_b << 4) | (u1_mb_a);
888
52.7k
        pu1_ngbr_avlb[1] = (u1_mb_b << 4) | 1;
889
52.7k
        pu1_ngbr_avlb[2] = (1 << 4) | (u1_mb_a);
890
52.7k
        pu1_ngbr_avlb[3] = 0x11;
891
892
        /* parse packed coeff data structure for residual data */
893
52.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]);
894
52.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]);
895
52.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]);
896
52.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]);
897
898
52.7k
        ENTROPY_TRACE("Chroma AC blk idx %d",0);
899
52.7k
        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
52.7k
    }
901
3.85M
    else
902
3.85M
    {
903
3.85M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
904
3.85M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
905
3.85M
    }
906
907
3.91M
    pu1_top_nnz += 2;
908
3.91M
    pu1_left_nnz += 2;
909
910
    /* encode sub blk */
911
3.91M
    if (u4_cbp_chroma & 0x2)
912
52.7k
    {
913
        /* parse packed coeff data structure for residual data */
914
52.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]);
915
52.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]);
916
52.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]);
917
52.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]);
918
919
52.7k
        ENTROPY_TRACE("Chroma AC blk idx %d",1);
920
52.7k
        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
52.7k
    }
922
3.85M
    else
923
3.85M
    {
924
3.85M
        pu1_top_nnz[0] = pu1_top_nnz[1] = 0;
925
3.85M
        pu1_left_nnz[0] = pu1_left_nnz[1] = 0;
926
3.85M
    }
927
928
    /* store the index of the next mb coeff data */
929
3.91M
    ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
930
931
3.91M
    return error_status;
932
3.91M
}
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.84M
{
958
    /* error status */
959
3.84M
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
960
961
    /* bit stream ptr */
962
3.84M
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
963
964
    /* packed header data */
965
3.84M
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
966
3.84M
    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.84M
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
978
3.84M
    WORD8 mb_qp_delta;
979
980
    /* temp var */
981
3.84M
    WORD32 i, mb_type_stream;
982
983
3.84M
    WORD32 bitstream_start_offset, bitstream_end_offset;
984
985
    /* Starting bitstream offset for header in bits */
986
3.84M
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
987
988
989
    /********************************************************************/
990
    /*                    BEGIN HEADER GENERATION                       */
991
    /********************************************************************/
992
993
    /* mb header info */
994
3.84M
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
995
3.84M
    cbp = ps_mb_hdr->u1_cbp;
996
3.84M
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
997
998
    /* mb type */
999
3.84M
    mb_type = mb_tpm & 0xF;
1000
    /* is intra ? */
1001
3.84M
    if (mb_type == I16x16)
1002
2.32M
    {
1003
2.32M
        UWORD32 u4_cbp_l, u4_cbp_c;
1004
1005
2.32M
        u4_cbp_c = (cbp >> 4);
1006
2.32M
        u4_cbp_l = (cbp & 0xF);
1007
2.32M
        luma_intra_mode = (mb_tpm >> 4) & 3;
1008
2.32M
        chroma_intra_mode = (mb_tpm >> 6);
1009
1010
2.32M
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1011
1012
        /* write mb type */
1013
2.32M
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1014
1015
        /* intra_chroma_pred_mode */
1016
2.32M
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1017
1018
2.32M
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1019
2.32M
    }
1020
1.52M
    else if (mb_type == I4x4)
1021
1.52M
    {
1022
1.52M
        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.52M
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1026
1.52M
        WORD32 byte;
1027
1028
1.52M
        chroma_intra_mode = (mb_tpm >> 6);
1029
1030
        /* write mb type */
1031
1.52M
        PUT_BITS_UEV(ps_bitstream, 0, error_status, "mb type");
1032
1033
13.6M
        for (i = 0; i < 16; i += 2)
1034
12.1M
        {
1035
            /* sub blk idx 1 */
1036
12.1M
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1037
1038
12.1M
            intra_pred_mode_flag = byte & 0x1;
1039
1040
            /* prev_intra4x4_pred_mode_flag */
1041
12.1M
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1042
1043
            /* rem_intra4x4_pred_mode */
1044
12.1M
            if (!intra_pred_mode_flag)
1045
164k
            {
1046
164k
                rem_intra_mode = (byte & 0xF) >> 1;
1047
164k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1048
163k
            }
1049
1050
            /* sub blk idx 2 */
1051
12.1M
            byte >>= 4;
1052
1053
12.1M
            intra_pred_mode_flag = byte & 0x1;
1054
1055
            /* prev_intra4x4_pred_mode_flag */
1056
12.1M
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1057
1058
            /* rem_intra4x4_pred_mode */
1059
12.1M
            if (!intra_pred_mode_flag)
1060
112k
            {
1061
112k
                rem_intra_mode = (byte & 0xF) >> 1;
1062
112k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1063
112k
            }
1064
12.1M
        }
1065
1066
        /* intra_chroma_pred_mode */
1067
1.52M
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1068
1069
1.52M
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1070
1.52M
    }
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.84M
    if (mb_type != I16x16)
1136
1.52M
    {
1137
1.52M
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][0], error_status, "coded_block_pattern");
1138
1.52M
    }
1139
1140
3.84M
    if (cbp || mb_type == I16x16)
1141
2.34M
    {
1142
        /* mb_qp_delta */
1143
2.34M
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1144
2.34M
    }
1145
1146
    /* Ending bitstream offset for header in bits */
1147
3.84M
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1148
1149
3.84M
    ps_ent_ctxt->u4_header_bits[0] += bitstream_end_offset - bitstream_start_offset;
1150
1151
    /* Starting bitstream offset for residue */
1152
3.84M
    bitstream_start_offset = bitstream_end_offset;
1153
1154
    /* residual */
1155
3.84M
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1156
1157
    /* Ending bitstream offset for reside in bits */
1158
3.84M
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1159
3.84M
    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.84M
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1163
1164
3.84M
    return error_status;
1165
3.84M
}
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
38.9k
{
1190
    /* error status */
1191
38.9k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
1192
1193
    /* bit stream ptr */
1194
38.9k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1195
1196
    /* packed header data */
1197
38.9k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1198
38.9k
    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
38.9k
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
1212
38.9k
    WORD8 mb_qp_delta;
1213
1214
    /* temp var */
1215
38.9k
    WORD32 i, mb_type_stream, cbptable = 1;
1216
1217
38.9k
    WORD32 is_inter = 0;
1218
1219
38.9k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1220
1221
    /* Starting bitstream offset for header in bits */
1222
38.9k
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
1223
1224
    /********************************************************************/
1225
    /*                    BEGIN HEADER GENERATION                       */
1226
    /********************************************************************/
1227
1228
    /* mb header info */
1229
38.9k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1230
1231
    /* mb type */
1232
38.9k
    mb_type = mb_tpm & 0xF;
1233
1234
    /* check for skip */
1235
38.9k
    if (mb_type == PSKIP)
1236
5.73k
    {
1237
5.73k
        UWORD32 *nnz;
1238
1239
5.73k
        is_inter = 1;
1240
1241
        /* increment skip counter */
1242
5.73k
        (*ps_ent_ctxt->pi4_mb_skip_run)++;
1243
1244
        /* store the index of the next mb syntax layer */
1245
5.73k
        pu1_byte += sizeof(mb_hdr_pskip_t);
1246
5.73k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1247
1248
        /* set nnz to zero */
1249
5.73k
        ps_ent_ctxt->u4_left_nnz_luma = 0;
1250
5.73k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
1251
5.73k
        *nnz = 0;
1252
5.73k
        ps_ent_ctxt->u4_left_nnz_cbcr = 0;
1253
5.73k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
1254
5.73k
        *nnz = 0;
1255
1256
        /* residual */
1257
5.73k
        error_status = ih264e_encode_residue(ps_ent_ctxt, P16x16, 0);
1258
1259
5.73k
        bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1260
1261
5.73k
        ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1262
1263
5.73k
        return error_status;
1264
5.73k
    }
1265
1266
    /* remaining mb header info */
1267
33.2k
    cbp = ps_mb_hdr->u1_cbp;
1268
33.2k
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
1269
1270
    /* mb skip run */
1271
33.2k
    PUT_BITS_UEV(ps_bitstream, *ps_ent_ctxt->pi4_mb_skip_run, error_status, "mb skip run");
1272
1273
    /* reset skip counter */
1274
32.1k
    *ps_ent_ctxt->pi4_mb_skip_run = 0;
1275
1276
    /* is intra ? */
1277
32.1k
    if (mb_type == I16x16)
1278
7.58k
    {
1279
7.58k
        UWORD32 u4_cbp_l, u4_cbp_c;
1280
1281
7.58k
        is_inter = 0;
1282
1283
7.58k
        u4_cbp_c = (cbp >> 4);
1284
7.58k
        u4_cbp_l = (cbp & 0xF);
1285
7.58k
        luma_intra_mode = (mb_tpm >> 4) & 3;
1286
7.58k
        chroma_intra_mode = (mb_tpm >> 6);
1287
1288
7.58k
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1289
1290
7.58k
        mb_type_stream += 5;
1291
1292
        /* write mb type */
1293
7.58k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1294
1295
        /* intra_chroma_pred_mode */
1296
7.49k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1297
7.46k
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1298
7.46k
    }
1299
24.5k
    else if (mb_type == I4x4)
1300
9.98k
    {
1301
9.98k
        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.98k
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1305
9.98k
        WORD32 byte;
1306
1307
9.98k
        is_inter = 0;
1308
1309
9.98k
        chroma_intra_mode = (mb_tpm >> 6);
1310
9.98k
        cbptable = 0;
1311
1312
        /* write mb type */
1313
9.98k
        PUT_BITS_UEV(ps_bitstream, 5, error_status, "mb type");
1314
1315
85.7k
        for (i = 0; i < 16; i += 2)
1316
76.3k
        {
1317
            /* sub blk idx 1 */
1318
76.3k
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1319
1320
76.3k
            intra_pred_mode_flag = byte & 0x1;
1321
1322
            /* prev_intra4x4_pred_mode_flag */
1323
76.3k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1324
1325
            /* rem_intra4x4_pred_mode */
1326
76.2k
            if (!intra_pred_mode_flag)
1327
35.1k
            {
1328
35.1k
                rem_intra_mode = (byte & 0xF) >> 1;
1329
35.1k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1330
35.0k
            }
1331
1332
            /* sub blk idx 2 */
1333
76.1k
            byte >>= 4;
1334
1335
76.1k
            intra_pred_mode_flag = byte & 0x1;
1336
1337
            /* prev_intra4x4_pred_mode_flag */
1338
76.1k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1339
1340
            /* rem_intra4x4_pred_mode */
1341
76.0k
            if (!intra_pred_mode_flag)
1342
29.7k
            {
1343
29.7k
                rem_intra_mode = (byte & 0xF) >> 1;
1344
29.7k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1345
29.6k
            }
1346
76.0k
        }
1347
1348
        /* intra_chroma_pred_mode */
1349
9.37k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1350
1351
9.28k
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1352
9.28k
    }
1353
14.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
14.5k
    else
1417
14.5k
    {
1418
14.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
14.5k
        const UWORD8 au1_part_cnt[] = { 1, 2, 2, 4 };
1422
1423
        /* mv ptr */
1424
14.5k
        WORD16 *pi2_mv_ptr = (WORD16 *)ps_mb_hdr_p16x16->ai2_mv;
1425
1426
        /* number of partitions for the current mb */
1427
14.5k
        UWORD32 u4_part_cnt = au1_part_cnt[mb_type - 3];
1428
1429
14.5k
        is_inter = 1;
1430
1431
        /* write mb type */
1432
14.5k
        PUT_BITS_UEV(ps_bitstream, mb_type - 3, error_status, "mb type");
1433
1434
28.8k
        for (i = 0; i < (WORD32)u4_part_cnt; i++)
1435
14.5k
        {
1436
14.5k
            PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv x");
1437
14.4k
            PUT_BITS_SEV(ps_bitstream, *pi2_mv_ptr++, error_status, "mv y");
1438
14.3k
        }
1439
1440
14.3k
        pu1_byte += sizeof(mb_hdr_p16x16_t);
1441
1442
14.3k
    }
1443
1444
    /* coded_block_pattern */
1445
31.1k
    if (mb_type != I16x16)
1446
23.6k
    {
1447
23.6k
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][cbptable], error_status, "coded_block_pattern");
1448
23.5k
    }
1449
1450
30.9k
    if (cbp || mb_type == I16x16)
1451
28.0k
    {
1452
        /* mb_qp_delta */
1453
28.0k
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1454
27.9k
    }
1455
1456
    /* Ending bitstream offset for header in bits */
1457
30.9k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1458
1459
30.9k
    ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1460
1461
    /* start bitstream offset for residue in bits */
1462
30.9k
    bitstream_start_offset = bitstream_end_offset;
1463
1464
    /* residual */
1465
30.9k
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1466
1467
    /* Ending bitstream offset for residue in bits */
1468
30.9k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1469
1470
30.9k
    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
30.9k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1474
1475
30.9k
    return error_status;
1476
30.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
35.8k
{
1502
    /* error status */
1503
35.8k
    IH264E_ERROR_T error_status = IH264E_SUCCESS;
1504
1505
    /* bit stream ptr */
1506
35.8k
    bitstrm_t *ps_bitstream = ps_ent_ctxt->ps_bitstrm;
1507
1508
    /* packed header data */
1509
35.8k
    UWORD8 *pu1_byte = ps_ent_ctxt->pv_mb_header_data;
1510
35.8k
    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
35.8k
    WORD32 mb_tpm, mb_type, cbp, chroma_intra_mode, luma_intra_mode;
1524
35.8k
    WORD8 mb_qp_delta;
1525
1526
    /* temp var */
1527
35.8k
    WORD32 i, mb_type_stream, cbptable = 1;
1528
1529
35.8k
    WORD32 is_inter = 0;
1530
1531
35.8k
    WORD32 bitstream_start_offset, bitstream_end_offset;
1532
1533
    /* Starting bitstream offset for header in bits */
1534
35.8k
    bitstream_start_offset = GET_NUM_BITS(ps_bitstream);
1535
1536
    /********************************************************************/
1537
    /*                    BEGIN HEADER GENERATION                       */
1538
    /********************************************************************/
1539
1540
35.8k
    mb_tpm = ps_mb_hdr->u1_mb_type_mode;
1541
1542
    /* mb type */
1543
35.8k
    mb_type = mb_tpm & 0xF;
1544
1545
    /* check for skip */
1546
35.8k
    if (mb_type == BSKIP)
1547
3.57k
    {
1548
3.57k
        UWORD32 *nnz;
1549
1550
3.57k
        is_inter = 1;
1551
1552
        /* increment skip counter */
1553
3.57k
        (*ps_ent_ctxt->pi4_mb_skip_run)++;
1554
1555
        /* store the index of the next mb syntax layer */
1556
3.57k
        pu1_byte += sizeof(mb_hdr_bskip_t);
1557
3.57k
        ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1558
1559
        /* set nnz to zero */
1560
3.57k
        ps_ent_ctxt->u4_left_nnz_luma = 0;
1561
3.57k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_luma[ps_ent_ctxt->i4_mb_x];
1562
3.57k
        *nnz = 0;
1563
3.57k
        ps_ent_ctxt->u4_left_nnz_cbcr = 0;
1564
3.57k
        nnz = (UWORD32 *)ps_ent_ctxt->pu1_top_nnz_cbcr[ps_ent_ctxt->i4_mb_x];
1565
3.57k
        *nnz = 0;
1566
1567
        /* residual */
1568
3.57k
        error_status = ih264e_encode_residue(ps_ent_ctxt, B16x16, 0);
1569
1570
3.57k
        bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1571
1572
3.57k
        ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset
1573
3.57k
                        - bitstream_start_offset;
1574
1575
3.57k
        return error_status;
1576
3.57k
    }
1577
1578
1579
    /* remaining mb header info */
1580
32.2k
    cbp = ps_mb_hdr->u1_cbp;
1581
32.2k
    mb_qp_delta = ps_mb_hdr->u1_mb_qp_delta;
1582
1583
    /* mb skip run */
1584
32.2k
    PUT_BITS_UEV(ps_bitstream, *ps_ent_ctxt->pi4_mb_skip_run, error_status, "mb skip run");
1585
1586
    /* reset skip counter */
1587
30.9k
    *ps_ent_ctxt->pi4_mb_skip_run = 0;
1588
1589
    /* is intra ? */
1590
30.9k
    if (mb_type == I16x16)
1591
3.88k
    {
1592
3.88k
        UWORD32 u4_cbp_l, u4_cbp_c;
1593
1594
3.88k
        is_inter = 0;
1595
1596
3.88k
        u4_cbp_c = (cbp >> 4);
1597
3.88k
        u4_cbp_l = (cbp & 0xF);
1598
3.88k
        luma_intra_mode = (mb_tpm >> 4) & 3;
1599
3.88k
        chroma_intra_mode = (mb_tpm >> 6);
1600
1601
3.88k
        mb_type_stream =  luma_intra_mode + 1 + (u4_cbp_c << 2) + (u4_cbp_l == 15) * 12;
1602
1603
3.88k
        mb_type_stream += 23;
1604
1605
        /* write mb type */
1606
3.88k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1607
1608
        /* intra_chroma_pred_mode */
1609
3.83k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1610
3.82k
        pu1_byte += sizeof(mb_hdr_i16x16_t);
1611
1612
3.82k
    }
1613
27.0k
    else if (mb_type == I4x4)
1614
6.73k
    {
1615
6.73k
        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
6.73k
        WORD32 intra_pred_mode_flag, rem_intra_mode;
1619
6.73k
        WORD32 byte;
1620
1621
6.73k
        is_inter = 0;
1622
1623
6.73k
        chroma_intra_mode = (mb_tpm >> 6);
1624
6.73k
        cbptable = 0;
1625
1626
        /* write mb type */
1627
6.73k
        PUT_BITS_UEV(ps_bitstream, 23, error_status, "mb type");
1628
1629
57.5k
        for (i = 0; i < 16; i += 2)
1630
51.3k
        {
1631
            /* sub blk idx 1 */
1632
51.3k
            byte = ps_mb_hdr_i4x4->au1_sub_blk_modes[i >> 1];
1633
1634
51.3k
            intra_pred_mode_flag = byte & 0x1;
1635
1636
            /* prev_intra4x4_pred_mode_flag */
1637
51.3k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1638
1639
            /* rem_intra4x4_pred_mode */
1640
51.2k
            if (!intra_pred_mode_flag)
1641
13.2k
            {
1642
13.2k
                rem_intra_mode = (byte & 0xF) >> 1;
1643
13.2k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1644
13.2k
            }
1645
1646
            /* sub blk idx 2 */
1647
51.2k
            byte >>= 4;
1648
1649
51.2k
            intra_pred_mode_flag = byte & 0x1;
1650
1651
            /* prev_intra4x4_pred_mode_flag */
1652
51.2k
            PUT_BITS(ps_bitstream, intra_pred_mode_flag, 1, error_status, "prev_intra4x4_pred_mode_flag");
1653
1654
            /* rem_intra4x4_pred_mode */
1655
51.1k
            if (!intra_pred_mode_flag)
1656
15.2k
            {
1657
15.2k
                rem_intra_mode = (byte & 0xF) >> 1;
1658
15.2k
                PUT_BITS(ps_bitstream, rem_intra_mode, 3, error_status, "rem_intra4x4_pred_mode");
1659
15.1k
            }
1660
51.1k
        }
1661
1662
        /* intra_chroma_pred_mode */
1663
6.28k
        PUT_BITS_UEV(ps_bitstream, chroma_intra_mode, error_status, "intra_chroma_pred_mode");
1664
6.27k
        pu1_byte += sizeof(mb_hdr_i4x4_t);
1665
1666
6.27k
    }
1667
20.3k
    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
20.3k
    else if(mb_type == BDIRECT)
1731
4.53k
    {
1732
4.53k
        is_inter = 1;
1733
        /* write mb type */
1734
4.53k
        PUT_BITS_UEV(ps_bitstream, B_DIRECT_16x16, error_status, "mb type");
1735
4.52k
        pu1_byte += sizeof(mb_hdr_bdirect_t);
1736
1737
4.52k
    }
1738
15.8k
    else /* if mb_type == B16x16 */
1739
15.8k
    {
1740
15.8k
        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.8k
        const UWORD8 au1_part_cnt[] = { 1, 2, 2, 4 };
1744
1745
        /* number of partitions for the current mb */
1746
15.8k
        UWORD32 u4_part_cnt = au1_part_cnt[mb_type - B16x16];
1747
1748
        /* Get the pred modes */
1749
15.8k
        WORD32 i4_mb_part_pred_mode = (mb_tpm >> 4);
1750
1751
15.8k
        is_inter = 1;
1752
1753
15.8k
        mb_type_stream = mb_type - B16x16 + B_L0_16x16 + i4_mb_part_pred_mode;
1754
1755
        /* write mb type */
1756
15.8k
        PUT_BITS_UEV(ps_bitstream, mb_type_stream, error_status, "mb type");
1757
1758
31.1k
        for (i = 0; i < (WORD32)u4_part_cnt; i++)
1759
15.6k
        {
1760
15.6k
            if (i4_mb_part_pred_mode != PRED_L1)/* || PRED_BI */
1761
8.33k
            {
1762
8.33k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[0][0], error_status, "mv l0 x");
1763
8.27k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[0][1], error_status, "mv l0 y");
1764
8.24k
            }
1765
15.5k
            if (i4_mb_part_pred_mode != PRED_L0)/* || PRED_BI */
1766
9.15k
            {
1767
9.15k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[1][0], error_status, "mv l1 x");
1768
9.08k
                PUT_BITS_SEV(ps_bitstream, ps_mb_hdr_b16x16->ai2_mv[1][1], error_status, "mv l1 y");
1769
9.06k
            }
1770
15.5k
        }
1771
1772
15.4k
        pu1_byte += sizeof(mb_hdr_b16x16_t);
1773
15.4k
    }
1774
1775
    /* coded_block_pattern */
1776
30.1k
    if (mb_type != I16x16)
1777
26.2k
    {
1778
26.2k
        PUT_BITS_UEV(ps_bitstream, gu1_cbp_map_tables[cbp][cbptable], error_status, "coded_block_pattern");
1779
26.1k
    }
1780
1781
30.0k
    if (cbp || mb_type == I16x16)
1782
27.0k
    {
1783
        /* mb_qp_delta */
1784
27.0k
        PUT_BITS_SEV(ps_bitstream, mb_qp_delta, error_status, "mb_qp_delta");
1785
27.0k
    }
1786
1787
    /* Ending bitstream offset for header in bits */
1788
29.9k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1789
1790
29.9k
    ps_ent_ctxt->u4_header_bits[is_inter] += bitstream_end_offset - bitstream_start_offset;
1791
1792
    /* start bitstream offset for residue in bits */
1793
29.9k
    bitstream_start_offset = bitstream_end_offset;
1794
1795
    /* residual */
1796
29.9k
    error_status = ih264e_encode_residue(ps_ent_ctxt, mb_type, cbp);
1797
1798
    /* Ending bitstream offset for residue in bits */
1799
29.9k
    bitstream_end_offset = GET_NUM_BITS(ps_bitstream);
1800
1801
29.9k
    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
29.9k
    ps_ent_ctxt->pv_mb_header_data = pu1_byte;
1805
1806
29.9k
    return error_status;
1807
30.0k
}