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.

2445 lines
78KB

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