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.

1055 lines
35KB

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