/Avc/avcdec_api.cpp

http://github.com/mbebenita/Broadway · C++ · 1037 lines · 689 code · 152 blank · 196 comment · 198 complexity · 6e905f515134a3c87cd13ed53fdca1fe MD5 · raw file

  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. /**
  19. This file contains application function interfaces to the AVC decoder library.
  20. @publishedAll
  21. */
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include "avcdec_api.h"
  25. #include "avcdec_lib.h"
  26. #include "avcdec_bitstream.h"
  27. /* ======================================================================== */
  28. /* Function : EBSPtoRBSP() */
  29. /* Date : 11/4/2003 */
  30. /* Purpose : Convert EBSP to RBSP and overwrite it. */
  31. /* Assuming that forbidden_zero, nal_ref_idc and nal_unit_type */
  32. /* (first byte), has been taken out of the nal_unit. */
  33. /* In/out : */
  34. /* Return : */
  35. /* Modified : */
  36. /* ======================================================================== */
  37. /**
  38. @pseudocode "
  39. NumBytesInRBSP = 0;
  40. for(i=0:i< *size; i++){
  41. if(i+2 < *size && next_bits(24)==0x000003){
  42. rbsp_byte[NumBytesInRBSP++];
  43. rbsp_byte[NumBytesInRBSP++];
  44. i+=2;
  45. emulation_prevention_three_byte (0x03)
  46. }
  47. else
  48. rbsp_byte[NumBytesInRBSP++];
  49. }"
  50. */
  51. AVCDec_Status EBSPtoRBSP(uint8 *nal_unit, int *size)
  52. {
  53. int i, j;
  54. int count = 0;
  55. /* This code is based on EBSPtoRBSP of JM */
  56. j = 0;
  57. for (i = 0; i < *size; i++)
  58. {
  59. if (count == 2 && nal_unit[i] == 0x03)
  60. {
  61. i++;
  62. count = 0;
  63. }
  64. nal_unit[j] = nal_unit[i];
  65. if (nal_unit[i] == 0x00)
  66. count++;
  67. else
  68. count = 0;
  69. j++;
  70. }
  71. *size = j;
  72. return AVCDEC_SUCCESS;
  73. }
  74. /* ======================================================================== */
  75. /* Function : PVAVCAnnexBGetNALUnit() */
  76. /* Date : 11/3/2003 */
  77. /* Purpose : Parse a NAL from byte stream format. */
  78. /* In/out : */
  79. /* Return : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail. */
  80. /* Modified : */
  81. /* ======================================================================== */
  82. /**
  83. @pseudocode "
  84. byte_stream_nal_unit(NumBytesInNalunit){
  85. while(next_bits(24) != 0x000001)
  86. zero_byte
  87. if(more_data_in_byte_stream()){
  88. start_code_prefix_one_3bytes // equal 0x000001
  89. nal_unit(NumBytesInNALunit)
  90. }
  91. }"
  92. */
  93. OSCL_EXPORT_REF AVCDec_Status PVAVCAnnexBGetNALUnit(uint8 *bitstream, uint8 **nal_unit,
  94. int *size)
  95. {
  96. int i, j, FoundStartCode = 0;
  97. int end;
  98. i = 0;
  99. while (bitstream[i] == 0 && i < *size)
  100. {
  101. i++;
  102. }
  103. if (i >= *size)
  104. {
  105. *nal_unit = bitstream;
  106. return AVCDEC_FAIL; /* cannot find any start_code_prefix. */
  107. }
  108. else if (bitstream[i] != 0x1)
  109. {
  110. i = -1; /* start_code_prefix is not at the beginning, continue */
  111. }
  112. i++;
  113. *nal_unit = bitstream + i; /* point to the beginning of the NAL unit */
  114. j = end = i;
  115. while (!FoundStartCode)
  116. {
  117. while ((j + 1 < *size) && (bitstream[j] != 0 || bitstream[j+1] != 0)) /* see 2 consecutive zero bytes */
  118. {
  119. j++;
  120. }
  121. end = j; /* stop and check for start code */
  122. while (j + 2 < *size && bitstream[j+2] == 0) /* keep reading for zero byte */
  123. {
  124. j++;
  125. }
  126. if (j + 2 >= *size)
  127. {
  128. *size -= i;
  129. return AVCDEC_NO_NEXT_SC; /* cannot find the second start_code_prefix */
  130. }
  131. if (bitstream[j+2] == 0x1)
  132. {
  133. FoundStartCode = 1;
  134. }
  135. else
  136. {
  137. /* could be emulation code 0x3 */
  138. j += 2; /* continue the search */
  139. }
  140. }
  141. *size = end - i;
  142. return AVCDEC_SUCCESS;
  143. }
  144. /* ======================================================================== */
  145. /* Function : PVAVCGetNALType() */
  146. /* Date : 11/4/2003 */
  147. /* Purpose : Sniff NAL type from the bitstream */
  148. /* In/out : */
  149. /* Return : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail. */
  150. /* Modified : */
  151. /* ======================================================================== */
  152. OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetNALType(uint8 *bitstream, int size,
  153. int *nal_type, int *nal_ref_idc)
  154. {
  155. int forbidden_zero_bit;
  156. if (size > 0)
  157. {
  158. forbidden_zero_bit = bitstream[0] >> 7;
  159. if (forbidden_zero_bit != 0)
  160. return AVCDEC_FAIL;
  161. *nal_ref_idc = (bitstream[0] & 0x60) >> 5;
  162. *nal_type = bitstream[0] & 0x1F;
  163. return AVCDEC_SUCCESS;
  164. }
  165. return AVCDEC_FAIL;
  166. }
  167. /* ======================================================================== */
  168. /* Function : PVAVCDecSeqParamSet() */
  169. /* Date : 11/4/2003 */
  170. /* Purpose : Initialize sequence, memory allocation if necessary. */
  171. /* In/out : */
  172. /* Return : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail. */
  173. /* Modified : */
  174. /* ======================================================================== */
  175. OSCL_EXPORT_REF AVCDec_Status PVAVCDecSeqParamSet(AVCHandle *avcHandle, uint8 *nal_unit,
  176. int nal_size)
  177. {
  178. AVCDec_Status status;
  179. AVCDecObject *decvid;
  180. AVCCommonObj *video;
  181. AVCDecBitstream *bitstream;
  182. void *userData = avcHandle->userData;
  183. bool first_seq = FALSE;
  184. int i;
  185. DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "PVAVCDecSeqParamSet", -1, -1);
  186. if (avcHandle->AVCObject == NULL)
  187. {
  188. first_seq = TRUE;
  189. //avcHandle->memory_usage = 0;
  190. /* allocate AVCDecObject */
  191. avcHandle->AVCObject = (void*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecObject), 0/*DEFAULT_ATTR*/);
  192. if (avcHandle->AVCObject == NULL)
  193. {
  194. return AVCDEC_MEMORY_FAIL;
  195. }
  196. decvid = (AVCDecObject*) avcHandle->AVCObject;
  197. memset(decvid, 0, sizeof(AVCDecObject));
  198. decvid->common = (AVCCommonObj*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCCommonObj), 0);
  199. if (decvid->common == NULL)
  200. {
  201. return AVCDEC_MEMORY_FAIL;
  202. }
  203. video = decvid->common;
  204. memset(video, 0, sizeof(AVCCommonObj));
  205. video->seq_parameter_set_id = 9999; /* set it to some illegal value */
  206. decvid->bitstream = (AVCDecBitstream *) avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecBitstream), 1/*DEFAULT_ATTR*/);
  207. if (decvid->bitstream == NULL)
  208. {
  209. return AVCDEC_MEMORY_FAIL;
  210. }
  211. decvid->bitstream->userData = avcHandle->userData; /* callback for more data */
  212. decvid->avcHandle = avcHandle;
  213. decvid->debugEnable = avcHandle->debugEnable;
  214. }
  215. decvid = (AVCDecObject*) avcHandle->AVCObject;
  216. video = decvid->common;
  217. bitstream = decvid->bitstream;
  218. /* check if we can reuse the memory without re-allocating it. */
  219. /* always check if(first_seq==TRUE) */
  220. /* Conversion from EBSP to RBSP */
  221. video->forbidden_bit = nal_unit[0] >> 7;
  222. if (video->forbidden_bit) return AVCDEC_FAIL;
  223. video->nal_ref_idc = (nal_unit[0] & 0x60) >> 5;
  224. video->nal_unit_type = (AVCNalUnitType)(nal_unit[0] & 0x1F);
  225. if (video->nal_unit_type != AVC_NALTYPE_SPS) /* not a SPS NAL */
  226. {
  227. return AVCDEC_FAIL;
  228. }
  229. /* Initialize bitstream structure*/
  230. BitstreamInit(bitstream, nal_unit + 1, nal_size - 1);
  231. /* if first_seq == TRUE, allocate the following memory */
  232. if (first_seq == TRUE)
  233. {
  234. video->currSeqParams = NULL; /* initialize it to NULL */
  235. video->currPicParams = NULL;
  236. /* There are 32 pointers to sequence param set, seqParams.
  237. There are 255 pointers to picture param set, picParams.*/
  238. for (i = 0; i < 32; i++)
  239. decvid->seqParams[i] = NULL;
  240. for (i = 0; i < 256; i++)
  241. decvid->picParams[i] = NULL;
  242. video->MbToSliceGroupMap = NULL;
  243. video->mem_mgr_ctrl_eq_5 = FALSE;
  244. video->newPic = TRUE;
  245. video->newSlice = TRUE;
  246. video->currPic = NULL;
  247. video->currFS = NULL;
  248. video->prevRefPic = NULL;
  249. video->mbNum = 0; // MC_Conceal
  250. /* Allocate sliceHdr. */
  251. video->sliceHdr = (AVCSliceHeader*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCSliceHeader), 5/*DEFAULT_ATTR*/);
  252. if (video->sliceHdr == NULL)
  253. {
  254. return AVCDEC_MEMORY_FAIL;
  255. }
  256. video->decPicBuf = (AVCDecPicBuffer*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCDecPicBuffer), 3/*DEFAULT_ATTR*/);
  257. if (video->decPicBuf == NULL)
  258. {
  259. return AVCDEC_MEMORY_FAIL;
  260. }
  261. memset(video->decPicBuf, 0, sizeof(AVCDecPicBuffer));
  262. }
  263. /* Decode SPS, allocate video->seqParams[i] and assign video->currSeqParams */
  264. status = DecodeSPS(decvid, bitstream);
  265. if (status != AVCDEC_SUCCESS)
  266. {
  267. return status;
  268. }
  269. return AVCDEC_SUCCESS;
  270. }
  271. /* ======================================================================== */
  272. /* Function : PVAVCDecGetSeqInfo() */
  273. /* Date : 11/4/2003 */
  274. /* Purpose : Get sequence parameter info. after SPS NAL is decoded. */
  275. /* In/out : */
  276. /* Return : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail. */
  277. /* Modified : */
  278. /* 12/20/03: change input argument, use structure instead. */
  279. /* ======================================================================== */
  280. OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetSeqInfo(AVCHandle *avcHandle, AVCDecSPSInfo *seqInfo)
  281. {
  282. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  283. AVCCommonObj *video;
  284. int PicWidthInMbs, PicHeightInMapUnits, FrameHeightInMbs;
  285. if (decvid == NULL || decvid->seqParams[0] == NULL)
  286. {
  287. return AVCDEC_FAIL;
  288. }
  289. video = decvid->common;
  290. PicWidthInMbs = decvid->seqParams[0]->pic_width_in_mbs_minus1 + 1;
  291. PicHeightInMapUnits = decvid->seqParams[0]->pic_height_in_map_units_minus1 + 1 ;
  292. FrameHeightInMbs = (2 - decvid->seqParams[0]->frame_mbs_only_flag) * PicHeightInMapUnits ;
  293. seqInfo->FrameWidth = PicWidthInMbs << 4;
  294. seqInfo->FrameHeight = FrameHeightInMbs << 4;
  295. seqInfo->frame_only_flag = decvid->seqParams[0]->frame_mbs_only_flag;
  296. if (decvid->seqParams[0]->frame_cropping_flag)
  297. {
  298. seqInfo->frame_crop_left = 2 * decvid->seqParams[0]->frame_crop_left_offset;
  299. seqInfo->frame_crop_right = seqInfo->FrameWidth - (2 * decvid->seqParams[0]->frame_crop_right_offset + 1);
  300. if (seqInfo->frame_only_flag)
  301. {
  302. seqInfo->frame_crop_top = 2 * decvid->seqParams[0]->frame_crop_top_offset;
  303. seqInfo->frame_crop_bottom = seqInfo->FrameHeight - (2 * decvid->seqParams[0]->frame_crop_bottom_offset + 1);
  304. /* Note in 7.4.2.1, there is a contraint on the value of frame_crop_left and frame_crop_top
  305. such that they have to be less than or equal to frame_crop_right/2 and frame_crop_bottom/2, respectively. */
  306. }
  307. else
  308. {
  309. seqInfo->frame_crop_top = 4 * decvid->seqParams[0]->frame_crop_top_offset;
  310. seqInfo->frame_crop_bottom = seqInfo->FrameHeight - (4 * decvid->seqParams[0]->frame_crop_bottom_offset + 1);
  311. /* Note in 7.4.2.1, there is a contraint on the value of frame_crop_left and frame_crop_top
  312. such that they have to be less than or equal to frame_crop_right/2 and frame_crop_bottom/4, respectively. */
  313. }
  314. }
  315. else /* no cropping flag, just give the first and last pixel */
  316. {
  317. seqInfo->frame_crop_bottom = seqInfo->FrameHeight - 1;
  318. seqInfo->frame_crop_right = seqInfo->FrameWidth - 1;
  319. seqInfo->frame_crop_top = seqInfo->frame_crop_left = 0;
  320. }
  321. return AVCDEC_SUCCESS;
  322. }
  323. /* ======================================================================== */
  324. /* Function : PVAVCDecPicParamSet() */
  325. /* Date : 11/4/2003 */
  326. /* Purpose : Initialize picture */
  327. /* create reference picture list. */
  328. /* In/out : */
  329. /* Return : AVCDEC_SUCCESS if succeed, AVC_FAIL if fail. */
  330. /* Modified : */
  331. /* ======================================================================== */
  332. /**
  333. Since PPS doesn't contain much data, most of the picture initialization will
  334. be done after decoding the slice header in PVAVCDecodeSlice. */
  335. OSCL_EXPORT_REF AVCDec_Status PVAVCDecPicParamSet(AVCHandle *avcHandle, uint8 *nal_unit,
  336. int nal_size)
  337. {
  338. AVCDec_Status status;
  339. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  340. AVCCommonObj *video;
  341. AVCDecBitstream *bitstream;
  342. if (decvid == NULL)
  343. {
  344. return AVCDEC_FAIL;
  345. }
  346. video = decvid->common;
  347. bitstream = decvid->bitstream;
  348. /* 1. Convert EBSP to RBSP. Create bitstream structure */
  349. video->forbidden_bit = nal_unit[0] >> 7;
  350. video->nal_ref_idc = (nal_unit[0] & 0x60) >> 5;
  351. video->nal_unit_type = (AVCNalUnitType)(nal_unit[0] & 0x1F);
  352. if (video->nal_unit_type != AVC_NALTYPE_PPS) /* not a PPS NAL */
  353. {
  354. return AVCDEC_FAIL;
  355. }
  356. /* 2. Initialize bitstream structure*/
  357. BitstreamInit(bitstream, nal_unit + 1, nal_size - 1);
  358. /* 2. Decode pic_parameter_set_rbsp syntax. Allocate video->picParams[i] and assign to currPicParams */
  359. status = DecodePPS(decvid, video, bitstream);
  360. if (status != AVCDEC_SUCCESS)
  361. {
  362. return status;
  363. }
  364. video->SliceGroupChangeRate = video->currPicParams->slice_group_change_rate_minus1 + 1 ;
  365. return AVCDEC_SUCCESS;
  366. }
  367. OSCL_EXPORT_REF AVCDec_Status PVAVCDecSEI(AVCHandle *avcHandle, uint8 *nal_unit,
  368. int nal_size)
  369. {
  370. OSCL_UNUSED_ARG(avcHandle);
  371. OSCL_UNUSED_ARG(nal_unit);
  372. OSCL_UNUSED_ARG(nal_size);
  373. return AVCDEC_SUCCESS;
  374. }
  375. /* ======================================================================== */
  376. /* Function : PVAVCDecodeSlice() */
  377. /* Date : 11/4/2003 */
  378. /* Purpose : Decode one NAL unit. */
  379. /* In/out : */
  380. /* Return : See enum AVCDec_Status for return values. */
  381. /* Modified : */
  382. /* ======================================================================== */
  383. OSCL_EXPORT_REF AVCDec_Status PVAVCDecodeSlice(AVCHandle *avcHandle, uint8 *buffer,
  384. int buf_size)
  385. {
  386. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  387. AVCCommonObj *video;
  388. AVCDecBitstream *bitstream;
  389. AVCDec_Status status;
  390. if (decvid == NULL)
  391. {
  392. return AVCDEC_FAIL;
  393. }
  394. video = decvid->common;
  395. bitstream = decvid->bitstream;
  396. if (video->mem_mgr_ctrl_eq_5)
  397. {
  398. return AVCDEC_PICTURE_OUTPUT_READY; // to flushout frame buffers
  399. }
  400. if (video->newSlice)
  401. {
  402. /* 2. Check NAL type */
  403. if (buffer == NULL)
  404. {
  405. return AVCDEC_FAIL;
  406. }
  407. video->prev_nal_unit_type = video->nal_unit_type;
  408. video->forbidden_bit = buffer[0] >> 7;
  409. video->nal_ref_idc = (buffer[0] & 0x60) >> 5;
  410. video->nal_unit_type = (AVCNalUnitType)(buffer[0] & 0x1F);
  411. if (video->nal_unit_type == AVC_NALTYPE_AUD)
  412. {
  413. return AVCDEC_SUCCESS;
  414. }
  415. if (video->nal_unit_type != AVC_NALTYPE_SLICE &&
  416. video->nal_unit_type != AVC_NALTYPE_IDR)
  417. {
  418. return AVCDEC_FAIL; /* not supported */
  419. }
  420. if (video->nal_unit_type >= 2 && video->nal_unit_type <= 4)
  421. {
  422. return AVCDEC_FAIL; /* not supported */
  423. }
  424. else
  425. {
  426. video->slice_data_partitioning = FALSE;
  427. }
  428. video->newSlice = FALSE;
  429. /* Initialize bitstream structure*/
  430. BitstreamInit(bitstream, buffer + 1, buf_size - 1);
  431. /* 2.1 Decode Slice Header (separate function)*/
  432. status = DecodeSliceHeader(decvid, video, bitstream);
  433. if (status != AVCDEC_SUCCESS)
  434. {
  435. video->newSlice = TRUE;
  436. return status;
  437. }
  438. if (video->sliceHdr->frame_num != video->prevFrameNum || (video->sliceHdr->first_mb_in_slice < (uint)video->mbNum && video->currSeqParams->constrained_set1_flag == 1))
  439. {
  440. video->newPic = TRUE;
  441. if (video->numMBs > 0)
  442. {
  443. // Conceal missing MBs of previously decoded frame
  444. ConcealSlice(decvid, video->PicSizeInMbs - video->numMBs, video->PicSizeInMbs); // Conceal
  445. video->numMBs = 0;
  446. // DeblockPicture(video); // No need to deblock
  447. /* 3.2 Decoded frame reference marking. */
  448. /* 3.3 Put the decoded picture in output buffers */
  449. /* set video->mem_mge_ctrl_eq_5 */
  450. AVCNalUnitType temp = video->nal_unit_type;
  451. video->nal_unit_type = video->prev_nal_unit_type;
  452. StorePictureInDPB(avcHandle, video);
  453. video->nal_unit_type = temp;
  454. video->mbNum = 0; // MC_Conceal
  455. return AVCDEC_PICTURE_OUTPUT_READY;
  456. }
  457. }
  458. if (video->nal_unit_type == AVC_NALTYPE_IDR)
  459. {
  460. video->prevFrameNum = 0;
  461. video->PrevRefFrameNum = 0;
  462. }
  463. if (!video->currSeqParams->gaps_in_frame_num_value_allowed_flag)
  464. { /* no gaps allowed, frame_num has to increase by one only */
  465. /* if(sliceHdr->frame_num != (video->PrevRefFrameNum + 1)%video->MaxFrameNum) */
  466. if (video->sliceHdr->frame_num != video->PrevRefFrameNum && video->sliceHdr->frame_num != (video->PrevRefFrameNum + 1) % video->MaxFrameNum)
  467. {
  468. // Conceal missing MBs of previously decoded frame
  469. video->numMBs = 0;
  470. video->newPic = TRUE;
  471. video->prevFrameNum++; // FIX
  472. video->PrevRefFrameNum++;
  473. AVCNalUnitType temp = video->nal_unit_type;
  474. video->nal_unit_type = AVC_NALTYPE_SLICE; //video->prev_nal_unit_type;
  475. status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
  476. if (status != AVCDEC_SUCCESS)
  477. {
  478. return status;
  479. }
  480. video->currFS->IsOutputted = 0x01;
  481. video->currFS->IsReference = 3;
  482. video->currFS->IsLongTerm = 0;
  483. DecodePOC(video);
  484. /* find an empty memory from DPB and assigned to currPic */
  485. DPBInitPic(video, video->PrevRefFrameNum % video->MaxFrameNum);
  486. RefListInit(video);
  487. ConcealSlice(decvid, 0, video->PicSizeInMbs); // Conceal
  488. video->currFS->IsOutputted |= 0x02;
  489. //conceal frame
  490. /* 3.2 Decoded frame reference marking. */
  491. /* 3.3 Put the decoded picture in output buffers */
  492. /* set video->mem_mge_ctrl_eq_5 */
  493. video->mbNum = 0; // Conceal
  494. StorePictureInDPB(avcHandle, video);
  495. video->nal_unit_type = temp;
  496. return AVCDEC_PICTURE_OUTPUT_READY;
  497. }
  498. }
  499. }
  500. if (video->newPic == TRUE)
  501. {
  502. status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
  503. if (status != AVCDEC_SUCCESS)
  504. {
  505. return status;
  506. }
  507. }
  508. video->newSlice = TRUE;
  509. /* function pointer setting at slice-level */
  510. // OPTIMIZE
  511. decvid->residual_block = &residual_block_cavlc;
  512. /* derive picture order count */
  513. if (video->newPic == TRUE)
  514. {
  515. video->numMBs = video->PicSizeInMbs;
  516. if (video->nal_unit_type != AVC_NALTYPE_IDR && video->currSeqParams->gaps_in_frame_num_value_allowed_flag)
  517. {
  518. if (video->sliceHdr->frame_num != (video->PrevRefFrameNum + 1) % video->MaxFrameNum)
  519. {
  520. status = fill_frame_num_gap(avcHandle, video);
  521. if (status != AVCDEC_SUCCESS)
  522. {
  523. video->numMBs = 0;
  524. return status;
  525. }
  526. status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
  527. if (status != AVCDEC_SUCCESS)
  528. {
  529. video->numMBs = 0;
  530. return status;
  531. }
  532. }
  533. }
  534. /* if there's gap in the frame_num, we have to fill in the gap with
  535. imaginary frames that won't get used for short-term ref. */
  536. /* see fill_frame_num_gap() in JM */
  537. DecodePOC(video);
  538. /* find an empty memory from DPB and assigned to currPic */
  539. DPBInitPic(video, video->CurrPicNum);
  540. video->currPic->isReference = TRUE; // FIX
  541. if (video->nal_ref_idc == 0)
  542. {
  543. video->currPic->isReference = FALSE;
  544. video->currFS->IsOutputted |= 0x02; /* The MASK 0x02 means not needed for reference, or returned */
  545. /* node need to check for freeing of this buffer */
  546. }
  547. FMOInit(video);
  548. if (video->currPic->isReference)
  549. {
  550. video->PrevRefFrameNum = video->sliceHdr->frame_num;
  551. }
  552. video->prevFrameNum = video->sliceHdr->frame_num;
  553. }
  554. video->newPic = FALSE;
  555. /* Initialize refListIdx for this picture */
  556. RefListInit(video);
  557. /* Re-order the reference list according to the ref_pic_list_reordering() */
  558. status = (AVCDec_Status)ReOrderList(video);
  559. if (status != AVCDEC_SUCCESS)
  560. {
  561. return AVCDEC_FAIL;
  562. }
  563. /* 2.2 Decode Slice. */
  564. status = (AVCDec_Status)DecodeSlice(decvid);
  565. video->slice_id++; // slice
  566. if (status == AVCDEC_PICTURE_READY)
  567. {
  568. /* 3. Check complete picture */
  569. #ifndef MB_BASED_DEBLOCK
  570. /* 3.1 Deblock */
  571. DeblockPicture(video);
  572. #endif
  573. /* 3.2 Decoded frame reference marking. */
  574. /* 3.3 Put the decoded picture in output buffers */
  575. /* set video->mem_mge_ctrl_eq_5 */
  576. status = (AVCDec_Status)StorePictureInDPB(avcHandle, video); // CHECK check the retunr status
  577. if (status != AVCDEC_SUCCESS)
  578. {
  579. return AVCDEC_FAIL;
  580. }
  581. if (video->mem_mgr_ctrl_eq_5)
  582. {
  583. video->PrevRefFrameNum = 0;
  584. video->prevFrameNum = 0;
  585. video->prevPicOrderCntMsb = 0;
  586. video->prevPicOrderCntLsb = video->TopFieldOrderCnt;
  587. video->prevFrameNumOffset = 0;
  588. }
  589. else
  590. {
  591. video->prevPicOrderCntMsb = video->PicOrderCntMsb;
  592. video->prevPicOrderCntLsb = video->sliceHdr->pic_order_cnt_lsb;
  593. video->prevFrameNumOffset = video->FrameNumOffset;
  594. }
  595. return AVCDEC_PICTURE_READY;
  596. }
  597. else if (status != AVCDEC_SUCCESS)
  598. {
  599. return AVCDEC_FAIL;
  600. }
  601. return AVCDEC_SUCCESS;
  602. }
  603. /* ======================================================================== */
  604. /* Function : PVAVCDecGetOutput() */
  605. /* Date : 11/3/2003 */
  606. /* Purpose : Get the next picture according to PicOrderCnt. */
  607. /* In/out : */
  608. /* Return : AVCFrameIO structure */
  609. /* Modified : */
  610. /* ======================================================================== */
  611. OSCL_EXPORT_REF AVCDec_Status PVAVCDecGetOutput(AVCHandle *avcHandle, int *indx, int *release, AVCFrameIO *output)
  612. {
  613. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  614. AVCCommonObj *video;
  615. AVCDecPicBuffer *dpb;
  616. AVCFrameStore *oldestFrame = NULL;
  617. int i, first = 1;
  618. int count_frame = 0;
  619. int index = 0;
  620. int min_poc = 0;
  621. if (decvid == NULL)
  622. {
  623. return AVCDEC_FAIL;
  624. }
  625. video = decvid->common;
  626. dpb = video->decPicBuf;
  627. if (dpb->num_fs == 0)
  628. {
  629. return AVCDEC_FAIL;
  630. }
  631. /* search for the oldest frame_num in dpb */
  632. /* extension to field decoding, we have to search for every top_field/bottom_field within
  633. each frame in the dpb. This code only works for frame based.*/
  634. if (video->mem_mgr_ctrl_eq_5 == FALSE)
  635. {
  636. for (i = 0; i < dpb->num_fs; i++)
  637. {
  638. if ((dpb->fs[i]->IsOutputted & 0x01) == 0)
  639. {
  640. count_frame++;
  641. if (first)
  642. {
  643. min_poc = dpb->fs[i]->PicOrderCnt;
  644. first = 0;
  645. oldestFrame = dpb->fs[i];
  646. index = i;
  647. }
  648. if (dpb->fs[i]->PicOrderCnt < min_poc)
  649. {
  650. min_poc = dpb->fs[i]->PicOrderCnt;
  651. oldestFrame = dpb->fs[i];
  652. index = i;
  653. }
  654. }
  655. }
  656. }
  657. else
  658. {
  659. for (i = 0; i < dpb->num_fs; i++)
  660. {
  661. if ((dpb->fs[i]->IsOutputted & 0x01) == 0 && dpb->fs[i] != video->currFS)
  662. {
  663. count_frame++;
  664. if (first)
  665. {
  666. min_poc = dpb->fs[i]->PicOrderCnt;
  667. first = 0;
  668. oldestFrame = dpb->fs[i];
  669. index = i;
  670. }
  671. if (dpb->fs[i]->PicOrderCnt < min_poc)
  672. {
  673. min_poc = dpb->fs[i]->PicOrderCnt;
  674. oldestFrame = dpb->fs[i];
  675. index = i;
  676. }
  677. }
  678. }
  679. if (count_frame < 2 && video->nal_unit_type != AVC_NALTYPE_IDR)
  680. {
  681. video->mem_mgr_ctrl_eq_5 = FALSE; // FIX
  682. }
  683. else if (count_frame < 1 && video->nal_unit_type == AVC_NALTYPE_IDR)
  684. {
  685. for (i = 0; i < dpb->num_fs; i++)
  686. {
  687. if (dpb->fs[i] == video->currFS && (dpb->fs[i]->IsOutputted & 0x01) == 0)
  688. {
  689. oldestFrame = dpb->fs[i];
  690. index = i;
  691. break;
  692. }
  693. }
  694. video->mem_mgr_ctrl_eq_5 = FALSE;
  695. }
  696. }
  697. if (oldestFrame == NULL)
  698. {
  699. /* Check for Mem_mgmt_operation_5 based forced output */
  700. for (i = 0; i < dpb->num_fs; i++)
  701. {
  702. /* looking for the one not used or not reference and has been outputted */
  703. if (dpb->fs[i]->IsReference == 0 && dpb->fs[i]->IsOutputted == 3)
  704. {
  705. break;
  706. }
  707. }
  708. if (i < dpb->num_fs)
  709. {
  710. /* there are frames available for decoding */
  711. return AVCDEC_FAIL; /* no frame to be outputted */
  712. }
  713. /* no free frame available, we have to release one to continue decoding */
  714. int MinIdx = 0;
  715. int32 MinFrameNumWrap = 0x7FFFFFFF;
  716. for (i = 0; i < dpb->num_fs; i++)
  717. {
  718. if (dpb->fs[i]->IsReference && !dpb->fs[i]->IsLongTerm)
  719. {
  720. if (dpb->fs[i]->FrameNumWrap < MinFrameNumWrap)
  721. {
  722. MinFrameNumWrap = dpb->fs[i]->FrameNumWrap;
  723. MinIdx = i;
  724. }
  725. }
  726. }
  727. /* mark the frame with smallest PicOrderCnt to be unused for reference */
  728. dpb->fs[MinIdx]->IsReference = 0;
  729. dpb->fs[MinIdx]->IsLongTerm = 0;
  730. dpb->fs[MinIdx]->frame.isReference = FALSE;
  731. dpb->fs[MinIdx]->frame.isLongTerm = FALSE;
  732. dpb->fs[MinIdx]->IsOutputted |= 0x02;
  733. #ifdef PV_MEMORY_POOL
  734. if (dpb->fs[MinIdx]->IsOutputted == 3)
  735. {
  736. avcHandle->CBAVC_FrameUnbind(avcHandle->userData, MinIdx);
  737. }
  738. #endif
  739. return AVCDEC_FAIL;
  740. }
  741. /* MASK 0x01 means the frame is outputted (for display). A frame gets freed when it is
  742. outputted (0x01) and not needed for reference (0x02) */
  743. oldestFrame->IsOutputted |= 0x01;
  744. if (oldestFrame->IsOutputted == 3)
  745. {
  746. *release = 1; /* flag to release the buffer */
  747. }
  748. else
  749. {
  750. *release = 0;
  751. }
  752. /* do not release buffer here, release it after it is sent to the sink node */
  753. output->YCbCr[0] = oldestFrame->frame.Sl;
  754. output->YCbCr[1] = oldestFrame->frame.Scb;
  755. output->YCbCr[2] = oldestFrame->frame.Scr;
  756. output->height = oldestFrame->frame.height;
  757. output->pitch = oldestFrame->frame.width;
  758. output->disp_order = oldestFrame->PicOrderCnt;
  759. output->coding_order = oldestFrame->FrameNum;
  760. output->id = (uintptr_t) oldestFrame->base_dpb; /* use the pointer as the id */
  761. *indx = index;
  762. return AVCDEC_SUCCESS;
  763. }
  764. /* ======================================================================== */
  765. /* Function : PVAVCDecReset() */
  766. /* Date : 03/04/2004 */
  767. /* Purpose : Reset decoder, prepare it for a new IDR frame. */
  768. /* In/out : */
  769. /* Return : void */
  770. /* Modified : */
  771. /* ======================================================================== */
  772. OSCL_EXPORT_REF void PVAVCDecReset(AVCHandle *avcHandle)
  773. {
  774. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  775. AVCCommonObj *video;
  776. AVCDecPicBuffer *dpb;
  777. int i;
  778. if (decvid == NULL)
  779. {
  780. return;
  781. }
  782. video = decvid->common;
  783. dpb = video->decPicBuf;
  784. /* reset the DPB */
  785. for (i = 0; i < dpb->num_fs; i++)
  786. {
  787. dpb->fs[i]->IsLongTerm = 0;
  788. dpb->fs[i]->IsReference = 0;
  789. dpb->fs[i]->IsOutputted = 3;
  790. dpb->fs[i]->frame.isReference = 0;
  791. dpb->fs[i]->frame.isLongTerm = 0;
  792. }
  793. video->mem_mgr_ctrl_eq_5 = FALSE;
  794. video->newPic = TRUE;
  795. video->newSlice = TRUE;
  796. video->currPic = NULL;
  797. video->currFS = NULL;
  798. video->prevRefPic = NULL;
  799. video->prevFrameNum = 0;
  800. video->PrevRefFrameNum = 0;
  801. video->prevFrameNumOffset = 0;
  802. video->FrameNumOffset = 0;
  803. video->mbNum = 0;
  804. video->numMBs = 0;
  805. return ;
  806. }
  807. /* ======================================================================== */
  808. /* Function : PVAVCCleanUpDecoder() */
  809. /* Date : 11/4/2003 */
  810. /* Purpose : Clean up the decoder, free all memories allocated. */
  811. /* In/out : */
  812. /* Return : void */
  813. /* Modified : */
  814. /* ======================================================================== */
  815. OSCL_EXPORT_REF void PVAVCCleanUpDecoder(AVCHandle *avcHandle)
  816. {
  817. AVCDecObject *decvid = (AVCDecObject*) avcHandle->AVCObject;
  818. AVCCommonObj *video;
  819. void *userData = avcHandle->userData;
  820. int i;
  821. DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "PVAVCCleanUpDecoder", -1, -1);
  822. if (decvid != NULL)
  823. {
  824. video = decvid->common;
  825. if (video != NULL)
  826. {
  827. if (video->MbToSliceGroupMap != NULL)
  828. {
  829. avcHandle->CBAVC_Free(userData, (uintptr_t)video->MbToSliceGroupMap);
  830. }
  831. #ifdef MB_BASED_DEBLOCK
  832. if (video->intra_pred_top != NULL)
  833. {
  834. avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top);
  835. }
  836. if (video->intra_pred_top_cb != NULL)
  837. {
  838. avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top_cb);
  839. }
  840. if (video->intra_pred_top_cr != NULL)
  841. {
  842. avcHandle->CBAVC_Free(userData, (int)video->intra_pred_top_cr);
  843. }
  844. #endif
  845. if (video->mblock != NULL)
  846. {
  847. avcHandle->CBAVC_Free(userData, (uintptr_t)video->mblock);
  848. }
  849. if (video->decPicBuf != NULL)
  850. {
  851. CleanUpDPB(avcHandle, video);
  852. avcHandle->CBAVC_Free(userData, (uintptr_t)video->decPicBuf);
  853. }
  854. if (video->sliceHdr != NULL)
  855. {
  856. avcHandle->CBAVC_Free(userData, (uintptr_t)video->sliceHdr);
  857. }
  858. avcHandle->CBAVC_Free(userData, (uintptr_t)video); /* last thing to do */
  859. }
  860. for (i = 0; i < 256; i++)
  861. {
  862. if (decvid->picParams[i] != NULL)
  863. {
  864. if (decvid->picParams[i]->slice_group_id != NULL)
  865. {
  866. avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->picParams[i]->slice_group_id);
  867. }
  868. avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->picParams[i]);
  869. }
  870. }
  871. for (i = 0; i < 32; i++)
  872. {
  873. if (decvid->seqParams[i] != NULL)
  874. {
  875. avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->seqParams[i]);
  876. }
  877. }
  878. if (decvid->bitstream != NULL)
  879. {
  880. avcHandle->CBAVC_Free(userData, (uintptr_t)decvid->bitstream);
  881. }
  882. avcHandle->CBAVC_Free(userData, (uintptr_t)decvid);
  883. }
  884. return ;
  885. }