1 /*!
2  ***********************************************************************
3  * \file read_comp_cabac.c
4  *
5  * \brief
6  *     Read Coefficient Components
7  *
8  * \author
9  *    Main contributors (see contributors.h for copyright, address and affiliation details)
10  *    - Alexis Michael Tourapis         <alexismt@ieee.org>
11  ***********************************************************************
12 */
13 
14 #include "contributors.h"
15 
16 #include "global.h"
17 #include "elements.h"
18 #include "macroblock.h"
19 #include "cabac.h"
20 #include "vlc.h"
21 #include "transform.h"
22 
23 #if TRACE
24 #define TRACE_STRING(s) strncpy(currSE.tracestring, s, TRACESTRING_SIZE)
25 #define TRACE_DECBITS(i) dectracebitcnt(1)
26 #define TRACE_PRINTF(s) sprintf(type, "%s", s);
27 #define TRACE_STRING_P(s) strncpy(currSE->tracestring, s, TRACESTRING_SIZE)
28 #else
29 #define TRACE_STRING(s)
30 #define TRACE_DECBITS(i)
31 #define TRACE_PRINTF(s)
32 #define TRACE_STRING_P(s)
33 #endif
34 
35 extern void  check_dp_neighbors (Macroblock *currMB);
36 extern void  read_delta_quant   (SyntaxElement *currSE, DataPartition *dP, Macroblock *currMB, const byte *partMap, int type);
37 
38 /*!
39 ************************************************************************
40 * \brief
41 *    Get coefficients (run/level) of 4x4 blocks in a SMB
42 *    from the NAL (CABAC Mode)
43 ************************************************************************
44 */
read_comp_coeff_4x4_smb_CABAC(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl,int block_y,int block_x,int start_scan,int64 * cbp_blk)45 static void read_comp_coeff_4x4_smb_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int block_y, int block_x, int start_scan, int64 *cbp_blk)
46 {
47   int i,j,k;
48   int i0, j0;
49   int level = 1;
50   DataPartition *dP;
51   //VideoParameters *p_Vid = currMB->p_Vid;
52   Slice *currSlice = currMB->p_Slice;
53   const byte *partMap = assignSE2partition[currSlice->dp_mode];
54 
55   const byte (*pos_scan4x4)[2] = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
56   const byte *pos_scan_4x4 = pos_scan4x4[0];
57   int **cof = currSlice->cof[pl];
58 
59   for (j = block_y; j < block_y + BLOCK_SIZE_8x8; j += 4)
60   {
61     currMB->subblock_y = j; // position for coeff_count ctx
62 
63     for (i = block_x; i < block_x + BLOCK_SIZE_8x8; i += 4)
64     {
65       currMB->subblock_x = i; // position for coeff_count ctx
66       pos_scan_4x4 = pos_scan4x4[start_scan];
67       level = 1;
68 
69       if (start_scan == 0)
70       {
71         /*
72         * make distinction between INTRA and INTER coded
73         * luminance coefficients
74         */
75         currSE->type = (currMB->is_intra_block ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER);
76         dP = &(currSlice->partArr[partMap[currSE->type]]);
77         if (dP->bitstream->ei_flag)
78           currSE->mapping = linfo_levrun_inter;
79         else
80           currSE->reading = readRunLevel_CABAC;
81 
82 #if TRACE
83         if (pl == PLANE_Y)
84           sprintf(currSE->tracestring, "Luma sng ");
85         else if (pl == PLANE_U)
86           sprintf(currSE->tracestring, "Cb   sng ");
87         else
88           sprintf(currSE->tracestring, "Cr   sng ");
89 #endif
90 
91         dP->readSyntaxElement(currMB, currSE, dP);
92         level = currSE->value1;
93 
94         if (level != 0)    /* leave if level == 0 */
95         {
96           pos_scan_4x4 += 2 * currSE->value2;
97 
98           i0 = *pos_scan_4x4++;
99           j0 = *pos_scan_4x4++;
100 
101           *cbp_blk |= i64_power2(j + (i >> 2)) ;
102           //cof[j + j0][i + i0]= rshift_rnd_sf((level * InvLevelScale4x4[j0][i0]) << qp_per, 4);
103           cof[j + j0][i + i0]= level;
104           //currSlice->fcf[pl][j + j0][i + i0]= level;
105         }
106       }
107 
108       if (level != 0)
109       {
110         // make distinction between INTRA and INTER coded luminance coefficients
111         currSE->type = (currMB->is_intra_block ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
112         dP = &(currSlice->partArr[partMap[currSE->type]]);
113 
114         if (dP->bitstream->ei_flag)
115           currSE->mapping = linfo_levrun_inter;
116         else
117           currSE->reading = readRunLevel_CABAC;
118 
119         for(k = 1; (k < 17) && (level != 0); ++k)
120         {
121 #if TRACE
122           if (pl == PLANE_Y)
123             sprintf(currSE->tracestring, "Luma sng ");
124           else if (pl == PLANE_U)
125             sprintf(currSE->tracestring, "Cb   sng ");
126           else
127             sprintf(currSE->tracestring, "Cr   sng ");
128 #endif
129 
130           dP->readSyntaxElement(currMB, currSE, dP);
131           level = currSE->value1;
132 
133           if (level != 0)    /* leave if level == 0 */
134           {
135             pos_scan_4x4 += 2 * currSE->value2;
136 
137             i0 = *pos_scan_4x4++;
138             j0 = *pos_scan_4x4++;
139 
140             cof[j + j0][i + i0] = level;
141             //currSlice->fcf[pl][j + j0][i + i0]= level;
142           }
143         }
144       }
145     }
146   }
147 }
148 
149 /*!
150 ************************************************************************
151 * \brief
152 *    Get coefficients (run/level) of all 4x4 blocks in a MB
153 *    from the NAL (CABAC Mode)
154 ************************************************************************
155 */
read_comp_coeff_4x4_CABAC(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl,int (* InvLevelScale4x4)[4],int qp_per,int cbp)156 static void read_comp_coeff_4x4_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp)
157 {
158   Slice *currSlice = currMB->p_Slice;
159   VideoParameters *p_Vid = currMB->p_Vid;
160   int start_scan = IS_I16MB (currMB)? 1 : 0;
161   int block_y, block_x;
162   int i, j;
163   int64 *cbp_blk = &currMB->s_cbp[pl].blk;
164 
165   if( pl == PLANE_Y || (p_Vid->separate_colour_plane_flag != 0) )
166     currSE->context = (IS_I16MB(currMB) ? LUMA_16AC: LUMA_4x4);
167   else if (pl == PLANE_U)
168     currSE->context = (IS_I16MB(currMB) ? CB_16AC: CB_4x4);
169   else
170     currSE->context = (IS_I16MB(currMB) ? CR_16AC: CR_4x4);
171 
172   for (block_y = 0; block_y < MB_BLOCK_SIZE; block_y += BLOCK_SIZE_8x8) /* all modes */
173   {
174     int **cof = &currSlice->cof[pl][block_y];
175     for (block_x = 0; block_x < MB_BLOCK_SIZE; block_x += BLOCK_SIZE_8x8)
176     {
177       if (cbp & (1 << ((block_y >> 2) + (block_x >> 3))))  // are there any coeff in current block at all
178       {
179         read_comp_coeff_4x4_smb_CABAC (currMB, currSE, pl, block_y, block_x, start_scan, cbp_blk);
180 
181         if (start_scan == 0)
182         {
183           for (j = 0; j < BLOCK_SIZE_8x8; ++j)
184           {
185             int *coef = &cof[j][block_x];
186             int jj = j & 0x03;
187             for (i = 0; i < BLOCK_SIZE_8x8; i+=4)
188             {
189               if (*coef)
190                 *coef = rshift_rnd_sf((*coef * InvLevelScale4x4[jj][0]) << qp_per, 4);
191               coef++;
192               if (*coef)
193                 *coef = rshift_rnd_sf((*coef * InvLevelScale4x4[jj][1]) << qp_per, 4);
194               coef++;
195               if (*coef)
196                 *coef = rshift_rnd_sf((*coef * InvLevelScale4x4[jj][2]) << qp_per, 4);
197               coef++;
198               if (*coef)
199                 *coef = rshift_rnd_sf((*coef * InvLevelScale4x4[jj][3]) << qp_per, 4);
200               coef++;
201             }
202           }
203         }
204         else
205         {
206           for (j = 0; j < BLOCK_SIZE_8x8; ++j)
207           {
208             int *coef = &cof[j][block_x];
209             int jj = j & 0x03;
210             for (i = 0; i < BLOCK_SIZE_8x8; i += 4)
211             {
212               if ((jj != 0) && *coef)
213                 *coef= rshift_rnd_sf((*coef * InvLevelScale4x4[jj][0]) << qp_per, 4);
214               coef++;
215               if (*coef)
216                 *coef= rshift_rnd_sf((*coef * InvLevelScale4x4[jj][1]) << qp_per, 4);
217               coef++;
218               if (*coef)
219                 *coef= rshift_rnd_sf((*coef * InvLevelScale4x4[jj][2]) << qp_per, 4);
220               coef++;
221               if (*coef)
222                 *coef= rshift_rnd_sf((*coef * InvLevelScale4x4[jj][3]) << qp_per, 4);
223               coef++;
224             }
225           }
226         }
227       }
228     }
229   }
230 }
231 
232 
233 /*!
234 ************************************************************************
235 * \brief
236 *    Get coefficients (run/level) of all 4x4 blocks in a MB
237 *    from the NAL (CABAC Mode)
238 ************************************************************************
239 */
read_comp_coeff_4x4_CABAC_ls(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl,int (* InvLevelScale4x4)[4],int qp_per,int cbp)240 static void read_comp_coeff_4x4_CABAC_ls (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int (*InvLevelScale4x4)[4], int qp_per, int cbp)
241 {
242   VideoParameters *p_Vid = currMB->p_Vid;
243   int start_scan = IS_I16MB (currMB)? 1 : 0;
244   int block_y, block_x;
245   int64 *cbp_blk = &currMB->s_cbp[pl].blk;
246 
247   if( pl == PLANE_Y || (p_Vid->separate_colour_plane_flag != 0) )
248     currSE->context = (IS_I16MB(currMB) ? LUMA_16AC: LUMA_4x4);
249   else if (pl == PLANE_U)
250     currSE->context = (IS_I16MB(currMB) ? CB_16AC: CB_4x4);
251   else
252     currSE->context = (IS_I16MB(currMB) ? CR_16AC: CR_4x4);
253 
254   for (block_y = 0; block_y < MB_BLOCK_SIZE; block_y += BLOCK_SIZE_8x8) /* all modes */
255   {
256     for (block_x = 0; block_x < MB_BLOCK_SIZE; block_x += BLOCK_SIZE_8x8)
257     {
258       if (cbp & (1 << ((block_y >> 2) + (block_x >> 3))))  // are there any coeff in current block at all
259       {
260         read_comp_coeff_4x4_smb_CABAC (currMB, currSE, pl, block_y, block_x, start_scan, cbp_blk);
261       }
262     }
263   }
264 }
265 
266 
267 /*!
268 ************************************************************************
269 * \brief
270 *    Get coefficients (run/level) of one 8x8 block
271 *    from the NAL (CABAC Mode)
272 ************************************************************************
273 */
readCompCoeff8x8_CABAC(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl,int b8)274 static void readCompCoeff8x8_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int b8)
275 {
276   if (currMB->cbp & (1<<b8))  // are there any coefficients in the current block
277   {
278     VideoParameters *p_Vid = currMB->p_Vid;
279     int transform_pl = (p_Vid->separate_colour_plane_flag != 0) ? currMB->p_Slice->colour_plane_id : pl;
280 
281     int **tcoeffs;
282     int i,j,k;
283     int level = 1;
284 
285     DataPartition *dP;
286     Slice *currSlice = currMB->p_Slice;
287     const byte *partMap = assignSE2partition[currSlice->dp_mode];
288     int boff_x, boff_y;
289 
290     int64 cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
291     int64 *cur_cbp = &currMB->s_cbp[pl].blk;
292 
293     // select scan type
294     const byte (*pos_scan8x8) = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8[0] : FIELD_SCAN8x8[0];
295 
296     int qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[pl] ];
297     int qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[pl] ];
298 
299     int (*InvLevelScale8x8)[8] = (currMB->is_intra_block == TRUE) ? currSlice->InvLevelScale8x8_Intra[transform_pl][qp_rem] : currSlice->InvLevelScale8x8_Inter[transform_pl][qp_rem];
300 
301     // === set offset in current macroblock ===
302     boff_x = (b8&0x01) << 3;
303     boff_y = (b8 >> 1) << 3;
304     tcoeffs = &currSlice->mb_rres[pl][boff_y];
305 
306     currMB->subblock_x = boff_x; // position for coeff_count ctx
307     currMB->subblock_y = boff_y; // position for coeff_count ctx
308 
309     if (pl==PLANE_Y || (p_Vid->separate_colour_plane_flag != 0))
310       currSE->context = LUMA_8x8;
311     else if (pl==PLANE_U)
312       currSE->context = CB_8x8;
313     else
314       currSE->context = CR_8x8;
315 
316     currSE->reading = readRunLevel_CABAC;
317 
318     // Read DC
319     currSE->type = ((currMB->is_intra_block == 1) ? SE_LUM_DC_INTRA : SE_LUM_DC_INTER ); // Intra or Inter?
320     dP = &(currSlice->partArr[partMap[currSE->type]]);
321 
322 #if TRACE
323     if (pl==PLANE_Y)
324       sprintf(currSE->tracestring, "Luma8x8 DC sng ");
325     else if (pl==PLANE_U)
326       sprintf(currSE->tracestring, "Cb  8x8 DC sng ");
327     else
328       sprintf(currSE->tracestring, "Cr  8x8 DC sng ");
329 #endif
330 
331     dP->readSyntaxElement(currMB, currSE, dP);
332     level = currSE->value1;
333 
334     //============ decode =============
335     if (level != 0)    /* leave if level == 0 */
336     {
337       *cur_cbp |= cbp_mask;
338 
339       pos_scan8x8 += 2 * (currSE->value2);
340 
341       i = *pos_scan8x8++;
342       j = *pos_scan8x8++;
343 
344       tcoeffs[j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
345       //tcoeffs[ j][boff_x + i] = level;
346 
347       // AC coefficients
348       currSE->type    = ((currMB->is_intra_block == 1) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER);
349       dP = &(currSlice->partArr[partMap[currSE->type]]);
350 
351       for(k = 1;(k < 65) && (level != 0);++k)
352       {
353 #if TRACE
354         if (pl==PLANE_Y)
355           sprintf(currSE->tracestring, "Luma8x8 sng ");
356         else if (pl==PLANE_U)
357           sprintf(currSE->tracestring, "Cb  8x8 sng ");
358         else
359           sprintf(currSE->tracestring, "Cr  8x8 sng ");
360 #endif
361 
362         dP->readSyntaxElement(currMB, currSE, dP);
363         level = currSE->value1;
364 
365         //============ decode =============
366         if (level != 0)    /* leave if level == 0 */
367         {
368           pos_scan8x8 += 2 * (currSE->value2);
369 
370           i = *pos_scan8x8++;
371           j = *pos_scan8x8++;
372 
373           tcoeffs[ j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
374           //tcoeffs[ j][boff_x + i] = level;
375         }
376       }
377       /*
378       for (j = 0; j < 8; j++)
379       {
380       for (i = 0; i < 8; i++)
381       {
382       if (tcoeffs[ j][boff_x + i])
383       tcoeffs[ j][boff_x + i] = rshift_rnd_sf((tcoeffs[ j][boff_x + i] * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
384       }
385       }
386       */
387     }
388   }
389 }
390 
391 /*!
392 ************************************************************************
393 * \brief
394 *    Get coefficients (run/level) of one 8x8 block
395 *    from the NAL (CABAC Mode - lossless)
396 ************************************************************************
397 */
readCompCoeff8x8_CABAC_lossless(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl,int b8)398 static void readCompCoeff8x8_CABAC_lossless (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl, int b8)
399 {
400   if (currMB->cbp & (1<<b8))  // are there any coefficients in the current block
401   {
402     VideoParameters *p_Vid = currMB->p_Vid;
403 
404     int **tcoeffs;
405     int i,j,k;
406     int level = 1;
407 
408     DataPartition *dP;
409     Slice *currSlice = currMB->p_Slice;
410     const byte *partMap = assignSE2partition[currSlice->dp_mode];
411     int boff_x, boff_y;
412 
413     int64 cbp_mask = (int64) 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
414     int64 *cur_cbp = &currMB->s_cbp[pl].blk;
415 
416     // select scan type
417     const byte (*pos_scan8x8) = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8[0] : FIELD_SCAN8x8[0];
418 
419     // === set offset in current macroblock ===
420     boff_x = (b8&0x01) << 3;
421     boff_y = (b8 >> 1) << 3;
422     tcoeffs = &currSlice->mb_rres[pl][boff_y];
423 
424     currMB->subblock_x = boff_x; // position for coeff_count ctx
425     currMB->subblock_y = boff_y; // position for coeff_count ctx
426 
427     if (pl==PLANE_Y || (p_Vid->separate_colour_plane_flag != 0))
428       currSE->context = LUMA_8x8;
429     else if (pl==PLANE_U)
430       currSE->context = CB_8x8;
431     else
432       currSE->context = CR_8x8;
433 
434     currSE->reading = readRunLevel_CABAC;
435 
436     for(k=0; (k < 65) && (level != 0);++k)
437     {
438       //============ read =============
439       /*
440       * make distinction between INTRA and INTER coded
441       * luminance coefficients
442       */
443 
444       currSE->type    = ((currMB->is_intra_block == 1)
445         ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
446         : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
447 
448 #if TRACE
449       if (pl==PLANE_Y)
450         sprintf(currSE->tracestring, "Luma8x8 sng ");
451       else if (pl==PLANE_U)
452         sprintf(currSE->tracestring, "Cb  8x8 sng ");
453       else
454         sprintf(currSE->tracestring, "Cr  8x8 sng ");
455 #endif
456 
457       dP = &(currSlice->partArr[partMap[currSE->type]]);
458       currSE->reading = readRunLevel_CABAC;
459 
460       dP->readSyntaxElement(currMB, currSE, dP);
461       level = currSE->value1;
462 
463       //============ decode =============
464       if (level != 0)    /* leave if level == 0 */
465       {
466         pos_scan8x8 += 2 * (currSE->value2);
467 
468         i = *pos_scan8x8++;
469         j = *pos_scan8x8++;
470 
471         *cur_cbp |= cbp_mask;
472 
473         tcoeffs[j][boff_x + i] = level;
474       }
475     }
476   }
477 }
478 
479 
480 /*!
481 ************************************************************************
482 * \brief
483 *    Get coefficients (run/level) of 8x8 blocks in a MB
484 *    from the NAL (CABAC Mode)
485 ************************************************************************
486 */
read_comp_coeff_8x8_MB_CABAC(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl)487 static void read_comp_coeff_8x8_MB_CABAC (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl)
488 {
489   //======= 8x8 transform size & CABAC ========
490   readCompCoeff8x8_CABAC (currMB, currSE, pl, 0);
491   readCompCoeff8x8_CABAC (currMB, currSE, pl, 1);
492   readCompCoeff8x8_CABAC (currMB, currSE, pl, 2);
493   readCompCoeff8x8_CABAC (currMB, currSE, pl, 3);
494 }
495 
496 
497 /*!
498 ************************************************************************
499 * \brief
500 *    Get coefficients (run/level) of 8x8 blocks in a MB
501 *    from the NAL (CABAC Mode)
502 ************************************************************************
503 */
read_comp_coeff_8x8_MB_CABAC_ls(Macroblock * currMB,SyntaxElement * currSE,ColorPlane pl)504 static void read_comp_coeff_8x8_MB_CABAC_ls (Macroblock *currMB, SyntaxElement *currSE, ColorPlane pl)
505 {
506   //======= 8x8 transform size & CABAC ========
507   readCompCoeff8x8_CABAC_lossless (currMB, currSE, pl, 0);
508   readCompCoeff8x8_CABAC_lossless (currMB, currSE, pl, 1);
509   readCompCoeff8x8_CABAC_lossless (currMB, currSE, pl, 2);
510   readCompCoeff8x8_CABAC_lossless (currMB, currSE, pl, 3);
511 }
512 
513 
514 /*!
515  ************************************************************************
516  * \brief
517  *    Get coded block pattern and coefficients (run/level)
518  *    from the NAL
519  ************************************************************************
520  */
read_CBP_and_coeffs_from_NAL_CABAC_420(Macroblock * currMB)521 static void read_CBP_and_coeffs_from_NAL_CABAC_420(Macroblock *currMB)
522 {
523   int i,j;
524   int level;
525   int cbp;
526   SyntaxElement currSE;
527   DataPartition *dP = NULL;
528   Slice *currSlice = currMB->p_Slice;
529   const byte *partMap = assignSE2partition[currSlice->dp_mode];
530   int i0, j0;
531 
532   int qp_per, qp_rem;
533   VideoParameters *p_Vid = currMB->p_Vid;
534   int smb = ((p_Vid->type==SP_SLICE) && (currMB->is_intra_block == FALSE)) || (p_Vid->type == SI_SLICE && currMB->mb_type == SI4MB);
535 
536   int qp_per_uv[2];
537   int qp_rem_uv[2];
538 
539   int intra = (currMB->is_intra_block == TRUE);
540 
541   StorablePicture *dec_picture = currSlice->dec_picture;
542   int yuv = dec_picture->chroma_format_idc - 1;
543 
544   int (*InvLevelScale4x4)[4] = NULL;
545 
546   // select scan type
547   const byte (*pos_scan4x4)[2] = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
548   const byte *pos_scan_4x4 = pos_scan4x4[0];
549 
550   if (!IS_I16MB (currMB))
551   {
552     int need_transform_size_flag;
553     //=====   C B P   =====
554     //---------------------
555     currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
556       ? SE_CBP_INTRA
557       : SE_CBP_INTER;
558 
559     dP = &(currSlice->partArr[partMap[currSE.type]]);
560 
561     if (dP->bitstream->ei_flag)
562     {
563       currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
564         ? currSlice->linfo_cbp_intra
565         : currSlice->linfo_cbp_inter;
566     }
567     else
568     {
569       currSE.reading = read_CBP_CABAC;
570     }
571 
572     TRACE_STRING("coded_block_pattern");
573     dP->readSyntaxElement(currMB, &currSE, dP);
574     currMB->cbp = cbp = currSE.value1;
575 
576     //============= Transform size flag for INTER MBs =============
577     //-------------------------------------------------------------
578     need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
579       (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
580       (currMB->NoMbPartLessThan8x8Flag))
581       && currMB->mb_type != I8MB && currMB->mb_type != I4MB
582       && (currMB->cbp&15)
583       && currSlice->Transform8x8Mode);
584 
585     if (need_transform_size_flag)
586     {
587       currSE.type   =  SE_HEADER;
588       dP = &(currSlice->partArr[partMap[SE_HEADER]]);
589       currSE.reading = readMB_transform_size_flag_CABAC;
590       TRACE_STRING("transform_size_8x8_flag");
591 
592       // read CAVLC transform_size_8x8_flag
593       if (dP->bitstream->ei_flag)
594       {
595         currSE.len = 1;
596         readSyntaxElement_FLC(&currSE, dP->bitstream);
597       }
598       else
599       {
600         dP->readSyntaxElement(currMB, &currSE, dP);
601       }
602       currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
603     }
604 
605     //=====   DQUANT   =====
606     //----------------------
607     // Delta quant only if nonzero coeffs
608     if (cbp !=0)
609     {
610       read_delta_quant(&currSE, dP, currMB, partMap, ((currMB->is_intra_block == FALSE)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
611 
612       if (currSlice->dp_mode)
613       {
614         if ((currMB->is_intra_block == FALSE) && currSlice->dpC_NotPresent )
615           currMB->dpl_flag = 1;
616 
617         if( intra && currSlice->dpB_NotPresent )
618         {
619           currMB->ei_flag = 1;
620           currMB->dpl_flag = 1;
621         }
622 
623         // check for prediction from neighbours
624         check_dp_neighbors (currMB);
625         if (currMB->dpl_flag)
626         {
627           cbp = 0;
628           currMB->cbp = cbp;
629         }
630       }
631     }
632   }
633   else // read DC coeffs for new intra modes
634   {
635     cbp = currMB->cbp;
636 
637     read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
638 
639     if (currSlice->dp_mode)
640     {
641       if (currSlice->dpB_NotPresent)
642       {
643         currMB->ei_flag  = 1;
644         currMB->dpl_flag = 1;
645       }
646       check_dp_neighbors (currMB);
647       if (currMB->dpl_flag)
648       {
649         currMB->cbp = cbp = 0;
650       }
651     }
652 
653     if (!currMB->dpl_flag)
654     {
655       int **cof = currSlice->cof[0];
656       int k;
657       pos_scan_4x4 = pos_scan4x4[0];
658 
659       currSE.type = SE_LUM_DC_INTRA;
660       dP = &(currSlice->partArr[partMap[currSE.type]]);
661 
662       currSE.context      = LUMA_16DC;
663       currSE.type         = SE_LUM_DC_INTRA;
664 
665       if (dP->bitstream->ei_flag)
666       {
667         currSE.mapping = linfo_levrun_inter;
668       }
669       else
670       {
671         currSE.reading = readRunLevel_CABAC;
672       }
673 
674       level = 1;                            // just to get inside the loop
675 
676       for(k = 0; (k < 17) && (level != 0); ++k)
677       {
678 #if TRACE
679         snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
680 #endif
681         dP->readSyntaxElement(currMB, &currSE, dP);
682         level = currSE.value1;
683 
684         if (level != 0)    /* leave if level == 0 */
685         {
686           pos_scan_4x4 += (2 * currSE.value2);
687 
688           i0 = ((*pos_scan_4x4++) << 2);
689           j0 = ((*pos_scan_4x4++) << 2);
690 
691           cof[j0][i0] = level;// add new intra DC coeff
692           //currSlice->fcf[0][j0][i0] = level;// add new intra DC coeff
693         }
694       }
695 
696       if(currMB->is_lossless == FALSE)
697         itrans_2(currMB, (ColorPlane) currSlice->colour_plane_id);// transform new intra DC
698     }
699   }
700 
701   update_qp(currMB, currSlice->qp);
702 
703   qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
704   qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
705 
706   // luma coefficients
707   //======= Other Modes & CABAC ========
708   //------------------------------------
709   if (cbp)
710   {
711     if(currMB->luma_transform_size_8x8_flag)
712     {
713       //======= 8x8 transform size & CABAC ========
714       currMB->read_comp_coeff_8x8_CABAC (currMB, &currSE, PLANE_Y);
715     }
716     else
717     {
718       InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[currSlice->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[currSlice->colour_plane_id][qp_rem];
719       currMB->read_comp_coeff_4x4_CABAC (currMB, &currSE, PLANE_Y, InvLevelScale4x4, qp_per, cbp);
720     }
721   }
722 
723   //init quant parameters for chroma
724   for(i=0; i < 2; ++i)
725   {
726     qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
727     qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
728   }
729 
730   //========================== CHROMA DC ============================
731   //-----------------------------------------------------------------
732   // chroma DC coeff
733   if(cbp>15)
734   {
735     CBPStructure  *s_cbp = &currMB->s_cbp[0];
736     int uv, ll, k, coef_ctr;
737 
738     for (ll = 0; ll < 3; ll += 2)
739     {
740       uv = ll >> 1;
741 
742       InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
743       //===================== CHROMA DC YUV420 ======================
744       memset(currSlice->cofu, 0, 4 *sizeof(int));
745       coef_ctr=-1;
746 
747       level = 1;
748       currMB->is_v_block  = ll;
749       currSE.context      = CHROMA_DC;
750       currSE.type         = (intra ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
751 
752       dP = &(currSlice->partArr[partMap[currSE.type]]);
753 
754       if (dP->bitstream->ei_flag)
755         currSE.mapping = linfo_levrun_c2x2;
756       else
757         currSE.reading = readRunLevel_CABAC;
758 
759       for(k = 0; (k < (p_Vid->num_cdc_coeff + 1))&&(level!=0);++k)
760       {
761 #if TRACE
762         snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x2 DC Chroma ");
763 #endif
764 
765         dP->readSyntaxElement(currMB, &currSE, dP);
766         level = currSE.value1;
767 
768         if (level != 0)
769         {
770           s_cbp->blk |= 0xf0000 << (ll<<1) ;
771           coef_ctr += currSE.value2 + 1;
772 
773           // Bug: currSlice->cofu has only 4 entries, hence coef_ctr MUST be <4 (which is
774           // caught by the assert().  If it is bigger than 4, it starts patching the
775           // p_Vid->predmode pointer, which leads to bugs later on.
776           //
777           // This assert() should be left in the code, because it captures a very likely
778           // bug early when testing in error prone environments (or when testing NAL
779           // functionality).
780 
781           assert (coef_ctr < p_Vid->num_cdc_coeff);
782           currSlice->cofu[coef_ctr] = level;
783         }
784       }
785 
786 
787       if (smb || (currMB->is_lossless == TRUE)) // check to see if MB type is SPred or SIntra4x4
788       {
789         currSlice->cof[uv + 1][0][0] = currSlice->cofu[0];
790         currSlice->cof[uv + 1][0][4] = currSlice->cofu[1];
791         currSlice->cof[uv + 1][4][0] = currSlice->cofu[2];
792         currSlice->cof[uv + 1][4][4] = currSlice->cofu[3];
793         //currSlice->fcf[uv + 1][0][0] = currSlice->cofu[0];
794         //currSlice->fcf[uv + 1][4][0] = currSlice->cofu[1];
795         //currSlice->fcf[uv + 1][0][4] = currSlice->cofu[2];
796         //currSlice->fcf[uv + 1][4][4] = currSlice->cofu[3];
797       }
798       else
799       {
800         int temp[4];
801         int scale_dc = InvLevelScale4x4[0][0];
802         int **cof = currSlice->cof[uv + 1];
803 
804         ihadamard2x2(currSlice->cofu, temp);
805 
806         //currSlice->fcf[uv + 1][0][0] = temp[0];
807         //currSlice->fcf[uv + 1][0][4] = temp[1];
808         //currSlice->fcf[uv + 1][4][0] = temp[2];
809         //currSlice->fcf[uv + 1][4][4] = temp[3];
810 
811         cof[0][0] = (((temp[0] * scale_dc) << qp_per_uv[uv]) >> 5);
812         cof[0][4] = (((temp[1] * scale_dc) << qp_per_uv[uv]) >> 5);
813         cof[4][0] = (((temp[2] * scale_dc) << qp_per_uv[uv]) >> 5);
814         cof[4][4] = (((temp[3] * scale_dc) << qp_per_uv[uv]) >> 5);
815       }
816     }
817   }
818 
819   //========================== CHROMA AC ============================
820   //-----------------------------------------------------------------
821   // chroma AC coeff, all zero fram start_scan
822   if (cbp >31)
823   {
824     currSE.context      = CHROMA_AC;
825     currSE.type         = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
826 
827     dP = &(currSlice->partArr[partMap[currSE.type]]);
828 
829     if (dP->bitstream->ei_flag)
830       currSE.mapping = linfo_levrun_inter;
831     else
832       currSE.reading = readRunLevel_CABAC;
833 
834     if(currMB->is_lossless == FALSE)
835     {
836       int b4, b8, uv, k;
837       int **cof;
838       CBPStructure  *s_cbp = &currMB->s_cbp[0];
839       for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
840       {
841         currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
842         InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
843         cof = currSlice->cof[uv + 1];
844 
845         for (b4 = 0; b4 < 4; ++b4)
846         {
847           i = cofuv_blk_x[yuv][b8][b4];
848           j = cofuv_blk_y[yuv][b8][b4];
849 
850           currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
851           currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
852 
853           pos_scan_4x4 = pos_scan4x4[1];
854           level = 1;
855 
856           for(k = 0; (k < 16) && (level != 0);++k)
857           {
858 #if TRACE
859             snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
860 #endif
861 
862             dP->readSyntaxElement(currMB, &currSE, dP);
863             level = currSE.value1;
864 
865             if (level != 0)
866             {
867               s_cbp->blk |= i64_power2(cbp_blk_chroma[b8][b4]);
868               pos_scan_4x4 += (currSE.value2 << 1);
869 
870               i0 = *pos_scan_4x4++;
871               j0 = *pos_scan_4x4++;
872 
873               cof[(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
874               //currSlice->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
875             }
876           } //for(k=0;(k<16)&&(level!=0);++k)
877         }
878       }
879     }
880     else
881     {
882       CBPStructure  *s_cbp = &currMB->s_cbp[0];
883       int b4, b8, k;
884       int uv;
885       for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
886       {
887         currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
888 
889         for (b4=0; b4 < 4; ++b4)
890         {
891           i = cofuv_blk_x[yuv][b8][b4];
892           j = cofuv_blk_y[yuv][b8][b4];
893 
894           pos_scan_4x4 = pos_scan4x4[1];
895           level=1;
896 
897           currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
898           currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
899 
900           for(k=0;(k<16)&&(level!=0);++k)
901           {
902 #if TRACE
903             snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
904 #endif
905             dP->readSyntaxElement(currMB, &currSE, dP);
906             level = currSE.value1;
907 
908             if (level != 0)
909             {
910               s_cbp->blk |= i64_power2(cbp_blk_chroma[b8][b4]);
911               pos_scan_4x4 += (currSE.value2 << 1);
912 
913               i0 = *pos_scan_4x4++;
914               j0 = *pos_scan_4x4++;
915 
916               currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
917               //currSlice->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
918             }
919           }
920         }
921       }
922     } //for (b4=0; b4 < 4; b4++)
923   }
924 }
925 
926 
927 /*!
928  ************************************************************************
929  * \brief
930  *    Get coded block pattern and coefficients (run/level)
931  *    from the NAL
932  ************************************************************************
933  */
read_CBP_and_coeffs_from_NAL_CABAC_400(Macroblock * currMB)934 static void read_CBP_and_coeffs_from_NAL_CABAC_400(Macroblock *currMB)
935 {
936   int k;
937   int level;
938   int cbp;
939   SyntaxElement currSE;
940   DataPartition *dP = NULL;
941   Slice *currSlice = currMB->p_Slice;
942   const byte *partMap = assignSE2partition[currSlice->dp_mode];
943   int i0, j0;
944 
945   int qp_per, qp_rem;
946   VideoParameters *p_Vid = currMB->p_Vid;
947 
948   int intra = (currMB->is_intra_block == TRUE);
949 
950   int need_transform_size_flag;
951 
952   int (*InvLevelScale4x4)[4] = NULL;
953   // select scan type
954   const byte (*pos_scan4x4)[2] = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
955   const byte *pos_scan_4x4 = pos_scan4x4[0];
956 
957 
958   // read CBP if not new intra mode
959   if (!IS_I16MB (currMB))
960   {
961     //=====   C B P   =====
962     //---------------------
963     currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
964       ? SE_CBP_INTRA
965       : SE_CBP_INTER;
966 
967     dP = &(currSlice->partArr[partMap[currSE.type]]);
968 
969     if (dP->bitstream->ei_flag)
970     {
971       currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
972         ? currSlice->linfo_cbp_intra
973         : currSlice->linfo_cbp_inter;
974     }
975     else
976     {
977       currSE.reading = read_CBP_CABAC;
978     }
979 
980     TRACE_STRING("coded_block_pattern");
981     dP->readSyntaxElement(currMB, &currSE, dP);
982     currMB->cbp = cbp = currSE.value1;
983 
984 
985     //============= Transform size flag for INTER MBs =============
986     //-------------------------------------------------------------
987     need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
988       (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
989       (currMB->NoMbPartLessThan8x8Flag))
990       && currMB->mb_type != I8MB && currMB->mb_type != I4MB
991       && (currMB->cbp&15)
992       && currSlice->Transform8x8Mode);
993 
994     if (need_transform_size_flag)
995     {
996       currSE.type   =  SE_HEADER;
997       dP = &(currSlice->partArr[partMap[SE_HEADER]]);
998       currSE.reading = readMB_transform_size_flag_CABAC;
999       TRACE_STRING("transform_size_8x8_flag");
1000 
1001       // read CAVLC transform_size_8x8_flag
1002       if (dP->bitstream->ei_flag)
1003       {
1004         currSE.len = 1;
1005         readSyntaxElement_FLC(&currSE, dP->bitstream);
1006       }
1007       else
1008       {
1009         dP->readSyntaxElement(currMB, &currSE, dP);
1010       }
1011       currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
1012     }
1013 
1014     //=====   DQUANT   =====
1015     //----------------------
1016     // Delta quant only if nonzero coeffs
1017     if (cbp !=0)
1018     {
1019       read_delta_quant(&currSE, dP, currMB, partMap, ((currMB->is_intra_block == FALSE)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
1020 
1021       if (currSlice->dp_mode)
1022       {
1023         if ((currMB->is_intra_block == FALSE) && currSlice->dpC_NotPresent )
1024           currMB->dpl_flag = 1;
1025 
1026         if( intra && currSlice->dpB_NotPresent )
1027         {
1028           currMB->ei_flag = 1;
1029           currMB->dpl_flag = 1;
1030         }
1031 
1032         // check for prediction from neighbours
1033         check_dp_neighbors (currMB);
1034         if (currMB->dpl_flag)
1035         {
1036           cbp = 0;
1037           currMB->cbp = cbp;
1038         }
1039       }
1040     }
1041   }
1042   else // read DC coeffs for new intra modes
1043   {
1044     cbp = currMB->cbp;
1045     read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
1046 
1047     if (currSlice->dp_mode)
1048     {
1049       if (currSlice->dpB_NotPresent)
1050       {
1051         currMB->ei_flag  = 1;
1052         currMB->dpl_flag = 1;
1053       }
1054       check_dp_neighbors (currMB);
1055       if (currMB->dpl_flag)
1056       {
1057         currMB->cbp = cbp = 0;
1058       }
1059     }
1060 
1061     if (!currMB->dpl_flag)
1062     {
1063       pos_scan_4x4 = pos_scan4x4[0];
1064 
1065       {
1066         currSE.type = SE_LUM_DC_INTRA;
1067         dP = &(currSlice->partArr[partMap[currSE.type]]);
1068 
1069         currSE.context      = LUMA_16DC;
1070         currSE.type         = SE_LUM_DC_INTRA;
1071 
1072         if (dP->bitstream->ei_flag)
1073         {
1074           currSE.mapping = linfo_levrun_inter;
1075         }
1076         else
1077         {
1078           currSE.reading = readRunLevel_CABAC;
1079         }
1080 
1081         level = 1;                            // just to get inside the loop
1082 
1083         for(k = 0; (k < 17) && (level != 0); ++k)
1084         {
1085 #if TRACE
1086           snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
1087 #endif
1088           dP->readSyntaxElement(currMB, &currSE, dP);
1089           level = currSE.value1;
1090 
1091           if (level != 0)    /* leave if level == 0 */
1092           {
1093             pos_scan_4x4 += (2 * currSE.value2);
1094 
1095             i0 = ((*pos_scan_4x4++) << 2);
1096             j0 = ((*pos_scan_4x4++) << 2);
1097 
1098             currSlice->cof[0][j0][i0] = level;// add new intra DC coeff
1099             //currSlice->fcf[0][j0][i0] = level;// add new intra DC coeff
1100           }
1101         }
1102       }
1103 
1104       if(currMB->is_lossless == FALSE)
1105         itrans_2(currMB, (ColorPlane) currSlice->colour_plane_id);// transform new intra DC
1106     }
1107   }
1108 
1109   update_qp(currMB, currSlice->qp);
1110 
1111   qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[PLANE_Y] ];
1112   qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[PLANE_Y] ];
1113 
1114   //======= Other Modes & CABAC ========
1115   //------------------------------------
1116   if (cbp)
1117   {
1118     if(currMB->luma_transform_size_8x8_flag)
1119     {
1120       //======= 8x8 transform size & CABAC ========
1121       currMB->read_comp_coeff_8x8_CABAC (currMB, &currSE, PLANE_Y);
1122     }
1123     else
1124     {
1125       InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[currSlice->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[currSlice->colour_plane_id][qp_rem];
1126       currMB->read_comp_coeff_4x4_CABAC (currMB, &currSE, PLANE_Y, InvLevelScale4x4, qp_per, cbp);
1127     }
1128   }
1129 }
1130 
1131 /*!
1132  ************************************************************************
1133  * \brief
1134  *    Get coded block pattern and coefficients (run/level)
1135  *    from the NAL
1136  ************************************************************************
1137  */
read_CBP_and_coeffs_from_NAL_CABAC_444(Macroblock * currMB)1138 static void read_CBP_and_coeffs_from_NAL_CABAC_444(Macroblock *currMB)
1139 {
1140   int i, k;
1141   int level;
1142   int cbp;
1143   SyntaxElement currSE;
1144   DataPartition *dP = NULL;
1145   Slice *currSlice = currMB->p_Slice;
1146   const byte *partMap = assignSE2partition[currSlice->dp_mode];
1147   int coef_ctr, i0, j0;
1148 
1149   int qp_per, qp_rem;
1150   VideoParameters *p_Vid = currMB->p_Vid;
1151 
1152   int uv;
1153   int qp_per_uv[2];
1154   int qp_rem_uv[2];
1155 
1156   int intra = (currMB->is_intra_block == TRUE);
1157 
1158 
1159   int need_transform_size_flag;
1160 
1161   int (*InvLevelScale4x4)[4] = NULL;
1162   // select scan type
1163   const byte (*pos_scan4x4)[2] = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
1164   const byte *pos_scan_4x4 = pos_scan4x4[0];
1165 
1166   // QPI
1167   //init constants for every chroma qp offset
1168   for (i=0; i<2; ++i)
1169   {
1170     qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
1171     qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
1172   }
1173 
1174 
1175   // read CBP if not new intra mode
1176   if (!IS_I16MB (currMB))
1177   {
1178     //=====   C B P   =====
1179     //---------------------
1180     currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
1181       ? SE_CBP_INTRA
1182       : SE_CBP_INTER;
1183 
1184     dP = &(currSlice->partArr[partMap[currSE.type]]);
1185 
1186     if (dP->bitstream->ei_flag)
1187     {
1188       currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
1189         ? currSlice->linfo_cbp_intra
1190         : currSlice->linfo_cbp_inter;
1191     }
1192     else
1193     {
1194       currSE.reading = read_CBP_CABAC;
1195     }
1196 
1197     TRACE_STRING("coded_block_pattern");
1198     dP->readSyntaxElement(currMB, &currSE, dP);
1199     currMB->cbp = cbp = currSE.value1;
1200 
1201 
1202     //============= Transform size flag for INTER MBs =============
1203     //-------------------------------------------------------------
1204     need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
1205       (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
1206       (currMB->NoMbPartLessThan8x8Flag))
1207       && currMB->mb_type != I8MB && currMB->mb_type != I4MB
1208       && (currMB->cbp&15)
1209       && currSlice->Transform8x8Mode);
1210 
1211     if (need_transform_size_flag)
1212     {
1213       currSE.type   =  SE_HEADER;
1214       dP = &(currSlice->partArr[partMap[SE_HEADER]]);
1215       currSE.reading = readMB_transform_size_flag_CABAC;
1216       TRACE_STRING("transform_size_8x8_flag");
1217 
1218       // read CAVLC transform_size_8x8_flag
1219       if (dP->bitstream->ei_flag)
1220       {
1221         currSE.len = 1;
1222         readSyntaxElement_FLC(&currSE, dP->bitstream);
1223       }
1224       else
1225       {
1226         dP->readSyntaxElement(currMB, &currSE, dP);
1227       }
1228       currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
1229     }
1230 
1231     //=====   DQUANT   =====
1232     //----------------------
1233     // Delta quant only if nonzero coeffs
1234     if (cbp !=0)
1235     {
1236       read_delta_quant(&currSE, dP, currMB, partMap, ((currMB->is_intra_block == FALSE)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
1237 
1238       if (currSlice->dp_mode)
1239       {
1240         if ((currMB->is_intra_block == FALSE) && currSlice->dpC_NotPresent )
1241           currMB->dpl_flag = 1;
1242 
1243         if( intra && currSlice->dpB_NotPresent )
1244         {
1245           currMB->ei_flag = 1;
1246           currMB->dpl_flag = 1;
1247         }
1248 
1249         // check for prediction from neighbours
1250         check_dp_neighbors (currMB);
1251         if (currMB->dpl_flag)
1252         {
1253           cbp = 0;
1254           currMB->cbp = cbp;
1255         }
1256       }
1257     }
1258   }
1259   else // read DC coeffs for new intra modes
1260   {
1261     cbp = currMB->cbp;
1262 
1263     read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
1264 
1265     if (currSlice->dp_mode)
1266     {
1267       if (currSlice->dpB_NotPresent)
1268       {
1269         currMB->ei_flag  = 1;
1270         currMB->dpl_flag = 1;
1271       }
1272       check_dp_neighbors (currMB);
1273       if (currMB->dpl_flag)
1274       {
1275         currMB->cbp = cbp = 0;
1276       }
1277     }
1278 
1279     if (!currMB->dpl_flag)
1280     {
1281       pos_scan_4x4 = pos_scan4x4[0];
1282 
1283       {
1284         currSE.type = SE_LUM_DC_INTRA;
1285         dP = &(currSlice->partArr[partMap[currSE.type]]);
1286 
1287         currSE.context      = LUMA_16DC;
1288         currSE.type         = SE_LUM_DC_INTRA;
1289 
1290         if (dP->bitstream->ei_flag)
1291         {
1292           currSE.mapping = linfo_levrun_inter;
1293         }
1294         else
1295         {
1296           currSE.reading = readRunLevel_CABAC;
1297         }
1298 
1299         level = 1;                            // just to get inside the loop
1300 
1301         for(k = 0; (k < 17) && (level != 0); ++k)
1302         {
1303 #if TRACE
1304           snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
1305 #endif
1306           dP->readSyntaxElement(currMB, &currSE, dP);
1307           level = currSE.value1;
1308 
1309           if (level != 0)    /* leave if level == 0 */
1310           {
1311             pos_scan_4x4 += (2 * currSE.value2);
1312 
1313             i0 = ((*pos_scan_4x4++) << 2);
1314             j0 = ((*pos_scan_4x4++) << 2);
1315 
1316             currSlice->cof[0][j0][i0] = level;// add new intra DC coeff
1317             //currSlice->fcf[0][j0][i0] = level;// add new intra DC coeff
1318           }
1319         }
1320       }
1321 
1322       if(currMB->is_lossless == FALSE)
1323         itrans_2(currMB, (ColorPlane) currSlice->colour_plane_id);// transform new intra DC
1324     }
1325   }
1326 
1327   update_qp(currMB, currSlice->qp);
1328 
1329   qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
1330   qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
1331 
1332   //init quant parameters for chroma
1333   for(i=0; i < 2; ++i)
1334   {
1335     qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
1336     qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
1337   }
1338 
1339 
1340   InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[currSlice->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[currSlice->colour_plane_id][qp_rem];
1341 
1342   // luma coefficients
1343   {
1344     //======= Other Modes & CABAC ========
1345     //------------------------------------
1346     if (cbp)
1347     {
1348       if(currMB->luma_transform_size_8x8_flag)
1349       {
1350         //======= 8x8 transform size & CABAC ========
1351         currMB->read_comp_coeff_8x8_CABAC (currMB, &currSE, PLANE_Y);
1352       }
1353       else
1354       {
1355         currMB->read_comp_coeff_4x4_CABAC (currMB, &currSE, PLANE_Y, InvLevelScale4x4, qp_per, cbp);
1356       }
1357     }
1358   }
1359 
1360   for (uv = 0; uv < 2; ++uv )
1361   {
1362     /*----------------------16x16DC Luma_Add----------------------*/
1363     if (IS_I16MB (currMB)) // read DC coeffs for new intra modes
1364     {
1365       {
1366         currSE.type = SE_LUM_DC_INTRA;
1367         dP = &(currSlice->partArr[partMap[currSE.type]]);
1368 
1369         if( (p_Vid->separate_colour_plane_flag != 0) )
1370           currSE.context = LUMA_16DC;
1371         else
1372           currSE.context = (uv==0) ? CB_16DC : CR_16DC;
1373 
1374         if (dP->bitstream->ei_flag)
1375         {
1376           currSE.mapping = linfo_levrun_inter;
1377         }
1378         else
1379         {
1380           currSE.reading = readRunLevel_CABAC;
1381         }
1382 
1383         coef_ctr = -1;
1384         level = 1;                            // just to get inside the loop
1385 
1386         for(k=0;(k<17) && (level!=0);++k)
1387         {
1388 #if TRACE
1389           if (uv == 0)
1390             snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cb   16x16 ");
1391           else
1392             snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Cr   16x16 ");
1393 #endif
1394 
1395           dP->readSyntaxElement(currMB, &currSE, dP);
1396           level = currSE.value1;
1397 
1398           if (level != 0)                     // leave if level == 0
1399           {
1400             coef_ctr += currSE.value2 + 1;
1401 
1402             i0 = pos_scan4x4[coef_ctr][0];
1403             j0 = pos_scan4x4[coef_ctr][1];
1404             currSlice->cof[uv + 1][j0<<2][i0<<2] = level;
1405             //currSlice->fcf[uv + 1][j0<<2][i0<<2] = level;
1406           }
1407         } //k loop
1408       } // else CAVLC
1409 
1410       if(currMB->is_lossless == FALSE)
1411       {
1412         itrans_2(currMB, (ColorPlane) (uv + 1)); // transform new intra DC
1413       }
1414     } //IS_I16MB
1415 
1416     update_qp(currMB, currSlice->qp);
1417 
1418     qp_per = p_Vid->qp_per_matrix[ (currSlice->qp + p_Vid->bitdepth_luma_qp_scale) ];
1419     qp_rem = p_Vid->qp_rem_matrix[ (currSlice->qp + p_Vid->bitdepth_luma_qp_scale) ];
1420 
1421     //init constants for every chroma qp offset
1422     qp_per_uv[uv] = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
1423     qp_rem_uv[uv] = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + p_Vid->bitdepth_chroma_qp_scale) ];
1424 
1425     InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
1426 
1427     {
1428       if (cbp)
1429       {
1430         if(currMB->luma_transform_size_8x8_flag)
1431         {
1432           //======= 8x8 transform size & CABAC ========
1433           currMB->read_comp_coeff_8x8_CABAC (currMB, &currSE, (ColorPlane) (PLANE_U + uv));
1434         }
1435         else //4x4
1436         {
1437           currMB->read_comp_coeff_4x4_CABAC (currMB, &currSE, (ColorPlane) (PLANE_U + uv), InvLevelScale4x4,  qp_per_uv[uv], cbp);
1438         }
1439       }
1440     }
1441   }
1442 }
1443 
1444 /*!
1445  ************************************************************************
1446  * \brief
1447  *    Get coded block pattern and coefficients (run/level)
1448  *    from the NAL
1449  ************************************************************************
1450  */
read_CBP_and_coeffs_from_NAL_CABAC_422(Macroblock * currMB)1451 static void read_CBP_and_coeffs_from_NAL_CABAC_422(Macroblock *currMB)
1452 {
1453   int i,j,k;
1454   int level;
1455   int cbp;
1456   SyntaxElement currSE;
1457   DataPartition *dP = NULL;
1458   Slice *currSlice = currMB->p_Slice;
1459   const byte *partMap = assignSE2partition[currSlice->dp_mode];
1460   int coef_ctr, i0, j0, b8;
1461   int ll;
1462 
1463   int qp_per, qp_rem;
1464   VideoParameters *p_Vid = currMB->p_Vid;
1465 
1466   int uv;
1467   int qp_per_uv[2];
1468   int qp_rem_uv[2];
1469 
1470   int intra = (currMB->is_intra_block == TRUE);
1471 
1472   int b4;
1473   StorablePicture *dec_picture = currSlice->dec_picture;
1474   int yuv = dec_picture->chroma_format_idc - 1;
1475   int m6[4];
1476 
1477   int need_transform_size_flag;
1478 
1479   int (*InvLevelScale4x4)[4] = NULL;
1480   // select scan type
1481   const byte (*pos_scan4x4)[2] = ((currSlice->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
1482   const byte *pos_scan_4x4 = pos_scan4x4[0];
1483 
1484   // QPI
1485   //init constants for every chroma qp offset
1486   for (i=0; i<2; ++i)
1487   {
1488     qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
1489     qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
1490   }
1491 
1492   // read CBP if not new intra mode
1493   if (!IS_I16MB (currMB))
1494   {
1495     //=====   C B P   =====
1496     //---------------------
1497     currSE.type = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
1498       ? SE_CBP_INTRA
1499       : SE_CBP_INTER;
1500 
1501     dP = &(currSlice->partArr[partMap[currSE.type]]);
1502 
1503     if (dP->bitstream->ei_flag)
1504     {
1505       currSE.mapping = (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
1506         ? currSlice->linfo_cbp_intra
1507         : currSlice->linfo_cbp_inter;
1508     }
1509     else
1510     {
1511       currSE.reading = read_CBP_CABAC;
1512     }
1513 
1514     TRACE_STRING("coded_block_pattern");
1515     dP->readSyntaxElement(currMB, &currSE, dP);
1516     currMB->cbp = cbp = currSE.value1;
1517 
1518 
1519     //============= Transform size flag for INTER MBs =============
1520     //-------------------------------------------------------------
1521     need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
1522       (IS_DIRECT(currMB) && p_Vid->active_sps->direct_8x8_inference_flag) ||
1523       (currMB->NoMbPartLessThan8x8Flag))
1524       && currMB->mb_type != I8MB && currMB->mb_type != I4MB
1525       && (currMB->cbp&15)
1526       && currSlice->Transform8x8Mode);
1527 
1528     if (need_transform_size_flag)
1529     {
1530       currSE.type   =  SE_HEADER;
1531       dP = &(currSlice->partArr[partMap[SE_HEADER]]);
1532       currSE.reading = readMB_transform_size_flag_CABAC;
1533       TRACE_STRING("transform_size_8x8_flag");
1534 
1535       // read CAVLC transform_size_8x8_flag
1536       if (dP->bitstream->ei_flag)
1537       {
1538         currSE.len = 1;
1539         readSyntaxElement_FLC(&currSE, dP->bitstream);
1540       }
1541       else
1542       {
1543         dP->readSyntaxElement(currMB, &currSE, dP);
1544       }
1545       currMB->luma_transform_size_8x8_flag = (Boolean) currSE.value1;
1546     }
1547 
1548     //=====   DQUANT   =====
1549     //----------------------
1550     // Delta quant only if nonzero coeffs
1551     if (cbp !=0)
1552     {
1553       read_delta_quant(&currSE, dP, currMB, partMap, ((currMB->is_intra_block == FALSE)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA);
1554 
1555       if (currSlice->dp_mode)
1556       {
1557         if ((currMB->is_intra_block == FALSE) && currSlice->dpC_NotPresent )
1558           currMB->dpl_flag = 1;
1559 
1560         if( intra && currSlice->dpB_NotPresent )
1561         {
1562           currMB->ei_flag = 1;
1563           currMB->dpl_flag = 1;
1564         }
1565 
1566         // check for prediction from neighbours
1567         check_dp_neighbors (currMB);
1568         if (currMB->dpl_flag)
1569         {
1570           cbp = 0;
1571           currMB->cbp = cbp;
1572         }
1573       }
1574     }
1575   }
1576   else // read DC coeffs for new intra modes
1577   {
1578     cbp = currMB->cbp;
1579 
1580     read_delta_quant(&currSE, dP, currMB, partMap, SE_DELTA_QUANT_INTRA);
1581 
1582     if (currSlice->dp_mode)
1583     {
1584       if (currSlice->dpB_NotPresent)
1585       {
1586         currMB->ei_flag  = 1;
1587         currMB->dpl_flag = 1;
1588       }
1589       check_dp_neighbors (currMB);
1590       if (currMB->dpl_flag)
1591       {
1592         currMB->cbp = cbp = 0;
1593       }
1594     }
1595 
1596     if (!currMB->dpl_flag)
1597     {
1598       pos_scan_4x4 = pos_scan4x4[0];
1599 
1600       {
1601         currSE.type = SE_LUM_DC_INTRA;
1602         dP = &(currSlice->partArr[partMap[currSE.type]]);
1603 
1604         currSE.context      = LUMA_16DC;
1605         currSE.type         = SE_LUM_DC_INTRA;
1606 
1607         if (dP->bitstream->ei_flag)
1608         {
1609           currSE.mapping = linfo_levrun_inter;
1610         }
1611         else
1612         {
1613           currSE.reading = readRunLevel_CABAC;
1614         }
1615 
1616         level = 1;                            // just to get inside the loop
1617 
1618         for(k = 0; (k < 17) && (level != 0); ++k)
1619         {
1620 #if TRACE
1621           snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
1622 #endif
1623           dP->readSyntaxElement(currMB, &currSE, dP);
1624           level = currSE.value1;
1625 
1626           if (level != 0)    /* leave if level == 0 */
1627           {
1628             pos_scan_4x4 += (2 * currSE.value2);
1629 
1630             i0 = ((*pos_scan_4x4++) << 2);
1631             j0 = ((*pos_scan_4x4++) << 2);
1632 
1633             currSlice->cof[0][j0][i0] = level;// add new intra DC coeff
1634             //currSlice->fcf[0][j0][i0] = level;// add new intra DC coeff
1635           }
1636         }
1637       }
1638 
1639       if(currMB->is_lossless == FALSE)
1640         itrans_2(currMB, (ColorPlane) currSlice->colour_plane_id);// transform new intra DC
1641     }
1642   }
1643 
1644   update_qp(currMB, currSlice->qp);
1645 
1646   qp_per = p_Vid->qp_per_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
1647   qp_rem = p_Vid->qp_rem_matrix[ currMB->qp_scaled[currSlice->colour_plane_id] ];
1648 
1649   //init quant parameters for chroma
1650   for(i=0; i < 2; ++i)
1651   {
1652     qp_per_uv[i] = p_Vid->qp_per_matrix[ currMB->qp_scaled[i + 1] ];
1653     qp_rem_uv[i] = p_Vid->qp_rem_matrix[ currMB->qp_scaled[i + 1] ];
1654   }
1655 
1656   InvLevelScale4x4 = intra? currSlice->InvLevelScale4x4_Intra[currSlice->colour_plane_id][qp_rem] : currSlice->InvLevelScale4x4_Inter[currSlice->colour_plane_id][qp_rem];
1657 
1658   // luma coefficients
1659   {
1660     //======= Other Modes & CABAC ========
1661     //------------------------------------
1662     if (cbp)
1663     {
1664       if(currMB->luma_transform_size_8x8_flag)
1665       {
1666         //======= 8x8 transform size & CABAC ========
1667         currMB->read_comp_coeff_8x8_CABAC (currMB, &currSE, PLANE_Y);
1668       }
1669       else
1670       {
1671         currMB->read_comp_coeff_4x4_CABAC (currMB, &currSE, PLANE_Y, InvLevelScale4x4, qp_per, cbp);
1672       }
1673     }
1674   }
1675 
1676   //========================== CHROMA DC ============================
1677   //-----------------------------------------------------------------
1678   // chroma DC coeff
1679   if(cbp>15)
1680   {
1681     for (ll=0;ll<3;ll+=2)
1682     {
1683       int (*InvLevelScale4x4)[4] = NULL;
1684       uv = ll>>1;
1685       {
1686         int **imgcof = currSlice->cof[uv + 1];
1687         int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
1688         int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
1689         int qp_per_uv_dc = p_Vid->qp_per_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ];       //for YUV422 only
1690         int qp_rem_uv_dc = p_Vid->qp_rem_matrix[ (currMB->qpc[uv] + 3 + p_Vid->bitdepth_chroma_qp_scale) ];       //for YUV422 only
1691         if (intra)
1692           InvLevelScale4x4 = currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv_dc];
1693         else
1694           InvLevelScale4x4 = currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv_dc];
1695 
1696 
1697         //===================== CHROMA DC YUV422 ======================
1698         {
1699           CBPStructure  *s_cbp = &currMB->s_cbp[0];
1700           coef_ctr=-1;
1701           level=1;
1702           for(k=0;(k<9)&&(level!=0);++k)
1703           {
1704             currSE.context      = CHROMA_DC_2x4;
1705             currSE.type         = ((currMB->is_intra_block == TRUE) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
1706             currMB->is_v_block     = ll;
1707 
1708 #if TRACE
1709             snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x4 DC Chroma ");
1710 #endif
1711             dP = &(currSlice->partArr[partMap[currSE.type]]);
1712 
1713             if (dP->bitstream->ei_flag)
1714               currSE.mapping = linfo_levrun_c2x2;
1715             else
1716               currSE.reading = readRunLevel_CABAC;
1717 
1718             dP->readSyntaxElement(currMB, &currSE, dP);
1719 
1720             level = currSE.value1;
1721 
1722             if (level != 0)
1723             {
1724               s_cbp->blk |= ((int64)0xff0000) << (ll<<2) ;
1725               coef_ctr += currSE.value2 + 1;
1726               assert (coef_ctr < p_Vid->num_cdc_coeff);
1727               i0=SCAN_YUV422[coef_ctr][0];
1728               j0=SCAN_YUV422[coef_ctr][1];
1729 
1730               m3[i0][j0]=level;
1731             }
1732           }
1733         }
1734         // inverse CHROMA DC YUV422 transform
1735         // horizontal
1736         if(currMB->is_lossless == FALSE)
1737         {
1738           m4[0][0] = m3[0][0] + m3[1][0];
1739           m4[0][1] = m3[0][1] + m3[1][1];
1740           m4[0][2] = m3[0][2] + m3[1][2];
1741           m4[0][3] = m3[0][3] + m3[1][3];
1742 
1743           m4[1][0] = m3[0][0] - m3[1][0];
1744           m4[1][1] = m3[0][1] - m3[1][1];
1745           m4[1][2] = m3[0][2] - m3[1][2];
1746           m4[1][3] = m3[0][3] - m3[1][3];
1747 
1748           for (i = 0; i < 2; ++i)
1749           {
1750             m6[0] = m4[i][0] + m4[i][2];
1751             m6[1] = m4[i][0] - m4[i][2];
1752             m6[2] = m4[i][1] - m4[i][3];
1753             m6[3] = m4[i][1] + m4[i][3];
1754 
1755             imgcof[ 0][i<<2] = m6[0] + m6[3];
1756             imgcof[ 4][i<<2] = m6[1] + m6[2];
1757             imgcof[ 8][i<<2] = m6[1] - m6[2];
1758             imgcof[12][i<<2] = m6[0] - m6[3];
1759           }//for (i=0;i<2;++i)
1760 
1761           for(j = 0;j < p_Vid->mb_cr_size_y; j += BLOCK_SIZE)
1762           {
1763             for(i=0;i < p_Vid->mb_cr_size_x;i+=BLOCK_SIZE)
1764             {
1765               imgcof[j][i] = rshift_rnd_sf((imgcof[j][i] * InvLevelScale4x4[0][0]) << qp_per_uv_dc, 6);
1766             }
1767           }
1768         }
1769         else
1770         {
1771           for(j=0;j<4;++j)
1772           {
1773             for(i=0;i<2;++i)
1774             {
1775               currSlice->cof[uv + 1][j<<2][i<<2] = m3[i][j];
1776               //currSlice->fcf[uv + 1][j<<2][i<<2] = m3[i][j];
1777             }
1778           }
1779         }
1780 
1781       }
1782     }//for (ll=0;ll<3;ll+=2)
1783   }
1784 
1785   //========================== CHROMA AC ============================
1786   //-----------------------------------------------------------------
1787   // chroma AC coeff, all zero fram start_scan
1788   if (cbp<=31)
1789   {
1790   }
1791   else
1792   {
1793     {
1794       currSE.context      = CHROMA_AC;
1795       currSE.type         = (currMB->is_intra_block ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
1796 
1797       dP = &(currSlice->partArr[partMap[currSE.type]]);
1798 
1799       if (dP->bitstream->ei_flag)
1800         currSE.mapping = linfo_levrun_inter;
1801       else
1802         currSE.reading = readRunLevel_CABAC;
1803 
1804       if(currMB->is_lossless == FALSE)
1805       {
1806         CBPStructure  *s_cbp = &currMB->s_cbp[0];
1807         for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
1808         {
1809           currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
1810           InvLevelScale4x4 = intra ? currSlice->InvLevelScale4x4_Intra[uv + 1][qp_rem_uv[uv]] : currSlice->InvLevelScale4x4_Inter[uv + 1][qp_rem_uv[uv]];
1811 
1812           for (b4 = 0; b4 < 4; ++b4)
1813           {
1814             i = cofuv_blk_x[yuv][b8][b4];
1815             j = cofuv_blk_y[yuv][b8][b4];
1816 
1817             currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
1818             currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
1819 
1820             pos_scan_4x4 = pos_scan4x4[1];
1821             level=1;
1822 
1823             for(k = 0; (k < 16) && (level != 0);++k)
1824             {
1825 #if TRACE
1826               snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
1827 #endif
1828 
1829               dP->readSyntaxElement(currMB, &currSE, dP);
1830               level = currSE.value1;
1831 
1832               if (level != 0)
1833               {
1834                 s_cbp->blk |= i64_power2(cbp_blk_chroma[b8][b4]);
1835                 pos_scan_4x4 += (currSE.value2 << 1);
1836 
1837                 i0 = *pos_scan_4x4++;
1838                 j0 = *pos_scan_4x4++;
1839 
1840                 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = rshift_rnd_sf((level * InvLevelScale4x4[j0][i0])<<qp_per_uv[uv], 4);
1841                 //currSlice->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
1842               }
1843             } //for(k=0;(k<16)&&(level!=0);++k)
1844           }
1845         }
1846       }
1847       else
1848       {
1849         CBPStructure  *s_cbp = &currMB->s_cbp[0];
1850         for (b8=0; b8 < p_Vid->num_blk8x8_uv; ++b8)
1851         {
1852           currMB->is_v_block = uv = (b8 > ((p_Vid->num_uv_blocks) - 1 ));
1853 
1854           for (b4=0; b4 < 4; ++b4)
1855           {
1856             i = cofuv_blk_x[yuv][b8][b4];
1857             j = cofuv_blk_y[yuv][b8][b4];
1858 
1859             pos_scan_4x4 = pos_scan4x4[1];
1860             level=1;
1861 
1862             currMB->subblock_y = subblk_offset_y[yuv][b8][b4];
1863             currMB->subblock_x = subblk_offset_x[yuv][b8][b4];
1864 
1865             for(k=0;(k<16)&&(level!=0);++k)
1866             {
1867 #if TRACE
1868               snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
1869 #endif
1870               dP->readSyntaxElement(currMB, &currSE, dP);
1871               level = currSE.value1;
1872 
1873               if (level != 0)
1874               {
1875                 s_cbp->blk |= i64_power2(cbp_blk_chroma[b8][b4]);
1876                 pos_scan_4x4 += (currSE.value2 << 1);
1877 
1878                 i0 = *pos_scan_4x4++;
1879                 j0 = *pos_scan_4x4++;
1880 
1881                 currSlice->cof[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
1882                 //currSlice->fcf[uv + 1][(j<<2) + j0][(i<<2) + i0] = level;
1883               }
1884             }
1885           }
1886         }
1887       } //for (b4=0; b4 < 4; b4++)
1888     } //for (b8=0; b8 < p_Vid->num_blk8x8_uv; b8++)
1889   } //if (dec_picture->chroma_format_idc != YUV400)
1890 }
1891 
set_read_CBP_and_coeffs_cabac(Slice * currSlice)1892 void set_read_CBP_and_coeffs_cabac(Slice *currSlice)
1893 {
1894   switch (currSlice->p_Vid->active_sps->chroma_format_idc)
1895   {
1896   case YUV444:
1897     if (currSlice->p_Vid->separate_colour_plane_flag == 0)
1898     {
1899       currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC_444;
1900     }
1901     else
1902     {
1903       currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC_400;
1904     }
1905     break;
1906   case YUV422:
1907     currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC_422;
1908     break;
1909   case YUV420:
1910     currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC_420;
1911     break;
1912   case YUV400:
1913     currSlice->read_CBP_and_coeffs_from_NAL = read_CBP_and_coeffs_from_NAL_CABAC_400;
1914     break;
1915   default:
1916     assert (1);
1917     currSlice->read_CBP_and_coeffs_from_NAL = NULL;
1918     break;
1919   }
1920 }
1921 
1922 
1923 /*!
1924 ************************************************************************
1925 * \brief
1926 *    setup coefficient reading functions for CABAC
1927 *
1928 ************************************************************************
1929 */
set_read_comp_coeff_cabac(Macroblock * currMB)1930 void set_read_comp_coeff_cabac(Macroblock *currMB)
1931 {
1932   if (currMB->is_lossless == FALSE)
1933   {
1934     currMB->read_comp_coeff_4x4_CABAC = read_comp_coeff_4x4_CABAC;
1935     currMB->read_comp_coeff_8x8_CABAC = read_comp_coeff_8x8_MB_CABAC;
1936   }
1937   else
1938   {
1939     currMB->read_comp_coeff_4x4_CABAC = read_comp_coeff_4x4_CABAC_ls;
1940     currMB->read_comp_coeff_8x8_CABAC = read_comp_coeff_8x8_MB_CABAC_ls;
1941   }
1942 }
1943 
1944