1
0

mp4decvops.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 S-VOPs
  10. //
  11. */
  12. #include "mp4def.h"
  13. #include "mp4dec.h"
  14. /*
  15. // decode MPEG-4 SVOP
  16. */
  17. mp4_Status mp4_DecodeVOP_S(mp4_Info* pInfo)
  18. {
  19. __ALIGN16(int16_t, coeffMB, 64*6);
  20. int32_t quant, quantPred, i, j, dcVLC, nmb, dx, dy, pat;
  21. int32_t stepYr, stepYc, stepCbr, stepCbc, stepCrr, stepCrc, mbPerRow, mbPerCol;
  22. int32_t mbCurr, mbInVideoPacket, colNum, rowNum, stepF[6];
  23. uint8_t *pYc, *pCbc, *pCrc, *pYr, *pCbr, *pCrr, *pF[6];
  24. int32_t mb_not_coded, mb_type, cbpc, cbpy, ac_pred_flag, cbpyPrev, mcsel;
  25. int32_t scan, obmc_disable, rt, quarter_sample, interlaced, fcode_forward;
  26. IppiRect limitRectL, limitRectC;
  27. IppMotionVector mvCur[4], mvPrev[4], mvTmp[4], mvCbCr;
  28. mp4_MacroBlock *pMBinfo;
  29. IppiRect spriteRect, vopRect;
  30. mp4_Status sts;
  31. if (!pInfo->VisualObject.cFrame)
  32. pInfo->VisualObject.cFrame = CreateFrame(&pInfo->VisualObject);
  33. mbPerRow = pInfo->VisualObject.VideoObject.MacroBlockPerRow;
  34. mbPerCol = pInfo->VisualObject.VideoObject.MacroBlockPerCol;
  35. stepYc = pInfo->VisualObject.cFrame->stepY;
  36. stepYr = pInfo->VisualObject.rFrame->stepY;
  37. stepCbc = pInfo->VisualObject.cFrame->stepCb;
  38. stepCbr = pInfo->VisualObject.rFrame->stepCb;
  39. stepCrc = pInfo->VisualObject.cFrame->stepCr;
  40. stepCrr = pInfo->VisualObject.rFrame->stepCr;
  41. pYc = pInfo->VisualObject.cFrame->pY;
  42. pCbc = pInfo->VisualObject.cFrame->pCb;
  43. pCrc = pInfo->VisualObject.cFrame->pCr;
  44. pYr = pInfo->VisualObject.rFrame->pY;
  45. pCbr = pInfo->VisualObject.rFrame->pCb;
  46. pCrr = pInfo->VisualObject.rFrame->pCr;
  47. stepF[0] = stepF[1] = stepF[2] = stepF[3] = stepYc; stepF[4] = stepCbc; stepF[5] = stepCrc;
  48. // Bounding rectangle for MV limitation
  49. limitRectL.x = - 16 * MP4_NUM_EXT_MB;
  50. limitRectL.y = - 16 * MP4_NUM_EXT_MB;
  51. limitRectL.width = pInfo->VisualObject.VideoObject.width + 16 * 2 * MP4_NUM_EXT_MB;
  52. limitRectL.height = pInfo->VisualObject.VideoObject.height + 16 * 2 * MP4_NUM_EXT_MB;
  53. limitRectC.x = -8 * MP4_NUM_EXT_MB;
  54. limitRectC.y = -8 * MP4_NUM_EXT_MB;
  55. limitRectC.width = (pInfo->VisualObject.VideoObject.width >> 1) + 8 * 2 * MP4_NUM_EXT_MB;
  56. limitRectC.height = (pInfo->VisualObject.VideoObject.height >> 1) + 8 * 2 * MP4_NUM_EXT_MB;
  57. pMBinfo = pInfo->VisualObject.VideoObject.MBinfo;
  58. rt = pInfo->VisualObject.VideoObject.VideoObjectPlane.rounding_type;
  59. quarter_sample = pInfo->VisualObject.VideoObject.quarter_sample;
  60. obmc_disable = pInfo->VisualObject.VideoObject.obmc_disable;
  61. interlaced = pInfo->VisualObject.VideoObject.interlaced;
  62. scan = pInfo->VisualObject.VideoObject.VideoObjectPlane.alternate_vertical_scan_flag ? IPPVC_SCAN_VERTICAL : IPPVC_SCAN_ZIGZAG;
  63. nmb = pInfo->VisualObject.VideoObject.MacroBlockPerVOP;
  64. quant = quantPred = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant;
  65. mbCurr = colNum = rowNum = 0;
  66. cbpc = 0;
  67. // init WarpSpec for Sprites or GMC
  68. vopRect.x = 0;
  69. vopRect.y = 0;
  70. vopRect.width = pInfo->VisualObject.VideoObject.width;
  71. vopRect.height = pInfo->VisualObject.VideoObject.height;
  72. if (pInfo->VisualObject.VideoObject.sprite_enable == MP4_SPRITE_STATIC) {
  73. spriteRect.x = pInfo->VisualObject.VideoObject.sprite_left_coordinate;
  74. spriteRect.y = pInfo->VisualObject.VideoObject.sprite_top_coordinate;
  75. spriteRect.width = pInfo->VisualObject.VideoObject.sprite_width;
  76. spriteRect.height = pInfo->VisualObject.VideoObject.sprite_height;
  77. fcode_forward = 1;
  78. } else {
  79. spriteRect = vopRect; // for shapes they may be different !!!!!!
  80. fcode_forward = pInfo->VisualObject.VideoObject.VideoObjectPlane.fcode_forward;
  81. }
  82. ippiWarpInit_MPEG4(pInfo->VisualObject.VideoObject.WarpSpec,
  83. pInfo->VisualObject.VideoObject.VideoObjectPlane.warping_mv_code_du,
  84. pInfo->VisualObject.VideoObject.VideoObjectPlane.warping_mv_code_dv,
  85. pInfo->VisualObject.VideoObject.sprite_warping_points,
  86. pInfo->VisualObject.VideoObject.sprite_enable,
  87. pInfo->VisualObject.VideoObject.sprite_warping_accuracy,
  88. rt, quarter_sample, fcode_forward,
  89. &spriteRect, &vopRect);
  90. // decode basic sprites
  91. if (pInfo->VisualObject.VideoObject.sprite_enable == MP4_SPRITE_STATIC)
  92. {
  93. //if (pInfo->VisualObject.VideoObject.shape != MP4_SHAPE_TYPE_RECTANGULAR) {
  94. // if (mp4_InitVOPShape(pInfo) != MP4_STATUS_OK)
  95. // return MP4_STATUS_ERROR;
  96. //}
  97. ippiWarpLuma_MPEG4_8u_C1R(pInfo->VisualObject.sFrame->pY, pInfo->VisualObject.sFrame->stepY,
  98. pYc, stepYc, &vopRect, pInfo->VisualObject.VideoObject.WarpSpec);
  99. if (pInfo->VisualObject.VideoObject.sprite_brightness_change)
  100. ippiChangeSpriteBrightness_MPEG4_8u_C1IR(pYc, stepYc, vopRect.width, vopRect.height, pInfo->VisualObject.VideoObject.VideoObjectPlane.brightness_change_factor);
  101. vopRect.width >>= 1; vopRect.height >>= 1;
  102. ippiWarpChroma_MPEG4_8u_P2R(pInfo->VisualObject.sFrame->pCb, pInfo->VisualObject.sFrame->stepCb,
  103. pInfo->VisualObject.sFrame->pCr, pInfo->VisualObject.sFrame->stepCr,
  104. pCbc, stepCbc, pCrc, stepCrc, &vopRect, pInfo->VisualObject.VideoObject.WarpSpec);
  105. return MP4_STATUS_OK;
  106. }
  107. sts = MP4_STATUS_OK;
  108. // decode data_partitioned S(GMC)-VOP
  109. if (pInfo->VisualObject.VideoObject.data_partitioned) {
  110. for (;;) {
  111. mp4_DataPartMacroBlock *pMBdp;
  112. int32_t x, y;
  113. x = colNum;
  114. y = rowNum;
  115. // reset Intra prediction buffer on new Video_packet
  116. mp4_ResetIntraPredBuffer(pInfo);
  117. pMBdp = &pInfo->VisualObject.VideoObject.DataPartBuff[mbCurr];
  118. pMBinfo = &pInfo->VisualObject.VideoObject.MBinfo[mbCurr];
  119. mbInVideoPacket = 0;
  120. // decode not_coded/mcsel/mb_type/cbpc/MV part
  121. for (;;) {
  122. mb_not_coded = mp4_GetBit(pInfo);
  123. if (mb_not_coded) {
  124. mb_type = IPPVC_MBTYPE_INTER;
  125. mp4_StatisticInc_(&pInfo->VisualObject.Statistic.nMB_NOTCODED);
  126. } else {
  127. if (mp4_DecodeMCBPC_P(pInfo, &mb_type, &cbpc, 1) != MP4_STATUS_OK)
  128. goto Err_2;
  129. }
  130. if (mb_type != IPPVC_MB_STUFFING) {
  131. if (mbInVideoPacket == nmb - mbCurr) {
  132. mp4_Error("DC Marker missed");
  133. goto Err_2;
  134. }
  135. pMBinfo->validPred = 1;
  136. if (mb_type < IPPVC_MBTYPE_INTER4V && !mb_not_coded)
  137. mcsel = mp4_GetBit(pInfo);
  138. else
  139. mcsel = mb_not_coded;
  140. if (!mcsel) {
  141. if (mb_type <= IPPVC_MBTYPE_INTER4V) {
  142. if (mb_type != IPPVC_MBTYPE_INTER4V) {
  143. if (mp4_PredictDecode1MV(pInfo, pMBinfo, y, x) != MP4_STATUS_OK)
  144. goto Err_2;
  145. pMBinfo->mv[1] = pMBinfo->mv[2] = pMBinfo->mv[3] = pMBinfo->mv[0];
  146. } else {
  147. if (mp4_PredictDecode4MV(pInfo, pMBinfo, y, x) != MP4_STATUS_OK)
  148. goto Err_2;
  149. }
  150. } else {
  151. mp4_Zero4MV(pMBinfo->mv);
  152. }
  153. } else {
  154. ippiCalcGlobalMV_MPEG4(x << 4, y << 4, pMBinfo->mv, pInfo->VisualObject.VideoObject.WarpSpec);
  155. pMBinfo->mv[1] = pMBinfo->mv[2] = pMBinfo->mv[3] = pMBinfo->mv[0];
  156. }
  157. pMBinfo->not_coded = (uint8_t)mb_not_coded;
  158. pMBinfo->type = (uint8_t)mb_type;
  159. pMBinfo ++;
  160. pMBdp->pat = (uint8_t)cbpc;
  161. pMBdp->mcsel = (uint8_t)mcsel;
  162. pMBdp ++;
  163. mbInVideoPacket ++;
  164. x ++;
  165. if (x == mbPerRow) {
  166. x = 0;
  167. y ++;
  168. }
  169. }
  170. if (mp4_ShowBits(pInfo, 17) == MP4_MV_MARKER) {
  171. mp4_GetBits(pInfo, 17);
  172. break;
  173. }
  174. }
  175. pMBdp = &pInfo->VisualObject.VideoObject.DataPartBuff[mbCurr];
  176. pMBinfo = &pInfo->VisualObject.VideoObject.MBinfo[mbCurr];
  177. // decode ac_pred_flag/cbpy/dquant/IntraDC part
  178. for (i = 0; i < mbInVideoPacket; i ++) {
  179. if (!pMBinfo->not_coded) {
  180. mb_type = pMBinfo->type;
  181. if (mb_type >= IPPVC_MBTYPE_INTRA)
  182. pMBdp->ac_pred_flag = (uint8_t)mp4_GetBit(pInfo);
  183. if (mp4_DecodeCBPY_P(pInfo, &cbpy, mb_type) != MP4_STATUS_OK)
  184. goto Err_2;
  185. pMBdp->pat = (uint8_t)((cbpy << 2) + pMBdp->pat);
  186. quantPred = quant;
  187. if (mb_type == IPPVC_MBTYPE_INTER_Q || mb_type == IPPVC_MBTYPE_INTRA_Q)
  188. mp4_UpdateQuant(pInfo, quant);
  189. pMBdp->quant = (uint8_t)quant;
  190. if (i == 0)
  191. quantPred = quant;
  192. // decode DC coefficient of Intra blocks
  193. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  194. if ((mb_type >= IPPVC_MBTYPE_INTRA) && dcVLC) {
  195. for (j = 0; j < 6; j ++) {
  196. if (ippiDecodeDCIntra_MPEG4_1u16s(&pInfo->bufptr, &pInfo->bitoff, &pMBdp->dct_dc[j], j < 4 ? IPPVC_BLOCK_LUMA : IPPVC_BLOCK_CHROMA) != ippStsNoErr)
  197. goto Err_2;
  198. }
  199. }
  200. } else
  201. pMBdp->pat = 0;
  202. pMBinfo->not_coded = 0; // for B-VOP all MB have MVs
  203. if (pMBdp->mcsel)
  204. pMBinfo->type = IPPVC_MBTYPE_INTRA; // for OBMC MVs
  205. pMBdp ++;
  206. pMBinfo ++;
  207. }
  208. if (mbCurr + mbInVideoPacket < nmb)
  209. pMBinfo->type = IPPVC_MBTYPE_INTRA; // for OBMC set first MB of the next videopacket as invalid for right MV
  210. pMBdp = &pInfo->VisualObject.VideoObject.DataPartBuff[mbCurr];
  211. pMBinfo = &pInfo->VisualObject.VideoObject.MBinfo[mbCurr];
  212. // decode coeffs and reconstruct macroblocks
  213. for (i = 0; i < mbInVideoPacket; i ++) {
  214. if (colNum == 0) {
  215. // reset B-prediction blocks on new row
  216. mp4_ResetIntraPredBblock(pInfo);
  217. }
  218. quant = pMBdp->quant;
  219. mb_type = pMBinfo->type;
  220. mcsel = pMBdp->mcsel;
  221. if (!mcsel && (mb_type >= IPPVC_MBTYPE_INTRA)) {
  222. quantPred = (i == 0) ? quant : pMBdp[-1].quant;
  223. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  224. pF[0] = pYc; pF[1] = pYc + 8; pF[2] = pYc + 8 * stepYc; pF[3] = pYc + 8 * stepYc + 8; pF[4] = pCbc; pF[5] = pCrc;
  225. if (mp4_DecodeIntraMB_DP(pInfo, pMBdp->dct_dc, colNum, pMBdp->pat, quant, dcVLC, pMBdp->ac_pred_flag, pF, stepF) != MP4_STATUS_OK)
  226. goto Err_2;
  227. } else {
  228. mp4_UpdateIntraPredBuffInvalid(pInfo, colNum);
  229. dx = colNum * 16;
  230. dy = rowNum * 16;
  231. pat = pMBdp->pat;
  232. cbpy = pat >> 2;
  233. cbpc = pat & 3;
  234. if (pat)
  235. if (mp4_DecodeInterMB(pInfo, coeffMB, quant, pat, scan) != MP4_STATUS_OK)
  236. goto Err_2;
  237. if (!mcsel) {
  238. if (mb_type == IPPVC_MBTYPE_INTER4V) {
  239. if (quarter_sample) {
  240. mp4_Limit4MVQ(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 8);
  241. mp4_ComputeChroma4MVQ(pMBinfo->mv, &mvCbCr);
  242. } else {
  243. mp4_Limit4MV(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 8);
  244. mp4_ComputeChroma4MV(pMBinfo->mv, &mvCbCr);
  245. }
  246. mp4_LimitMV(&mvCbCr, &mvCbCr, &limitRectC, dx >> 1, dy >> 1, 8);
  247. } else {
  248. if (quarter_sample) {
  249. mp4_LimitMVQ(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 16);
  250. mp4_ComputeChromaMVQ(mvCur, &mvCbCr);
  251. } else {
  252. mp4_LimitMV(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 16);
  253. mp4_ComputeChromaMV(mvCur, &mvCbCr);
  254. }
  255. mvCur[1] = mvCur[2] = mvCur[3] = mvCur[0];
  256. }
  257. if (obmc_disable) {
  258. if (quarter_sample) {
  259. if (mb_type == IPPVC_MBTYPE_INTER4V) {
  260. mp4_Copy8x8QP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  261. mp4_Copy8x8QP_8u(pYr+8, stepYr, pYc+8, stepYc, &mvCur[1], rt);
  262. mp4_Copy8x8QP_8u(pYr+8*stepYr, stepYr, pYc+8*stepYc, stepYc, &mvCur[2], rt);
  263. mp4_Copy8x8QP_8u(pYr+8*stepYr+8, stepYr, pYc+8*stepYc+8, stepYc, &mvCur[3], rt);
  264. } else
  265. mp4_Copy16x16QP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  266. } else {
  267. if (mb_type == IPPVC_MBTYPE_INTER4V) {
  268. mp4_Copy8x8HP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  269. mp4_Copy8x8HP_8u(pYr+8, stepYr, pYc+8, stepYc, &mvCur[1], rt);
  270. mp4_Copy8x8HP_8u(pYr+8*stepYr, stepYr, pYc+8*stepYc, stepYc, &mvCur[2], rt);
  271. mp4_Copy8x8HP_8u(pYr+8*stepYr+8, stepYr, pYc+8*stepYc+8, stepYc, &mvCur[3], rt);
  272. } else
  273. mp4_Copy16x16HP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  274. }
  275. mp4_AddResidual(cbpy & 8, pYc, stepYc, coeffMB);
  276. mp4_AddResidual(cbpy & 4, pYc+8, stepYc, coeffMB+64);
  277. mp4_AddResidual(cbpy & 2, pYc+stepYc*8, stepYc, coeffMB+128);
  278. mp4_AddResidual(cbpy & 1, pYc+stepYc*8+8, stepYc, coeffMB+192);
  279. } else {
  280. mp4_OBMC(pInfo, pMBinfo, mvCur, colNum, rowNum, limitRectL, pYc, stepYc, pYr, stepYr, cbpy, coeffMB, 0);
  281. }
  282. mp4_MC_HP(cbpc & 2, pCbr, stepCbr, pCbc, stepCbc, coeffMB+256, &mvCbCr, rt);
  283. mp4_MC_HP(cbpc & 1, pCrr, stepCrr, pCrc, stepCrc, coeffMB+320, &mvCbCr, rt);
  284. } else {
  285. IppiRect mbRect;
  286. mbRect.x = dx; mbRect.y = dy; mbRect.width = mbRect.height = 16;
  287. ippiWarpLuma_MPEG4_8u_C1R(pInfo->VisualObject.rFrame->pY, stepYr, pYc, stepYc, &mbRect, pInfo->VisualObject.VideoObject.WarpSpec);
  288. mbRect.x >>= 1; mbRect.y >>= 1; mbRect.width = mbRect.height = 8;
  289. ippiWarpChroma_MPEG4_8u_P2R(pInfo->VisualObject.rFrame->pCb, stepCbr, pInfo->VisualObject.rFrame->pCr, stepCrr, pCbc, stepCbc, pCrc, stepCrc, &mbRect, pInfo->VisualObject.VideoObject.WarpSpec);
  290. if (pat) {
  291. mp4_AddResidual(cbpy & 8, pYc, stepYc, coeffMB);
  292. mp4_AddResidual(cbpy & 4, pYc+8, stepYc, coeffMB+64);
  293. mp4_AddResidual(cbpy & 2, pYc+stepYc*8, stepYc, coeffMB+128);
  294. mp4_AddResidual(cbpy & 1, pYc+stepYc*8+8, stepYc, coeffMB+192);
  295. mp4_AddResidual(cbpc & 2, pCbc, stepCbc, coeffMB+256);
  296. mp4_AddResidual(cbpc & 1, pCrc, stepCrc, coeffMB+320);
  297. }
  298. }
  299. }
  300. pMBinfo ++;
  301. pMBdp ++;
  302. colNum ++;
  303. if (colNum == mbPerRow) {
  304. colNum = 0;
  305. rowNum ++;
  306. if (rowNum == mbPerCol)
  307. return sts;
  308. pYc += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  309. pCbc += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbc << 3) - stepCbc;
  310. pCrc += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrc << 3) - stepCrc;
  311. pYr += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYr << 4) - stepYr;
  312. pCbr += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbr << 3) - stepCbr;
  313. pCrr += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrr << 3) - stepCrr;
  314. }
  315. else
  316. {
  317. pYc += 16; pCrc += 8; pCbc += 8;
  318. pYr += 16; pCrr += 8; pCbr += 8;
  319. }
  320. }
  321. //mbCurr += mbInVideoPacket;
  322. if (!pInfo->VisualObject.VideoObject.resync_marker_disable)
  323. {
  324. int32_t found;
  325. ErrRet_2:
  326. if (mp4_CheckDecodeVideoPacket(pInfo, &found) == MP4_STATUS_OK)
  327. {
  328. if (found)
  329. {
  330. quant = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant_scale;
  331. mbInVideoPacket = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num - mbCurr;
  332. mbCurr = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num;
  333. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, mbCurr - rowNum * mbPerRow - colNum);
  334. rowNum = mbCurr / mbPerRow;
  335. colNum = mbCurr % mbPerRow;
  336. pYc = pInfo->VisualObject.cFrame->pY + (rowNum * stepYc + colNum) * 16; pCbc = pInfo->VisualObject.cFrame->pCb + (rowNum * stepCbc + colNum) * 8; pCrc = pInfo->VisualObject.cFrame->pCr + (rowNum * stepCrc + colNum) * 8;
  337. pYr = pInfo->VisualObject.rFrame->pY + (rowNum * stepYr + colNum) * 16; pCbr = pInfo->VisualObject.rFrame->pCb + (rowNum * stepCbr + colNum) * 8; pCrr = pInfo->VisualObject.rFrame->pCr + (rowNum * stepCrr + colNum) * 8;
  338. pMBinfo = pInfo->VisualObject.VideoObject.MBinfo + mbCurr;
  339. } else
  340. goto Err_2;
  341. } else
  342. goto Err_2;
  343. }
  344. // mark MBs the previous videopacket as invalid for prediction
  345. for (i = 1; i <= IPP_MIN(mbInVideoPacket, mbPerRow + 1); i ++)
  346. pMBinfo[-i].validPred = 0;
  347. }
  348. Err_2:
  349. sts = MP4_STATUS_ERROR;
  350. if (pInfo->stopOnErr)
  351. return sts;
  352. if (pInfo->VisualObject.VideoObject.resync_marker_disable || !mp4_SeekResyncMarker(pInfo))
  353. {
  354. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, pInfo->VisualObject.VideoObject.MacroBlockPerVOP - rowNum * mbPerRow - colNum);
  355. return sts;
  356. }
  357. goto ErrRet_2;
  358. }
  359. // decode not data_partitioned S-VOP
  360. {
  361. int32_t stepY, pYoff23, field_prediction, dct_type, dct_typePrev, mb_ftfr, mb_fbfr;
  362. IppMotionVector mvCbCrT, mvCbCrB, *mvField = pInfo->VisualObject.VideoObject.FieldMV;
  363. // warning "variable may be used without having been initialized"
  364. dx = dy = ac_pred_flag = cbpy = cbpyPrev = field_prediction = dct_type = dct_typePrev = mb_ftfr = mb_fbfr = 0;
  365. mvCbCr.dx = mvCbCr.dy = mvCbCrT.dx = mvCbCrT.dy = mvCbCrB.dx = mvCbCrB.dy = 0;
  366. for (;;) {
  367. // reset Intra prediction buffer on new Video_packet
  368. mp4_ResetIntraPredBuffer(pInfo);
  369. mbInVideoPacket = 0;
  370. // decode blocks
  371. for (;;) {
  372. if (colNum == 0) {
  373. // reset B-prediction blocks on new row
  374. mp4_ResetIntraPredBblock(pInfo);
  375. }
  376. mb_not_coded = mp4_GetBit(pInfo);
  377. mb_type = IPPVC_MBTYPE_INTER;
  378. if (!mb_not_coded) {
  379. if (mp4_DecodeMCBPC_P(pInfo, &mb_type, &cbpc, 1) != MP4_STATUS_OK)
  380. goto Err_3;
  381. }
  382. if (mb_type != IPPVC_MB_STUFFING) {
  383. if (!mb_not_coded) {
  384. mcsel = (mb_type < IPPVC_MBTYPE_INTER4V) ? mp4_GetBit(pInfo) : 0;
  385. if (mb_type >= IPPVC_MBTYPE_INTRA)
  386. ac_pred_flag = mp4_GetBit(pInfo);
  387. if (mp4_DecodeCBPY_P(pInfo, &cbpy, mb_type) != MP4_STATUS_OK)
  388. goto Err_3;
  389. quantPred = quant;
  390. if (mb_type == IPPVC_MBTYPE_INTER_Q || mb_type == IPPVC_MBTYPE_INTRA_Q)
  391. mp4_UpdateQuant(pInfo, quant);
  392. if (interlaced) {
  393. dct_type = 0;
  394. field_prediction = 0;
  395. if (mb_type >= IPPVC_MBTYPE_INTRA || (cbpy + cbpc) != 0)
  396. dct_type = mp4_GetBit(pInfo);
  397. if ((mb_type == IPPVC_MBTYPE_INTER || mb_type == IPPVC_MBTYPE_INTER_Q) && !mcsel) {
  398. field_prediction = mp4_GetBit(pInfo);
  399. if (field_prediction) {
  400. mb_ftfr = mp4_GetBit(pInfo);
  401. mb_fbfr = mp4_GetBit(pInfo);
  402. }
  403. }
  404. }
  405. } else {
  406. mp4_StatisticInc_(&pInfo->VisualObject.Statistic.nMB_NOTCODED);
  407. mcsel = 1;
  408. field_prediction = 0;
  409. }
  410. pMBinfo->validPred = 1;
  411. pMBinfo->not_coded = 0; // for B-VOP all MB have MVs
  412. pMBinfo->type = (uint8_t)(mcsel ? IPPVC_MBTYPE_INTRA : mb_type); // for OBMC MVs
  413. pMBinfo->field_info = (uint8_t)(field_prediction + (mb_ftfr << 1) + (mb_fbfr << 2));
  414. if (mb_type >= IPPVC_MBTYPE_INTRA) {
  415. if (mbInVideoPacket == 0)
  416. quantPred = quant;
  417. dcVLC = (quantPred < mp4_DC_vlc_Threshold[pInfo->VisualObject.VideoObject.VideoObjectPlane.intra_dc_vlc_thr]) ? 1 : 0;
  418. if (dct_type) {
  419. stepY = stepYc * 2;
  420. pYoff23 = stepYc;
  421. } else {
  422. stepY = stepYc;
  423. pYoff23 = 8 * stepYc;
  424. }
  425. stepF[0] = stepF[1] = stepF[2] = stepF[3] = stepY;
  426. pF[0] = pYc; pF[1] = pYc + 8; pF[2] = pYc + pYoff23; pF[3] = pYc + pYoff23 + 8; pF[4] = pCbc; pF[5] = pCrc;
  427. if (mp4_DecodeIntraMB(pInfo, colNum, (cbpy << 2) + cbpc, quant, dcVLC, ac_pred_flag, pF, stepF) != MP4_STATUS_OK)
  428. goto Err_3;
  429. mp4_Zero4MV(pMBinfo->mv);
  430. } else {
  431. mp4_UpdateIntraPredBuffInvalid(pInfo, colNum);
  432. dx = colNum * 16;
  433. dy = rowNum * 16;
  434. if (!mcsel) {
  435. if (!field_prediction) {
  436. if (mb_type != IPPVC_MBTYPE_INTER4V) {
  437. if (mp4_PredictDecode1MV(pInfo, pMBinfo, rowNum, colNum) != MP4_STATUS_OK)
  438. goto Err_3;
  439. pMBinfo->mv[1] = pMBinfo->mv[2] = pMBinfo->mv[3] = pMBinfo->mv[0];
  440. if (quarter_sample) {
  441. mp4_LimitMVQ(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 16);
  442. mp4_ComputeChromaMVQ(mvCur, &mvCbCr);
  443. } else {
  444. mp4_LimitMV(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 16);
  445. mp4_ComputeChromaMV(mvCur, &mvCbCr);
  446. }
  447. mvCur[1] = mvCur[2] = mvCur[3] = mvCur[0];
  448. } else {
  449. if (mp4_PredictDecode4MV(pInfo, pMBinfo, rowNum, colNum) != MP4_STATUS_OK)
  450. goto Err_3;
  451. if (quarter_sample) {
  452. mp4_Limit4MVQ(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 8);
  453. mp4_ComputeChroma4MVQ(pMBinfo->mv, &mvCbCr);
  454. } else {
  455. mp4_Limit4MV(pMBinfo->mv, mvCur, &limitRectL, dx, dy, 8);
  456. mp4_ComputeChroma4MV(pMBinfo->mv, &mvCbCr);
  457. }
  458. mp4_LimitMV(&mvCbCr, &mvCbCr, &limitRectC, dx >> 1, dy >> 1, 8);
  459. }
  460. } else {
  461. IppMotionVector mvFT, mvFB;
  462. if (mp4_PredictDecodeFMV(pInfo, pMBinfo, rowNum, colNum, &mvFT, &mvFB) != MP4_STATUS_OK)
  463. goto Err_3;
  464. mvField[0] = mvFT; mvField[1] = mvFB;
  465. if (quarter_sample) {
  466. mp4_LimitFMVQ(&mvFT, &mvCur[0], &limitRectL, dx, dy, 16);
  467. mp4_LimitFMVQ(&mvFB, &mvCur[2], &limitRectL, dx, dy, 16);
  468. mvTmp[0].dx = (int16_t)mp4_Div2(mvCur[0].dx);
  469. mvTmp[0].dy = (int16_t)(mp4_Div2(mvCur[0].dy << 1) >> 1);
  470. mvTmp[2].dx = (int16_t)mp4_Div2(mvCur[2].dx);
  471. mvTmp[2].dy = (int16_t)(mp4_Div2(mvCur[2].dy << 1) >> 1);
  472. mp4_ComputeChromaMV(&mvTmp[0], &mvCbCrT);
  473. mp4_ComputeChromaMV(&mvTmp[2], &mvCbCrB);
  474. } else {
  475. mp4_LimitFMV(&mvFT, &mvCur[0], &limitRectL, dx, dy, 16);
  476. mp4_LimitFMV(&mvFB, &mvCur[2], &limitRectL, dx, dy, 16);
  477. mp4_ComputeChromaMV(&mvCur[0], &mvCbCrT);
  478. mp4_ComputeChromaMV(&mvCur[2], &mvCbCrB);
  479. }
  480. }
  481. } else {
  482. ippiCalcGlobalMV_MPEG4(dx, dy, pMBinfo->mv, pInfo->VisualObject.VideoObject.WarpSpec);
  483. pMBinfo->mv[1] = pMBinfo->mv[2] = pMBinfo->mv[3] = pMBinfo->mv[0];
  484. }
  485. }
  486. if (!obmc_disable) {
  487. // OBMC for previous MB
  488. if (colNum > 0)
  489. if (pMBinfo[-1].type < IPPVC_MBTYPE_INTRA && !(pMBinfo[-1].field_info & 1)) // previous is marked as INTRA if it was mcsel
  490. mp4_OBMC(pInfo, pMBinfo - 1, mvPrev, colNum - 1, rowNum, limitRectL, pYc - 16, stepYc, pYr - 16, stepYr, cbpyPrev, coeffMB, dct_typePrev);
  491. if (mb_type < IPPVC_MBTYPE_INTRA && !field_prediction && !mcsel) {
  492. cbpyPrev = cbpy;
  493. dct_typePrev = dct_type;
  494. mvPrev[0] = mvCur[0]; mvPrev[1] = mvCur[1]; mvPrev[2] = mvCur[2]; mvPrev[3] = mvCur[3];
  495. if (mp4_DecodeInterMB(pInfo, coeffMB, quant, (cbpy << 2) + cbpc, scan) != MP4_STATUS_OK)
  496. goto Err_3;
  497. mp4_MC_HP(cbpc & 2, pCbr, stepCbr, pCbc, stepCbc, coeffMB+256, &mvCbCr, rt);
  498. mp4_MC_HP(cbpc & 1, pCrr, stepCrr, pCrc, stepCrc, coeffMB+320, &mvCbCr, rt);
  499. // OBMC current MB if it is the last in the row
  500. if (colNum == mbPerRow - 1)
  501. mp4_OBMC(pInfo, pMBinfo, mvPrev, colNum, rowNum, limitRectL, pYc, stepYc, pYr, stepYr, cbpyPrev, coeffMB, dct_typePrev);
  502. }
  503. }
  504. if (mb_type < IPPVC_MBTYPE_INTRA && (obmc_disable || field_prediction || mcsel)) {
  505. pat = mb_not_coded ? 0 : ((cbpy << 2) + cbpc);
  506. if (pat)
  507. if (mp4_DecodeInterMB(pInfo, coeffMB, quant, pat, scan) != MP4_STATUS_OK)
  508. goto Err_3;
  509. if (!mcsel) {
  510. if (quarter_sample) {
  511. if (!field_prediction) {
  512. if (mb_type == IPPVC_MBTYPE_INTER4V) {
  513. mp4_Copy8x8QP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  514. mp4_Copy8x8QP_8u(pYr+8, stepYr, pYc+8, stepYc, &mvCur[1], rt);
  515. mp4_Copy8x8QP_8u(pYr+stepYr*8, stepYr, pYc+stepYc*8, stepYc, &mvCur[2], rt);
  516. mp4_Copy8x8QP_8u(pYr+8+stepYr*8, stepYr, pYc+8+stepYc*8, stepYc, &mvCur[3], rt);
  517. } else {
  518. mp4_Copy16x16QP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  519. }
  520. } else {
  521. mp4_Copy16x8QP_8u(pYr+stepYr*mb_ftfr, stepYr*2, pYc, stepYc*2, &mvCur[0], rt);
  522. mp4_Copy16x8QP_8u(pYr+stepYr*mb_fbfr, stepYr*2, pYc+stepYc, stepYc*2, &mvCur[2], rt);
  523. }
  524. } else {
  525. if (!field_prediction) {
  526. if (mb_type == IPPVC_MBTYPE_INTER4V) {
  527. mp4_Copy8x8HP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  528. mp4_Copy8x8HP_8u(pYr+8, stepYr, pYc+8, stepYc, &mvCur[1], rt);
  529. mp4_Copy8x8HP_8u(pYr+stepYr*8, stepYr, pYc+stepYc*8, stepYc, &mvCur[2], rt);
  530. mp4_Copy8x8HP_8u(pYr+8+stepYr*8, stepYr, pYc+8+stepYc*8, stepYc, &mvCur[3], rt);
  531. } else {
  532. mp4_Copy16x16HP_8u(pYr, stepYr, pYc, stepYc, &mvCur[0], rt);
  533. }
  534. } else {
  535. mp4_Copy16x8HP_8u(pYr+stepYr*mb_ftfr, stepYr*2, pYc, stepYc*2, &mvCur[0], rt);
  536. mp4_Copy16x8HP_8u(pYr+stepYr*mb_fbfr, stepYr*2, pYc+stepYc, stepYc*2, &mvCur[2], rt);
  537. }
  538. }
  539. if (!dct_type) {
  540. mp4_AddResidual(cbpy & 8, pYc, stepYc, coeffMB);
  541. mp4_AddResidual(cbpy & 4, pYc+8, stepYc, coeffMB+64);
  542. mp4_AddResidual(cbpy & 2, pYc+stepYc*8, stepYc, coeffMB+128);
  543. mp4_AddResidual(cbpy & 1, pYc+stepYc*8+8, stepYc, coeffMB+192);
  544. } else {
  545. mp4_AddResidual(cbpy & 8, pYc, stepYc*2, coeffMB);
  546. mp4_AddResidual(cbpy & 4, pYc+8, stepYc*2, coeffMB+64);
  547. mp4_AddResidual(cbpy & 2, pYc+stepYc, stepYc*2, coeffMB+128);
  548. mp4_AddResidual(cbpy & 1, pYc+stepYc+8, stepYc*2, coeffMB+192);
  549. }
  550. if (!field_prediction) {
  551. mp4_MC_HP(cbpc & 2, pCbr, stepCbr, pCbc, stepCbc, coeffMB+256, &mvCbCr, rt);
  552. mp4_MC_HP(cbpc & 1, pCrr, stepCrr, pCrc, stepCrc, coeffMB+320, &mvCbCr, rt);
  553. } else {
  554. mp4_Copy8x4HP_8u(pCbr+(mb_ftfr ? stepCbr : 0), stepCbr*2, pCbc, stepCbc*2, &mvCbCrT, rt);
  555. mp4_Copy8x4HP_8u(pCrr+(mb_ftfr ? stepCrr : 0), stepCrr*2, pCrc, stepCrc*2, &mvCbCrT, rt);
  556. mp4_Copy8x4HP_8u(pCbr+(mb_fbfr ? stepCbr : 0), stepCbr*2, pCbc+stepCbc, stepCbc*2, &mvCbCrB, rt);
  557. mp4_Copy8x4HP_8u(pCrr+(mb_fbfr ? stepCrr : 0), stepCrr*2, pCrc+stepCrc, stepCrc*2, &mvCbCrB, rt);
  558. mp4_AddResidual(cbpc & 2, pCbc, stepCbc, coeffMB+256);
  559. mp4_AddResidual(cbpc & 1, pCrc, stepCrc, coeffMB+320);
  560. }
  561. } else {
  562. IppiRect mbRect;
  563. mbRect.x = dx; mbRect.y = dy; mbRect.width = mbRect.height = 16;
  564. ippiWarpLuma_MPEG4_8u_C1R(pInfo->VisualObject.rFrame->pY, stepYr, pYc, stepYc, &mbRect, pInfo->VisualObject.VideoObject.WarpSpec);
  565. mbRect.x >>= 1; mbRect.y >>= 1; mbRect.width = mbRect.height = 8;
  566. ippiWarpChroma_MPEG4_8u_P2R(pInfo->VisualObject.rFrame->pCb, stepCbr, pInfo->VisualObject.rFrame->pCr, stepCrr, pCbc, stepCbc, pCrc, stepCrc, &mbRect, pInfo->VisualObject.VideoObject.WarpSpec);
  567. if (pat) {
  568. if (!dct_type) {
  569. mp4_AddResidual(cbpy & 8, pYc, stepYc, coeffMB);
  570. mp4_AddResidual(cbpy & 4, pYc+8, stepYc, coeffMB+64);
  571. mp4_AddResidual(cbpy & 2, pYc+stepYc*8, stepYc, coeffMB+128);
  572. mp4_AddResidual(cbpy & 1, pYc+stepYc*8+8, stepYc, coeffMB+192);
  573. } else {
  574. mp4_AddResidual(cbpy & 8, pYc, stepYc*2, coeffMB);
  575. mp4_AddResidual(cbpy & 4, pYc+8, stepYc*2, coeffMB+64);
  576. mp4_AddResidual(cbpy & 2, pYc+stepYc, stepYc*2, coeffMB+128);
  577. mp4_AddResidual(cbpy & 1, pYc+stepYc+8, stepYc*2, coeffMB+192);
  578. }
  579. mp4_AddResidual(cbpc & 2, pCbc, stepCbc, coeffMB+256);
  580. mp4_AddResidual(cbpc & 1, pCrc, stepCrc, coeffMB+320);
  581. }
  582. }
  583. }
  584. pMBinfo ++;
  585. mvField += 2;
  586. mbInVideoPacket ++;
  587. colNum ++;
  588. if (colNum == mbPerRow) {
  589. colNum = 0;
  590. rowNum ++;
  591. if (rowNum == mbPerCol) {
  592. // skip stuffing
  593. while (mp4_ShowBits(pInfo, 10) == 1)
  594. mp4_FlushBits(pInfo, 10);
  595. return sts;
  596. }
  597. pYc += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYc << 4) - stepYc;
  598. pCbc += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbc << 3) - stepCbc;
  599. pCrc += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrc << 3) - stepCrc;
  600. pYr += (2 * MP4_NUM_EXT_MB + 1) * 16 + (stepYr << 4) - stepYr;
  601. pCbr += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCbr << 3) - stepCbr;
  602. pCrr += (2 * MP4_NUM_EXT_MB + 1) * 8 + (stepCrr << 3) - stepCrr;
  603. } else {
  604. pYc += 16; pCrc += 8; pCbc += 8;
  605. pYr += 16; pCrr += 8; pCbr += 8;
  606. }
  607. }
  608. if (!pInfo->VisualObject.VideoObject.resync_marker_disable) {
  609. int32_t found;
  610. ErrRet_3:
  611. if (mp4_CheckDecodeVideoPacket(pInfo, &found) == MP4_STATUS_OK) {
  612. if (found) {
  613. mbInVideoPacket = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num - mbCurr;
  614. quant = pInfo->VisualObject.VideoObject.VideoObjectPlane.quant_scale;
  615. mbCurr = pInfo->VisualObject.VideoObject.VideoObjectPlane.macroblock_num;
  616. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, mbCurr - rowNum * mbPerRow - colNum);
  617. rowNum = mbCurr / mbPerRow;
  618. colNum = mbCurr % mbPerRow;
  619. pYc = pInfo->VisualObject.cFrame->pY + (rowNum * stepYc + colNum) * 16; pCbc = pInfo->VisualObject.cFrame->pCb + (rowNum * stepCbc + colNum) * 8; pCrc = pInfo->VisualObject.cFrame->pCr + (rowNum * stepCrc + colNum) * 8;
  620. pYr = pInfo->VisualObject.rFrame->pY + (rowNum * stepYr + colNum) * 16; pCbr = pInfo->VisualObject.rFrame->pCb + (rowNum * stepCbr + colNum) * 8; pCrr = pInfo->VisualObject.rFrame->pCr + (rowNum * stepCrr + colNum) * 8;
  621. pMBinfo = pInfo->VisualObject.VideoObject.MBinfo + mbCurr;
  622. mvField = pInfo->VisualObject.VideoObject.FieldMV + 2 * mbCurr;
  623. break;
  624. }
  625. } else
  626. goto Err_3;
  627. }
  628. }
  629. // mark MBs the previous videopacket as invalid for prediction
  630. for (i = 1; i <= IPP_MIN(mbInVideoPacket, mbPerRow + 1); i ++) {
  631. pMBinfo[-i].validPred = 0;
  632. }
  633. }
  634. Err_3:
  635. sts = MP4_STATUS_ERROR;
  636. if (pInfo->stopOnErr)
  637. return sts;
  638. if (pInfo->VisualObject.VideoObject.resync_marker_disable || !mp4_SeekResyncMarker(pInfo)) {
  639. mp4_CopyMacroBlocks(pInfo->VisualObject.rFrame, pInfo->VisualObject.cFrame, mbPerRow, rowNum, colNum, pInfo->VisualObject.VideoObject.MacroBlockPerVOP - rowNum * mbPerRow - colNum);
  640. return sts;
  641. }
  642. goto ErrRet_3;
  643. }
  644. }