summaryrefslogtreecommitdiffstats
path: root/encoder/ih264e_core_coding.c
blob: 89243a50cdc2d3efa82bbc70d30f5c3d129a1f7d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
/******************************************************************************
 *
 * 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_core_coding.c
 *
 * @brief
 *  This file contains routines that perform luma and chroma core coding for
 *  intra macroblocks
 *
 * @author
 *  ittiam
 *
 * @par List of Functions:
 *  - ih264e_pack_l_mb_i16()
 *  - ih264e_pack_c_mb_i8()
 *  - ih264e_code_luma_intra_macroblock_16x16()
 *  - ih264e_code_luma_intra_macroblock_4x4()
 *  - ih264e_code_chroma_intra_macroblock_8x8()
 *
 * @remarks
 *  None
 *
 *******************************************************************************
 */

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/

/* System include files */
#include <stdio.h>
#include <string.h>
#include <assert.h>

/* User include files */
#include "ih264e_config.h"
#include "ih264_typedefs.h"
#include "ih264_platform_macros.h"
#include "iv2.h"
#include "ive2.h"
#include "ih264_macros.h"
#include "ih264_defs.h"
#include "ih264e_defs.h"
#include "ih264_trans_data.h"
#include "ih264e_error.h"
#include "ih264e_bitstream.h"
#include "ime_distortion_metrics.h"
#include "ime_structs.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"
#include "ih264e_rate_control.h"
#include "ih264e_structs.h"
#include "ih264e_globals.h"
#include "ih264e_core_coding.h"
#include "ih264e_mc.h"


/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/**
*******************************************************************************
*
* @brief
*  This function performs does the DCT transform then Hadamard transform
*  and quantization for a macroblock when the mb mode is intra 16x16 mode
*
* @par Description:
*  First  cf4 is done on all 16 4x4 blocks of the 16x16 input block.
*  Then hadamard transform is done on the DC coefficients
*  Quantization is then performed on the 16x16 block, 4x4 wise
*
* @param[in] pu1_src
*  Pointer to source sub-block
*
* @param[in] pu1_pred
*  Pointer to prediction sub-block
*
* @param[in] pi2_out
*  Pointer to residual sub-block
*  The output will be in linear format
*  The first 16 continuous locations will contain the values of Dc block
*  After DC block and a stride 1st AC block will follow
*  After one more stride next AC block will follow
*  The blocks will be in raster scan order
*
* @param[in] src_strd
*  Source stride
*
* @param[in] pred_strd
*  Prediction stride
*
* @param[in] dst_strd
*  Destination stride
*
* @param[in] pu2_scale_matrix
*  The quantization matrix for 4x4 transform
*
* @param[in] pu2_threshold_matrix
*  Threshold matrix
*
* @param[in] u4_qbits
*  15+QP/6
*
* @param[in] u4_round_factor
*  Round factor for quant
*
* @param[out] pu1_nnz
*  Memory to store the non-zeros after transform
*  The first byte will be the nnz of DC block
*  From the next byte the AC nnzs will be stored in raster scan order
*
* @param u4_dc_flag
*  Signals if Dc transform is to be done or not
*   1 -> Dc transform will be done
*   0 -> Dc transform will not be done
*
* @remarks
*
*******************************************************************************
*/
void ih264e_luma_16x16_resi_trans_dctrans_quant(codec_t *ps_codec,
                                                UWORD8 *pu1_src,
                                                UWORD8 *pu1_pred,
                                                WORD16 *pi2_out,
                                                WORD32 src_strd,
                                                WORD32 pred_strd,
                                                WORD32 dst_strd,
                                                const UWORD16 *pu2_scale_matrix,
                                                const UWORD16 *pu2_threshold_matrix,
                                                UWORD32 u4_qbits,
                                                UWORD32 u4_round_factor,
                                                UWORD8 *pu1_nnz,
                                                UWORD32 u4_dc_flag)

{
    WORD32 blk_cntr;
    WORD32 i4_offsetx, i4_offsety;
    UWORD8 *pu1_curr_src, *pu1_curr_pred;

    WORD16 *pi2_dc_str = pi2_out;

    /* Move to the ac addresses */
    pu1_nnz++;
    pi2_out += dst_strd;

    for (blk_cntr = 0; blk_cntr < NUM_LUMA4x4_BLOCKS_IN_MB; blk_cntr++)
    {
        IND2SUB_LUMA_MB(blk_cntr, i4_offsetx, i4_offsety);

        pu1_curr_src = pu1_src + i4_offsetx + i4_offsety * src_strd;
        pu1_curr_pred = pu1_pred + i4_offsetx + i4_offsety * pred_strd;

        ps_codec->pf_resi_trans_quant_4x4(pu1_curr_src, pu1_curr_pred,
                                          pi2_out + blk_cntr * dst_strd,
                                          src_strd, pred_strd, pu2_scale_matrix,
                                          pu2_threshold_matrix, u4_qbits,
                                          u4_round_factor, &pu1_nnz[blk_cntr],
                                          &pi2_dc_str[blk_cntr]);

    }

    if (!u4_dc_flag)
        return;

    /*
     * In case of i16x16, we need to remove the contribution of dc coeffs into
     * nnz of each block. We are doing that in the packing function
     */

    /* Adjust pointers to point to dc values */
    pi2_out -= dst_strd;
    pu1_nnz--;

    u4_qbits++;
    u4_round_factor <<= 1;

    ps_codec->pf_hadamard_quant_4x4(pi2_dc_str, pi2_out, pu2_scale_matrix,
                                    pu2_threshold_matrix, u4_qbits,
                                    u4_round_factor, &pu1_nnz[0]);
}

/**
*******************************************************************************
*
* @brief
*  This function performs the intra 16x16 inverse transform process for H264
*  it includes inverse Dc transform, inverse quant and then inverse transform
*
* @par Description:
*
* @param[in] pi2_src
*  Input data, 16x16 size
*  First 16 mem locations will have the Dc coffs in rater scan order in linear fashion
*  after a stride 1st AC clock will be present again in raster can order
*  Then each AC block of the 16x16 block will follow in raster scan order
*
* @param[in] pu1_pred
*  The predicted data, 16x16 size
*  Block by block form
*
* @param[in] pu1_out
*  Output 16x16
*  In block by block form
*
* @param[in] src_strd
*  Source stride
*
* @param[in] pred_strd
*  input stride for prediction buffer
*
* @param[in] out_strd
*  input stride for output buffer
*
* @param[in] pu2_iscale_mat
*  Inverse quantization matrix for 4x4 transform
*
* @param[in] pu2_weigh_mat
*  weight matrix of 4x4 transform
*
* @param[in] qp_div
*  QP/6
*
* @param[in] pi4_tmp
*  Input temporary buffer
*  needs to be at least 20 in size
*
* @param[in] pu4_cntrl
*  Controls the transform path
*  total Last 17 bits are used
*  the 16th th bit will correspond to DC block
*  and 32-17 will correspond to the ac blocks in raster scan order
*  bit equaling zero indicates that the entire 4x4 block is zero for DC
*  For AC blocks a bit equaling zero will mean that all 15 AC coffs of the block is nonzero
*
* @param[in] pi4_tmp
*  Input temporary buffer
*  needs to be at least COFF_CNT_SUB_BLK_4x4+COFF_CNT_SUB_BLK_4x4 size
*
* @returns
*  none
*
* @remarks
*  The all zero case must be taken care outside
*
*******************************************************************************
*/
void ih264e_luma_16x16_idctrans_iquant_itrans_recon(codec_t *ps_codec,
                                                    WORD16 *pi2_src,
                                                    UWORD8 *pu1_pred,
                                                    UWORD8 *pu1_out,
                                                    WORD32 src_strd,
                                                    WORD32 pred_strd,
                                                    WORD32 out_strd,
                                                    const UWORD16 *pu2_iscale_mat,
                                                    const UWORD16 *pu2_weigh_mat,
                                                    UWORD32 qp_div,
                                                    UWORD32 u4_cntrl,
                                                    UWORD32 u4_dc_trans_flag,
                                                    WORD32 *pi4_tmp)
{
    /* Start index for inverse quant in a 4x4 block */
    WORD32 iq_start_idx = (u4_dc_trans_flag == 0) ? 0 : 1;

    /* Cntrl bits for 4x4 transforms
     * u4_blk_cntrl       : controls if a 4x4 block should be processed in ac path
     * u4_dc_cntrl        : controls is a 4x4 block is to be processed in dc path
     *                    : dc block must contain only single dc coefficient
     * u4_empty_blk_cntrl : control fot 4x4 block with no coeffs, ie no dc and ac
     *                    : ie not (ac or dc)
     */
    UWORD32 u4_blk_cntrl, u4_dc_cntrl, u4_empty_blk_cntrl;

    /* tmp registers for block ids */
    UWORD32 u4_blk_id;

    /* Subscrripts */
    WORD32 i4_offset_x, i4_offset_y;

    UWORD8 *pu1_cur_prd_blk, *pu1_cur_out_blk;

    /* Src and stride for dc coeffs */
    UWORD32 u4_dc_inc;
    WORD16 *pi2_dc_src;

    /*
     * For intra blocks we need to do inverse dc transform
     * In case if intra blocks, its here that we populate the dc bits in cntrl
     * as they cannot be populated any earlier
     */
    if (u4_dc_trans_flag)
    {
        UWORD32 cntr, u4_dc_cntrl;
        /* Do inv hadamard and place the results at the start of each AC block */
        ps_codec->pf_ihadamard_scaling_4x4(pi2_src, pi2_src, pu2_iscale_mat,
                                           pu2_weigh_mat, qp_div, pi4_tmp);

        /* Update the cntrl flag */
        u4_dc_cntrl = 0;
        for (cntr = 0; cntr < DC_COEFF_CNT_LUMA_MB; cntr++)
        {
            u4_dc_cntrl |= ((pi2_src[cntr] != 0) << (15 - cntr));
        }
        /* Mark dc bits as 1 if corresponding ac bit is 0 */
        u4_dc_cntrl = (~(u4_cntrl >> 16) & u4_dc_cntrl);
        /* Combine both ac and dc bits */
        u4_cntrl = (u4_cntrl & CNTRL_FLAG_AC_MASK_LUMA)
                        | (u4_dc_cntrl & CNTRL_FLAG_DC_MASK_LUMA);
    }

    /* Source for dc coeffs
     * If the block is intra, we have to read dc values from first row of src
     * then stride for each block is 1, other wise its src stride
     */
    pi2_dc_src = (iq_start_idx == 0) ? (pi2_src + src_strd) : pi2_src;
    u4_dc_inc = (iq_start_idx == 0) ? src_strd : 1;

    /* The AC blocks starts from 2nd row */
    pi2_src += src_strd;

    /* Get the block bits */
    u4_blk_cntrl = (u4_cntrl & CNTRL_FLAG_AC_MASK_LUMA);
    u4_dc_cntrl = (u4_cntrl & CNTRL_FLAG_DC_MASK_LUMA) << 16;
    u4_empty_blk_cntrl = (~(u4_dc_cntrl | u4_blk_cntrl)) & 0xFFFF0000;

    /* Get first block to process */
    DEQUEUE_BLKID_FROM_CONTROL(u4_dc_cntrl, u4_blk_id);
    while (u4_blk_id < NUM_LUMA4x4_BLOCKS_IN_MB)
    {
        /* Compute address of src blocks */
        WORD32 i4_src_offset = u4_dc_inc * u4_blk_id;

        IND2SUB_LUMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        /* Compute address of out and pred blocks */
        pu1_cur_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        /* Do inv dc transform */
        ps_codec->pf_iquant_itrans_recon_4x4_dc(pi2_dc_src + i4_src_offset,
                                                pu1_cur_prd_blk,
                                                pu1_cur_out_blk, pred_strd,
                                                out_strd, pu2_iscale_mat,
                                                pu2_weigh_mat, qp_div, NULL,
                                                iq_start_idx,
                                                pi2_dc_src + i4_src_offset);
        /* Get next DC block to process */
        DEQUEUE_BLKID_FROM_CONTROL(u4_dc_cntrl, u4_blk_id);
    }

    /* now process ac/mixed blocks */
    DEQUEUE_BLKID_FROM_CONTROL(u4_blk_cntrl, u4_blk_id);
    while (u4_blk_id < NUM_LUMA4x4_BLOCKS_IN_MB)
    {

        WORD32 i4_src_offset = src_strd * u4_blk_id;

        IND2SUB_LUMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        pu1_cur_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        ps_codec->pf_iquant_itrans_recon_4x4(pi2_src + i4_src_offset,
                                             pu1_cur_prd_blk, pu1_cur_out_blk,
                                             pred_strd, out_strd,
                                             pu2_iscale_mat, pu2_weigh_mat,
                                             qp_div, (WORD16*) pi4_tmp,
                                             iq_start_idx,
                                             pi2_dc_src + u4_blk_id);

        DEQUEUE_BLKID_FROM_CONTROL(u4_blk_cntrl, u4_blk_id);
    }

    /* Now process empty blocks */
    DEQUEUE_BLKID_FROM_CONTROL(u4_empty_blk_cntrl, u4_blk_id);
    while (u4_blk_id < NUM_LUMA4x4_BLOCKS_IN_MB)
    {
        IND2SUB_LUMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        pu1_cur_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        ps_codec->pf_inter_pred_luma_copy(pu1_cur_prd_blk, pu1_cur_out_blk,
                                          pred_strd, out_strd, SIZE_4X4_BLK_HRZ,
                                          SIZE_4X4_BLK_VERT, 0, 0);

        DEQUEUE_BLKID_FROM_CONTROL(u4_empty_blk_cntrl, u4_blk_id);
    }
}

