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.

2674 lines
85KB

  1. /*
  2. * VC-1 and WMV3 decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /**
  22. * @file vc1.c
  23. * VC-1 and WMV3 decoder
  24. *
  25. */
  26. #include "common.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "vc1data.h"
  31. #include "vc1acdata.h"
  32. #undef NDEBUG
  33. #include <assert.h>
  34. extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
  35. extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
  36. extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
  37. #define MB_INTRA_VLC_BITS 9
  38. extern VLC ff_msmp4_mb_i_vlc;
  39. extern const uint16_t ff_msmp4_mb_i_table[64][2];
  40. #define DC_VLC_BITS 9
  41. #define AC_VLC_BITS 9
  42. static const uint16_t table_mb_intra[64][2];
  43. /** Available Profiles */
  44. //@{
  45. enum Profile {
  46. PROFILE_SIMPLE,
  47. PROFILE_MAIN,
  48. PROFILE_COMPLEX, ///< TODO: WMV9 specific
  49. PROFILE_ADVANCED
  50. };
  51. //@}
  52. /** Sequence quantizer mode */
  53. //@{
  54. enum QuantMode {
  55. QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
  56. QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
  57. QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
  58. QUANT_UNIFORM ///< Uniform quant used for all frames
  59. };
  60. //@}
  61. /** Where quant can be changed */
  62. //@{
  63. enum DQProfile {
  64. DQPROFILE_FOUR_EDGES,
  65. DQPROFILE_DOUBLE_EDGES,
  66. DQPROFILE_SINGLE_EDGE,
  67. DQPROFILE_ALL_MBS
  68. };
  69. //@}
  70. /** @name Where quant can be changed
  71. */
  72. //@{
  73. enum DQSingleEdge {
  74. DQSINGLE_BEDGE_LEFT,
  75. DQSINGLE_BEDGE_TOP,
  76. DQSINGLE_BEDGE_RIGHT,
  77. DQSINGLE_BEDGE_BOTTOM
  78. };
  79. //@}
  80. /** Which pair of edges is quantized with ALTPQUANT */
  81. //@{
  82. enum DQDoubleEdge {
  83. DQDOUBLE_BEDGE_TOPLEFT,
  84. DQDOUBLE_BEDGE_TOPRIGHT,
  85. DQDOUBLE_BEDGE_BOTTOMRIGHT,
  86. DQDOUBLE_BEDGE_BOTTOMLEFT
  87. };
  88. //@}
  89. /** MV modes for P frames */
  90. //@{
  91. enum MVModes {
  92. MV_PMODE_1MV_HPEL_BILIN,
  93. MV_PMODE_1MV,
  94. MV_PMODE_1MV_HPEL,
  95. MV_PMODE_MIXED_MV,
  96. MV_PMODE_INTENSITY_COMP
  97. };
  98. //@}
  99. /** @name MV types for B frames */
  100. //@{
  101. enum BMVTypes {
  102. BMV_TYPE_BACKWARD,
  103. BMV_TYPE_FORWARD,
  104. BMV_TYPE_INTERPOLATED = 3 //XXX: ??
  105. };
  106. //@}
  107. /** @name Block types for P/B frames */
  108. //@{
  109. enum TransformTypes {
  110. TT_8X8,
  111. TT_8X4_BOTTOM,
  112. TT_8X4_TOP,
  113. TT_8X4, //Both halves
  114. TT_4X8_RIGHT,
  115. TT_4X8_LEFT,
  116. TT_4X8, //Both halves
  117. TT_4X4
  118. };
  119. //@}
  120. /** Table for conversion between TTBLK and TTMB */
  121. static const int ttblk_to_tt[3][8] = {
  122. { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
  123. { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
  124. { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
  125. };
  126. /** MV P mode - the 5th element is only used for mode 1 */
  127. static const uint8_t mv_pmode_table[2][5] = {
  128. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
  129. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
  130. };
  131. /** One more frame type */
  132. #define BI_TYPE 7
  133. static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
  134. fps_dr[2] = { 1000, 1001 };
  135. static const uint8_t pquant_table[3][32] = {
  136. { /* Implicit quantizer */
  137. 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
  138. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
  139. },
  140. { /* Explicit quantizer, pquantizer uniform */
  141. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  142. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  143. },
  144. { /* Explicit quantizer, pquantizer non-uniform */
  145. 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  146. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
  147. }
  148. };
  149. /** @name VC-1 VLC tables and defines
  150. * @todo TODO move this into the context
  151. */
  152. //@{
  153. #define VC1_BFRACTION_VLC_BITS 7
  154. static VLC vc1_bfraction_vlc;
  155. #define VC1_IMODE_VLC_BITS 4
  156. static VLC vc1_imode_vlc;
  157. #define VC1_NORM2_VLC_BITS 3
  158. static VLC vc1_norm2_vlc;
  159. #define VC1_NORM6_VLC_BITS 9
  160. static VLC vc1_norm6_vlc;
  161. /* Could be optimized, one table only needs 8 bits */
  162. #define VC1_TTMB_VLC_BITS 9 //12
  163. static VLC vc1_ttmb_vlc[3];
  164. #define VC1_MV_DIFF_VLC_BITS 9 //15
  165. static VLC vc1_mv_diff_vlc[4];
  166. #define VC1_CBPCY_P_VLC_BITS 9 //14
  167. static VLC vc1_cbpcy_p_vlc[4];
  168. #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
  169. static VLC vc1_4mv_block_pattern_vlc[4];
  170. #define VC1_TTBLK_VLC_BITS 5
  171. static VLC vc1_ttblk_vlc[3];
  172. #define VC1_SUBBLKPAT_VLC_BITS 6
  173. static VLC vc1_subblkpat_vlc[3];
  174. static VLC vc1_ac_coeff_table[8];
  175. //@}
  176. enum CodingSet {
  177. CS_HIGH_MOT_INTRA = 0,
  178. CS_HIGH_MOT_INTER,
  179. CS_LOW_MOT_INTRA,
  180. CS_LOW_MOT_INTER,
  181. CS_MID_RATE_INTRA,
  182. CS_MID_RATE_INTER,
  183. CS_HIGH_RATE_INTRA,
  184. CS_HIGH_RATE_INTER
  185. };
  186. /** Bitplane struct
  187. * We mainly need data and is_raw, so this struct could be avoided
  188. * to save a level of indirection; feel free to modify
  189. * @fixme For now, stride=width
  190. * @warning Data are bits, either 1 or 0
  191. */
  192. typedef struct BitPlane {
  193. uint8_t *data; ///< Data buffer
  194. int width; ///< Width of the buffer
  195. int stride; ///< Stride of the buffer
  196. int height; ///< Plane height
  197. uint8_t is_raw; ///< Bit values must be read at MB level
  198. } BitPlane;
  199. /** Block data for DC/AC prediction
  200. */
  201. typedef struct Block {
  202. uint16_t dc;
  203. int16_t hor_ac[7];
  204. int16_t vert_ac[7];
  205. int16_t dcstep, step;
  206. } Block;
  207. /** The VC1 Context
  208. * @fixme Change size wherever another size is more efficient
  209. * Many members are only used for Advanced Profile
  210. */
  211. typedef struct VC1Context{
  212. MpegEncContext s;
  213. int bits;
  214. /** Simple/Main Profile sequence header */
  215. //@{
  216. int res_sm; ///< reserved, 2b
  217. int res_x8; ///< reserved
  218. int multires; ///< frame-level RESPIC syntax element present
  219. int res_fasttx; ///< reserved, always 1
  220. int res_transtab; ///< reserved, always 0
  221. int rangered; ///< RANGEREDFRM (range reduction) syntax element present
  222. ///< at frame level
  223. int res_rtm_flag; ///< reserved, set to 1
  224. int reserved; ///< reserved
  225. //@}
  226. /** Advanced Profile */
  227. //@{
  228. int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
  229. int chromaformat; ///< 2bits, 2=4:2:0, only defined
  230. int postprocflag; ///< Per-frame processing suggestion flag present
  231. int broadcast; ///< TFF/RFF present
  232. int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
  233. int tfcntrflag; ///< TFCNTR present
  234. int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
  235. int extended_dmv; ///< Additional extended dmv range at P/B frame-level
  236. int color_prim; ///< 8bits, chroma coordinates of the color primaries
  237. int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
  238. int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
  239. int hrd_param_flag; ///< Presence of Hypothetical Reference
  240. ///< Decoder parameters
  241. //@}
  242. /** Sequence header data for all Profiles
  243. * TODO: choose between ints, uint8_ts and monobit flags
  244. */
  245. //@{
  246. int profile; ///< 2bits, Profile
  247. int frmrtq_postproc; ///< 3bits,
  248. int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
  249. int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
  250. int extended_mv; ///< Ext MV in P/B (not in Simple)
  251. int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
  252. int vstransform; ///< variable-size [48]x[48] transform type + info
  253. int overlap; ///< overlapped transforms in use
  254. int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
  255. int finterpflag; ///< INTERPFRM present
  256. //@}
  257. /** Frame decoding info for all profiles */
  258. //@{
  259. uint8_t mv_mode; ///< MV coding monde
  260. uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
  261. int k_x; ///< Number of bits for MVs (depends on MV range)
  262. int k_y; ///< Number of bits for MVs (depends on MV range)
  263. int range_x, range_y; ///< MV range
  264. uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
  265. /** pquant parameters */
  266. //@{
  267. uint8_t dquantfrm;
  268. uint8_t dqprofile;
  269. uint8_t dqsbedge;
  270. uint8_t dqbilevel;
  271. //@}
  272. /** AC coding set indexes
  273. * @see 8.1.1.10, p(1)10
  274. */
  275. //@{
  276. int c_ac_table_index; ///< Chroma index from ACFRM element
  277. int y_ac_table_index; ///< Luma index from AC2FRM element
  278. //@}
  279. int ttfrm; ///< Transform type info present at frame level
  280. uint8_t ttmbf; ///< Transform type flag
  281. int ttmb; ///< Transform type
  282. uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
  283. int codingset; ///< index of current table set from 11.8 to use for luma block decoding
  284. int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
  285. int pqindex; ///< raw pqindex used in coding set selection
  286. /** Luma compensation parameters */
  287. //@{
  288. uint8_t lumscale;
  289. uint8_t lumshift;
  290. //@}
  291. int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
  292. uint8_t halfpq; ///< Uniform quant over image and qp+.5
  293. uint8_t respic; ///< Frame-level flag for resized images
  294. int buffer_fullness; ///< HRD info
  295. /** Ranges:
  296. * -# 0 -> [-64n 63.f] x [-32, 31.f]
  297. * -# 1 -> [-128, 127.f] x [-64, 63.f]
  298. * -# 2 -> [-512, 511.f] x [-128, 127.f]
  299. * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
  300. */
  301. uint8_t mvrange;
  302. uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
  303. uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
  304. VLC *cbpcy_vlc; ///< CBPCY VLC table
  305. int tt_index; ///< Index for Transform Type tables
  306. BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
  307. BitPlane skip_mb_plane; ///< bitplane for skipped MBs
  308. BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
  309. /** Frame decoding info for S/M profiles only */
  310. //@{
  311. uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
  312. uint8_t interpfrm;
  313. //@}
  314. /** Frame decoding info for Advanced profile */
  315. //@{
  316. uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  317. uint8_t numpanscanwin;
  318. uint8_t tfcntr;
  319. uint8_t rptfrm, tff, rff;
  320. uint16_t topleftx;
  321. uint16_t toplefty;
  322. uint16_t bottomrightx;
  323. uint16_t bottomrighty;
  324. uint8_t uvsamp;
  325. uint8_t postproc;
  326. int hrd_num_leaky_buckets;
  327. uint8_t bit_rate_exponent;
  328. uint8_t buffer_size_exponent;
  329. BitPlane ac_pred_plane; ///< AC prediction flags bitplane
  330. BitPlane over_flags_plane; ///< Overflags bitplane
  331. uint8_t condover;
  332. uint16_t *hrd_rate, *hrd_buffer;
  333. uint8_t *hrd_fullness;
  334. uint8_t range_mapy_flag;
  335. uint8_t range_mapuv_flag;
  336. uint8_t range_mapy;
  337. uint8_t range_mapuv;
  338. //@}
  339. } VC1Context;
  340. /**
  341. * Get unary code of limited length
  342. * @fixme FIXME Slow and ugly
  343. * @param gb GetBitContext
  344. * @param[in] stop The bitstop value (unary code of 1's or 0's)
  345. * @param[in] len Maximum length
  346. * @return Unary length/index
  347. */
  348. static int get_prefix(GetBitContext *gb, int stop, int len)
  349. {
  350. #if 1
  351. int i;
  352. for(i = 0; i < len && get_bits1(gb) != stop; i++);
  353. return i;
  354. /* int i = 0, tmp = !stop;
  355. while (i != len && tmp != stop)
  356. {
  357. tmp = get_bits(gb, 1);
  358. i++;
  359. }
  360. if (i == len && tmp != stop) return len+1;
  361. return i;*/
  362. #else
  363. unsigned int buf;
  364. int log;
  365. OPEN_READER(re, gb);
  366. UPDATE_CACHE(re, gb);
  367. buf=GET_CACHE(re, gb); //Still not sure
  368. if (stop) buf = ~buf;
  369. log= av_log2(-buf); //FIXME: -?
  370. if (log < limit){
  371. LAST_SKIP_BITS(re, gb, log+1);
  372. CLOSE_READER(re, gb);
  373. return log;
  374. }
  375. LAST_SKIP_BITS(re, gb, limit);
  376. CLOSE_READER(re, gb);
  377. return limit;
  378. #endif
  379. }
  380. static inline int decode210(GetBitContext *gb){
  381. int n;
  382. n = get_bits1(gb);
  383. if (n == 1)
  384. return 0;
  385. else
  386. return 2 - get_bits1(gb);
  387. }
  388. /**
  389. * Init VC-1 specific tables and VC1Context members
  390. * @param v The VC1Context to initialize
  391. * @return Status
  392. */
  393. static int vc1_init_common(VC1Context *v)
  394. {
  395. static int done = 0;
  396. int i = 0;
  397. /* Set the bit planes */
  398. v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  399. v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  400. v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  401. v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  402. v->hrd_rate = v->hrd_buffer = NULL;
  403. /* VLC tables */
  404. if(!done)
  405. {
  406. done = 1;
  407. init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
  408. vc1_bfraction_bits, 1, 1,
  409. vc1_bfraction_codes, 1, 1, 1);
  410. init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
  411. vc1_norm2_bits, 1, 1,
  412. vc1_norm2_codes, 1, 1, 1);
  413. init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
  414. vc1_norm6_bits, 1, 1,
  415. vc1_norm6_codes, 2, 2, 1);
  416. init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
  417. vc1_imode_bits, 1, 1,
  418. vc1_imode_codes, 1, 1, 1);
  419. for (i=0; i<3; i++)
  420. {
  421. init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
  422. vc1_ttmb_bits[i], 1, 1,
  423. vc1_ttmb_codes[i], 2, 2, 1);
  424. init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
  425. vc1_ttblk_bits[i], 1, 1,
  426. vc1_ttblk_codes[i], 1, 1, 1);
  427. init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
  428. vc1_subblkpat_bits[i], 1, 1,
  429. vc1_subblkpat_codes[i], 1, 1, 1);
  430. }
  431. for(i=0; i<4; i++)
  432. {
  433. init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
  434. vc1_4mv_block_pattern_bits[i], 1, 1,
  435. vc1_4mv_block_pattern_codes[i], 1, 1, 1);
  436. init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
  437. vc1_cbpcy_p_bits[i], 1, 1,
  438. vc1_cbpcy_p_codes[i], 2, 2, 1);
  439. init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
  440. vc1_mv_diff_bits[i], 1, 1,
  441. vc1_mv_diff_codes[i], 2, 2, 1);
  442. }
  443. for(i=0; i<8; i++)
  444. init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
  445. &vc1_ac_tables[i][0][1], 8, 4,
  446. &vc1_ac_tables[i][0][0], 8, 4, 1);
  447. init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
  448. &ff_msmp4_mb_i_table[0][1], 4, 2,
  449. &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
  450. }
  451. /* Other defaults */
  452. v->pq = -1;
  453. v->mvrange = 0; /* 7.1.1.18, p80 */
  454. return 0;
  455. }
  456. /***********************************************************************/
  457. /**
  458. * @defgroup bitplane VC9 Bitplane decoding
  459. * @see 8.7, p56
  460. * @{
  461. */
  462. /** @addtogroup bitplane
  463. * Imode types
  464. * @{
  465. */
  466. enum Imode {
  467. IMODE_RAW,
  468. IMODE_NORM2,
  469. IMODE_DIFF2,
  470. IMODE_NORM6,
  471. IMODE_DIFF6,
  472. IMODE_ROWSKIP,
  473. IMODE_COLSKIP
  474. };
  475. /** @} */ //imode defines
  476. /** Allocate the buffer from a bitplane, given its dimensions
  477. * @param bp Bitplane which buffer is to allocate
  478. * @param[in] width Width of the buffer
  479. * @param[in] height Height of the buffer
  480. * @return Status
  481. * @todo TODO: Take into account stride
  482. * @todo TODO: Allow use of external buffers ?
  483. */
  484. static int alloc_bitplane(BitPlane *bp, int width, int height)
  485. {
  486. if (!bp || bp->width<0 || bp->height<0) return -1;
  487. bp->data = (uint8_t*)av_malloc(width*height);
  488. if (!bp->data) return -1;
  489. bp->width = bp->stride = width;
  490. bp->height = height;
  491. return 0;
  492. }
  493. /** Free the bitplane's buffer
  494. * @param bp Bitplane which buffer is to free
  495. */
  496. static void free_bitplane(BitPlane *bp)
  497. {
  498. bp->width = bp->stride = bp->height = 0;
  499. if (bp->data) av_freep(&bp->data);
  500. }
  501. /** Decode rows by checking if they are skipped
  502. * @param plane Buffer to store decoded bits
  503. * @param[in] width Width of this buffer
  504. * @param[in] height Height of this buffer
  505. * @param[in] stride of this buffer
  506. */
  507. static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  508. int x, y;
  509. for (y=0; y<height; y++){
  510. if (!get_bits(gb, 1)) //rowskip
  511. memset(plane, 0, width);
  512. else
  513. for (x=0; x<width; x++)
  514. plane[x] = get_bits(gb, 1);
  515. plane += stride;
  516. }
  517. }
  518. /** Decode columns by checking if they are skipped
  519. * @param plane Buffer to store decoded bits
  520. * @param[in] width Width of this buffer
  521. * @param[in] height Height of this buffer
  522. * @param[in] stride of this buffer
  523. * @fixme FIXME: Optimize
  524. */
  525. static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  526. int x, y;
  527. for (x=0; x<width; x++){
  528. if (!get_bits(gb, 1)) //colskip
  529. for (y=0; y<height; y++)
  530. plane[y*stride] = 0;
  531. else
  532. for (y=0; y<height; y++)
  533. plane[y*stride] = get_bits(gb, 1);
  534. plane ++;
  535. }
  536. }
  537. /** Decode a bitplane's bits
  538. * @param bp Bitplane where to store the decode bits
  539. * @param v VC-1 context for bit reading and logging
  540. * @return Status
  541. * @fixme FIXME: Optimize
  542. * @todo TODO: Decide if a struct is needed
  543. */
  544. static int bitplane_decoding(BitPlane *bp, VC1Context *v)
  545. {
  546. GetBitContext *gb = &v->s.gb;
  547. int imode, x, y, code, offset;
  548. uint8_t invert, *planep = bp->data;
  549. invert = get_bits(gb, 1);
  550. imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
  551. bp->is_raw = 0;
  552. switch (imode)
  553. {
  554. case IMODE_RAW:
  555. //Data is actually read in the MB layer (same for all tests == "raw")
  556. bp->is_raw = 1; //invert ignored
  557. return invert;
  558. case IMODE_DIFF2:
  559. case IMODE_NORM2:
  560. if ((bp->height * bp->width) & 1)
  561. {
  562. *planep++ = get_bits(gb, 1);
  563. offset = 1;
  564. }
  565. else offset = 0;
  566. // decode bitplane as one long line
  567. for (y = offset; y < bp->height * bp->width; y += 2) {
  568. code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
  569. *planep++ = code & 1;
  570. offset++;
  571. if(offset == bp->width) {
  572. offset = 0;
  573. planep += bp->stride - bp->width;
  574. }
  575. *planep++ = code >> 1;
  576. offset++;
  577. if(offset == bp->width) {
  578. offset = 0;
  579. planep += bp->stride - bp->width;
  580. }
  581. }
  582. break;
  583. case IMODE_DIFF6:
  584. case IMODE_NORM6:
  585. if(!(bp->height % 3) && (bp->width % 3)) { // use 2x3 decoding
  586. for(y = 0; y < bp->height; y+= 3) {
  587. for(x = bp->width & 1; x < bp->width; x += 2) {
  588. code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
  589. if(code < 0){
  590. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  591. return -1;
  592. }
  593. planep[x + 0] = (code >> 0) & 1;
  594. planep[x + 1] = (code >> 1) & 1;
  595. planep[x + 0 + bp->stride] = (code >> 2) & 1;
  596. planep[x + 1 + bp->stride] = (code >> 3) & 1;
  597. planep[x + 0 + bp->stride * 2] = (code >> 4) & 1;
  598. planep[x + 1 + bp->stride * 2] = (code >> 5) & 1;
  599. }
  600. planep += bp->stride * 3;
  601. }
  602. if(bp->width & 1) decode_colskip(bp->data, 1, bp->height, bp->stride, &v->s.gb);
  603. } else { // 3x2
  604. for(y = bp->height & 1; y < bp->height; y += 2) {
  605. for(x = bp->width % 3; x < bp->width; x += 3) {
  606. code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
  607. if(code < 0){
  608. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  609. return -1;
  610. }
  611. planep[x + 0] = (code >> 0) & 1;
  612. planep[x + 1] = (code >> 1) & 1;
  613. planep[x + 2] = (code >> 2) & 1;
  614. planep[x + 0 + bp->stride] = (code >> 3) & 1;
  615. planep[x + 1 + bp->stride] = (code >> 4) & 1;
  616. planep[x + 2 + bp->stride] = (code >> 5) & 1;
  617. }
  618. planep += bp->stride * 2;
  619. }
  620. x = bp->width % 3;
  621. if(x) decode_colskip(bp->data , x, bp->height , bp->stride, &v->s.gb);
  622. if(bp->height & 1) decode_rowskip(bp->data+x, bp->width - x, bp->height & 1, bp->stride, &v->s.gb);
  623. }
  624. break;
  625. case IMODE_ROWSKIP:
  626. decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  627. break;
  628. case IMODE_COLSKIP:
  629. decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  630. break;
  631. default: break;
  632. }
  633. /* Applying diff operator */
  634. if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
  635. {
  636. planep = bp->data;
  637. planep[0] ^= invert;
  638. for (x=1; x<bp->width; x++)
  639. planep[x] ^= planep[x-1];
  640. for (y=1; y<bp->height; y++)
  641. {
  642. planep += bp->stride;
  643. planep[0] ^= planep[-bp->stride];
  644. for (x=1; x<bp->width; x++)
  645. {
  646. if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
  647. else planep[x] ^= planep[x-1];
  648. }
  649. }
  650. }
  651. else if (invert)
  652. {
  653. planep = bp->data;
  654. for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
  655. }
  656. return (imode<<1) + invert;
  657. }
  658. /** @} */ //Bitplane group
  659. /***********************************************************************/
  660. /** VOP Dquant decoding
  661. * @param v VC-1 Context
  662. */
  663. static int vop_dquant_decoding(VC1Context *v)
  664. {
  665. GetBitContext *gb = &v->s.gb;
  666. int pqdiff;
  667. //variable size
  668. if (v->dquant == 2)
  669. {
  670. pqdiff = get_bits(gb, 3);
  671. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  672. else v->altpq = v->pq + pqdiff + 1;
  673. }
  674. else
  675. {
  676. v->dquantfrm = get_bits(gb, 1);
  677. if ( v->dquantfrm )
  678. {
  679. v->dqprofile = get_bits(gb, 2);
  680. switch (v->dqprofile)
  681. {
  682. case DQPROFILE_SINGLE_EDGE:
  683. case DQPROFILE_DOUBLE_EDGES:
  684. v->dqsbedge = get_bits(gb, 2);
  685. break;
  686. case DQPROFILE_ALL_MBS:
  687. v->dqbilevel = get_bits(gb, 1);
  688. default: break; //Forbidden ?
  689. }
  690. if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
  691. {
  692. pqdiff = get_bits(gb, 3);
  693. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  694. else v->altpq = v->pq + pqdiff + 1;
  695. }
  696. }
  697. }
  698. return 0;
  699. }
  700. /** Do inverse transform
  701. */
  702. static void vc1_inv_trans(DCTELEM block[64], int M, int N)
  703. {
  704. int i;
  705. register int t1,t2,t3,t4,t5,t6,t7,t8;
  706. DCTELEM *src, *dst;
  707. src = block;
  708. dst = block;
  709. if(M==4){
  710. for(i = 0; i < N; i++){
  711. t1 = 17 * (src[0] + src[2]);
  712. t2 = 17 * (src[0] - src[2]);
  713. t3 = 22 * src[1];
  714. t4 = 22 * src[3];
  715. t5 = 10 * src[1];
  716. t6 = 10 * src[3];
  717. dst[0] = (t1 + t3 + t6 + 4) >> 3;
  718. dst[1] = (t2 - t4 + t5 + 4) >> 3;
  719. dst[2] = (t2 + t4 - t5 + 4) >> 3;
  720. dst[3] = (t1 - t3 - t6 + 4) >> 3;
  721. src += 8;
  722. dst += 8;
  723. }
  724. }else{
  725. for(i = 0; i < N; i++){
  726. t1 = 12 * (src[0] + src[4]);
  727. t2 = 12 * (src[0] - src[4]);
  728. t3 = 16 * src[2] + 6 * src[6];
  729. t4 = 6 * src[2] - 16 * src[6];
  730. t5 = t1 + t3;
  731. t6 = t2 + t4;
  732. t7 = t2 - t4;
  733. t8 = t1 - t3;
  734. t1 = 16 * src[1] + 15 * src[3] + 9 * src[5] + 4 * src[7];
  735. t2 = 15 * src[1] - 4 * src[3] - 16 * src[5] - 9 * src[7];
  736. t3 = 9 * src[1] - 16 * src[3] + 4 * src[5] + 15 * src[7];
  737. t4 = 4 * src[1] - 9 * src[3] + 15 * src[5] - 16 * src[7];
  738. dst[0] = (t5 + t1 + 4) >> 3;
  739. dst[1] = (t6 + t2 + 4) >> 3;
  740. dst[2] = (t7 + t3 + 4) >> 3;
  741. dst[3] = (t8 + t4 + 4) >> 3;
  742. dst[4] = (t8 - t4 + 4) >> 3;
  743. dst[5] = (t7 - t3 + 4) >> 3;
  744. dst[6] = (t6 - t2 + 4) >> 3;
  745. dst[7] = (t5 - t1 + 4) >> 3;
  746. src += 8;
  747. dst += 8;
  748. }
  749. }
  750. src = block;
  751. dst = block;
  752. if(N==4){
  753. for(i = 0; i < M; i++){
  754. t1 = 17 * (src[ 0] + src[16]);
  755. t2 = 17 * (src[ 0] - src[16]);
  756. t3 = 22 * src[ 8];
  757. t4 = 22 * src[24];
  758. t5 = 10 * src[ 8];
  759. t6 = 10 * src[24];
  760. dst[ 0] = (t1 + t3 + t6 + 64) >> 7;
  761. dst[ 8] = (t2 - t4 + t5 + 64) >> 7;
  762. dst[16] = (t2 + t4 - t5 + 64) >> 7;
  763. dst[24] = (t1 - t3 - t6 + 64) >> 7;
  764. src ++;
  765. dst ++;
  766. }
  767. }else{
  768. for(i = 0; i < M; i++){
  769. t1 = 12 * (src[ 0] + src[32]);
  770. t2 = 12 * (src[ 0] - src[32]);
  771. t3 = 16 * src[16] + 6 * src[48];
  772. t4 = 6 * src[16] - 16 * src[48];
  773. t5 = t1 + t3;
  774. t6 = t2 + t4;
  775. t7 = t2 - t4;
  776. t8 = t1 - t3;
  777. t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56];
  778. t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56];
  779. t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56];
  780. t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56];
  781. dst[ 0] = (t5 + t1 + 64) >> 7;
  782. dst[ 8] = (t6 + t2 + 64) >> 7;
  783. dst[16] = (t7 + t3 + 64) >> 7;
  784. dst[24] = (t8 + t4 + 64) >> 7;
  785. dst[32] = (t8 - t4 + 64 + 1) >> 7;
  786. dst[40] = (t7 - t3 + 64 + 1) >> 7;
  787. dst[48] = (t6 - t2 + 64 + 1) >> 7;
  788. dst[56] = (t5 - t1 + 64 + 1) >> 7;
  789. src++;
  790. dst++;
  791. }
  792. }
  793. }
  794. /** Apply overlap transform
  795. * @todo optimize
  796. * @todo move to DSPContext
  797. */
  798. static void vc1_overlap_block(MpegEncContext *s, DCTELEM block[64], int n, int do_hor, int do_vert)
  799. {
  800. int i;
  801. if(do_hor) { //TODO
  802. }
  803. if(do_vert) { //TODO
  804. }
  805. for(i = 0; i < 64; i++)
  806. block[i] += 128;
  807. }
  808. /** Put block onto picture
  809. * @todo move to DSPContext
  810. */
  811. static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
  812. {
  813. uint8_t *Y;
  814. int ys, us, vs;
  815. DSPContext *dsp = &v->s.dsp;
  816. ys = v->s.current_picture.linesize[0];
  817. us = v->s.current_picture.linesize[1];
  818. vs = v->s.current_picture.linesize[2];
  819. Y = v->s.dest[0];
  820. dsp->put_pixels_clamped(block[0], Y, ys);
  821. dsp->put_pixels_clamped(block[1], Y + 8, ys);
  822. Y += ys * 8;
  823. dsp->put_pixels_clamped(block[2], Y, ys);
  824. dsp->put_pixels_clamped(block[3], Y + 8, ys);
  825. dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
  826. dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
  827. }
  828. /** Do motion compensation over 1 macroblock
  829. * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
  830. */
  831. static void vc1_mc_1mv(VC1Context *v)
  832. {
  833. MpegEncContext *s = &v->s;
  834. DSPContext *dsp = &v->s.dsp;
  835. uint8_t *srcY, *srcU, *srcV;
  836. int dxy, mx, my, src_x, src_y;
  837. int width = s->mb_width * 16, height = s->mb_height * 16;
  838. if(!v->s.last_picture.data[0])return;
  839. mx = s->mv[0][0][0] >> s->mspel;
  840. my = s->mv[0][0][1] >> s->mspel;
  841. srcY = s->last_picture.data[0];
  842. srcU = s->last_picture.data[1];
  843. srcV = s->last_picture.data[2];
  844. if(s->mspel) { // hpel mc
  845. dxy = ((my & 1) << 1) | (mx & 1);
  846. src_x = s->mb_x * 16 + (mx >> 1);
  847. src_y = s->mb_y * 16 + (my >> 1);
  848. /* src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  849. if (src_x == width)
  850. dxy &= ~1;
  851. src_y = clip(src_y, -16, height);
  852. if (src_y == height)
  853. dxy &= ~2;*/
  854. srcY += src_y * s->linesize + src_x;
  855. srcU += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
  856. srcV += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
  857. if((unsigned)src_x > s->h_edge_pos - (mx&1) - 16
  858. || (unsigned)src_y > s->v_edge_pos - (my&1) - 16){
  859. uint8_t *uvbuf= s->edge_emu_buffer + 18 * s->linesize;
  860. ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
  861. src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  862. srcY = s->edge_emu_buffer;
  863. ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 8+1, 8+1,
  864. src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  865. ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
  866. src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
  867. srcU = uvbuf;
  868. srcV = uvbuf + 16;
  869. }
  870. dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
  871. dsp->put_no_rnd_pixels_tab[1][0](s->dest[1], srcU, s->uvlinesize, 8);
  872. dsp->put_no_rnd_pixels_tab[1][0](s->dest[2], srcV, s->uvlinesize, 8);
  873. } else {
  874. int motion_x = mx, motion_y = my, uvdxy, uvsrc_x, uvsrc_y;
  875. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  876. src_x = s->mb_x * 16 + (mx >> 2);
  877. src_y = s->mb_y * 16 + (my >> 2);
  878. mx= motion_x/2;
  879. my= motion_y/2;
  880. mx= (mx>>1)|(mx&1);
  881. my= (my>>1)|(my&1);
  882. uvdxy= (mx&1) | ((my&1)<<1);
  883. mx>>=1;
  884. my>>=1;
  885. uvsrc_x = s->mb_x * 8 + mx;
  886. uvsrc_y = s->mb_y * 8 + my;
  887. srcY = s->last_picture.data[0] + src_y * s->linesize + src_x;
  888. srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
  889. srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
  890. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  891. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 16 ){
  892. uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
  893. ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
  894. src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  895. srcY = s->edge_emu_buffer;
  896. ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 9, 9,
  897. uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  898. ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 9, 9,
  899. uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  900. srcU = uvbuf;
  901. srcV = uvbuf + 16;
  902. }
  903. dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
  904. dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize, 8);
  905. dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize, 8);
  906. }
  907. }
  908. /**
  909. * Decode Simple/Main Profiles sequence header
  910. * @see Figure 7-8, p16-17
  911. * @param avctx Codec context
  912. * @param gb GetBit context initialized from Codec context extra_data
  913. * @return Status
  914. */
  915. static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  916. {
  917. VC1Context *v = avctx->priv_data;
  918. av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
  919. v->profile = get_bits(gb, 2);
  920. if (v->profile == 2)
  921. {
  922. av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
  923. return -1;
  924. }
  925. if (v->profile == PROFILE_ADVANCED)
  926. {
  927. v->level = get_bits(gb, 3);
  928. if(v->level >= 5)
  929. {
  930. av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
  931. }
  932. v->chromaformat = get_bits(gb, 2);
  933. if (v->chromaformat != 1)
  934. {
  935. av_log(avctx, AV_LOG_ERROR,
  936. "Only 4:2:0 chroma format supported\n");
  937. return -1;
  938. }
  939. }
  940. else
  941. {
  942. v->res_sm = get_bits(gb, 2); //reserved
  943. if (v->res_sm)
  944. {
  945. av_log(avctx, AV_LOG_ERROR,
  946. "Reserved RES_SM=%i is forbidden\n", v->res_sm);
  947. return -1;
  948. }
  949. }
  950. // (fps-2)/4 (->30)
  951. v->frmrtq_postproc = get_bits(gb, 3); //common
  952. // (bitrate-32kbps)/64kbps
  953. v->bitrtq_postproc = get_bits(gb, 5); //common
  954. v->s.loop_filter = get_bits(gb, 1); //common
  955. if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
  956. {
  957. av_log(avctx, AV_LOG_ERROR,
  958. "LOOPFILTER shell not be enabled in simple profile\n");
  959. }
  960. if (v->profile < PROFILE_ADVANCED)
  961. {
  962. v->res_x8 = get_bits(gb, 1); //reserved
  963. if (v->res_x8)
  964. {
  965. av_log(avctx, AV_LOG_ERROR,
  966. "1 for reserved RES_X8 is forbidden\n");
  967. //return -1;
  968. }
  969. v->multires = get_bits(gb, 1);
  970. v->res_fasttx = get_bits(gb, 1);
  971. if (!v->res_fasttx)
  972. {
  973. av_log(avctx, AV_LOG_ERROR,
  974. "0 for reserved RES_FASTTX is forbidden\n");
  975. //return -1;
  976. }
  977. }
  978. v->fastuvmc = get_bits(gb, 1); //common
  979. if (!v->profile && !v->fastuvmc)
  980. {
  981. av_log(avctx, AV_LOG_ERROR,
  982. "FASTUVMC unavailable in Simple Profile\n");
  983. return -1;
  984. }
  985. v->extended_mv = get_bits(gb, 1); //common
  986. if (!v->profile && v->extended_mv)
  987. {
  988. av_log(avctx, AV_LOG_ERROR,
  989. "Extended MVs unavailable in Simple Profile\n");
  990. return -1;
  991. }
  992. v->dquant = get_bits(gb, 2); //common
  993. v->vstransform = get_bits(gb, 1); //common
  994. if (v->profile < PROFILE_ADVANCED)
  995. {
  996. v->res_transtab = get_bits(gb, 1);
  997. if (v->res_transtab)
  998. {
  999. av_log(avctx, AV_LOG_ERROR,
  1000. "1 for reserved RES_TRANSTAB is forbidden\n");
  1001. return -1;
  1002. }
  1003. }
  1004. v->overlap = get_bits(gb, 1); //common
  1005. if (v->profile < PROFILE_ADVANCED)
  1006. {
  1007. v->s.resync_marker = get_bits(gb, 1);
  1008. v->rangered = get_bits(gb, 1);
  1009. if (v->rangered && v->profile == PROFILE_SIMPLE)
  1010. {
  1011. av_log(avctx, AV_LOG_INFO,
  1012. "RANGERED should be set to 0 in simple profile\n");
  1013. }
  1014. }
  1015. v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
  1016. v->quantizer_mode = get_bits(gb, 2); //common
  1017. if (v->profile < PROFILE_ADVANCED)
  1018. {
  1019. v->finterpflag = get_bits(gb, 1); //common
  1020. v->res_rtm_flag = get_bits(gb, 1); //reserved
  1021. if (!v->res_rtm_flag)
  1022. {
  1023. av_log(avctx, AV_LOG_ERROR,
  1024. "0 for reserved RES_RTM_FLAG is forbidden\n");
  1025. //return -1;
  1026. }
  1027. av_log(avctx, AV_LOG_DEBUG,
  1028. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  1029. "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
  1030. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  1031. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  1032. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  1033. v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
  1034. v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
  1035. v->dquant, v->quantizer_mode, avctx->max_b_frames
  1036. );
  1037. return 0;
  1038. }
  1039. return -1;
  1040. }
  1041. static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
  1042. {
  1043. int pqindex, lowquant, status;
  1044. if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
  1045. skip_bits(gb, 2); //framecnt unused
  1046. v->rangeredfrm = 0;
  1047. if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
  1048. v->s.pict_type = get_bits(gb, 1);
  1049. if (v->s.avctx->max_b_frames) {
  1050. if (!v->s.pict_type) {
  1051. if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
  1052. else v->s.pict_type = B_TYPE;
  1053. } else v->s.pict_type = P_TYPE;
  1054. } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
  1055. if(v->s.pict_type == I_TYPE)
  1056. get_bits(gb, 7); // skip buffer fullness
  1057. /* Quantizer stuff */
  1058. pqindex = get_bits(gb, 5);
  1059. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1060. v->pq = pquant_table[0][pqindex];
  1061. else
  1062. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1063. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1064. v->pquantizer = pqindex < 9;
  1065. if (v->quantizer_mode == QUANT_UNIFORM || v->quantizer_mode == QUANT_NON_UNIFORM)
  1066. v->pquantizer = v->quantizer_mode == QUANT_UNIFORM;
  1067. v->pqindex = pqindex;
  1068. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1069. else v->halfpq = 0;
  1070. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1071. v->pquantizer = get_bits(gb, 1);
  1072. v->dquantfrm = 0;
  1073. //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
  1074. // (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
  1075. //TODO: complete parsing for P/B/BI frames
  1076. switch(v->s.pict_type) {
  1077. case P_TYPE:
  1078. if (v->pq < 5) v->tt_index = 0;
  1079. else if(v->pq < 13) v->tt_index = 1;
  1080. else v->tt_index = 2;
  1081. if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
  1082. v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
  1083. v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
  1084. v->range_x = 1 << (v->k_x - 1);
  1085. v->range_y = 1 << (v->k_y - 1);
  1086. if (v->profile == PROFILE_ADVANCED)
  1087. {
  1088. if (v->postprocflag) v->postproc = get_bits(gb, 1);
  1089. }
  1090. else
  1091. if (v->multires) v->respic = get_bits(gb, 2);
  1092. lowquant = (v->pq > 12) ? 0 : 1;
  1093. v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
  1094. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  1095. {
  1096. v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
  1097. v->lumscale = get_bits(gb, 6);
  1098. v->lumshift = get_bits(gb, 6);
  1099. }
  1100. if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
  1101. v->s.mspel = 1;
  1102. else
  1103. v->s.mspel = 0;
  1104. if(v->mv_mode != MV_PMODE_1MV && v->mv_mode != MV_PMODE_1MV_HPEL && v->mv_mode != MV_PMODE_1MV_HPEL_BILIN) {
  1105. av_log(v->s.avctx, AV_LOG_ERROR, "Only 1MV P-frames are supported by now\n");
  1106. return -1;
  1107. }
  1108. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1109. v->mv_mode2 == MV_PMODE_MIXED_MV)
  1110. || v->mv_mode == MV_PMODE_MIXED_MV)
  1111. {
  1112. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1113. if (status < 0) return -1;
  1114. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1115. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1116. }
  1117. status = bitplane_decoding(&v->skip_mb_plane, v);
  1118. if (status < 0) return -1;
  1119. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1120. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1121. /* Hopefully this is correct for P frames */
  1122. v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
  1123. v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
  1124. if (v->dquant)
  1125. {
  1126. av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
  1127. vop_dquant_decoding(v);
  1128. }
  1129. v->ttfrm = 0; //FIXME Is that so ?
  1130. if (v->vstransform)
  1131. {
  1132. v->ttmbf = get_bits(gb, 1);
  1133. if (v->ttmbf)
  1134. {
  1135. v->ttfrm = get_bits(gb, 2);
  1136. }
  1137. }
  1138. break;
  1139. case B_TYPE:
  1140. break;
  1141. }
  1142. /* AC Syntax */
  1143. v->c_ac_table_index = decode012(gb);
  1144. if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
  1145. {
  1146. v->y_ac_table_index = decode012(gb);
  1147. }
  1148. /* DC Syntax */
  1149. v->s.dc_table_index = get_bits(gb, 1);
  1150. return 0;
  1151. }
  1152. /***********************************************************************/
  1153. /**
  1154. * @defgroup block VC-1 Block-level functions
  1155. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1156. * @todo TODO: Integrate to MpegEncContext facilities
  1157. * @{
  1158. */
  1159. /**
  1160. * @def GET_MQUANT
  1161. * @brief Get macroblock-level quantizer scale
  1162. * @warning XXX: qdiff to the frame quant, not previous quant ?
  1163. * @fixme XXX: Don't know how to initialize mquant otherwise in last case
  1164. */
  1165. #define GET_MQUANT() \
  1166. if (v->dquantfrm) \
  1167. { \
  1168. if (v->dqprofile == DQPROFILE_ALL_MBS) \
  1169. { \
  1170. if (v->dqbilevel) \
  1171. { \
  1172. mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \
  1173. } \
  1174. else \
  1175. { \
  1176. mqdiff = get_bits(gb, 3); \
  1177. if (mqdiff != 7) mquant = v->pq + mqdiff; \
  1178. else mquant = get_bits(gb, 5); \
  1179. } \
  1180. } \
  1181. else mquant = v->pq; \
  1182. }
  1183. /**
  1184. * @def GET_MVDATA(_dmv_x, _dmv_y)
  1185. * @brief Get MV differentials
  1186. * @see MVDATA decoding from 8.3.5.2, p(1)20
  1187. * @param _dmv_x Horizontal differential for decoded MV
  1188. * @param _dmv_y Vertical differential for decoded MV
  1189. * @todo TODO: Use MpegEncContext arrays to store them
  1190. */
  1191. #define GET_MVDATA(_dmv_x, _dmv_y) \
  1192. index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
  1193. VC1_MV_DIFF_VLC_BITS, 2); \
  1194. if (index > 36) \
  1195. { \
  1196. mb_has_coeffs = 1; \
  1197. index -= 37; \
  1198. } \
  1199. else mb_has_coeffs = 0; \
  1200. s->mb_intra = 0; \
  1201. if (!index) { _dmv_x = _dmv_y = 0; } \
  1202. else if (index == 35) \
  1203. { \
  1204. _dmv_x = get_bits(gb, v->k_x - s->mspel); \
  1205. _dmv_y = get_bits(gb, v->k_y - s->mspel); \
  1206. } \
  1207. else if (index == 36) \
  1208. { \
  1209. _dmv_x = 0; \
  1210. _dmv_y = 0; \
  1211. s->mb_intra = 1; \
  1212. } \
  1213. else \
  1214. { \
  1215. index1 = index%6; \
  1216. if (s->mspel && index1 == 5) val = 1; \
  1217. else val = 0; \
  1218. val = get_bits(gb, size_table[index1] - val); \
  1219. sign = 0 - (val&1); \
  1220. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1221. \
  1222. index1 = index/6; \
  1223. if (s->mspel && index1 == 5) val = 1; \
  1224. else val = 0; \
  1225. val = get_bits(gb, size_table[index1] - val); \
  1226. sign = 0 - (val&1); \
  1227. _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1228. }
  1229. /** Predict and set motion vector
  1230. */
  1231. static inline void vc1_pred_mv(MpegEncContext *s, int dmv_x, int dmv_y, int mv1, int r_x, int r_y)
  1232. {
  1233. int xy, wrap, off;
  1234. int16_t *A, *B, *C;
  1235. int px, py;
  1236. int sum;
  1237. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1238. /* scale MV difference to be quad-pel */
  1239. dmv_x <<= s->mspel;
  1240. dmv_y <<= s->mspel;
  1241. wrap = s->b8_stride;
  1242. xy = s->block_index[0];
  1243. C = s->current_picture.motion_val[0][xy - (1 << mv1)];
  1244. A = s->current_picture.motion_val[0][xy - (wrap << mv1)];
  1245. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
  1246. B = s->current_picture.motion_val[0][xy + ((off - wrap) << mv1)];
  1247. if(!s->first_slice_line) { // predictor A is not out of bounds
  1248. if(s->mb_width == 1) {
  1249. px = A[0];
  1250. py = A[1];
  1251. } else {
  1252. px = mid_pred(A[0], B[0], C[0]);
  1253. py = mid_pred(A[1], B[1], C[1]);
  1254. }
  1255. } else if(s->mb_x) { // predictor C is not out of bounds
  1256. px = C[0];
  1257. py = C[1];
  1258. } else {
  1259. px = py = 0;
  1260. }
  1261. if(s->mb_intra) px = py = 0;
  1262. /* Pullback MV as specified in 8.3.5.3.4 */
  1263. {
  1264. int qx, qy, X, Y;
  1265. qx = s->mb_x << 6; //FIXME: add real block coords for 4MV mode
  1266. qy = s->mb_y << 6;
  1267. X = (s->mb_width << 6) - 4;
  1268. Y = (s->mb_height << 6) - 4;
  1269. if(mv1) {
  1270. if(qx + px < -60) px = -60 - qx;
  1271. if(qy + py < -60) py = -60 - qy;
  1272. } else {
  1273. if(qx + px < -28) px = -28 - qx;
  1274. if(qy + py < -28) py = -28 - qy;
  1275. }
  1276. if(qx + px > X) px = X - qx;
  1277. if(qy + py > Y) py = Y - qy;
  1278. }
  1279. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  1280. if(!s->mb_intra && !s->first_slice_line && s->mb_x) {
  1281. if(IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride]))
  1282. sum = ABS(px) + ABS(py);
  1283. else
  1284. sum = ABS(px - A[0]) + ABS(py - A[1]);
  1285. if(sum > 32) {
  1286. if(get_bits1(&s->gb)) {
  1287. px = A[0];
  1288. py = A[1];
  1289. } else {
  1290. px = C[0];
  1291. py = C[1];
  1292. }
  1293. } else {
  1294. if(IS_INTRA(s->current_picture.mb_type[mb_pos - 1]))
  1295. sum = ABS(px) + ABS(py);
  1296. else
  1297. sum = ABS(px - C[0]) + ABS(py - C[1]);
  1298. if(sum > 32) {
  1299. if(get_bits1(&s->gb)) {
  1300. px = A[0];
  1301. py = A[1];
  1302. } else {
  1303. px = C[0];
  1304. py = C[1];
  1305. }
  1306. }
  1307. }
  1308. }
  1309. /* store MV using signed modulus of MV range defined in 4.11 */
  1310. s->mv[0][0][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  1311. s->mv[0][0][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
  1312. }
  1313. /** Get predicted DC value for I-frames only
  1314. * prediction dir: left=0, top=1
  1315. * @param s MpegEncContext
  1316. * @param[in] n block index in the current MB
  1317. * @param dc_val_ptr Pointer to DC predictor
  1318. * @param dir_ptr Prediction direction for use in AC prediction
  1319. */
  1320. static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  1321. int16_t **dc_val_ptr, int *dir_ptr)
  1322. {
  1323. int a, b, c, wrap, pred, scale;
  1324. int16_t *dc_val;
  1325. static const uint16_t dcpred[32] = {
  1326. -1, 1024, 512, 341, 256, 205, 171, 146, 128,
  1327. 114, 102, 93, 85, 79, 73, 68, 64,
  1328. 60, 57, 54, 51, 49, 47, 45, 43,
  1329. 41, 39, 38, 37, 35, 34, 33
  1330. };
  1331. /* find prediction - wmv3_dc_scale always used here in fact */
  1332. if (n < 4) scale = s->y_dc_scale;
  1333. else scale = s->c_dc_scale;
  1334. wrap = s->block_wrap[n];
  1335. dc_val= s->dc_val[0] + s->block_index[n];
  1336. /* B A
  1337. * C X
  1338. */
  1339. c = dc_val[ - 1];
  1340. b = dc_val[ - 1 - wrap];
  1341. a = dc_val[ - wrap];
  1342. if (pq < 9 || !overlap)
  1343. {
  1344. /* Set outer values */
  1345. if (!s->mb_y && (n!=2 && n!=3)) b=a=dcpred[scale];
  1346. if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
  1347. }
  1348. else
  1349. {
  1350. /* Set outer values */
  1351. if (!s->mb_y && (n!=2 && n!=3)) b=a=0;
  1352. if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
  1353. }
  1354. if (abs(a - b) <= abs(b - c)) {
  1355. pred = c;
  1356. *dir_ptr = 1;//left
  1357. } else {
  1358. pred = a;
  1359. *dir_ptr = 0;//top
  1360. }
  1361. /* update predictor */
  1362. *dc_val_ptr = &dc_val[0];
  1363. return pred;
  1364. }
  1365. /** Get predicted DC value
  1366. * prediction dir: left=0, top=1
  1367. * @param s MpegEncContext
  1368. * @param[in] n block index in the current MB
  1369. * @param dc_val_ptr Pointer to DC predictor
  1370. * @param dir_ptr Prediction direction for use in AC prediction
  1371. */
  1372. static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  1373. int a_avail, int c_avail,
  1374. int16_t **dc_val_ptr, int *dir_ptr)
  1375. {
  1376. int a, b, c, wrap, pred, scale;
  1377. int16_t *dc_val;
  1378. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1379. int mb_pos2, q1, q2;
  1380. /* find prediction - wmv3_dc_scale always used here in fact */
  1381. if (n < 4) scale = s->y_dc_scale;
  1382. else scale = s->c_dc_scale;
  1383. wrap = s->block_wrap[n];
  1384. dc_val= s->dc_val[0] + s->block_index[n];
  1385. /* B A
  1386. * C X
  1387. */
  1388. c = dc_val[ - 1];
  1389. b = dc_val[ - 1 - wrap];
  1390. a = dc_val[ - wrap];
  1391. if(a_avail && c_avail) {
  1392. if(abs(a - b) <= abs(b - c)) {
  1393. pred = c;
  1394. *dir_ptr = 1;//left
  1395. } else {
  1396. pred = a;
  1397. *dir_ptr = 0;//top
  1398. }
  1399. } else if(a_avail) {
  1400. pred = a;
  1401. *dir_ptr = 0;//top
  1402. } else if(c_avail) {
  1403. pred = c;
  1404. *dir_ptr = 1;//left
  1405. } else {
  1406. pred = 0;
  1407. *dir_ptr = 1;//left
  1408. }
  1409. /* scale coeffs if needed */
  1410. mb_pos2 = mb_pos - *dir_ptr - (1 - *dir_ptr) * s->mb_stride;
  1411. q1 = s->current_picture.qscale_table[mb_pos];
  1412. q2 = s->current_picture.qscale_table[mb_pos2];
  1413. if(0 && q1 && q2 && q1 != q2) {
  1414. q1 = s->y_dc_scale_table[q1];
  1415. q2 = s->y_dc_scale_table[q2];
  1416. pred = (pred * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1417. }
  1418. /* update predictor */
  1419. *dc_val_ptr = &dc_val[0];
  1420. return pred;
  1421. }
  1422. /**
  1423. * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
  1424. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1425. * @todo TODO: Integrate to MpegEncContext facilities
  1426. * @{
  1427. */
  1428. static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
  1429. {
  1430. int xy, wrap, pred, a, b, c;
  1431. xy = s->block_index[n];
  1432. wrap = s->b8_stride;
  1433. /* B C
  1434. * A X
  1435. */
  1436. a = s->coded_block[xy - 1 ];
  1437. b = s->coded_block[xy - 1 - wrap];
  1438. c = s->coded_block[xy - wrap];
  1439. if (b == c) {
  1440. pred = a;
  1441. } else {
  1442. pred = c;
  1443. }
  1444. /* store value */
  1445. *coded_block_ptr = &s->coded_block[xy];
  1446. return pred;
  1447. }
  1448. /**
  1449. * Decode one AC coefficient
  1450. * @param v The VC1 context
  1451. * @param last Last coefficient
  1452. * @param skip How much zero coefficients to skip
  1453. * @param value Decoded AC coefficient value
  1454. * @see 8.1.3.4
  1455. */
  1456. static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
  1457. {
  1458. GetBitContext *gb = &v->s.gb;
  1459. int index, escape, run = 0, level = 0, lst = 0;
  1460. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1461. if (index != vc1_ac_sizes[codingset] - 1) {
  1462. run = vc1_index_decode_table[codingset][index][0];
  1463. level = vc1_index_decode_table[codingset][index][1];
  1464. lst = index >= vc1_last_decode_table[codingset];
  1465. if(get_bits(gb, 1))
  1466. level = -level;
  1467. } else {
  1468. escape = decode210(gb);
  1469. if (escape == 0) {
  1470. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1471. run = vc1_index_decode_table[codingset][index][0];
  1472. level = vc1_index_decode_table[codingset][index][1];
  1473. lst = index >= vc1_last_decode_table[codingset];
  1474. if(lst)
  1475. level += vc1_last_delta_level_table[codingset][run];
  1476. else
  1477. level += vc1_delta_level_table[codingset][run];
  1478. if(get_bits(gb, 1))
  1479. level = -level;
  1480. } else if (escape == 1) {
  1481. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1482. run = vc1_index_decode_table[codingset][index][0];
  1483. level = vc1_index_decode_table[codingset][index][1];
  1484. lst = index >= vc1_last_decode_table[codingset];
  1485. if(lst)
  1486. run += vc1_last_delta_run_table[codingset][level] + 1;
  1487. else
  1488. run += vc1_delta_run_table[codingset][level] + 1;
  1489. if(get_bits(gb, 1))
  1490. level = -level;
  1491. } else {
  1492. int sign;
  1493. lst = get_bits(gb, 1);
  1494. if(v->s.esc3_level_length == 0) {
  1495. if(v->pq < 8 || v->dquantfrm) { // table 59
  1496. v->s.esc3_level_length = get_bits(gb, 3);
  1497. if(!v->s.esc3_level_length)
  1498. v->s.esc3_level_length = get_bits(gb, 2) + 8;
  1499. } else { //table 60
  1500. v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
  1501. }
  1502. v->s.esc3_run_length = 3 + get_bits(gb, 2);
  1503. }
  1504. run = get_bits(gb, v->s.esc3_run_length);
  1505. sign = get_bits(gb, 1);
  1506. level = get_bits(gb, v->s.esc3_level_length);
  1507. if(sign)
  1508. level = -level;
  1509. }
  1510. }
  1511. *last = lst;
  1512. *skip = run;
  1513. *value = level;
  1514. }
  1515. /** Decode intra block in intra frames - should be faster than decode_intra_block
  1516. * @param v VC1Context
  1517. * @param block block to decode
  1518. * @param coded are AC coeffs present or not
  1519. * @param codingset set of VLC to decode data
  1520. */
  1521. static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
  1522. {
  1523. GetBitContext *gb = &v->s.gb;
  1524. MpegEncContext *s = &v->s;
  1525. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  1526. int run_diff, i;
  1527. int16_t *dc_val;
  1528. int16_t *ac_val, *ac_val2;
  1529. int dcdiff;
  1530. /* Get DC differential */
  1531. if (n < 4) {
  1532. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1533. } else {
  1534. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1535. }
  1536. if (dcdiff < 0){
  1537. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  1538. return -1;
  1539. }
  1540. if (dcdiff)
  1541. {
  1542. if (dcdiff == 119 /* ESC index value */)
  1543. {
  1544. /* TODO: Optimize */
  1545. if (v->pq == 1) dcdiff = get_bits(gb, 10);
  1546. else if (v->pq == 2) dcdiff = get_bits(gb, 9);
  1547. else dcdiff = get_bits(gb, 8);
  1548. }
  1549. else
  1550. {
  1551. if (v->pq == 1)
  1552. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1553. else if (v->pq == 2)
  1554. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1555. }
  1556. if (get_bits(gb, 1))
  1557. dcdiff = -dcdiff;
  1558. }
  1559. /* Prediction */
  1560. dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
  1561. *dc_val = dcdiff;
  1562. /* Store the quantized DC coeff, used for prediction */
  1563. if (n < 4) {
  1564. block[0] = dcdiff * s->y_dc_scale;
  1565. } else {
  1566. block[0] = dcdiff * s->c_dc_scale;
  1567. }
  1568. /* Skip ? */
  1569. run_diff = 0;
  1570. i = 0;
  1571. if (!coded) {
  1572. goto not_coded;
  1573. }
  1574. //AC Decoding
  1575. i = 1;
  1576. {
  1577. int last = 0, skip, value;
  1578. const int8_t *zz_table;
  1579. int scale;
  1580. int k;
  1581. scale = v->pq * 2 + v->halfpq;
  1582. if(v->s.ac_pred) {
  1583. if(!dc_pred_dir)
  1584. zz_table = vc1_horizontal_zz;
  1585. else
  1586. zz_table = vc1_vertical_zz;
  1587. } else
  1588. zz_table = vc1_normal_zz;
  1589. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1590. ac_val2 = ac_val;
  1591. if(dc_pred_dir) //left
  1592. ac_val -= 16;
  1593. else //top
  1594. ac_val -= 16 * s->block_wrap[n];
  1595. while (!last) {
  1596. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  1597. i += skip;
  1598. if(i > 63)
  1599. break;
  1600. block[zz_table[i++]] = value;
  1601. }
  1602. /* apply AC prediction if needed */
  1603. if(s->ac_pred) {
  1604. if(dc_pred_dir) { //left
  1605. for(k = 1; k < 8; k++)
  1606. block[k << 3] += ac_val[k];
  1607. } else { //top
  1608. for(k = 1; k < 8; k++)
  1609. block[k] += ac_val[k + 8];
  1610. }
  1611. }
  1612. /* save AC coeffs for further prediction */
  1613. for(k = 1; k < 8; k++) {
  1614. ac_val2[k] = block[k << 3];
  1615. ac_val2[k + 8] = block[k];
  1616. }
  1617. /* scale AC coeffs */
  1618. for(k = 1; k < 64; k++)
  1619. if(block[k]) {
  1620. block[k] *= scale;
  1621. if(!v->pquantizer)
  1622. block[k] += (block[k] < 0) ? -v->pq : v->pq;
  1623. }
  1624. if(s->ac_pred) i = 63;
  1625. }
  1626. not_coded:
  1627. if(!coded) {
  1628. int k, scale;
  1629. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1630. ac_val2 = ac_val;
  1631. scale = v->pq * 2 + v->halfpq;
  1632. memset(ac_val2, 0, 16 * 2);
  1633. if(dc_pred_dir) {//left
  1634. ac_val -= 16;
  1635. if(s->ac_pred)
  1636. memcpy(ac_val2, ac_val, 8 * 2);
  1637. } else {//top
  1638. ac_val -= 16 * s->block_wrap[n];
  1639. if(s->ac_pred)
  1640. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  1641. }
  1642. /* apply AC prediction if needed */
  1643. if(s->ac_pred) {
  1644. if(dc_pred_dir) { //left
  1645. for(k = 1; k < 8; k++) {
  1646. block[k << 3] = ac_val[k] * scale;
  1647. if(!v->pquantizer)
  1648. block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
  1649. }
  1650. } else { //top
  1651. for(k = 1; k < 8; k++) {
  1652. block[k] = ac_val[k + 8] * scale;
  1653. if(!v->pquantizer)
  1654. block[k] += (block[k] < 0) ? -v->pq : v->pq;
  1655. }
  1656. }
  1657. i = 63;
  1658. }
  1659. }
  1660. s->block_last_index[n] = i;
  1661. return 0;
  1662. }
  1663. /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
  1664. * @param v VC1Context
  1665. * @param block block to decode
  1666. * @param coded are AC coeffs present or not
  1667. * @param mquant block quantizer
  1668. * @param codingset set of VLC to decode data
  1669. */
  1670. static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
  1671. {
  1672. GetBitContext *gb = &v->s.gb;
  1673. MpegEncContext *s = &v->s;
  1674. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  1675. int run_diff, i;
  1676. int16_t *dc_val;
  1677. int16_t *ac_val, *ac_val2;
  1678. int dcdiff;
  1679. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1680. int a_avail, c_avail;
  1681. /* XXX: Guard against dumb values of mquant */
  1682. mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
  1683. /* Set DC scale - y and c use the same */
  1684. s->y_dc_scale = s->y_dc_scale_table[mquant];
  1685. s->c_dc_scale = s->c_dc_scale_table[mquant];
  1686. /* check if prediction blocks A and C are available */
  1687. a_avail = c_avail = 0;
  1688. if((n == 2 || n == 3) || (s->mb_y && IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride])))
  1689. a_avail = 1;
  1690. if((n == 1 || n == 3) || (s->mb_x && IS_INTRA(s->current_picture.mb_type[mb_pos - 1])))
  1691. c_avail = 1;
  1692. /* Get DC differential */
  1693. if (n < 4) {
  1694. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1695. } else {
  1696. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1697. }
  1698. if (dcdiff < 0){
  1699. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  1700. return -1;
  1701. }
  1702. if (dcdiff)
  1703. {
  1704. if (dcdiff == 119 /* ESC index value */)
  1705. {
  1706. /* TODO: Optimize */
  1707. if (mquant == 1) dcdiff = get_bits(gb, 10);
  1708. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  1709. else dcdiff = get_bits(gb, 8);
  1710. }
  1711. else
  1712. {
  1713. if (mquant == 1)
  1714. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1715. else if (mquant == 2)
  1716. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1717. }
  1718. if (get_bits(gb, 1))
  1719. dcdiff = -dcdiff;
  1720. }
  1721. /* Prediction */
  1722. dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
  1723. *dc_val = dcdiff;
  1724. /* Store the quantized DC coeff, used for prediction */
  1725. if (n < 4) {
  1726. block[0] = dcdiff * s->y_dc_scale;
  1727. } else {
  1728. block[0] = dcdiff * s->c_dc_scale;
  1729. }
  1730. /* Skip ? */
  1731. run_diff = 0;
  1732. i = 0;
  1733. if (!coded) {
  1734. goto not_coded;
  1735. }
  1736. //AC Decoding
  1737. i = 1;
  1738. {
  1739. int last = 0, skip, value;
  1740. const int8_t *zz_table;
  1741. int scale;
  1742. int k;
  1743. scale = mquant * 2 + v->halfpq;
  1744. zz_table = vc1_simple_progressive_8x8_zz;
  1745. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1746. ac_val2 = ac_val;
  1747. if(dc_pred_dir) //left
  1748. ac_val -= 16;
  1749. else //top
  1750. ac_val -= 16 * s->block_wrap[n];
  1751. while (!last) {
  1752. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  1753. i += skip;
  1754. if(i > 63)
  1755. break;
  1756. block[zz_table[i++]] = value;
  1757. }
  1758. /* apply AC prediction if needed */
  1759. if(s->ac_pred) {
  1760. /* scale predictors if needed*/
  1761. int mb_pos2, q1, q2;
  1762. mb_pos2 = mb_pos - dc_pred_dir - (1 - dc_pred_dir) * s->mb_stride;
  1763. q1 = s->current_picture.qscale_table[mb_pos];
  1764. q2 = s->current_picture.qscale_table[mb_pos2];
  1765. if(!c_avail) {
  1766. memset(ac_val, 0, 8 * sizeof(ac_val[0]));
  1767. dc_pred_dir = 0;
  1768. }
  1769. if(!a_avail) {
  1770. memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
  1771. dc_pred_dir = 1;
  1772. }
  1773. if(!q1 && q1 && q2 && q1 != q2) {
  1774. q1 = q1 * 2 - 1;
  1775. q2 = q2 * 2 - 1;
  1776. if(dc_pred_dir) { //left
  1777. for(k = 1; k < 8; k++)
  1778. block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1779. } else { //top
  1780. for(k = 1; k < 8; k++)
  1781. block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1782. }
  1783. } else {
  1784. if(dc_pred_dir) { //left
  1785. for(k = 1; k < 8; k++)
  1786. block[k << 3] += ac_val[k];
  1787. } else { //top
  1788. for(k = 1; k < 8; k++)
  1789. block[k] += ac_val[k + 8];
  1790. }
  1791. }
  1792. }
  1793. /* save AC coeffs for further prediction */
  1794. for(k = 1; k < 8; k++) {
  1795. ac_val2[k] = block[k << 3];
  1796. ac_val2[k + 8] = block[k];
  1797. }
  1798. /* scale AC coeffs */
  1799. for(k = 1; k < 64; k++)
  1800. if(block[k]) {
  1801. block[k] *= scale;
  1802. if(!v->pquantizer)
  1803. block[k] += (block[k] < 0) ? -mquant : mquant;
  1804. }
  1805. if(s->ac_pred) i = 63;
  1806. }
  1807. not_coded:
  1808. if(!coded) {
  1809. int k, scale;
  1810. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1811. ac_val2 = ac_val;
  1812. if(!c_avail) {
  1813. memset(ac_val, 0, 8 * sizeof(ac_val[0]));
  1814. dc_pred_dir = 0;
  1815. }
  1816. if(!a_avail) {
  1817. memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
  1818. dc_pred_dir = 1;
  1819. }
  1820. scale = mquant * 2 + v->halfpq;
  1821. memset(ac_val2, 0, 16 * 2);
  1822. if(dc_pred_dir) {//left
  1823. ac_val -= 16;
  1824. if(s->ac_pred)
  1825. memcpy(ac_val2, ac_val, 8 * 2);
  1826. } else {//top
  1827. ac_val -= 16 * s->block_wrap[n];
  1828. if(s->ac_pred)
  1829. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  1830. }
  1831. /* apply AC prediction if needed */
  1832. if(s->ac_pred) {
  1833. if(dc_pred_dir) { //left
  1834. for(k = 1; k < 8; k++) {
  1835. block[k << 3] = ac_val[k] * scale;
  1836. if(!v->pquantizer)
  1837. block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
  1838. }
  1839. } else { //top
  1840. for(k = 1; k < 8; k++) {
  1841. block[k] = ac_val[k + 8] * scale;
  1842. if(!v->pquantizer)
  1843. block[k] += (block[k] < 0) ? -mquant : mquant;
  1844. }
  1845. }
  1846. i = 63;
  1847. }
  1848. }
  1849. s->block_last_index[n] = i;
  1850. return 0;
  1851. }
  1852. /** Decode P block
  1853. */
  1854. static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
  1855. {
  1856. MpegEncContext *s = &v->s;
  1857. GetBitContext *gb = &s->gb;
  1858. int i, j;
  1859. int subblkpat = 0;
  1860. int scale, off, idx, last, skip, value;
  1861. int ttblk = ttmb & 7;
  1862. if(ttmb == -1) {
  1863. ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
  1864. }
  1865. if(ttblk == TT_4X4) {
  1866. subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
  1867. }
  1868. if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
  1869. subblkpat = decode012(gb);
  1870. if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
  1871. if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
  1872. }
  1873. scale = 2 * mquant;
  1874. // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
  1875. if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
  1876. ttblk = TT_8X4;
  1877. subblkpat = 2 - (ttblk == TT_8X4_TOP);
  1878. }
  1879. if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
  1880. ttblk = TT_4X8;
  1881. subblkpat = 2 - (ttblk == TT_4X8_LEFT);
  1882. }
  1883. switch(ttblk) {
  1884. case TT_8X8:
  1885. i = 0;
  1886. last = 0;
  1887. while (!last) {
  1888. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1889. i += skip;
  1890. if(i > 63)
  1891. break;
  1892. idx = vc1_simple_progressive_8x8_zz[i++];
  1893. block[idx] = value * scale;
  1894. }
  1895. vc1_inv_trans(block, 8, 8);
  1896. break;
  1897. case TT_4X4:
  1898. for(j = 0; j < 4; j++) {
  1899. last = subblkpat & (1 << (3 - j));
  1900. i = 0;
  1901. off = (j & 1) * 4 + (j & 2) * 32;
  1902. while (!last) {
  1903. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1904. i += skip;
  1905. if(i > 15)
  1906. break;
  1907. idx = vc1_simple_progressive_4x4_zz[i++];
  1908. block[idx + off] = value * scale;
  1909. }
  1910. vc1_inv_trans(block + off, 4, 4);
  1911. }
  1912. break;
  1913. case TT_8X4:
  1914. for(j = 0; j < 2; j++) {
  1915. last = subblkpat & (1 << (1 - j));
  1916. i = 0;
  1917. off = j * 32;
  1918. while (!last) {
  1919. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1920. i += skip;
  1921. if(i > 31)
  1922. break;
  1923. idx = vc1_simple_progressive_8x4_zz[i++];
  1924. block[idx + off] = value * scale;
  1925. }
  1926. if(!(subblkpat & (1 << (1 - j)))) vc1_inv_trans(block + off, 8, 4);
  1927. }
  1928. break;
  1929. case TT_4X8:
  1930. for(j = 0; j < 2; j++) {
  1931. last = subblkpat & (1 << (1 - j));
  1932. i = 0;
  1933. off = j * 4;
  1934. while (!last) {
  1935. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1936. i += skip;
  1937. if(i > 31)
  1938. break;
  1939. idx = vc1_simple_progressive_8x4_zz[i++];
  1940. block[idx + off] = value * scale;
  1941. }
  1942. vc1_inv_trans(block + off, 4, 8);
  1943. }
  1944. break;
  1945. }
  1946. return 0;
  1947. }
  1948. /** Decode one P-frame MB (in Simple/Main profile)
  1949. * @todo TODO: Extend to AP
  1950. * @fixme FIXME: DC value for inter blocks not set
  1951. */
  1952. static int vc1_decode_p_mb(VC1Context *v, DCTELEM block[6][64])
  1953. {
  1954. MpegEncContext *s = &v->s;
  1955. GetBitContext *gb = &s->gb;
  1956. int i, j, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
  1957. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1958. int cbp; /* cbp decoding stuff */
  1959. int hybrid_pred; /* Prediction types */
  1960. int mqdiff, mquant; /* MB quantization */
  1961. int ttmb = v->ttmb; /* MB Transform type */
  1962. int status;
  1963. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1964. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1965. int mb_has_coeffs = 1; /* last_flag */
  1966. int dmv_x, dmv_y; /* Differential MV components */
  1967. int index, index1; /* LUT indices */
  1968. int val, sign; /* temp values */
  1969. int first_block = 1;
  1970. int dst_idx, off;
  1971. mquant = v->pq; /* Loosy initialization */
  1972. if (v->mv_type_mb_plane.is_raw)
  1973. v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1974. if (v->skip_mb_plane.is_raw)
  1975. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1976. s->current_picture.mbskip_table[mb_pos] = v->skip_mb_plane.data[mb_offset];
  1977. if (!v->mv_type_mb_plane.data[mb_offset]) /* 1MV mode */
  1978. {
  1979. if (!v->skip_mb_plane.data[mb_offset])
  1980. {
  1981. GET_MVDATA(dmv_x, dmv_y);
  1982. s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
  1983. vc1_pred_mv(s, dmv_x, dmv_y, 1, v->range_x, v->range_y);
  1984. /* FIXME Set DC val for inter block ? */
  1985. if (s->mb_intra && !mb_has_coeffs)
  1986. {
  1987. GET_MQUANT();
  1988. s->ac_pred = get_bits(gb, 1);
  1989. cbp = 0;
  1990. }
  1991. else if (mb_has_coeffs)
  1992. {
  1993. if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
  1994. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  1995. GET_MQUANT();
  1996. }
  1997. else
  1998. {
  1999. mquant = v->pq;
  2000. cbp = 0;
  2001. }
  2002. s->current_picture.qscale_table[mb_pos] = mquant;
  2003. if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
  2004. ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
  2005. VC1_TTMB_VLC_BITS, 2);
  2006. s->dsp.clear_blocks(block[0]);
  2007. vc1_mc_1mv(v);
  2008. dst_idx = 0;
  2009. for (i=0; i<6; i++)
  2010. {
  2011. s->dc_val[0][s->block_index[i]] = 0;
  2012. dst_idx += i >> 2;
  2013. val = ((cbp >> (5 - i)) & 1);
  2014. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  2015. if(s->mb_intra) {
  2016. vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
  2017. vc1_inv_trans(s->block[i], 8, 8);
  2018. for(j = 0; j < 64; j++) s->block[i][j] += 128;
  2019. s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
  2020. } else if(val) {
  2021. vc1_decode_p_block(v, block[i], i, mquant, ttmb, first_block);
  2022. if(!v->ttmbf && ttmb < 8) ttmb = -1;
  2023. first_block = 0;
  2024. s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
  2025. }
  2026. }
  2027. }
  2028. else //Skipped
  2029. {
  2030. s->mb_intra = 0;
  2031. s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
  2032. vc1_pred_mv(s, 0, 0, 1, v->range_x, v->range_y);
  2033. vc1_mc_1mv(v);
  2034. return 0;
  2035. }
  2036. } //1MV mode
  2037. else //4MV mode
  2038. {//FIXME: looks not conforming to standard and is not even theoretically complete
  2039. if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
  2040. {
  2041. int blk_intra[4], blk_coded[4];
  2042. /* Get CBPCY */
  2043. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  2044. for (i=0; i<4; i++)
  2045. {
  2046. val = ((cbp >> (5 - i)) & 1);
  2047. blk_intra[i] = 0;
  2048. blk_coded[i] = val;
  2049. if(val) {
  2050. GET_MVDATA(dmv_x, dmv_y);
  2051. blk_intra[i] = s->mb_intra;
  2052. }
  2053. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2054. hybrid_pred = get_bits(gb, 1);
  2055. }
  2056. if((blk_intra[0] | blk_intra[1] | blk_intra[2] | blk_intra[3]) ||
  2057. (blk_coded[0] | blk_coded[1] | blk_coded[2] | blk_coded[3])) {
  2058. GET_MQUANT();
  2059. if (s->mb_intra /* One of the 4 blocks is intra */
  2060. /* non-zero pred for that block */)
  2061. s->ac_pred = get_bits(gb, 1);
  2062. if (!v->ttmbf)
  2063. ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
  2064. VC1_TTMB_VLC_BITS, 12);
  2065. for(i = 0; i < 6; i++) {
  2066. val = ((cbp >> (5 - i)) & 1);
  2067. if(i & 4 || blk_intra[i] || val) {
  2068. if(i < 4 && blk_intra[i])
  2069. status = vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
  2070. else
  2071. status = vc1_decode_p_block(v, block[i], i, mquant, ttmb, 0);
  2072. }
  2073. }
  2074. }
  2075. return status;
  2076. }
  2077. else //Skipped MB
  2078. {
  2079. /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
  2080. for (i=0; i<4; i++)
  2081. {
  2082. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2083. hybrid_pred = get_bits(gb, 1);
  2084. }
  2085. /* TODO: blah */
  2086. return 0;
  2087. }
  2088. }
  2089. /* Should never happen */
  2090. return -1;
  2091. }
  2092. /** Decode blocks of I-frame
  2093. */
  2094. static void vc1_decode_i_blocks(VC1Context *v)
  2095. {
  2096. int k;
  2097. MpegEncContext *s = &v->s;
  2098. int cbp, val;
  2099. uint8_t *coded_val;
  2100. int mb_pos;
  2101. /* select codingmode used for VLC tables selection */
  2102. switch(v->y_ac_table_index){
  2103. case 0:
  2104. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  2105. break;
  2106. case 1:
  2107. v->codingset = CS_HIGH_MOT_INTRA;
  2108. break;
  2109. case 2:
  2110. v->codingset = CS_MID_RATE_INTRA;
  2111. break;
  2112. }
  2113. switch(v->c_ac_table_index){
  2114. case 0:
  2115. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  2116. break;
  2117. case 1:
  2118. v->codingset2 = CS_HIGH_MOT_INTER;
  2119. break;
  2120. case 2:
  2121. v->codingset2 = CS_MID_RATE_INTER;
  2122. break;
  2123. }
  2124. /* Set DC scale - y and c use the same */
  2125. s->y_dc_scale = s->y_dc_scale_table[v->pq];
  2126. s->c_dc_scale = s->c_dc_scale_table[v->pq];
  2127. //do frame decode
  2128. s->mb_x = s->mb_y = 0;
  2129. s->mb_intra = 1;
  2130. ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
  2131. for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  2132. for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  2133. ff_init_block_index(s);
  2134. ff_update_block_index(s);
  2135. s->dsp.clear_blocks(s->block[0]);
  2136. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  2137. s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
  2138. s->current_picture.qscale_table[mb_pos] = v->pq;
  2139. // do actual MB decoding and displaying
  2140. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  2141. v->s.ac_pred = get_bits(&v->s.gb, 1);
  2142. for(k = 0; k < 6; k++) {
  2143. val = ((cbp >> (5 - k)) & 1);
  2144. if (k < 4) {
  2145. int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
  2146. val = val ^ pred;
  2147. *coded_val = val;
  2148. }
  2149. cbp |= val << (5 - k);
  2150. vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
  2151. vc1_inv_trans(s->block[k], 8, 8);
  2152. if(v->pq >= 9 && v->overlap) {
  2153. vc1_overlap_block(s, s->block[k], k, (s->mb_y || k>1), (s->mb_x || (k != 0 && k != 2)));
  2154. }
  2155. }
  2156. vc1_put_block(v, s->block);
  2157. if(get_bits_count(&s->gb) > v->bits) {
  2158. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
  2159. return;
  2160. }
  2161. }
  2162. ff_draw_horiz_band(s, s->mb_y * 16, 16);
  2163. }
  2164. }
  2165. static void vc1_decode_p_blocks(VC1Context *v)
  2166. {
  2167. MpegEncContext *s = &v->s;
  2168. /* select codingmode used for VLC tables selection */
  2169. switch(v->c_ac_table_index){
  2170. case 0:
  2171. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  2172. break;
  2173. case 1:
  2174. v->codingset = CS_HIGH_MOT_INTRA;
  2175. break;
  2176. case 2:
  2177. v->codingset = CS_MID_RATE_INTRA;
  2178. break;
  2179. }
  2180. switch(v->c_ac_table_index){
  2181. case 0:
  2182. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  2183. break;
  2184. case 1:
  2185. v->codingset2 = CS_HIGH_MOT_INTER;
  2186. break;
  2187. case 2:
  2188. v->codingset2 = CS_MID_RATE_INTER;
  2189. break;
  2190. }
  2191. ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
  2192. s->first_slice_line = 1;
  2193. for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  2194. for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  2195. ff_init_block_index(s);
  2196. ff_update_block_index(s);
  2197. s->dsp.clear_blocks(s->block[0]);
  2198. vc1_decode_p_mb(v, s->block);
  2199. if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
  2200. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
  2201. return;
  2202. }
  2203. }
  2204. ff_draw_horiz_band(s, s->mb_y * 16, 16);
  2205. s->first_slice_line = 0;
  2206. }
  2207. }
  2208. static void vc1_decode_blocks(VC1Context *v)
  2209. {
  2210. v->s.esc3_level_length = 0;
  2211. switch(v->s.pict_type) {
  2212. case I_TYPE:
  2213. vc1_decode_i_blocks(v);
  2214. break;
  2215. case P_TYPE:
  2216. vc1_decode_p_blocks(v);
  2217. break;
  2218. }
  2219. }
  2220. /** Initialize a VC1/WMV3 decoder
  2221. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  2222. * @todo TODO: Decypher remaining bits in extra_data
  2223. */
  2224. static int vc1_decode_init(AVCodecContext *avctx)
  2225. {
  2226. VC1Context *v = avctx->priv_data;
  2227. MpegEncContext *s = &v->s;
  2228. GetBitContext gb;
  2229. if (!avctx->extradata_size || !avctx->extradata) return -1;
  2230. avctx->pix_fmt = PIX_FMT_YUV420P;
  2231. v->s.avctx = avctx;
  2232. if(ff_h263_decode_init(avctx) < 0)
  2233. return -1;
  2234. if (vc1_init_common(v) < 0) return -1;
  2235. av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
  2236. av_log(avctx, AV_LOG_INFO, "If you see a picture, don't believe your eyes.\n");
  2237. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  2238. avctx->coded_width = avctx->width;
  2239. avctx->coded_height = avctx->height;
  2240. if (avctx->codec_id == CODEC_ID_WMV3)
  2241. {
  2242. int count = 0;
  2243. // looks like WMV3 has a sequence header stored in the extradata
  2244. // advanced sequence header may be before the first frame
  2245. // the last byte of the extradata is a version number, 1 for the
  2246. // samples we can decode
  2247. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  2248. if (decode_sequence_header(avctx, &gb) < 0)
  2249. return -1;
  2250. count = avctx->extradata_size*8 - get_bits_count(&gb);
  2251. if (count>0)
  2252. {
  2253. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  2254. count, get_bits(&gb, count));
  2255. }
  2256. else if (count < 0)
  2257. {
  2258. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  2259. }
  2260. }
  2261. avctx->has_b_frames= !!(avctx->max_b_frames);
  2262. s->mb_width = (avctx->coded_width+15)>>4;
  2263. s->mb_height = (avctx->coded_height+15)>>4;
  2264. /* Allocate mb bitplanes */
  2265. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2266. return -1;
  2267. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2268. return -1;
  2269. if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
  2270. return -1;
  2271. if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
  2272. return -1;
  2273. /* For predictors */
  2274. v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
  2275. if (!v->previous_line_cbpcy) return -1;
  2276. /* Init coded blocks info */
  2277. if (v->profile == PROFILE_ADVANCED)
  2278. {
  2279. if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  2280. return -1;
  2281. if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  2282. return -1;
  2283. }
  2284. return 0;
  2285. }
  2286. /** Decode a VC1/WMV3 frame
  2287. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  2288. * @warning Initial try at using MpegEncContext stuff
  2289. */
  2290. static int vc1_decode_frame(AVCodecContext *avctx,
  2291. void *data, int *data_size,
  2292. uint8_t *buf, int buf_size)
  2293. {
  2294. VC1Context *v = avctx->priv_data;
  2295. MpegEncContext *s = &v->s;
  2296. AVFrame *pict = data;
  2297. /* no supplementary picture */
  2298. if (buf_size == 0) {
  2299. /* special case for last picture */
  2300. if (s->low_delay==0 && s->next_picture_ptr) {
  2301. *pict= *(AVFrame*)s->next_picture_ptr;
  2302. s->next_picture_ptr= NULL;
  2303. *data_size = sizeof(AVFrame);
  2304. }
  2305. return 0;
  2306. }
  2307. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  2308. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  2309. int i= ff_find_unused_picture(s, 0);
  2310. s->current_picture_ptr= &s->picture[i];
  2311. }
  2312. avctx->has_b_frames= !s->low_delay;
  2313. init_get_bits(&s->gb, buf, buf_size*8);
  2314. // do parse frame header
  2315. if(vc1_parse_frame_header(v, &s->gb) == -1)
  2316. return -1;
  2317. if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1;
  2318. // for hurry_up==5
  2319. s->current_picture.pict_type= s->pict_type;
  2320. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2321. /* skip B-frames if we don't have reference frames */
  2322. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
  2323. /* skip b frames if we are in a hurry */
  2324. if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
  2325. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  2326. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  2327. || avctx->skip_frame >= AVDISCARD_ALL)
  2328. return buf_size;
  2329. /* skip everything if we are in a hurry>=5 */
  2330. if(avctx->hurry_up>=5) return -1;//buf_size;
  2331. if(s->next_p_frame_damaged){
  2332. if(s->pict_type==B_TYPE)
  2333. return buf_size;
  2334. else
  2335. s->next_p_frame_damaged=0;
  2336. }
  2337. if(MPV_frame_start(s, avctx) < 0)
  2338. return -1;
  2339. ff_er_frame_start(s);
  2340. v->bits = buf_size * 8;
  2341. vc1_decode_blocks(v);
  2342. //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
  2343. // if(get_bits_count(&s->gb) > buf_size * 8)
  2344. // return -1;
  2345. ff_er_frame_end(s);
  2346. MPV_frame_end(s);
  2347. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  2348. assert(s->current_picture.pict_type == s->pict_type);
  2349. if (s->pict_type == B_TYPE || s->low_delay) {
  2350. *pict= *(AVFrame*)s->current_picture_ptr;
  2351. } else if (s->last_picture_ptr != NULL) {
  2352. *pict= *(AVFrame*)s->last_picture_ptr;
  2353. }
  2354. if(s->last_picture_ptr || s->low_delay){
  2355. *data_size = sizeof(AVFrame);
  2356. ff_print_debug_info(s, pict);
  2357. }
  2358. /* Return the Picture timestamp as the frame number */
  2359. /* we substract 1 because it is added on utils.c */
  2360. avctx->frame_number = s->picture_number - 1;
  2361. return buf_size;
  2362. }
  2363. /** Close a VC1/WMV3 decoder
  2364. * @warning Initial try at using MpegEncContext stuff
  2365. */
  2366. static int vc1_decode_end(AVCodecContext *avctx)
  2367. {
  2368. VC1Context *v = avctx->priv_data;
  2369. av_freep(&v->hrd_rate);
  2370. av_freep(&v->hrd_buffer);
  2371. MPV_common_end(&v->s);
  2372. free_bitplane(&v->mv_type_mb_plane);
  2373. free_bitplane(&v->skip_mb_plane);
  2374. free_bitplane(&v->direct_mb_plane);
  2375. return 0;
  2376. }
  2377. AVCodec vc1_decoder = {
  2378. "vc1",
  2379. CODEC_TYPE_VIDEO,
  2380. CODEC_ID_VC1,
  2381. sizeof(VC1Context),
  2382. vc1_decode_init,
  2383. NULL,
  2384. vc1_decode_end,
  2385. vc1_decode_frame,
  2386. CODEC_CAP_DELAY,
  2387. NULL
  2388. };
  2389. AVCodec wmv3_decoder = {
  2390. "wmv3",
  2391. CODEC_TYPE_VIDEO,
  2392. CODEC_ID_WMV3,
  2393. sizeof(VC1Context),
  2394. vc1_decode_init,
  2395. NULL,
  2396. vc1_decode_end,
  2397. vc1_decode_frame,
  2398. CODEC_CAP_DELAY,
  2399. NULL
  2400. };