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.

2647 lines
81KB

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