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.

2618 lines
80KB

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