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
45KB

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