• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 #include "avcdec_lib.h"
19 #include "avcdec_bitstream.h"
20 #include "oscl_mem.h"
21 
DecodeIntraPCM(AVCCommonObj * video,AVCDecBitstream * stream)22 AVCDec_Status DecodeIntraPCM(AVCCommonObj *video, AVCDecBitstream *stream)
23 {
24     AVCDec_Status status;
25     int j;
26     int mb_x, mb_y, offset1;
27     uint8 *pDst;
28     uint32 byte0, byte1;
29     int pitch;
30 
31     mb_x = video->mb_x;
32     mb_y = video->mb_y;
33 
34 #ifdef USE_PRED_BLOCK
35     pDst = video->pred_block + 84;
36     pitch = 20;
37 #else
38     offset1 = (mb_x << 4) + (mb_y << 4) * video->PicWidthInSamplesL;
39     pDst = video->currPic->Sl + offset1;
40     pitch = video->currPic->pitch;
41 #endif
42 
43     /* at this point bitstream is byte-aligned */
44     j = 16;
45     while (j > 0)
46     {
47         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
48         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
49         byte0 |= (byte1 << 8);
50         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
51         byte0 |= (byte1 << 16);
52         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
53         byte0 |= (byte1 << 24);
54         *((uint32*)pDst) = byte0;
55 
56         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
57         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
58         byte0 |= (byte1 << 8);
59         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
60         byte0 |= (byte1 << 16);
61         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
62         byte0 |= (byte1 << 24);
63         *((uint32*)(pDst + 4)) = byte0;
64 
65         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
66         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
67         byte0 |= (byte1 << 8);
68         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
69         byte0 |= (byte1 << 16);
70         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
71         byte0 |= (byte1 << 24);
72         *((uint32*)(pDst + 8)) = byte0;
73 
74         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
75         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
76         byte0 |= (byte1 << 8);
77         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
78         byte0 |= (byte1 << 16);
79         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
80         byte0 |= (byte1 << 24);
81         *((uint32*)(pDst + 12)) = byte0;
82         j--;
83         pDst += pitch;
84 
85         if (status != AVCDEC_SUCCESS)  /* check only once per line */
86             return status;
87     }
88 
89 #ifdef USE_PRED_BLOCK
90     pDst = video->pred_block + 452;
91     pitch = 12;
92 #else
93     offset1 = (offset1 >> 2) + (mb_x << 2);
94     pDst = video->currPic->Scb + offset1;
95     pitch >>= 1;
96 #endif
97 
98     j = 8;
99     while (j > 0)
100     {
101         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
102         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
103         byte0 |= (byte1 << 8);
104         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
105         byte0 |= (byte1 << 16);
106         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
107         byte0 |= (byte1 << 24);
108         *((uint32*)pDst) = byte0;
109 
110         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
111         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
112         byte0 |= (byte1 << 8);
113         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
114         byte0 |= (byte1 << 16);
115         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
116         byte0 |= (byte1 << 24);
117         *((uint32*)(pDst + 4)) = byte0;
118 
119         j--;
120         pDst += pitch;
121 
122         if (status != AVCDEC_SUCCESS)  /* check only once per line */
123             return status;
124     }
125 
126 #ifdef USE_PRED_BLOCK
127     pDst = video->pred_block + 596;
128     pitch = 12;
129 #else
130     pDst = video->currPic->Scr + offset1;
131 #endif
132     j = 8;
133     while (j > 0)
134     {
135         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
136         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
137         byte0 |= (byte1 << 8);
138         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
139         byte0 |= (byte1 << 16);
140         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
141         byte0 |= (byte1 << 24);
142         *((uint32*)pDst) = byte0;
143 
144         status = BitstreamReadBits(stream, 8, (uint*) & byte0);
145         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
146         byte0 |= (byte1 << 8);
147         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
148         byte0 |= (byte1 << 16);
149         status = BitstreamReadBits(stream, 8, (uint*) & byte1);
150         byte0 |= (byte1 << 24);
151         *((uint32*)(pDst + 4)) = byte0;
152 
153         j--;
154         pDst += pitch;
155 
156         if (status != AVCDEC_SUCCESS)  /* check only once per line */
157             return status;
158     }
159 
160 #ifdef MB_BASED_DEBLOCK
161     SaveNeighborForIntraPred(video, offset1);
162 #endif
163 
164     return AVCDEC_SUCCESS;
165 }
166 
167 
168 
169 /* see subclause 7.3.5.3 and readCBPandCoeffsFromNAL() in JM*/
residual(AVCDecObject * decvid,AVCMacroblock * currMB)170 AVCDec_Status residual(AVCDecObject *decvid, AVCMacroblock *currMB)
171 {
172     AVCCommonObj *video = decvid->common;
173     int16 *block;
174     int level[16], run[16], numcoeff; /* output from residual_block_cavlc */
175     int block_x, i, j, k, idx, iCbCr;
176     int mbPartIdx, subMbPartIdx, mbPartIdx_X, mbPartIdx_Y;
177     int nC, maxNumCoeff = 16;
178     int coeffNum, start_scan = 0;
179     uint8 *zz_scan;
180     int Rq, Qq;
181     uint32 cbp4x4 = 0;
182 
183     /* in 8.5.4, it only says if it's field macroblock. */
184 
185     zz_scan = (uint8*) ZZ_SCAN_BLOCK;
186 
187 
188     /* see 8.5.8 for the initialization of these values */
189     Qq = video->QPy_div_6;
190     Rq = video->QPy_mod_6;
191 
192     oscl_memset(video->block, 0, sizeof(int16)*NUM_PIXELS_IN_MB);
193 
194     if (currMB->mbMode == AVC_I16)
195     {
196         nC = predict_nnz(video, 0, 0);
197         decvid->residual_block(decvid, nC, 16, level, run, &numcoeff);
198         /* then performs zigzag and transform */
199         block = video->block;
200         coeffNum = -1;
201         for (i = numcoeff - 1; i >= 0; i--)
202         {
203             coeffNum += run[i] + 1;
204             if (coeffNum > 15)
205             {
206                 return AVCDEC_FAIL;
207             }
208             idx = zz_scan[coeffNum] << 2;
209             /*          idx = ((idx>>2)<<6) + ((idx&3)<<2); */
210             block[idx] = level[i];
211         }
212 
213         /* inverse transform on Intra16x16DCLevel */
214         if (numcoeff)
215         {
216             Intra16DCTrans(block, Qq, Rq);
217             cbp4x4 = 0xFFFF;
218         }
219         maxNumCoeff = 15;
220         start_scan = 1;
221     }
222 
223     oscl_memset(currMB->nz_coeff, 0, sizeof(uint8)*24);
224 
225     for (mbPartIdx = 0; mbPartIdx < 4; mbPartIdx++)
226     {
227         mbPartIdx_X = (mbPartIdx & 1) << 1;
228         mbPartIdx_Y = mbPartIdx & -2;
229 
230         if (currMB->CBP&(1 << mbPartIdx))
231         {
232             for (subMbPartIdx = 0; subMbPartIdx < 4; subMbPartIdx++)
233             {
234                 i = mbPartIdx_X + (subMbPartIdx & 1);  // check this
235                 j = mbPartIdx_Y + (subMbPartIdx >> 1);
236                 block = video->block + (j << 6) + (i << 2);  //
237                 nC = predict_nnz(video, i, j);
238                 decvid->residual_block(decvid, nC, maxNumCoeff, level, run, &numcoeff);
239 
240                 /* convert to raster scan and quantize*/
241                 /* Note: for P mb in SP slice and SI mb in SI slice,
242                  the quantization cannot be done here.
243                  block[idx] should be assigned with level[k].
244                 itrans will be done after the prediction.
245                 There will be transformation on the predicted value,
246                 then addition with block[idx], then this quantization
247                 and transform.*/
248 
249                 coeffNum = -1 + start_scan;
250                 for (k = numcoeff - 1; k >= 0; k--)
251                 {
252                     coeffNum += run[k] + 1;
253                     if (coeffNum > 15)
254                     {
255                         return AVCDEC_FAIL;
256                     }
257                     idx = zz_scan[coeffNum];
258                     block[idx] = (level[k] * dequant_coefres[Rq][coeffNum]) << Qq ;
259                 }
260 
261                 currMB->nz_coeff[(j<<2)+i] = numcoeff;
262                 if (numcoeff)
263                 {
264                     cbp4x4 |= (1 << ((j << 2) + i));
265                 }
266             }
267         }
268     }
269 
270     Qq = video->QPc_div_6;
271     Rq = video->QPc_mod_6;
272 
273     if (currMB->CBP & (3 << 4)) /* chroma DC residual present */
274     {
275         for (iCbCr = 0; iCbCr < 2; iCbCr++)
276         {
277             decvid->residual_block(decvid, -1, 4, level, run, &numcoeff);
278             block = video->block + 256 + (iCbCr << 3);
279             coeffNum = -1;
280             for (i = numcoeff - 1; i >= 0; i--)
281             {
282                 coeffNum += run[i] + 1;
283                 if (coeffNum > 3)
284                 {
285                     return AVCDEC_FAIL;
286                 }
287                 block[(coeffNum>>1)*64 + (coeffNum&1)*4] = level[i];
288             }
289             /* inverse transform on chroma DC */
290             /* for P in SP and SI in SI, this function can't be done here,
291             must do prediction transform/quant first. */
292             if (numcoeff)
293             {
294                 ChromaDCTrans(block, Qq, Rq);
295                 cbp4x4 |= (iCbCr ? 0xcc0000 : 0x330000);
296             }
297         }
298     }
299 
300     if (currMB->CBP & (2 << 4))
301     {
302         for (block_x = 0; block_x < 4; block_x += 2) /* for iCbCr */
303         {
304             for (j = 4; j < 6; j++)  /* for each block inside Cb or Cr */
305             {
306                 for (i = block_x; i < block_x + 2; i++)
307                 {
308 
309                     block = video->block + (j << 6) + (i << 2);
310 
311                     nC = predict_nnz_chroma(video, i, j);
312                     decvid->residual_block(decvid, nC, 15, level, run, &numcoeff);
313 
314                     /* convert to raster scan and quantize */
315                     /* for P MB in SP slice and SI MB in SI slice,
316                        the dequant and transform cannot be done here.
317                        It needs the prediction values. */
318                     coeffNum = 0;
319                     for (k = numcoeff - 1; k >= 0; k--)
320                     {
321                         coeffNum += run[k] + 1;
322                         if (coeffNum > 15)
323                         {
324                             return AVCDEC_FAIL;
325                         }
326                         idx = zz_scan[coeffNum];
327                         block[idx] = (level[k] * dequant_coefres[Rq][coeffNum]) << Qq;
328                     }
329 
330 
331                     /* then transform */
332                     //              itrans(block); /* transform */
333                     currMB->nz_coeff[(j<<2)+i] = numcoeff;    //
334                     if (numcoeff)
335                     {
336                         cbp4x4 |= (1 << ((j << 2) + i));
337                     }
338                 }
339 
340             }
341         }
342     }
343 
344     video->cbp4x4 = cbp4x4;
345 
346     return AVCDEC_SUCCESS;
347 }
348 
349 /* see subclause 7.3.5.3.1 and 9.2 and readCoeff4x4_CAVLC() in JM */
residual_block_cavlc(AVCDecObject * decvid,int nC,int maxNumCoeff,int * level,int * run,int * numcoeff)350 AVCDec_Status residual_block_cavlc(AVCDecObject *decvid, int nC, int maxNumCoeff,
351                                    int *level, int *run, int *numcoeff)
352 {
353     int i, j;
354     int TrailingOnes, TotalCoeff;
355     AVCDecBitstream *stream = decvid->bitstream;
356     int suffixLength;
357     uint trailing_ones_sign_flag, level_prefix, level_suffix;
358     int levelCode, levelSuffixSize, zerosLeft;
359     int run_before;
360 
361 
362     if (nC >= 0)
363     {
364         ce_TotalCoeffTrailingOnes(stream, &TrailingOnes, &TotalCoeff, nC);
365     }
366     else
367     {
368         ce_TotalCoeffTrailingOnesChromaDC(stream, &TrailingOnes, &TotalCoeff);
369     }
370 
371     *numcoeff = TotalCoeff;
372 
373     /* This part is done quite differently in ReadCoef4x4_CAVLC() */
374     if (TotalCoeff == 0)
375     {
376         return AVCDEC_SUCCESS;
377     }
378 
379     if (TrailingOnes) /* keep reading the sign of those trailing ones */
380     {
381         /* instead of reading one bit at a time, read the whole thing at once */
382         BitstreamReadBits(stream, TrailingOnes, &trailing_ones_sign_flag);
383         trailing_ones_sign_flag <<= 1;
384         for (i = 0; i < TrailingOnes; i++)
385         {
386             level[i] = 1 - ((trailing_ones_sign_flag >> (TrailingOnes - i - 1)) & 2);
387         }
388     }
389 
390     i = TrailingOnes;
391     suffixLength = 1;
392     if (TotalCoeff > TrailingOnes)
393     {
394         ce_LevelPrefix(stream, &level_prefix);
395         if (TotalCoeff < 11 || TrailingOnes == 3)
396         {
397             if (level_prefix < 14)
398             {
399 //              levelSuffixSize = 0;
400                 levelCode = level_prefix;
401             }
402             else if (level_prefix == 14)
403             {
404 //              levelSuffixSize = 4;
405                 BitstreamReadBits(stream, 4, &level_suffix);
406                 levelCode = 14 + level_suffix;
407             }
408             else /* if (level_prefix == 15) */
409             {
410 //              levelSuffixSize = 12;
411                 BitstreamReadBits(stream, 12, &level_suffix);
412                 levelCode = 30 + level_suffix;
413             }
414         }
415         else
416         {
417             /*              suffixLength = 1; */
418             if (level_prefix < 15)
419             {
420                 levelSuffixSize = suffixLength;
421             }
422             else
423             {
424                 levelSuffixSize = 12;
425             }
426             BitstreamReadBits(stream, levelSuffixSize, &level_suffix);
427 
428             levelCode = (level_prefix << 1) + level_suffix;
429         }
430 
431         if (TrailingOnes < 3)
432         {
433             levelCode += 2;
434         }
435 
436         level[i] = (levelCode + 2) >> 1;
437         if (level[i] > 3)
438         {
439             suffixLength = 2;
440         }
441 
442         if (levelCode & 1)
443         {
444             level[i] = -level[i];
445         }
446         i++;
447 
448     }
449 
450     for (j = TotalCoeff - i; j > 0 ; j--)
451     {
452         ce_LevelPrefix(stream, &level_prefix);
453         if (level_prefix < 15)
454         {
455             levelSuffixSize = suffixLength;
456         }
457         else
458         {
459             levelSuffixSize = 12;
460         }
461         BitstreamReadBits(stream, levelSuffixSize, &level_suffix);
462 
463         levelCode = (level_prefix << suffixLength) + level_suffix;
464         level[i] = (levelCode >> 1) + 1;
465         if (level[i] > (3 << (suffixLength - 1)) && suffixLength < 6)
466         {
467             suffixLength++;
468         }
469         if (levelCode & 1)
470         {
471             level[i] = -level[i];
472         }
473         i++;
474     }
475 
476 
477     if (TotalCoeff < maxNumCoeff)
478     {
479         if (nC >= 0)
480         {
481             ce_TotalZeros(stream, &zerosLeft, TotalCoeff);
482         }
483         else
484         {
485             ce_TotalZerosChromaDC(stream, &zerosLeft, TotalCoeff);
486         }
487     }
488     else
489     {
490         zerosLeft = 0;
491     }
492 
493     for (i = 0; i < TotalCoeff - 1; i++)
494     {
495         if (zerosLeft > 0)
496         {
497             ce_RunBefore(stream, &run_before, zerosLeft);
498             run[i] = run_before;
499         }
500         else
501         {
502             run[i] = 0;
503             zerosLeft = 0; // could be negative under error conditions
504         }
505 
506         zerosLeft = zerosLeft - run[i];
507     }
508 
509     if (zerosLeft < 0)
510     {
511         zerosLeft = 0;
512 //      return AVCDEC_FAIL;
513     }
514 
515     run[TotalCoeff-1] = zerosLeft;
516 
517     /* leave the inverse zigzag scan part for the caller */
518 
519 
520     return AVCDEC_SUCCESS;
521 }
522