/**
*******************************************************************************
*
* @brief
*  This function performs does the DCT transform then Hadamard transform
*  and quantization for a chroma macroblock
*
* @par Description:
*  First  cf4 is done on all 16 4x4 blocks of the 8x8input block
*  Then hadamard transform is done on the DC coefficients
*  Quantization is then performed on the 8x8 block, 4x4 wise
*
* @param[in] pu1_src
*  Pointer to source sub-block
*  The input is in interleaved format for two chroma planes
*
* @param[in] pu1_pred
*  Pointer to prediction sub-block
*  Prediction is in inter leaved format
*
* @param[in] pi2_out
*  Pointer to residual sub-block
*  The output will be in linear format
*  The first 4 continuous locations will contain the values of DC block for U
*  and then next 4 will contain for V.
*  After DC block and a stride 1st AC block of U plane will follow
*  After one more stride next AC block of V plane will follow
*  The blocks will be in raster scan order
*
*  After all the AC blocks of U plane AC blocks of V plane will follow in exact
*  same way
*
* @param[in] src_strd
*  Source stride
*
* @param[in] pred_strd
*  Prediction stride
*
* @param[in] dst_strd
*  Destination stride
*
* @param[in] pu2_scale_matrix
*  The quantization matrix for 4x4 transform
*
* @param[in] pu2_threshold_matrix
*  Threshold matrix
*
* @param[in] u4_qbits
*  15+QP/6
*
* @param[in] u4_round_factor
*  Round factor for quant
*
* @param[out] pu1_nnz
*  Memory to store the non-zeros after transform
*  The first byte will be the nnz od DC block for U plane
*  From the next byte the AC nnzs will be storerd in raster scan order
*  The fifth byte will be nnz of Dc block of V plane
*  Then Ac blocks will follow
*
* @param u4_dc_flag
*  Signals if Dc transform is to be done or not
*   1 -> Dc transform will be done
*   0 -> Dc transform will not be done
*
* @remarks
*
*******************************************************************************
*/
void ih264e_chroma_8x8_resi_trans_dctrans_quant(codec_t *ps_codec,
                                                UWORD8 *pu1_src,
                                                UWORD8 *pu1_pred,
                                                WORD16 *pi2_out,
                                                WORD32 src_strd,
                                                WORD32 pred_strd,
                                                WORD32 out_strd,
                                                const UWORD16 *pu2_scale_matrix,
                                                const UWORD16 *pu2_threshold_matrix,
                                                UWORD32 u4_qbits,
                                                UWORD32 u4_round_factor,
                                                UWORD8 *pu1_nnz_c)
{
    WORD32 blk_cntr;
    WORD32 i4_offsetx, i4_offsety;
    UWORD8 *pu1_curr_src, *pu1_curr_pred;

    WORD16 pi2_dc_str[8];
    UWORD8 au1_dcnnz[2];

    /* Move to the ac addresses */
    pu1_nnz_c++;
    pi2_out += out_strd;

    for (blk_cntr = 0; blk_cntr < NUM_CHROMA4x4_BLOCKS_IN_MB; blk_cntr++)
    {
        IND2SUB_CHROMA_MB(blk_cntr, i4_offsetx, i4_offsety);

        pu1_curr_src = pu1_src + i4_offsetx + i4_offsety * src_strd;
        pu1_curr_pred = pu1_pred + i4_offsetx + i4_offsety * pred_strd;

        /* For chroma, v plane nnz is populated from position 5 */
        ps_codec->pf_resi_trans_quant_chroma_4x4(
                        pu1_curr_src, pu1_curr_pred,
                        pi2_out + blk_cntr * out_strd, src_strd, pred_strd,
                        pu2_scale_matrix, pu2_threshold_matrix, u4_qbits,
                        u4_round_factor, &pu1_nnz_c[blk_cntr + (blk_cntr > 3)],
                        &pi2_dc_str[blk_cntr]);
    }

    /* Adjust pointers to point to dc values */
    pi2_out -= out_strd;
    pu1_nnz_c--;

    u4_qbits++;
    u4_round_factor <<= 1;

    ps_codec->pf_hadamard_quant_2x2_uv(pi2_dc_str, pi2_out, pu2_scale_matrix,
                                       pu2_threshold_matrix, u4_qbits,
                                       u4_round_factor, au1_dcnnz);

    /* Copy the dc nnzs */
    pu1_nnz_c[0] = au1_dcnnz[0];
    pu1_nnz_c[5] = au1_dcnnz[1];

}

