You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1081 lines
36KB

  1. /*
  2. * VC-1 and WMV3 decoder common code
  3. * Copyright (c) 2006-2007 Konstantin Shishkov
  4. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * VC-1 and WMV3 decoder common code
  25. *
  26. */
  27. #include "internal.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "vc1.h"
  32. #include "vc1data.h"
  33. #include "msmpeg4data.h"
  34. #include "unary.h"
  35. #include "simple_idct.h"
  36. #undef NDEBUG
  37. #include <assert.h>
  38. /***********************************************************************/
  39. /**
  40. * @defgroup vc1bitplane VC-1 Bitplane decoding
  41. * @see 8.7, p56
  42. * @{
  43. */
  44. /**
  45. * Imode types
  46. * @{
  47. */
  48. enum Imode {
  49. IMODE_RAW,
  50. IMODE_NORM2,
  51. IMODE_DIFF2,
  52. IMODE_NORM6,
  53. IMODE_DIFF6,
  54. IMODE_ROWSKIP,
  55. IMODE_COLSKIP
  56. };
  57. /** @} */ //imode defines
  58. /** Decode rows by checking if they are skipped
  59. * @param plane Buffer to store decoded bits
  60. * @param[in] width Width of this buffer
  61. * @param[in] height Height of this buffer
  62. * @param[in] stride of this buffer
  63. */
  64. static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  65. int x, y;
  66. for (y=0; y<height; y++){
  67. if (!get_bits1(gb)) //rowskip
  68. memset(plane, 0, width);
  69. else
  70. for (x=0; x<width; x++)
  71. plane[x] = get_bits1(gb);
  72. plane += stride;
  73. }
  74. }
  75. /** Decode columns by checking if they are skipped
  76. * @param plane Buffer to store decoded bits
  77. * @param[in] width Width of this buffer
  78. * @param[in] height Height of this buffer
  79. * @param[in] stride of this buffer
  80. * @todo FIXME: Optimize
  81. */
  82. static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  83. int x, y;
  84. for (x=0; x<width; x++){
  85. if (!get_bits1(gb)) //colskip
  86. for (y=0; y<height; y++)
  87. plane[y*stride] = 0;
  88. else
  89. for (y=0; y<height; y++)
  90. plane[y*stride] = get_bits1(gb);
  91. plane ++;
  92. }
  93. }
  94. /** Decode a bitplane's bits
  95. * @param data bitplane where to store the decode bits
  96. * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
  97. * @param v VC-1 context for bit reading and logging
  98. * @return Status
  99. * @todo FIXME: Optimize
  100. */
  101. static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
  102. {
  103. GetBitContext *gb = &v->s.gb;
  104. int imode, x, y, code, offset;
  105. uint8_t invert, *planep = data;
  106. int width, height, stride;
  107. width = v->s.mb_width;
  108. height = v->s.mb_height;
  109. stride = v->s.mb_stride;
  110. invert = get_bits1(gb);
  111. imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
  112. *raw_flag = 0;
  113. switch (imode)
  114. {
  115. case IMODE_RAW:
  116. //Data is actually read in the MB layer (same for all tests == "raw")
  117. *raw_flag = 1; //invert ignored
  118. return invert;
  119. case IMODE_DIFF2:
  120. case IMODE_NORM2:
  121. if ((height * width) & 1)
  122. {
  123. *planep++ = get_bits1(gb);
  124. offset = 1;
  125. }
  126. else offset = 0;
  127. // decode bitplane as one long line
  128. for (y = offset; y < height * width; y += 2) {
  129. code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
  130. *planep++ = code & 1;
  131. offset++;
  132. if(offset == width) {
  133. offset = 0;
  134. planep += stride - width;
  135. }
  136. *planep++ = code >> 1;
  137. offset++;
  138. if(offset == width) {
  139. offset = 0;
  140. planep += stride - width;
  141. }
  142. }
  143. break;
  144. case IMODE_DIFF6:
  145. case IMODE_NORM6:
  146. if(!(height % 3) && (width % 3)) { // use 2x3 decoding
  147. for(y = 0; y < height; y+= 3) {
  148. for(x = width & 1; x < width; x += 2) {
  149. code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
  150. if(code < 0){
  151. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  152. return -1;
  153. }
  154. planep[x + 0] = (code >> 0) & 1;
  155. planep[x + 1] = (code >> 1) & 1;
  156. planep[x + 0 + stride] = (code >> 2) & 1;
  157. planep[x + 1 + stride] = (code >> 3) & 1;
  158. planep[x + 0 + stride * 2] = (code >> 4) & 1;
  159. planep[x + 1 + stride * 2] = (code >> 5) & 1;
  160. }
  161. planep += stride * 3;
  162. }
  163. if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
  164. } else { // 3x2
  165. planep += (height & 1) * stride;
  166. for(y = height & 1; y < height; y += 2) {
  167. for(x = width % 3; x < width; x += 3) {
  168. code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
  169. if(code < 0){
  170. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  171. return -1;
  172. }
  173. planep[x + 0] = (code >> 0) & 1;
  174. planep[x + 1] = (code >> 1) & 1;
  175. planep[x + 2] = (code >> 2) & 1;
  176. planep[x + 0 + stride] = (code >> 3) & 1;
  177. planep[x + 1 + stride] = (code >> 4) & 1;
  178. planep[x + 2 + stride] = (code >> 5) & 1;
  179. }
  180. planep += stride * 2;
  181. }
  182. x = width % 3;
  183. if(x) decode_colskip(data , x, height , stride, &v->s.gb);
  184. if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
  185. }
  186. break;
  187. case IMODE_ROWSKIP:
  188. decode_rowskip(data, width, height, stride, &v->s.gb);
  189. break;
  190. case IMODE_COLSKIP:
  191. decode_colskip(data, width, height, stride, &v->s.gb);
  192. break;
  193. default: break;
  194. }
  195. /* Applying diff operator */
  196. if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
  197. {
  198. planep = data;
  199. planep[0] ^= invert;
  200. for (x=1; x<width; x++)
  201. planep[x] ^= planep[x-1];
  202. for (y=1; y<height; y++)
  203. {
  204. planep += stride;
  205. planep[0] ^= planep[-stride];
  206. for (x=1; x<width; x++)
  207. {
  208. if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
  209. else planep[x] ^= planep[x-1];
  210. }
  211. }
  212. }
  213. else if (invert)
  214. {
  215. planep = data;
  216. for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
  217. }
  218. return (imode<<1) + invert;
  219. }
  220. /** @} */ //Bitplane group
  221. /***********************************************************************/
  222. /** VOP Dquant decoding
  223. * @param v VC-1 Context
  224. */
  225. static int vop_dquant_decoding(VC1Context *v)
  226. {
  227. GetBitContext *gb = &v->s.gb;
  228. int pqdiff;
  229. //variable size
  230. if (v->dquant == 2)
  231. {
  232. pqdiff = get_bits(gb, 3);
  233. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  234. else v->altpq = v->pq + pqdiff + 1;
  235. }
  236. else
  237. {
  238. v->dquantfrm = get_bits1(gb);
  239. if ( v->dquantfrm )
  240. {
  241. v->dqprofile = get_bits(gb, 2);
  242. switch (v->dqprofile)
  243. {
  244. case DQPROFILE_SINGLE_EDGE:
  245. case DQPROFILE_DOUBLE_EDGES:
  246. v->dqsbedge = get_bits(gb, 2);
  247. break;
  248. case DQPROFILE_ALL_MBS:
  249. v->dqbilevel = get_bits1(gb);
  250. if(!v->dqbilevel)
  251. v->halfpq = 0;
  252. default: break; //Forbidden ?
  253. }
  254. if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
  255. {
  256. pqdiff = get_bits(gb, 3);
  257. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  258. else v->altpq = v->pq + pqdiff + 1;
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264. static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
  265. static void simple_idct_put_rangered(uint8_t *dest, int line_size, DCTELEM *block)
  266. {
  267. int i;
  268. ff_simple_idct(block);
  269. for (i = 0; i < 64; i++) block[i] = (block[i] - 64) << 1;
  270. ff_put_pixels_clamped_c(block, dest, line_size);
  271. }
  272. static void simple_idct_put_signed(uint8_t *dest, int line_size, DCTELEM *block)
  273. {
  274. ff_simple_idct(block);
  275. ff_put_signed_pixels_clamped_c(block, dest, line_size);
  276. }
  277. static void simple_idct_put_signed_rangered(uint8_t *dest, int line_size, DCTELEM *block)
  278. {
  279. int i;
  280. ff_simple_idct(block);
  281. for (i = 0; i < 64; i++) block[i] <<= 1;
  282. ff_put_signed_pixels_clamped_c(block, dest, line_size);
  283. }
  284. /**
  285. * Decode Simple/Main Profiles sequence header
  286. * @see Figure 7-8, p16-17
  287. * @param avctx Codec context
  288. * @param gb GetBit context initialized from Codec context extra_data
  289. * @return Status
  290. */
  291. int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
  292. {
  293. av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
  294. v->profile = get_bits(gb, 2);
  295. if (v->profile == PROFILE_COMPLEX)
  296. {
  297. av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
  298. }
  299. if (v->profile == PROFILE_ADVANCED)
  300. {
  301. v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
  302. v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
  303. return decode_sequence_header_adv(v, gb);
  304. }
  305. else
  306. {
  307. v->zz_8x4 = wmv2_scantableA;
  308. v->zz_4x8 = wmv2_scantableB;
  309. v->res_y411 = get_bits1(gb);
  310. v->res_sprite = get_bits1(gb);
  311. if (v->res_y411)
  312. {
  313. av_log(avctx, AV_LOG_ERROR,
  314. "Old interlaced mode is not supported\n");
  315. return -1;
  316. }
  317. if (v->res_sprite) {
  318. av_log(avctx, AV_LOG_ERROR, "WMVP is not fully supported\n");
  319. }
  320. }
  321. // (fps-2)/4 (->30)
  322. v->frmrtq_postproc = get_bits(gb, 3); //common
  323. // (bitrate-32kbps)/64kbps
  324. v->bitrtq_postproc = get_bits(gb, 5); //common
  325. v->s.loop_filter = get_bits1(gb); //common
  326. if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
  327. {
  328. av_log(avctx, AV_LOG_ERROR,
  329. "LOOPFILTER shall not be enabled in Simple Profile\n");
  330. }
  331. if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
  332. v->s.loop_filter = 0;
  333. v->res_x8 = get_bits1(gb); //reserved
  334. v->multires = get_bits1(gb);
  335. v->res_fasttx = get_bits1(gb);
  336. if (!v->res_fasttx)
  337. {
  338. v->vc1dsp.vc1_inv_trans_8x8_add = ff_simple_idct_add;
  339. v->vc1dsp.vc1_inv_trans_8x8_put[0] = ff_simple_idct_put;
  340. v->vc1dsp.vc1_inv_trans_8x8_put[1] = simple_idct_put_rangered;
  341. v->vc1dsp.vc1_inv_trans_8x8_put_signed[0] = simple_idct_put_signed;
  342. v->vc1dsp.vc1_inv_trans_8x8_put_signed[1] = simple_idct_put_signed_rangered;
  343. v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
  344. v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
  345. v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
  346. v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add;
  347. v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
  348. v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
  349. v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
  350. }
  351. v->fastuvmc = get_bits1(gb); //common
  352. if (!v->profile && !v->fastuvmc)
  353. {
  354. av_log(avctx, AV_LOG_ERROR,
  355. "FASTUVMC unavailable in Simple Profile\n");
  356. return -1;
  357. }
  358. v->extended_mv = get_bits1(gb); //common
  359. if (!v->profile && v->extended_mv)
  360. {
  361. av_log(avctx, AV_LOG_ERROR,
  362. "Extended MVs unavailable in Simple Profile\n");
  363. return -1;
  364. }
  365. v->dquant = get_bits(gb, 2); //common
  366. v->vstransform = get_bits1(gb); //common
  367. v->res_transtab = get_bits1(gb);
  368. if (v->res_transtab)
  369. {
  370. av_log(avctx, AV_LOG_ERROR,
  371. "1 for reserved RES_TRANSTAB is forbidden\n");
  372. return -1;
  373. }
  374. v->overlap = get_bits1(gb); //common
  375. v->s.resync_marker = get_bits1(gb);
  376. v->rangered = get_bits1(gb);
  377. if (v->rangered && v->profile == PROFILE_SIMPLE)
  378. {
  379. av_log(avctx, AV_LOG_INFO,
  380. "RANGERED should be set to 0 in Simple Profile\n");
  381. }
  382. v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
  383. v->quantizer_mode = get_bits(gb, 2); //common
  384. v->finterpflag = get_bits1(gb); //common
  385. if (v->res_sprite) {
  386. v->s.avctx->width = v->s.avctx->coded_width = get_bits(gb, 11);
  387. v->s.avctx->height = v->s.avctx->coded_height = get_bits(gb, 11);
  388. skip_bits(gb, 5); //frame rate
  389. v->res_x8 = get_bits1(gb);
  390. if (get_bits1(gb)) { // something to do with DC VLC selection
  391. av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
  392. return -1;
  393. }
  394. skip_bits(gb, 3); //slice code
  395. v->res_rtm_flag = 0;
  396. } else {
  397. v->res_rtm_flag = get_bits1(gb); //reserved
  398. }
  399. if (!v->res_rtm_flag)
  400. {
  401. // av_log(avctx, AV_LOG_ERROR,
  402. // "0 for reserved RES_RTM_FLAG is forbidden\n");
  403. av_log(avctx, AV_LOG_ERROR,
  404. "Old WMV3 version detected, some frames may be decoded incorrectly\n");
  405. //return -1;
  406. }
  407. //TODO: figure out what they mean (always 0x402F)
  408. if(!v->res_fasttx) skip_bits(gb, 16);
  409. av_log(avctx, AV_LOG_DEBUG,
  410. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  411. "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
  412. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  413. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  414. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  415. v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
  416. v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
  417. v->dquant, v->quantizer_mode, avctx->max_b_frames
  418. );
  419. return 0;
  420. }
  421. static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
  422. {
  423. v->res_rtm_flag = 1;
  424. v->level = get_bits(gb, 3);
  425. if(v->level >= 5)
  426. {
  427. av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
  428. }
  429. v->chromaformat = get_bits(gb, 2);
  430. if (v->chromaformat != 1)
  431. {
  432. av_log(v->s.avctx, AV_LOG_ERROR,
  433. "Only 4:2:0 chroma format supported\n");
  434. return -1;
  435. }
  436. // (fps-2)/4 (->30)
  437. v->frmrtq_postproc = get_bits(gb, 3); //common
  438. // (bitrate-32kbps)/64kbps
  439. v->bitrtq_postproc = get_bits(gb, 5); //common
  440. v->postprocflag = get_bits1(gb); //common
  441. v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
  442. v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
  443. v->s.avctx->width = v->s.avctx->coded_width;
  444. v->s.avctx->height = v->s.avctx->coded_height;
  445. v->broadcast = get_bits1(gb);
  446. v->interlace = get_bits1(gb);
  447. v->tfcntrflag = get_bits1(gb);
  448. v->finterpflag = get_bits1(gb);
  449. skip_bits1(gb); // reserved
  450. v->s.h_edge_pos = v->s.avctx->coded_width;
  451. v->s.v_edge_pos = v->s.avctx->coded_height;
  452. av_log(v->s.avctx, AV_LOG_DEBUG,
  453. "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  454. "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
  455. "TFCTRflag=%i, FINTERPflag=%i\n",
  456. v->level, v->frmrtq_postproc, v->bitrtq_postproc,
  457. v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
  458. v->tfcntrflag, v->finterpflag
  459. );
  460. v->psf = get_bits1(gb);
  461. if(v->psf) { //PsF, 6.1.13
  462. av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
  463. return -1;
  464. }
  465. v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
  466. if(get_bits1(gb)) { //Display Info - decoding is not affected by it
  467. int w, h, ar = 0;
  468. av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
  469. v->s.avctx->width = w = get_bits(gb, 14) + 1;
  470. v->s.avctx->height = h = get_bits(gb, 14) + 1;
  471. av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
  472. if(get_bits1(gb))
  473. ar = get_bits(gb, 4);
  474. if(ar && ar < 14){
  475. v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
  476. }else if(ar == 15){
  477. w = get_bits(gb, 8);
  478. h = get_bits(gb, 8);
  479. v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
  480. }
  481. av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den);
  482. if(get_bits1(gb)){ //framerate stuff
  483. if(get_bits1(gb)) {
  484. v->s.avctx->time_base.num = 32;
  485. v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
  486. } else {
  487. int nr, dr;
  488. nr = get_bits(gb, 8);
  489. dr = get_bits(gb, 4);
  490. if(nr && nr < 8 && dr && dr < 3){
  491. v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
  492. v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
  493. }
  494. }
  495. }
  496. if(get_bits1(gb)){
  497. v->color_prim = get_bits(gb, 8);
  498. v->transfer_char = get_bits(gb, 8);
  499. v->matrix_coef = get_bits(gb, 8);
  500. }
  501. }
  502. v->hrd_param_flag = get_bits1(gb);
  503. if(v->hrd_param_flag) {
  504. int i;
  505. v->hrd_num_leaky_buckets = get_bits(gb, 5);
  506. skip_bits(gb, 4); //bitrate exponent
  507. skip_bits(gb, 4); //buffer size exponent
  508. for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
  509. skip_bits(gb, 16); //hrd_rate[n]
  510. skip_bits(gb, 16); //hrd_buffer[n]
  511. }
  512. }
  513. return 0;
  514. }
  515. int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
  516. {
  517. int i;
  518. av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
  519. v->broken_link = get_bits1(gb);
  520. v->closed_entry = get_bits1(gb);
  521. v->panscanflag = get_bits1(gb);
  522. v->refdist_flag = get_bits1(gb);
  523. v->s.loop_filter = get_bits1(gb);
  524. v->fastuvmc = get_bits1(gb);
  525. v->extended_mv = get_bits1(gb);
  526. v->dquant = get_bits(gb, 2);
  527. v->vstransform = get_bits1(gb);
  528. v->overlap = get_bits1(gb);
  529. v->quantizer_mode = get_bits(gb, 2);
  530. if(v->hrd_param_flag){
  531. for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
  532. skip_bits(gb, 8); //hrd_full[n]
  533. }
  534. }
  535. if(get_bits1(gb)){
  536. avctx->coded_width = (get_bits(gb, 12)+1)<<1;
  537. avctx->coded_height = (get_bits(gb, 12)+1)<<1;
  538. }
  539. if(v->extended_mv)
  540. v->extended_dmv = get_bits1(gb);
  541. if((v->range_mapy_flag = get_bits1(gb))) {
  542. av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
  543. v->range_mapy = get_bits(gb, 3);
  544. }
  545. if((v->range_mapuv_flag = get_bits1(gb))) {
  546. av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
  547. v->range_mapuv = get_bits(gb, 3);
  548. }
  549. av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
  550. "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
  551. "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
  552. "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
  553. v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
  554. v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
  555. return 0;
  556. }
  557. int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
  558. {
  559. int pqindex, lowquant, status;
  560. if(v->res_sprite) {
  561. skip_bits(gb, 2); //not yet deciphered
  562. }
  563. if(v->finterpflag) v->interpfrm = get_bits1(gb);
  564. skip_bits(gb, 2); //framecnt unused
  565. v->rangeredfrm = 0;
  566. if (v->rangered) v->rangeredfrm = get_bits1(gb);
  567. v->s.pict_type = get_bits1(gb);
  568. if (v->s.avctx->max_b_frames) {
  569. if (!v->s.pict_type) {
  570. if (get_bits1(gb)) v->s.pict_type = FF_I_TYPE;
  571. else v->s.pict_type = FF_B_TYPE;
  572. } else v->s.pict_type = FF_P_TYPE;
  573. } else v->s.pict_type = v->s.pict_type ? FF_P_TYPE : FF_I_TYPE;
  574. v->bi_type = 0;
  575. if(v->s.pict_type == FF_B_TYPE) {
  576. v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
  577. v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
  578. if(v->bfraction == 0) {
  579. v->s.pict_type = FF_BI_TYPE;
  580. }
  581. }
  582. if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
  583. skip_bits(gb, 7); // skip buffer fullness
  584. if(v->parse_only)
  585. return 0;
  586. /* calculate RND */
  587. if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
  588. v->rnd = 1;
  589. if(v->s.pict_type == FF_P_TYPE)
  590. v->rnd ^= 1;
  591. /* Quantizer stuff */
  592. pqindex = get_bits(gb, 5);
  593. if(!pqindex) return -1;
  594. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  595. v->pq = ff_vc1_pquant_table[0][pqindex];
  596. else
  597. v->pq = ff_vc1_pquant_table[1][pqindex];
  598. v->pquantizer = 1;
  599. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  600. v->pquantizer = pqindex < 9;
  601. if (v->quantizer_mode == QUANT_NON_UNIFORM)
  602. v->pquantizer = 0;
  603. v->pqindex = pqindex;
  604. if (pqindex < 9) v->halfpq = get_bits1(gb);
  605. else v->halfpq = 0;
  606. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  607. v->pquantizer = get_bits1(gb);
  608. v->dquantfrm = 0;
  609. if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
  610. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  611. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  612. v->range_x = 1 << (v->k_x - 1);
  613. v->range_y = 1 << (v->k_y - 1);
  614. if (v->multires && v->s.pict_type != FF_B_TYPE) v->respic = get_bits(gb, 2);
  615. if(v->res_x8 && (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)){
  616. v->x8_type = get_bits1(gb);
  617. }else v->x8_type = 0;
  618. //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
  619. // (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
  620. if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
  621. switch(v->s.pict_type) {
  622. case FF_P_TYPE:
  623. if (v->pq < 5) v->tt_index = 0;
  624. else if(v->pq < 13) v->tt_index = 1;
  625. else v->tt_index = 2;
  626. lowquant = (v->pq > 12) ? 0 : 1;
  627. v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
  628. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  629. {
  630. int scale, shift, i;
  631. v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
  632. v->lumscale = get_bits(gb, 6);
  633. v->lumshift = get_bits(gb, 6);
  634. v->use_ic = 1;
  635. /* fill lookup tables for intensity compensation */
  636. if(!v->lumscale) {
  637. scale = -64;
  638. shift = (255 - v->lumshift * 2) << 6;
  639. if(v->lumshift > 31)
  640. shift += 128 << 6;
  641. } else {
  642. scale = v->lumscale + 32;
  643. if(v->lumshift > 31)
  644. shift = (v->lumshift - 64) << 6;
  645. else
  646. shift = v->lumshift << 6;
  647. }
  648. for(i = 0; i < 256; i++) {
  649. v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
  650. v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
  651. }
  652. }
  653. if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
  654. v->s.quarter_sample = 0;
  655. else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  656. if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
  657. v->s.quarter_sample = 0;
  658. else
  659. v->s.quarter_sample = 1;
  660. } else
  661. v->s.quarter_sample = 1;
  662. v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
  663. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  664. v->mv_mode2 == MV_PMODE_MIXED_MV)
  665. || v->mv_mode == MV_PMODE_MIXED_MV)
  666. {
  667. status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
  668. if (status < 0) return -1;
  669. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  670. "Imode: %i, Invert: %i\n", status>>1, status&1);
  671. } else {
  672. v->mv_type_is_raw = 0;
  673. memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
  674. }
  675. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  676. if (status < 0) return -1;
  677. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  678. "Imode: %i, Invert: %i\n", status>>1, status&1);
  679. /* Hopefully this is correct for P frames */
  680. v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
  681. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  682. if (v->dquant)
  683. {
  684. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  685. vop_dquant_decoding(v);
  686. }
  687. v->ttfrm = 0; //FIXME Is that so ?
  688. if (v->vstransform)
  689. {
  690. v->ttmbf = get_bits1(gb);
  691. if (v->ttmbf)
  692. {
  693. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  694. }
  695. } else {
  696. v->ttmbf = 1;
  697. v->ttfrm = TT_8X8;
  698. }
  699. break;
  700. case FF_B_TYPE:
  701. if (v->pq < 5) v->tt_index = 0;
  702. else if(v->pq < 13) v->tt_index = 1;
  703. else v->tt_index = 2;
  704. v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
  705. v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
  706. v->s.mspel = v->s.quarter_sample;
  707. status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
  708. if (status < 0) return -1;
  709. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
  710. "Imode: %i, Invert: %i\n", status>>1, status&1);
  711. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  712. if (status < 0) return -1;
  713. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  714. "Imode: %i, Invert: %i\n", status>>1, status&1);
  715. v->s.mv_table_index = get_bits(gb, 2);
  716. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  717. if (v->dquant)
  718. {
  719. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  720. vop_dquant_decoding(v);
  721. }
  722. v->ttfrm = 0;
  723. if (v->vstransform)
  724. {
  725. v->ttmbf = get_bits1(gb);
  726. if (v->ttmbf)
  727. {
  728. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  729. }
  730. } else {
  731. v->ttmbf = 1;
  732. v->ttfrm = TT_8X8;
  733. }
  734. break;
  735. }
  736. if(!v->x8_type)
  737. {
  738. /* AC Syntax */
  739. v->c_ac_table_index = decode012(gb);
  740. if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
  741. {
  742. v->y_ac_table_index = decode012(gb);
  743. }
  744. /* DC Syntax */
  745. v->s.dc_table_index = get_bits1(gb);
  746. }
  747. if(v->s.pict_type == FF_BI_TYPE) {
  748. v->s.pict_type = FF_B_TYPE;
  749. v->bi_type = 1;
  750. }
  751. return 0;
  752. }
  753. int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
  754. {
  755. int pqindex, lowquant;
  756. int status;
  757. v->p_frame_skipped = 0;
  758. if(v->interlace){
  759. v->fcm = decode012(gb);
  760. if(v->fcm){
  761. if(!v->warn_interlaced++)
  762. av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
  763. return -1;
  764. }
  765. }
  766. switch(get_unary(gb, 0, 4)) {
  767. case 0:
  768. v->s.pict_type = FF_P_TYPE;
  769. break;
  770. case 1:
  771. v->s.pict_type = FF_B_TYPE;
  772. break;
  773. case 2:
  774. v->s.pict_type = FF_I_TYPE;
  775. break;
  776. case 3:
  777. v->s.pict_type = FF_BI_TYPE;
  778. break;
  779. case 4:
  780. v->s.pict_type = FF_P_TYPE; // skipped pic
  781. v->p_frame_skipped = 1;
  782. return 0;
  783. }
  784. if(v->tfcntrflag)
  785. skip_bits(gb, 8);
  786. if(v->broadcast) {
  787. if(!v->interlace || v->psf) {
  788. v->rptfrm = get_bits(gb, 2);
  789. } else {
  790. v->tff = get_bits1(gb);
  791. v->rptfrm = get_bits1(gb);
  792. }
  793. }
  794. if(v->panscanflag) {
  795. //...
  796. }
  797. v->rnd = get_bits1(gb);
  798. if(v->interlace)
  799. v->uvsamp = get_bits1(gb);
  800. if(v->finterpflag) v->interpfrm = get_bits1(gb);
  801. if(v->s.pict_type == FF_B_TYPE) {
  802. v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
  803. v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
  804. if(v->bfraction == 0) {
  805. v->s.pict_type = FF_BI_TYPE; /* XXX: should not happen here */
  806. }
  807. }
  808. pqindex = get_bits(gb, 5);
  809. if(!pqindex) return -1;
  810. v->pqindex = pqindex;
  811. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  812. v->pq = ff_vc1_pquant_table[0][pqindex];
  813. else
  814. v->pq = ff_vc1_pquant_table[1][pqindex];
  815. v->pquantizer = 1;
  816. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  817. v->pquantizer = pqindex < 9;
  818. if (v->quantizer_mode == QUANT_NON_UNIFORM)
  819. v->pquantizer = 0;
  820. v->pqindex = pqindex;
  821. if (pqindex < 9) v->halfpq = get_bits1(gb);
  822. else v->halfpq = 0;
  823. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  824. v->pquantizer = get_bits1(gb);
  825. if(v->postprocflag)
  826. v->postproc = get_bits(gb, 2);
  827. if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0;
  828. if(v->parse_only)
  829. return 0;
  830. switch(v->s.pict_type) {
  831. case FF_I_TYPE:
  832. case FF_BI_TYPE:
  833. status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
  834. if (status < 0) return -1;
  835. av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
  836. "Imode: %i, Invert: %i\n", status>>1, status&1);
  837. v->condover = CONDOVER_NONE;
  838. if(v->overlap && v->pq <= 8) {
  839. v->condover = decode012(gb);
  840. if(v->condover == CONDOVER_SELECT) {
  841. status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
  842. if (status < 0) return -1;
  843. av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
  844. "Imode: %i, Invert: %i\n", status>>1, status&1);
  845. }
  846. }
  847. break;
  848. case FF_P_TYPE:
  849. if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
  850. else v->mvrange = 0;
  851. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  852. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  853. v->range_x = 1 << (v->k_x - 1);
  854. v->range_y = 1 << (v->k_y - 1);
  855. if (v->pq < 5) v->tt_index = 0;
  856. else if(v->pq < 13) v->tt_index = 1;
  857. else v->tt_index = 2;
  858. lowquant = (v->pq > 12) ? 0 : 1;
  859. v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
  860. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  861. {
  862. int scale, shift, i;
  863. v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
  864. v->lumscale = get_bits(gb, 6);
  865. v->lumshift = get_bits(gb, 6);
  866. /* fill lookup tables for intensity compensation */
  867. if(!v->lumscale) {
  868. scale = -64;
  869. shift = (255 - v->lumshift * 2) << 6;
  870. if(v->lumshift > 31)
  871. shift += 128 << 6;
  872. } else {
  873. scale = v->lumscale + 32;
  874. if(v->lumshift > 31)
  875. shift = (v->lumshift - 64) << 6;
  876. else
  877. shift = v->lumshift << 6;
  878. }
  879. for(i = 0; i < 256; i++) {
  880. v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
  881. v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
  882. }
  883. v->use_ic = 1;
  884. }
  885. if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
  886. v->s.quarter_sample = 0;
  887. else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  888. if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
  889. v->s.quarter_sample = 0;
  890. else
  891. v->s.quarter_sample = 1;
  892. } else
  893. v->s.quarter_sample = 1;
  894. v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
  895. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  896. v->mv_mode2 == MV_PMODE_MIXED_MV)
  897. || v->mv_mode == MV_PMODE_MIXED_MV)
  898. {
  899. status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
  900. if (status < 0) return -1;
  901. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  902. "Imode: %i, Invert: %i\n", status>>1, status&1);
  903. } else {
  904. v->mv_type_is_raw = 0;
  905. memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
  906. }
  907. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  908. if (status < 0) return -1;
  909. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  910. "Imode: %i, Invert: %i\n", status>>1, status&1);
  911. /* Hopefully this is correct for P frames */
  912. v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
  913. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  914. if (v->dquant)
  915. {
  916. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  917. vop_dquant_decoding(v);
  918. }
  919. v->ttfrm = 0; //FIXME Is that so ?
  920. if (v->vstransform)
  921. {
  922. v->ttmbf = get_bits1(gb);
  923. if (v->ttmbf)
  924. {
  925. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  926. }
  927. } else {
  928. v->ttmbf = 1;
  929. v->ttfrm = TT_8X8;
  930. }
  931. break;
  932. case FF_B_TYPE:
  933. if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
  934. else v->mvrange = 0;
  935. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  936. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  937. v->range_x = 1 << (v->k_x - 1);
  938. v->range_y = 1 << (v->k_y - 1);
  939. if (v->pq < 5) v->tt_index = 0;
  940. else if(v->pq < 13) v->tt_index = 1;
  941. else v->tt_index = 2;
  942. v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
  943. v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
  944. v->s.mspel = v->s.quarter_sample;
  945. status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
  946. if (status < 0) return -1;
  947. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
  948. "Imode: %i, Invert: %i\n", status>>1, status&1);
  949. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  950. if (status < 0) return -1;
  951. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  952. "Imode: %i, Invert: %i\n", status>>1, status&1);
  953. v->s.mv_table_index = get_bits(gb, 2);
  954. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  955. if (v->dquant)
  956. {
  957. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  958. vop_dquant_decoding(v);
  959. }
  960. v->ttfrm = 0;
  961. if (v->vstransform)
  962. {
  963. v->ttmbf = get_bits1(gb);
  964. if (v->ttmbf)
  965. {
  966. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  967. }
  968. } else {
  969. v->ttmbf = 1;
  970. v->ttfrm = TT_8X8;
  971. }
  972. break;
  973. }
  974. /* AC Syntax */
  975. v->c_ac_table_index = decode012(gb);
  976. if (v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE)
  977. {
  978. v->y_ac_table_index = decode012(gb);
  979. }
  980. /* DC Syntax */
  981. v->s.dc_table_index = get_bits1(gb);
  982. if ((v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) && v->dquant) {
  983. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  984. vop_dquant_decoding(v);
  985. }
  986. v->bi_type = 0;
  987. if(v->s.pict_type == FF_BI_TYPE) {
  988. v->s.pict_type = FF_B_TYPE;
  989. v->bi_type = 1;
  990. }
  991. return 0;
  992. }