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.

1060 lines
36KB

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