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