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.

1244 lines
44KB

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