mp4decvopi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* ///////////////////////////////////////////////////////////////////////
  2. //
  3. // INTEL CORPORATION PROPRIETARY INFORMATION
  4. // This software is supplied under the terms of a license agreement or
  5. // nondisclosure agreement with Intel Corporation and may not be copied
  6. // or disclosed except in accordance with the terms of that agreement.
  7. // Copyright (c) 2001-2007 Intel Corporation. All Rights Reserved.
  8. //
  9. // Description: Decodes I-VOPs
  10. //
  11. */
  12. #include "mp4def.h"
  13. #include "mp4dec.h"
  14. /*
  15. // decode mcbpc and set MBtype and ChromaPattern
  16. */
  17. /*static*/ mp4_Status mp4_DecodeMCBPC_I(mp4_Info* pInfo, int32_t *mbType, int32_t *mbPattern)
  18. {
  19. uint32_t code;
  20. int32_t type, pattern, fb;
  21. code = mp4_ShowBits9(pInfo, 9);
  22. if (code == 1) {
  23. type = IPPVC_MB_STUFFING;
  24. pattern = 0;
  25. fb = 9;
  26. } else if (code >= 64) {
  27. type = IPPVC_MBTYPE_INTRA;
  28. pattern = code >> 6;
  29. if (pattern >= 4) {
  30. pattern = 0;
  31. fb = 1;
  32. } else
  33. fb = 3;
  34. } else {
  35. type = IPPVC_MBTYPE_INTRA_Q;
  36. pattern = code >> 3;
  37. if (pattern >= 4) {
  38. pattern = 0;
  39. fb = 4;
  40. } else if (code >= 8) {
  41. fb = 6;
  42. } else {
  43. mp4_Error("Error: decoding MCBPC");
  44. return MP4_STATUS_ERROR;
  45. }
  46. }
  47. mp4_FlushBits(pInfo, fb);
  48. *mbType = type;
  49. *mbPattern = pattern;
  50. if (type == IPPVC_MBTYPE_INTRA)
  51. mp4_StatisticInc_(&pInfo->VisualObject.Statistic.nMB_INTRA);
  52. else if (type == IPPVC_MBTYPE_INTRA_Q)
  53. mp4_StatisticInc_(&pInfo->VisualObject.Statistic.nMB_INTRA_Q);
  54. return MP4_STATUS_OK;
  55. }
  56. /*
  57. // decode IVOP
  58. */
  59. mp4_Status mp4_DecodeVOP_I(mp4_Info* pInfo)
  60. {
  61. int32_t quant, quantPred, dcVLC, mb_type, cbpc, cbpy, ac_pred_flag;
  62. int32_t i, j, nmb, nmbgob, stepYc, stepCbc, stepCrc, stepFc[6], mbCurr, mbInVideoPacket, colNum, rowNum, mbPerRow, mbPerCol;
  63. uint8_t *pFc[6];
  64. mp4_Status sts;
  65. if (pInfo->VisualObject.cFrame)
  66. {
  67. DebugBreak();
  68. }
  69. pInfo->VisualObject.cFrame = CreateFrame(&pInfo->VisualObject);
  70. stepYc = pInfo->VisualObject.cFrame->stepY;
  71. stepCbc = pInfo->VisualObject.cFrame->stepCb;
  72. stepCrc = pInfo->VisualObject.cFrame->stepCr;
  73. mbPerRow = pInfo->VisualObject.VideoObject.MacroBlockPerRow;
  74. mbPerCol = pInfo->VisualObject.VideoObject.MacroBlockPerCol;
  75. stepFc[0] = stepFc[1] = stepFc[2] = stepFc[3] = stepYc; stepFc[4] = stepCbc; stepFc[5] = stepCrc;
  76. pFc[0] = pInfo->VisualObject.cFrame->pY; pFc[1] = pInfo->VisualObject.cFrame->pY + 8;
  77. pFc[2] = pInfo->VisualObject.cFrame->pY + 8 * stepYc; pFc[3] = pInfo->VisualObject.cFrame->pY + 8 * stepYc + 8;
  78. pFc[4] = pInfo->VisualObject.cFrame->pCb; pFc[5] = pInfo->VisualObject.cFrame->pCr;
  79. nmb = pInfo->VisualObject.VideoObject.MacroBlockPerVOP;
  80. mbCurr = colNum = rowNum = 0;
  81. sts = MP4_STATUS_OK;
  82. // decode short_video_header I-VOP
  83. if (pInfo->VisualObject.VideoObject.short_video_header)
  84. {
  85. quant = pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.vop_quant;
  86. nmbgob = 0;
  87. pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.gob_number = 0;
  88. for (;;)
  89. {
  90. do
  91. {
  92. if (mp4_DecodeMCBPC_I(pInfo, &mb_type, &cbpc) != MP4_STATUS_OK)
  93. goto Err_1;
  94. } while (mb_type == IPPVC_MB_STUFFING);
  95. if (mp4_DecodeCBPY_I(pInfo, &cbpy) != MP4_STATUS_OK)
  96. goto Err_1;
  97. if (mb_type == IPPVC_MBTYPE_INTRA_Q)
  98. mp4_UpdateQuant(pInfo, quant);
  99. if (mp4_DecodeIntraMB_SVH(pInfo, (cbpy << 2) + cbpc, quant, pFc, stepFc) != MP4_STATUS_OK)
  100. goto Err_1;
  101. colNum ++;
  102. if (colNum == mbPerRow) {
  103. colNum = 0;
  104. rowNum ++;
  105. if (rowNum == mbPerCol)
  106. break;
  107. pFc[0] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  108. pFc[1] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  109. pFc[2] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  110. pFc[3] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  111. pFc[4] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbc << 3) - stepCbc;
  112. pFc[5] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrc << 3) - stepCrc;
  113. }
  114. else
  115. {
  116. pFc[0] += 16; pFc[1] += 16; pFc[2] += 16; pFc[3] += 16; pFc[4] += 8; pFc[5] += 8;
  117. }
  118. nmbgob ++;
  119. if (nmbgob == pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.num_macroblocks_in_gob && pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.gob_number < (pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.num_gobs_in_vop - 1)) {
  120. ErrRet_1:
  121. if (mp4_CheckDecodeGOB_SVH(pInfo) != MP4_STATUS_OK)
  122. goto Err_1;
  123. if (!pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.gob_header_empty)
  124. {
  125. quant = pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.quant_scale;
  126. i = pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.gob_number * pInfo->VisualObject.VideoObject.VideoObjectPlaneH263.num_rows_in_gob;
  127. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, (i - rowNum) * mbPerRow - colNum);
  128. rowNum = i;
  129. colNum = 0;
  130. pFc[0] = pInfo->VisualObject.cFrame->pY + i * stepYc * 16; pFc[1] = pFc[0] + 8; pFc[2] = pFc[0] + 8 * stepYc; pFc[3] = pFc[2] + 8;
  131. pFc[4] = pInfo->VisualObject.cFrame->pCb + i * stepCbc * 8; pFc[5] = pInfo->VisualObject.cFrame->pCr + i * stepCrc * 8;
  132. }
  133. nmbgob = 0;
  134. }
  135. }
  136. mp4_AlignBits(pInfo);
  137. return sts;
  138. Err_1:
  139. sts = MP4_STATUS_ERROR;
  140. if (pInfo->stopOnErr)
  141. return sts;
  142. if (!mp4_SeekGOBMarker(pInfo))
  143. {
  144. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, pInfo->VisualObject.VideoObject.MacroBlockPerVOP - rowNum * mbPerRow - colNum);
  145. return sts;
  146. }
  147. goto ErrRet_1;
  148. }
  149. quant = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant;
  150. if (pInfo->VisualObject.VideoObject.sprite_enable != MP4_SPRITE_STATIC)
  151. ippsZero_8u((uint8_t*)pInfo->VisualObject.VideoObject.MBinfo, nmb * sizeof(mp4_MacroBlock));
  152. // decode data_partitioned I-VOP
  153. if (pInfo->VisualObject.VideoObject.data_partitioned)
  154. {
  155. mp4_DataPartMacroBlock *pMBdp;
  156. for (;;)
  157. {
  158. // reset Intra prediction buffer on new Video_packet
  159. mp4_ResetIntraPredBuffer(pInfo);
  160. mbInVideoPacket = 0;
  161. pMBdp = &pInfo->VisualObject.VideoObject.DataPartBuff[mbCurr];
  162. // decode mb_type/cbpc/dquant/DC part
  163. for (;;)
  164. {
  165. if (mp4_DecodeMCBPC_I(pInfo, &mb_type, &cbpc) != MP4_STATUS_OK)
  166. goto Err_2;
  167. if (mb_type != IPPVC_MB_STUFFING)
  168. {
  169. if (mbInVideoPacket == nmb - mbCurr)
  170. {
  171. mp4_Error("DC Marker missed");
  172. goto Err_2;
  173. }
  174. quantPred = quant;
  175. if (mb_type == IPPVC_MBTYPE_INTRA_Q)
  176. mp4_UpdateQuant(pInfo, quant);
  177. if (mbInVideoPacket == 0)
  178. quantPred = quant;
  179. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  180. if (dcVLC) {
  181. for (i = 0; i < 6; i ++) {
  182. if (ippiDecodeDCIntra_MPEG4_1u16s(&pInfo->bufptr, &pInfo->bitoff, &pMBdp->dct_dc[i], (i < 4) ? IPPVC_BLOCK_LUMA : IPPVC_BLOCK_CHROMA) != ippStsNoErr)
  183. goto Err_2;
  184. }
  185. }
  186. pMBdp->quant = (uint8_t)quant;
  187. pMBdp->type = (uint8_t)mb_type;
  188. pMBdp->pat = (uint8_t)cbpc;
  189. pMBdp ++;
  190. mbInVideoPacket ++;
  191. }
  192. if (mp4_ShowBits(pInfo, 19) == MP4_DC_MARKER)
  193. {
  194. mp4_GetBits(pInfo, 19);
  195. break;
  196. }
  197. }
  198. pMBdp = &pInfo->VisualObject.VideoObject.DataPartBuff[mbCurr];
  199. // decode ac_pred_flag/cbpy part
  200. for (i = 0; i < mbInVideoPacket; i ++)
  201. {
  202. pMBdp[i].ac_pred_flag = (uint8_t)mp4_GetBit(pInfo);
  203. if (mp4_DecodeCBPY_I(pInfo, &cbpy) != MP4_STATUS_OK)
  204. {
  205. if (pInfo->stopOnErr)
  206. goto Err_2;
  207. for (j = i + 1; j < mbInVideoPacket; j ++)
  208. pMBdp[j].ac_pred_flag = 1;
  209. break;
  210. }
  211. pMBdp[i].pat = (uint8_t)((cbpy << 2) + pMBdp[i].pat);
  212. }
  213. // decode AC part and reconstruct macroblocks
  214. for (i = 0; i < mbInVideoPacket; i ++) {
  215. if (colNum == 0) {
  216. // reset B-prediction blocks on new row
  217. mp4_ResetIntraPredBblock(pInfo);
  218. }
  219. quant = pMBdp[i].quant;
  220. quantPred = (i == 0) ? quant : pMBdp[i-1].quant;
  221. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  222. ac_pred_flag = pMBdp[i].ac_pred_flag;
  223. if (mp4_DecodeIntraMB_DP(pInfo, pMBdp[i].dct_dc, colNum, pMBdp[i].pat, quant, dcVLC, ac_pred_flag, pFc, stepFc) != MP4_STATUS_OK)
  224. //if (!pInfo->VisualObject.VideoObject.reversible_vlc)
  225. goto Err_2;
  226. //else
  227. // goto Err_RVLC;
  228. colNum ++;
  229. if (colNum == mbPerRow)
  230. {
  231. colNum = 0;
  232. rowNum ++;
  233. if (rowNum == mbPerCol)
  234. return sts;
  235. pFc[0] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  236. pFc[1] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  237. pFc[2] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  238. pFc[3] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  239. pFc[4] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbc << 3) - stepCbc;
  240. pFc[5] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrc << 3) - stepCrc;
  241. }
  242. else
  243. {
  244. pFc[0] += 16; pFc[1] += 16; pFc[2] += 16; pFc[3] += 16; pFc[4] += 8; pFc[5] += 8;
  245. }
  246. }
  247. mbCurr += mbInVideoPacket;
  248. if (!pInfo->VisualObject.VideoObject.resync_marker_disable)
  249. {
  250. int32_t found;
  251. ErrRet_2:
  252. if (mp4_CheckDecodeVideoPacket(pInfo, &found) == MP4_STATUS_OK)
  253. {
  254. if (found)
  255. {
  256. quant = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant_scale;
  257. mbCurr = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num;
  258. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, mbCurr - rowNum * mbPerRow - colNum);
  259. rowNum = mbCurr / mbPerRow;
  260. colNum = mbCurr % mbPerRow;
  261. pFc[0] = pInfo->VisualObject.cFrame->pY + (rowNum * stepYc + colNum) * 16; pFc[1] = pFc[0] + 8; pFc[2] = pFc[0] + stepYc * 8; pFc[3] = pFc[2] + 8;
  262. pFc[4] = pInfo->VisualObject.cFrame->pCb + (rowNum * stepCbc + colNum) * 8; pFc[5] = pInfo->VisualObject.cFrame->pCr + (rowNum * stepCrc + colNum) * 8;
  263. }
  264. else
  265. goto Err_2;
  266. }
  267. else
  268. goto Err_2;
  269. }
  270. }
  271. Err_2:
  272. sts = MP4_STATUS_ERROR;
  273. if (pInfo->stopOnErr)
  274. return sts;
  275. if (pInfo->VisualObject.VideoObject.resync_marker_disable || !mp4_SeekResyncMarker(pInfo))
  276. {
  277. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, pInfo->VisualObject.VideoObject.MacroBlockPerVOP - rowNum * mbPerRow - colNum);
  278. return sts;
  279. }
  280. goto ErrRet_2;
  281. #if 0
  282. Err_RVLC:
  283. {
  284. uint8_t *sBufptr = pInfo->bufptr;
  285. int32_t sBitoff = pInfo->bitoff;
  286. pInfo->bitoff --;
  287. if (pInfo->bitoff == -1) {
  288. pInfo->bitoff = 7;
  289. pInfo->bufptr --;
  290. }
  291. // decode AC part and reconstruct macroblocks
  292. for (j = mbInVideoPacket - 1; j >= i; j --) {
  293. int32_t lnz, quantPred, dcVLC;
  294. quantPred = ((j == 0) ? pMBdp[j].quant : pMBdp[j-1].quant);
  295. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  296. if (ippiDecodeCoeffsIntraRVLCBack_MPEG4_1u16s(&pInfo->bufptr, &pInfo->bitoff, pDCTdp[j*64*6], &lnz, dcVLC) != ippStsNoErr)
  297. break;
  298. }
  299. pInfo->bufptr = sBufptr;
  300. pInfo->bitoff = sBitoff;
  301. }
  302. goto ErrRet_2;
  303. #endif
  304. }
  305. // decode not data partitioned I-VOP
  306. else {
  307. int32_t stepY = stepYc, dct_type = 0, pYoff23 = 8 * stepYc;
  308. for (;;) {
  309. // reset Intra prediction buffer on new Video_packet
  310. mp4_ResetIntraPredBuffer(pInfo);
  311. mbInVideoPacket = 0;
  312. // decode blocks
  313. for (;;) {
  314. if (colNum == 0) {
  315. // reset B-prediction blocks on new row
  316. mp4_ResetIntraPredBblock(pInfo);
  317. }
  318. if (mp4_DecodeMCBPC_I(pInfo, &mb_type, &cbpc) != MP4_STATUS_OK)
  319. goto Err_3;
  320. if (mb_type != IPPVC_MB_STUFFING) {
  321. ac_pred_flag = mp4_GetBit(pInfo);
  322. if (mp4_DecodeCBPY_I(pInfo, &cbpy) != MP4_STATUS_OK)
  323. goto Err_3;
  324. quantPred = quant;
  325. if (mb_type == IPPVC_MBTYPE_INTRA_Q)
  326. mp4_UpdateQuant(pInfo, quant);
  327. if (mbInVideoPacket == 0)
  328. quantPred = quant;
  329. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  330. if (pInfo->VisualObject.VideoObject.interlaced) {
  331. dct_type = mp4_GetBit(pInfo);
  332. if (dct_type) {
  333. stepY = stepYc * 2;
  334. pYoff23 = stepYc;
  335. } else {
  336. stepY = stepYc;
  337. pYoff23 = 8 * stepYc;
  338. }
  339. stepFc[0] = stepFc[1] = stepFc[2] = stepFc[3] = stepY;
  340. }
  341. pFc[2] = pFc[0] + pYoff23; pFc[3] = pFc[1] + pYoff23;
  342. if (mp4_DecodeIntraMB(pInfo, colNum, (cbpy << 2) + cbpc, quant, dcVLC, ac_pred_flag, pFc, stepFc) != MP4_STATUS_OK)
  343. goto Err_3;
  344. mbInVideoPacket ++;
  345. colNum ++;
  346. if (colNum == mbPerRow) {
  347. colNum = 0;
  348. rowNum ++;
  349. if (rowNum == mbPerCol) {
  350. // skip stuffing
  351. while (mp4_ShowBits9(pInfo, 9) == 1)
  352. mp4_FlushBits(pInfo, 9);
  353. return sts;
  354. }
  355. pFc[0] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  356. pFc[1] += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  357. pFc[4] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbc << 3) - stepCbc;
  358. pFc[5] += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrc << 3) - stepCrc;
  359. } else {
  360. pFc[0] += 16; pFc[1] += 16; pFc[4] += 8; pFc[5] += 8;
  361. }
  362. }
  363. if (!pInfo->VisualObject.VideoObject.resync_marker_disable)
  364. {
  365. int32_t found;
  366. ErrRet_3:
  367. if (mp4_CheckDecodeVideoPacket(pInfo, &found) == MP4_STATUS_OK)
  368. {
  369. if (found)
  370. {
  371. quant = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant_scale;
  372. mbCurr = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num;
  373. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, mbCurr - rowNum * mbPerRow - colNum);
  374. rowNum = mbCurr / mbPerRow;
  375. colNum = mbCurr % mbPerRow;
  376. pFc[0] = pInfo->VisualObject.cFrame->pY + (rowNum * stepYc + colNum) * 16; pFc[1] = pFc[0] + 8;
  377. pFc[4] = pInfo->VisualObject.cFrame->pCb + (rowNum * stepCbc + colNum) * 8; pFc[5] = pInfo->VisualObject.cFrame->pCr + (rowNum * stepCrc + colNum) * 8;
  378. break;
  379. }
  380. }
  381. else
  382. goto Err_3;
  383. }
  384. }
  385. }
  386. Err_3:
  387. sts = MP4_STATUS_ERROR;
  388. if (pInfo->stopOnErr)
  389. return sts;
  390. if (pInfo->VisualObject.VideoObject.resync_marker_disable || !mp4_SeekResyncMarker(pInfo))
  391. {
  392. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, pInfo->VisualObject.VideoObject.MacroBlockPerVOP - rowNum * mbPerRow - colNum);
  393. return sts;
  394. }
  395. goto ErrRet_3;
  396. }
  397. }