/**
*******************************************************************************
* @brief
*  This function performs the inverse transform with process for chroma MB of H264
*
* @par Description:
*  Does inverse DC transform ,inverse quantization inverse transform
*
* @param[in] pi2_src
*  Input data, 16x16 size
*  The input is in the form of, first 4 locations will contain DC coeffs of
*  U plane, next 4 will contain DC coeffs of V plane, then AC blocks of U plane
*  in raster scan order will follow, each block as linear array in raster scan order.
*  After a stride next AC block will follow. After all AC blocks of U plane
*  V plane AC blocks will follow in exact same order.
*
* @param[in] pu1_pred
*  The predicted data, 8x16 size, U and V interleaved
*
* @param[in] pu1_out
*  Output 8x16, U and V interleaved
*
* @param[in] src_strd
*  Source stride
*
* @param[in] pred_strd
*  input stride for prediction buffer
*
* @param[in] out_strd
*  input stride for output buffer
*
* @param[in] pu2_iscale_mat
*  Inverse quantization martix for 4x4 transform
*
* @param[in] pu2_weigh_mat
*  weight matrix of 4x4 transform
*
* @param[in] qp_div
*  QP/6
*
* @param[in] pi4_tmp
*  Input temporary buffer
*  needs to be at least COFF_CNT_SUB_BLK_4x4 + Number of Dc cofss for chroma * number of planes
*  in size
*
* @param[in] pu4_cntrl
*  Controls the transform path
*  the 15 th bit will correspond to DC block of U plane , 14th will indicate the V plane Dc block
*  32-28 bits will indicate AC blocks of U plane in raster scan order
*  27-23 bits will indicate AC blocks of V plane in rater scan order
*  The bit 1 implies that there is at least one non zero coeff in a block
*
* @returns
*  none
*
* @remarks
*******************************************************************************
*/
void ih264e_chroma_8x8_idctrans_iquant_itrans_recon(codec_t *ps_codec,
                                                    WORD16 *pi2_src,
                                                    UWORD8 *pu1_pred,
                                                    UWORD8 *pu1_out,
                                                    WORD32 src_strd,
                                                    WORD32 pred_strd,
                                                    WORD32 out_strd,
                                                    const UWORD16 *pu2_iscale_mat,
                                                    const UWORD16 *pu2_weigh_mat,
                                                    UWORD32 qp_div,
                                                    UWORD32 u4_cntrl,
                                                    WORD32 *pi4_tmp)
{
    /* Cntrl bits for 4x4 transforms
     * u4_blk_cntrl       : controls if a 4x4 block should be processed in ac path
     * u4_dc_cntrl        : controls is a 4x4 block is to be processed in dc path
     *                    : dc block must contain only single dc coefficient
     * u4_empty_blk_cntrl : control fot 4x4 block with no coeffs, ie no dc and ac
     *                    : ie not (ac or dc)
     */

    UWORD32 u4_blk_cntrl, u4_dc_cntrl, u4_empty_blk_cntrl;

    /* tmp registers for block ids */
    WORD32 u4_blk_id;

    /* Offsets for pointers */
    WORD32 i4_offset_x, i4_offset_y;

    /* Pointer to 4x4 blocks */
    UWORD8 *pu1_cur_4x4_prd_blk, *pu1_cur_4x4_out_blk;

    /* Tmp register for pointer to dc coffs */
    WORD16 *pi2_dc_src;

    WORD16 i2_zero = 0;

    /* Increment for dc block */
    WORD32 i4_dc_inc;

    /*
     * Lets do the inverse transform for dc coeffs in chroma
     */
    if (u4_cntrl & CNTRL_FLAG_DCBLK_MASK_CHROMA)
    {
        UWORD32 cntr, u4_dc_cntrl;
        /* Do inv hadamard for u an v block */

        ps_codec->pf_ihadamard_scaling_2x2_uv(pi2_src, pi2_src, pu2_iscale_mat,
                                              pu2_weigh_mat, qp_div, NULL);
        /*
         * Update the cntrl flag
         * Flag is updated as follows bits 15-11 -> u block dc bits
         */
        u4_dc_cntrl = 0;
        for (cntr = 0; cntr < 8; cntr++)
        {
            u4_dc_cntrl |= ((pi2_src[cntr] != 0) << (15 - cntr));
        }

        /* Mark dc bits as 1 if corresponding ac bit is 0 */
        u4_dc_cntrl = (~(u4_cntrl >> 16) & u4_dc_cntrl);
        /* Combine both ac and dc bits */
        u4_cntrl = (u4_cntrl & CNTRL_FLAG_AC_MASK_CHROMA)
                        | (u4_dc_cntrl & CNTRL_FLAG_DC_MASK_CHROMA);

        /* Since we populated the dc coffs, we have to read them from there */
        pi2_dc_src = pi2_src;
        i4_dc_inc = 1;
    }
    else
    {
        u4_cntrl = u4_cntrl & CNTRL_FLAG_AC_MASK_CHROMA;
        pi2_dc_src = &i2_zero;
        i4_dc_inc = 0;
    }

    /* Get the block bits */
    u4_blk_cntrl = (u4_cntrl & CNTRL_FLAG_AC_MASK_CHROMA);
    u4_dc_cntrl = (u4_cntrl & CNTRL_FLAG_DC_MASK_CHROMA) << 16;
    u4_empty_blk_cntrl = (~(u4_dc_cntrl | u4_blk_cntrl)) & 0xFF000000;

    /* The AC blocks starts from 2nd row */
    pi2_src += src_strd;

    DEQUEUE_BLKID_FROM_CONTROL(u4_dc_cntrl, u4_blk_id);
    while (u4_blk_id < 8)
    {
        WORD32 dc_src_offset = u4_blk_id * i4_dc_inc;

        IND2SUB_CHROMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        pu1_cur_4x4_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_4x4_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        ps_codec->pf_iquant_itrans_recon_chroma_4x4_dc(
                        pi2_dc_src + dc_src_offset, pu1_cur_4x4_prd_blk,
                        pu1_cur_4x4_out_blk, pred_strd, out_strd, NULL, NULL, 0,
                        NULL, pi2_dc_src + dc_src_offset);
        /* Get next DC block to process */
        DEQUEUE_BLKID_FROM_CONTROL(u4_dc_cntrl, u4_blk_id);
    }

    /* now process ac/mixed blocks */
    DEQUEUE_BLKID_FROM_CONTROL(u4_blk_cntrl, u4_blk_id);
    while (u4_blk_id < 8)
    {
        WORD32 i4_src_offset = src_strd * u4_blk_id;
        WORD32 dc_src_offset = i4_dc_inc * u4_blk_id;

        IND2SUB_CHROMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        pu1_cur_4x4_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_4x4_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        ps_codec->pf_iquant_itrans_recon_chroma_4x4(pi2_src + i4_src_offset,
                                                    pu1_cur_4x4_prd_blk,
                                                    pu1_cur_4x4_out_blk,
                                                    pred_strd, out_strd,
                                                    pu2_iscale_mat,
                                                    pu2_weigh_mat, qp_div,
                                                    (WORD16 *) pi4_tmp,
                                                    pi2_dc_src + dc_src_offset);

        DEQUEUE_BLKID_FROM_CONTROL(u4_blk_cntrl, u4_blk_id);
    }

    /* Now process empty blocks */
    DEQUEUE_BLKID_FROM_CONTROL(u4_empty_blk_cntrl, u4_blk_id);
    while (u4_blk_id < 8)
    {
        IND2SUB_CHROMA_MB(u4_blk_id, i4_offset_x, i4_offset_y);

        pu1_cur_4x4_prd_blk = pu1_pred + i4_offset_x + i4_offset_y * pred_strd;
        pu1_cur_4x4_out_blk = pu1_out + i4_offset_x + i4_offset_y * out_strd;

        ps_codec->pf_interleave_copy(pu1_cur_4x4_prd_blk, pu1_cur_4x4_out_blk,
                                     pred_strd, out_strd, SIZE_4X4_BLK_VERT,
                                     SIZE_4X4_BLK_HRZ);

        DEQUEUE_BLKID_FROM_CONTROL(u4_empty_blk_cntrl, u4_blk_id);
    }
}

/**
******************************************************************************
*
* @brief  This function packs residue of an i16x16 luma mb for entropy coding
*
* @par   Description
*  An i16 macro block contains two classes of units, dc 4x4 block and
*  4x4 ac blocks. while packing the mb, the dc block is sent first, and
*  the 16 ac blocks are sent next in scan order. Each and every block is
*  represented by 3 parameters (nnz, significant coefficient map and the
*  residue coefficients itself). If a 4x4 unit does not have any coefficients
*  then only nnz is sent. Inside a 4x4 block the individual coefficients are
*  sent in scan order.
*
*  The first byte of each block will be nnz of the block, if it is non zero,
*  a 2 byte significance map is sent. This is followed by nonzero coefficients.
*  This is repeated for 1 dc + 16 ac blocks.
*
* @param[in]  pi2_res_mb
*  pointer to residue mb
*
* @param[in, out]  pv_mb_coeff_data
*  buffer pointing to packed residue coefficients
*
* @param[in]  u4_res_strd
*  residual block stride
*
* @param[out]  u1_cbp_l
*  coded block pattern luma
*
* @param[in]   pu1_nnz
*  number of non zero coefficients in each 4x4 unit
*
* @param[out]
*  Control signal for inverse transform of 16x16 blocks
*
* @return none
*
* @ remarks
*
******************************************************************************
*/
void ih264e_pack_l_mb_i16(WORD16 *pi2_res_mb,
                          void **pv_mb_coeff_data,
                          WORD32 i4_res_strd,
                          UWORD8 *u1_cbp_l,
                          UWORD8 *pu1_nnz,
                          UWORD32 *pu4_cntrl)
{
    /* pointer to packed sub block buffer space */
    tu_sblk_coeff_data_t *ps_mb_coeff_data = (*pv_mb_coeff_data), *ps_mb_coeff_data_ac;

    /* no of non zero coefficients in the current sub block */
    UWORD32 u4_nnz_cnt;

    /* significant coefficient map */
    UWORD32 u4_s_map;

    /* pointer to scanning matrix */
    const UWORD8 *pu1_scan_order;

    /* number of non zeros in sub block */
    UWORD32 u4_nnz;

    /* coeff scan order */
    const UWORD8 u1_scan_order[16] = {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};

    /* temp var */
    UWORD32 coeff_cnt, mask, b4,u4_cntrl=0;

    /*DC and AC coeff pointers*/
    WORD16 *pi2_res_mb_ac,*pi2_res_mb_dc;

    /********************************************************/
    /*  pack dc coeff data for entropy coding               */
    /********************************************************/

    pi2_res_mb_dc = pi2_res_mb;
    pu1_scan_order = gu1_luma_scan_order_dc;

    u4_nnz = *pu1_nnz;
    u4_cntrl = 0;

    /* write number of non zero coefficients */
    ps_mb_coeff_data->i4_sig_map_nnz = u4_nnz;

    if (u4_nnz)
    {
        for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < u4_nnz; coeff_cnt++)
        {
            if (pi2_res_mb_dc[pu1_scan_order[coeff_cnt]])
            {
                /* write residue */
                ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = pi2_res_mb_dc[pu1_scan_order[coeff_cnt]];
                u4_s_map |= mask;
            }
            mask <<= 1;
        }
        /* write significant coeff map */
        ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);
        (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);

        u4_cntrl = 0x00008000;// Set DC bit in ctrl code
    }
    else
    {
        (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
    }

    /********************************************************/
    /*  pack ac coeff data for entropy coding               */
    /********************************************************/

    pu1_nnz ++;
    pu1_scan_order = gu1_luma_scan_order;
    pi2_res_mb += i4_res_strd; /*Move to AC block*/

    ps_mb_coeff_data_ac = (*pv_mb_coeff_data);

    for (b4 = 0; b4 < 16; b4++)
    {
        ps_mb_coeff_data = (*pv_mb_coeff_data);

        u4_nnz = pu1_nnz[u1_scan_order[b4]];

        /* Jump according to the scan order */
        pi2_res_mb_ac = pi2_res_mb + (i4_res_strd * u1_scan_order[b4]);

        /*
         * Since this is a i16x16 block, we should not count dc coeff on indi
         * vidual 4x4 blocks to nnz. But due to the implementation of 16x16
         * trans function, we add dc's nnz to u4_nnz too. Hence we adjust that
         * here
         */
        u4_nnz -= (pi2_res_mb_ac[0] != 0);

        /* write number of non zero coefficients */
        ps_mb_coeff_data->i4_sig_map_nnz = u4_nnz;

        if (u4_nnz)
        {
            for (u4_nnz_cnt = 0, coeff_cnt = 1, mask = 1, u4_s_map = 0; u4_nnz_cnt < u4_nnz; coeff_cnt++)
            {
                if (pi2_res_mb_ac[pu1_scan_order[coeff_cnt]])
                {
                    /* write residue */
                    ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = pi2_res_mb_ac[pu1_scan_order[coeff_cnt]];
                    u4_s_map |= mask;
                }
                mask <<= 1;
            }
            /* write significant coeff map */
            ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);
            *u1_cbp_l = 15;

            u4_cntrl |= (1 << (31 - u1_scan_order[b4]));
        }
        else
        {
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
        }

    }

    if (!(*u1_cbp_l))
    {
        (*pv_mb_coeff_data) = ps_mb_coeff_data_ac;
    }

    /* Store the cntrl signal */
    (*pu4_cntrl) = u4_cntrl;
    return;
}

