PageRenderTime 71ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Avc/residual.cpp

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