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.

2701 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. val = get_bits(gb, size_table[index1] - val); \
  1220. sign = 0 - (val&1); \
  1221. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1222. \
  1223. index1 = index/6; \
  1224. if (s->mspel && index1 == 5) val = 1; \
  1225. else val = 0; \
  1226. val = get_bits(gb, size_table[index1] - val); \
  1227. sign = 0 - (val&1); \
  1228. _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1229. }
  1230. /** Predict and set motion vector
  1231. */
  1232. static inline void vc1_pred_mv(MpegEncContext *s, int dmv_x, int dmv_y, int mv1, int r_x, int r_y)
  1233. {
  1234. int xy, wrap, off;
  1235. int16_t *A, *B, *C;
  1236. int px, py;
  1237. int sum;
  1238. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1239. /* scale MV difference to be quad-pel */
  1240. dmv_x <<= s->mspel;
  1241. dmv_y <<= s->mspel;
  1242. wrap = s->b8_stride;
  1243. xy = s->block_index[0];
  1244. C = s->current_picture.motion_val[0][xy - (1 << mv1)];
  1245. A = s->current_picture.motion_val[0][xy - (wrap << mv1)];
  1246. off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
  1247. B = s->current_picture.motion_val[0][xy + ((off - wrap) << mv1)];
  1248. if(!s->first_slice_line) { // predictor A is not out of bounds
  1249. if(s->mb_width == 1) {
  1250. px = A[0];
  1251. py = A[1];
  1252. } else {
  1253. px = mid_pred(A[0], B[0], C[0]);
  1254. py = mid_pred(A[1], B[1], C[1]);
  1255. }
  1256. } else if(s->mb_x) { // predictor C is not out of bounds
  1257. px = C[0];
  1258. py = C[1];
  1259. } else {
  1260. px = py = 0;
  1261. }
  1262. if(s->mb_intra) px = py = 0;
  1263. /* Pullback MV as specified in 8.3.5.3.4 */
  1264. {
  1265. int qx, qy, X, Y;
  1266. qx = s->mb_x << 6; //FIXME: add real block coords for 4MV mode
  1267. qy = s->mb_y << 6;
  1268. X = (s->mb_width << 6) - 4;
  1269. Y = (s->mb_height << 6) - 4;
  1270. if(mv1) {
  1271. if(qx + px < -60) px = -60 - qx;
  1272. if(qy + py < -60) py = -60 - qy;
  1273. } else {
  1274. if(qx + px < -28) px = -28 - qx;
  1275. if(qy + py < -28) py = -28 - qy;
  1276. }
  1277. if(qx + px > X) px = X - qx;
  1278. if(qy + py > Y) py = Y - qy;
  1279. }
  1280. /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
  1281. if(!s->mb_intra && !s->first_slice_line && s->mb_x) {
  1282. if(IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride]))
  1283. sum = ABS(px) + ABS(py);
  1284. else
  1285. sum = ABS(px - A[0]) + ABS(py - A[1]);
  1286. if(sum > 32) {
  1287. if(get_bits1(&s->gb)) {
  1288. px = A[0];
  1289. py = A[1];
  1290. } else {
  1291. px = C[0];
  1292. py = C[1];
  1293. }
  1294. } else {
  1295. if(IS_INTRA(s->current_picture.mb_type[mb_pos - 1]))
  1296. sum = ABS(px) + ABS(py);
  1297. else
  1298. sum = ABS(px - C[0]) + ABS(py - C[1]);
  1299. if(sum > 32) {
  1300. if(get_bits1(&s->gb)) {
  1301. px = A[0];
  1302. py = A[1];
  1303. } else {
  1304. px = C[0];
  1305. py = C[1];
  1306. }
  1307. }
  1308. }
  1309. }
  1310. /* store MV using signed modulus of MV range defined in 4.11 */
  1311. s->mv[0][0][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
  1312. s->mv[0][0][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
  1313. }
  1314. /** Get predicted DC value for I-frames only
  1315. * prediction dir: left=0, top=1
  1316. * @param s MpegEncContext
  1317. * @param[in] n block index in the current MB
  1318. * @param dc_val_ptr Pointer to DC predictor
  1319. * @param dir_ptr Prediction direction for use in AC prediction
  1320. */
  1321. static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  1322. int16_t **dc_val_ptr, int *dir_ptr)
  1323. {
  1324. int a, b, c, wrap, pred, scale;
  1325. int16_t *dc_val;
  1326. static const uint16_t dcpred[32] = {
  1327. -1, 1024, 512, 341, 256, 205, 171, 146, 128,
  1328. 114, 102, 93, 85, 79, 73, 68, 64,
  1329. 60, 57, 54, 51, 49, 47, 45, 43,
  1330. 41, 39, 38, 37, 35, 34, 33
  1331. };
  1332. /* find prediction - wmv3_dc_scale always used here in fact */
  1333. if (n < 4) scale = s->y_dc_scale;
  1334. else scale = s->c_dc_scale;
  1335. wrap = s->block_wrap[n];
  1336. dc_val= s->dc_val[0] + s->block_index[n];
  1337. /* B A
  1338. * C X
  1339. */
  1340. c = dc_val[ - 1];
  1341. b = dc_val[ - 1 - wrap];
  1342. a = dc_val[ - wrap];
  1343. if (pq < 9 || !overlap)
  1344. {
  1345. /* Set outer values */
  1346. if (!s->mb_y && (n!=2 && n!=3)) b=a=dcpred[scale];
  1347. if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
  1348. }
  1349. else
  1350. {
  1351. /* Set outer values */
  1352. if (!s->mb_y && (n!=2 && n!=3)) b=a=0;
  1353. if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
  1354. }
  1355. if (abs(a - b) <= abs(b - c)) {
  1356. pred = c;
  1357. *dir_ptr = 1;//left
  1358. } else {
  1359. pred = a;
  1360. *dir_ptr = 0;//top
  1361. }
  1362. /* update predictor */
  1363. *dc_val_ptr = &dc_val[0];
  1364. return pred;
  1365. }
  1366. /** Get predicted DC value
  1367. * prediction dir: left=0, top=1
  1368. * @param s MpegEncContext
  1369. * @param[in] n block index in the current MB
  1370. * @param dc_val_ptr Pointer to DC predictor
  1371. * @param dir_ptr Prediction direction for use in AC prediction
  1372. */
  1373. static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
  1374. int a_avail, int c_avail,
  1375. int16_t **dc_val_ptr, int *dir_ptr)
  1376. {
  1377. int a, b, c, wrap, pred, scale;
  1378. int16_t *dc_val;
  1379. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1380. int mb_pos2, q1, q2;
  1381. /* find prediction - wmv3_dc_scale always used here in fact */
  1382. if (n < 4) scale = s->y_dc_scale;
  1383. else scale = s->c_dc_scale;
  1384. wrap = s->block_wrap[n];
  1385. dc_val= s->dc_val[0] + s->block_index[n];
  1386. /* B A
  1387. * C X
  1388. */
  1389. c = dc_val[ - 1];
  1390. b = dc_val[ - 1 - wrap];
  1391. a = dc_val[ - wrap];
  1392. if(a_avail && c_avail) {
  1393. if(abs(a - b) <= abs(b - c)) {
  1394. pred = c;
  1395. *dir_ptr = 1;//left
  1396. } else {
  1397. pred = a;
  1398. *dir_ptr = 0;//top
  1399. }
  1400. } else if(a_avail) {
  1401. pred = a;
  1402. *dir_ptr = 0;//top
  1403. } else if(c_avail) {
  1404. pred = c;
  1405. *dir_ptr = 1;//left
  1406. } else {
  1407. pred = 0;
  1408. *dir_ptr = 1;//left
  1409. }
  1410. /* scale coeffs if needed */
  1411. mb_pos2 = mb_pos - *dir_ptr - (1 - *dir_ptr) * s->mb_stride;
  1412. q1 = s->current_picture.qscale_table[mb_pos];
  1413. q2 = s->current_picture.qscale_table[mb_pos2];
  1414. if(0 && q1 && q2 && q1 != q2) {
  1415. q1 = s->y_dc_scale_table[q1];
  1416. q2 = s->y_dc_scale_table[q2];
  1417. pred = (pred * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1418. }
  1419. /* update predictor */
  1420. *dc_val_ptr = &dc_val[0];
  1421. return pred;
  1422. }
  1423. /**
  1424. * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
  1425. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1426. * @todo TODO: Integrate to MpegEncContext facilities
  1427. * @{
  1428. */
  1429. static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
  1430. {
  1431. int xy, wrap, pred, a, b, c;
  1432. xy = s->block_index[n];
  1433. wrap = s->b8_stride;
  1434. /* B C
  1435. * A X
  1436. */
  1437. a = s->coded_block[xy - 1 ];
  1438. b = s->coded_block[xy - 1 - wrap];
  1439. c = s->coded_block[xy - wrap];
  1440. if (b == c) {
  1441. pred = a;
  1442. } else {
  1443. pred = c;
  1444. }
  1445. /* store value */
  1446. *coded_block_ptr = &s->coded_block[xy];
  1447. return pred;
  1448. }
  1449. /**
  1450. * Decode one AC coefficient
  1451. * @param v The VC1 context
  1452. * @param last Last coefficient
  1453. * @param skip How much zero coefficients to skip
  1454. * @param value Decoded AC coefficient value
  1455. * @see 8.1.3.4
  1456. */
  1457. static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
  1458. {
  1459. GetBitContext *gb = &v->s.gb;
  1460. int index, escape, run = 0, level = 0, lst = 0;
  1461. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1462. if (index != vc1_ac_sizes[codingset] - 1) {
  1463. run = vc1_index_decode_table[codingset][index][0];
  1464. level = vc1_index_decode_table[codingset][index][1];
  1465. lst = index >= vc1_last_decode_table[codingset];
  1466. if(get_bits(gb, 1))
  1467. level = -level;
  1468. } else {
  1469. escape = decode210(gb);
  1470. if (escape == 0) {
  1471. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1472. run = vc1_index_decode_table[codingset][index][0];
  1473. level = vc1_index_decode_table[codingset][index][1];
  1474. lst = index >= vc1_last_decode_table[codingset];
  1475. if(lst)
  1476. level += vc1_last_delta_level_table[codingset][run];
  1477. else
  1478. level += vc1_delta_level_table[codingset][run];
  1479. if(get_bits(gb, 1))
  1480. level = -level;
  1481. } else if (escape == 1) {
  1482. index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
  1483. run = vc1_index_decode_table[codingset][index][0];
  1484. level = vc1_index_decode_table[codingset][index][1];
  1485. lst = index >= vc1_last_decode_table[codingset];
  1486. if(lst)
  1487. run += vc1_last_delta_run_table[codingset][level] + 1;
  1488. else
  1489. run += vc1_delta_run_table[codingset][level] + 1;
  1490. if(get_bits(gb, 1))
  1491. level = -level;
  1492. } else {
  1493. int sign;
  1494. lst = get_bits(gb, 1);
  1495. if(v->s.esc3_level_length == 0) {
  1496. if(v->pq < 8 || v->dquantfrm) { // table 59
  1497. v->s.esc3_level_length = get_bits(gb, 3);
  1498. if(!v->s.esc3_level_length)
  1499. v->s.esc3_level_length = get_bits(gb, 2) + 8;
  1500. } else { //table 60
  1501. v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
  1502. }
  1503. v->s.esc3_run_length = 3 + get_bits(gb, 2);
  1504. }
  1505. run = get_bits(gb, v->s.esc3_run_length);
  1506. sign = get_bits(gb, 1);
  1507. level = get_bits(gb, v->s.esc3_level_length);
  1508. if(sign)
  1509. level = -level;
  1510. }
  1511. }
  1512. *last = lst;
  1513. *skip = run;
  1514. *value = level;
  1515. }
  1516. /** Decode intra block in intra frames - should be faster than decode_intra_block
  1517. * @param v VC1Context
  1518. * @param block block to decode
  1519. * @param coded are AC coeffs present or not
  1520. * @param codingset set of VLC to decode data
  1521. */
  1522. static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
  1523. {
  1524. GetBitContext *gb = &v->s.gb;
  1525. MpegEncContext *s = &v->s;
  1526. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  1527. int run_diff, i;
  1528. int16_t *dc_val;
  1529. int16_t *ac_val, *ac_val2;
  1530. int dcdiff;
  1531. /* Get DC differential */
  1532. if (n < 4) {
  1533. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1534. } else {
  1535. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1536. }
  1537. if (dcdiff < 0){
  1538. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  1539. return -1;
  1540. }
  1541. if (dcdiff)
  1542. {
  1543. if (dcdiff == 119 /* ESC index value */)
  1544. {
  1545. /* TODO: Optimize */
  1546. if (v->pq == 1) dcdiff = get_bits(gb, 10);
  1547. else if (v->pq == 2) dcdiff = get_bits(gb, 9);
  1548. else dcdiff = get_bits(gb, 8);
  1549. }
  1550. else
  1551. {
  1552. if (v->pq == 1)
  1553. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1554. else if (v->pq == 2)
  1555. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1556. }
  1557. if (get_bits(gb, 1))
  1558. dcdiff = -dcdiff;
  1559. }
  1560. /* Prediction */
  1561. dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
  1562. *dc_val = dcdiff;
  1563. /* Store the quantized DC coeff, used for prediction */
  1564. if (n < 4) {
  1565. block[0] = dcdiff * s->y_dc_scale;
  1566. } else {
  1567. block[0] = dcdiff * s->c_dc_scale;
  1568. }
  1569. /* Skip ? */
  1570. run_diff = 0;
  1571. i = 0;
  1572. if (!coded) {
  1573. goto not_coded;
  1574. }
  1575. //AC Decoding
  1576. i = 1;
  1577. {
  1578. int last = 0, skip, value;
  1579. const int8_t *zz_table;
  1580. int scale;
  1581. int k;
  1582. scale = v->pq * 2 + v->halfpq;
  1583. if(v->s.ac_pred) {
  1584. if(!dc_pred_dir)
  1585. zz_table = vc1_horizontal_zz;
  1586. else
  1587. zz_table = vc1_vertical_zz;
  1588. } else
  1589. zz_table = vc1_normal_zz;
  1590. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1591. ac_val2 = ac_val;
  1592. if(dc_pred_dir) //left
  1593. ac_val -= 16;
  1594. else //top
  1595. ac_val -= 16 * s->block_wrap[n];
  1596. while (!last) {
  1597. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  1598. i += skip;
  1599. if(i > 63)
  1600. break;
  1601. block[zz_table[i++]] = value;
  1602. }
  1603. /* apply AC prediction if needed */
  1604. if(s->ac_pred) {
  1605. if(dc_pred_dir) { //left
  1606. for(k = 1; k < 8; k++)
  1607. block[k << 3] += ac_val[k];
  1608. } else { //top
  1609. for(k = 1; k < 8; k++)
  1610. block[k] += ac_val[k + 8];
  1611. }
  1612. }
  1613. /* save AC coeffs for further prediction */
  1614. for(k = 1; k < 8; k++) {
  1615. ac_val2[k] = block[k << 3];
  1616. ac_val2[k + 8] = block[k];
  1617. }
  1618. /* scale AC coeffs */
  1619. for(k = 1; k < 64; k++)
  1620. if(block[k]) {
  1621. block[k] *= scale;
  1622. if(!v->pquantizer)
  1623. block[k] += (block[k] < 0) ? -v->pq : v->pq;
  1624. }
  1625. if(s->ac_pred) i = 63;
  1626. }
  1627. not_coded:
  1628. if(!coded) {
  1629. int k, scale;
  1630. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1631. ac_val2 = ac_val;
  1632. scale = v->pq * 2 + v->halfpq;
  1633. memset(ac_val2, 0, 16 * 2);
  1634. if(dc_pred_dir) {//left
  1635. ac_val -= 16;
  1636. if(s->ac_pred)
  1637. memcpy(ac_val2, ac_val, 8 * 2);
  1638. } else {//top
  1639. ac_val -= 16 * s->block_wrap[n];
  1640. if(s->ac_pred)
  1641. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  1642. }
  1643. /* apply AC prediction if needed */
  1644. if(s->ac_pred) {
  1645. if(dc_pred_dir) { //left
  1646. for(k = 1; k < 8; k++) {
  1647. block[k << 3] = ac_val[k] * scale;
  1648. if(!v->pquantizer)
  1649. block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
  1650. }
  1651. } else { //top
  1652. for(k = 1; k < 8; k++) {
  1653. block[k] = ac_val[k + 8] * scale;
  1654. if(!v->pquantizer)
  1655. block[k] += (block[k] < 0) ? -v->pq : v->pq;
  1656. }
  1657. }
  1658. i = 63;
  1659. }
  1660. }
  1661. s->block_last_index[n] = i;
  1662. return 0;
  1663. }
  1664. /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
  1665. * @param v VC1Context
  1666. * @param block block to decode
  1667. * @param coded are AC coeffs present or not
  1668. * @param mquant block quantizer
  1669. * @param codingset set of VLC to decode data
  1670. */
  1671. static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
  1672. {
  1673. GetBitContext *gb = &v->s.gb;
  1674. MpegEncContext *s = &v->s;
  1675. int dc_pred_dir = 0; /* Direction of the DC prediction used */
  1676. int run_diff, i;
  1677. int16_t *dc_val;
  1678. int16_t *ac_val, *ac_val2;
  1679. int dcdiff;
  1680. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1681. int a_avail = v->a_avail, c_avail = v->c_avail;
  1682. /* XXX: Guard against dumb values of mquant */
  1683. mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
  1684. /* Set DC scale - y and c use the same */
  1685. s->y_dc_scale = s->y_dc_scale_table[mquant];
  1686. s->c_dc_scale = s->c_dc_scale_table[mquant];
  1687. /* Get DC differential */
  1688. if (n < 4) {
  1689. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1690. } else {
  1691. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1692. }
  1693. if (dcdiff < 0){
  1694. av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
  1695. return -1;
  1696. }
  1697. if (dcdiff)
  1698. {
  1699. if (dcdiff == 119 /* ESC index value */)
  1700. {
  1701. /* TODO: Optimize */
  1702. if (mquant == 1) dcdiff = get_bits(gb, 10);
  1703. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  1704. else dcdiff = get_bits(gb, 8);
  1705. }
  1706. else
  1707. {
  1708. if (mquant == 1)
  1709. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1710. else if (mquant == 2)
  1711. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1712. }
  1713. if (get_bits(gb, 1))
  1714. dcdiff = -dcdiff;
  1715. }
  1716. /* Prediction */
  1717. dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
  1718. *dc_val = dcdiff;
  1719. /* Store the quantized DC coeff, used for prediction */
  1720. if (n < 4) {
  1721. block[0] = dcdiff * s->y_dc_scale;
  1722. } else {
  1723. block[0] = dcdiff * s->c_dc_scale;
  1724. }
  1725. /* Skip ? */
  1726. run_diff = 0;
  1727. i = 0;
  1728. if (!coded) {
  1729. goto not_coded;
  1730. }
  1731. //AC Decoding
  1732. i = 1;
  1733. {
  1734. int last = 0, skip, value;
  1735. const int8_t *zz_table;
  1736. int scale;
  1737. int k;
  1738. scale = mquant * 2;
  1739. zz_table = vc1_simple_progressive_8x8_zz;
  1740. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1741. ac_val2 = ac_val;
  1742. if(dc_pred_dir) //left
  1743. ac_val -= 16;
  1744. else //top
  1745. ac_val -= 16 * s->block_wrap[n];
  1746. while (!last) {
  1747. vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
  1748. i += skip;
  1749. if(i > 63)
  1750. break;
  1751. block[zz_table[i++]] = value;
  1752. }
  1753. /* apply AC prediction if needed */
  1754. if(s->ac_pred) {
  1755. /* scale predictors if needed*/
  1756. int mb_pos2, q1, q2;
  1757. mb_pos2 = mb_pos - dc_pred_dir - (1 - dc_pred_dir) * s->mb_stride;
  1758. q1 = s->current_picture.qscale_table[mb_pos];
  1759. q2 = s->current_picture.qscale_table[mb_pos2];
  1760. if(!c_avail) {
  1761. memset(ac_val, 0, 8 * sizeof(ac_val[0]));
  1762. dc_pred_dir = 0;
  1763. }
  1764. if(!a_avail) {
  1765. memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
  1766. dc_pred_dir = 1;
  1767. }
  1768. if(!q1 && q1 && q2 && q1 != q2) {
  1769. q1 = q1 * 2 - 1;
  1770. q2 = q2 * 2 - 1;
  1771. if(dc_pred_dir) { //left
  1772. for(k = 1; k < 8; k++)
  1773. block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1774. } else { //top
  1775. for(k = 1; k < 8; k++)
  1776. block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
  1777. }
  1778. } else {
  1779. if(dc_pred_dir) { //left
  1780. for(k = 1; k < 8; k++)
  1781. block[k << 3] += ac_val[k];
  1782. } else { //top
  1783. for(k = 1; k < 8; k++)
  1784. block[k] += ac_val[k + 8];
  1785. }
  1786. }
  1787. }
  1788. /* save AC coeffs for further prediction */
  1789. for(k = 1; k < 8; k++) {
  1790. ac_val2[k] = block[k << 3];
  1791. ac_val2[k + 8] = block[k];
  1792. }
  1793. /* scale AC coeffs */
  1794. for(k = 1; k < 64; k++)
  1795. if(block[k]) {
  1796. block[k] *= scale;
  1797. if(!v->pquantizer)
  1798. block[k] += (block[k] < 0) ? -mquant : mquant;
  1799. }
  1800. if(s->ac_pred) i = 63;
  1801. }
  1802. not_coded:
  1803. if(!coded) {
  1804. int k, scale;
  1805. ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
  1806. ac_val2 = ac_val;
  1807. if(!c_avail) {
  1808. memset(ac_val, 0, 8 * sizeof(ac_val[0]));
  1809. dc_pred_dir = 0;
  1810. }
  1811. if(!a_avail) {
  1812. memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
  1813. dc_pred_dir = 1;
  1814. }
  1815. scale = mquant * 2;
  1816. memset(ac_val2, 0, 16 * 2);
  1817. if(dc_pred_dir) {//left
  1818. ac_val -= 16;
  1819. if(s->ac_pred)
  1820. memcpy(ac_val2, ac_val, 8 * 2);
  1821. } else {//top
  1822. ac_val -= 16 * s->block_wrap[n];
  1823. if(s->ac_pred)
  1824. memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
  1825. }
  1826. /* apply AC prediction if needed */
  1827. if(s->ac_pred) {
  1828. if(dc_pred_dir) { //left
  1829. for(k = 1; k < 8; k++) {
  1830. block[k << 3] = ac_val[k] * scale;
  1831. if(!v->pquantizer)
  1832. block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
  1833. }
  1834. } else { //top
  1835. for(k = 1; k < 8; k++) {
  1836. block[k] = ac_val[k + 8] * scale;
  1837. if(!v->pquantizer)
  1838. block[k] += (block[k] < 0) ? -mquant : mquant;
  1839. }
  1840. }
  1841. i = 63;
  1842. }
  1843. }
  1844. s->block_last_index[n] = i;
  1845. return 0;
  1846. }
  1847. /** Decode P block
  1848. */
  1849. static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
  1850. {
  1851. MpegEncContext *s = &v->s;
  1852. GetBitContext *gb = &s->gb;
  1853. int i, j;
  1854. int subblkpat = 0;
  1855. int scale, off, idx, last, skip, value;
  1856. int ttblk = ttmb & 7;
  1857. if(ttmb == -1) {
  1858. ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
  1859. }
  1860. if(ttblk == TT_4X4) {
  1861. subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
  1862. }
  1863. if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
  1864. subblkpat = decode012(gb);
  1865. if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
  1866. if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
  1867. if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
  1868. }
  1869. scale = 2 * mquant;
  1870. // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
  1871. if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
  1872. ttblk = TT_8X4;
  1873. subblkpat = 2 - (ttblk == TT_8X4_TOP);
  1874. }
  1875. if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
  1876. ttblk = TT_4X8;
  1877. subblkpat = 2 - (ttblk == TT_4X8_LEFT);
  1878. }
  1879. switch(ttblk) {
  1880. case TT_8X8:
  1881. i = 0;
  1882. last = 0;
  1883. while (!last) {
  1884. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1885. i += skip;
  1886. if(i > 63)
  1887. break;
  1888. idx = vc1_simple_progressive_8x8_zz[i++];
  1889. block[idx] = value * scale;
  1890. }
  1891. vc1_inv_trans(block, 8, 8);
  1892. break;
  1893. case TT_4X4:
  1894. for(j = 0; j < 4; j++) {
  1895. last = subblkpat & (1 << (3 - j));
  1896. i = 0;
  1897. off = (j & 1) * 4 + (j & 2) * 32;
  1898. while (!last) {
  1899. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1900. i += skip;
  1901. if(i > 15)
  1902. break;
  1903. idx = vc1_simple_progressive_4x4_zz[i++];
  1904. block[idx + off] = value * scale;
  1905. }
  1906. vc1_inv_trans(block + off, 4, 4);
  1907. }
  1908. break;
  1909. case TT_8X4:
  1910. for(j = 0; j < 2; j++) {
  1911. last = subblkpat & (1 << (1 - j));
  1912. i = 0;
  1913. off = j * 32;
  1914. while (!last) {
  1915. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1916. i += skip;
  1917. if(i > 31)
  1918. break;
  1919. idx = vc1_simple_progressive_8x4_zz[i++];
  1920. block[idx + off] = value * scale;
  1921. }
  1922. if(!(subblkpat & (1 << (1 - j)))) vc1_inv_trans(block + off, 8, 4);
  1923. }
  1924. break;
  1925. case TT_4X8:
  1926. for(j = 0; j < 2; j++) {
  1927. last = subblkpat & (1 << (1 - j));
  1928. i = 0;
  1929. off = j * 4;
  1930. while (!last) {
  1931. vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
  1932. i += skip;
  1933. if(i > 31)
  1934. break;
  1935. idx = vc1_simple_progressive_8x4_zz[i++];
  1936. block[idx + off] = value * scale;
  1937. }
  1938. vc1_inv_trans(block + off, 4, 8);
  1939. }
  1940. break;
  1941. }
  1942. return 0;
  1943. }
  1944. /** Decode one P-frame MB (in Simple/Main profile)
  1945. * @todo TODO: Extend to AP
  1946. * @fixme FIXME: DC value for inter blocks not set
  1947. */
  1948. static int vc1_decode_p_mb(VC1Context *v, DCTELEM block[6][64])
  1949. {
  1950. MpegEncContext *s = &v->s;
  1951. GetBitContext *gb = &s->gb;
  1952. int i, j, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
  1953. int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
  1954. int cbp; /* cbp decoding stuff */
  1955. int hybrid_pred; /* Prediction types */
  1956. int mqdiff, mquant; /* MB quantization */
  1957. int ttmb = v->ttmb; /* MB Transform type */
  1958. int status;
  1959. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1960. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1961. int mb_has_coeffs = 1; /* last_flag */
  1962. int dmv_x, dmv_y; /* Differential MV components */
  1963. int index, index1; /* LUT indices */
  1964. int val, sign; /* temp values */
  1965. int first_block = 1;
  1966. int dst_idx, off;
  1967. mquant = v->pq; /* Loosy initialization */
  1968. if (v->mv_type_mb_plane.is_raw)
  1969. v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1970. if (v->skip_mb_plane.is_raw)
  1971. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1972. s->current_picture.mbskip_table[mb_pos] = v->skip_mb_plane.data[mb_offset];
  1973. if (!v->mv_type_mb_plane.data[mb_offset]) /* 1MV mode */
  1974. {
  1975. if (!v->skip_mb_plane.data[mb_offset])
  1976. {
  1977. GET_MVDATA(dmv_x, dmv_y);
  1978. s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
  1979. vc1_pred_mv(s, dmv_x, dmv_y, 1, v->range_x, v->range_y);
  1980. /* FIXME Set DC val for inter block ? */
  1981. if (s->mb_intra && !mb_has_coeffs)
  1982. {
  1983. GET_MQUANT();
  1984. s->ac_pred = get_bits(gb, 1);
  1985. cbp = 0;
  1986. }
  1987. else if (mb_has_coeffs)
  1988. {
  1989. if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
  1990. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  1991. GET_MQUANT();
  1992. }
  1993. else
  1994. {
  1995. mquant = v->pq;
  1996. cbp = 0;
  1997. }
  1998. s->current_picture.qscale_table[mb_pos] = mquant;
  1999. if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
  2000. ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
  2001. VC1_TTMB_VLC_BITS, 2);
  2002. s->dsp.clear_blocks(block[0]);
  2003. vc1_mc_1mv(v);
  2004. dst_idx = 0;
  2005. for (i=0; i<6; i++)
  2006. {
  2007. s->dc_val[0][s->block_index[i]] = 0;
  2008. dst_idx += i >> 2;
  2009. val = ((cbp >> (5 - i)) & 1);
  2010. off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
  2011. if(s->mb_intra) {
  2012. /* check if prediction blocks A and C are available */
  2013. v->a_avail = v->c_avail = 0;
  2014. if((i == 2 || i == 3) || (s->mb_y && IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride])))
  2015. v->a_avail = 1;
  2016. if((i == 1 || i == 3) || (s->mb_x && IS_INTRA(s->current_picture.mb_type[mb_pos - 1])))
  2017. v->c_avail = 1;
  2018. vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
  2019. vc1_inv_trans(s->block[i], 8, 8);
  2020. for(j = 0; j < 64; j++) s->block[i][j] += 128;
  2021. s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
  2022. /* TODO: proper loop filtering */
  2023. if(v->a_avail)
  2024. s->dsp.h263_v_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
  2025. if(v->c_avail)
  2026. s->dsp.h263_h_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
  2027. } else if(val) {
  2028. vc1_decode_p_block(v, block[i], i, mquant, ttmb, first_block);
  2029. if(!v->ttmbf && ttmb < 8) ttmb = -1;
  2030. first_block = 0;
  2031. s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
  2032. }
  2033. }
  2034. }
  2035. else //Skipped
  2036. {
  2037. s->mb_intra = 0;
  2038. s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
  2039. vc1_pred_mv(s, 0, 0, 1, v->range_x, v->range_y);
  2040. vc1_mc_1mv(v);
  2041. return 0;
  2042. }
  2043. } //1MV mode
  2044. else //4MV mode
  2045. {//FIXME: looks not conforming to standard and is not even theoretically complete
  2046. if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
  2047. {
  2048. int blk_intra[4], blk_coded[4];
  2049. /* Get CBPCY */
  2050. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
  2051. for (i=0; i<4; i++)
  2052. {
  2053. val = ((cbp >> (5 - i)) & 1);
  2054. blk_intra[i] = 0;
  2055. blk_coded[i] = val;
  2056. if(val) {
  2057. GET_MVDATA(dmv_x, dmv_y);
  2058. blk_intra[i] = s->mb_intra;
  2059. }
  2060. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2061. hybrid_pred = get_bits(gb, 1);
  2062. }
  2063. if((blk_intra[0] | blk_intra[1] | blk_intra[2] | blk_intra[3]) ||
  2064. (blk_coded[0] | blk_coded[1] | blk_coded[2] | blk_coded[3])) {
  2065. GET_MQUANT();
  2066. if (s->mb_intra /* One of the 4 blocks is intra */
  2067. /* non-zero pred for that block */)
  2068. s->ac_pred = get_bits(gb, 1);
  2069. if (!v->ttmbf)
  2070. ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
  2071. VC1_TTMB_VLC_BITS, 12);
  2072. for(i = 0; i < 6; i++) {
  2073. val = ((cbp >> (5 - i)) & 1);
  2074. if(i & 4 || blk_intra[i] || val) {
  2075. if(i < 4 && blk_intra[i])
  2076. status = vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
  2077. else
  2078. status = vc1_decode_p_block(v, block[i], i, mquant, ttmb, 0);
  2079. }
  2080. }
  2081. }
  2082. return status;
  2083. }
  2084. else //Skipped MB
  2085. {
  2086. /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
  2087. for (i=0; i<4; i++)
  2088. {
  2089. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2090. hybrid_pred = get_bits(gb, 1);
  2091. }
  2092. /* TODO: blah */
  2093. return 0;
  2094. }
  2095. }
  2096. /* Should never happen */
  2097. return -1;
  2098. }
  2099. /** Decode blocks of I-frame
  2100. */
  2101. static void vc1_decode_i_blocks(VC1Context *v)
  2102. {
  2103. int k;
  2104. MpegEncContext *s = &v->s;
  2105. int cbp, val;
  2106. uint8_t *coded_val;
  2107. int mb_pos;
  2108. /* select codingmode used for VLC tables selection */
  2109. switch(v->y_ac_table_index){
  2110. case 0:
  2111. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  2112. break;
  2113. case 1:
  2114. v->codingset = CS_HIGH_MOT_INTRA;
  2115. break;
  2116. case 2:
  2117. v->codingset = CS_MID_RATE_INTRA;
  2118. break;
  2119. }
  2120. switch(v->c_ac_table_index){
  2121. case 0:
  2122. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  2123. break;
  2124. case 1:
  2125. v->codingset2 = CS_HIGH_MOT_INTER;
  2126. break;
  2127. case 2:
  2128. v->codingset2 = CS_MID_RATE_INTER;
  2129. break;
  2130. }
  2131. /* Set DC scale - y and c use the same */
  2132. s->y_dc_scale = s->y_dc_scale_table[v->pq];
  2133. s->c_dc_scale = s->c_dc_scale_table[v->pq];
  2134. //do frame decode
  2135. s->mb_x = s->mb_y = 0;
  2136. s->mb_intra = 1;
  2137. ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
  2138. for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  2139. for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  2140. ff_init_block_index(s);
  2141. ff_update_block_index(s);
  2142. s->dsp.clear_blocks(s->block[0]);
  2143. mb_pos = s->mb_x + s->mb_y * s->mb_width;
  2144. s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
  2145. s->current_picture.qscale_table[mb_pos] = v->pq;
  2146. // do actual MB decoding and displaying
  2147. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  2148. v->s.ac_pred = get_bits(&v->s.gb, 1);
  2149. for(k = 0; k < 6; k++) {
  2150. val = ((cbp >> (5 - k)) & 1);
  2151. if (k < 4) {
  2152. int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
  2153. val = val ^ pred;
  2154. *coded_val = val;
  2155. }
  2156. cbp |= val << (5 - k);
  2157. vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
  2158. vc1_inv_trans(s->block[k], 8, 8);
  2159. if(v->pq >= 9 && v->overlap) {
  2160. vc1_overlap_block(s, s->block[k], k, (s->mb_y || k>1), (s->mb_x || (k != 0 && k != 2)));
  2161. }
  2162. }
  2163. vc1_put_block(v, s->block);
  2164. if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
  2165. if(s->mb_y) {
  2166. s->dsp.h263_v_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
  2167. s->dsp.h263_v_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
  2168. s->dsp.h263_v_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
  2169. s->dsp.h263_v_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
  2170. }
  2171. s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
  2172. s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
  2173. if(s->mb_x) {
  2174. s->dsp.h263_h_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
  2175. s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
  2176. s->dsp.h263_h_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
  2177. s->dsp.h263_h_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
  2178. }
  2179. s->dsp.h263_h_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
  2180. s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
  2181. }
  2182. if(get_bits_count(&s->gb) > v->bits) {
  2183. av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
  2184. return;
  2185. }
  2186. }
  2187. ff_draw_horiz_band(s, s->mb_y * 16, 16);
  2188. }
  2189. }
  2190. static void vc1_decode_p_blocks(VC1Context *v)
  2191. {
  2192. MpegEncContext *s = &v->s;
  2193. /* select codingmode used for VLC tables selection */
  2194. switch(v->c_ac_table_index){
  2195. case 0:
  2196. v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
  2197. break;
  2198. case 1:
  2199. v->codingset = CS_HIGH_MOT_INTRA;
  2200. break;
  2201. case 2:
  2202. v->codingset = CS_MID_RATE_INTRA;
  2203. break;
  2204. }
  2205. switch(v->c_ac_table_index){
  2206. case 0:
  2207. v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
  2208. break;
  2209. case 1:
  2210. v->codingset2 = CS_HIGH_MOT_INTER;
  2211. break;
  2212. case 2:
  2213. v->codingset2 = CS_MID_RATE_INTER;
  2214. break;
  2215. }
  2216. ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
  2217. s->first_slice_line = 1;
  2218. for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  2219. for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  2220. ff_init_block_index(s);
  2221. ff_update_block_index(s);
  2222. s->dsp.clear_blocks(s->block[0]);
  2223. vc1_decode_p_mb(v, s->block);
  2224. if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
  2225. 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);
  2226. return;
  2227. }
  2228. }
  2229. ff_draw_horiz_band(s, s->mb_y * 16, 16);
  2230. s->first_slice_line = 0;
  2231. }
  2232. }
  2233. static void vc1_decode_blocks(VC1Context *v)
  2234. {
  2235. v->s.esc3_level_length = 0;
  2236. switch(v->s.pict_type) {
  2237. case I_TYPE:
  2238. vc1_decode_i_blocks(v);
  2239. break;
  2240. case P_TYPE:
  2241. vc1_decode_p_blocks(v);
  2242. break;
  2243. }
  2244. }
  2245. /** Initialize a VC1/WMV3 decoder
  2246. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  2247. * @todo TODO: Decypher remaining bits in extra_data
  2248. */
  2249. static int vc1_decode_init(AVCodecContext *avctx)
  2250. {
  2251. VC1Context *v = avctx->priv_data;
  2252. MpegEncContext *s = &v->s;
  2253. GetBitContext gb;
  2254. if (!avctx->extradata_size || !avctx->extradata) return -1;
  2255. avctx->pix_fmt = PIX_FMT_YUV420P;
  2256. v->s.avctx = avctx;
  2257. if(ff_h263_decode_init(avctx) < 0)
  2258. return -1;
  2259. if (vc1_init_common(v) < 0) return -1;
  2260. av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
  2261. av_log(avctx, AV_LOG_INFO, "If you see a picture, don't believe your eyes.\n");
  2262. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  2263. avctx->coded_width = avctx->width;
  2264. avctx->coded_height = avctx->height;
  2265. if (avctx->codec_id == CODEC_ID_WMV3)
  2266. {
  2267. int count = 0;
  2268. // looks like WMV3 has a sequence header stored in the extradata
  2269. // advanced sequence header may be before the first frame
  2270. // the last byte of the extradata is a version number, 1 for the
  2271. // samples we can decode
  2272. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  2273. if (decode_sequence_header(avctx, &gb) < 0)
  2274. return -1;
  2275. count = avctx->extradata_size*8 - get_bits_count(&gb);
  2276. if (count>0)
  2277. {
  2278. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  2279. count, get_bits(&gb, count));
  2280. }
  2281. else if (count < 0)
  2282. {
  2283. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  2284. }
  2285. }
  2286. avctx->has_b_frames= !!(avctx->max_b_frames);
  2287. s->mb_width = (avctx->coded_width+15)>>4;
  2288. s->mb_height = (avctx->coded_height+15)>>4;
  2289. /* Allocate mb bitplanes */
  2290. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2291. return -1;
  2292. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2293. return -1;
  2294. if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
  2295. return -1;
  2296. if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
  2297. return -1;
  2298. /* For predictors */
  2299. v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
  2300. if (!v->previous_line_cbpcy) return -1;
  2301. /* Init coded blocks info */
  2302. if (v->profile == PROFILE_ADVANCED)
  2303. {
  2304. if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  2305. return -1;
  2306. if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  2307. return -1;
  2308. }
  2309. return 0;
  2310. }
  2311. /** Decode a VC1/WMV3 frame
  2312. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  2313. * @warning Initial try at using MpegEncContext stuff
  2314. */
  2315. static int vc1_decode_frame(AVCodecContext *avctx,
  2316. void *data, int *data_size,
  2317. uint8_t *buf, int buf_size)
  2318. {
  2319. VC1Context *v = avctx->priv_data;
  2320. MpegEncContext *s = &v->s;
  2321. AVFrame *pict = data;
  2322. /* no supplementary picture */
  2323. if (buf_size == 0) {
  2324. /* special case for last picture */
  2325. if (s->low_delay==0 && s->next_picture_ptr) {
  2326. *pict= *(AVFrame*)s->next_picture_ptr;
  2327. s->next_picture_ptr= NULL;
  2328. *data_size = sizeof(AVFrame);
  2329. }
  2330. return 0;
  2331. }
  2332. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  2333. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  2334. int i= ff_find_unused_picture(s, 0);
  2335. s->current_picture_ptr= &s->picture[i];
  2336. }
  2337. avctx->has_b_frames= !s->low_delay;
  2338. init_get_bits(&s->gb, buf, buf_size*8);
  2339. // do parse frame header
  2340. if(vc1_parse_frame_header(v, &s->gb) == -1)
  2341. return -1;
  2342. if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1;
  2343. // for hurry_up==5
  2344. s->current_picture.pict_type= s->pict_type;
  2345. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2346. /* skip B-frames if we don't have reference frames */
  2347. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
  2348. /* skip b frames if we are in a hurry */
  2349. if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
  2350. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  2351. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  2352. || avctx->skip_frame >= AVDISCARD_ALL)
  2353. return buf_size;
  2354. /* skip everything if we are in a hurry>=5 */
  2355. if(avctx->hurry_up>=5) return -1;//buf_size;
  2356. if(s->next_p_frame_damaged){
  2357. if(s->pict_type==B_TYPE)
  2358. return buf_size;
  2359. else
  2360. s->next_p_frame_damaged=0;
  2361. }
  2362. if(MPV_frame_start(s, avctx) < 0)
  2363. return -1;
  2364. ff_er_frame_start(s);
  2365. v->bits = buf_size * 8;
  2366. vc1_decode_blocks(v);
  2367. //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
  2368. // if(get_bits_count(&s->gb) > buf_size * 8)
  2369. // return -1;
  2370. ff_er_frame_end(s);
  2371. MPV_frame_end(s);
  2372. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  2373. assert(s->current_picture.pict_type == s->pict_type);
  2374. if (s->pict_type == B_TYPE || s->low_delay) {
  2375. *pict= *(AVFrame*)s->current_picture_ptr;
  2376. } else if (s->last_picture_ptr != NULL) {
  2377. *pict= *(AVFrame*)s->last_picture_ptr;
  2378. }
  2379. if(s->last_picture_ptr || s->low_delay){
  2380. *data_size = sizeof(AVFrame);
  2381. ff_print_debug_info(s, pict);
  2382. }
  2383. /* Return the Picture timestamp as the frame number */
  2384. /* we substract 1 because it is added on utils.c */
  2385. avctx->frame_number = s->picture_number - 1;
  2386. return buf_size;
  2387. }
  2388. /** Close a VC1/WMV3 decoder
  2389. * @warning Initial try at using MpegEncContext stuff
  2390. */
  2391. static int vc1_decode_end(AVCodecContext *avctx)
  2392. {
  2393. VC1Context *v = avctx->priv_data;
  2394. av_freep(&v->hrd_rate);
  2395. av_freep(&v->hrd_buffer);
  2396. MPV_common_end(&v->s);
  2397. free_bitplane(&v->mv_type_mb_plane);
  2398. free_bitplane(&v->skip_mb_plane);
  2399. free_bitplane(&v->direct_mb_plane);
  2400. return 0;
  2401. }
  2402. AVCodec vc1_decoder = {
  2403. "vc1",
  2404. CODEC_TYPE_VIDEO,
  2405. CODEC_ID_VC1,
  2406. sizeof(VC1Context),
  2407. vc1_decode_init,
  2408. NULL,
  2409. vc1_decode_end,
  2410. vc1_decode_frame,
  2411. CODEC_CAP_DELAY,
  2412. NULL
  2413. };
  2414. AVCodec wmv3_decoder = {
  2415. "wmv3",
  2416. CODEC_TYPE_VIDEO,
  2417. CODEC_ID_WMV3,
  2418. sizeof(VC1Context),
  2419. vc1_decode_init,
  2420. NULL,
  2421. vc1_decode_end,
  2422. vc1_decode_frame,
  2423. CODEC_CAP_DELAY,
  2424. NULL
  2425. };