/**
******************************************************************************
*
* @brief  This function packs residue of an p16x16 luma mb for entropy coding
*
* @par   Description
*  A p16x16 macro block contains two classes of units 16  4x4 ac blocks.
*  while packing the mb, the dc block is sent first, and
*  the 16 ac blocks are sent next in scan order. Each and every block is
*  represented by 3 parameters (nnz, significant coefficient map and the
*  residue coefficients itself). If a 4x4 unit does not have any coefficients
*  then only nnz is sent. Inside a 4x4 block the individual coefficients are
*  sent in scan order.
*
*  The first byte of each block will be nnz of the block, if it is non zero,
*  a 2 byte significance map is sent. This is followed by nonzero coefficients.
*  This is repeated for 1 dc + 16 ac blocks.
*
* @param[in]  pi2_res_mb
*  pointer to residue mb
*
* @param[in, out]  pv_mb_coeff_data
*  buffer pointing to packed residue coefficients
*
* @param[in]  i4_res_strd
*  residual block stride
*
* @param[out]  u1_cbp_l
*  coded block pattern luma
*
* @param[in]   pu1_nnz
*  number of non zero coefficients in each 4x4 unit
*
* @param[out] pu4_cntrl
*  Control signal for inverse transform
*
* @return none
*
* @remarks Killing coffs not yet coded
*
******************************************************************************
*/
void ih264e_pack_l_mb(WORD16 *pi2_res_mb,
                      void **pv_mb_coeff_data,
                      WORD32 i4_res_strd,
                      UWORD8 *u1_cbp_l,
                      UWORD8 *pu1_nnz,
                      UWORD32 u4_thres_resi,
                      UWORD32 *pu4_cntrl)
{
    /* pointer to packed sub block buffer space */
    tu_sblk_coeff_data_t *ps_mb_coeff_data, *ps_mb_coeff_data_b8, *ps_mb_coeff_data_mb;

    /* no of non zero coefficients in the current sub block */
    UWORD32 u4_nnz_cnt;

    /* significant coefficient map */
    UWORD32 u4_s_map;

    /* pointer to scanning matrix */
    const UWORD8 *pu1_scan_order = gu1_luma_scan_order;

    /* number of non zeros in sub block */
    UWORD32 u4_nnz;

    /* pointer to residual sub block */
    WORD16  *pi2_res_sb;

    /* coeff scan order */
    const UWORD8 u1_scan_order[16] = {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};

    /* coeff cost */
    const UWORD8  *pu1_coeff_cost = gu1_coeff_cost;

    /* temp var */
    UWORD32 u4_mb_coeff_cost = 0, u4_b8_coeff_cost = 0, coeff_cnt, mask, u4_cntrl = 0, b4, b8;

    /* temp var */
    WORD32 i4_res_val, i4_run = -1, dcac_block;

    /* When Hadamard transform is disabled, first row values are dont care, ignore them */
    pi2_res_mb += i4_res_strd;

    /* When Hadamard transform is disabled, first unit value is dont care, ignore this */
    pu1_nnz ++;

    ps_mb_coeff_data_mb = ps_mb_coeff_data_b8 = (*pv_mb_coeff_data);

    /********************************************************/
    /*  pack coeff data for entropy coding                  */
    /********************************************************/

    for (b4 = 0; b4 < 16; b4++)
    {
        ps_mb_coeff_data = (*pv_mb_coeff_data);

        b8 = b4 >> 2;

        u4_nnz = pu1_nnz[u1_scan_order[b4]];

        /* Jump according to the scan order */
        pi2_res_sb = pi2_res_mb + (i4_res_strd * u1_scan_order[b4]);

        /* write number of non zero coefficients */
        ps_mb_coeff_data->i4_sig_map_nnz = u4_nnz;

        if (u4_nnz)
        {
            for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < u4_nnz; coeff_cnt++)
            {
                /* number of runs of zero before, this is used to compute coeff cost */
                i4_run++;

                i4_res_val = pi2_res_sb[pu1_scan_order[coeff_cnt]];

                if (i4_res_val)
                {
                    /* write residue */
                    ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = i4_res_val;
                    u4_s_map |= mask;

                    if (u4_thres_resi)
                    {
                        /* compute coeff cost */
                        if (i4_res_val == 1 || i4_res_val == -1)
                        {
                            if (i4_run < 6)
                                u4_b8_coeff_cost += pu1_coeff_cost[i4_run];
                        }
                        else
                            u4_b8_coeff_cost += 9;

                        i4_run = -1;
                    }
                }

                mask <<= 1;
            }

            /* write significant coeff map */
            ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);

            /* cbp */
            *u1_cbp_l |= (1 << b8);

            /* Cntrl map for inverse transform computation
             *
             * If coeff_cnt is zero, it means that only nonzero was a dc coeff
             * Hence we have to set the 16 - u1_scan_order[b4]) position instead
             * of 31 - u1_scan_order[b4]
             */
            dcac_block = (coeff_cnt == 0)?16:31;
            u4_cntrl |= (1 << (dcac_block - u1_scan_order[b4]));
        }
        else
        {
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
        }

        /* Decide if the 8x8 unit has to be sent for entropy coding? */
        if ((b4+1) % 4 == 0)
        {
            if ( u4_thres_resi && (u4_b8_coeff_cost <= LUMA_SUB_BLOCK_SKIP_THRESHOLD) &&
                            (*u1_cbp_l & (1 << b8)) )
            {


                /*
                 * When we want to reset the full 8x8 block, we have to reset
                 * both the dc and ac coeff bits hence we have the symmetric
                 * arrangement of bits
                 */
                const UWORD32 cntrl_mask_map[4] = {0xcc00cc00, 0x33003300, 0x00cc00cc, 0x00330033};

                /* restore cbp */
                *u1_cbp_l = (*u1_cbp_l & (~(1 << b8)));

                /* correct cntrl flag */
                u4_cntrl = u4_cntrl & (~cntrl_mask_map[(b4 >> 2)]);

                /* correct nnz */
                pu1_nnz[u1_scan_order[b4 - 3]] = 0;
                pu1_nnz[u1_scan_order[b4 - 2]] = 0;
                pu1_nnz[u1_scan_order[b4 - 1]] = 0;
                pu1_nnz[u1_scan_order[b4]] = 0;

                /* reset blk cost */
                u4_b8_coeff_cost = 0;
            }

            if (!(*u1_cbp_l & (1 << b8)))
            {
                (*pv_mb_coeff_data) = ps_mb_coeff_data_b8;
            }

            u4_mb_coeff_cost += u4_b8_coeff_cost;

            u4_b8_coeff_cost = 0;
            i4_run = -1;
            ps_mb_coeff_data_b8 = (*pv_mb_coeff_data);
        }
    }

    if (u4_thres_resi && (u4_mb_coeff_cost <= LUMA_BLOCK_SKIP_THRESHOLD)
                    && (*u1_cbp_l))
    {
        (*pv_mb_coeff_data) = ps_mb_coeff_data_mb;
        *u1_cbp_l = 0;
        u4_cntrl = 0;
        memset(pu1_nnz, 0, 16);
    }

    (*pu4_cntrl) = u4_cntrl;

    return;
}

