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.

1249 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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. int w = get_bits(gb, 11);
  355. int h = get_bits(gb, 11);
  356. avcodec_set_dimensions(v->s.avctx, w, h);
  357. skip_bits(gb, 5); //frame rate
  358. v->res_x8 = get_bits1(gb);
  359. if (get_bits1(gb)) { // something to do with DC VLC selection
  360. av_log(avctx, AV_LOG_ERROR, "Unsupported sprite feature\n");
  361. return -1;
  362. }
  363. skip_bits(gb, 3); //slice code
  364. v->res_rtm_flag = 0;
  365. } else {
  366. v->res_rtm_flag = get_bits1(gb); //reserved
  367. }
  368. if (!v->res_rtm_flag) {
  369. // av_log(avctx, AV_LOG_ERROR,
  370. // "0 for reserved RES_RTM_FLAG is forbidden\n");
  371. av_log(avctx, AV_LOG_ERROR,
  372. "Old WMV3 version detected, some frames may be decoded incorrectly\n");
  373. //return -1;
  374. }
  375. //TODO: figure out what they mean (always 0x402F)
  376. if (!v->res_fasttx)
  377. skip_bits(gb, 16);
  378. av_log(avctx, AV_LOG_DEBUG,
  379. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  380. "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
  381. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  382. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  383. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  384. v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
  385. v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
  386. v->dquant, v->quantizer_mode, avctx->max_b_frames);
  387. return 0;
  388. }
  389. static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
  390. {
  391. int w, h;
  392. v->res_rtm_flag = 1;
  393. v->level = get_bits(gb, 3);
  394. if (v->level >= 5) {
  395. av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
  396. }
  397. v->chromaformat = get_bits(gb, 2);
  398. if (v->chromaformat != 1) {
  399. av_log(v->s.avctx, AV_LOG_ERROR,
  400. "Only 4:2:0 chroma format supported\n");
  401. return -1;
  402. }
  403. // (fps-2)/4 (->30)
  404. v->frmrtq_postproc = get_bits(gb, 3); //common
  405. // (bitrate-32kbps)/64kbps
  406. v->bitrtq_postproc = get_bits(gb, 5); //common
  407. v->postprocflag = get_bits1(gb); //common
  408. w = (get_bits(gb, 12) + 1) << 1;
  409. h = (get_bits(gb, 12) + 1) << 1;
  410. avcodec_set_dimensions(v->s.avctx, w, h);
  411. v->broadcast = get_bits1(gb);
  412. v->interlace = get_bits1(gb);
  413. v->tfcntrflag = get_bits1(gb);
  414. v->finterpflag = get_bits1(gb);
  415. skip_bits1(gb); // reserved
  416. av_log(v->s.avctx, AV_LOG_DEBUG,
  417. "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  418. "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
  419. "TFCTRflag=%i, FINTERPflag=%i\n",
  420. v->level, v->frmrtq_postproc, v->bitrtq_postproc,
  421. v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
  422. v->tfcntrflag, v->finterpflag);
  423. v->psf = get_bits1(gb);
  424. if (v->psf) { //PsF, 6.1.13
  425. av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
  426. return -1;
  427. }
  428. v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
  429. if (get_bits1(gb)) { //Display Info - decoding is not affected by it
  430. int w, h, ar = 0;
  431. av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
  432. w = get_bits(gb, 14) + 1;
  433. h = get_bits(gb, 14) + 1;
  434. av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
  435. if (get_bits1(gb))
  436. ar = get_bits(gb, 4);
  437. if (ar && ar < 14) {
  438. v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
  439. } else if (ar == 15) {
  440. w = get_bits(gb, 8) + 1;
  441. h = get_bits(gb, 8) + 1;
  442. v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
  443. } else {
  444. av_reduce(&v->s.avctx->sample_aspect_ratio.num,
  445. &v->s.avctx->sample_aspect_ratio.den,
  446. v->s.avctx->height * w,
  447. v->s.avctx->width * h,
  448. 1 << 30);
  449. }
  450. av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
  451. v->s.avctx->sample_aspect_ratio.num,
  452. v->s.avctx->sample_aspect_ratio.den);
  453. if (get_bits1(gb)) { //framerate stuff
  454. if (get_bits1(gb)) {
  455. v->s.avctx->time_base.num = 32;
  456. v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
  457. } else {
  458. int nr, dr;
  459. nr = get_bits(gb, 8);
  460. dr = get_bits(gb, 4);
  461. if (nr && nr < 8 && dr && dr < 3) {
  462. v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
  463. v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
  464. }
  465. }
  466. if (v->broadcast) { // Pulldown may be present
  467. v->s.avctx->time_base.den *= 2;
  468. v->s.avctx->ticks_per_frame = 2;
  469. }
  470. }
  471. if (get_bits1(gb)) {
  472. v->color_prim = get_bits(gb, 8);
  473. v->transfer_char = get_bits(gb, 8);
  474. v->matrix_coef = get_bits(gb, 8);
  475. }
  476. }
  477. v->hrd_param_flag = get_bits1(gb);
  478. if (v->hrd_param_flag) {
  479. int i;
  480. v->hrd_num_leaky_buckets = get_bits(gb, 5);
  481. skip_bits(gb, 4); //bitrate exponent
  482. skip_bits(gb, 4); //buffer size exponent
  483. for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
  484. skip_bits(gb, 16); //hrd_rate[n]
  485. skip_bits(gb, 16); //hrd_buffer[n]
  486. }
  487. }
  488. return 0;
  489. }
  490. int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb)
  491. {
  492. int i;
  493. av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
  494. v->broken_link = get_bits1(gb);
  495. v->closed_entry = get_bits1(gb);
  496. v->panscanflag = get_bits1(gb);
  497. v->refdist_flag = get_bits1(gb);
  498. v->s.loop_filter = get_bits1(gb);
  499. v->fastuvmc = get_bits1(gb);
  500. v->extended_mv = get_bits1(gb);
  501. v->dquant = get_bits(gb, 2);
  502. v->vstransform = get_bits1(gb);
  503. v->overlap = get_bits1(gb);
  504. v->quantizer_mode = get_bits(gb, 2);
  505. if (v->hrd_param_flag) {
  506. for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
  507. skip_bits(gb, 8); //hrd_full[n]
  508. }
  509. }
  510. if(get_bits1(gb)){
  511. int w = (get_bits(gb, 12)+1)<<1;
  512. int h = (get_bits(gb, 12)+1)<<1;
  513. avcodec_set_dimensions(avctx, w, h);
  514. }
  515. if (v->extended_mv)
  516. v->extended_dmv = get_bits1(gb);
  517. if ((v->range_mapy_flag = get_bits1(gb))) {
  518. av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
  519. v->range_mapy = get_bits(gb, 3);
  520. }
  521. if ((v->range_mapuv_flag = get_bits1(gb))) {
  522. av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
  523. v->range_mapuv = get_bits(gb, 3);
  524. }
  525. av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
  526. "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
  527. "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
  528. "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
  529. v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
  530. v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
  531. return 0;
  532. }
  533. int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
  534. {
  535. int pqindex, lowquant, status;
  536. if (v->finterpflag)
  537. v->interpfrm = get_bits1(gb);
  538. skip_bits(gb, 2); //framecnt unused
  539. v->rangeredfrm = 0;
  540. if (v->rangered)
  541. v->rangeredfrm = get_bits1(gb);
  542. v->s.pict_type = get_bits1(gb);
  543. if (v->s.avctx->max_b_frames) {
  544. if (!v->s.pict_type) {
  545. if (get_bits1(gb))
  546. v->s.pict_type = AV_PICTURE_TYPE_I;
  547. else
  548. v->s.pict_type = AV_PICTURE_TYPE_B;
  549. } else
  550. v->s.pict_type = AV_PICTURE_TYPE_P;
  551. } else
  552. v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  553. v->bi_type = 0;
  554. if (v->s.pict_type == AV_PICTURE_TYPE_B) {
  555. v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
  556. v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
  557. if (v->bfraction == 0) {
  558. v->s.pict_type = AV_PICTURE_TYPE_BI;
  559. }
  560. }
  561. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
  562. skip_bits(gb, 7); // skip buffer fullness
  563. if (v->parse_only)
  564. return 0;
  565. /* calculate RND */
  566. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
  567. v->rnd = 1;
  568. if (v->s.pict_type == AV_PICTURE_TYPE_P)
  569. v->rnd ^= 1;
  570. /* Quantizer stuff */
  571. pqindex = get_bits(gb, 5);
  572. if (!pqindex)
  573. 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)
  585. v->halfpq = get_bits1(gb);
  586. else
  587. v->halfpq = 0;
  588. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  589. v->pquantizer = get_bits1(gb);
  590. v->dquantfrm = 0;
  591. if (v->extended_mv == 1)
  592. v->mvrange = get_unary(gb, 0, 3);
  593. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  594. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  595. v->range_x = 1 << (v->k_x - 1);
  596. v->range_y = 1 << (v->k_y - 1);
  597. if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B)
  598. v->respic = get_bits(gb, 2);
  599. if (v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)) {
  600. v->x8_type = get_bits1(gb);
  601. } else
  602. v->x8_type = 0;
  603. //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
  604. // (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);
  605. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
  606. v->use_ic = 0;
  607. switch (v->s.pict_type) {
  608. case AV_PICTURE_TYPE_P:
  609. if (v->pq < 5) v->tt_index = 0;
  610. else if (v->pq < 13) v->tt_index = 1;
  611. else v->tt_index = 2;
  612. lowquant = (v->pq > 12) ? 0 : 1;
  613. v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
  614. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  615. int scale, shift, i;
  616. v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
  617. v->lumscale = get_bits(gb, 6);
  618. v->lumshift = get_bits(gb, 6);
  619. v->use_ic = 1;
  620. /* fill lookup tables for intensity compensation */
  621. if (!v->lumscale) {
  622. scale = -64;
  623. shift = (255 - v->lumshift * 2) << 6;
  624. if (v->lumshift > 31)
  625. shift += 128 << 6;
  626. } else {
  627. scale = v->lumscale + 32;
  628. if (v->lumshift > 31)
  629. shift = (v->lumshift - 64) << 6;
  630. else
  631. shift = v->lumshift << 6;
  632. }
  633. for (i = 0; i < 256; i++) {
  634. v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
  635. v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
  636. }
  637. }
  638. v->qs_last = v->s.quarter_sample;
  639. if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
  640. v->s.quarter_sample = 0;
  641. else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  642. if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
  643. v->s.quarter_sample = 0;
  644. else
  645. v->s.quarter_sample = 1;
  646. } else
  647. v->s.quarter_sample = 1;
  648. 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));
  649. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  650. v->mv_mode2 == MV_PMODE_MIXED_MV) ||
  651. v->mv_mode == MV_PMODE_MIXED_MV) {
  652. status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
  653. if (status < 0)
  654. return -1;
  655. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  656. "Imode: %i, Invert: %i\n", status>>1, status&1);
  657. } else {
  658. v->mv_type_is_raw = 0;
  659. memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
  660. }
  661. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  662. if (status < 0)
  663. return -1;
  664. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  665. "Imode: %i, Invert: %i\n", status>>1, status&1);
  666. /* Hopefully this is correct for P frames */
  667. v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
  668. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  669. if (v->dquant) {
  670. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  671. vop_dquant_decoding(v);
  672. }
  673. v->ttfrm = 0; //FIXME Is that so ?
  674. if (v->vstransform) {
  675. v->ttmbf = get_bits1(gb);
  676. if (v->ttmbf) {
  677. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  678. }
  679. } else {
  680. v->ttmbf = 1;
  681. v->ttfrm = TT_8X8;
  682. }
  683. break;
  684. case AV_PICTURE_TYPE_B:
  685. if (v->pq < 5) v->tt_index = 0;
  686. else if (v->pq < 13) v->tt_index = 1;
  687. else v->tt_index = 2;
  688. v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
  689. v->qs_last = v->s.quarter_sample;
  690. v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
  691. v->s.mspel = v->s.quarter_sample;
  692. status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
  693. if (status < 0)
  694. return -1;
  695. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
  696. "Imode: %i, Invert: %i\n", status>>1, status&1);
  697. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  698. if (status < 0)
  699. return -1;
  700. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  701. "Imode: %i, Invert: %i\n", status>>1, status&1);
  702. v->s.mv_table_index = get_bits(gb, 2);
  703. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  704. if (v->dquant) {
  705. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  706. vop_dquant_decoding(v);
  707. }
  708. v->ttfrm = 0;
  709. if (v->vstransform) {
  710. v->ttmbf = get_bits1(gb);
  711. if (v->ttmbf) {
  712. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  713. }
  714. } else {
  715. v->ttmbf = 1;
  716. v->ttfrm = TT_8X8;
  717. }
  718. break;
  719. }
  720. if (!v->x8_type) {
  721. /* AC Syntax */
  722. v->c_ac_table_index = decode012(gb);
  723. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
  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->numref=0;
  760. v->fcm=0;
  761. v->field_mode=0;
  762. v->p_frame_skipped = 0;
  763. if (v->second_field) {
  764. v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  765. if (v->fptype & 4)
  766. v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
  767. v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
  768. if (!v->pic_header_flag)
  769. goto parse_common_info;
  770. }
  771. v->field_mode = 0;
  772. if (v->interlace) {
  773. v->fcm = decode012(gb);
  774. if (v->fcm) {
  775. if (v->fcm == 2)
  776. v->field_mode = 1;
  777. if (!v->warn_interlaced++)
  778. av_log(v->s.avctx, AV_LOG_ERROR,
  779. "Interlaced frames/fields support is incomplete\n");
  780. }
  781. } else {
  782. v->fcm = 0;
  783. }
  784. if (v->field_mode) {
  785. v->fptype = get_bits(gb, 3);
  786. v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  787. if (v->fptype & 4) // B-picture
  788. v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
  789. } else {
  790. switch (get_unary(gb, 0, 4)) {
  791. case 0:
  792. v->s.pict_type = AV_PICTURE_TYPE_P;
  793. break;
  794. case 1:
  795. v->s.pict_type = AV_PICTURE_TYPE_B;
  796. break;
  797. case 2:
  798. v->s.pict_type = AV_PICTURE_TYPE_I;
  799. break;
  800. case 3:
  801. v->s.pict_type = AV_PICTURE_TYPE_BI;
  802. break;
  803. case 4:
  804. v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
  805. v->p_frame_skipped = 1;
  806. break;
  807. }
  808. }
  809. if (v->tfcntrflag)
  810. skip_bits(gb, 8);
  811. if (v->broadcast) {
  812. if (!v->interlace || v->psf) {
  813. v->rptfrm = get_bits(gb, 2);
  814. } else {
  815. v->tff = get_bits1(gb);
  816. v->rff = get_bits1(gb);
  817. }
  818. }
  819. if (v->panscanflag) {
  820. av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
  821. //...
  822. }
  823. if (v->p_frame_skipped) {
  824. return 0;
  825. }
  826. v->rnd = get_bits1(gb);
  827. if (v->interlace)
  828. v->uvsamp = get_bits1(gb);
  829. if (v->field_mode) {
  830. if (!v->refdist_flag)
  831. v->refdist = 0;
  832. else {
  833. if ((v->s.pict_type != AV_PICTURE_TYPE_B)
  834. && (v->s.pict_type != AV_PICTURE_TYPE_BI)) {
  835. v->refdist = get_bits(gb, 2);
  836. if (v->refdist == 3)
  837. v->refdist += get_unary(gb, 0, 16);
  838. } else {
  839. v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
  840. v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
  841. v->frfd = (v->bfraction * v->refdist) >> 8;
  842. v->brfd = v->refdist - v->frfd - 1;
  843. if (v->brfd < 0)
  844. v->brfd = 0;
  845. }
  846. }
  847. goto parse_common_info;
  848. }
  849. if (v->finterpflag)
  850. v->interpfrm = get_bits1(gb);
  851. if (v->s.pict_type == AV_PICTURE_TYPE_B) {
  852. v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
  853. v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
  854. if (v->bfraction == 0) {
  855. v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
  856. }
  857. }
  858. parse_common_info:
  859. if (v->field_mode)
  860. v->cur_field_type = !(v->tff ^ v->second_field);
  861. pqindex = get_bits(gb, 5);
  862. if (!pqindex)
  863. return -1;
  864. v->pqindex = pqindex;
  865. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  866. v->pq = ff_vc1_pquant_table[0][pqindex];
  867. else
  868. v->pq = ff_vc1_pquant_table[1][pqindex];
  869. v->pquantizer = 1;
  870. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  871. v->pquantizer = pqindex < 9;
  872. if (v->quantizer_mode == QUANT_NON_UNIFORM)
  873. v->pquantizer = 0;
  874. v->pqindex = pqindex;
  875. if (pqindex < 9)
  876. v->halfpq = get_bits1(gb);
  877. else
  878. v->halfpq = 0;
  879. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  880. v->pquantizer = get_bits1(gb);
  881. if (v->postprocflag)
  882. v->postproc = get_bits(gb, 2);
  883. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
  884. v->use_ic = 0;
  885. if (v->parse_only)
  886. return 0;
  887. switch (v->s.pict_type) {
  888. case AV_PICTURE_TYPE_I:
  889. case AV_PICTURE_TYPE_BI:
  890. if (v->fcm == 1) { //interlace frame picture
  891. status = bitplane_decoding(v->fieldtx_plane, &v->fieldtx_is_raw, v);
  892. if (status < 0)
  893. return -1;
  894. av_log(v->s.avctx, AV_LOG_DEBUG, "FIELDTX plane encoding: "
  895. "Imode: %i, Invert: %i\n", status>>1, status&1);
  896. }
  897. status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
  898. if (status < 0)
  899. return -1;
  900. av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
  901. "Imode: %i, Invert: %i\n", status>>1, status&1);
  902. v->condover = CONDOVER_NONE;
  903. if (v->overlap && v->pq <= 8) {
  904. v->condover = decode012(gb);
  905. if (v->condover == CONDOVER_SELECT) {
  906. status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
  907. if (status < 0)
  908. return -1;
  909. av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
  910. "Imode: %i, Invert: %i\n", status>>1, status&1);
  911. }
  912. }
  913. break;
  914. case AV_PICTURE_TYPE_P:
  915. if (v->field_mode) {
  916. v->numref = get_bits1(gb);
  917. if (!v->numref) {
  918. v->reffield = get_bits1(gb);
  919. v->ref_field_type[0] = v->reffield ^ !v->cur_field_type;
  920. }
  921. }
  922. if (v->extended_mv)
  923. v->mvrange = get_unary(gb, 0, 3);
  924. else
  925. v->mvrange = 0;
  926. if (v->interlace) {
  927. if (v->extended_dmv)
  928. v->dmvrange = get_unary(gb, 0, 3);
  929. else
  930. v->dmvrange = 0;
  931. if (v->fcm == 1) { // interlaced frame picture
  932. v->fourmvswitch = get_bits1(gb);
  933. v->intcomp = get_bits1(gb);
  934. if (v->intcomp) {
  935. v->lumscale = get_bits(gb, 6);
  936. v->lumshift = get_bits(gb, 6);
  937. INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
  938. }
  939. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  940. av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
  941. "Imode: %i, Invert: %i\n", status>>1, status&1);
  942. mbmodetab = get_bits(gb, 2);
  943. if (v->fourmvswitch)
  944. v->mbmode_vlc = &ff_vc1_intfr_4mv_mbmode_vlc[mbmodetab];
  945. else
  946. v->mbmode_vlc = &ff_vc1_intfr_non4mv_mbmode_vlc[mbmodetab];
  947. imvtab = get_bits(gb, 2);
  948. v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
  949. // interlaced p-picture cbpcy range is [1, 63]
  950. icbptab = get_bits(gb, 3);
  951. v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
  952. twomvbptab = get_bits(gb, 2);
  953. v->twomvbp_vlc = &ff_vc1_2mv_block_pattern_vlc[twomvbptab];
  954. if (v->fourmvswitch) {
  955. fourmvbptab = get_bits(gb, 2);
  956. v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
  957. }
  958. }
  959. }
  960. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  961. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  962. v->range_x = 1 << (v->k_x - 1);
  963. v->range_y = 1 << (v->k_y - 1);
  964. if (v->pq < 5)
  965. v->tt_index = 0;
  966. else if (v->pq < 13)
  967. v->tt_index = 1;
  968. else
  969. v->tt_index = 2;
  970. if (v->fcm != 1) {
  971. int mvmode;
  972. mvmode = get_unary(gb, 1, 4);
  973. lowquant = (v->pq > 12) ? 0 : 1;
  974. v->mv_mode = ff_vc1_mv_pmode_table[lowquant][mvmode];
  975. if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  976. int mvmode2;
  977. mvmode2 = get_unary(gb, 1, 3);
  978. v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
  979. if (v->field_mode)
  980. v->intcompfield = decode210(gb);
  981. v->lumscale = get_bits(gb, 6);
  982. v->lumshift = get_bits(gb, 6);
  983. INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
  984. if ((v->field_mode) && !v->intcompfield) {
  985. v->lumscale2 = get_bits(gb, 6);
  986. v->lumshift2 = get_bits(gb, 6);
  987. INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
  988. }
  989. v->use_ic = 1;
  990. }
  991. v->qs_last = v->s.quarter_sample;
  992. if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
  993. v->s.quarter_sample = 0;
  994. else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
  995. if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
  996. v->s.quarter_sample = 0;
  997. else
  998. v->s.quarter_sample = 1;
  999. } else
  1000. v->s.quarter_sample = 1;
  1001. v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN
  1002. || (v->mv_mode == MV_PMODE_INTENSITY_COMP
  1003. && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
  1004. }
  1005. if (v->fcm == 0) { // progressive
  1006. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1007. v->mv_mode2 == MV_PMODE_MIXED_MV)
  1008. || v->mv_mode == MV_PMODE_MIXED_MV) {
  1009. status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
  1010. if (status < 0)
  1011. return -1;
  1012. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1013. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1014. } else {
  1015. v->mv_type_is_raw = 0;
  1016. memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
  1017. }
  1018. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  1019. if (status < 0)
  1020. return -1;
  1021. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1022. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1023. /* Hopefully this is correct for P frames */
  1024. v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
  1025. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  1026. } else if (v->fcm == 1) { // frame interlaced
  1027. v->qs_last = v->s.quarter_sample;
  1028. v->s.quarter_sample = 1;
  1029. v->s.mspel = 1;
  1030. } else { // field interlaced
  1031. mbmodetab = get_bits(gb, 3);
  1032. imvtab = get_bits(gb, 2 + v->numref);
  1033. if (!v->numref)
  1034. v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
  1035. else
  1036. v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
  1037. icbptab = get_bits(gb, 3);
  1038. v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
  1039. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1040. v->mv_mode2 == MV_PMODE_MIXED_MV) || v->mv_mode == MV_PMODE_MIXED_MV) {
  1041. fourmvbptab = get_bits(gb, 2);
  1042. v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
  1043. v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
  1044. } else {
  1045. v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
  1046. }
  1047. }
  1048. if (v->dquant) {
  1049. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  1050. vop_dquant_decoding(v);
  1051. }
  1052. v->ttfrm = 0; //FIXME Is that so ?
  1053. if (v->vstransform) {
  1054. v->ttmbf = get_bits1(gb);
  1055. if (v->ttmbf) {
  1056. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  1057. }
  1058. } else {
  1059. v->ttmbf = 1;
  1060. v->ttfrm = TT_8X8;
  1061. }
  1062. break;
  1063. case AV_PICTURE_TYPE_B:
  1064. // TODO: implement interlaced frame B picture decoding
  1065. if (v->fcm == 1)
  1066. return -1;
  1067. if (v->extended_mv)
  1068. v->mvrange = get_unary(gb, 0, 3);
  1069. else
  1070. v->mvrange = 0;
  1071. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  1072. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  1073. v->range_x = 1 << (v->k_x - 1);
  1074. v->range_y = 1 << (v->k_y - 1);
  1075. if (v->pq < 5)
  1076. v->tt_index = 0;
  1077. else if (v->pq < 13)
  1078. v->tt_index = 1;
  1079. else
  1080. v->tt_index = 2;
  1081. if (v->field_mode) {
  1082. int mvmode;
  1083. av_log(v->s.avctx, AV_LOG_ERROR, "B Fields do not work currently\n");
  1084. return -1;
  1085. if (v->extended_dmv)
  1086. v->dmvrange = get_unary(gb, 0, 3);
  1087. mvmode = get_unary(gb, 1, 3);
  1088. lowquant = (v->pq > 12) ? 0 : 1;
  1089. v->mv_mode = ff_vc1_mv_pmode_table2[lowquant][mvmode];
  1090. v->qs_last = v->s.quarter_sample;
  1091. v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV || v->mv_mode == MV_PMODE_MIXED_MV);
  1092. v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || v->mv_mode == MV_PMODE_1MV_HPEL);
  1093. status = bitplane_decoding(v->forward_mb_plane, &v->fmb_is_raw, v);
  1094. if (status < 0)
  1095. return -1;
  1096. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Forward Type plane encoding: "
  1097. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1098. mbmodetab = get_bits(gb, 3);
  1099. if (v->mv_mode == MV_PMODE_MIXED_MV)
  1100. v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
  1101. else
  1102. v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
  1103. imvtab = get_bits(gb, 3);
  1104. v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
  1105. icbptab = get_bits(gb, 3);
  1106. v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
  1107. if (v->mv_mode == MV_PMODE_MIXED_MV) {
  1108. fourmvbptab = get_bits(gb, 2);
  1109. v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
  1110. }
  1111. v->numref = 1; // interlaced field B pictures are always 2-ref
  1112. } else {
  1113. v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
  1114. v->qs_last = v->s.quarter_sample;
  1115. v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
  1116. v->s.mspel = v->s.quarter_sample;
  1117. status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
  1118. if (status < 0)
  1119. return -1;
  1120. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
  1121. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1122. status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
  1123. if (status < 0)
  1124. return -1;
  1125. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1126. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1127. v->s.mv_table_index = get_bits(gb, 2);
  1128. v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  1129. }
  1130. if (v->dquant) {
  1131. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  1132. vop_dquant_decoding(v);
  1133. }
  1134. v->ttfrm = 0;
  1135. if (v->vstransform) {
  1136. v->ttmbf = get_bits1(gb);
  1137. if (v->ttmbf) {
  1138. v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
  1139. }
  1140. } else {
  1141. v->ttmbf = 1;
  1142. v->ttfrm = TT_8X8;
  1143. }
  1144. break;
  1145. }
  1146. /* AC Syntax */
  1147. v->c_ac_table_index = decode012(gb);
  1148. if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
  1149. v->y_ac_table_index = decode012(gb);
  1150. }
  1151. /* DC Syntax */
  1152. v->s.dc_table_index = get_bits1(gb);
  1153. if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
  1154. && v->dquant) {
  1155. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  1156. vop_dquant_decoding(v);
  1157. }
  1158. v->bi_type = 0;
  1159. if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
  1160. v->s.pict_type = AV_PICTURE_TYPE_B;
  1161. v->bi_type = 1;
  1162. }
  1163. return 0;
  1164. }