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

/media/libstagefright/codecs/m4v_h263/dec/src/vop.cpp

https://gitlab.com/frantisheq/android_frameworks_av
C++ | 1643 lines | 1160 code | 196 blank | 287 comment | 369 complexity | 49ebd2c85a47c4f2a64f087595ad92c4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file