/**
******************************************************************************
*
* @brief  This function packs residue of an i8x8 chroma mb for entropy coding
*
* @par   Description
*  An i8 chroma macro block contains two classes of units, dc 2x2 block and
*  4x4 ac blocks. while packing the mb, the dc block is sent first, and
*  the 4 ac blocks are sent next in scan order. Each and every block is
*  represented by 3 parameters (nnz, significant coefficient map and the
*  residue coefficients itself). If a 4x4 unit does not have any coefficients
*  then only nnz is sent. Inside a 4x4 block the individual coefficients are
*  sent in scan order.
*
*  The first byte of each block will be nnz of the block, if it is non zero,
*  a 2 byte significance map is sent. This is followed by nonzero coefficients.
*  This is repeated for 1 dc + 4 ac blocks.
*
* @param[in]  pi2_res_mb
*  pointer to residue mb
*
* @param[in, out]  pv_mb_coeff_data
*  buffer pointing to packed residue coefficients
*
* @param[in]  u4_res_strd
*  residual block stride
*
* @param[out]  u1_cbp_c
*  coded block pattern chroma
*
* @param[in]   pu1_nnz
*  number of non zero coefficients in each 4x4 unit
*
* @param[out]   pu1_nnz
*  Control signal for inverse transform
*
* @param[in]   u4_swap_uv
*  Swaps the order of U and V planes in entropy bitstream
*
* @return none
*
* @ remarks
*
******************************************************************************
*/
void ih264e_pack_c_mb(WORD16 *pi2_res_mb,
                      void **pv_mb_coeff_data,
                      WORD32 i4_res_strd,
                      UWORD8 *u1_cbp_c,
                      UWORD8 *pu1_nnz,
                      UWORD32 u4_thres_resi,
                      UWORD32 *pu4_cntrl,
                      UWORD32 u4_swap_uv)
{
    /* pointer to packed sub block buffer space */
    tu_sblk_coeff_data_t *ps_mb_coeff_data = (*pv_mb_coeff_data);
    tu_sblk_coeff_data_t *ps_mb_coeff_data_dc, *ps_mb_coeff_data_ac;

    /* nnz pointer */
    UWORD8 *pu1_nnz_ac, *pu1_nnz_dc;

    /* nnz counter */
    UWORD32 u4_nnz_cnt;

    /* significant coefficient map */
    UWORD32 u4_s_map;

    /* pointer to scanning matrix */
    const UWORD8 *pu1_scan_order;

    /* no of non zero coefficients in the current sub block */
    UWORD32 u4_nnz;

    /* pointer to residual sub block, res val */
    WORD16 *pi2_res_sb, i2_res_val;

    /* temp var */
    UWORD32 coeff_cnt, mask, b4,plane;

    /* temp var */
    UWORD32 u4_coeff_cost;
    WORD32 i4_run;

    /* coeff cost */
    const UWORD8 *pu1_coeff_cost = gu1_coeff_cost;

    /* pointer to packed buffer space */
    UWORD32 *pu4_mb_coeff_data = NULL;

    /* ac coded block pattern */
    UWORD8 u1_cbp_ac;

    /* Variable to store the current bit pos in cntrl variable*/
    UWORD32 cntrl_pos = 0;

    /********************************************************/
    /*  pack dc coeff data for entropy coding               */
    /********************************************************/
    pu1_scan_order = gu1_chroma_scan_order_dc;
    pi2_res_sb = pi2_res_mb;
    pu1_nnz_dc = pu1_nnz;
    (*pu4_cntrl) = 0;
    cntrl_pos = 15;
    ps_mb_coeff_data_dc = (*pv_mb_coeff_data);

    /* Color space conversion between SP_UV and SP_VU
     * We always assume SP_UV for all the processing
     * Hence to get proper stream output we need to swap U and V channels here
     *
     * For that there are two paths we need to look for
     * One is the path to bitstream , these variables should have the proper input
     * configured UV or VU
     * For the other path the inverse transform variables should have ehat ever 0ordering the
     * input had
     */

    if (u4_swap_uv)
    {
        pu1_nnz_dc += 5;/* Move to NNZ of V planve */
        pi2_res_sb += 4;/* Move to DC coff of V plane */

        cntrl_pos = 14; /* Control bit for V plane */
    }

    for (plane = 0; plane < 2; plane++)
    {
        ps_mb_coeff_data = (*pv_mb_coeff_data);

        u4_nnz = *pu1_nnz_dc;
        /* write number of non zero coefficients U/V */
        ps_mb_coeff_data->i4_sig_map_nnz = u4_nnz;

        if (u4_nnz)
        {
            for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < u4_nnz; coeff_cnt++)
            {
                i2_res_val = pi2_res_sb[pu1_scan_order[coeff_cnt]];
                if (i2_res_val)
                {
                    /* write residue U/V */
                    ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = i2_res_val;
                    u4_s_map |= mask;
                }
                mask <<= 1;
            }
            /* write significant coeff map U/V */
            ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);
            *u1_cbp_c = 1;

            (*pu4_cntrl) |= (1 << cntrl_pos);
        }
        else
        {
            (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
        }

        if (u4_swap_uv)
        {
            cntrl_pos++; /* Control bit for U plane */
            pu1_nnz_dc -= 5; /* Move to NNZ of U plane */
            pi2_res_sb -= 4; /* Move to DC coff of U plane */

        }
        else
        {
            cntrl_pos--; /* Control bit for U plane */
            pu1_nnz_dc += 5; /* 4 for AC NNZ and 1 for DC */
            pi2_res_sb += 4; /* Move to DC coff of V plane */
        }
    }

    /********************************************************/
    /*  pack ac coeff data for entropy coding               */
    /********************************************************/

    pu1_scan_order = gu1_chroma_scan_order;
    ps_mb_coeff_data_ac = (*pv_mb_coeff_data);

    if (u4_swap_uv)
    {
        pi2_res_sb = pi2_res_mb + i4_res_strd * 5; /* Move to V plane ,ie 1dc row+ 4 ac row */
        cntrl_pos = 27; /* The control bits are to be added for V bloc ie 31-4 th bit */
        pu1_nnz_ac = pu1_nnz + 6;/*Move the nnz to V block NNZ 1 dc + 1dc + 4 ac */
    }
    else
    {
        pi2_res_sb = pi2_res_mb + i4_res_strd; /* Move to U plane ,ie 1dc row */
        cntrl_pos = 31;
        pu1_nnz_ac = pu1_nnz + 1; /* Move the nnz to V block NNZ 1 dc */
    }

    for (plane = 0; plane < 2; plane++)
    {
        pu4_mb_coeff_data = (*pv_mb_coeff_data);

        u4_coeff_cost = 0;
        i4_run = -1;

        /* get the current cbp, so that it automatically
         * gets reverted in case of zero ac values */
        u1_cbp_ac = *u1_cbp_c;

        for (b4 = 0; b4 < 4; b4++)
        {
            ps_mb_coeff_data = (*pv_mb_coeff_data);

            u4_nnz = *pu1_nnz_ac;

            /*
             * We are scanning only ac coeffs, but the nnz is for the
             * complete 4x4 block. Hence we have to discount the nnz contributed
             * by the dc coefficient
             */
            u4_nnz -= (pi2_res_sb[0]!=0);

            /* write number of non zero coefficients U/V */
            ps_mb_coeff_data->i4_sig_map_nnz = u4_nnz;

            if (u4_nnz)
            {
                for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < u4_nnz; coeff_cnt++)
                {
                    i2_res_val = pi2_res_sb[pu1_scan_order[coeff_cnt]];

                    i4_run++;

                    if (i2_res_val)
                    {
                        /* write residue U/V */
                        ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = i2_res_val;
                        u4_s_map |= mask;

                        if ( u4_thres_resi && (u4_coeff_cost < CHROMA_BLOCK_SKIP_THRESHOLD) )
                        {
                            /* compute coeff cost */
                            if (i2_res_val == 1 || i2_res_val == -1)
                            {
                                if (i4_run < 6)
                                    u4_coeff_cost += pu1_coeff_cost[i4_run];
                            }
                            else
                                u4_coeff_cost += 9;

                            i4_run = -1;
                        }
                    }
                    mask <<= 1;
                }

                /* write significant coeff map U/V */
                ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);
                u1_cbp_ac = 2;

                (*pu4_cntrl) |= 1 << cntrl_pos;
            }
            else
            {
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
            }

            pu1_nnz_ac++;
            pi2_res_sb += i4_res_strd;
            cntrl_pos--;
        }

        /* reset block */
        if (u4_thres_resi && (u4_coeff_cost < CHROMA_BLOCK_SKIP_THRESHOLD))
        {
            pu4_mb_coeff_data[0] = 0;
            pu4_mb_coeff_data[1] = 0;
            pu4_mb_coeff_data[2] = 0;
            pu4_mb_coeff_data[3] = 0;
            (*pv_mb_coeff_data) = pu4_mb_coeff_data + 4;

            /* Generate the control signal */
            /* Zero out the current plane's AC coefficients */
            (*pu4_cntrl) &= ((plane == u4_swap_uv) ? 0x0FFFFFFF : 0xF0FFFFFF);

            /* Similarly do for the NNZ also */
            *(pu1_nnz_ac - 4) = 0;
            *(pu1_nnz_ac - 3) = 0;
            *(pu1_nnz_ac - 2) = 0;
            *(pu1_nnz_ac - 1) = 0;
        }
        else
        {
            *u1_cbp_c = u1_cbp_ac;
        }

        if (u4_swap_uv)
        {
            pi2_res_sb = pi2_res_mb + i4_res_strd; /* Move to V plane ,ie 1dc row+ 4 ac row + 1 dc row */
            cntrl_pos = 31; /* The control bits are to be added for V bloc ie 31-4 th bit */
            pu1_nnz_ac = pu1_nnz + 1; /* Move the nnz to V block NNZ 1 dc + 1dc + 4 ac */

            pu1_nnz_ac = pu1_nnz + 1;
        }
        else
            pu1_nnz_ac = pu1_nnz + 6; /* Go to nnz of V plane */
    }

    /* restore the ptr basing on cbp */
    if (*u1_cbp_c == 0)
    {
        (*pv_mb_coeff_data) = ps_mb_coeff_data_dc;
    }
    else if (*u1_cbp_c == 1)
    {
        (*pv_mb_coeff_data) = ps_mb_coeff_data_ac;
    }

    return ;
}

/**
*******************************************************************************
*
* @brief performs luma core coding when intra mode is i16x16
*
* @par Description:
*  If the current mb is to be coded as intra of mb type i16x16, the mb is first
*  predicted using one of i16x16 prediction filters, basing on the intra mode
*  chosen. Then, error is computed between the input blk and the estimated blk.
*  This error is transformed (hierarchical transform i.e., dct followed by hada-
*  -mard), quantized. The quantized coefficients are packed in scan order for
*  entropy coding.
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_l
*  coded block pattern luma
*
* @remarks none
*
*******************************************************************************
*/

