forked from ittiam-systems/libavc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathih264e_cabac_encode.c
2339 lines (2119 loc) · 81.8 KB
/
ih264e_cabac_encode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*****************************************************************************
* Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
* ih264e_cabac_encode.c
*
* @brief
* Contains all functions to encode in CABAC entropy mode
*
* @author
* ittiam
*
* @par List of Functions
* - ih264e_cabac_enc_mb_skip
* - ih264e_cabac_enc_intra_mb_type
* - ih264e_cabac_enc_4x4mb_modes
* - ih264e_cabac_enc_chroma_predmode
* - ih264e_cabac_enc_cbp
* - ih264e_cabac_enc_mb_qp_delta
* - ih264e_cabac_write_coeff4x4
* - ih264e_cabac_encode_residue_luma_dc
* - ih264e_cabac_write_chroma_residue
* - ih264e_cabac_encode_residue
* - ih264e_cabac_enc_ctx_mvd
* - ih264e_cabac_enc_mvds_p16x16
* - ih264e_cabac_enc_mvds_b16x16
* - ih264e_write_islice_mb_cabac
* - ih264e_write_pslice_mb_cabac
* - ih264e_write_bslice_mb_cabac
*
* @remarks
* none
*
*******************************************************************************
*/
/*****************************************************************************/
/* File Includes */
/*****************************************************************************/
/* System Include Files */
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
/* User Include Files */
#include "ih264e_config.h"
#include "ih264_typedefs.h"
#include "iv2.h"
#include "ive2.h"
#include "ih264_debug.h"
#include "ih264_macros.h"
#include "ih264_error.h"
#include "ih264_defs.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "ih264_cavlc_tables.h"
#include "ih264_cabac_tables.h"
#include "ih264_platform_macros.h"
#include "ime_defs.h"
#include "ime_distortion_metrics.h"
#include "ime_structs.h"
#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"
#include "ih264e_error.h"
#include "ih264e_defs.h"
#include "ih264e_bitstream.h"
#include "ih264e_cabac_structs.h"
#include "ih264e_structs.h"
#include "ih264e_encode_header.h"
#include "ih264e_cabac.h"
#include "ih264e_statistics.h"
#include "ih264e_trace.h"
/*****************************************************************************/
/* Global Definitions */
/*****************************************************************************/
/* ! < Table 9-36 : Binarization for macroblock types in I slices in ITU_T_H264
* Bits 0-7 : binarised value
* Bits 8-15: length of binary sequence
*/
static const UWORD32 u4_mb_type_intra[26] =
{ 0x0100, 0x0620, 0x0621, 0x0622, 0x0623, 0x0748, 0x0749, 0x074a, 0x074b,
0x074c, 0x074d, 0x074e, 0x074f, 0x0628, 0x0629, 0x062a, 0x062b, 0x0758,
0x0759, 0x075a, 0x075b, 0x075c, 0x075d, 0x075e, 0x075f, 0x0203 };
/* CtxInc for mb types */
static const UWORD32 u4_mb_ctxinc[2][26] =
{
/* Intra CtxInc's */
{ 0x00,
0x03467, 0x03467, 0x03467, 0x03467, 0x034567, 0x034567, 0x034567,
0x034567, 0x034567, 0x034567, 0x034567, 0x034567, 0x03467, 0x03467,
0x03467, 0x03467, 0x034567, 0x034567, 0x034567, 0x034567, 0x034567,
0x034567, 0x034567, 0x034567, 0x00},
/* Inter CtxInc's */
{ 0x00,
0x001233, 0x001233, 0x001233, 0x001233, 0x0012233, 0x0012233, 0x0012233,
0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x001233, 0x001233,
0x001233, 0x001233, 0x0012233, 0x0012233, 0x0012233, 0x0012233, 0x0012233,
0x0012233, 0x0012233, 0x0012233, 0x00}
};
/* ! < Table 9-37 : Binarization for macroblock types in B slices in ITU_T_H264-201402
* Bits 0-7 : binarised value
* Bits 8-15: length of binary sequence */
static const UWORD32 u4_b_mb_type[27] =
{ 0x0100, 0x0301, 0x0305, 0x0603, 0x0623, 0x0613, 0x0633, 0x060b, 0x062b,
0x061b, 0x063b, 0x061f, 0x0707, 0x0747, 0x0727, 0x0767, 0x0717, 0x0757,
0x0737, 0x0777, 0x070f, 0x074f, 0x063f };
/* CtxInc for mb types in B slices */
static const UWORD32 ui_b_mb_type_ctx_inc[27] =
{ 0x00, 0x0530, 0x0530, 0x0555430, 0x0555430, 0x0555430, 0x0555430,
0x0555430, 0x0555430, 0x0555430, 0x0555430, 0x0555430, 0x05555430,
0x05555430, 0x05555430, 0x05555430, 0x05555430, 0x05555430, 0x05555430,
0x05555430, 0x05555430, 0x05555430, 0x0555430 };
/*****************************************************************************/
/* Function Definitions */
/*****************************************************************************/
/**
*******************************************************************************
*
* @brief
* Encodes mb_skip_flag using CABAC entropy coding mode.
*
* @param[in] u1_mb_skip_flag
* mb_skip_flag
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @param[in] u4_ctxidx_offset
* ctxIdxOffset for mb_skip_flag context
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_mb_skip(UWORD8 u1_mb_skip_flag,
cabac_ctxt_t *ps_cabac_ctxt,
UWORD32 u4_ctxidx_offset)
{
UWORD8 u4_ctx_inc;
WORD8 a, b;
a = ((ps_cabac_ctxt->ps_left_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ?
0 : 1);
b = ((ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_mb_type & CAB_SKIP_MASK) ?
0 : 1);
u4_ctx_inc = a + b;
/* Encode the bin */
ih264e_cabac_encode_bin(ps_cabac_ctxt,
(UWORD32) u1_mb_skip_flag,
ps_cabac_ctxt->au1_cabac_ctxt_table +
u4_ctxidx_offset + u4_ctx_inc);
}
/**
*******************************************************************************
*
* @brief
* Encodes mb_type for an intra MB.
*
* @param[in] u4_slice_type
* slice type
*
* @param[in] u4_intra_mb_type
* MB type (Table 7-11)
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @param[in] u4_ctxidx_offset
* ctxIdxOffset for mb_type context
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_intra_mb_type(UWORD32 u4_slice_type,
UWORD32 u4_intra_mb_type,
cabac_ctxt_t *ps_cabac_ctxt,
UWORD32 u4_ctx_idx_offset)
{
encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
bin_ctxt_model *pu1_mb_bin_ctxt, *pu1_bin_ctxt;
UWORD8 u1_bin;
mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
UWORD32 u4_bins;
UWORD32 u4_ctx_inc;
WORD8 i1_bins_len;
UWORD32 u4_code_int_range;
UWORD32 u4_code_int_low;
UWORD16 u2_quant_code_int_range;
UWORD16 u4_code_int_range_lps;
WORD8 i;
UWORD8 u1_ctx_inc;
UWORD32 u4_table_val;
pu1_mb_bin_ctxt = ps_cabac_ctxt->au1_cabac_ctxt_table + u4_ctx_idx_offset;
u4_bins = u4_mb_type_intra[u4_intra_mb_type];
i1_bins_len = (WORD8) ((u4_bins >> 8) & 0x0f);
u4_ctx_inc = u4_mb_ctxinc[(u4_slice_type != ISLICE)][u4_intra_mb_type];
u1_ctx_inc = 0;
if (u4_slice_type == ISLICE)
{
if (ps_left_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
u1_ctx_inc += ((ps_left_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
if (ps_top_ctxt != ps_cabac_ctxt->ps_def_ctxt_mb_info)
u1_ctx_inc += ((ps_top_ctxt->u1_mb_type != CAB_I4x4) ? 1 : 0);
u4_ctx_inc = (u4_ctx_inc | (u1_ctx_inc << ((i1_bins_len - 1) << 2)));
}
else
{
pu1_mb_bin_ctxt += 3;
if (u4_slice_type == BSLICE)
pu1_mb_bin_ctxt += 2;
}
u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
for (i = (i1_bins_len - 1); i >= 0; i--)
{
WORD32 shift;
u1_ctx_inc = ((u4_ctx_inc >> (i << 2)) & 0x0f);
u1_bin = ((u4_bins >> i) & 0x01);
/* Encode the bin */
pu1_bin_ctxt = pu1_mb_bin_ctxt + u1_ctx_inc;
if (i != (i1_bins_len - 2))
{
WORD8 i1_mps = !!((*pu1_bin_ctxt) & (0x40));
WORD8 i1_state = (*pu1_bin_ctxt) & 0x3F;
u2_quant_code_int_range = ((u4_code_int_range >> 6) & 0x03);
u4_table_val = gau4_ih264_cabac_table[i1_state][u2_quant_code_int_range];
u4_code_int_range_lps = u4_table_val & 0xFF;
u4_code_int_range -= u4_code_int_range_lps;
if (u1_bin != i1_mps)
{
u4_code_int_low += u4_code_int_range;
u4_code_int_range = u4_code_int_range_lps;
if (i1_state == 0)
{
/* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
i1_mps = 1 - i1_mps;
}
i1_state = (u4_table_val >> 15) & 0x3F;
}
else
{
i1_state = (u4_table_val >> 8) & 0x3F;
}
(*pu1_bin_ctxt) = (i1_mps << 6) | i1_state;
}
else
{
u4_code_int_range -= 2;
}
/* Renormalize */
/*****************************************************************/
/* Renormalization; calculate bits generated based on range(R) */
/* Note : 6 <= R < 512; R is 2 only for terminating encode */
/*****************************************************************/
GETRANGE(shift, u4_code_int_range);
shift = 9 - shift;
u4_code_int_low <<= shift;
u4_code_int_range <<= shift;
/* bits to be inserted in the bitstream */
ps_cab_enc_env->u4_bits_gen += shift;
ps_cab_enc_env->u4_code_int_range = u4_code_int_range;
ps_cab_enc_env->u4_code_int_low = u4_code_int_low;
/* generate stream when a byte is ready */
if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
{
ih264e_cabac_put_byte(ps_cabac_ctxt);
u4_code_int_range = ps_cab_enc_env->u4_code_int_range;
u4_code_int_low = ps_cab_enc_env->u4_code_int_low;
}
}
}
/**
*******************************************************************************
*
* @brief
* Encodes prev_intra4x4_pred_mode_flag and rem_intra4x4_pred_mode using
* CABAC entropy coding mode
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @param[in] pu1_intra_4x4_modes
* Pointer to array containing prev_intra4x4_pred_mode_flag and
* rem_intra4x4_pred_mode
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_4x4mb_modes(cabac_ctxt_t *ps_cabac_ctxt,
UWORD8 *pu1_intra_4x4_modes)
{
WORD32 i;
WORD8 byte;
for (i = 0; i < 16; i += 2)
{
/* sub blk idx 1 */
byte = pu1_intra_4x4_modes[i >> 1];
if (byte & 0x1)
{
ih264e_cabac_encode_bin(ps_cabac_ctxt,
1,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ PREV_INTRA4X4_PRED_MODE_FLAG);
}
else
{
/* Binarization is FL and Cmax=7 */
ih264e_encode_decision_bins(byte & 0xF,
4,
0x05554,
4,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ REM_INTRA4X4_PRED_MODE - 5,
ps_cabac_ctxt);
}
/* sub blk idx 2 */
byte >>= 4;
if (byte & 0x1)
{
ih264e_cabac_encode_bin(ps_cabac_ctxt,
1,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ PREV_INTRA4X4_PRED_MODE_FLAG);
}
else
{
ih264e_encode_decision_bins(byte & 0xF,
4,
0x05554,
4,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ REM_INTRA4X4_PRED_MODE - 5,
ps_cabac_ctxt);
}
}
}
/**
*******************************************************************************
*
* @brief
* Encodes chroma intra pred mode for the MB.
*
* @param[in] u1_chroma_pred_mode
* Chroma intra prediction mode
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_chroma_predmode(UWORD8 u1_chroma_pred_mode,
cabac_ctxt_t *ps_cabac_ctxt)
{
WORD8 i1_temp;
mb_info_ctxt_t *ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
UWORD32 u4_bins = 0;
WORD8 i1_bins_len = 1;
UWORD32 u4_ctx_inc = 0;
UWORD8 a, b;
a = ((ps_left_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
b = ((ps_top_ctxt->u1_intrapred_chroma_mode != 0) ? 1 : 0);
/* Binarization is TU and Cmax=3 */
ps_curr_ctxt->u1_intrapred_chroma_mode = u1_chroma_pred_mode;
u4_ctx_inc = a + b;
u4_ctx_inc = (u4_ctx_inc | 0x330);
if (u1_chroma_pred_mode)
{
u4_bins = 1;
i1_temp = u1_chroma_pred_mode;
i1_temp--;
/* Put a stream of 1's of length Chromaps_pred_mode_ctxt value */
while (i1_temp)
{
u4_bins = (u4_bins | (1 << i1_bins_len));
i1_bins_len++;
i1_temp--;
}
/* If Chromaps_pred_mode_ctxt < Cmax i.e 3. Terminate put a zero */
if (u1_chroma_pred_mode < 3)
{
i1_bins_len++;
}
}
ih264e_encode_decision_bins(u4_bins,
i1_bins_len,
u4_ctx_inc,
3,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ INTRA_CHROMA_PRED_MODE,
ps_cabac_ctxt);
}
/**
*******************************************************************************
*
* @brief Encodes CBP for the MB.
*
* @param[in] u1_cbp
* CBP for the MB
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_cbp(UWORD32 u4_cbp, cabac_ctxt_t *ps_cabac_ctxt)
{
mb_info_ctxt_t *ps_left_ctxt = ps_cabac_ctxt->ps_left_ctxt_mb_info;
mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
WORD8 i2_cbp_chroma, i, j;
UWORD8 u1_ctxt_inc, u1_bin;
UWORD8 a, b;
UWORD32 u4_ctx_inc;
UWORD32 u4_bins;
WORD8 i1_bins_len;
/* CBP Luma, FL, Cmax = 15, L = 4 */
u4_ctx_inc = 0;
u4_bins = 0;
i1_bins_len = 5;
for (i = 0; i < 4; i++)
{
/* calulate ctxtInc, depending on neighbour availability */
/* u1_ctxt_inc = CondTerm(A) + 2 * CondTerm(B);
A: Left block and B: Top block */
/* Check for Top availability */
if (i >> 1)
{
j = i - 2;
/* Top is available always and it's current MB */
b = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
}
else
{
/* for blocks whose top reference is in another MB */
{
j = i + 2;
b = ((ps_top_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
}
}
/* Check for Left availability */
if (i & 0x01)
{
/* Left is available always and it's current MB */
j = i - 1;
a = (((u4_cbp >> j) & 0x01) != 0 ? 0 : 1);
}
else
{
{
j = i + 1;
a = ((ps_left_ctxt->u1_cbp >> j) & 0x01) ? 0 : 1;
}
}
u1_ctxt_inc = a + 2 * b;
u1_bin = ((u4_cbp >> i) & 0x01);
u4_ctx_inc = (u4_ctx_inc | (u1_ctxt_inc << (i << 2)));
u4_bins = (u4_bins | (u1_bin << i));
}
/* CBP Chroma, TU, Cmax = 2 */
i2_cbp_chroma = u4_cbp >> 4;
/* calulate ctxtInc, depending on neighbour availability */
a = (ps_left_ctxt->u1_cbp > 15) ? 1 : 0;
b = (ps_top_ctxt->u1_cbp > 15) ? 1 : 0;
u1_ctxt_inc = a + 2 * b;
if (i2_cbp_chroma)
{
u4_ctx_inc = u4_ctx_inc | ((4 + u1_ctxt_inc) << 16);
u4_bins = (u4_bins | 0x10);
/* calulate ctxtInc, depending on neighbour availability */
a = (ps_left_ctxt->u1_cbp > 31) ? 1 : 0;
b = (ps_top_ctxt->u1_cbp > 31) ? 1 : 0;
u1_ctxt_inc = a + 2 * b;
u4_ctx_inc = u4_ctx_inc | ((8 + u1_ctxt_inc) << 20);
u4_bins = (u4_bins | (((i2_cbp_chroma >> 1) & 0x01) << i1_bins_len));
i1_bins_len++;
}
else
{
u4_ctx_inc = (u4_ctx_inc | ((4 + u1_ctxt_inc) << 16));
}
ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 8,
ps_cabac_ctxt->au1_cabac_ctxt_table + CBP_LUMA,
ps_cabac_ctxt);
}
/**
*******************************************************************************
*
* @brief Encodes mb_qp_delta for the MB.
*
* @param[in] i1_mb_qp_delta
* mb_qp_delta
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_enc_mb_qp_delta(WORD8 i1_mb_qp_delta,
cabac_ctxt_t *ps_cabac_ctxt)
{
UWORD8 u1_code_num;
UWORD8 u1_ctxt_inc;
UWORD32 u4_ctx_inc;
UWORD32 u4_bins;
WORD8 i1_bins_len;
UWORD8 u1_ctx_inc, u1_bin;
/* Range of ps_mb_qp_delta_ctxt= -26 to +25 inclusive */
ASSERT((i1_mb_qp_delta < 26) && (i1_mb_qp_delta > -27));
/* if ps_mb_qp_delta_ctxt=0, then codeNum=0 */
u1_code_num = 0;
if (i1_mb_qp_delta > 0)
u1_code_num = (i1_mb_qp_delta << 1) - 1;
else if (i1_mb_qp_delta < 0)
u1_code_num = (ABS(i1_mb_qp_delta)) << 1;
u4_ctx_inc = 0;
u4_bins = 0;
i1_bins_len = 1;
/* calculate ctxtInc, depending on neighbour availability */
u1_ctxt_inc = (!(!(ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt)));
ps_cabac_ctxt->i1_prev_mb_qp_delta_ctxt = i1_mb_qp_delta;
if (u1_code_num == 0)
{
/* b0 */
u1_bin = (UWORD8) (u4_bins);
u1_ctx_inc = u1_ctxt_inc & 0x0f;
/* Encode the bin */
ih264e_cabac_encode_bin(ps_cabac_ctxt,
u1_bin,
ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA
+ u1_ctx_inc);
}
else
{
/* b0 */
u4_ctx_inc = u1_ctxt_inc;
u4_bins = 1;
u1_code_num--;
if (u1_code_num == 0)
{
/* b1 */
u4_ctx_inc = (u4_ctx_inc | 0x20);
i1_bins_len++;
ih264e_encode_decision_bins(u4_bins, i1_bins_len, u4_ctx_inc, 3,
ps_cabac_ctxt->au1_cabac_ctxt_table + MB_QP_DELTA,
ps_cabac_ctxt);
}
else
{
/* b1 */
u4_ctx_inc = (u4_ctx_inc | 0x20);
u4_bins = (u4_bins | (1 << i1_bins_len));
i1_bins_len++;
u1_code_num--;
/* BinIdx from b2 onwards */
if (u1_code_num < 30)
{ /* maximum i1_bins_len = 31 */
while (u1_code_num)
{
u4_bins = (u4_bins | (1 << i1_bins_len));
i1_bins_len++;
u1_code_num--;
};
u4_ctx_inc = (u4_ctx_inc | 0x300);
i1_bins_len++;
ih264e_encode_decision_bins(u4_bins,
i1_bins_len,
u4_ctx_inc,
2,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ MB_QP_DELTA,
ps_cabac_ctxt);
}
else
{
/* maximum i1_bins_len = 53 */
u4_bins = 0xffffffff;
i1_bins_len = 32;
u4_ctx_inc = (u4_ctx_inc | 0x300);
u1_code_num -= 30;
ih264e_encode_decision_bins(u4_bins,
i1_bins_len,
u4_ctx_inc,
2,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ MB_QP_DELTA,
ps_cabac_ctxt);
u4_bins = 0;
i1_bins_len = 0;
u4_ctx_inc = 0x033;
while (u1_code_num)
{
u4_bins = (u4_bins | (1 << i1_bins_len));
i1_bins_len++;
u1_code_num--;
};
u4_ctx_inc = (u4_ctx_inc | 0x300);
i1_bins_len++;
ih264e_encode_decision_bins(u4_bins,
i1_bins_len,
u4_ctx_inc,
1,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ MB_QP_DELTA,
ps_cabac_ctxt);
}
}
}
}
/**
*******************************************************************************
* @brief
* Encodes 4x4 residual_block_cabac as defined in 7.3.5.3.3.
*
* @param[in] pi2_res_block
* pointer to the array of residues
*
* @param[in] u1_nnz
* Number of non zero coeffs in the block
*
* @param[in] u1_max_num_coeffs
* Max number of coeffs that can be there in the block
*
* @param[in] u2_sig_coeff_map
* Significant coeff map
*
* @param[in] u4_ctx_cat_offset
* ctxIdxOffset for absolute value contexts
*
* @param[in] pu1_ctxt_sig_coeff
* Pointer to residual state variables
*
* @param[in] ps_cabac_ctxt
* Pointer to cabac context structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_write_coeff4x4(WORD16 *pi2_res_block, UWORD8 u1_nnz,
UWORD8 u1_max_num_coeffs,
UWORD16 u2_sig_coeff_map,
UWORD32 u4_ctx_cat_offset,
bin_ctxt_model *pu1_ctxt_sig_coeff,
cabac_ctxt_t *ps_cabac_ctxt)
{
WORD8 i;
WORD16 *pi16_coeffs;
UWORD32 u4_sig_coeff, u4_bins;
UWORD32 u4_ctx_inc;
UWORD8 u1_last_sig_coef_index = (31 - CLZ(u2_sig_coeff_map));
/* Always put Coded Block Flag as 1 */
pi16_coeffs = pi2_res_block;
{
bin_ctxt_model *pu1_bin_ctxt;
UWORD8 u1_bin, uc_last;
i = 0;
pu1_bin_ctxt = pu1_ctxt_sig_coeff;
u4_sig_coeff = 0;
u1_bin = 1;
if ((u1_last_sig_coef_index))
{
u1_bin = !!(u2_sig_coeff_map & 01);
}
uc_last = 1;
do
{
/* Encode Decision */
ih264e_cabac_encode_bin(ps_cabac_ctxt, u1_bin, pu1_bin_ctxt);
if (u1_bin & uc_last)
{
u4_sig_coeff = (u4_sig_coeff | (1 << i));
pu1_bin_ctxt = pu1_ctxt_sig_coeff + i
+ LAST_SIGNIFICANT_COEFF_FLAG_FRAME
- SIGNIFICANT_COEFF_FLAG_FRAME;
u1_bin = (i == u1_last_sig_coef_index);
uc_last = 0;
}
else
{
i = i + 1;
pu1_bin_ctxt = pu1_ctxt_sig_coeff + i;
u1_bin = (i == u1_last_sig_coef_index);
uc_last = 1;
if ((i != u1_last_sig_coef_index))
{
u1_bin = !!((u2_sig_coeff_map >> i) & 01);
}
}
} while (!((i > u1_last_sig_coef_index) || (i > (u1_max_num_coeffs - 1))));
}
/* Encode coeff_abs_level_minus1 and coeff_sign_flag */
{
UWORD8 u1_sign;
UWORD16 u2_abs_level;
UWORD8 u1_abs_level_equal1 = 1, u1_abs_level_gt1 = 0;
UWORD8 u1_ctx_inc;
UWORD8 u1_coff;
WORD16 i2_sufs;
WORD8 i1_bins_len;
i = u1_last_sig_coef_index;
pi16_coeffs = pi2_res_block + u1_nnz - 1;
do
{
{
u4_sig_coeff = u4_sig_coeff & ((1 << i) - 1);
u4_bins = 0;
u4_ctx_inc = 0;
i1_bins_len = 1;
/* Encode the AbsLevelMinus1 */
u2_abs_level = ABS(*(pi16_coeffs)) - 1;
/* CtxInc for bin0 */
u4_ctx_inc = MIN(u1_abs_level_equal1, 4);
/* CtxInc for remaining */
u1_ctx_inc = 5 + MIN(u1_abs_level_gt1, 4);
u4_ctx_inc = u4_ctx_inc + (u1_ctx_inc << 4);
if (u2_abs_level)
{
u1_abs_level_gt1++;
u1_abs_level_equal1 = 0;
}
if (!u1_abs_level_gt1)
u1_abs_level_equal1++;
u1_coff = 14;
if (u2_abs_level >= u1_coff)
{
/* Prefix TU i.e string of 14 1's */
u4_bins = 0x3fff;
i1_bins_len = 14;
ih264e_encode_decision_bins(
u4_bins,
i1_bins_len,
u4_ctx_inc,
1,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ u4_ctx_cat_offset,
ps_cabac_ctxt);
/* Suffix, uses EncodeBypass */
i2_sufs = u2_abs_level - u1_coff;
u4_bins = ih264e_cabac_UEGk0_binarization(i2_sufs,
&i1_bins_len);
ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins,
i1_bins_len);
}
else
{
/* Prefix only */
u4_bins = (1 << u2_abs_level) - 1;
i1_bins_len = u2_abs_level + 1;
/* Encode Terminating bit */
ih264e_encode_decision_bins(
u4_bins,
i1_bins_len,
u4_ctx_inc,
1,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ u4_ctx_cat_offset,
ps_cabac_ctxt);
}
}
/* encode coeff_sign_flag[i] */
u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0;
ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign);
i = CLZ(u4_sig_coeff);
i = 31 - i;
pi16_coeffs--;
} while (u4_sig_coeff);
}
}
/**
*******************************************************************************
* @brief
* Write DC coeffs for intra predicted luma block
*
* @param[in] ps_ent_ctxt
* Pointer to entropy context structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_encode_residue_luma_dc(entropy_ctxt_t *ps_ent_ctxt)
{
cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
tu_sblk_coeff_data_t *ps_mb_coeff_data;
void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
UWORD16 u2_sig_coeff_map;
WORD16 *pi2_res_block;
UWORD8 u1_nnz;
UWORD8 u1_cbf;
mb_info_ctxt_t *ps_top_ctxt = ps_cabac_ctxt->ps_top_ctxt_mb_info;
mb_info_ctxt_t *p_CurCtxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data, u1_nnz,
u2_sig_coeff_map, pi2_res_block);
u1_cbf = !!(u1_nnz);
{
UWORD32 u4_ctx_inc;
UWORD8 u1_a, u1_b;
u1_a = ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] & 0x1;
u1_b = ps_top_ctxt->u1_yuv_dc_csbp & 0x1;
u4_ctx_inc = u1_a + (u1_b << 1);
ih264e_cabac_encode_bin(ps_cabac_ctxt,
u1_cbf,
ps_cabac_ctxt->au1_cabac_ctxt_table + CBF
+ (LUMA_DC_CTXCAT << 2) + u4_ctx_inc);
}
/* Write coded_block_flag */
if (u1_cbf)
{
ih264e_cabac_write_coeff4x4(pi2_res_block,
u1_nnz,
15,
u2_sig_coeff_map,
COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET,
ps_cabac_ctxt->au1_cabac_ctxt_table
+ SIGNIFICANT_COEFF_FLAG_FRAME
+ SIG_COEFF_CTXT_CAT_0_OFFSET,
ps_cabac_ctxt);
ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] |= 0x1;
p_CurCtxt->u1_yuv_dc_csbp |= 0x1;
}
else
{
ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0] &= 0x6;
p_CurCtxt->u1_yuv_dc_csbp &= 0x6;
}
ps_ent_ctxt->pv_mb_coeff_data = pv_mb_coeff_data;
}
/**
*******************************************************************************
* @brief
* Write chroma residues to the bitstream
*
* @param[in] ps_ent_ctxt
* Pointer to entropy context structure
*
* @param[in] u1_chroma_cbp
* coded block pattern, chroma
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
static void ih264e_cabac_write_chroma_residue(entropy_ctxt_t *ps_ent_ctxt,
UWORD8 u1_chroma_cbp)
{
cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
tu_sblk_coeff_data_t *ps_mb_coeff_data;
void *pv_mb_coeff_data = ps_ent_ctxt->pv_mb_coeff_data;
UWORD16 u2_sig_coeff_map;
UWORD8 u1_nnz;
mb_info_ctxt_t *ps_top_ctxt_mb_info, *ps_curr_ctxt;
ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_top_ctxt_mb_info;
ps_curr_ctxt = ps_cabac_ctxt->ps_curr_ctxt_mb_info;
/********************/
/* Write Chroma DC */
/********************/
{
WORD16 *pi2_res_block;
UWORD8 u1_left_dc_csbp, u1_top_dc_csbp, u1_uv, u1_cbf;
u1_left_dc_csbp = (ps_cabac_ctxt->pu1_left_yuv_dc_csbp[0]) >> 1;
u1_top_dc_csbp = (ps_top_ctxt_mb_info->u1_yuv_dc_csbp) >> 1;
for (u1_uv = 0; u1_uv < 2; u1_uv++)
{
PARSE_COEFF_DATA_BLOCK_4x4(pv_mb_coeff_data, ps_mb_coeff_data,
u1_nnz, u2_sig_coeff_map, pi2_res_block);
u1_cbf = !!(u1_nnz);
{
UWORD8 u1_a, u1_b;
UWORD32 u4_ctx_inc;
u1_a = (u1_left_dc_csbp >> u1_uv) & 0x01;
u1_b = (u1_top_dc_csbp >> u1_uv) & 0x01;