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.

2870 lines
89KB

  1. /*
  2. * VC-9 and WMV3 decoder
  3. * Copyright (c) 2005 Anonymous
  4. * Copyright (c) 2005 Alex Beregszaszi
  5. * Copyright (c) 2005 Michael Niedermayer
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /**
  23. * @file vc9.c
  24. * VC-9 and WMV3 decoder
  25. *
  26. * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
  27. * TODO: use MPV_ !!
  28. */
  29. #include "common.h"
  30. #include "dsputil.h"
  31. #include "avcodec.h"
  32. #include "mpegvideo.h"
  33. #include "vc9data.h"
  34. #undef NDEBUG
  35. #include <assert.h>
  36. extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
  37. extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
  38. extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
  39. #define MB_INTRA_VLC_BITS 9
  40. extern VLC ff_msmp4_mb_i_vlc;
  41. #define DC_VLC_BITS 9
  42. static const uint16_t table_mb_intra[64][2];
  43. /* Some inhibiting stuff */
  44. #define HAS_ADVANCED_PROFILE 1
  45. #define TRACE 1
  46. #if TRACE
  47. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  48. codes, codes_wrap, codes_size, use_static) \
  49. if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  50. codes, codes_wrap, codes_size, use_static) < 0) \
  51. { \
  52. av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \
  53. return -1; \
  54. }
  55. #else
  56. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  57. codes, codes_wrap, codes_size, use_static) \
  58. init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  59. codes, codes_wrap, codes_size, use_static)
  60. #endif
  61. /** Available Profiles */
  62. //@{
  63. #define PROFILE_SIMPLE 0
  64. #define PROFILE_MAIN 1
  65. #define PROFILE_COMPLEX 2 ///< TODO: WMV9 specific
  66. #define PROFILE_ADVANCED 3
  67. //@}
  68. /** Sequence quantizer mode */
  69. //@{
  70. #define QUANT_FRAME_IMPLICIT 0 ///< Implicitly specified at frame level
  71. #define QUANT_FRAME_EXPLICIT 1 ///< Explicitly specified at frame level
  72. #define QUANT_NON_UNIFORM 2 ///< Non-uniform quant used for all frames
  73. #define QUANT_UNIFORM 3 ///< Uniform quant used for all frames
  74. //@}
  75. /** Where quant can be changed */
  76. //@{
  77. #define DQPROFILE_FOUR_EDGES 0
  78. #define DQPROFILE_DOUBLE_EDGES 1
  79. #define DQPROFILE_SINGLE_EDGE 2
  80. #define DQPROFILE_ALL_MBS 3
  81. //@}
  82. /** @name Where quant can be changed
  83. */
  84. //@{
  85. #define DQPROFILE_FOUR_EDGES 0
  86. #define DQSINGLE_BEDGE_LEFT 0
  87. #define DQSINGLE_BEDGE_TOP 1
  88. #define DQSINGLE_BEDGE_RIGHT 2
  89. #define DQSINGLE_BEDGE_BOTTOM 3
  90. //@}
  91. /** Which pair of edges is quantized with ALTPQUANT */
  92. //@{
  93. #define DQDOUBLE_BEDGE_TOPLEFT 0
  94. #define DQDOUBLE_BEDGE_TOPRIGHT 1
  95. #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
  96. #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
  97. //@}
  98. /** MV modes for P frames */
  99. //@{
  100. #define MV_PMODE_1MV_HPEL_BILIN 0
  101. #define MV_PMODE_1MV 1
  102. #define MV_PMODE_1MV_HPEL 2
  103. #define MV_PMODE_MIXED_MV 3
  104. #define MV_PMODE_INTENSITY_COMP 4
  105. //@}
  106. /** @name MV types for B frames */
  107. //@{
  108. #define BMV_TYPE_BACKWARD 0
  109. #define BMV_TYPE_BACKWARD 0
  110. #define BMV_TYPE_FORWARD 1
  111. #define BMV_TYPE_INTERPOLATED 3
  112. //@}
  113. /** MV P mode - the 5th element is only used for mode 1 */
  114. static const uint8_t mv_pmode_table[2][5] = {
  115. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
  116. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
  117. };
  118. /** One more frame type */
  119. #define BI_TYPE 7
  120. static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
  121. fps_dr[2] = { 1000, 1001 };
  122. static const uint8_t pquant_table[3][32] = {
  123. { /* Implicit quantizer */
  124. 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
  125. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
  126. },
  127. { /* Explicit quantizer, pquantizer uniform */
  128. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  129. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  130. },
  131. { /* Explicit quantizer, pquantizer non-uniform */
  132. 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  133. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
  134. }
  135. };
  136. /** @name VC-9 VLC tables and defines
  137. * @todo TODO move this into the context
  138. */
  139. //@{
  140. #define VC9_BFRACTION_VLC_BITS 7
  141. static VLC vc9_bfraction_vlc;
  142. #define VC9_IMODE_VLC_BITS 4
  143. static VLC vc9_imode_vlc;
  144. #define VC9_NORM2_VLC_BITS 3
  145. static VLC vc9_norm2_vlc;
  146. #define VC9_NORM6_VLC_BITS 9
  147. static VLC vc9_norm6_vlc;
  148. /* Could be optimized, one table only needs 8 bits */
  149. #define VC9_TTMB_VLC_BITS 9 //12
  150. static VLC vc9_ttmb_vlc[3];
  151. #define VC9_MV_DIFF_VLC_BITS 9 //15
  152. static VLC vc9_mv_diff_vlc[4];
  153. #define VC9_CBPCY_P_VLC_BITS 9 //14
  154. static VLC vc9_cbpcy_p_vlc[4];
  155. #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
  156. static VLC vc9_4mv_block_pattern_vlc[4];
  157. #define VC9_TTBLK_VLC_BITS 5
  158. static VLC vc9_ttblk_vlc[3];
  159. #define VC9_SUBBLKPAT_VLC_BITS 6
  160. static VLC vc9_subblkpat_vlc[3];
  161. //@}
  162. /** Bitplane struct
  163. * We mainly need data and is_raw, so this struct could be avoided
  164. * to save a level of indirection; feel free to modify
  165. * @fixme For now, stride=width
  166. * @warning Data are bits, either 1 or 0
  167. */
  168. typedef struct BitPlane {
  169. uint8_t *data; ///< Data buffer
  170. int width; ///< Width of the buffer
  171. int stride; ///< Stride of the buffer
  172. int height; ///< Plane height
  173. uint8_t is_raw; ///< Bit values must be read at MB level
  174. } BitPlane;
  175. /** The VC9 Context */
  176. typedef struct VC9Context{
  177. MpegEncContext s;
  178. /** Simple/Main Profile sequence header */
  179. //@{
  180. int res_sm; ///< reserved, 2b
  181. int res_x8; ///< reserved
  182. int multires; ///< frame-level RESPIC syntax element present
  183. int res_fasttx; ///< reserved, always 1
  184. int res_transtab; ///< reserved, always 0
  185. int rangered; ///< RANGEREDFRM (range reduction) syntax element present
  186. ///< at frame level
  187. int res_rtm_flag; ///< reserved, set to 1
  188. int reserved; ///< reserved
  189. //@}
  190. #if HAS_ADVANCED_PROFILE
  191. /** Advanced Profile */
  192. //@{
  193. int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
  194. int chromaformat; ///< 2bits, 2=4:2:0, only defined
  195. int postprocflag; ///< Per-frame processing suggestion flag present
  196. int broadcast; ///< TFF/RFF present
  197. int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
  198. int tfcntrflag; ///< TFCNTR present
  199. int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
  200. int extended_dmv; ///< Additional extended dmv range at P/B frame-level
  201. int color_prim; ///< 8bits, chroma coordinates of the color primaries
  202. int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
  203. int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
  204. int hrd_param_flag; ///< Presence of Hypothetical Reference
  205. ///< Decoder parameters
  206. //@}
  207. #endif
  208. /** Sequence header data for all Profiles
  209. * TODO: choose between ints, uint8_ts and monobit flags
  210. */
  211. //@{
  212. int profile; ///< 2bits, Profile
  213. int frmrtq_postproc; ///< 3bits,
  214. int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
  215. int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
  216. int extended_mv; ///< Ext MV in P/B (not in Simple)
  217. int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
  218. int vstransform; ///< variable-size [48]x[48] transform type + info
  219. int overlap; ///< overlapped transforms in use
  220. int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
  221. int finterpflag; ///< INTERPFRM present
  222. //@}
  223. /** Frame decoding info for all profiles */
  224. //@{
  225. uint8_t mv_mode; ///< MV coding monde
  226. uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
  227. int k_x; ///< Number of bits for MVs (depends on MV range)
  228. int k_y; ///< Number of bits for MVs (depends on MV range)
  229. uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
  230. /** pquant parameters */
  231. //@{
  232. uint8_t dquantfrm;
  233. uint8_t dqprofile;
  234. uint8_t dqsbedge;
  235. uint8_t dqbilevel;
  236. //@}
  237. int ac_table_level; ///< Index for AC tables from ACFRM element
  238. int ttfrm; ///< Transform type info present at frame level
  239. uint8_t ttmbf; ///< Transform type flag
  240. int ttmb; ///< Transform type
  241. uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
  242. /** Luma compensation parameters */
  243. //@{
  244. uint8_t lumscale;
  245. uint8_t lumshift;
  246. //@}
  247. int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
  248. uint8_t halfpq; ///< Uniform quant over image and qp+.5
  249. uint8_t respic; ///< Frame-level flag for resized images
  250. int buffer_fullness; ///< HRD info
  251. /** Ranges:
  252. * -# 0 -> [-64n 63.f] x [-32, 31.f]
  253. * -# 1 -> [-128, 127.f] x [-64, 63.f]
  254. * -# 2 -> [-512, 511.f] x [-128, 127.f]
  255. * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
  256. */
  257. uint8_t mvrange;
  258. uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
  259. uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
  260. VLC *cbpcy_vlc; ///< CBPCY VLC table
  261. int tt_index; ///< Index for Transform Type tables
  262. BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
  263. BitPlane skip_mb_plane; ///< bitplane for skipped MBs
  264. BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
  265. /** Frame decoding info for S/M profiles only */
  266. //@{
  267. uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
  268. uint8_t interpfrm;
  269. //@}
  270. #if HAS_ADVANCED_PROFILE
  271. /** Frame decoding info for Advanced profile */
  272. //@{
  273. uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  274. uint8_t numpanscanwin;
  275. uint8_t tfcntr;
  276. uint8_t rptfrm, tff, rff;
  277. uint16_t topleftx;
  278. uint16_t toplefty;
  279. uint16_t bottomrightx;
  280. uint16_t bottomrighty;
  281. uint8_t uvsamp;
  282. uint8_t postproc;
  283. int hrd_num_leaky_buckets;
  284. uint8_t bit_rate_exponent;
  285. uint8_t buffer_size_exponent;
  286. BitPlane ac_pred_plane; ///< AC prediction flags bitplane
  287. BitPlane over_flags_plane; ///< Overflags bitplane
  288. uint8_t condover;
  289. uint16_t *hrd_rate, *hrd_buffer;
  290. int ac2_table_level; ///< Index for AC2 tables from AC2FRM element
  291. //@}
  292. #endif
  293. } VC9Context;
  294. /**
  295. * Get unary code of limited length
  296. * @fixme FIXME Slow and ugly
  297. * @param gb GetBitContext
  298. * @param[in] stop The bitstop value (unary code of 1's or 0's)
  299. * @param[in] len Maximum length
  300. * @return Unary length/index
  301. */
  302. static int get_prefix(GetBitContext *gb, int stop, int len)
  303. {
  304. #if 1
  305. int i = 0, tmp = !stop;
  306. while (i != len && tmp != stop)
  307. {
  308. tmp = get_bits(gb, 1);
  309. i++;
  310. }
  311. if (i == len && tmp != stop) return len+1;
  312. return i;
  313. #else
  314. unsigned int buf;
  315. int log;
  316. OPEN_READER(re, gb);
  317. UPDATE_CACHE(re, gb);
  318. buf=GET_CACHE(re, gb); //Still not sure
  319. if (stop) buf = ~buf;
  320. log= av_log2(-buf); //FIXME: -?
  321. if (log < limit){
  322. LAST_SKIP_BITS(re, gb, log+1);
  323. CLOSE_READER(re, gb);
  324. return log;
  325. }
  326. LAST_SKIP_BITS(re, gb, limit);
  327. CLOSE_READER(re, gb);
  328. return limit;
  329. #endif
  330. }
  331. /**
  332. * Init VC-9 specific tables and VC9Context members
  333. * @param v The VC9Context to initialize
  334. * @return Status
  335. */
  336. static int vc9_init_common(VC9Context *v)
  337. {
  338. static int done = 0;
  339. int i = 0;
  340. /* Set the bit planes */
  341. v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  342. v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  343. v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  344. #if HAS_ADVANCED_PROFILE
  345. v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  346. v->hrd_rate = v->hrd_buffer = NULL;
  347. #endif
  348. /* VLC tables */
  349. #if 0 // spec -> actual tables converter
  350. for(i=0; i<64; i++){
  351. int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
  352. av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
  353. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  354. }
  355. for(i=0; i<64; i++){
  356. int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
  357. av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
  358. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  359. }
  360. #endif
  361. if(!done)
  362. {
  363. done = 1;
  364. INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
  365. vc9_bfraction_bits, 1, 1,
  366. vc9_bfraction_codes, 1, 1, 1);
  367. INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
  368. vc9_norm2_bits, 1, 1,
  369. vc9_norm2_codes, 1, 1, 1);
  370. INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
  371. vc9_norm6_bits, 1, 1,
  372. vc9_norm6_codes, 2, 2, 1);
  373. INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
  374. vc9_imode_bits, 1, 1,
  375. vc9_imode_codes, 1, 1, 1);
  376. for (i=0; i<3; i++)
  377. {
  378. INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
  379. vc9_ttmb_bits[i], 1, 1,
  380. vc9_ttmb_codes[i], 2, 2, 1);
  381. INIT_VLC(&vc9_ttblk_vlc[i], VC9_TTBLK_VLC_BITS, 8,
  382. vc9_ttblk_bits[i], 1, 1,
  383. vc9_ttblk_codes[i], 1, 1, 1);
  384. INIT_VLC(&vc9_subblkpat_vlc[i], VC9_SUBBLKPAT_VLC_BITS, 15,
  385. vc9_subblkpat_bits[i], 1, 1,
  386. vc9_subblkpat_codes[i], 1, 1, 1);
  387. }
  388. for(i=0; i<4; i++)
  389. {
  390. INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
  391. vc9_4mv_block_pattern_bits[i], 1, 1,
  392. vc9_4mv_block_pattern_codes[i], 1, 1, 1);
  393. INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
  394. vc9_cbpcy_p_bits[i], 1, 1,
  395. vc9_cbpcy_p_codes[i], 2, 2, 1);
  396. INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
  397. vc9_mv_diff_bits[i], 1, 1,
  398. vc9_mv_diff_codes[i], 2, 2, 1);
  399. }
  400. }
  401. /* Other defaults */
  402. v->pq = -1;
  403. v->mvrange = 0; /* 7.1.1.18, p80 */
  404. return 0;
  405. }
  406. #if HAS_ADVANCED_PROFILE
  407. /**
  408. * Decode sequence header's Hypothetic Reference Decoder data
  409. * @see 6.2.1, p32
  410. * @param v The VC9Context to initialize
  411. * @param gb A GetBitContext initialized from AVCodecContext extra_data
  412. * @return Status
  413. */
  414. static int decode_hrd(VC9Context *v, GetBitContext *gb)
  415. {
  416. int i, num;
  417. num = get_bits(gb, 5);
  418. if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
  419. {
  420. av_freep(&v->hrd_rate);
  421. }
  422. if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
  423. if (!v->hrd_rate) return -1;
  424. if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
  425. {
  426. av_freep(&v->hrd_buffer);
  427. }
  428. if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
  429. if (!v->hrd_buffer) return -1;
  430. v->hrd_num_leaky_buckets = num;
  431. //exponent in base-2 for rate
  432. v->bit_rate_exponent = get_bits(gb, 4);
  433. //exponent in base-2 for buffer_size
  434. v->buffer_size_exponent = get_bits(gb, 4);
  435. for (i=0; i<num; i++)
  436. {
  437. //mantissae, ordered (if not, use a function ?
  438. v->hrd_rate[i] = get_bits(gb, 16);
  439. if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
  440. {
  441. av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
  442. "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
  443. return -1;
  444. }
  445. v->hrd_buffer[i] = get_bits(gb, 16);
  446. if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
  447. {
  448. av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
  449. "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
  450. return -1;
  451. }
  452. }
  453. return 0;
  454. }
  455. /**
  456. * Decode sequence header for Advanced Profile
  457. * @see Table 2, p18
  458. * @see 6.1.7, pp21-27
  459. * @param v The VC9Context to initialize
  460. * @param gb A GetBitContext initialized from AVCodecContext extra_data
  461. * @return Status
  462. */
  463. static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  464. {
  465. VC9Context *v = avctx->priv_data;
  466. int nr, dr, aspect_ratio;
  467. v->postprocflag = get_bits(gb, 1);
  468. v->broadcast = get_bits(gb, 1);
  469. v->interlace = get_bits(gb, 1);
  470. v->tfcntrflag = get_bits(gb, 1);
  471. v->finterpflag = get_bits(gb, 1); //common
  472. v->panscanflag = get_bits(gb, 1);
  473. v->reserved = get_bits(gb, 1);
  474. if (v->reserved)
  475. {
  476. av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
  477. v->reserved);
  478. return -1;
  479. }
  480. if (v->extended_mv)
  481. v->extended_dmv = get_bits(gb, 1);
  482. /* 6.1.7, p21 */
  483. if (get_bits(gb, 1) /* pic_size_flag */)
  484. {
  485. avctx->coded_width = get_bits(gb, 12);
  486. avctx->coded_height = get_bits(gb, 12);
  487. if ( get_bits(gb, 1) /* disp_size_flag */)
  488. {
  489. avctx->width = get_bits(gb, 14);
  490. avctx->height = get_bits(gb, 14);
  491. }
  492. /* 6.1.7.4, p22 */
  493. if ( get_bits(gb, 1) /* aspect_ratio_flag */)
  494. {
  495. aspect_ratio = get_bits(gb, 4); //SAR
  496. if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
  497. {
  498. avctx->sample_aspect_ratio.num = get_bits(gb, 8);
  499. avctx->sample_aspect_ratio.den = get_bits(gb, 8);
  500. }
  501. else if (aspect_ratio == 0x0E)
  502. {
  503. av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
  504. }
  505. else
  506. {
  507. avctx->sample_aspect_ratio = vc9_pixel_aspect[aspect_ratio];
  508. }
  509. }
  510. }
  511. else
  512. {
  513. avctx->coded_width = avctx->width;
  514. avctx->coded_height = avctx->height;
  515. }
  516. /* 6.1.8, p23 */
  517. if ( !get_bits(gb, 1) /* framerateflag */)
  518. {
  519. if ( get_bits(gb, 1) /* framerateind */)
  520. {
  521. nr = get_bits(gb, 8);
  522. dr = get_bits(gb, 4);
  523. if (nr<1)
  524. {
  525. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
  526. return -1;
  527. }
  528. if (nr>5)
  529. {
  530. av_log(avctx, AV_LOG_ERROR,
  531. "Reserved FRAMERATENR %i not handled\n", nr);
  532. }
  533. if (dr<1)
  534. {
  535. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
  536. }
  537. if (dr>2)
  538. {
  539. av_log(avctx, AV_LOG_ERROR,
  540. "Reserved FRAMERATEDR %i not handled\n", dr);
  541. }
  542. avctx->frame_rate_base = fps_nr[dr];
  543. avctx->frame_rate = fps_nr[nr];
  544. }
  545. else
  546. {
  547. nr = get_bits(gb, 16);
  548. // 0.03125->2048Hz / 0.03125Hz
  549. avctx->frame_rate = 1000000;
  550. avctx->frame_rate_base = 31250*(1+nr);
  551. }
  552. }
  553. /* 6.1.9, p25 */
  554. if ( get_bits(gb, 1) /* color_format_flag */)
  555. {
  556. //Chromacity coordinates of color primaries
  557. //like ITU-R BT.709-2, BT.470-2, ...
  558. v->color_prim = get_bits(gb, 8);
  559. if (v->color_prim<1)
  560. {
  561. av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is reserved\n");
  562. return -1;
  563. }
  564. if (v->color_prim == 3 || v->color_prim>6)
  565. {
  566. av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
  567. v->color_prim);
  568. return -1;
  569. }
  570. //Opto-electronic transfer characteristics
  571. v->transfer_char = get_bits(gb, 8);
  572. if (v->transfer_char == 3 || v->transfer_char>8)
  573. {
  574. av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
  575. v->color_prim);
  576. return -1;
  577. }
  578. //Matrix coefficient for primariev->YCbCr
  579. v->matrix_coef = get_bits(gb, 8);
  580. if (v->matrix_coef < 1) return -1; //forbidden
  581. if ((v->matrix_coef>3 && v->matrix_coef<6) || v->matrix_coef>7)
  582. {
  583. av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
  584. v->color_prim);
  585. return -1;
  586. }
  587. }
  588. //Hypothetical reference decoder indicator flag
  589. v->hrd_param_flag = get_bits(gb, 1);
  590. if (v->hrd_param_flag)
  591. {
  592. if (decode_hrd(v, gb) < 0) return -1;
  593. }
  594. av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
  595. return -1;
  596. }
  597. #endif
  598. /**
  599. * Decode Simple/Main Profiles sequence header
  600. * @see Figure 7-8, p16-17
  601. * @param avctx Codec context
  602. * @param gb GetBit context initialized from Codec context extra_data
  603. * @return Status
  604. */
  605. static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  606. {
  607. VC9Context *v = avctx->priv_data;
  608. av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
  609. v->profile = get_bits(gb, 2);
  610. if (v->profile == 2)
  611. av_log(avctx, AV_LOG_ERROR, "Profile 2 is reserved\n");
  612. #if HAS_ADVANCED_PROFILE
  613. if (v->profile == PROFILE_ADVANCED)
  614. {
  615. v->level = get_bits(gb, 3);
  616. v->chromaformat = get_bits(gb, 2);
  617. if (v->chromaformat != 1)
  618. {
  619. av_log(avctx, AV_LOG_ERROR,
  620. "Only 4:2:0 chroma format supported\n");
  621. return -1;
  622. }
  623. }
  624. else
  625. #endif
  626. {
  627. v->res_sm = get_bits(gb, 2); //reserved
  628. if (v->res_sm)
  629. {
  630. av_log(avctx, AV_LOG_ERROR,
  631. "Reserved RES_SM=%i is forbidden\n", v->res_sm);
  632. return -1;
  633. }
  634. }
  635. // (fps-2)/4 (->30)
  636. v->frmrtq_postproc = get_bits(gb, 3); //common
  637. // (bitrate-32kbps)/64kbps
  638. v->bitrtq_postproc = get_bits(gb, 5); //common
  639. v->s.loop_filter = get_bits(gb, 1); //common
  640. #if HAS_ADVANCED_PROFILE
  641. if (v->profile < PROFILE_ADVANCED)
  642. #endif
  643. {
  644. v->res_x8 = get_bits(gb, 1); //reserved
  645. if (v->res_x8)
  646. {
  647. av_log(avctx, AV_LOG_ERROR,
  648. "1 for reserved RES_X8 is forbidden\n");
  649. //return -1;
  650. }
  651. v->multires = get_bits(gb, 1);
  652. v->res_fasttx = get_bits(gb, 1);
  653. if (!v->res_fasttx)
  654. {
  655. av_log(avctx, AV_LOG_ERROR,
  656. "0 for reserved RES_FASTTX is forbidden\n");
  657. //return -1;
  658. }
  659. }
  660. v->fastuvmc = get_bits(gb, 1); //common
  661. if (!v->profile && !v->fastuvmc)
  662. {
  663. av_log(avctx, AV_LOG_ERROR,
  664. "FASTUVMC unavailable in Simple Profile\n");
  665. return -1;
  666. }
  667. v->extended_mv = get_bits(gb, 1); //common
  668. if (!v->profile && v->extended_mv)
  669. {
  670. av_log(avctx, AV_LOG_ERROR,
  671. "Extended MVs unavailable in Simple Profile\n");
  672. return -1;
  673. }
  674. v->dquant = get_bits(gb, 2); //common
  675. v->vstransform = get_bits(gb, 1); //common
  676. #if HAS_ADVANCED_PROFILE
  677. if (v->profile < PROFILE_ADVANCED)
  678. #endif
  679. {
  680. v->res_transtab = get_bits(gb, 1);
  681. if (v->res_transtab)
  682. {
  683. av_log(avctx, AV_LOG_ERROR,
  684. "1 for reserved RES_TRANSTAB is forbidden\n");
  685. return -1;
  686. }
  687. }
  688. v->overlap = get_bits(gb, 1); //common
  689. #if HAS_ADVANCED_PROFILE
  690. if (v->profile < PROFILE_ADVANCED)
  691. #endif
  692. {
  693. v->s.resync_marker = get_bits(gb, 1);
  694. v->rangered = get_bits(gb, 1);
  695. }
  696. v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
  697. v->quantizer_mode = get_bits(gb, 2); //common
  698. #if HAS_ADVANCED_PROFILE
  699. if (v->profile < PROFILE_ADVANCED)
  700. #endif
  701. {
  702. v->finterpflag = get_bits(gb, 1); //common
  703. v->res_rtm_flag = get_bits(gb, 1); //reserved
  704. if (!v->res_rtm_flag)
  705. {
  706. av_log(avctx, AV_LOG_ERROR,
  707. "0 for reserved RES_RTM_FLAG is forbidden\n");
  708. //return -1;
  709. }
  710. #if TRACE
  711. av_log(avctx, AV_LOG_INFO,
  712. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  713. "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
  714. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  715. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  716. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  717. v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
  718. v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
  719. v->dquant, v->quantizer_mode, avctx->max_b_frames
  720. );
  721. return 0;
  722. #endif
  723. }
  724. #if HAS_ADVANCED_PROFILE
  725. else return decode_advanced_sequence_header(avctx, gb);
  726. #endif
  727. }
  728. #if HAS_ADVANCED_PROFILE
  729. /** Entry point decoding (Advanced Profile)
  730. * @param avctx Codec context
  731. * @param gb GetBit context initialized from avctx->extra_data
  732. * @return Status
  733. */
  734. static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
  735. {
  736. VC9Context *v = avctx->priv_data;
  737. int range_mapy_flag, range_mapuv_flag, i;
  738. if (v->profile != PROFILE_ADVANCED)
  739. {
  740. av_log(avctx, AV_LOG_ERROR,
  741. "Entry point are only defined in Advanced Profile!\n");
  742. return -1; //Only for advanced profile!
  743. }
  744. if (v->hrd_param_flag)
  745. {
  746. //Update buffer fullness
  747. av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
  748. for (i=0; i<v->hrd_num_leaky_buckets; i++)
  749. skip_bits(gb, 8);
  750. }
  751. if ((range_mapy_flag = get_bits(gb, 1)))
  752. {
  753. //RANGE_MAPY
  754. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
  755. skip_bits(gb, 3);
  756. }
  757. if ((range_mapuv_flag = get_bits(gb, 1)))
  758. {
  759. //RANGE_MAPUV
  760. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
  761. skip_bits(gb, 3);
  762. }
  763. if (v->panscanflag)
  764. {
  765. //NUMPANSCANWIN
  766. v->numpanscanwin = get_bits(gb, 3);
  767. av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
  768. }
  769. return 0;
  770. }
  771. #endif
  772. /***********************************************************************/
  773. /**
  774. * @defgroup bitplane VC9 Bitplane decoding
  775. * @see 8.7, p56
  776. * @{
  777. */
  778. /** @addtogroup bitplane
  779. * Imode types
  780. * @{
  781. */
  782. #define IMODE_RAW 0
  783. #define IMODE_NORM2 1
  784. #define IMODE_DIFF2 2
  785. #define IMODE_NORM6 3
  786. #define IMODE_DIFF6 4
  787. #define IMODE_ROWSKIP 5
  788. #define IMODE_COLSKIP 6
  789. /** @} */ //imode defines
  790. /** Allocate the buffer from a bitplane, given its dimensions
  791. * @param bp Bitplane which buffer is to allocate
  792. * @param[in] width Width of the buffer
  793. * @param[in] height Height of the buffer
  794. * @return Status
  795. * @todo TODO: Take into account stride
  796. * @todo TODO: Allow use of external buffers ?
  797. */
  798. int alloc_bitplane(BitPlane *bp, int width, int height)
  799. {
  800. if (!bp || bp->width<0 || bp->height<0) return -1;
  801. bp->data = (uint8_t*)av_malloc(width*height);
  802. if (!bp->data) return -1;
  803. bp->width = bp->stride = width;
  804. bp->height = height;
  805. return 0;
  806. }
  807. /** Free the bitplane's buffer
  808. * @param bp Bitplane which buffer is to free
  809. */
  810. void free_bitplane(BitPlane *bp)
  811. {
  812. bp->width = bp->stride = bp->height = 0;
  813. if (bp->data) av_freep(&bp->data);
  814. }
  815. /** Decode rows by checking if they are skiped
  816. * @param plane Buffer to store decoded bits
  817. * @param[in] width Width of this buffer
  818. * @param[in] height Height of this buffer
  819. * @param[in] stride of this buffer
  820. */
  821. static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  822. int x, y;
  823. for (y=0; y<height; y++){
  824. if (!get_bits(gb, 1)) //rowskip
  825. memset(plane, 0, width);
  826. else
  827. for (x=0; x<width; x++)
  828. plane[x] = get_bits(gb, 1);
  829. plane += stride;
  830. }
  831. }
  832. /** Decode columns by checking if they are skiped
  833. * @param plane Buffer to store decoded bits
  834. * @param[in] width Width of this buffer
  835. * @param[in] height Height of this buffer
  836. * @param[in] stride of this buffer
  837. * @fixme FIXME: Optimize
  838. */
  839. static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
  840. int x, y;
  841. for (x=0; x<width; x++){
  842. if (!get_bits(gb, 1)) //colskip
  843. for (y=0; y<height; y++)
  844. plane[y*stride] = 0;
  845. else
  846. for (y=0; y<height; y++)
  847. plane[y*stride] = get_bits(gb, 1);
  848. plane ++;
  849. }
  850. }
  851. /** Decode a bitplane's bits
  852. * @param bp Bitplane where to store the decode bits
  853. * @param v VC9 context for bit reading and logging
  854. * @return Status
  855. * @fixme FIXME: Optimize
  856. * @todo TODO: Decide if a struct is needed
  857. */
  858. static int bitplane_decoding(BitPlane *bp, VC9Context *v)
  859. {
  860. GetBitContext *gb = &v->s.gb;
  861. int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
  862. uint8_t invert, *planep = bp->data;
  863. invert = get_bits(gb, 1);
  864. imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
  865. bp->is_raw = 0;
  866. switch (imode)
  867. {
  868. case IMODE_RAW:
  869. //Data is actually read in the MB layer (same for all tests == "raw")
  870. bp->is_raw = 1; //invert ignored
  871. return invert;
  872. case IMODE_DIFF2:
  873. case IMODE_NORM2:
  874. if ((bp->height*bp->width) & 1)
  875. {
  876. *(++planep) = get_bits(gb, 1);
  877. offset = x = 1;
  878. }
  879. else offset = x = 0;
  880. for (y=0; y<bp->height; y++)
  881. {
  882. for(; x<bp->width; x+=2)
  883. {
  884. code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
  885. *(++planep) = code&1; //lsb => left
  886. *(++planep) = (code>>1)&1; //msb => right
  887. }
  888. planep += bp->stride-bp->width;
  889. if ((bp->width-offset)&1) //Odd number previously processed
  890. {
  891. code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
  892. *planep = code&1;
  893. planep += bp->stride-bp->width;
  894. *planep = (code>>1)&1; //msb => right
  895. offset = x = 1;
  896. }
  897. else
  898. {
  899. offset = x = 0;
  900. planep += bp->stride-bp->width;
  901. }
  902. }
  903. break;
  904. case IMODE_DIFF6:
  905. case IMODE_NORM6:
  906. use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
  907. tile_w= use_vertical_tile ? 2 : 3;
  908. tile_h= use_vertical_tile ? 3 : 2;
  909. for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
  910. for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
  911. code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
  912. if(code<0){
  913. av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
  914. return -1;
  915. }
  916. //FIXME following is a pure guess and probably wrong
  917. //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
  918. planep[x + 0*bp->stride]= (code>>0)&1;
  919. planep[x + 1 + 0*bp->stride]= (code>>1)&1;
  920. //FIXME Does branch prediction help here?
  921. if(use_vertical_tile){
  922. planep[x + 0 + 1*bp->stride]= (code>>2)&1;
  923. planep[x + 1 + 1*bp->stride]= (code>>3)&1;
  924. planep[x + 0 + 2*bp->stride]= (code>>4)&1;
  925. planep[x + 1 + 2*bp->stride]= (code>>5)&1;
  926. }else{
  927. planep[x + 2 + 0*bp->stride]= (code>>2)&1;
  928. planep[x + 0 + 1*bp->stride]= (code>>3)&1;
  929. planep[x + 1 + 1*bp->stride]= (code>>4)&1;
  930. planep[x + 2 + 1*bp->stride]= (code>>5)&1;
  931. }
  932. }
  933. }
  934. x= bp->width % tile_w;
  935. decode_colskip(bp->data , x, bp->height , bp->stride, &v->s.gb);
  936. decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, &v->s.gb);
  937. break;
  938. case IMODE_ROWSKIP:
  939. decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  940. break;
  941. case IMODE_COLSKIP:
  942. decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
  943. break;
  944. default: break;
  945. }
  946. /* Applying diff operator */
  947. if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
  948. {
  949. planep = bp->data;
  950. planep[0] ^= invert;
  951. for (x=1; x<bp->width; x++)
  952. planep[x] ^= planep[x-1];
  953. for (y=1; y<bp->height; y++)
  954. {
  955. planep += bp->stride;
  956. planep[0] ^= planep[-bp->stride];
  957. for (x=1; x<bp->width; x++)
  958. {
  959. if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
  960. else planep[x] ^= planep[x-1];
  961. }
  962. }
  963. }
  964. else if (invert)
  965. {
  966. planep = bp->data;
  967. for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
  968. }
  969. return (imode<<1) + invert;
  970. }
  971. /** @} */ //Bitplane group
  972. /***********************************************************************/
  973. /** VOP Dquant decoding
  974. * @param v VC9 Context
  975. */
  976. static int vop_dquant_decoding(VC9Context *v)
  977. {
  978. GetBitContext *gb = &v->s.gb;
  979. int pqdiff;
  980. //variable size
  981. if (v->dquant == 2)
  982. {
  983. pqdiff = get_bits(gb, 3);
  984. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  985. else v->altpq = v->pq + pqdiff + 1;
  986. }
  987. else
  988. {
  989. v->dquantfrm = get_bits(gb, 1);
  990. if ( v->dquantfrm )
  991. {
  992. v->dqprofile = get_bits(gb, 2);
  993. switch (v->dqprofile)
  994. {
  995. case DQPROFILE_SINGLE_EDGE:
  996. case DQPROFILE_DOUBLE_EDGES:
  997. v->dqsbedge = get_bits(gb, 2);
  998. break;
  999. case DQPROFILE_ALL_MBS:
  1000. v->dqbilevel = get_bits(gb, 1);
  1001. default: break; //Forbidden ?
  1002. }
  1003. if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
  1004. {
  1005. pqdiff = get_bits(gb, 3);
  1006. if (pqdiff == 7) v->altpq = get_bits(gb, 5);
  1007. else v->altpq = v->pq + pqdiff + 1;
  1008. }
  1009. }
  1010. }
  1011. return 0;
  1012. }
  1013. /***********************************************************************/
  1014. /**
  1015. * @defgroup all_frame_hdr All VC9 profiles frame header
  1016. * @brief Part of the frame header decoding from all profiles
  1017. * @warning Only pro/epilog differs between Simple/Main and Advanced => check caller
  1018. * @{
  1019. */
  1020. /** B and BI frame header decoding, primary part
  1021. * @see Tables 11+12, p62-65
  1022. * @param v VC9 context
  1023. * @return Status
  1024. * @warning Also handles BI frames
  1025. */
  1026. static int decode_b_picture_primary_header(VC9Context *v)
  1027. {
  1028. GetBitContext *gb = &v->s.gb;
  1029. int pqindex;
  1030. /* Prolog common to all frametypes should be done in caller */
  1031. if (v->profile == PROFILE_SIMPLE)
  1032. {
  1033. av_log(v->s.avctx, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
  1034. return FRAME_SKIPED;
  1035. }
  1036. v->bfraction = vc9_bfraction_lut[get_vlc2(gb, vc9_bfraction_vlc.table,
  1037. VC9_BFRACTION_VLC_BITS, 2)];
  1038. if (v->bfraction < -1)
  1039. {
  1040. av_log(v->s.avctx, AV_LOG_ERROR, "Invalid BFRaction\n");
  1041. return FRAME_SKIPED;
  1042. }
  1043. else if (!v->bfraction)
  1044. {
  1045. /* We actually have a BI frame */
  1046. v->s.pict_type = BI_TYPE;
  1047. v->buffer_fullness = get_bits(gb, 7);
  1048. }
  1049. /* Read the quantization stuff */
  1050. pqindex = get_bits(gb, 5);
  1051. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1052. v->pq = pquant_table[0][pqindex];
  1053. else
  1054. {
  1055. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1056. }
  1057. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1058. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1059. v->pquantizer = get_bits(gb, 1);
  1060. if (v->profile == PROFILE_ADVANCED)
  1061. {
  1062. if (v->postprocflag) v->postproc = get_bits(gb, 2);
  1063. if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE)
  1064. v->mvrange = get_prefix(gb, 0, 3);
  1065. }
  1066. else
  1067. {
  1068. if (v->extended_mv == 1)
  1069. v->mvrange = get_prefix(gb, 0, 3);
  1070. }
  1071. /* Read the MV mode */
  1072. if (v->s.pict_type != BI_TYPE)
  1073. {
  1074. v->mv_mode = get_bits(gb, 1);
  1075. if (v->pq < 13)
  1076. {
  1077. if (!v->mv_mode)
  1078. {
  1079. v->mv_mode = get_bits(gb, 2);
  1080. if (v->mv_mode)
  1081. av_log(v->s.avctx, AV_LOG_ERROR,
  1082. "mv_mode for lowquant B frame was %i\n", v->mv_mode);
  1083. }
  1084. }
  1085. else
  1086. {
  1087. if (!v->mv_mode)
  1088. {
  1089. if (get_bits(gb, 1))
  1090. av_log(v->s.avctx, AV_LOG_ERROR,
  1091. "mv_mode for highquant B frame was %i\n", v->mv_mode);
  1092. }
  1093. v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
  1094. }
  1095. }
  1096. return 0;
  1097. }
  1098. /** B and BI frame header decoding, secondary part
  1099. * @see Tables 11+12, p62-65
  1100. * @param v VC9 context
  1101. * @return Status
  1102. * @warning Also handles BI frames
  1103. * @warning To call once all MB arrays are allocated
  1104. * @todo Support Advanced Profile headers
  1105. */
  1106. static int decode_b_picture_secondary_header(VC9Context *v)
  1107. {
  1108. GetBitContext *gb = &v->s.gb;
  1109. int status;
  1110. status = bitplane_decoding(&v->skip_mb_plane, v);
  1111. if (status < 0) return -1;
  1112. #if TRACE
  1113. if (v->mv_mode == MV_PMODE_MIXED_MV)
  1114. {
  1115. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1116. if (status < 0)
  1117. return -1;
  1118. #if TRACE
  1119. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1120. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1121. #endif
  1122. }
  1123. //bitplane
  1124. status = bitplane_decoding(&v->direct_mb_plane, v);
  1125. if (status < 0) return -1;
  1126. #if TRACE
  1127. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
  1128. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1129. #endif
  1130. av_log(v->s.avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
  1131. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1132. #endif
  1133. /* FIXME: what is actually chosen for B frames ? */
  1134. v->s.mv_table_index = get_bits(gb, 2); //but using vc9_ tables
  1135. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
  1136. if (v->dquant)
  1137. {
  1138. vop_dquant_decoding(v);
  1139. }
  1140. if (v->vstransform)
  1141. {
  1142. v->ttmbf = get_bits(gb, 1);
  1143. if (v->ttmbf)
  1144. {
  1145. v->ttfrm = get_bits(gb, 2);
  1146. av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
  1147. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1148. }
  1149. }
  1150. /* Epilog (AC/DC syntax) should be done in caller */
  1151. return 0;
  1152. }
  1153. /** I frame header decoding, primary part
  1154. * @see Tables 5+7, p53-54 and 55-57
  1155. * @param v VC9 context
  1156. * @return Status
  1157. * @todo Support Advanced Profile headers
  1158. */
  1159. static int decode_i_picture_primary_header(VC9Context *v)
  1160. {
  1161. GetBitContext *gb = &v->s.gb;
  1162. int pqindex;
  1163. /* Prolog common to all frametypes should be done in caller */
  1164. //BF = Buffer Fullness
  1165. if (v->profile < PROFILE_ADVANCED && get_bits(gb, 7))
  1166. {
  1167. av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n");
  1168. }
  1169. /* Quantizer stuff */
  1170. pqindex = get_bits(gb, 5);
  1171. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1172. v->pq = pquant_table[0][pqindex];
  1173. else
  1174. {
  1175. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1176. }
  1177. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1178. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1179. v->pquantizer = get_bits(gb, 1);
  1180. av_log(v->s.avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n",
  1181. v->pq, v->halfpq);
  1182. return 0;
  1183. }
  1184. /** I frame header decoding, secondary part
  1185. * @param v VC9 context
  1186. * @return Status
  1187. * @todo Support Advanced Profile headers
  1188. */
  1189. static int decode_i_picture_secondary_header(VC9Context *v)
  1190. {
  1191. int status;
  1192. #if HAS_ADVANCED_PROFILE
  1193. if (v->profile == PROFILE_ADVANCED)
  1194. {
  1195. v->s.ac_pred = get_bits(&v->s.gb, 1);
  1196. if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1);
  1197. /* 7.1.1.34 + 8.5.2 */
  1198. if (v->overlap && v->pq<9)
  1199. {
  1200. v->condover = get_bits(&v->s.gb, 1);
  1201. if (v->condover)
  1202. {
  1203. v->condover = 2+get_bits(&v->s.gb, 1);
  1204. if (v->condover == 3)
  1205. {
  1206. status = bitplane_decoding(&v->over_flags_plane, v);
  1207. if (status < 0) return -1;
  1208. # if TRACE
  1209. av_log(v->s.avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
  1210. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1211. # endif
  1212. }
  1213. }
  1214. }
  1215. }
  1216. #endif
  1217. /* Epilog (AC/DC syntax) should be done in caller */
  1218. return 0;
  1219. }
  1220. /** P frame header decoding, primary part
  1221. * @see Tables 5+7, p53-54 and 55-57
  1222. * @param v VC9 context
  1223. * @todo Support Advanced Profile headers
  1224. * @return Status
  1225. */
  1226. static int decode_p_picture_primary_header(VC9Context *v)
  1227. {
  1228. /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
  1229. GetBitContext *gb = &v->s.gb;
  1230. int lowquant, pqindex;
  1231. pqindex = get_bits(gb, 5);
  1232. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1233. v->pq = pquant_table[0][pqindex];
  1234. else
  1235. {
  1236. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1237. }
  1238. if (pqindex < 9) v->halfpq = get_bits(gb, 1);
  1239. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1240. v->pquantizer = get_bits(gb, 1);
  1241. av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n",
  1242. v->pq, v->halfpq);
  1243. if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
  1244. #if HAS_ADVANCED_PROFILE
  1245. if (v->profile == PROFILE_ADVANCED)
  1246. {
  1247. if (v->postprocflag) v->postproc = get_bits(gb, 1);
  1248. }
  1249. else
  1250. #endif
  1251. if (v->multires) v->respic = get_bits(gb, 2);
  1252. lowquant = (v->pquantizer>12) ? 0 : 1;
  1253. v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
  1254. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  1255. {
  1256. v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
  1257. v->lumscale = get_bits(gb, 6);
  1258. v->lumshift = get_bits(gb, 6);
  1259. }
  1260. return 0;
  1261. }
  1262. /** P frame header decoding, secondary part
  1263. * @see Tables 5+7, p53-54 and 55-57
  1264. * @param v VC9 context
  1265. * @warning To call once all MB arrays are allocated
  1266. * @return Status
  1267. */
  1268. static int decode_p_picture_secondary_header(VC9Context *v)
  1269. {
  1270. GetBitContext *gb = &v->s.gb;
  1271. int status = 0;
  1272. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1273. v->mv_mode2 == MV_PMODE_MIXED_MV)
  1274. || v->mv_mode == MV_PMODE_MIXED_MV)
  1275. {
  1276. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1277. if (status < 0) return -1;
  1278. #if TRACE
  1279. av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1280. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1281. #endif
  1282. }
  1283. status = bitplane_decoding(&v->skip_mb_plane, v);
  1284. if (status < 0) return -1;
  1285. #if TRACE
  1286. av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1287. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1288. #endif
  1289. /* Hopefully this is correct for P frames */
  1290. v->s.mv_table_index =get_bits(gb, 2); //but using vc9_ tables
  1291. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
  1292. if (v->dquant)
  1293. {
  1294. av_log(v->s.avctx, AV_LOG_INFO, "VOP DQuant info\n");
  1295. vop_dquant_decoding(v);
  1296. }
  1297. v->ttfrm = 0; //FIXME Is that so ?
  1298. if (v->vstransform)
  1299. {
  1300. v->ttmbf = get_bits(gb, 1);
  1301. if (v->ttmbf)
  1302. {
  1303. v->ttfrm = get_bits(gb, 2);
  1304. av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
  1305. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1306. }
  1307. }
  1308. /* Epilog (AC/DC syntax) should be done in caller */
  1309. return 0;
  1310. }
  1311. /** @} */ //End of group all_frm_hdr
  1312. /***********************************************************************/
  1313. /**
  1314. * @defgroup std_frame_hdr VC9 Simple/Main Profiles header decoding
  1315. * @brief Part of the frame header decoding belonging to Simple/Main Profiles
  1316. * @warning Only pro/epilog differs between Simple/Main and Advanced =>
  1317. * check caller
  1318. * @{
  1319. */
  1320. /** Frame header decoding, first part, in Simple and Main profiles
  1321. * @see Tables 5+7, p53-54 and 55-57
  1322. * @param v VC9 context
  1323. * @todo FIXME: RANGEREDFRM element not read if BI frame from Table6, P54
  1324. * However, 7.1.1.8 says "all frame types, for main profiles"
  1325. * @return Status
  1326. */
  1327. static int standard_decode_picture_primary_header(VC9Context *v)
  1328. {
  1329. GetBitContext *gb = &v->s.gb;
  1330. int status = 0;
  1331. if (v->finterpflag) v->interpfrm = get_bits(gb, 1);
  1332. skip_bits(gb, 2); //framecnt unused
  1333. if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
  1334. v->s.pict_type = get_bits(gb, 1);
  1335. if (v->s.avctx->max_b_frames)
  1336. {
  1337. if (!v->s.pict_type)
  1338. {
  1339. if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
  1340. else v->s.pict_type = B_TYPE;
  1341. }
  1342. else v->s.pict_type = P_TYPE;
  1343. }
  1344. else v->s.pict_type++;
  1345. switch (v->s.pict_type)
  1346. {
  1347. case I_TYPE: status = decode_i_picture_primary_header(v); break;
  1348. case P_TYPE: status = decode_p_picture_primary_header(v); break;
  1349. case BI_TYPE: //Same as B
  1350. case B_TYPE: status = decode_b_picture_primary_header(v); break;
  1351. }
  1352. if (status == FRAME_SKIPED)
  1353. {
  1354. av_log(v->s.avctx, AV_LOG_INFO, "Skipping frame...\n");
  1355. return status;
  1356. }
  1357. return 0;
  1358. }
  1359. /** Frame header decoding, secondary part
  1360. * @param v VC9 context
  1361. * @warning To call once all MB arrays are allocated
  1362. * @return Status
  1363. */
  1364. static int standard_decode_picture_secondary_header(VC9Context *v)
  1365. {
  1366. GetBitContext *gb = &v->s.gb;
  1367. int status = 0;
  1368. switch (v->s.pict_type)
  1369. {
  1370. case P_TYPE: status = decode_p_picture_secondary_header(v); break;
  1371. case B_TYPE: status = decode_b_picture_secondary_header(v); break;
  1372. case BI_TYPE:
  1373. case I_TYPE: break; //Nothing needed as it's done in the epilog
  1374. }
  1375. if (status < 0) return FRAME_SKIPED;
  1376. /* AC Syntax */
  1377. v->ac_table_level = decode012(gb);
  1378. if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
  1379. {
  1380. v->ac2_table_level = decode012(gb);
  1381. }
  1382. /* DC Syntax */
  1383. v->s.dc_table_index = decode012(gb);
  1384. return 0;
  1385. }
  1386. /** @} */ //End for group std_frame_hdr
  1387. #if HAS_ADVANCED_PROFILE
  1388. /***********************************************************************/
  1389. /**
  1390. * @defgroup adv_frame_hdr VC9 Advanced Profile header decoding
  1391. * @brief Part of the frame header decoding belonging to Advanced Profiles
  1392. * @warning Only pro/epilog differs between Simple/Main and Advanced =>
  1393. * check caller
  1394. * @{
  1395. */
  1396. /** Frame header decoding, primary part
  1397. * @param v VC9 context
  1398. * @return Status
  1399. */
  1400. static int advanced_decode_picture_primary_header(VC9Context *v)
  1401. {
  1402. GetBitContext *gb = &v->s.gb;
  1403. static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
  1404. int type;
  1405. if (v->interlace)
  1406. {
  1407. v->fcm = get_bits(gb, 1);
  1408. if (v->fcm) v->fcm = 2+get_bits(gb, 1);
  1409. }
  1410. type = get_prefix(gb, 0, 4);
  1411. if (type > 4 || type < 0) return FRAME_SKIPED;
  1412. v->s.pict_type = type_table[type];
  1413. av_log(v->s.avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->s.pict_type);
  1414. if (v->tfcntrflag) v->tfcntr = get_bits(gb, 8);
  1415. if (v->broadcast)
  1416. {
  1417. if (!v->interlace) v->rptfrm = get_bits(gb, 2);
  1418. else
  1419. {
  1420. v->tff = get_bits(gb, 1);
  1421. v->rff = get_bits(gb, 1);
  1422. }
  1423. }
  1424. if (v->panscanflag)
  1425. {
  1426. #if 0
  1427. for (i=0; i<v->numpanscanwin; i++)
  1428. {
  1429. v->topleftx[i] = get_bits(gb, 16);
  1430. v->toplefty[i] = get_bits(gb, 16);
  1431. v->bottomrightx[i] = get_bits(gb, 16);
  1432. v->bottomrighty[i] = get_bits(gb, 16);
  1433. }
  1434. #else
  1435. skip_bits(gb, 16*4*v->numpanscanwin);
  1436. #endif
  1437. }
  1438. v->s.no_rounding = !get_bits(gb, 1);
  1439. v->uvsamp = get_bits(gb, 1);
  1440. if (v->finterpflag == 1) v->interpfrm = get_bits(gb, 1);
  1441. switch(v->s.pict_type)
  1442. {
  1443. case I_TYPE: if (decode_i_picture_primary_header(v) < 0) return -1;
  1444. case P_TYPE: if (decode_p_picture_primary_header(v) < 0) return -1;
  1445. case BI_TYPE:
  1446. case B_TYPE: if (decode_b_picture_primary_header(v) < 0) return FRAME_SKIPED;
  1447. default: return -1;
  1448. }
  1449. }
  1450. /** Frame header decoding, secondary part
  1451. * @param v VC9 context
  1452. * @return Status
  1453. */
  1454. static int advanced_decode_picture_secondary_header(VC9Context *v)
  1455. {
  1456. GetBitContext *gb = &v->s.gb;
  1457. int status = 0;
  1458. switch(v->s.pict_type)
  1459. {
  1460. case P_TYPE: status = decode_p_picture_secondary_header(v); break;
  1461. case B_TYPE: status = decode_b_picture_secondary_header(v); break;
  1462. case BI_TYPE:
  1463. case I_TYPE: status = decode_i_picture_secondary_header(v); break;
  1464. }
  1465. if (status<0) return FRAME_SKIPED;
  1466. /* AC Syntax */
  1467. v->ac_table_level = decode012(gb);
  1468. if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
  1469. {
  1470. v->ac2_table_level = decode012(gb);
  1471. }
  1472. /* DC Syntax */
  1473. v->s.dc_table_index = decode012(gb);
  1474. return 0;
  1475. }
  1476. #endif
  1477. /** @} */ //End for adv_frame_hdr
  1478. /***********************************************************************/
  1479. /**
  1480. * @defgroup block VC9 Block-level functions
  1481. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1482. * @todo TODO: Integrate to MpegEncContext facilities
  1483. * @{
  1484. */
  1485. /**
  1486. * @def GET_MQUANT
  1487. * @brief Get macroblock-level quantizer scale
  1488. */
  1489. #define GET_MQUANT() \
  1490. if (v->dquantfrm) \
  1491. { \
  1492. if (v->dqprofile == DQPROFILE_ALL_MBS) \
  1493. { \
  1494. if (v->dqbilevel) \
  1495. { \
  1496. mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \
  1497. } \
  1498. else \
  1499. { \
  1500. mqdiff = get_bits(gb, 3); \
  1501. if (mqdiff != 7) mquant = v->pq + mqdiff; \
  1502. else mquant = get_bits(gb, 5); \
  1503. } \
  1504. } \
  1505. }
  1506. /**
  1507. * @def GET_MVDATA(_dmv_x, _dmv_y)
  1508. * @brief Get MV differentials
  1509. * @see MVDATA decoding from 8.3.5.2, p(1)20
  1510. * @param dmv_x Horizontal differential for decoded MV
  1511. * @param dmv_y Vertical differential for decoded MV
  1512. */
  1513. #define GET_MVDATA(_dmv_x, _dmv_y) \
  1514. index = 1 + get_vlc2(gb, vc9_mv_diff_vlc[s->mv_table_index].table,\
  1515. VC9_MV_DIFF_VLC_BITS, 2); \
  1516. if (index > 36) \
  1517. { \
  1518. mb_has_coeffs = 1; \
  1519. index -= 37; \
  1520. } \
  1521. else mb_has_coeffs = 0; \
  1522. s->mb_intra = 0; \
  1523. if (!index) { _dmv_x = _dmv_y = 0; } \
  1524. else if (index == 35) \
  1525. { \
  1526. _dmv_x = get_bits(gb, v->k_x); \
  1527. _dmv_y = get_bits(gb, v->k_y); \
  1528. s->mb_intra = 1; \
  1529. } \
  1530. else \
  1531. { \
  1532. index1 = index%6; \
  1533. if (s->mspel && index1 == 5) val = 1; \
  1534. else val = 0; \
  1535. val = get_bits(gb, size_table[index1] - val); \
  1536. sign = 0 - (val&1); \
  1537. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1538. \
  1539. index1 = index/6; \
  1540. if (s->mspel && index1 == 5) val = 1; \
  1541. else val = 0; \
  1542. val = get_bits(gb, size_table[index1] - val); \
  1543. sign = 0 - (val&1); \
  1544. _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1545. }
  1546. /** Get predicted DC value
  1547. * prediction dir: left=0, top=1
  1548. */
  1549. static inline int vc9_pred_dc(MpegEncContext *s, int n,
  1550. uint16_t **dc_val_ptr, int *dir_ptr)
  1551. {
  1552. int a, b, c, wrap, pred, scale;
  1553. int16_t *dc_val;
  1554. /* find prediction */
  1555. if (n < 4) {
  1556. scale = s->y_dc_scale;
  1557. } else {
  1558. scale = s->c_dc_scale;
  1559. }
  1560. wrap = s->block_wrap[n];
  1561. dc_val= s->dc_val[0] + s->block_index[n];
  1562. /* B C
  1563. * A X
  1564. */
  1565. a = dc_val[ - 1];
  1566. b = dc_val[ - 1 - wrap];
  1567. c = dc_val[ - wrap];
  1568. if(s->first_slice_line && (n&2)==0) b=c=1024;
  1569. /* XXX: the following solution consumes divisions, but it does not
  1570. necessitate to modify mpegvideo.c. The problem comes from the
  1571. fact they decided to store the quantized DC (which would lead
  1572. to problems if Q could vary !) */
  1573. #if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC
  1574. asm volatile(
  1575. "movl %3, %%eax \n\t"
  1576. "shrl $1, %%eax \n\t"
  1577. "addl %%eax, %2 \n\t"
  1578. "addl %%eax, %1 \n\t"
  1579. "addl %0, %%eax \n\t"
  1580. "mull %4 \n\t"
  1581. "movl %%edx, %0 \n\t"
  1582. "movl %1, %%eax \n\t"
  1583. "mull %4 \n\t"
  1584. "movl %%edx, %1 \n\t"
  1585. "movl %2, %%eax \n\t"
  1586. "mull %4 \n\t"
  1587. "movl %%edx, %2 \n\t"
  1588. : "+b" (a), "+c" (b), "+D" (c)
  1589. : "g" (scale), "S" (inverse[scale])
  1590. : "%eax", "%edx"
  1591. );
  1592. #else
  1593. /* #elif defined (ARCH_ALPHA) */
  1594. /* Divisions are extremely costly on Alpha; optimize the most
  1595. common case. But they are costly everywhere...
  1596. */
  1597. if (scale == 8) {
  1598. a = (a + (8 >> 1)) / 8;
  1599. b = (b + (8 >> 1)) / 8;
  1600. c = (c + (8 >> 1)) / 8;
  1601. } else {
  1602. a = FASTDIV((a + (scale >> 1)), scale);
  1603. b = FASTDIV((b + (scale >> 1)), scale);
  1604. c = FASTDIV((c + (scale >> 1)), scale);
  1605. }
  1606. #endif
  1607. if (abs(a - b) <= abs(b - c)) {
  1608. pred = c;
  1609. *dir_ptr = 1;
  1610. } else {
  1611. pred = a;
  1612. *dir_ptr = 0;
  1613. }
  1614. /* update predictor */
  1615. *dc_val_ptr = &dc_val[0];
  1616. return pred;
  1617. }
  1618. /** Decode one block, inter or intra
  1619. * @param v The VC9 context
  1620. * @param block 8x8 DCT block
  1621. * @param n Block index in the current MB (<4=>luma)
  1622. * @param coded If the block is coded
  1623. * @param MB quant, if decoded at the MB layer
  1624. * @see Inter TT: Table 21, p73 + p91-85
  1625. * @see Intra TT: Table 20, p72 + p(1)05-(1)07
  1626. * @todo TODO: Process the blocks
  1627. * @todo TODO: Use M$ MPEG-4 cbp prediction
  1628. */
  1629. int vc9_decode_block(VC9Context *v, DCTELEM block[64], int n, int coded, int mquant)
  1630. {
  1631. GetBitContext *gb = &v->s.gb;
  1632. MpegEncContext *s = &v->s;
  1633. int ttblk; /* Transform Type per Block */
  1634. int subblkpat; /* Sub-block Transform Type Pattern */
  1635. int dc_pred_dir; /* Direction of the DC prediction used */
  1636. int run_diff, i;
  1637. if (s->mb_intra)
  1638. {
  1639. int dcdiff;
  1640. uint16_t *dc_val;
  1641. /* Get DC differential */
  1642. if (n < 4) {
  1643. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1644. } else {
  1645. dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
  1646. }
  1647. if (dcdiff < 0){
  1648. av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
  1649. return -1;
  1650. }
  1651. if (dcdiff)
  1652. {
  1653. if (dcdiff == 119 /* ESC index value */)
  1654. {
  1655. /* TODO: Optimize */
  1656. if (mquant == 1) dcdiff = get_bits(gb, 10);
  1657. else if (mquant == 2) dcdiff = get_bits(gb, 9);
  1658. else dcdiff = get_bits(gb, 8);
  1659. }
  1660. else
  1661. {
  1662. if (mquant == 1)
  1663. dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
  1664. else if (mquant == 2)
  1665. dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
  1666. }
  1667. if (get_bits(gb, 1))
  1668. dcdiff = -dcdiff;
  1669. }
  1670. /* Prediction */
  1671. dcdiff += vc9_pred_dc(s, n, &dc_val, &dc_pred_dir);
  1672. if (n < 4) {
  1673. *dc_val = dcdiff * s->y_dc_scale;
  1674. } else {
  1675. *dc_val = dcdiff * s->c_dc_scale;
  1676. }
  1677. if (dcdiff < 0)
  1678. {
  1679. av_log(s->avctx, AV_LOG_ERROR, "DC=%i<0\n", dcdiff);
  1680. return -1;
  1681. }
  1682. block[0] = dcdiff; //XXX: Must be > 0
  1683. /* Skip ? */
  1684. run_diff = 0;
  1685. i = 0;
  1686. if (!coded) {
  1687. goto not_coded;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. mquant = v->pq;
  1693. /* Get TTBLK */
  1694. if (v->ttmb < 8) /* per block */
  1695. ttblk = get_vlc2(gb, vc9_ttblk_vlc[v->tt_index].table, VC9_TTBLK_VLC_BITS, 2);
  1696. else /* Per frame */
  1697. ttblk = 0; //FIXME, depends on ttfrm
  1698. /* Get SUBBLKPAT */
  1699. if (ttblk == v->ttblk4x4) /* 4x4 transform for that qp value */
  1700. subblkpat = 1+get_vlc2(gb, vc9_subblkpat_vlc[v->tt_index].table,
  1701. VC9_SUBBLKPAT_VLC_BITS, 2);
  1702. else /* All others: 8x8, 4x8, 8x4 */
  1703. subblkpat = decode012(gb);
  1704. }
  1705. //TODO AC Decoding
  1706. not_coded:
  1707. if (s->mb_intra) {
  1708. mpeg4_pred_ac(s, block, n, dc_pred_dir);
  1709. if (s->ac_pred) {
  1710. i = 63; /* XXX: not optimal */
  1711. }
  1712. }
  1713. if(i>0) i=63; //FIXME/XXX optimize
  1714. s->block_last_index[n] = i;
  1715. return 0;
  1716. }
  1717. /** @} */ //End for group block
  1718. /***********************************************************************/
  1719. /**
  1720. * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
  1721. * @see 7.1.4, p91 and 8.1.1.7, p(1)04
  1722. * @todo TODO: Integrate to MpegEncContext facilities
  1723. * @{
  1724. */
  1725. static inline int vc9_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
  1726. {
  1727. int xy, wrap, pred, a, b, c;
  1728. xy = s->block_index[n];
  1729. wrap = s->b8_stride;
  1730. /* B C
  1731. * A X
  1732. */
  1733. a = s->coded_block[xy - 1 ];
  1734. b = s->coded_block[xy - 1 - wrap];
  1735. c = s->coded_block[xy - wrap];
  1736. if (b == c) {
  1737. pred = a;
  1738. } else {
  1739. pred = c;
  1740. }
  1741. /* store value */
  1742. *coded_block_ptr = &s->coded_block[xy];
  1743. return pred;
  1744. }
  1745. int vc9_decode_i_mb(VC9Context *v, DCTELEM block[6][64])
  1746. {
  1747. int i, cbp, val;
  1748. uint8_t *coded_val;
  1749. uint32_t * const mb_type_ptr= &v->s.current_picture.mb_type[ v->s.mb_x + v->s.mb_y*v->s.mb_stride ];
  1750. v->s.mb_intra = 1;
  1751. cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
  1752. if (cbp < 0) return -1;
  1753. v->s.ac_pred = get_bits(&v->s.gb, 1);
  1754. for (i=0; i<6; i++)
  1755. {
  1756. val = ((cbp >> (5 - i)) & 1);
  1757. if (i < 4) {
  1758. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1759. val = val ^ pred;
  1760. *coded_val = val;
  1761. }
  1762. cbp |= val << (5 - i);
  1763. if (vc9_decode_block(v, block[i], i, val, v->pq) < 0) //FIXME Should be mquant
  1764. {
  1765. av_log(v->s.avctx, AV_LOG_ERROR,
  1766. "\nerror while decoding block: %d x %d (%d)\n", v->s.mb_x, v->s.mb_y, i);
  1767. return -1;
  1768. }
  1769. }
  1770. return 0;
  1771. }
  1772. int vc9_decode_p_mb(VC9Context *v, DCTELEM block[6][64])
  1773. {
  1774. MpegEncContext *s = &v->s;
  1775. GetBitContext *gb = &s->gb;
  1776. int i, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
  1777. int cbp; /* cbp decoding stuff */
  1778. int hybrid_pred; /* Prediction types */
  1779. int mv_mode_bit = 0;
  1780. int mqdiff, mquant; /* MB quantization */
  1781. int ttmb; /* MB Transform type */
  1782. int status;
  1783. uint8_t *coded_val;
  1784. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1785. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1786. int mb_has_coeffs = 1; /* last_flag */
  1787. int dmv_x, dmv_y; /* Differential MV components */
  1788. int index, index1; /* LUT indices */
  1789. int val, sign; /* temp values */
  1790. if (v->mv_type_mb_plane.is_raw)
  1791. v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1792. if (v->skip_mb_plane.is_raw)
  1793. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  1794. if (!mv_mode_bit) /* 1MV mode */
  1795. {
  1796. if (!v->skip_mb_plane.data[mb_offset])
  1797. {
  1798. GET_MVDATA(dmv_x, dmv_y);
  1799. /* hybrid mv pred, 8.3.5.3.4 */
  1800. if (v->mv_mode == MV_PMODE_1MV ||
  1801. v->mv_mode == MV_PMODE_MIXED_MV)
  1802. hybrid_pred = get_bits(gb, 1);
  1803. if (s->mb_intra && !mb_has_coeffs)
  1804. {
  1805. GET_MQUANT();
  1806. s->ac_pred = get_bits(gb, 1);
  1807. /* XXX: how to handle cbp ? */
  1808. }
  1809. else if (mb_has_coeffs)
  1810. {
  1811. if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
  1812. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
  1813. GET_MQUANT();
  1814. }
  1815. else
  1816. {
  1817. mquant = v->pq;
  1818. /* XXX: How to handle cbp ? */
  1819. }
  1820. if (!v->ttmbf)
  1821. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  1822. VC9_TTMB_VLC_BITS, 12);
  1823. for (i=0; i<6; i++)
  1824. {
  1825. val = ((cbp >> (5 - i)) & 1);
  1826. if (i < 4) {
  1827. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1828. val = val ^ pred;
  1829. *coded_val = val;
  1830. }
  1831. vc9_decode_block(v, block[i], i, val, mquant); //FIXME
  1832. }
  1833. }
  1834. else //Skipped
  1835. {
  1836. /* hybrid mv pred, 8.3.5.3.4 */
  1837. if (v->mv_mode == MV_PMODE_1MV ||
  1838. v->mv_mode == MV_PMODE_MIXED_MV)
  1839. hybrid_pred = get_bits(gb, 1);
  1840. /* TODO: blah */
  1841. return 0;
  1842. }
  1843. } //1MV mode
  1844. else //4MV mode
  1845. {
  1846. if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
  1847. {
  1848. /* Get CBPCY */
  1849. cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
  1850. for (i=0; i<6; i++)
  1851. {
  1852. val = ((cbp >> (5 - i)) & 1);
  1853. if (i < 4) {
  1854. int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
  1855. val = val ^ pred;
  1856. *coded_val = val;
  1857. }
  1858. if (i<4 && val)
  1859. {
  1860. GET_MVDATA(dmv_x, dmv_y);
  1861. }
  1862. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1863. hybrid_pred = get_bits(gb, 1);
  1864. GET_MQUANT();
  1865. if (s->mb_intra /* One of the 4 blocks is intra */ &&
  1866. index /* non-zero pred for that block */)
  1867. s->ac_pred = get_bits(gb, 1);
  1868. if (!v->ttmbf)
  1869. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  1870. VC9_TTMB_VLC_BITS, 12);
  1871. status = vc9_decode_block(v, block[i], i, val, mquant);
  1872. }
  1873. return status;
  1874. }
  1875. else //Skipped MB
  1876. {
  1877. for (i=0; i<4; i++)
  1878. {
  1879. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1880. hybrid_pred = get_bits(gb, 1);
  1881. vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, mquant); //FIXME
  1882. }
  1883. /* TODO: blah */
  1884. return 0;
  1885. }
  1886. }
  1887. /* Should never happen */
  1888. return -1;
  1889. }
  1890. int vc9_decode_b_mb(VC9Context *v, DCTELEM block[6][64])
  1891. {
  1892. int i;
  1893. //Decode CBP
  1894. for (i=0; i<6; i++)
  1895. {
  1896. vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, v->pq /*Should be mquant*/); //FIXME
  1897. }
  1898. return 0;
  1899. }
  1900. static int standard_decode_mbs(VC9Context *v)
  1901. {
  1902. GetBitContext *gb = &v->s.gb;
  1903. MpegEncContext *s = &v->s;
  1904. /* Set transform type info depending on pq */
  1905. if (v->pq < 5)
  1906. {
  1907. v->tt_index = 0;
  1908. v->ttblk4x4 = 3;
  1909. }
  1910. else if (v->pq < 13)
  1911. {
  1912. v->tt_index = 1;
  1913. v->ttblk4x4 = 3;
  1914. }
  1915. else
  1916. {
  1917. v->tt_index = 2;
  1918. v->ttblk4x4 = 2;
  1919. }
  1920. if (s->pict_type != I_TYPE)
  1921. {
  1922. /* Select proper long MV range */
  1923. switch (v->mvrange)
  1924. {
  1925. case 1: v->k_x = 10; v->k_y = 9; break;
  1926. case 2: v->k_x = 12; v->k_y = 10; break;
  1927. case 3: v->k_x = 13; v->k_y = 11; break;
  1928. default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
  1929. }
  1930. s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
  1931. v->k_x -= s->mspel;
  1932. v->k_y -= s->mspel;
  1933. }
  1934. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  1935. {
  1936. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
  1937. {
  1938. //FIXME Get proper MB DCTELEM
  1939. //TODO Move out of the loop
  1940. switch (s->pict_type)
  1941. {
  1942. case I_TYPE: vc9_decode_i_mb(v, NULL); break;
  1943. case P_TYPE: vc9_decode_i_mb(v, NULL); break;
  1944. case BI_TYPE:
  1945. case B_TYPE: vc9_decode_i_mb(v, NULL); break;
  1946. }
  1947. }
  1948. //Add a check for overconsumption ?
  1949. }
  1950. return 0;
  1951. }
  1952. /**
  1953. * @def GET_CBPCY(table, bits)
  1954. * @brief Get the Coded Block Pattern for luma and chroma
  1955. * @param table VLC table to use (get_vlc2 second parameter)
  1956. * @param bits Average bitlength (third parameter to get_vlc2)
  1957. * @see 8.1.1.5, p(1)02-(1)03
  1958. */
  1959. #define GET_CBPCY(table, bits) \
  1960. predicted_cbpcy = get_vlc2(gb, table, bits, 2); \
  1961. cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
  1962. ? previous_cbpcy[1] : p_cbpcy[+2]; \
  1963. cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
  1964. cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \
  1965. cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \
  1966. cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) \
  1967. ? previous_cbpcy[3] : cbpcy[0]; \
  1968. cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
  1969. cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
  1970. cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
  1971. /** Decode all MBs for an I frame in Simple/Main profile
  1972. * @see 8.1, p100
  1973. * @todo TODO: Process the blocks
  1974. * @todo TODO: Use M$ MPEG-4 cbp prediction
  1975. */
  1976. static int standard_decode_i_mbs(VC9Context *v)
  1977. {
  1978. GetBitContext *gb = &v->s.gb;
  1979. MpegEncContext *s = &v->s;
  1980. int mb_offset = 0; /* MB/Block Position info */
  1981. uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
  1982. *p_cbpcy /* Pointer to skip some math */;
  1983. /* Reset CBPCY predictors */
  1984. memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
  1985. /* Select ttmb table depending on pq */
  1986. if (v->pq < 5)
  1987. {
  1988. v->tt_index = 0;
  1989. v->ttblk4x4 = 3;
  1990. }
  1991. else if (v->pq < 13)
  1992. {
  1993. v->tt_index = 1;
  1994. v->ttblk4x4 = 3;
  1995. }
  1996. else
  1997. {
  1998. v->tt_index = 2;
  1999. v->ttblk4x4 = 2;
  2000. }
  2001. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2002. {
  2003. /* Init CBPCY for line */
  2004. *((uint32_t*)previous_cbpcy) = 0x00000000;
  2005. p_cbpcy = v->previous_line_cbpcy+4;
  2006. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4)
  2007. {
  2008. /* Get CBPCY */
  2009. GET_CBPCY(ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS);
  2010. s->ac_pred = get_bits(gb, 1);
  2011. /* TODO: Decode blocks from that mb wrt cbpcy */
  2012. /* Update for next block */
  2013. #if TRACE > 2
  2014. av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
  2015. " cbpcy=%i%i%i%i\n", mb_offset,
  2016. p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
  2017. previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
  2018. cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
  2019. #endif
  2020. *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
  2021. *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
  2022. mb_offset++;
  2023. }
  2024. }
  2025. return 0;
  2026. }
  2027. /** Decode all MBs for an P frame in Simple/Main profile
  2028. * @see 8.1, p(1)15
  2029. * @todo TODO: Process the blocks
  2030. * @todo TODO: Use M$ MPEG-4 cbp prediction
  2031. */
  2032. static int decode_p_mbs(VC9Context *v)
  2033. {
  2034. MpegEncContext *s = &v->s;
  2035. GetBitContext *gb = &v->s.gb;
  2036. int mb_offset = 0, i; /* MB/Block Position info */
  2037. uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
  2038. *p_cbpcy /* Pointer to skip some math */;
  2039. int hybrid_pred; /* Prediction types */
  2040. int mv_mode_bit = 0;
  2041. int mqdiff, mquant; /* MB quantization */
  2042. int ttmb; /* MB Transform type */
  2043. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  2044. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  2045. int mb_has_coeffs = 1; /* last_flag */
  2046. int dmv_x, dmv_y; /* Differential MV components */
  2047. int index, index1; /* LUT indices */
  2048. int val, sign; /* MVDATA temp values */
  2049. /* Select ttmb table depending on pq */
  2050. if (v->pq < 5)
  2051. {
  2052. v->tt_index = 0;
  2053. v->ttblk4x4 = 3;
  2054. }
  2055. else if (v->pq < 13)
  2056. {
  2057. v->tt_index = 1;
  2058. v->ttblk4x4 = 3;
  2059. }
  2060. else
  2061. {
  2062. v->tt_index = 2;
  2063. v->ttblk4x4 = 2;
  2064. }
  2065. /* Select proper long MV range */
  2066. switch (v->mvrange)
  2067. {
  2068. case 1: v->k_x = 10; v->k_y = 9; break;
  2069. case 2: v->k_x = 12; v->k_y = 10; break;
  2070. case 3: v->k_x = 13; v->k_y = 11; break;
  2071. default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
  2072. }
  2073. s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
  2074. v->k_x -= s->mspel;
  2075. v->k_y -= s->mspel;
  2076. /* Reset CBPCY predictors */
  2077. memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
  2078. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2079. {
  2080. /* Init CBPCY for line */
  2081. *((uint32_t*)previous_cbpcy) = 0x00000000;
  2082. p_cbpcy = v->previous_line_cbpcy+4;
  2083. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4)
  2084. {
  2085. if (v->mv_type_mb_plane.is_raw)
  2086. v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2087. if (v->skip_mb_plane.is_raw)
  2088. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2089. if (!mv_mode_bit) /* 1MV mode */
  2090. {
  2091. if (!v->skip_mb_plane.data[mb_offset])
  2092. {
  2093. GET_MVDATA(dmv_x, dmv_y);
  2094. /* hybrid mv pred, 8.3.5.3.4 */
  2095. if (v->mv_mode == MV_PMODE_1MV ||
  2096. v->mv_mode == MV_PMODE_MIXED_MV)
  2097. hybrid_pred = get_bits(gb, 1);
  2098. if (s->mb_intra && !mb_has_coeffs)
  2099. {
  2100. GET_MQUANT();
  2101. s->ac_pred = get_bits(gb, 1);
  2102. }
  2103. else if (mb_has_coeffs)
  2104. {
  2105. if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
  2106. predicted_cbpcy = get_vlc2(gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
  2107. GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
  2108. GET_MQUANT();
  2109. }
  2110. if (!v->ttmbf)
  2111. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  2112. VC9_TTMB_VLC_BITS, 12);
  2113. /* TODO: decode blocks from that mb wrt cbpcy */
  2114. }
  2115. else //Skipped
  2116. {
  2117. /* hybrid mv pred, 8.3.5.3.4 */
  2118. if (v->mv_mode == MV_PMODE_1MV ||
  2119. v->mv_mode == MV_PMODE_MIXED_MV)
  2120. hybrid_pred = get_bits(gb, 1);
  2121. }
  2122. } //1MV mode
  2123. else //4MV mode
  2124. {
  2125. if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
  2126. {
  2127. /* Get CBPCY */
  2128. GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
  2129. for (i=0; i<4; i++) //For all 4 Y blocks
  2130. {
  2131. if (cbpcy[i] /* cbpcy set for this block */)
  2132. {
  2133. GET_MVDATA(dmv_x, dmv_y);
  2134. }
  2135. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2136. hybrid_pred = get_bits(gb, 1);
  2137. GET_MQUANT();
  2138. if (s->mb_intra /* One of the 4 blocks is intra */ &&
  2139. index /* non-zero pred for that block */)
  2140. s->ac_pred = get_bits(gb, 1);
  2141. if (!v->ttmbf)
  2142. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  2143. VC9_TTMB_VLC_BITS, 12);
  2144. /* TODO: Process blocks wrt cbpcy */
  2145. }
  2146. }
  2147. else //Skipped MB
  2148. {
  2149. for (i=0; i<4; i++) //All 4 Y blocks
  2150. {
  2151. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  2152. hybrid_pred = get_bits(gb, 1);
  2153. /* TODO: do something */
  2154. }
  2155. }
  2156. }
  2157. /* Update for next block */
  2158. #if TRACE > 2
  2159. av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
  2160. " cbpcy=%i%i%i%i\n", mb_offset,
  2161. p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
  2162. previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
  2163. cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
  2164. #endif
  2165. *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
  2166. *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
  2167. mb_offset++;
  2168. }
  2169. }
  2170. return 0;
  2171. }
  2172. /** Decode all MBs for an P frame in Simple/Main profile
  2173. * @todo TODO: Process the blocks
  2174. * @todo TODO: Use M$ MPEG-4 cbp prediction
  2175. */
  2176. static int decode_b_mbs(VC9Context *v)
  2177. {
  2178. MpegEncContext *s = &v->s;
  2179. GetBitContext *gb = &v->s.gb;
  2180. int mb_offset = 0, i /* MB / B postion information */;
  2181. int b_mv_type = BMV_TYPE_BACKWARD;
  2182. int mquant, mqdiff; /* MB quant stuff */
  2183. int ttmb; /* MacroBlock transform type */
  2184. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  2185. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  2186. int mb_has_coeffs = 1; /* last_flag */
  2187. int dmv1_x, dmv1_y, dmv2_x, dmv2_y; /* Differential MV components */
  2188. int k_x, k_y; /* Long MV fixed bitlength */
  2189. int hpel_flag; /* Some MB properties */
  2190. int index, index1; /* LUT indices */
  2191. int val, sign; /* MVDATA temp values */
  2192. /* Select proper long MV range */
  2193. switch (v->mvrange)
  2194. {
  2195. case 1: k_x = 10; k_y = 9; break;
  2196. case 2: k_x = 12; k_y = 10; break;
  2197. case 3: k_x = 13; k_y = 11; break;
  2198. default: /*case 0 too */ k_x = 9; k_y = 8; break;
  2199. }
  2200. hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
  2201. k_x -= hpel_flag;
  2202. k_y -= hpel_flag;
  2203. /* Select ttmb table depending on pq */
  2204. if (v->pq < 5)
  2205. {
  2206. v->tt_index = 0;
  2207. v->ttblk4x4 = 3;
  2208. }
  2209. else if (v->pq < 13)
  2210. {
  2211. v->tt_index = 1;
  2212. v->ttblk4x4 = 3;
  2213. }
  2214. else
  2215. {
  2216. v->tt_index = 2;
  2217. v->ttblk4x4 = 2;
  2218. }
  2219. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2220. {
  2221. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
  2222. {
  2223. if (v->direct_mb_plane.is_raw)
  2224. v->direct_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2225. if (v->skip_mb_plane.is_raw)
  2226. v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
  2227. if (!v->direct_mb_plane.data[mb_offset])
  2228. {
  2229. if (v->skip_mb_plane.data[mb_offset])
  2230. {
  2231. b_mv_type = decode012(gb);
  2232. if (v->bfraction > 420 /*1/2*/ &&
  2233. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  2234. }
  2235. else
  2236. {
  2237. GET_MVDATA(dmv1_x, dmv1_y);
  2238. if (!s->mb_intra /* b_mv1 tells not intra */)
  2239. {
  2240. b_mv_type = decode012(gb);
  2241. if (v->bfraction > 420 /*1/2*/ &&
  2242. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  2243. }
  2244. }
  2245. }
  2246. if (!v->skip_mb_plane.data[mb_offset])
  2247. {
  2248. if (mb_has_coeffs /* BMV1 == "last" */)
  2249. {
  2250. GET_MQUANT();
  2251. if (s->mb_intra /* intra mb */)
  2252. s->ac_pred = get_bits(gb, 1);
  2253. }
  2254. else
  2255. {
  2256. /* if bmv1 tells MVs are interpolated */
  2257. if (b_mv_type == BMV_TYPE_INTERPOLATED)
  2258. {
  2259. GET_MVDATA(dmv2_x, dmv2_y);
  2260. }
  2261. /* GET_MVDATA has reset some stuff */
  2262. if (mb_has_coeffs /* b_mv2 == "last" */)
  2263. {
  2264. if (s->mb_intra /* intra_mb */)
  2265. s->ac_pred = get_bits(gb, 1);
  2266. GET_MQUANT();
  2267. }
  2268. }
  2269. }
  2270. //End1
  2271. if (v->ttmbf)
  2272. ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
  2273. VC9_TTMB_VLC_BITS, 12);
  2274. //End2
  2275. for (i=0; i<6; i++)
  2276. {
  2277. /* FIXME: process the block */
  2278. }
  2279. mb_offset++;
  2280. }
  2281. }
  2282. return 0;
  2283. }
  2284. /** @} */ //End for group std_mb
  2285. #if HAS_ADVANCED_PROFILE
  2286. /***********************************************************************/
  2287. /**
  2288. * @defgroup adv_mb VC9 Macroblock-level functions in Advanced Profile
  2289. * @todo TODO: Integrate to MpegEncContext facilities
  2290. * @todo TODO: Code P, B and BI
  2291. * @{
  2292. */
  2293. static int advanced_decode_i_mbs(VC9Context *v)
  2294. {
  2295. MpegEncContext *s = &v->s;
  2296. GetBitContext *gb = &v->s.gb;
  2297. int mqdiff, mquant, mb_offset = 0, over_flags_mb = 0;
  2298. for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
  2299. {
  2300. for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
  2301. {
  2302. if (v->ac_pred_plane.is_raw)
  2303. s->ac_pred = get_bits(gb, 1);
  2304. else
  2305. s->ac_pred = v->ac_pred_plane.data[mb_offset];
  2306. if (v->condover == 3 && v->over_flags_plane.is_raw)
  2307. over_flags_mb = get_bits(gb, 1);
  2308. GET_MQUANT();
  2309. /* TODO: lots */
  2310. }
  2311. mb_offset++;
  2312. }
  2313. return 0;
  2314. }
  2315. /** @} */ //End for group adv_mb
  2316. #endif
  2317. /** Initialize a VC9/WMV3 decoder
  2318. * @todo TODO: Handle VC-9 IDUs (Transport level?)
  2319. * @todo TODO: Decypher remaining bits in extra_data
  2320. */
  2321. static int vc9_decode_init(AVCodecContext *avctx)
  2322. {
  2323. VC9Context *v = avctx->priv_data;
  2324. MpegEncContext *s = &v->s;
  2325. GetBitContext gb;
  2326. if (!avctx->extradata_size || !avctx->extradata) return -1;
  2327. avctx->pix_fmt = PIX_FMT_YUV420P;
  2328. v->s.avctx = avctx;
  2329. if(ff_h263_decode_init(avctx) < 0)
  2330. return -1;
  2331. if (vc9_init_common(v) < 0) return -1;
  2332. avctx->coded_width = avctx->width;
  2333. avctx->coded_height = avctx->height;
  2334. if (avctx->codec_id == CODEC_ID_WMV3)
  2335. {
  2336. int count = 0;
  2337. // looks like WMV3 has a sequence header stored in the extradata
  2338. // advanced sequence header may be before the first frame
  2339. // the last byte of the extradata is a version number, 1 for the
  2340. // samples we can decode
  2341. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  2342. if (decode_sequence_header(avctx, &gb) < 0)
  2343. return -1;
  2344. count = avctx->extradata_size*8 - get_bits_count(&gb);
  2345. if (count>0)
  2346. {
  2347. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  2348. count, get_bits(&gb, count));
  2349. }
  2350. else if (count < 0)
  2351. {
  2352. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  2353. }
  2354. }
  2355. avctx->has_b_frames= !!(avctx->max_b_frames);
  2356. s->mb_width = (avctx->coded_width+15)>>4;
  2357. s->mb_height = (avctx->coded_height+15)>>4;
  2358. /* Allocate mb bitplanes */
  2359. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2360. return -1;
  2361. if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
  2362. return -1;
  2363. if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
  2364. return -1;
  2365. if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
  2366. return -1;
  2367. /* For predictors */
  2368. v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
  2369. if (!v->previous_line_cbpcy) return -1;
  2370. #if HAS_ADVANCED_PROFILE
  2371. if (v->profile == PROFILE_ADVANCED)
  2372. {
  2373. if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  2374. return -1;
  2375. if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  2376. return -1;
  2377. }
  2378. #endif
  2379. return 0;
  2380. }
  2381. /** Decode a VC9/WMV3 frame
  2382. * @todo TODO: Handle VC-9 IDUs (Transport level?)
  2383. * @warning Initial try at using MpegEncContext stuff
  2384. */
  2385. static int vc9_decode_frame(AVCodecContext *avctx,
  2386. void *data, int *data_size,
  2387. uint8_t *buf, int buf_size)
  2388. {
  2389. VC9Context *v = avctx->priv_data;
  2390. MpegEncContext *s = &v->s;
  2391. int ret = FRAME_SKIPED, len, start_code;
  2392. AVFrame *pict = data;
  2393. uint8_t *tmp_buf;
  2394. v->s.avctx = avctx;
  2395. //buf_size = 0 -> last frame
  2396. if (!buf_size) return 0;
  2397. len = avpicture_get_size(avctx->pix_fmt, avctx->width,
  2398. avctx->height);
  2399. tmp_buf = (uint8_t *)av_mallocz(len);
  2400. avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt,
  2401. avctx->width, avctx->height);
  2402. if (avctx->codec_id == CODEC_ID_VC9)
  2403. {
  2404. #if 0
  2405. // search for IDU's
  2406. // FIXME
  2407. uint32_t scp = 0;
  2408. int scs = 0, i = 0;
  2409. while (i < buf_size)
  2410. {
  2411. for (; i < buf_size && scp != 0x000001; i++)
  2412. scp = ((scp<<8)|buf[i])&0xffffff;
  2413. if (scp != 0x000001)
  2414. break; // eof ?
  2415. scs = buf[i++];
  2416. init_get_bits(gb, buf+i, (buf_size-i)*8);
  2417. switch(scs)
  2418. {
  2419. case 0x0A: //Sequence End Code
  2420. return 0;
  2421. case 0x0B: //Slice Start Code
  2422. av_log(avctx, AV_LOG_ERROR, "Slice coding not supported\n");
  2423. return -1;
  2424. case 0x0C: //Field start code
  2425. av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
  2426. return -1;
  2427. case 0x0D: //Frame start code
  2428. break;
  2429. case 0x0E: //Entry point Start Code
  2430. if (v->profile < PROFILE_ADVANCED)
  2431. av_log(avctx, AV_LOG_ERROR,
  2432. "Found an entry point in profile %i\n", v->profile);
  2433. advanced_entry_point_process(avctx, gb);
  2434. break;
  2435. case 0x0F: //Sequence header Start Code
  2436. decode_sequence_header(avctx, gb);
  2437. break;
  2438. default:
  2439. av_log(avctx, AV_LOG_ERROR,
  2440. "Unsupported IDU suffix %lX\n", scs);
  2441. }
  2442. i += get_bits_count(gb)*8;
  2443. }
  2444. #else
  2445. av_abort();
  2446. #endif
  2447. }
  2448. else
  2449. init_get_bits(&v->s.gb, buf, buf_size*8);
  2450. s->flags= avctx->flags;
  2451. s->flags2= avctx->flags2;
  2452. /* no supplementary picture */
  2453. if (buf_size == 0) {
  2454. /* special case for last picture */
  2455. if (s->low_delay==0 && s->next_picture_ptr) {
  2456. *pict= *(AVFrame*)s->next_picture_ptr;
  2457. s->next_picture_ptr= NULL;
  2458. *data_size = sizeof(AVFrame);
  2459. }
  2460. return 0;
  2461. }
  2462. //No IDU - we mimic ff_h263_decode_frame
  2463. s->bitstream_buffer_size=0;
  2464. if (!s->context_initialized) {
  2465. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  2466. return -1;
  2467. }
  2468. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  2469. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  2470. s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)];
  2471. }
  2472. #if HAS_ADVANCED_PROFILE
  2473. if (v->profile == PROFILE_ADVANCED)
  2474. ret= advanced_decode_picture_primary_header(v);
  2475. else
  2476. #endif
  2477. ret= standard_decode_picture_primary_header(v);
  2478. if (ret == FRAME_SKIPED) return buf_size;
  2479. /* skip if the header was thrashed */
  2480. if (ret < 0){
  2481. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  2482. return -1;
  2483. }
  2484. //No bug workaround yet, no DCT conformance
  2485. //WMV9 does have resized images
  2486. if (v->profile < PROFILE_ADVANCED && v->multires){
  2487. //Parse context stuff in here, don't know how appliable it is
  2488. }
  2489. //Not sure about context initialization
  2490. // for hurry_up==5
  2491. s->current_picture.pict_type= s->pict_type;
  2492. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2493. /* skip b frames if we dont have reference frames */
  2494. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable))
  2495. return buf_size; //FIXME simulating all buffer consumed
  2496. /* skip b frames if we are in a hurry */
  2497. if(avctx->hurry_up && s->pict_type==B_TYPE)
  2498. return buf_size; //FIXME simulating all buffer consumed
  2499. /* skip everything if we are in a hurry>=5 */
  2500. if(avctx->hurry_up>=5)
  2501. return buf_size; //FIXME simulating all buffer consumed
  2502. if(s->next_p_frame_damaged){
  2503. if(s->pict_type==B_TYPE)
  2504. return buf_size; //FIXME simulating all buffer consumed
  2505. else
  2506. s->next_p_frame_damaged=0;
  2507. }
  2508. if(MPV_frame_start(s, avctx) < 0)
  2509. return -1;
  2510. ff_er_frame_start(s);
  2511. //wmv9 may or may not have skip bits
  2512. #if HAS_ADVANCED_PROFILE
  2513. if (v->profile == PROFILE_ADVANCED)
  2514. ret= advanced_decode_picture_secondary_header(v);
  2515. else
  2516. #endif
  2517. ret = standard_decode_picture_secondary_header(v);
  2518. if (ret<0) return FRAME_SKIPED; //FIXME Non fatal for now
  2519. //We consider the image coded in only one slice
  2520. #if HAS_ADVANCED_PROFILE
  2521. if (v->profile == PROFILE_ADVANCED)
  2522. {
  2523. switch(s->pict_type)
  2524. {
  2525. case I_TYPE: ret = advanced_decode_i_mbs(v); break;
  2526. case P_TYPE: ret = decode_p_mbs(v); break;
  2527. case B_TYPE:
  2528. case BI_TYPE: ret = decode_b_mbs(v); break;
  2529. default: ret = FRAME_SKIPED;
  2530. }
  2531. if (ret == FRAME_SKIPED) return buf_size; //We ignore for now failures
  2532. }
  2533. else
  2534. #endif
  2535. {
  2536. switch(s->pict_type)
  2537. {
  2538. case I_TYPE: ret = standard_decode_i_mbs(v); break;
  2539. case P_TYPE: ret = decode_p_mbs(v); break;
  2540. case B_TYPE:
  2541. case BI_TYPE: ret = decode_b_mbs(v); break;
  2542. default: ret = FRAME_SKIPED;
  2543. }
  2544. if (ret == FRAME_SKIPED) return buf_size;
  2545. }
  2546. ff_er_frame_end(s);
  2547. MPV_frame_end(s);
  2548. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  2549. assert(s->current_picture.pict_type == s->pict_type);
  2550. if(s->pict_type==B_TYPE || s->low_delay){
  2551. *pict= *(AVFrame*)&s->current_picture;
  2552. ff_print_debug_info(s, pict);
  2553. } else {
  2554. *pict= *(AVFrame*)&s->last_picture;
  2555. if(pict)
  2556. ff_print_debug_info(s, pict);
  2557. }
  2558. /* Return the Picture timestamp as the frame number */
  2559. /* we substract 1 because it is added on utils.c */
  2560. avctx->frame_number = s->picture_number - 1;
  2561. /* dont output the last pic after seeking */
  2562. if(s->last_picture_ptr || s->low_delay)
  2563. *data_size = sizeof(AVFrame);
  2564. av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
  2565. get_bits_count(&s->gb), buf_size*8);
  2566. /* Fake consumption of all data */
  2567. *data_size = len;
  2568. return buf_size; //Number of bytes consumed
  2569. }
  2570. /** Close a VC9/WMV3 decoder
  2571. * @warning Initial try at using MpegEncContext stuff
  2572. */
  2573. static int vc9_decode_end(AVCodecContext *avctx)
  2574. {
  2575. VC9Context *v = avctx->priv_data;
  2576. #if HAS_ADVANCED_PROFILE
  2577. av_freep(&v->hrd_rate);
  2578. av_freep(&v->hrd_buffer);
  2579. #endif
  2580. MPV_common_end(&v->s);
  2581. free_bitplane(&v->mv_type_mb_plane);
  2582. free_bitplane(&v->skip_mb_plane);
  2583. free_bitplane(&v->direct_mb_plane);
  2584. return 0;
  2585. }
  2586. AVCodec vc9_decoder = {
  2587. "vc9",
  2588. CODEC_TYPE_VIDEO,
  2589. CODEC_ID_VC9,
  2590. sizeof(VC9Context),
  2591. vc9_decode_init,
  2592. NULL,
  2593. vc9_decode_end,
  2594. vc9_decode_frame,
  2595. CODEC_CAP_DELAY,
  2596. NULL
  2597. };
  2598. AVCodec wmv3_decoder = {
  2599. "wmv3",
  2600. CODEC_TYPE_VIDEO,
  2601. CODEC_ID_WMV3,
  2602. sizeof(VC9Context),
  2603. vc9_decode_init,
  2604. NULL,
  2605. vc9_decode_end,
  2606. vc9_decode_frame,
  2607. CODEC_CAP_DELAY,
  2608. NULL
  2609. };