UWORD8 ih264e_code_luma_intra_macroblock_16x16(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_ref_mb = ps_proc->pu1_rec_buf_luma;

    /* pointer to src macro block */
    UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_luma;

    /* pointer to prediction macro block */
    UWORD8 *pu1_pred_mb = NULL;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf;

    /* strides */
    WORD32 i4_src_strd = ps_proc->i4_src_strd;
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
    WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
    WORD32 i4_res_strd = ps_proc->i4_res_strd;

    /* intra mode */
    UWORD8 u1_intra_mode = ps_proc->u1_l_i16_mode;

    /* coded block pattern */
    UWORD8 u1_cbp_l = 0;

    /* number of non zero coeffs*/
    UWORD32 au4_nnz[5];
    UWORD8  *pu1_nnz = (UWORD8 *)au4_nnz;

    /*Cntrol signal for itrans*/
    UWORD32 u4_cntrl;

    /* quantization parameters */
    quant_params_t *ps_qp_params = ps_proc->ps_qp_params[0];

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /* init nnz */
    au4_nnz[0] = 0;
    au4_nnz[1] = 0;
    au4_nnz[2] = 0;
    au4_nnz[3] = 0;
    au4_nnz[4] = 0;

    if (u1_intra_mode == PLANE_I16x16)
    {
        pu1_pred_mb = ps_proc->pu1_pred_mb_intra_16x16_plane;
    }
    else
    {
        pu1_pred_mb = ps_proc->pu1_pred_mb_intra_16x16;
    }

    /********************************************************/
    /*  error estimation,                                   */
    /*  transform                                           */
    /*  quantization                                        */
    /********************************************************/
    ih264e_luma_16x16_resi_trans_dctrans_quant(ps_codec, pu1_curr_mb,
                                               pu1_pred_mb, pi2_res_mb,
                                               i4_src_strd, i4_pred_strd,
                                               i4_res_strd,
                                               ps_qp_params->pu2_scale_mat,
                                               ps_qp_params->pu2_thres_mat,
                                               ps_qp_params->u1_qbits,
                                               ps_qp_params->u4_dead_zone,
                                               pu1_nnz, ENABLE_DC_TRANSFORM);

    /********************************************************/
    /*  pack coeff data for entropy coding                  */
    /********************************************************/
    ih264e_pack_l_mb_i16(pi2_res_mb, pv_mb_coeff_data, i4_res_strd, &u1_cbp_l,
                         pu1_nnz, &u4_cntrl);

    /********************************************************/
    /*  ierror estimation,                                  */
    /*  itransform                                          */
    /*  iquantization                                       */
    /********************************************************/
    /*
     *if refernce frame is not to be computed
     *we only need the right and bottom border 4x4 blocks to predict next intra
     *blocks, hence only compute them
     */
    if (!ps_proc->u4_compute_recon)
    {
        u4_cntrl &= 0x111F8000;
    }

    if (u4_cntrl)
    {
        ih264e_luma_16x16_idctrans_iquant_itrans_recon(
                        ps_codec, pi2_res_mb, pu1_pred_mb, pu1_ref_mb,
                        i4_res_strd, i4_pred_strd, i4_rec_strd,
                        ps_qp_params->pu2_iscale_mat,
                        ps_qp_params->pu2_weigh_mat, ps_qp_params->u1_qp_div,
                        u4_cntrl, ENABLE_DC_TRANSFORM,
                        ps_proc->pv_scratch_buff);
    }
    else
    {
        ps_codec->pf_inter_pred_luma_copy(pu1_pred_mb, pu1_ref_mb, i4_pred_strd,
                                          i4_rec_strd, MB_SIZE, MB_SIZE, NULL,
                                          0);
    }

    return (u1_cbp_l);
}


/**
*******************************************************************************
*
* @brief performs luma core coding when intra mode is i4x4
*
* @par Description:
*  If the current mb is to be coded as intra of mb type i4x4, the mb is first
*  predicted using one of i4x4 prediction filters, basing on the intra mode
*  chosen. Then, error is computed between the input blk and the estimated blk.
*  This error is dct transformed and quantized. The quantized coefficients are
*  packed in scan order for entropy coding.
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_l
*  coded block pattern luma
*
* @remarks
*  The traversal of 4x4 subblocks in the 16x16 macroblock is as per the scan order
*  mentioned in h.264 specification
*
*******************************************************************************
*/
UWORD8 ih264e_code_luma_intra_macroblock_4x4(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_ref_mb = ps_proc->pu1_rec_buf_luma;

    /* pointer to src macro block */
    UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_luma;

    /* pointer to prediction macro block */
    UWORD8 *pu1_pred_mb = ps_proc->pu1_pred_mb;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf;

    /* strides */
    WORD32 i4_src_strd = ps_proc->i4_src_strd;
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
    WORD32 i4_pred_strd = ps_proc->i4_pred_strd;

    /* pointer to neighbors: left, top, top-left */
    UWORD8 *pu1_mb_a;
    UWORD8 *pu1_mb_b;
    UWORD8 *pu1_mb_c;
    UWORD8 *pu1_mb_d;

    /* intra mode */
    UWORD8 u1_intra_mode = ps_proc->u1_l_i16_mode;

    /* neighbor availability */
    WORD32 i4_ngbr_avbl;

    /* neighbor pels for intra prediction */
    UWORD8 *pu1_ngbr_pels_i4 = ps_proc->au1_ngbr_pels;

    /* coded block pattern */
    UWORD8 u1_cbp_l = 0;

    /* number of non zero coeffs*/
    UWORD8  u1_nnz;

    /* quantization parameters */
    quant_params_t *ps_qp_params = ps_proc->ps_qp_params[0];

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /* pointer to packed mb coeff data */
    tu_sblk_coeff_data_t *ps_mb_coeff_data, *ps_mb_coeff_data_b8;

    /* no of non zero coefficients in the current sub block */
    UWORD32 u4_nnz_cnt;

    /* significant coefficient map */
    UWORD32 u4_s_map;

    /* pointer to scanning matrix */
    const UWORD8 *pu1_scan_order = gu1_luma_scan_order;

    /*Dummy variable for 4x4 trans fucntion*/
    WORD16 i2_dc_dummy;

    /* temp var */
    UWORD32 i, b8, b4, u1_blk_x, u1_blk_y, u1_pix_x, u1_pix_y, coeff_cnt, mask;

    /* Process 16 4x4 lum sub-blocks of the MB in scan order */
    for (b8 = 0; b8 < 4; b8++)
    {
        u1_blk_x = GET_BLK_RASTER_POS_X(b8) << 3;
        u1_blk_y = GET_BLK_RASTER_POS_Y(b8) << 3;

        /* if in case cbp for the 8x8 block is zero, send no residue */
        ps_mb_coeff_data_b8 = *pv_mb_coeff_data;

        for (b4 = 0; b4 < 4; b4++)
        {
            /* index of pel in MB */
            u1_pix_x = u1_blk_x + (GET_SUB_BLK_RASTER_POS_X(b4) << 2);
            u1_pix_y = u1_blk_y + (GET_SUB_BLK_RASTER_POS_Y(b4) << 2);

            /* Initialize source and reference pointers */
            pu1_curr_mb = ps_proc->pu1_src_buf_luma + u1_pix_x + (u1_pix_y * i4_src_strd);
            pu1_ref_mb = ps_proc->pu1_rec_buf_luma + u1_pix_x + (u1_pix_y * i4_rec_strd);

            /* pointer to left of ref macro block */
            pu1_mb_a = pu1_ref_mb - 1;
            /* pointer to top of ref macro block */
            pu1_mb_b = pu1_ref_mb - i4_rec_strd;
            /* pointer to topright of ref macro block */
            pu1_mb_c = pu1_mb_b + 4;
            /* pointer to topleft macro block */
            pu1_mb_d = pu1_mb_b - 1;

            /* compute neighbor availability */
            i4_ngbr_avbl = ps_proc->au1_ngbr_avbl_4x4_subblks[(b8 << 2) + b4];

            /* sub block intra mode */
            u1_intra_mode = ps_proc->au1_intra_luma_mb_4x4_modes[(b8 << 2) + b4];

            /********************************************************/
            /* gather prediction pels from neighbors for prediction */
            /********************************************************/
            /* left pels */
            if (i4_ngbr_avbl & LEFT_MB_AVAILABLE_MASK)
            {
                for (i = 0; i < 4; i++)
                    pu1_ngbr_pels_i4[4 - 1 - i] = pu1_mb_a[i * i4_rec_strd];
            }
            else
            {
                memset(pu1_ngbr_pels_i4, 0, 4);
            }

            /* top pels */
            if (i4_ngbr_avbl & TOP_MB_AVAILABLE_MASK)
            {
                memcpy(pu1_ngbr_pels_i4 + 4 + 1, pu1_mb_b, 4);
            }
            else
            {
                memset(pu1_ngbr_pels_i4 + 5, 0, 4);
            }
            /* top left pels */
            if (i4_ngbr_avbl & TOP_LEFT_MB_AVAILABLE_MASK)
            {
                pu1_ngbr_pels_i4[4] = *pu1_mb_d;
            }
            else
            {
                pu1_ngbr_pels_i4[4] = 0;
            }
            /* top right pels */
            if (i4_ngbr_avbl & TOP_RIGHT_MB_AVAILABLE_MASK)
            {
                memcpy(pu1_ngbr_pels_i4+8+1,pu1_mb_c,4);
            }
            else if (i4_ngbr_avbl & TOP_MB_AVAILABLE_MASK)
            {
                memset(pu1_ngbr_pels_i4+8+1,pu1_ngbr_pels_i4[8],4);
            }

            /********************************************************/
            /*  prediction                                          */
            /********************************************************/
            (ps_codec->apf_intra_pred_4_l)[u1_intra_mode](pu1_ngbr_pels_i4,
                                                          pu1_pred_mb, 0,
                                                          i4_pred_strd,
                                                          i4_ngbr_avbl);

            /********************************************************/
            /*  error estimation,                                   */
            /*  transform                                           */
            /*  quantization                                        */
            /********************************************************/
            ps_codec->pf_resi_trans_quant_4x4(pu1_curr_mb, pu1_pred_mb,
                                              pi2_res_mb, i4_src_strd,
                                              i4_pred_strd,
                                              ps_qp_params->pu2_scale_mat,
                                              ps_qp_params->pu2_thres_mat,
                                              ps_qp_params->u1_qbits,
                                              ps_qp_params->u4_dead_zone,
                                              &u1_nnz, &i2_dc_dummy);

            /********************************************************/
            /*  pack coeff data for entropy coding                  */
            /********************************************************/
            ps_mb_coeff_data = *pv_mb_coeff_data;

            /* write number of non zero coefficients */
            ps_mb_coeff_data->i4_sig_map_nnz = u1_nnz;

            if (u1_nnz)
            {
                for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < u1_nnz; coeff_cnt++)
                {
                    if (pi2_res_mb[pu1_scan_order[coeff_cnt]])
                    {
                        /* write residue */
                        ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = pi2_res_mb[pu1_scan_order[coeff_cnt]];
                        u4_s_map |= mask;
                    }
                    mask <<= 1;
                }
                /* write significant coeff map */
                ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);

                /* update ptr to coeff data */
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);

                /* cbp */
                u1_cbp_l |= (1 << b8);
            }
            else
            {
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
            }

            /********************************************************/
            /*  ierror estimation,                                  */
            /*  itransform                                          */
            /*  iquantization                                       */
            /********************************************************/
            /* If the frame is not to be used for P frame reference or dumping recon
             * we only will use the recon for only predicting intra Mbs
             * This will need only right and bottom edge 4x4 blocks recon
             * Hence we selectively enable them
             */
            if (ps_proc->u4_compute_recon || (0xF888 & (1 << ((b8 << 2) + b4))))
            {
                if (u1_nnz)
                    ps_codec->pf_iquant_itrans_recon_4x4(
                                    pi2_res_mb, pu1_pred_mb, pu1_ref_mb,
                                    /*No input stride,*/i4_pred_strd,
                                    i4_rec_strd, ps_qp_params->pu2_iscale_mat,
                                    ps_qp_params->pu2_weigh_mat,
                                    ps_qp_params->u1_qp_div,
                                    ps_proc->pv_scratch_buff, 0, 0);
                else
                    ps_codec->pf_inter_pred_luma_copy(pu1_pred_mb, pu1_ref_mb,
                                                      i4_pred_strd, i4_rec_strd,
                                                      BLK_SIZE, BLK_SIZE, NULL,
                                                      0);
            }

        }

        /* if the 8x8 block has no residue, nothing needs to be sent to entropy */
        if (!(u1_cbp_l & (1 << b8)))
        {
            *pv_mb_coeff_data = ps_mb_coeff_data_b8;
        }
    }

    return (u1_cbp_l);
}

