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 #include "mp4dec_lib.h" 19 #include "bitstream.h" 20 #include "vlc_decode.h" 21 #include "zigzag.h" 22 23 #define OSCL_DISABLE_WARNING_CONV_POSSIBLE_LOSS_OF_DATA 24 #include "osclconfig_compiler_warnings.h" 25 26 #ifdef PV_SUPPORT_MAIN_PROFILE 27 /* INTRA */ 28 const static int mpeg_iqmat_def[NCOEFF_BLOCK] = 29 { 30 8, 17, 18, 19, 21, 23, 25, 27, 31 17, 18, 19, 21, 23, 25, 27, 28, 32 20, 21, 22, 23, 24, 26, 28, 30, 33 21, 22, 23, 24, 26, 28, 30, 32, 34 22, 23, 24, 26, 28, 30, 32, 35, 35 23, 24, 26, 28, 30, 32, 35, 38, 36 25, 26, 28, 30, 32, 35, 38, 41, 37 27, 28, 30, 32, 35, 38, 41, 45 38 }; 39 40 /* INTER */ 41 const static int mpeg_nqmat_def[64] = 42 { 43 16, 17, 18, 19, 20, 21, 22, 23, 44 17, 18, 19, 20, 21, 22, 23, 24, 45 18, 19, 20, 21, 22, 23, 24, 25, 46 19, 20, 21, 22, 23, 24, 26, 27, 47 20, 21, 22, 23, 25, 26, 27, 28, 48 21, 22, 23, 24, 26, 27, 28, 30, 49 22, 23, 24, 26, 27, 28, 30, 31, 50 23, 24, 25, 27, 28, 30, 31, 33 51 }; 52 #endif 53 54 /* ======================================================================== */ 55 /* Function : CalcNumBits() */ 56 /* Purpose : */ 57 /* In/out : */ 58 /* Return : Calculate the minimum number of bits required to */ 59 /* represent x. */ 60 /* Note : This is an equivalent implementation of */ 61 /* (long)ceil(log((double)x)/log(2.0)) */ 62 /* Modified : */ 63 /* ======================================================================== */ 64 int CalcNumBits(uint x) 65 { 66 int i = 1; 67 while (x >>= 1) i++; 68 return i; 69 } 70 71 72 73 /***********************************************************CommentBegin****** 74 * 75 * -- DecodeVolHeader -- Decode the header of a VOL 76 * 77 * 04/10/2000 : initial modification to the new PV-Decoder Lib format. 78 * 10/12/2001 : reject non compliant bitstreams 79 * 80 ***********************************************************CommentEnd********/ 81 PV_STATUS DecodeVOLHeader(VideoDecData *video, int layer) 82 { 83 PV_STATUS status; 84 Vol *currVol; 85 BitstreamDecVideo *stream; 86 uint32 tmpvar, vol_shape; 87 uint32 startCode; 88 #ifdef PV_SUPPORT_MAIN_PROFILE 89 int *qmat, i, j; 90 #endif 91 int version_id = 1; 92 #ifdef PV_TOLERATE_VOL_ERRORS 93 uint32 profile = 0x01; 94 #endif 95 /* There's a "currLayer" variable inside videoDecData. */ 96 /* However, we don't maintain it until we decode frame data. 04/05/2000 */ 97 currVol = video->vol[layer]; 98 stream = currVol->bitstream; 99 currVol->moduloTimeBase = 0; 100 101 /* Determine which start code for the decoder to begin with */ 102 status = BitstreamShowBits32HC(stream, &startCode); 103 104 if (startCode == VISUAL_OBJECT_SEQUENCE_START_CODE) 105 { /* Bitstream Exhchange Fix 9/99 */ 106 /* Bitstream Exchange requires we allow start with Video Object Sequence */ 107 /* visual_object_sequence_start_code */ 108 (void) BitstreamReadBits32HC(stream); 109 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* profile */ 110 #ifndef PV_TOLERATE_VOL_ERRORS 111 if (layer) /* */ 112 { 113 /* support SSPL0-2 */ 114 if (tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 && 115 tmpvar != 0xA1 && tmpvar != 0xA2 && tmpvar != 0xA3/* Core SP@L1-L3 */) 116 return PV_FAIL; 117 } 118 else 119 { 120 /* support SPL0-3 & SSPL0-2 */ 121 if (tmpvar != 0x01 && tmpvar != 0x02 && tmpvar != 0x03 && tmpvar != 0x08 && 122 tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 && 123 tmpvar != 0x21 && tmpvar != 0x22 && /* Core Profile Levels */ 124 tmpvar != 0xA1 && tmpvar != 0xA2 && tmpvar != 0xA3 && 125 tmpvar != 0xF0 && tmpvar != 0xF1 && /* Advanced Simple Profile Levels*/ 126 tmpvar != 0xF2 && tmpvar != 0xF3 && 127 tmpvar != 0xF4 && tmpvar != 0xF5) 128 return PV_FAIL; 129 } 130 #else 131 profile = tmpvar; 132 #endif 133 134 // save the profile and level for the query 135 currVol->profile_level_id = (uint)tmpvar; // 6/10/04 136 137 138 139 status = BitstreamShowBits32HC(stream, &tmpvar); 140 if (tmpvar == USER_DATA_START_CODE) 141 { 142 /* Something has to be done with user data 11/11/99 */ 143 status = DecodeUserData(stream); 144 if (status != PV_SUCCESS) return PV_FAIL; 145 } 146 /* visual_object_start_code */ 147 BitstreamShowBits32HC(stream, &tmpvar); 148 if (tmpvar != VISUAL_OBJECT_START_CODE) 149 { 150 do 151 { 152 /* Search for VOL_HEADER */ 153 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */ 154 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */ 155 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar); 156 PV_BitstreamFlushBits(stream, 8); 157 } 158 while (tmpvar != VOL_START_CODE); 159 goto decode_vol; 160 } 161 else 162 { 163 BitstreamReadBits32HC(stream); 164 } 165 166 /* is_visual_object_identifier */ 167 tmpvar = (uint32) BitstreamRead1Bits(stream); 168 if (tmpvar) 169 { 170 /* visual_object_verid */ 171 tmpvar = (uint32) BitstreamReadBits16(stream, 4); 172 /* visual_object_priority */ 173 tmpvar = (uint32) BitstreamReadBits16(stream, 3); 174 } 175 /* visual_object_type */ 176 BitstreamShowBits32(stream, 4, &tmpvar); 177 if (tmpvar == 1) 178 { /* video_signal_type */ 179 PV_BitstreamFlushBits(stream, 4); 180 tmpvar = (uint32) BitstreamRead1Bits(stream); 181 if (tmpvar == 1) 182 { 183 /* video_format */ 184 tmpvar = (uint32) BitstreamReadBits16(stream, 3); 185 /* video_range */ 186 tmpvar = (uint32) BitstreamRead1Bits(stream); 187 /* color_description */ 188 tmpvar = (uint32) BitstreamRead1Bits(stream); 189 if (tmpvar == 1) 190 { 191 /* color_primaries */ 192 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 193 /* transfer_characteristics */ 194 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 195 /* matrix_coefficients */ 196 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 197 } 198 } 199 } 200 else 201 { 202 do 203 { 204 /* Search for VOL_HEADER */ 205 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */ 206 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */ 207 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar); 208 PV_BitstreamFlushBits(stream, 8); 209 } 210 while (tmpvar != VOL_START_CODE); 211 goto decode_vol; 212 } 213 214 /* next_start_code() */ 215 status = PV_BitstreamByteAlign(stream); /* 10/12/01 */ 216 status = BitstreamShowBits32HC(stream, &tmpvar); 217 218 if (tmpvar == USER_DATA_START_CODE) 219 { 220 /* Something has to be done to deal with user data (parse it) 11/11/99 */ 221 status = DecodeUserData(stream); 222 if (status != PV_SUCCESS) return PV_FAIL; 223 } 224 status = BitstreamShowBits32(stream, 27, &tmpvar); /* 10/12/01 */ 225 } 226 else 227 { 228 /* tmpvar = 0; */ /* 10/12/01 */ 229 status = BitstreamShowBits32(stream, 27, &tmpvar); /* uncomment this line if you want 230 to start decoding with a 231 video_object_start_code */ 232 } 233 234 if (tmpvar == VO_START_CODE) 235 { 236 /***** 237 * 238 * Read the VOL header entries from the bitstream 239 * 240 *****/ 241 /* video_object_start_code */ 242 tmpvar = BitstreamReadBits32(stream, 27); 243 tmpvar = (uint32) BitstreamReadBits16(stream, 5); 244 245 246 /* video_object_layer_start_code */ 247 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar); 248 if (tmpvar != VOL_START_CODE) 249 { 250 status = BitstreamCheckEndBuffer(stream); 251 if (status == PV_END_OF_VOP) 252 { 253 video->shortVideoHeader = TRUE; 254 return PV_SUCCESS; 255 } 256 else 257 { 258 do 259 { 260 /* Search for VOL_HEADER */ 261 status = PVSearchNextM4VFrame(stream);/* search 0x00 0x00 0x01 */ 262 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */ 263 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar); 264 PV_BitstreamFlushBits(stream, 8); /* advance the byte ptr */ 265 } 266 while (tmpvar != VOL_START_CODE); 267 } 268 } 269 else 270 { 271 PV_BitstreamFlushBits(stream, 8); 272 } 273 274 decode_vol: 275 PV_BitstreamFlushBits(stream, VOL_START_CODE_LENGTH - 8); 276 video->shortVideoHeader = 0; 277 278 /* vol_id (4 bits) */ 279 currVol->volID = (int) BitstreamReadBits16(stream, 4); 280 281 /* RandomAccessible flag */ 282 tmpvar = (uint32) BitstreamRead1Bits(stream); 283 284 /* object type */ 285 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* */ 286 287 #ifdef PV_TOLERATE_VOL_ERRORS 288 if (tmpvar == 0) 289 { 290 if (layer) /* */ 291 { 292 /* support SSPL0-2 */ 293 if (profile != 0x10 && profile != 0x11 && profile != 0x12) 294 return PV_FAIL; 295 tmpvar = 0x02; 296 } 297 else 298 { 299 /* support SPL0-3 & SSPL0-2 */ 300 if (profile != 0x01 && profile != 0x02 && profile != 0x03 && profile != 0x08 && 301 profile != 0x10 && profile != 0x11 && profile != 0x12) 302 return PV_FAIL; 303 tmpvar = 0x01; 304 } 305 profile |= 0x0100; 306 } 307 #endif 308 309 if (layer) 310 { 311 if (tmpvar != 0x02) return PV_FAIL; 312 } 313 else 314 { 315 if (tmpvar != 0x01) return PV_FAIL; 316 } 317 318 /* version id specified? */ 319 tmpvar = (uint32) BitstreamRead1Bits(stream); 320 if (tmpvar == 1) 321 { 322 /* version ID */ 323 version_id = (uint32) BitstreamReadBits16(stream, 4); 324 /* priority */ 325 tmpvar = (uint32) BitstreamReadBits16(stream, 3); 326 327 } 328 329 /* aspect ratio info */ 330 tmpvar = (uint32) BitstreamReadBits16(stream, 4); 331 if (tmpvar == 0) return PV_FAIL; 332 if (tmpvar == 0xf /* extended_par */) 333 { 334 /* width */ 335 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 336 /* height */ 337 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 338 } 339 340 341 /* control parameters present? */ 342 tmpvar = (uint32) BitstreamRead1Bits(stream); 343 344 /* Get the parameters (skipped) */ 345 /* 03/10/99 */ 346 if (tmpvar) 347 { 348 /* chroma_format */ 349 tmpvar = BitstreamReadBits16(stream, 2); 350 if (tmpvar != 1) return PV_FAIL; 351 /* low_delay */ 352 tmpvar = BitstreamRead1Bits(stream); 353 354 /* vbv_parameters present? */ 355 tmpvar = (uint32) BitstreamRead1Bits(stream); 356 if (tmpvar) 357 { 358 /* first_half_bit_rate */ 359 BitstreamReadBits16(stream, 15); 360 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 361 /* latter_half_bit_rate */ 362 BitstreamReadBits16(stream, 15); 363 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 364 /* first_half_vbv_buffer_size */ 365 BitstreamReadBits16(stream, 15); 366 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 367 /* latter_half_vbv_buffer_size */ 368 BitstreamReadBits16(stream, 3); 369 /* first_half_vbv_occupancy */ 370 BitstreamReadBits16(stream, 11); 371 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 372 /* latter_half_vbv_occupancy */ 373 BitstreamReadBits16(stream, 15); 374 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 375 } 376 } 377 378 /* video_object_layer_shape (2 bits), only 00 (rect) is supported for now */ 379 vol_shape = (uint32) BitstreamReadBits16(stream, 2); 380 if (vol_shape) return PV_FAIL; 381 382 /* marker bit, 03/10/99 */ 383 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 384 385 /* vop_time_increment_resolution */ 386 currVol->timeIncrementResolution = BitstreamReadBits16(stream, 16); 387 if (currVol->timeIncrementResolution == 0) return PV_FAIL; 388 389 /* . since nbitsTimeIncRes will be used over and over again, */ 390 /* we should put it in Vol structure. 04/12/2000. */ 391 currVol->nbitsTimeIncRes = CalcNumBits((uint)currVol->timeIncrementResolution - 1); 392 393 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 394 395 /* fixed_vop_rate */ 396 currVol->fixedVopRate = (int) BitstreamRead1Bits(stream); 397 if (currVol->fixedVopRate) 398 { 399 /* fixed_vop_time_increment */ 400 tmpvar = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes); 401 } 402 403 /* marker bit */ 404 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 405 406 /* video_object_layer_width (13 bits) */ 407 video->displayWidth = video->width = (int) BitstreamReadBits16(stream, 13); 408 409 /* round up to a multiple of MB_SIZE. 08/09/2000 */ 410 video->width = (video->width + 15) & -16; 411 // video->displayWidth += (video->displayWidth & 0x1); /* displayed image should be even size */ 412 413 /* marker bit */ 414 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 415 416 /* video_object_layer_height (13 bits) */ 417 video->displayHeight = video->height = (int) BitstreamReadBits16(stream, 13); 418 419 /* round up to a multiple of MB_SIZE. 08/09/2000 */ 420 video->height = (video->height + 15) & -16; 421 // video->displayHeight += (video->displayHeight & 0x1); /* displayed image should be even size */ 422 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 423 424 /* 03/10/99 */ 425 /* interlaced */ 426 tmpvar = (uint32) BitstreamRead1Bits(stream); 427 if (tmpvar != 0) 428 { 429 mp4dec_log("DecodeVOLHeader(): Interlaced video is not supported.\n"); 430 return PV_FAIL; 431 } 432 433 /* obmc_disable */ 434 tmpvar = (uint32) BitstreamRead1Bits(stream); 435 if (tmpvar == 0) return PV_FAIL; 436 437 if (version_id == 1) 438 { 439 /* sprite_enable (1 bits) */ 440 tmpvar = (uint32) BitstreamRead1Bits(stream); 441 if (tmpvar) 442 { 443 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n"); 444 return PV_FAIL; 445 } 446 } 447 else 448 { 449 /* For version 2, vol_sprite_usage has two bits. */ 450 /* sprite_enable */ 451 tmpvar = (uint32) BitstreamReadBits16(stream, 2); 452 if (tmpvar) 453 { 454 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n"); 455 return PV_FAIL; 456 } 457 } 458 459 /* not_8_bit */ 460 if (BitstreamRead1Bits(stream)) 461 { 462 /* quant_precision */ 463 currVol->quantPrecision = BitstreamReadBits16(stream, 4); 464 /* bits_per_pixel */ 465 currVol->bitsPerPixel = BitstreamReadBits16(stream, 4); 466 mp4dec_log("DecodeVOLHeader(): not an 8-bit stream.\n"); // For the time being we do not support != 8 bits 467 468 return PV_FAIL; 469 } 470 else 471 { 472 currVol->quantPrecision = 5; 473 currVol->bitsPerPixel = 8; 474 } 475 476 /* quant_type (1 bit) */ 477 currVol->quantType = BitstreamRead1Bits(stream); 478 if (currVol->quantType) 479 { 480 #ifdef PV_SUPPORT_MAIN_PROFILE 481 /* load quantization matrices. 5/22/2000 */ 482 /* load_intra_quant_mat (1 bit) */ 483 qmat = currVol->iqmat; 484 currVol->loadIntraQuantMat = BitstreamRead1Bits(stream); 485 if (currVol->loadIntraQuantMat) 486 { 487 /* intra_quant_mat (8*64 bits) */ 488 i = 0; 489 do 490 { 491 qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8); 492 } 493 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64)); 494 495 for (j = i; j < 64; j++) 496 qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)]; 497 } 498 else 499 { 500 oscl_memcpy(qmat, mpeg_iqmat_def, 64*sizeof(int)); 501 } 502 503 qmat[0] = 0; /* necessary for switched && MPEG quant 07/09/01 */ 504 505 /* load_nonintra_quant_mat (1 bit) */ 506 qmat = currVol->niqmat; 507 currVol->loadNonIntraQuantMat = BitstreamRead1Bits(stream); 508 if (currVol->loadNonIntraQuantMat) 509 { 510 /* nonintra_quant_mat (8*64 bits) */ 511 i = 0; 512 do 513 { 514 qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8); 515 } 516 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64)); 517 518 for (j = i; j < 64; j++) 519 qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)]; 520 } 521 else 522 { 523 oscl_memcpy(qmat, mpeg_nqmat_def, 64*sizeof(int)); 524 } 525 #else 526 return PV_FAIL; 527 #endif 528 } 529 530 if (version_id != 1) 531 { 532 /* quarter_sample enabled */ 533 tmpvar = BitstreamRead1Bits(stream); 534 if (tmpvar) return PV_FAIL; 535 } 536 537 /* complexity_estimation_disable */ 538 currVol->complexity_estDisable = BitstreamRead1Bits(stream); 539 if (currVol->complexity_estDisable == 0) 540 { 541 currVol->complexity_estMethod = BitstreamReadBits16(stream, 2); 542 543 if (currVol->complexity_estMethod < 2) 544 { 545 /* shape_complexity_estimation_disable */ 546 tmpvar = BitstreamRead1Bits(stream); 547 if (tmpvar == 0) 548 { 549 mp4dec_log("DecodeVOLHeader(): Shape Complexity estimation is not supported.\n"); 550 return PV_FAIL; 551 } 552 /* texture_complexity_estimation_set_1_disable */ 553 tmpvar = BitstreamRead1Bits(stream); 554 if (tmpvar == 0) 555 { 556 currVol->complexity.text_1 = BitstreamReadBits16(stream, 4); 557 } 558 /* marker bit */ 559 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 560 /* texture_complexity_estimation_set_2_disable */ 561 tmpvar = BitstreamRead1Bits(stream); 562 if (tmpvar == 0) 563 { 564 currVol->complexity.text_2 = BitstreamReadBits16(stream, 4); 565 } 566 /* motion_compensation_complexity_disable */ 567 tmpvar = BitstreamRead1Bits(stream); 568 if (tmpvar == 0) 569 { 570 currVol->complexity.mc = BitstreamReadBits16(stream, 6); 571 } 572 /* marker bit */ 573 if (!BitstreamRead1Bits(stream)) return PV_FAIL; 574 575 if (currVol->complexity_estMethod == 1) 576 { /* version2_complexity_estimation_disable */ 577 tmpvar = BitstreamRead1Bits(stream); 578 if (tmpvar == 0) 579 { 580 mp4dec_log("DecodeVOLHeader(): sadct, quarter pel not supported.\n"); 581 return PV_FAIL; 582 } 583 } 584 } 585 } 586 587 /* 03/10/99 */ 588 /* resync_marker_disable */ 589 currVol->errorResDisable = (int) BitstreamRead1Bits(stream); 590 /* data_partititioned */ 591 currVol->dataPartitioning = (int) BitstreamRead1Bits(stream); 592 593 video->vlcDecCoeffIntra = &VlcDecTCOEFIntra; 594 video->vlcDecCoeffInter = &VlcDecTCOEFInter; 595 596 if (currVol->dataPartitioning) 597 { 598 if (layer) return PV_FAIL; /* */ 599 /* reversible_vlc */ 600 currVol->useReverseVLC = (int)BitstreamRead1Bits(stream); 601 if (currVol->useReverseVLC) 602 { 603 video->vlcDecCoeffIntra = &RvlcDecTCOEFIntra; 604 video->vlcDecCoeffInter = &RvlcDecTCOEFInter; 605 } 606 currVol->errorResDisable = 0; 607 } 608 else 609 { 610 currVol->useReverseVLC = 0; 611 } 612 613 if (version_id != 1) 614 { 615 /* newpred_enable */ 616 tmpvar = BitstreamRead1Bits(stream); 617 if (tmpvar) return PV_FAIL; 618 619 /* reduced_resolution_vop */ 620 tmpvar = BitstreamRead1Bits(stream); 621 if (tmpvar) return PV_FAIL; 622 623 } 624 625 /* Intra AC/DC prediction is always true */ 626 video->intra_acdcPredDisable = 0; 627 /* scalability */ 628 currVol->scalability = (int) BitstreamRead1Bits(stream); 629 630 if (currVol->scalability) 631 { 632 if (layer == 0) return PV_FAIL; /* */ 633 /* hierarchy_type: 1 : temporal, 0 : spatial */ 634 /* 03/10/99 */ 635 currVol->scalType = (int) BitstreamRead1Bits(stream); /* */ 636 if (!currVol->scalType) return PV_FAIL; 637 638 /* ref_layer_id (4 bits) */ 639 currVol->refVolID = (int) BitstreamReadBits16(stream, 4); 640 if (layer) /* */ 641 { 642 if (currVol->refVolID != video->vol[0]->volID) return PV_FAIL; 643 } 644 /* ref_layer_sampling_direc (1 bits) */ 645 /* 1 : ref. layer has higher resolution */ 646 /* 0 : ref. layer has equal or lower resolution */ 647 currVol->refSampDir = (int) BitstreamRead1Bits(stream); 648 if (currVol->refSampDir) return PV_FAIL; 649 650 /* hor_sampling_factor_n (5 bits) */ 651 currVol->horSamp_n = (int) BitstreamReadBits16(stream, 5); 652 653 /* hor_sampling_factor_m (5 bits) */ 654 currVol->horSamp_m = (int) BitstreamReadBits16(stream, 5); 655 656 if (currVol->horSamp_m == 0) return PV_FAIL; 657 if (currVol->horSamp_n != currVol->horSamp_m) return PV_FAIL; 658 659 /* ver_sampling_factor_n (5 bits) */ 660 currVol->verSamp_n = (int) BitstreamReadBits16(stream, 5); 661 662 /* ver_sampling_factor_m (5 bits) */ 663 currVol->verSamp_m = (int) BitstreamReadBits16(stream, 5); 664 665 if (currVol->verSamp_m == 0) return PV_FAIL; 666 if (currVol->verSamp_n != currVol->verSamp_m) return PV_FAIL; 667 668 669 /* enhancement_type: 1 : partial region, 0 : full region */ 670 /* 04/10/2000: we only support full region enhancement layer. */ 671 if (BitstreamRead1Bits(stream)) return PV_FAIL; 672 } 673 674 PV_BitstreamByteAlign(stream); 675 676 status = BitstreamShowBits32HC(stream, &tmpvar); 677 678 /* if we hit the end of buffer, tmpvar == 0. 08/30/2000 */ 679 if (tmpvar == USER_DATA_START_CODE) 680 { 681 status = DecodeUserData(stream); 682 /* you should not check for status here 03/19/2002 */ 683 status = PV_SUCCESS; 684 } 685 686 /* Compute some convenience variables: 04/13/2000 */ 687 video->nMBPerRow = video->width / MB_SIZE; 688 video->nMBPerCol = video->height / MB_SIZE; 689 video->nTotalMB = video->nMBPerRow * video->nMBPerCol; 690 video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1); 691 #ifdef PV_ANNEX_IJKT_SUPPORT 692 video->modified_quant = 0; 693 video->advanced_INTRA = 0; 694 video->deblocking = 0; 695 video->slice_structure = 0; 696 #endif 697 } 698 else 699 { 700 /* SHORT_HEADER */ 701 status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar); 702 703 if (tmpvar == SHORT_VIDEO_START_MARKER) 704 { 705 video->shortVideoHeader = TRUE; 706 } 707 else 708 { 709 do 710 { 711 /* Search for VOL_HEADER */ 712 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */ 713 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */ 714 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar); 715 PV_BitstreamFlushBits(stream, 8); 716 } 717 while (tmpvar != VOL_START_CODE); 718 goto decode_vol; 719 } 720 } 721 #ifdef PV_TOLERATE_VOL_ERRORS 722 if (profile > 0xFF || profile == 0) 723 { 724 return PV_BAD_VOLHEADER; 725 } 726 #endif 727 728 return status; 729 } 730 731 732 /***********************************************************CommentBegin****** 733 * 734 * -- DecodeGOV -- Decodes the Group of VOPs from bitstream 735 * 736 * 04/20/2000 initial modification to the new PV-Decoder Lib format. 737 * 738 ***********************************************************CommentEnd********/ 739 PV_STATUS DecodeGOVHeader(BitstreamDecVideo *stream, uint32 *time_base) 740 { 741 uint32 tmpvar, time_s; 742 int closed_gov, broken_link; 743 744 /* group_start_code (32 bits) */ 745 // tmpvar = BitstreamReadBits32(stream, 32); 746 747 /* hours */ 748 tmpvar = (uint32) BitstreamReadBits16(stream, 5); 749 time_s = tmpvar * 3600; 750 751 /* minutes */ 752 tmpvar = (uint32) BitstreamReadBits16(stream, 6); 753 time_s += tmpvar * 60; 754 755 /* marker bit */ 756 tmpvar = (uint32) BitstreamRead1Bits(stream); 757 758 /* seconds */ 759 tmpvar = (uint32) BitstreamReadBits16(stream, 6); 760 time_s += tmpvar; 761 762 /* We have to check the timestamp here. If the sync timestamp is */ 763 /* earlier than the previous timestamp or longer than 60 sec. */ 764 /* after the previous timestamp, assume the GOV header is */ 765 /* corrupted. 05/12/2000 */ 766 *time_base = time_s; /* 02/27/2002 */ 767 // *time_base = *time_base/1000; 768 // tmpvar = time_s - *time_base; 769 // if (tmpvar <= 60) *time_base = time_s; 770 // else return PV_FAIL; 771 772 tmpvar = (uint32) BitstreamRead1Bits(stream); 773 closed_gov = tmpvar; 774 tmpvar = (uint32) BitstreamRead1Bits(stream); 775 broken_link = tmpvar; 776 777 if ((closed_gov == 0) && (broken_link == 1)) 778 { 779 return PV_SUCCESS; /* 03/15/2002 you can also return PV_FAIL */ 780 } 781 782 PV_BitstreamByteAlign(stream); 783 784 BitstreamShowBits32HC(stream, &tmpvar); 785 786 while (tmpvar == USER_DATA_START_CODE) /* 03/15/2002 */ 787 { 788 DecodeUserData(stream); 789 BitstreamShowBits32HC(stream, &tmpvar); 790 } 791 792 return PV_SUCCESS; 793 } 794 795 /***********************************************************CommentBegin****** 796 * 797 * -- DecodeVopHeader -- Decodes the VOPheader information from the bitstream 798 * 799 * 04/12/2000 Initial port to the new PV decoder library format. 800 * 05/10/2000 Error resilient decoding of vop header. 801 * 802 ***********************************************************CommentEnd********/ 803 PV_STATUS DecodeVOPHeader(VideoDecData *video, Vop *currVop, Bool use_ext_timestamp) 804 { 805 PV_STATUS status = PV_SUCCESS; 806 Vol *currVol = video->vol[video->currLayer]; 807 BitstreamDecVideo *stream = currVol->bitstream; 808 uint32 tmpvar; 809 int time_base; 810 811 /***** 812 * Read the VOP header from the bitstream (No shortVideoHeader Mode here!) 813 *****/ 814 BitstreamShowBits32HC(stream, &tmpvar); 815 816 /* check if we have a GOV header here. 08/30/2000 */ 817 if (tmpvar == GROUP_START_CODE) 818 { 819 tmpvar = BitstreamReadBits32HC(stream); 820 // rewindBitstream(stream, START_CODE_LENGTH); /* for backward compatibility */ 821 status = DecodeGOVHeader(stream, &tmpvar); 822 if (status != PV_SUCCESS) 823 { 824 return status; 825 } 826 // use_ext_timestamp = TRUE; /* 02/08/2002 */ 827 /* We should have a VOP header following the GOV header. 03/15/2001 */ 828 BitstreamShowBits32HC(stream, &tmpvar); 829 } 830 #ifdef PV_SUPPORT_TEMPORAL_SCALABILITY 831 currVop->timeStamp = -1; 832 #endif 833 if (tmpvar == VOP_START_CODE) 834 { 835 tmpvar = BitstreamReadBits32HC(stream); 836 } 837 else 838 { 839 PV_BitstreamFlushBits(stream, 8); // advance by a byte 840 status = PV_FAIL; 841 goto return_point; 842 } 843 844 845 846 /* vop_prediction_type (2 bits) */ 847 currVop->predictionType = (int) BitstreamReadBits16(stream, 2); 848 849 /* modulo_time_base (? bits) */ 850 time_base = -1; 851 do 852 { 853 time_base++; 854 tmpvar = (uint32) BitstreamRead1Bits(stream); 855 } 856 while (tmpvar == 1); 857 858 859 860 if (!use_ext_timestamp) 861 { 862 currVol->moduloTimeBase += 1000 * time_base; /* milliseconds based MTB 11/12/01 */ 863 } 864 865 /* marker_bit (1 bit) */ 866 if (!BitstreamRead1Bits(stream)) 867 { 868 status = PV_FAIL; 869 goto return_point; 870 } 871 872 /* vop_time_increment (1-15 bits) in Nov_Compliant (1-16 bits) */ 873 /* we always assumes fixed vop rate here */ 874 currVop->timeInc = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes); 875 876 877 /* marker_bit (1 bit) */ 878 if (!BitstreamRead1Bits(stream)) 879 { 880 status = PV_FAIL; 881 goto return_point; 882 } 883 884 /* vop_coded */ 885 currVop->vopCoded = (int) BitstreamRead1Bits(stream); 886 887 888 if (currVop->vopCoded == 0) 889 { 890 status = PV_SUCCESS; 891 goto return_point; 892 } 893 894 895 /* read vop_rounding_type */ 896 if (currVop->predictionType == P_VOP) 897 { 898 currVop->roundingType = (int) BitstreamRead1Bits(stream); 899 } 900 else 901 { 902 currVop->roundingType = 0; 903 } 904 905 if (currVol->complexity_estDisable == 0) 906 { 907 if (currVol->complexity_estMethod < 2) /* OCT 2002 */ 908 { 909 if ((currVol->complexity.text_1 >> 3) & 0x1) /* intra */ 910 BitstreamReadBits16(stream, 8); 911 if (currVol->complexity.text_1 & 0x1) /* not_coded */ 912 BitstreamReadBits16(stream, 8); 913 if ((currVol->complexity.text_2 >> 3) & 0x1) /* dct_coefs */ 914 BitstreamReadBits16(stream, 8); 915 if ((currVol->complexity.text_2 >> 2) & 0x1) /* dct_lines */ 916 BitstreamReadBits16(stream, 8); 917 if ((currVol->complexity.text_2 >> 1) & 0x1) /* vlc_symbols */ 918 BitstreamReadBits16(stream, 8); 919 if (currVol->complexity.text_2 & 0x1) /* vlc_bits */ 920 BitstreamReadBits16(stream, 4); 921 922 if (currVop->predictionType != I_VOP) 923 { 924 if ((currVol->complexity.text_1 >> 2) & 0x1) /* inter */ 925 BitstreamReadBits16(stream, 8); 926 if ((currVol->complexity.text_1 >> 1) & 0x1) /* inter_4v */ 927 BitstreamReadBits16(stream, 8); 928 if ((currVol->complexity.mc >> 5) & 0x1) /* apm */ 929 BitstreamReadBits16(stream, 8); 930 if ((currVol->complexity.mc >> 4) & 0x1) /* npm */ 931 BitstreamReadBits16(stream, 8); 932 /* interpolate_mc_q */ 933 if ((currVol->complexity.mc >> 2) & 0x1) /* forw_back_mc_q */ 934 BitstreamReadBits16(stream, 8); 935 if ((currVol->complexity.mc >> 1) & 0x1) /* halfpel2 */ 936 BitstreamReadBits16(stream, 8); 937 if (currVol->complexity.mc & 0x1) /* halfpel4 */ 938 BitstreamReadBits16(stream, 8); 939 } 940 if (currVop->predictionType == B_VOP) 941 { 942 if ((currVol->complexity.mc >> 3) & 0x1) /* interpolate_mc_q */ 943 BitstreamReadBits16(stream, 8); 944 } 945 } 946 } 947 948 /* read intra_dc_vlc_thr */ 949 currVop->intraDCVlcThr = (int) BitstreamReadBits16(stream, 3); 950 951 /* read vop_quant (currVol->quantPrecision bits) */ 952 currVop->quantizer = (int16) BitstreamReadBits16(stream, currVol->quantPrecision); 953 if (currVop->quantizer == 0) 954 { 955 currVop->quantizer = video->prevVop->quantizer; 956 status = PV_FAIL; 957 goto return_point; 958 } 959 960 961 /* read vop_fcode_forward */ 962 if (currVop->predictionType != I_VOP) 963 { 964 tmpvar = (uint32) BitstreamReadBits16(stream, 3); 965 if (tmpvar < 1) 966 { 967 currVop->fcodeForward = 1; 968 status = PV_FAIL; 969 goto return_point; 970 } 971 currVop->fcodeForward = tmpvar; 972 } 973 else 974 { 975 currVop->fcodeForward = 0; 976 } 977 978 /* read vop_fcode_backward */ 979 if (currVop->predictionType == B_VOP) 980 { 981 tmpvar = (uint32) BitstreamReadBits16(stream, 3); 982 if (tmpvar < 1) 983 { 984 currVop->fcodeBackward = 1; 985 status = PV_FAIL; 986 goto return_point; 987 } 988 currVop->fcodeBackward = tmpvar; 989 } 990 else 991 { 992 currVop->fcodeBackward = 0; 993 } 994 995 if (currVol->scalability) 996 { 997 currVop->refSelectCode = (int) BitstreamReadBits16(stream, 2); 998 } 999 1000 return_point: 1001 return status; 1002 } 1003 1004 1005 /***********************************************************CommentBegin****** 1006 * 1007 * -- VideoPlaneWithShortHeader -- Decodes the short_video_header information from the bitstream 1008 * Modified : 1009 04/23/2001. Remove the codes related to the 1010 "first pass" decoding. We use a different function 1011 to set up the decoder now. 1012 ***********************************************************CommentEnd********/ 1013 PV_STATUS DecodeShortHeader(VideoDecData *video, Vop *currVop) 1014 { 1015 PV_STATUS status = PV_SUCCESS; 1016 Vol *currVol = video->vol[0]; 1017 BitstreamDecVideo *stream = currVol->bitstream; 1018 uint32 tmpvar; 1019 int32 size; 1020 1021 int extended_PTYPE = FALSE; 1022 int UFEP = 0, custom_PFMT = 0, custom_PCF = 0; 1023 1024 status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar); 1025 1026 if (tmpvar != SHORT_VIDEO_START_MARKER) 1027 { 1028 status = PV_FAIL; 1029 goto return_point; 1030 } 1031 1032 1033 PV_BitstreamFlushBits(stream, SHORT_VIDEO_START_MARKER_LENGTH); 1034 1035 /* Temporal reference. Using vop_time_increment_resolution = 30000 */ 1036 tmpvar = (uint32) BitstreamReadBits16(stream, 8); 1037 currVop->temporalRef = (int) tmpvar; 1038 1039 1040 currVop->timeInc = 0xff & (256 + currVop->temporalRef - video->prevVop->temporalRef); 1041 currVol->moduloTimeBase += currVop->timeInc; /* mseconds 11/12/01 */ 1042 /* Marker Bit */ 1043 if (!BitstreamRead1Bits(stream)) 1044 { 1045 mp4dec_log("DecodeShortHeader(): Market bit wrong.\n"); 1046 status = PV_FAIL; 1047 goto return_point; 1048 } 1049 1050 /* Zero Bit */ 1051 if (BitstreamRead1Bits(stream)) 1052 { 1053 mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n"); 1054 status = PV_FAIL; 1055 goto return_point; 1056 } 1057 1058 /*split_screen_indicator*/ 1059 if (BitstreamRead1Bits(stream)) 1060 { 1061 mp4dec_log("DecodeShortHeader(): Split Screen not supported.\n"); 1062 VideoDecoderErrorDetected(video); 1063 } 1064 1065 /*document_freeze_camera*/ 1066 if (BitstreamRead1Bits(stream)) 1067 { 1068 mp4dec_log("DecodeShortHeader(): Freeze Camera not supported.\n"); 1069 VideoDecoderErrorDetected(video); 1070 } 1071 1072 /*freeze_picture_release*/ 1073 if (BitstreamRead1Bits(stream)) 1074 { 1075 mp4dec_log("DecodeShortHeader(): Freeze Release not supported.\n"); 1076 VideoDecoderErrorDetected(video); 1077 } 1078 /* source format */ 1079 switch (BitstreamReadBits16(stream, 3)) 1080 { 1081 case 1: 1082 if (video->size < 128*96) 1083 { 1084 status = PV_FAIL; 1085 goto return_point; 1086 } 1087 video->displayWidth = video->width = 128; 1088 video->displayHeight = video->height = 96; 1089 break; 1090 1091 case 2: 1092 if (video->size < 176*144) 1093 { 1094 status = PV_FAIL; 1095 goto return_point; 1096 } 1097 video->displayWidth = video->width = 176; 1098 video->displayHeight = video->height = 144; 1099 break; 1100 1101 case 3: 1102 if (video->size < 352*288) 1103 { 1104 status = PV_FAIL; 1105 goto return_point; 1106 } 1107 video->displayWidth = video->width = 352; 1108 video->displayHeight = video->height = 288; 1109 break; 1110 1111 case 4: 1112 if (video->size < 704*576) 1113 { 1114 status = PV_FAIL; 1115 goto return_point; 1116 } 1117 video->displayWidth = video->width = 704; 1118 video->displayHeight = video->height = 576; 1119 break; 1120 1121 case 5: 1122 if (video->size < 1408*1152) 1123 { 1124 status = PV_FAIL; 1125 goto return_point; 1126 } 1127 video->displayWidth = video->width = 1408; 1128 video->displayHeight = video->height = 1152; 1129 break; 1130 1131 case 7: 1132 extended_PTYPE = TRUE; 1133 break; 1134 1135 default: 1136 /* Msg("H.263 source format not legal\n"); */ 1137 status = PV_FAIL; 1138 goto return_point; 1139 } 1140 1141 1142 currVop->roundingType = 0; 1143 1144 if (extended_PTYPE == FALSE) 1145 { 1146 currVop->predictionType = (int) BitstreamRead1Bits(stream); 1147 1148 /* four_reserved_zero_bits */ 1149 if (BitstreamReadBits16(stream, 4)) 1150 { 1151 mp4dec_log("DecodeShortHeader(): Reserved bits wrong.\n"); 1152 status = PV_FAIL; 1153 goto return_point; 1154 } 1155 } 1156 else 1157 { 1158 UFEP = BitstreamReadBits16(stream, 3); 1159 if (UFEP == 1) 1160 { 1161 /* source format */ 1162 switch (BitstreamReadBits16(stream, 3)) 1163 { 1164 case 1: 1165 if (video->size < 128*96) 1166 { 1167 status = PV_FAIL; 1168 goto return_point; 1169 } 1170 video->displayWidth = video->width = 128; 1171 video->displayHeight = video->height = 96; 1172 break; 1173 1174 case 2: 1175 if (video->size < 176*144) 1176 { 1177 status = PV_FAIL; 1178 goto return_point; 1179 } 1180 video->displayWidth = video->width = 176; 1181 video->displayHeight = video->height = 144; 1182 break; 1183 1184 case 3: 1185 if (video->size < 352*288) 1186 { 1187 status = PV_FAIL; 1188 goto return_point; 1189 } 1190 video->displayWidth = video->width = 352; 1191 video->displayHeight = video->height = 288; 1192 break; 1193 1194 case 4: 1195 if (video->size < 704*576) 1196 { 1197 status = PV_FAIL; 1198 goto return_point; 1199 } 1200 video->displayWidth = video->width = 704; 1201 video->displayHeight = video->height = 576; 1202 break; 1203 1204 case 5: 1205 if (video->size < 1408*1152) 1206 { 1207 status = PV_FAIL; 1208 goto return_point; 1209 } 1210 video->displayWidth = video->width = 1408; 1211 video->displayHeight = video->height = 1152; 1212 break; 1213 1214 case 6: 1215 custom_PFMT = TRUE; 1216 break; 1217 1218 default: 1219 /* Msg("H.263 source format not legal\n"); */ 1220 status = PV_FAIL; 1221 goto return_point; 1222 } 1223 1224 custom_PCF = BitstreamRead1Bits(stream); 1225 /* unrestricted MV */ 1226 if (BitstreamRead1Bits(stream)) 1227 { 1228 status = PV_FAIL; 1229 goto return_point; 1230 } 1231 /* SAC */ 1232 if (BitstreamRead1Bits(stream)) 1233 { 1234 status = PV_FAIL; 1235 goto return_point; 1236 } 1237 1238 /* AP */ 1239 if (BitstreamRead1Bits(stream)) 1240 { 1241 status = PV_FAIL; 1242 goto return_point; 1243 } 1244 1245 video->advanced_INTRA = BitstreamRead1Bits(stream); 1246 1247 video->deblocking = BitstreamRead1Bits(stream); 1248 1249 video->slice_structure = BitstreamRead1Bits(stream); 1250 1251 /* RPS, ISD, AIV */ 1252 if (BitstreamReadBits16(stream, 3)) 1253 { 1254 status = PV_FAIL; 1255 goto return_point; 1256 } 1257 video->modified_quant = BitstreamRead1Bits(stream); 1258 1259 /* Marker Bit and reserved*/ 1260 if (BitstreamReadBits16(stream, 4) != 8) 1261 { 1262 status = PV_FAIL; 1263 goto return_point; 1264 } 1265 } 1266 #ifndef PV_ANNEX_IJKT_SUPPORT 1267 if (video->advanced_INTRA | video->deblocking | video->modified_quant | video->modified_quant) 1268 { 1269 status = PV_FAIL; 1270 goto return_point; 1271 } 1272 #endif 1273 1274 if (UFEP == 0 || UFEP == 1) 1275 { 1276 tmpvar = BitstreamReadBits16(stream, 3); 1277 if (tmpvar > 1) 1278 { 1279 status = PV_FAIL; 1280 goto return_point; 1281 } 1282 currVop->predictionType = tmpvar; 1283 /* RPR */ 1284 if (BitstreamRead1Bits(stream)) 1285 { 1286 status = PV_FAIL; 1287 goto return_point; 1288 } 1289 1290 /* RRU */ 1291 if (BitstreamRead1Bits(stream)) 1292 { 1293 status = PV_FAIL; 1294 goto return_point; 1295 } 1296 currVop->roundingType = (int) BitstreamRead1Bits(stream); 1297 if (BitstreamReadBits16(stream, 3) != 1) 1298 { 1299 status = PV_FAIL; 1300 goto return_point; 1301 } 1302 } 1303 else 1304 { 1305 status = PV_FAIL; 1306 goto return_point; 1307 } 1308 /* CPM */ 1309 if (BitstreamRead1Bits(stream)) 1310 { 1311 status = PV_FAIL; 1312 goto return_point; 1313 } 1314 /* CPFMT */ 1315 if (custom_PFMT == 1 && UFEP == 1) 1316 { 1317 /* aspect ratio */ 1318 tmpvar = BitstreamReadBits16(stream, 4); 1319 if (tmpvar == 0) 1320 { 1321 status = PV_FAIL; 1322 goto return_point; 1323 } 1324 /* Extended PAR */ 1325 if (tmpvar == 0xF) 1326 { 1327 /* Read par_width and par_height but do nothing */ 1328 /* par_width */ 1329 tmpvar = BitstreamReadBits16(stream, 8); 1330 1331 /* par_height */ 1332 tmpvar = BitstreamReadBits16(stream, 8); 1333 } 1334 tmpvar = BitstreamReadBits16(stream, 9); 1335 1336 video->displayWidth = (tmpvar + 1) << 2; 1337 video->width = (video->displayWidth + 15) & -16; 1338 /* marker bit */ 1339 if (!BitstreamRead1Bits(stream)) 1340 { 1341 status = PV_FAIL; 1342 goto return_point; 1343 } 1344 tmpvar = BitstreamReadBits16(stream, 9); 1345 if (tmpvar == 0) 1346 { 1347 status = PV_FAIL; 1348 goto return_point; 1349 } 1350 video->displayHeight = tmpvar << 2; 1351 video->height = (video->displayHeight + 15) & -16; 1352 1353 if (video->height * video->width > video->size) 1354 { 1355 status = PV_FAIL; 1356 goto return_point; 1357 } 1358 1359 video->nTotalMB = video->width / MB_SIZE * video->height / MB_SIZE; 1360 1361 if (video->nTotalMB <= 48) 1362 { 1363 video->nBitsForMBID = 6; 1364 } 1365 else if (video->nTotalMB <= 99) 1366 { 1367 video->nBitsForMBID = 7; 1368 } 1369 else if (video->nTotalMB <= 396) 1370 { 1371 video->nBitsForMBID = 9; 1372 } 1373 else if (video->nTotalMB <= 1584) 1374 { 1375 video->nBitsForMBID = 11; 1376 } 1377 else if (video->nTotalMB <= 6336) 1378 { 1379 video->nBitsForMBID = 13 ; 1380 } 1381 else if (video->nTotalMB <= 9216) 1382 { 1383 video->nBitsForMBID = 14 ; 1384 } 1385 else 1386 { 1387 status = PV_FAIL; 1388 goto return_point; 1389 } 1390 } 1391 if (UFEP == 1 && custom_PCF == 1) 1392 { 1393 BitstreamRead1Bits(stream); 1394 1395 tmpvar = BitstreamReadBits16(stream, 7); 1396 if (tmpvar == 0) 1397 { 1398 status = PV_FAIL; 1399 goto return_point; 1400 } 1401 } 1402 1403 if (custom_PCF == 1) 1404 { 1405 currVop->ETR = BitstreamReadBits16(stream, 2); 1406 } 1407 1408 if (UFEP == 1 && video->slice_structure == 1) 1409 { 1410 /* SSS */ 1411 tmpvar = BitstreamReadBits16(stream, 2); 1412 if (tmpvar != 0) 1413 { 1414 status = PV_FAIL; 1415 goto return_point; 1416 } 1417 } 1418 } 1419 1420 /* Recalculate number of macroblocks per row & col since */ 1421 /* the frame size can change. 04/23/2001. */ 1422 video->nMBinGOB = video->nMBPerRow = video->width / MB_SIZE; 1423 video->nGOBinVop = video->nMBPerCol = video->height / MB_SIZE; 1424 video->nTotalMB = video->nMBPerRow * video->nMBPerCol; 1425 if (custom_PFMT == 0 || UFEP == 0) 1426 { 1427 video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1); /* otherwise calculate above */ 1428 } 1429 size = (int32)video->width * video->height; 1430 if (video->currVop->predictionType == P_VOP && size > video->videoDecControls->size) 1431 { 1432 status = PV_FAIL; 1433 goto return_point; 1434 } 1435 video->videoDecControls->size = size; 1436 video->currVop->uChan = video->currVop->yChan + size; 1437 video->currVop->vChan = video->currVop->uChan + (size >> 2); 1438 video->prevVop->uChan = video->prevVop->yChan + size; 1439 video->prevVop->vChan = video->prevVop->uChan + (size >> 2); 1440 1441 1442 currVop->quantizer = (int16) BitstreamReadBits16(stream, 5); 1443 1444 if (currVop->quantizer == 0) /* 04/03/01 */ 1445 { 1446 currVop->quantizer = video->prevVop->quantizer; 1447 status = PV_FAIL; 1448 goto return_point; 1449 } 1450 1451 1452 /* Zero bit */ 1453 if (extended_PTYPE == FALSE) 1454 { 1455 if (BitstreamRead1Bits(stream)) 1456 { 1457 mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n"); 1458 status = PV_FAIL; 1459 goto return_point; 1460 } 1461 } 1462 /* pei */ 1463 tmpvar = (uint32) BitstreamRead1Bits(stream); 1464 1465 while (tmpvar) 1466 { 1467 tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* "PSPARE" */ 1468 tmpvar = (uint32) BitstreamRead1Bits(stream); /* "PEI" */ 1469 } 1470 1471 if (video->slice_structure) /* ANNEX_K */ 1472 { 1473 if (!BitstreamRead1Bits(stream)) /* SEPB1 */ 1474 { 1475 status = PV_FAIL; 1476 goto return_point; 1477 } 1478 1479 // if (currVol->nBitsForMBID // 1480 if (BitstreamReadBits16(stream, video->nBitsForMBID)) 1481 { 1482 status = PV_FAIL; /* no ASO, RS support for Annex K */ 1483 goto return_point; 1484 } 1485 1486 if (!BitstreamRead1Bits(stream)) /*SEPB3 */ 1487 { 1488 status = PV_FAIL; 1489 goto return_point; 1490 } 1491 1492 } 1493 /* Setting of other VOP-header parameters */ 1494 currVop->gobNumber = 0; 1495 currVop->vopCoded = 1; 1496 1497 currVop->intraDCVlcThr = 0; 1498 currVop->gobFrameID = 0; /* initial value, 05/22/00 */ 1499 currVol->errorResDisable = 0; 1500 /*PutVopInterlaced(0,curr_vop); no implemented yet */ 1501 if (currVop->predictionType != I_VOP) 1502 currVop->fcodeForward = 1; 1503 else 1504 currVop->fcodeForward = 0; 1505 1506 return_point: 1507 1508 return status; 1509 } 1510 /***********************************************************CommentBegin****** 1511 * 1512 * -- PV_DecodeVop -- Decodes the VOP information from the bitstream 1513 * 1514 * 04/12/2000 1515 * Initial port to the new PV decoder library format. 1516 * This function is different from the one in MoMuSys MPEG-4 1517 * visual decoder. We handle combined mode with or withput 1518 * error resilience and H.263 mode through the sam path now. 1519 * 1520 * 05/04/2000 1521 * Added temporal scalability to the decoder. 1522 * 1523 ***********************************************************CommentEnd********/ 1524 PV_STATUS PV_DecodeVop(VideoDecData *video) 1525 { 1526 Vol *currVol = video->vol[video->currLayer]; 1527 PV_STATUS status; 1528 uint32 tmpvar; 1529 1530 /***** 1531 * Do scalable or non-scalable decoding of the current VOP 1532 *****/ 1533 1534 if (!currVol->scalability) 1535 { 1536 if (currVol->dataPartitioning) 1537 { 1538 /* Data partitioning mode comes here */ 1539 status = DecodeFrameDataPartMode(video); 1540 } 1541 else 1542 { 1543 /* Combined mode with or without error resilience */ 1544 /* and short video header comes here. */ 1545 status = DecodeFrameCombinedMode(video); 1546 } 1547 } 1548 else 1549 { 1550 #ifdef DO_NOT_FOLLOW_STANDARD 1551 /* according to the standard, only combined mode is allowed */ 1552 /* in the enhancement layer. 06/01/2000. */ 1553 if (currVol->dataPartitioning) 1554 { 1555 /* Data partitioning mode comes here */ 1556 status = DecodeFrameDataPartMode(video); 1557 } 1558 else 1559 { 1560 /* Combined mode with or without error resilience */ 1561 /* and short video header comes here. */ 1562 status = DecodeFrameCombinedMode(video); 1563 } 1564 #else 1565 status = DecodeFrameCombinedMode(video); 1566 #endif 1567 } 1568 1569 /* This part is for consuming Visual_object_sequence_end_code and EOS Code */ /* 10/15/01 */ 1570 if (!video->shortVideoHeader) 1571 { 1572 /* at this point bitstream is expected to be byte aligned */ 1573 BitstreamByteAlignNoForceStuffing(currVol->bitstream); 1574 1575 status = BitstreamShowBits32HC(currVol->bitstream, &tmpvar); /* 07/07/01 */ 1576 if (tmpvar == VISUAL_OBJECT_SEQUENCE_END_CODE)/* VOS_END_CODE */ 1577 { 1578 PV_BitstreamFlushBits(currVol->bitstream, 16); 1579 PV_BitstreamFlushBits(currVol->bitstream, 16); 1580 } 1581 1582 } 1583 else 1584 { 1585 #ifdef PV_ANNEX_IJKT_SUPPORT 1586 if (video->deblocking) 1587 { 1588 H263_Deblock(video->currVop->yChan, video->width, video->height, video->QPMB, video->headerInfo.Mode, 0, 0); 1589 H263_Deblock(video->currVop->uChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant); 1590 H263_Deblock(video->currVop->vChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant); 1591 } 1592 #endif 1593 /* Read EOS code for shortheader bitstreams */ 1594 status = BitstreamShowBits32(currVol->bitstream, 22, &tmpvar); 1595 if (tmpvar == SHORT_VIDEO_END_MARKER) 1596 { 1597 PV_BitstreamFlushBits(currVol->bitstream, 22); 1598 } 1599 else 1600 { 1601 status = PV_BitstreamShowBitsByteAlign(currVol->bitstream, 22, &tmpvar); 1602 if (tmpvar == SHORT_VIDEO_END_MARKER) 1603 { 1604 PV_BitstreamByteAlign(currVol->bitstream); 1605 PV_BitstreamFlushBits(currVol->bitstream, 22); 1606 } 1607 } 1608 } 1609 return status; 1610 } 1611 1612 1613 /***********************************************************CommentBegin****** 1614 * 1615 * -- CalcVopDisplayTime -- calculate absolute time when VOP is to be displayed 1616 * 1617 * 04/12/2000 Initial port to the new PV decoder library format. 1618 * 1619 ***********************************************************CommentEnd********/ 1620 uint32 CalcVopDisplayTime(Vol *currVol, Vop *currVop, int shortVideoHeader) 1621 { 1622 uint32 display_time; 1623 1624 1625 /***** 1626 * Calculate the time when the VOP is to be displayed next 1627 *****/ 1628 1629 if (!shortVideoHeader) 1630 { 1631 display_time = (uint32)(currVol->moduloTimeBase + (((int32)currVop->timeInc - (int32)currVol->timeInc_offset) * 1000) / ((int32)currVol->timeIncrementResolution)); /* 11/12/2001 */ 1632 if (currVop->timeStamp >= display_time) 1633 { 1634 display_time += 1000; /* this case is valid if GOVHeader timestamp is ignored */ 1635 } 1636 } 1637 else 1638 { 1639 display_time = (uint32)(currVol->moduloTimeBase * 33 + (currVol->moduloTimeBase * 11) / 30); /* 11/12/2001 */ 1640 } 1641 1642 return(display_time); 1643 } 1644 1645