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.

2705 lines
87KB

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