/**
*******************************************************************************
*
* @brief performs luma core coding when intra mode is i4x4
*
* @par Description:
*  If the current mb is to be coded as intra of mb type i4x4, the mb is first
*  predicted using one of i4x4 prediction filters, basing on the intra mode
*  chosen. Then, error is computed between the input blk and the estimated blk.
*  This error is dct transformed and quantized. The quantized coefficients are
*  packed in scan order for entropy coding.
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_l
*  coded block pattern luma
*
* @remarks
*  The traversal of 4x4 subblocks in the 16x16 macroblock is as per the scan order
*  mentioned in h.264 specification
*
*******************************************************************************
*/
UWORD8 ih264e_code_luma_intra_macroblock_4x4_rdopt_on(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_ref_mb_intra_4x4 = ps_proc->pu1_ref_mb_intra_4x4;

    /* pointer to recon buffer */
    UWORD8 *pu1_rec_mb = ps_proc->pu1_rec_buf_luma;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf_intra_4x4;

    /* strides */
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;

    /* number of non zero coeffs*/
    UWORD8  *pu1_nnz = (UWORD8 *)ps_proc->au4_nnz_intra_4x4;

    /* coded block pattern */
    UWORD8 u1_cbp_l = 0;

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /* pointer to packed mb coeff data */
    tu_sblk_coeff_data_t *ps_mb_coeff_data, *ps_mb_coeff_data_b8;

    /* no of non zero coefficients in the current sub block */
    UWORD32 u4_nnz_cnt;

    /* significant coefficient map */
    UWORD32 u4_s_map;

    /* pointer to scanning matrix */
    const UWORD8 *pu1_scan_order = gu1_luma_scan_order;

    /* temp var */
    UWORD32 b8, b4, coeff_cnt, mask;

    /* Process 16 4x4 lum sub-blocks of the MB in scan order */
    for (b8 = 0; b8 < 4; b8++)
    {
        /* if in case cbp for the 8x8 block is zero, send no residue */
        ps_mb_coeff_data_b8 = *pv_mb_coeff_data;

        for (b4 = 0; b4 < 4; b4++, pu1_nnz++, pi2_res_mb += MB_SIZE)
        {
            /********************************************************/
            /*  pack coeff data for entropy coding                  */
            /********************************************************/
            ps_mb_coeff_data = *pv_mb_coeff_data;

            /* write number of non zero coefficients */
            ps_mb_coeff_data->i4_sig_map_nnz = *pu1_nnz;

            if (*pu1_nnz)
            {
                for (u4_nnz_cnt = 0, coeff_cnt = 0, mask = 1, u4_s_map = 0; u4_nnz_cnt < *pu1_nnz; coeff_cnt++)
                {
                    if (pi2_res_mb[pu1_scan_order[coeff_cnt]])
                    {
                        /* write residue */
                        ps_mb_coeff_data->ai2_residue[u4_nnz_cnt++] = pi2_res_mb[pu1_scan_order[coeff_cnt]];
                        u4_s_map |= mask;
                    }
                    mask <<= 1;
                }
                /* write significant coeff map */
                ps_mb_coeff_data->i4_sig_map_nnz |= (u4_s_map << 16);

                /* update ptr to coeff data */
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue + ALIGN2(u4_nnz_cnt);

                /* cbp */
                u1_cbp_l |= (1 << b8);
            }
            else
            {
                (*pv_mb_coeff_data) = ps_mb_coeff_data->ai2_residue;
            }
        }

        /* if the 8x8 block has no residue, nothing needs to be sent to entropy */
        if (!(u1_cbp_l & (1 << b8)))
        {
            *pv_mb_coeff_data = ps_mb_coeff_data_b8;
        }
    }

    /* memcpy recon */
    ps_codec->pf_inter_pred_luma_copy(pu1_ref_mb_intra_4x4, pu1_rec_mb, MB_SIZE, i4_rec_strd, MB_SIZE, MB_SIZE, NULL, 0);

    return (u1_cbp_l);
}


/**
*******************************************************************************
*
* @brief performs chroma core coding for intra macro blocks
*
* @par Description:
*  If the current MB is to be intra coded with mb type chroma I8x8, the MB is
*  first predicted using intra 8x8 prediction filters. The predicted data is
*  compared with the input for error and the error is transformed. The DC
*  coefficients of each transformed sub blocks are further transformed using
*  Hadamard transform. The resulting coefficients are quantized, packed and sent
*  for entropy coding.
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_c
*  coded block pattern chroma
*
* @remarks
*  The traversal of 4x4 subblocks in the 8x8 macroblock is as per the scan order
*  mentioned in h.264 specification
*
*******************************************************************************
*/
UWORD8 ih264e_code_chroma_intra_macroblock_8x8(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_ref_mb = ps_proc->pu1_rec_buf_chroma;

    /* pointer to src macro block */
    UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_chroma;

    /* pointer to prediction macro block */
    UWORD8 *pu1_pred_mb = NULL;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf;

    /* strides */
    WORD32 i4_src_strd = ps_proc->i4_src_strd;
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
    WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
    WORD32 i4_res_strd = ps_proc->i4_res_strd;

    /* intra mode */
    UWORD8 u1_intra_mode = ps_proc->u1_c_i8_mode;

    /* coded block pattern */
    UWORD8 u1_cbp_c = 0;

    /* number of non zero coeffs*/
    UWORD8 au1_nnz[18] = {0};

    /* quantization parameters */
    quant_params_t *ps_qp_params = ps_proc->ps_qp_params[1];

    /* Control signal for inverse transform */
    UWORD32 u4_cntrl;

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /* See if we need to swap U and V plances for entropy */
    UWORD32 u4_swap_uv = ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420SP_VU;

    if (PLANE_CH_I8x8 == u1_intra_mode)
    {
        pu1_pred_mb = ps_proc->pu1_pred_mb_intra_chroma_plane;
    }
    else
    {
        pu1_pred_mb = ps_proc->pu1_pred_mb_intra_chroma;
    }

    /********************************************************/
    /*  error estimation,                                   */
    /*  transform                                           */
    /*  quantization                                        */
    /********************************************************/
    ih264e_chroma_8x8_resi_trans_dctrans_quant(ps_codec, pu1_curr_mb,
                                               pu1_pred_mb, pi2_res_mb,
                                               i4_src_strd, i4_pred_strd,
                                               i4_res_strd,
                                               ps_qp_params->pu2_scale_mat,
                                               ps_qp_params->pu2_thres_mat,
                                               ps_qp_params->u1_qbits,
                                               ps_qp_params->u4_dead_zone,
                                               au1_nnz);

    /********************************************************/
    /*  pack coeff data for entropy coding                  */
    /********************************************************/
    ih264e_pack_c_mb(pi2_res_mb, pv_mb_coeff_data, i4_res_strd, &u1_cbp_c,
                     au1_nnz, ps_codec->u4_thres_resi, &u4_cntrl, u4_swap_uv);

    /********************************************************/
    /*  ierror estimation,                                  */
    /*  itransform                                          */
    /*  iquantization                                       */
    /********************************************************/
    ih264e_chroma_8x8_idctrans_iquant_itrans_recon(ps_codec, pi2_res_mb,
                                                   pu1_pred_mb, pu1_ref_mb,
                                                   i4_res_strd, i4_pred_strd,
                                                   i4_rec_strd,
                                                   ps_qp_params->pu2_iscale_mat,
                                                   ps_qp_params->pu2_weigh_mat,
                                                   ps_qp_params->u1_qp_div,
                                                   u4_cntrl,
                                                   ps_proc->pv_scratch_buff);
    return (u1_cbp_c);
}


