PageRenderTime 84ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Avc/slice.cpp

http://github.com/mbebenita/Broadway
C++ | 772 lines | 589 code | 106 blank | 77 comment | 106 complexity | d7c2bec3907542468414ae91a27496a1 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. /* Note for optimization: syntax decoding or operations related to B_SLICE should be
  19. commented out by macro definition or function pointers. */
  20. #include <string.h>
  21. #include "avcdec_lib.h"
  22. #include "avcdec_bitstream.h"
  23. const static int mbPart2raster[3][4] = {{0, 0, 0, 0}, {1, 1, 0, 0}, {1, 0, 1, 0}};
  24. /* decode_frame_slice() */
  25. /* decode_one_slice() */
  26. AVCDec_Status DecodeSlice(AVCDecObject *decvid)
  27. {
  28. AVCDec_Status status;
  29. AVCCommonObj *video = decvid->common;
  30. AVCSliceHeader *sliceHdr = video->sliceHdr;
  31. AVCMacroblock *currMB ;
  32. AVCDecBitstream *stream = decvid->bitstream;
  33. uint slice_group_id;
  34. uint CurrMbAddr, moreDataFlag;
  35. /* set the first mb in slice */
  36. CurrMbAddr = sliceHdr->first_mb_in_slice;
  37. slice_group_id = video->MbToSliceGroupMap[CurrMbAddr];
  38. if ((CurrMbAddr && (CurrMbAddr != (uint)(video->mbNum + 1))) && video->currSeqParams->constrained_set1_flag == 1)
  39. {
  40. ConcealSlice(decvid, video->mbNum, CurrMbAddr);
  41. }
  42. moreDataFlag = 1;
  43. video->mb_skip_run = -1;
  44. /* while loop , see subclause 7.3.4 */
  45. do
  46. {
  47. if (CurrMbAddr >= video->PicSizeInMbs)
  48. {
  49. return AVCDEC_FAIL;
  50. }
  51. currMB = video->currMB = &(video->mblock[CurrMbAddr]);
  52. video->mbNum = CurrMbAddr;
  53. currMB->slice_id = video->slice_id; // slice
  54. /* we can remove this check if we don't support Mbaff. */
  55. /* we can wrap below into an initMB() function which will also
  56. do necessary reset of macroblock related parameters. */
  57. video->mb_x = CurrMbAddr % video->PicWidthInMbs;
  58. video->mb_y = CurrMbAddr / video->PicWidthInMbs;
  59. /* check the availability of neighboring macroblocks */
  60. InitNeighborAvailability(video, CurrMbAddr);
  61. /* read_macroblock and decode_one_macroblock() */
  62. status = DecodeMB(decvid);
  63. if (status != AVCDEC_SUCCESS)
  64. {
  65. return status;
  66. }
  67. #ifdef MB_BASED_DEBLOCK
  68. if (video->currPicParams->num_slice_groups_minus1 == 0)
  69. {
  70. MBInLoopDeblock(video); /* MB-based deblocking */
  71. }
  72. else /* this mode cannot be used if the number of slice group is not one. */
  73. {
  74. return AVCDEC_FAIL;
  75. }
  76. #endif
  77. video->numMBs--;
  78. moreDataFlag = more_rbsp_data(stream);
  79. /* go to next MB */
  80. while (++CurrMbAddr < video->PicSizeInMbs && video->MbToSliceGroupMap[CurrMbAddr] != (int)slice_group_id)
  81. {
  82. }
  83. }
  84. while ((moreDataFlag && video->numMBs > 0) || video->mb_skip_run > 0); /* even if no more data, but last few MBs are skipped */
  85. if (video->numMBs == 0)
  86. {
  87. video->newPic = TRUE;
  88. video->mbNum = 0; // _Conceal
  89. return AVCDEC_PICTURE_READY;
  90. }
  91. return AVCDEC_SUCCESS;
  92. }
  93. /* read MB mode and motion vectors */
  94. /* perform Intra/Inter prediction and residue */
  95. /* update video->mb_skip_run */
  96. AVCDec_Status DecodeMB(AVCDecObject *decvid)
  97. {
  98. AVCDec_Status status;
  99. AVCCommonObj *video = decvid->common;
  100. AVCDecBitstream *stream = decvid->bitstream;
  101. AVCMacroblock *currMB = video->currMB;
  102. uint mb_type;
  103. int slice_type = video->slice_type;
  104. int temp;
  105. currMB->QPy = video->QPy;
  106. currMB->QPc = video->QPc;
  107. if (slice_type == AVC_P_SLICE)
  108. {
  109. if (video->mb_skip_run < 0)
  110. {
  111. ue_v(stream, (uint *)&(video->mb_skip_run));
  112. }
  113. if (video->mb_skip_run == 0)
  114. {
  115. /* this will not handle the case where the slice ends with a mb_skip_run == 0 and no following MB data */
  116. ue_v(stream, &mb_type);
  117. if (mb_type > 30)
  118. {
  119. return AVCDEC_FAIL;
  120. }
  121. InterpretMBModeP(currMB, mb_type);
  122. video->mb_skip_run = -1;
  123. }
  124. else
  125. {
  126. /* see subclause 7.4.4 for more details on how
  127. mb_field_decoding_flag is derived in case of skipped MB */
  128. currMB->mb_intra = FALSE;
  129. currMB->mbMode = AVC_SKIP;
  130. currMB->MbPartWidth = currMB->MbPartHeight = 16;
  131. currMB->NumMbPart = 1;
  132. currMB->NumSubMbPart[0] = currMB->NumSubMbPart[1] =
  133. currMB->NumSubMbPart[2] = currMB->NumSubMbPart[3] = 1; //
  134. currMB->SubMbPartWidth[0] = currMB->SubMbPartWidth[1] =
  135. currMB->SubMbPartWidth[2] = currMB->SubMbPartWidth[3] = currMB->MbPartWidth;
  136. currMB->SubMbPartHeight[0] = currMB->SubMbPartHeight[1] =
  137. currMB->SubMbPartHeight[2] = currMB->SubMbPartHeight[3] = currMB->MbPartHeight;
  138. memset(currMB->nz_coeff, 0, sizeof(uint8)*NUM_BLKS_IN_MB);
  139. currMB->CBP = 0;
  140. video->cbp4x4 = 0;
  141. /* for skipped MB, always look at the first entry in RefPicList */
  142. currMB->RefIdx[0] = currMB->RefIdx[1] =
  143. currMB->RefIdx[2] = currMB->RefIdx[3] = video->RefPicList0[0]->RefIdx;
  144. InterMBPrediction(video);
  145. video->mb_skip_run--;
  146. return AVCDEC_SUCCESS;
  147. }
  148. }
  149. else
  150. {
  151. /* Then decode mode and MV */
  152. ue_v(stream, &mb_type);
  153. if (mb_type > 25)
  154. {
  155. return AVCDEC_FAIL;
  156. }
  157. InterpretMBModeI(currMB, mb_type);
  158. }
  159. if (currMB->mbMode != AVC_I_PCM)
  160. {
  161. if (currMB->mbMode == AVC_P8 || currMB->mbMode == AVC_P8ref0)
  162. {
  163. status = sub_mb_pred(video, currMB, stream);
  164. }
  165. else
  166. {
  167. status = mb_pred(video, currMB, stream) ;
  168. }
  169. if (status != AVCDEC_SUCCESS)
  170. {
  171. return status;
  172. }
  173. if (currMB->mbMode != AVC_I16)
  174. {
  175. /* decode coded_block_pattern */
  176. status = DecodeCBP(currMB, stream);
  177. if (status != AVCDEC_SUCCESS)
  178. {
  179. return status;
  180. }
  181. }
  182. if (currMB->CBP > 0 || currMB->mbMode == AVC_I16)
  183. {
  184. se_v(stream, &temp);
  185. if (temp)
  186. {
  187. temp += (video->QPy + 52);
  188. currMB->QPy = video->QPy = temp - 52 * (temp * 79 >> 12);
  189. if (currMB->QPy > 51 || currMB->QPy < 0)
  190. {
  191. video->QPy = AVC_CLIP3(0, 51, video->QPy);
  192. // return AVCDEC_FAIL;
  193. }
  194. video->QPy_div_6 = (video->QPy * 43) >> 8;
  195. video->QPy_mod_6 = video->QPy - 6 * video->QPy_div_6;
  196. currMB->QPc = video->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, video->QPy + video->currPicParams->chroma_qp_index_offset)];
  197. video->QPc_div_6 = (video->QPc * 43) >> 8;
  198. video->QPc_mod_6 = video->QPc - 6 * video->QPc_div_6;
  199. }
  200. }
  201. /* decode residue and inverse transform */
  202. status = residual(decvid, currMB);
  203. if (status != AVCDEC_SUCCESS)
  204. {
  205. return status;
  206. }
  207. }
  208. else
  209. {
  210. if (stream->bitcnt & 7)
  211. {
  212. BitstreamByteAlign(stream);
  213. }
  214. /* decode pcm_byte[i] */
  215. DecodeIntraPCM(video, stream);
  216. currMB->QPy = 0; /* necessary for deblocking */ // _OPTIMIZE
  217. currMB->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, video->currPicParams->chroma_qp_index_offset)];
  218. /* default values, don't know if really needed */
  219. currMB->CBP = 0x3F;
  220. video->cbp4x4 = 0xFFFF;
  221. currMB->mb_intra = TRUE;
  222. memset(currMB->nz_coeff, 16, sizeof(uint8)*NUM_BLKS_IN_MB);
  223. return AVCDEC_SUCCESS;
  224. }
  225. /* do Intra/Inter prediction, together with the residue compensation */
  226. /* This part should be common between the skip and no-skip */
  227. if (currMB->mbMode == AVC_I4 || currMB->mbMode == AVC_I16)
  228. {
  229. IntraMBPrediction(video);
  230. }
  231. else
  232. {
  233. InterMBPrediction(video);
  234. }
  235. return AVCDEC_SUCCESS;
  236. }
  237. /* see subclause 7.3.5.1 */
  238. AVCDec_Status mb_pred(AVCCommonObj *video, AVCMacroblock *currMB, AVCDecBitstream *stream)
  239. {
  240. int mbPartIdx;
  241. AVCSliceHeader *sliceHdr = video->sliceHdr;
  242. uint max_ref_idx;
  243. const int *temp_0;
  244. int16 *temp_1;
  245. uint code;
  246. if (currMB->mbMode == AVC_I4 || currMB->mbMode == AVC_I16)
  247. {
  248. video->intraAvailA = video->intraAvailB = video->intraAvailC = video->intraAvailD = 0;
  249. if (!video->currPicParams->constrained_intra_pred_flag)
  250. {
  251. video->intraAvailA = video->mbAvailA;
  252. video->intraAvailB = video->mbAvailB;
  253. video->intraAvailC = video->mbAvailC;
  254. video->intraAvailD = video->mbAvailD;
  255. }
  256. else
  257. {
  258. if (video->mbAvailA)
  259. {
  260. video->intraAvailA = video->mblock[video->mbAddrA].mb_intra;
  261. }
  262. if (video->mbAvailB)
  263. {
  264. video->intraAvailB = video->mblock[video->mbAddrB].mb_intra ;
  265. }
  266. if (video->mbAvailC)
  267. {
  268. video->intraAvailC = video->mblock[video->mbAddrC].mb_intra;
  269. }
  270. if (video->mbAvailD)
  271. {
  272. video->intraAvailD = video->mblock[video->mbAddrD].mb_intra;
  273. }
  274. }
  275. if (currMB->mbMode == AVC_I4)
  276. {
  277. /* perform prediction to get the actual intra 4x4 pred mode */
  278. DecodeIntra4x4Mode(video, currMB, stream);
  279. /* output will be in currMB->i4Mode[4][4] */
  280. }
  281. ue_v(stream, &code);
  282. if (code > 3)
  283. {
  284. return AVCDEC_FAIL; /* out of range */
  285. }
  286. currMB->intra_chroma_pred_mode = (AVCIntraChromaPredMode)code;
  287. }
  288. else
  289. {
  290. memset(currMB->ref_idx_L0, 0, sizeof(int16)*4);
  291. /* see subclause 7.4.5.1 for the range of ref_idx_lX */
  292. // max_ref_idx = sliceHdr->num_ref_idx_l0_active_minus1;
  293. max_ref_idx = video->refList0Size - 1;
  294. /* decode ref index for L0 */
  295. if (sliceHdr->num_ref_idx_l0_active_minus1 > 0)
  296. {
  297. for (mbPartIdx = 0; mbPartIdx < currMB->NumMbPart; mbPartIdx++)
  298. {
  299. te_v(stream, &code, max_ref_idx);
  300. if (code > (uint)max_ref_idx)
  301. {
  302. return AVCDEC_FAIL;
  303. }
  304. currMB->ref_idx_L0[mbPartIdx] = code;
  305. }
  306. }
  307. /* populate ref_idx_L0 */
  308. temp_0 = &mbPart2raster[currMB->mbMode-AVC_P16][0];
  309. temp_1 = &currMB->ref_idx_L0[3];
  310. *temp_1-- = currMB->ref_idx_L0[*temp_0++];
  311. *temp_1-- = currMB->ref_idx_L0[*temp_0++];
  312. *temp_1-- = currMB->ref_idx_L0[*temp_0++];
  313. *temp_1-- = currMB->ref_idx_L0[*temp_0++];
  314. /* Global reference index, these values are used in deblock */
  315. currMB->RefIdx[0] = video->RefPicList0[currMB->ref_idx_L0[0]]->RefIdx;
  316. currMB->RefIdx[1] = video->RefPicList0[currMB->ref_idx_L0[1]]->RefIdx;
  317. currMB->RefIdx[2] = video->RefPicList0[currMB->ref_idx_L0[2]]->RefIdx;
  318. currMB->RefIdx[3] = video->RefPicList0[currMB->ref_idx_L0[3]]->RefIdx;
  319. /* see subclause 7.4.5.1 for the range of ref_idx_lX */
  320. max_ref_idx = sliceHdr->num_ref_idx_l1_active_minus1;
  321. /* decode mvd_l0 */
  322. for (mbPartIdx = 0; mbPartIdx < currMB->NumMbPart; mbPartIdx++)
  323. {
  324. se_v(stream, &(video->mvd_l0[mbPartIdx][0][0]));
  325. se_v(stream, &(video->mvd_l0[mbPartIdx][0][1]));
  326. }
  327. }
  328. return AVCDEC_SUCCESS;
  329. }
  330. /* see subclause 7.3.5.2 */
  331. AVCDec_Status sub_mb_pred(AVCCommonObj *video, AVCMacroblock *currMB, AVCDecBitstream *stream)
  332. {
  333. int mbPartIdx, subMbPartIdx;
  334. AVCSliceHeader *sliceHdr = video->sliceHdr;
  335. uint max_ref_idx;
  336. uint sub_mb_type[4];
  337. uint code;
  338. memset(currMB->ref_idx_L0, 0, sizeof(int16)*4);
  339. for (mbPartIdx = 0; mbPartIdx < 4; mbPartIdx++)
  340. {
  341. ue_v(stream, &(sub_mb_type[mbPartIdx]));
  342. if (sub_mb_type[mbPartIdx] > 3)
  343. {
  344. return AVCDEC_FAIL;
  345. }
  346. }
  347. /* we have to check the values to make sure they are valid */
  348. /* assign values to currMB->sub_mb_type[], currMB->MBPartPredMode[][x] */
  349. InterpretSubMBModeP(currMB, sub_mb_type);
  350. /* see subclause 7.4.5.1 for the range of ref_idx_lX */
  351. // max_ref_idx = sliceHdr->num_ref_idx_l0_active_minus1;
  352. max_ref_idx = video->refList0Size - 1;
  353. if (sliceHdr->num_ref_idx_l0_active_minus1 > 0 && currMB->mbMode != AVC_P8ref0)
  354. {
  355. for (mbPartIdx = 0; mbPartIdx < 4; mbPartIdx++)
  356. {
  357. te_v(stream, (uint*)&code, max_ref_idx);
  358. if (code > max_ref_idx)
  359. {
  360. return AVCDEC_FAIL;
  361. }
  362. currMB->ref_idx_L0[mbPartIdx] = code;
  363. }
  364. }
  365. /* see subclause 7.4.5.1 for the range of ref_idx_lX */
  366. max_ref_idx = sliceHdr->num_ref_idx_l1_active_minus1;
  367. /* if(video->MbaffFrameFlag && currMB->mb_field_decoding_flag)
  368. max_ref_idx = 2*sliceHdr->num_ref_idx_l1_active_minus1 + 1;*/
  369. for (mbPartIdx = 0; mbPartIdx < 4; mbPartIdx++)
  370. {
  371. for (subMbPartIdx = 0; subMbPartIdx < currMB->NumSubMbPart[mbPartIdx]; subMbPartIdx++)
  372. {
  373. se_v(stream, &(video->mvd_l0[mbPartIdx][subMbPartIdx][0]));
  374. se_v(stream, &(video->mvd_l0[mbPartIdx][subMbPartIdx][1]));
  375. }
  376. /* used in deblocking */
  377. currMB->RefIdx[mbPartIdx] = video->RefPicList0[currMB->ref_idx_L0[mbPartIdx]]->RefIdx;
  378. }
  379. return AVCDEC_SUCCESS;
  380. }
  381. void InterpretMBModeI(AVCMacroblock *mblock, uint mb_type)
  382. {
  383. mblock->NumMbPart = 1;
  384. mblock->mb_intra = TRUE;
  385. if (mb_type == 0) /* I_4x4 */
  386. {
  387. mblock->mbMode = AVC_I4;
  388. }
  389. else if (mb_type < 25) /* I_PCM */
  390. {
  391. mblock->mbMode = AVC_I16;
  392. mblock->i16Mode = (AVCIntra16x16PredMode)((mb_type - 1) & 0x3);
  393. if (mb_type > 12)
  394. {
  395. mblock->CBP = (((mb_type - 13) >> 2) << 4) + 0x0F;
  396. }
  397. else
  398. {
  399. mblock->CBP = ((mb_type - 1) >> 2) << 4;
  400. }
  401. }
  402. else
  403. {
  404. mblock->mbMode = AVC_I_PCM;
  405. }
  406. return ;
  407. }
  408. void InterpretMBModeP(AVCMacroblock *mblock, uint mb_type)
  409. {
  410. const static int map2PartWidth[5] = {16, 16, 8, 8, 8};
  411. const static int map2PartHeight[5] = {16, 8, 16, 8, 8};
  412. const static int map2NumPart[5] = {1, 2, 2, 4, 4};
  413. const static AVCMBMode map2mbMode[5] = {AVC_P16, AVC_P16x8, AVC_P8x16, AVC_P8, AVC_P8ref0};
  414. mblock->mb_intra = FALSE;
  415. if (mb_type < 5)
  416. {
  417. mblock->mbMode = map2mbMode[mb_type];
  418. mblock->MbPartWidth = map2PartWidth[mb_type];
  419. mblock->MbPartHeight = map2PartHeight[mb_type];
  420. mblock->NumMbPart = map2NumPart[mb_type];
  421. mblock->NumSubMbPart[0] = mblock->NumSubMbPart[1] =
  422. mblock->NumSubMbPart[2] = mblock->NumSubMbPart[3] = 1;
  423. mblock->SubMbPartWidth[0] = mblock->SubMbPartWidth[1] =
  424. mblock->SubMbPartWidth[2] = mblock->SubMbPartWidth[3] = mblock->MbPartWidth;
  425. mblock->SubMbPartHeight[0] = mblock->SubMbPartHeight[1] =
  426. mblock->SubMbPartHeight[2] = mblock->SubMbPartHeight[3] = mblock->MbPartHeight;
  427. }
  428. else
  429. {
  430. InterpretMBModeI(mblock, mb_type - 5);
  431. /* set MV and Ref_Idx codes of Intra blocks in P-slices */
  432. memset(mblock->mvL0, 0, sizeof(int32)*16);
  433. mblock->ref_idx_L0[0] = mblock->ref_idx_L0[1] = mblock->ref_idx_L0[2] = mblock->ref_idx_L0[3] = -1;
  434. }
  435. return ;
  436. }
  437. void InterpretMBModeB(AVCMacroblock *mblock, uint mb_type)
  438. {
  439. const static int map2PartWidth[23] = {8, 16, 16, 16, 16, 8, 16, 8, 16, 8,
  440. 16, 8, 16, 8, 16, 8, 16, 8, 16, 8, 16, 8, 8
  441. };
  442. const static int map2PartHeight[23] = {8, 16, 16, 16, 8, 16, 8, 16, 8,
  443. 16, 8, 16, 8, 16, 8, 16, 8, 16, 8, 16, 8, 16, 8
  444. };
  445. /* see enum AVCMBType declaration */
  446. const static AVCMBMode map2mbMode[23] = {AVC_BDirect16, AVC_P16, AVC_P16, AVC_P16,
  447. AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16,
  448. AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16,
  449. AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16, AVC_P16x8, AVC_P8x16, AVC_P8
  450. };
  451. const static int map2PredMode1[23] = {3, 0, 1, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, -1};
  452. const static int map2PredMode2[23] = { -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 1, 1, 2, 2, -1};
  453. const static int map2NumPart[23] = { -1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4};
  454. mblock->mb_intra = FALSE;
  455. if (mb_type < 23)
  456. {
  457. mblock->mbMode = map2mbMode[mb_type];
  458. mblock->NumMbPart = map2NumPart[mb_type];
  459. mblock->MBPartPredMode[0][0] = (AVCPredMode)map2PredMode1[mb_type];
  460. if (mblock->NumMbPart > 1)
  461. {
  462. mblock->MBPartPredMode[1][0] = (AVCPredMode)map2PredMode2[mb_type];
  463. }
  464. mblock->MbPartWidth = map2PartWidth[mb_type];
  465. mblock->MbPartHeight = map2PartHeight[mb_type];
  466. }
  467. else
  468. {
  469. InterpretMBModeI(mblock, mb_type - 23);
  470. }
  471. return ;
  472. }
  473. void InterpretMBModeSI(AVCMacroblock *mblock, uint mb_type)
  474. {
  475. mblock->mb_intra = TRUE;
  476. if (mb_type == 0)
  477. {
  478. mblock->mbMode = AVC_SI4;
  479. /* other values are N/A */
  480. }
  481. else
  482. {
  483. InterpretMBModeI(mblock, mb_type - 1);
  484. }
  485. return ;
  486. }
  487. /* input is mblock->sub_mb_type[] */
  488. void InterpretSubMBModeP(AVCMacroblock *mblock, uint *sub_mb_type)
  489. {
  490. int i, sub_type;
  491. /* see enum AVCMBType declaration */
  492. // const static AVCSubMBMode map2subMbMode[4] = {AVC_8x8,AVC_8x4,AVC_4x8,AVC_4x4};
  493. const static int map2subPartWidth[4] = {8, 8, 4, 4};
  494. const static int map2subPartHeight[4] = {8, 4, 8, 4};
  495. const static int map2numSubPart[4] = {1, 2, 2, 4};
  496. for (i = 0; i < 4 ; i++)
  497. {
  498. sub_type = (int) sub_mb_type[i];
  499. // mblock->subMbMode[i] = map2subMbMode[sub_type];
  500. mblock->NumSubMbPart[i] = map2numSubPart[sub_type];
  501. mblock->SubMbPartWidth[i] = map2subPartWidth[sub_type];
  502. mblock->SubMbPartHeight[i] = map2subPartHeight[sub_type];
  503. }
  504. return ;
  505. }
  506. void InterpretSubMBModeB(AVCMacroblock *mblock, uint *sub_mb_type)
  507. {
  508. int i, j, sub_type;
  509. /* see enum AVCMBType declaration */
  510. const static AVCSubMBMode map2subMbMode[13] = {AVC_BDirect8, AVC_8x8, AVC_8x8,
  511. AVC_8x8, AVC_8x4, AVC_4x8, AVC_8x4, AVC_4x8, AVC_8x4, AVC_4x8, AVC_4x4, AVC_4x4, AVC_4x4
  512. };
  513. const static int map2subPartWidth[13] = {4, 8, 8, 8, 8, 4, 8, 4, 8, 4, 4, 4, 4};
  514. const static int map2subPartHeight[13] = {4, 8, 8, 8, 4, 8, 4, 8, 4, 8, 4, 4, 4};
  515. const static int map2numSubPart[13] = {1, 1, 1, 2, 2, 2, 2, 2, 2, 4, 4, 4};
  516. const static int map2predMode[13] = {3, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 2};
  517. for (i = 0; i < 4 ; i++)
  518. {
  519. sub_type = (int) sub_mb_type[i];
  520. mblock->subMbMode[i] = map2subMbMode[sub_type];
  521. mblock->NumSubMbPart[i] = map2numSubPart[sub_type];
  522. mblock->SubMbPartWidth[i] = map2subPartWidth[sub_type];
  523. mblock->SubMbPartHeight[i] = map2subPartHeight[sub_type];
  524. for (j = 0; j < 4; j++)
  525. {
  526. mblock->MBPartPredMode[i][j] = (AVCPredMode)map2predMode[sub_type];
  527. }
  528. }
  529. return ;
  530. }
  531. /* see subclause 8.3.1 */
  532. AVCDec_Status DecodeIntra4x4Mode(AVCCommonObj *video, AVCMacroblock *currMB, AVCDecBitstream *stream)
  533. {
  534. int intra4x4PredModeA = 0, intra4x4PredModeB = 0, predIntra4x4PredMode = 0;
  535. int component, SubBlock_indx, block_x, block_y;
  536. int dcOnlyPredictionFlag;
  537. uint prev_intra4x4_pred_mode_flag[16];
  538. int rem_intra4x4_pred_mode[16];
  539. int bindx = 0;
  540. for (component = 0; component < 4; component++) /* partition index */
  541. {
  542. block_x = ((component & 1) << 1);
  543. block_y = ((component >> 1) << 1);
  544. for (SubBlock_indx = 0; SubBlock_indx < 4; SubBlock_indx++) /* sub-partition index */
  545. {
  546. BitstreamRead1Bit(stream, &(prev_intra4x4_pred_mode_flag[bindx]));
  547. if (!prev_intra4x4_pred_mode_flag[bindx])
  548. {
  549. BitstreamReadBits(stream, 3, (uint*)&(rem_intra4x4_pred_mode[bindx]));
  550. }
  551. dcOnlyPredictionFlag = 0;
  552. if (block_x > 0)
  553. {
  554. intra4x4PredModeA = currMB->i4Mode[(block_y << 2) + block_x - 1 ];
  555. }
  556. else
  557. {
  558. if (video->intraAvailA)
  559. {
  560. if (video->mblock[video->mbAddrA].mbMode == AVC_I4)
  561. {
  562. intra4x4PredModeA = video->mblock[video->mbAddrA].i4Mode[(block_y << 2) + 3];
  563. }
  564. else
  565. {
  566. intra4x4PredModeA = AVC_I4_DC;
  567. }
  568. }
  569. else
  570. {
  571. dcOnlyPredictionFlag = 1;
  572. }
  573. }
  574. if (block_y > 0)
  575. {
  576. intra4x4PredModeB = currMB->i4Mode[((block_y-1) << 2) + block_x];
  577. }
  578. else
  579. {
  580. if (video->intraAvailB)
  581. {
  582. if (video->mblock[video->mbAddrB].mbMode == AVC_I4)
  583. {
  584. intra4x4PredModeB = video->mblock[video->mbAddrB].i4Mode[(3 << 2) + block_x];
  585. }
  586. else
  587. {
  588. intra4x4PredModeB = AVC_I4_DC;
  589. }
  590. }
  591. else
  592. {
  593. dcOnlyPredictionFlag = 1;
  594. }
  595. }
  596. if (dcOnlyPredictionFlag)
  597. {
  598. intra4x4PredModeA = intra4x4PredModeB = AVC_I4_DC;
  599. }
  600. predIntra4x4PredMode = AVC_MIN(intra4x4PredModeA, intra4x4PredModeB);
  601. if (prev_intra4x4_pred_mode_flag[bindx])
  602. {
  603. currMB->i4Mode[(block_y<<2)+block_x] = (AVCIntra4x4PredMode)predIntra4x4PredMode;
  604. }
  605. else
  606. {
  607. if (rem_intra4x4_pred_mode[bindx] < predIntra4x4PredMode)
  608. {
  609. currMB->i4Mode[(block_y<<2)+block_x] = (AVCIntra4x4PredMode)rem_intra4x4_pred_mode[bindx];
  610. }
  611. else
  612. {
  613. currMB->i4Mode[(block_y<<2)+block_x] = (AVCIntra4x4PredMode)(rem_intra4x4_pred_mode[bindx] + 1);
  614. }
  615. }
  616. bindx++;
  617. block_y += (SubBlock_indx & 1) ;
  618. block_x += (1 - 2 * (SubBlock_indx & 1)) ;
  619. }
  620. }
  621. return AVCDEC_SUCCESS;
  622. }
  623. AVCDec_Status ConcealSlice(AVCDecObject *decvid, int mbnum_start, int mbnum_end)
  624. {
  625. AVCCommonObj *video = decvid->common;
  626. AVCMacroblock *currMB ;
  627. int CurrMbAddr;
  628. if (video->RefPicList0[0] == NULL)
  629. {
  630. return AVCDEC_FAIL;
  631. }
  632. for (CurrMbAddr = mbnum_start; CurrMbAddr < mbnum_end; CurrMbAddr++)
  633. {
  634. currMB = video->currMB = &(video->mblock[CurrMbAddr]);
  635. video->mbNum = CurrMbAddr;
  636. currMB->slice_id = video->slice_id++; // slice
  637. /* we can remove this check if we don't support Mbaff. */
  638. /* we can wrap below into an initMB() function which will also
  639. do necessary reset of macroblock related parameters. */
  640. video->mb_x = CurrMbAddr % video->PicWidthInMbs;
  641. video->mb_y = CurrMbAddr / video->PicWidthInMbs;
  642. /* check the availability of neighboring macroblocks */
  643. InitNeighborAvailability(video, CurrMbAddr);
  644. currMB->mb_intra = FALSE;
  645. currMB->mbMode = AVC_SKIP;
  646. currMB->MbPartWidth = currMB->MbPartHeight = 16;
  647. currMB->NumMbPart = 1;
  648. currMB->NumSubMbPart[0] = currMB->NumSubMbPart[1] =
  649. currMB->NumSubMbPart[2] = currMB->NumSubMbPart[3] = 1;
  650. currMB->SubMbPartWidth[0] = currMB->SubMbPartWidth[1] =
  651. currMB->SubMbPartWidth[2] = currMB->SubMbPartWidth[3] = currMB->MbPartWidth;
  652. currMB->SubMbPartHeight[0] = currMB->SubMbPartHeight[1] =
  653. currMB->SubMbPartHeight[2] = currMB->SubMbPartHeight[3] = currMB->MbPartHeight;
  654. currMB->QPy = 26;
  655. currMB->QPc = 26;
  656. memset(currMB->nz_coeff, 0, sizeof(uint8)*NUM_BLKS_IN_MB);
  657. currMB->CBP = 0;
  658. video->cbp4x4 = 0;
  659. /* for skipped MB, always look at the first entry in RefPicList */
  660. currMB->RefIdx[0] = currMB->RefIdx[1] =
  661. currMB->RefIdx[2] = currMB->RefIdx[3] = video->RefPicList0[0]->RefIdx;
  662. InterMBPrediction(video);
  663. video->numMBs--;
  664. }
  665. return AVCDEC_SUCCESS;
  666. }