PageRenderTime 175ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Show/avc/avcdec_api.cpp

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