/**
*******************************************************************************
*
* @brief performs luma core coding when  mode is inter
*
* @par Description:
*  If the current mb is to be coded as inter the mb is predicted based on the
*  sub mb partitions and corresponding motion vectors generated by ME. Then,
*  error is computed between the input blk and the estimated blk. This error is
*  transformed, quantized. The quantized coefficients are packed in scan order
*  for entropy coding
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_l
*  coded block pattern luma
*
* @remarks none
*
*******************************************************************************
*/

UWORD8 ih264e_code_luma_inter_macroblock_16x16(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_rec_mb = ps_proc->pu1_rec_buf_luma;

    /* pointer to src macro block */
    UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_luma;

    /* pointer to prediction macro block */
    UWORD8 *pu1_pred_mb = ps_proc->pu1_pred_mb;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf;

    /* strides */
    WORD32 i4_src_strd = ps_proc->i4_src_strd;
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
    WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
    WORD32 i4_res_strd = ps_proc->i4_res_strd;

    /* coded block pattern */
    UWORD8 u1_cbp_l = 0;

    /*Control signal of itrans*/
    UWORD32 u4_cntrl;

    /* number of non zero coeffs*/
    UWORD8  *pu1_nnz = (UWORD8 *)ps_proc->au4_nnz;

    /* quantization parameters */
    quant_params_t *ps_qp_params = ps_proc->ps_qp_params[0];

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /* pseudo pred buffer */
    UWORD8 *pu1_pseudo_pred = pu1_pred_mb;

    /* pseudo pred buffer stride */
    WORD32 i4_pseudo_pred_strd = i4_pred_strd;

    /* init nnz */
    ps_proc->au4_nnz[0] = 0;
    ps_proc->au4_nnz[1] = 0;
    ps_proc->au4_nnz[2] = 0;
    ps_proc->au4_nnz[3] = 0;
    ps_proc->au4_nnz[4] = 0;

    /********************************************************/
    /*  prediction                                          */
    /********************************************************/
    ih264e_motion_comp_luma(ps_proc, &pu1_pseudo_pred, &i4_pseudo_pred_strd);

    /********************************************************/
    /*  error estimation,                                   */
    /*  transform                                           */
    /*  quantization                                        */
    /********************************************************/
    if (ps_proc->u4_min_sad_reached == 0 || ps_proc->u4_min_sad != 0)
    {
        ih264e_luma_16x16_resi_trans_dctrans_quant(ps_codec, pu1_curr_mb,
                                                   pu1_pseudo_pred, pi2_res_mb,
                                                   i4_src_strd,
                                                   i4_pseudo_pred_strd,
                                                   i4_res_strd,
                                                   ps_qp_params->pu2_scale_mat,
                                                   ps_qp_params->pu2_thres_mat,
                                                   ps_qp_params->u1_qbits,
                                                   ps_qp_params->u4_dead_zone,
                                                   pu1_nnz,
                                                   DISABLE_DC_TRANSFORM);

        /********************************************************/
        /*  pack coeff data for entropy coding                  */
        /********************************************************/
        ih264e_pack_l_mb(pi2_res_mb, pv_mb_coeff_data, i4_res_strd, &u1_cbp_l,
                         pu1_nnz, ps_codec->u4_thres_resi, &u4_cntrl);
    }
    else
    {
        u1_cbp_l = 0;
        u4_cntrl = 0;
    }

    /********************************************************/
    /*  ierror estimation,                                  */
    /*  itransform                                          */
    /*  iquantization                                       */
    /********************************************************/

    /*If the frame is not to be used for P frame reference or dumping recon
     * we only will use the reocn for only predicting intra Mbs
     * THis will need only right and bottom edge 4x4 blocks recon
     * Hence we selectively enable them using control signal(including DC)
     */
    if (ps_proc->u4_compute_recon != 1)
    {
        u4_cntrl &= 0x111F0000;
    }

    if (u4_cntrl)
    {
        ih264e_luma_16x16_idctrans_iquant_itrans_recon(
                        ps_codec, pi2_res_mb, pu1_pseudo_pred, pu1_rec_mb,
                        i4_res_strd, i4_pseudo_pred_strd, i4_rec_strd,
                        ps_qp_params->pu2_iscale_mat,
                        ps_qp_params->pu2_weigh_mat, ps_qp_params->u1_qp_div,
                        u4_cntrl /*Cntrl*/, DISABLE_DC_TRANSFORM,
                        ps_proc->pv_scratch_buff);
    }
    else
    {
        ps_codec->pf_inter_pred_luma_copy(pu1_pseudo_pred, pu1_rec_mb,
                                          i4_pseudo_pred_strd, i4_rec_strd,
                                          MB_SIZE, MB_SIZE, NULL, 0);
    }


    return (u1_cbp_l);
}

/**
*******************************************************************************
*
* @brief performs chroma core coding for inter macro blocks
*
* @par Description:
*  If the current mb is to be coded as inter predicted mb,based on the sub mb partitions
*  and corresponding motion vectors generated by ME  ,prediction is done.
*  Then, error is computed between the input blk and the estimated blk.
*  This error is transformed , quantized. The quantized coefficients
*  are packed in scan order for
*  entropy coding.
*
* @param[in] ps_proc_ctxt
*  pointer to the current macro block context
*
* @returns u1_cbp_l
*  coded block pattern chroma
*
* @remarks none
*
*******************************************************************************
*/
UWORD8 ih264e_code_chroma_inter_macroblock_8x8(process_ctxt_t *ps_proc)
{
    /* Codec Context */
    codec_t *ps_codec = ps_proc->ps_codec;

    /* pointer to ref macro block */
    UWORD8 *pu1_rec_mb = ps_proc->pu1_rec_buf_chroma;

    /* pointer to src macro block */
    UWORD8 *pu1_curr_mb = ps_proc->pu1_src_buf_chroma;

    /* pointer to prediction macro block */
    UWORD8 *pu1_pred_mb = ps_proc->pu1_pred_mb;

    /* pointer to residual macro block */
    WORD16 *pi2_res_mb = ps_proc->pi2_res_buf;

    /* strides */
    WORD32 i4_src_strd = ps_proc->i4_src_strd;
    WORD32 i4_rec_strd = ps_proc->i4_rec_strd;
    WORD32 i4_pred_strd = ps_proc->i4_pred_strd;
    WORD32 i4_res_strd = ps_proc->i4_res_strd;

    /* coded block pattern */
    UWORD8 u1_cbp_c = 0;

    /*Control signal for inverse transform*/
    UWORD32 u4_cntrl;

    /* number of non zero coeffs*/
    UWORD8 au1_nnz[10] = {0};

    /* quantization parameters */
    quant_params_t *ps_qp_params = ps_proc->ps_qp_params[1];

    /* pointer to packed mb coeff data */
    void **pv_mb_coeff_data = &(ps_proc->pv_mb_coeff_data);

    /*See if we need to swap U and V plances for entropy*/
    UWORD32 u4_swap_uv = ps_codec->s_cfg.e_inp_color_fmt == IV_YUV_420SP_VU;

    /********************************************************/
    /*  prediction                                          */
    /********************************************************/
    ih264e_motion_comp_chroma(ps_proc);

    /********************************************************/
    /*  error estimation,                                   */
    /*  transform                                           */
    /*  quantization                                        */
    /********************************************************/
    ih264e_chroma_8x8_resi_trans_dctrans_quant(ps_codec, pu1_curr_mb,
                                               pu1_pred_mb, pi2_res_mb,
                                               i4_src_strd, i4_pred_strd,
                                               i4_res_strd,
                                               ps_qp_params->pu2_scale_mat,
                                               ps_qp_params->pu2_thres_mat,
                                               ps_qp_params->u1_qbits,
                                               ps_qp_params->u4_dead_zone,
                                               au1_nnz);

    /********************************************************/
    /*  pack coeff data for entropy coding                  */
    /********************************************************/
    ih264e_pack_c_mb(pi2_res_mb, pv_mb_coeff_data, i4_res_strd, &u1_cbp_c,
                     au1_nnz, ps_codec->u4_thres_resi, &u4_cntrl, u4_swap_uv);

    /********************************************************/
    /*  ierror estimation,                                  */
    /*  itransform                                          */
    /*  iquantization                                       */
    /********************************************************/

    /* If the frame is not to be used for P frame reference or dumping recon
     * we only will use the reocn for only predicting intra Mbs
     * THis will need only right and bottom edge 4x4 blocks recon
     * Hence we selectively enable them using control signal(including DC)
     */
    if (!ps_proc->u4_compute_recon)
    {
        u4_cntrl &= 0x7700C000;
    }

    if (u4_cntrl)
    {
        ih264e_chroma_8x8_idctrans_iquant_itrans_recon(
                        ps_codec, pi2_res_mb, pu1_pred_mb, pu1_rec_mb,
                        i4_res_strd, i4_pred_strd, i4_rec_strd,
                        ps_qp_params->pu2_iscale_mat,
                        ps_qp_params->pu2_weigh_mat, ps_qp_params->u1_qp_div,
                        u4_cntrl, ps_proc->pv_scratch_buff);
    }
    else
    {
        ps_codec->pf_inter_pred_luma_copy(pu1_pred_mb, pu1_rec_mb, i4_pred_strd,
                                          i4_rec_strd, MB_SIZE >> 1, MB_SIZE,
                                          NULL, 0);
    }

    return (u1_cbp_c);
}