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.

2007 lines
65KB

  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. extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
  35. extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
  36. /* Some inhibiting stuff */
  37. #define HAS_ADVANCED_PROFILE 1
  38. #define TRACE 1
  39. #if TRACE
  40. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  41. codes, codes_wrap, codes_size, use_static) \
  42. if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  43. codes, codes_wrap, codes_size, use_static) < 0) \
  44. { \
  45. av_log(v->avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \
  46. return -1; \
  47. }
  48. #else
  49. # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  50. codes, codes_wrap, codes_size, use_static) \
  51. init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
  52. codes, codes_wrap, codes_size, use_static)
  53. #endif
  54. #define PROFILE_SIMPLE 0
  55. #define PROFILE_MAIN 1
  56. #define PROFILE_ADVANCED 3
  57. #define QUANT_FRAME_IMPLICIT 0
  58. #define QUANT_FRAME_EXPLICIT 1
  59. #define QUANT_NON_UNIFORM 2
  60. #define QUANT_UNIFORM 3
  61. /* Where quant can be changed */
  62. #define DQPROFILE_FOUR_EDGES 0
  63. #define DQPROFILE_DOUBLE_EDGES 1
  64. #define DQPROFILE_SINGLE_EDGE 2
  65. #define DQPROFILE_ALL_MBS 3
  66. /* Which edge is quantized with ALTPQUANT */
  67. #define DQSINGLE_BEDGE_LEFT 0
  68. #define DQSINGLE_BEDGE_TOP 1
  69. #define DQSINGLE_BEDGE_RIGHT 2
  70. #define DQSINGLE_BEDGE_BOTTOM 3
  71. /* Which pair of edges is quantized with ALTPQUANT */
  72. #define DQDOUBLE_BEDGE_TOPLEFT 0
  73. #define DQDOUBLE_BEDGE_TOPRIGHT 1
  74. #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
  75. #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
  76. /* MV P modes */
  77. #define MV_PMODE_1MV_HPEL_BILIN 0
  78. #define MV_PMODE_1MV 1
  79. #define MV_PMODE_1MV_HPEL 2
  80. #define MV_PMODE_MIXED_MV 3
  81. #define MV_PMODE_INTENSITY_COMP 4
  82. #define BMV_TYPE_BACKWARD 0
  83. #define BMV_TYPE_FORWARD 1
  84. #define BMV_TYPE_INTERPOLATED 3
  85. /* MV P mode - the 5th element is only used for mode 1 */
  86. static const uint8_t mv_pmode_table[2][5] = {
  87. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
  88. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
  89. };
  90. /* One more frame type */
  91. #define BI_TYPE 7
  92. /* FIXME Worse than ugly */
  93. static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
  94. fps_dr[2] = { 1000, 1001 };
  95. static const uint8_t pquant_table[3][32] = {
  96. { /* Implicit quantizer */
  97. 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
  98. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
  99. },
  100. { /* Explicit quantizer, pquantizer uniform */
  101. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  102. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  103. },
  104. { /* Explicit quantizer, pquantizer non-uniform */
  105. 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  106. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
  107. }
  108. };
  109. // FIXME move this into the context
  110. #define VC9_BFRACTION_VLC_BITS 7
  111. static VLC vc9_bfraction_vlc;
  112. #define VC9_IMODE_VLC_BITS 4
  113. static VLC vc9_imode_vlc;
  114. #define VC9_NORM2_VLC_BITS 3
  115. static VLC vc9_norm2_vlc;
  116. #define VC9_NORM6_VLC_BITS 9
  117. static VLC vc9_norm6_vlc;
  118. /* Could be optimized, one table only needs 8 bits */
  119. #define VC9_TTMB_VLC_BITS 9 //12
  120. static VLC vc9_ttmb_vlc[3];
  121. #define VC9_MV_DIFF_VLC_BITS 9 //15
  122. static VLC vc9_mv_diff_vlc[4];
  123. #define VC9_CBPCY_I_VLC_BITS 9 //13
  124. static VLC vc9_cbpcy_i_vlc;
  125. #define VC9_CBPCY_P_VLC_BITS 9 //14
  126. static VLC vc9_cbpcy_p_vlc[4];
  127. #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
  128. static VLC vc9_4mv_block_pattern_vlc[4];
  129. #define VC9_DC_VLC_BITS 9
  130. static VLC vc9_luma_dc_vlc[2];
  131. static VLC vc9_chroma_dc_vlc[2];
  132. //We mainly need data and is_raw, so this struct could be avoided
  133. //to save a level of indirection; feel free to modify
  134. typedef struct BitPlane {
  135. uint8_t *data;
  136. int width, stride;
  137. int height;
  138. uint8_t is_raw;
  139. } BitPlane;
  140. typedef struct VC9Context{
  141. /* No MpegEnc context, might be good to use it */
  142. GetBitContext gb;
  143. AVCodecContext *avctx;
  144. /***************************/
  145. /* Sequence Header */
  146. /***************************/
  147. /* Simple/Main Profile */
  148. int res_sm; //reserved, 2b
  149. int res_x8; //reserved
  150. int multires; //frame-level RESPIC syntax element present
  151. int res_fasttx; //always 1
  152. int res_transtab; //always 0
  153. int syncmarker; //Sync markers presents
  154. int rangered; //RANGEREDFRM (range reduction) syntax element present
  155. int res_rtm_flag; //reserved, set to 1
  156. int reserved; //duh
  157. #if HAS_ADVANCED_PROFILE
  158. /* Advanced Profile */
  159. int level; //3
  160. int chromaformat; //2
  161. int postprocflag; //frame-based processing use
  162. int broadcast; //TFF/RFF present
  163. int interlace; //Progressive/interlaced (RPTFTM syntax element)
  164. int tfcntrflag; //TFCNTR present
  165. int panscanflag; //NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} presents
  166. int extended_dmv;
  167. int color_prim; //8
  168. int transfer_char; //8
  169. int matrix_coef; //8
  170. int hrd_param_flag;
  171. #endif
  172. /* All Profiles */
  173. /* TODO: move all int to flags */
  174. int profile; //2
  175. int frmrtq_postproc; //3
  176. int bitrtq_postproc; //5
  177. int loopfilter;
  178. int fastuvmc; //Rounding of qpel vector to hpel ? (not in Simple)
  179. int extended_mv; //Ext MV in P/B (not in Simple)
  180. int dquant; //Q varies with MBs, 2bits (not in Simple)
  181. int vstransform; //variable-size transform46
  182. int overlap; //overlapped transforms in use
  183. int quantizer_mode; //2, quantizer mode used for sequence, see QUANT_*
  184. int finterpflag; //INTERPFRM present
  185. /*****************************/
  186. /* Frame decoding */
  187. /*****************************/
  188. /* All profiles */
  189. uint8_t mv_mode, mv_mode2; /* MV coding mode */
  190. uint8_t pict_type; /* Picture type, mapped on MPEG types */
  191. uint8_t pq, altpq; /* Quantizers */
  192. uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */
  193. int width_mb, height_mb;
  194. int tile; /* 3x2 if (width_mb%3) else 2x3 */
  195. VLC *luma_ac_vlc, *chroma_ac_vlc,
  196. *luma_dc_vlc, *chroma_dc_vlc; /* transac/dcfrm bits are indexes */
  197. uint8_t ttmbf, ttfrm; /* Transform type */
  198. uint8_t lumscale, lumshift; /* Luma compensation parameters */
  199. int16_t bfraction; /* Relative position % anchors=> how to scale MVs */
  200. uint8_t halfpq; /* Uniform quant over image and qp+.5 */
  201. uint8_t respic;
  202. /* Ranges:
  203. * 0 -> [-64n 63.f] x [-32, 31.f]
  204. * 1 -> [-128, 127.f] x [-64, 63.f]
  205. * 2 -> [-512, 511.f] x [-128, 127.f]
  206. * 3 -> [-1024, 1023.f] x [-256, 255.f]
  207. */
  208. uint8_t mvrange;
  209. uint8_t pquantizer;
  210. uint8_t *previous_line_cbpcy; /* To use for predicted CBPCY */
  211. VLC *cbpcy_vlc /* Current CBPCY VLC table */,
  212. *mv_diff_vlc /* Current MV Diff VLC table */,
  213. *ttmb_vlc /* Current MB Transform Type VLC table */;
  214. BitPlane mv_type_mb_plane; /* bitplane for mv_type == (4MV) */
  215. BitPlane skip_mb_plane, /* bitplane for skipped MBs */
  216. direct_mb_plane; /* bitplane for "direct" MBs */
  217. /* S/M only ? */
  218. uint8_t rangeredfrm; /* out_sample = CLIP((in_sample-128)*2+128) */
  219. uint8_t interpfrm;
  220. #if HAS_ADVANCED_PROFILE
  221. /* Advanced */
  222. uint8_t fcm; //0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  223. uint8_t numpanscanwin;
  224. uint8_t tfcntr;
  225. uint8_t rptfrm, tff, rff;
  226. uint8_t topleftx;
  227. uint8_t toplefty;
  228. uint8_t bottomrightx;
  229. uint8_t bottomrighty;
  230. uint8_t rndctrl;
  231. uint8_t uvsamp;
  232. uint8_t postproc;
  233. int hrd_num_leaky_buckets;
  234. uint8_t bit_rate_exponent;
  235. uint8_t buffer_size_exponent;
  236. BitPlane ac_pred_plane; //AC prediction flags bitplane
  237. BitPlane over_flags_plane; //Overflags bitplane
  238. uint8_t condover;
  239. uint16_t *hrd_rate, *hrd_buffer;
  240. VLC *luma_ac2_vlc, *chroma_ac2_vlc;
  241. #endif
  242. } VC9Context;
  243. /* FIXME Slow and ugly */
  244. static int get_prefix(GetBitContext *gb, int stop, int len)
  245. {
  246. #if 1
  247. int i = 0, tmp = !stop;
  248. while (i != len && tmp != stop)
  249. {
  250. tmp = get_bits(gb, 1);
  251. i++;
  252. }
  253. return i;
  254. #else
  255. unsigned int buf;
  256. int log;
  257. OPEN_READER(re, gb);
  258. UPDATE_CACHE(re, gb);
  259. buf=GET_CACHE(re, gb); //Still not sure
  260. if (stop) buf = ~buf;
  261. log= av_log2(-buf); //FIXME: -?
  262. if (log < limit){
  263. LAST_SKIP_BITS(re, gb, log+1);
  264. CLOSE_READER(re, gb);
  265. return log;
  266. }
  267. LAST_SKIP_BITS(re, gb, limit);
  268. CLOSE_READER(re, gb);
  269. return limit;
  270. #endif
  271. }
  272. static int init_common(VC9Context *v)
  273. {
  274. static int done = 0;
  275. int i;
  276. /* Set the bit planes */
  277. /* FIXME memset better ? (16bytes) */
  278. v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  279. v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  280. v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  281. #if HAS_ADVANCED_PROFILE
  282. v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
  283. v->hrd_rate = v->hrd_buffer = NULL;
  284. #endif
  285. /* VLC tables */
  286. #if 0 // spec -> actual tables converter
  287. for(i=0; i<64; i++){
  288. int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
  289. av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
  290. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  291. }
  292. for(i=0; i<64; i++){
  293. int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
  294. av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
  295. if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  296. }
  297. #endif
  298. if(!done)
  299. {
  300. done = 1;
  301. INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
  302. vc9_bfraction_bits, 1, 1,
  303. vc9_bfraction_codes, 1, 1, 1);
  304. INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
  305. vc9_norm2_bits, 1, 1,
  306. vc9_norm2_codes, 1, 1, 1);
  307. INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
  308. vc9_norm6_bits, 1, 1,
  309. vc9_norm6_codes, 2, 2, 1);
  310. INIT_VLC(&vc9_cbpcy_i_vlc, VC9_CBPCY_I_VLC_BITS, 64,
  311. vc9_cbpcy_i_bits, 1, 1,
  312. vc9_cbpcy_i_codes, 2, 2, 1);
  313. INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
  314. vc9_imode_bits, 1, 1,
  315. vc9_imode_codes, 1, 1, 1);
  316. INIT_VLC(&vc9_luma_dc_vlc[0], VC9_DC_VLC_BITS, 120,
  317. &ff_table0_dc_lum[0][1], 8, 4,
  318. &ff_table0_dc_lum[0][0], 8, 4, 1);
  319. INIT_VLC(&vc9_chroma_dc_vlc[0], VC9_DC_VLC_BITS, 120,
  320. &ff_table0_dc_chroma[0][1], 8, 4,
  321. &ff_table0_dc_chroma[0][0], 8, 4, 1);
  322. INIT_VLC(&vc9_luma_dc_vlc[1], VC9_DC_VLC_BITS, 120,
  323. &ff_table1_dc_lum[0][1], 8, 4,
  324. &ff_table1_dc_lum[0][0], 8, 4, 1);
  325. INIT_VLC(&vc9_chroma_dc_vlc[1], VC9_DC_VLC_BITS, 120,
  326. &ff_table1_dc_chroma[0][1], 8, 4,
  327. &ff_table1_dc_chroma[0][0], 8, 4, 1);
  328. for (i=0; i<3; i++)
  329. {
  330. INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
  331. vc9_ttmb_bits[i], 1, 1,
  332. vc9_ttmb_codes[i], 2, 2, 1);
  333. }
  334. for(i=0; i<4; i++)
  335. {
  336. INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
  337. vc9_4mv_block_pattern_bits[i], 1, 1,
  338. vc9_4mv_block_pattern_codes[i], 1, 1, 1);
  339. INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
  340. vc9_cbpcy_p_bits[i], 1, 1,
  341. vc9_cbpcy_p_codes[i], 2, 2, 1);
  342. INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
  343. vc9_mv_diff_bits[i], 1, 1,
  344. vc9_mv_diff_codes[i], 2, 2, 1);
  345. }
  346. }
  347. /* Other defaults */
  348. v->pq = -1;
  349. v->mvrange = 0; /* 7.1.1.18, p80 */
  350. return 0;
  351. }
  352. #if HAS_ADVANCED_PROFILE
  353. /* 6.2.1, p32 */
  354. static int decode_hrd(VC9Context *v, GetBitContext *gb)
  355. {
  356. int i, num;
  357. num = get_bits(gb, 5);
  358. if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
  359. {
  360. av_freep(&v->hrd_rate);
  361. }
  362. if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
  363. if (!v->hrd_rate) return -1;
  364. if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
  365. {
  366. av_freep(&v->hrd_buffer);
  367. }
  368. if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
  369. if (!v->hrd_buffer) return -1;
  370. v->hrd_num_leaky_buckets = num;
  371. //exponent in base-2 for rate
  372. v->bit_rate_exponent = get_bits(gb, 4);
  373. //exponent in base-2 for buffer_size
  374. v->buffer_size_exponent = get_bits(gb, 4);
  375. for (i=0; i<num; i++)
  376. {
  377. //mantissae, ordered (if not, use a function ?
  378. v->hrd_rate[i] = get_bits(gb, 16);
  379. if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
  380. {
  381. av_log(v, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
  382. "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
  383. return -1;
  384. }
  385. v->hrd_buffer[i] = get_bits(gb, 16);
  386. if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
  387. {
  388. av_log(v, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
  389. "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
  390. return -1;
  391. }
  392. }
  393. return 0;
  394. }
  395. /* Table 2, p18 */
  396. static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  397. {
  398. VC9Context *v = avctx->priv_data;
  399. int nr, dr, aspect_ratio;
  400. v->postprocflag = get_bits(gb, 1);
  401. v->broadcast = get_bits(gb, 1);
  402. v->interlace = get_bits(gb, 1);
  403. v->tfcntrflag = get_bits(gb, 1);
  404. v->finterpflag = get_bits(gb, 1); //common
  405. v->panscanflag = get_bits(gb, 1);
  406. v->reserved = get_bits(gb, 1);
  407. if (v->reserved)
  408. {
  409. av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
  410. v->reserved);
  411. return -1;
  412. }
  413. if (v->extended_mv)
  414. v->extended_dmv = get_bits(gb, 1);
  415. /* 6.1.7, p21 */
  416. if (get_bits(gb, 1) /* pic_size_flag */)
  417. {
  418. avctx->coded_width = get_bits(gb, 12);
  419. avctx->coded_height = get_bits(gb, 12);
  420. if ( get_bits(gb, 1) /* disp_size_flag */)
  421. {
  422. avctx->width = get_bits(gb, 14);
  423. avctx->height = get_bits(gb, 14);
  424. }
  425. /* 6.1.7.4, p22 */
  426. if ( get_bits(gb, 1) /* aspect_ratio_flag */)
  427. {
  428. aspect_ratio = get_bits(gb, 4); //SAR
  429. if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
  430. {
  431. avctx->sample_aspect_ratio.num = get_bits(gb, 8);
  432. avctx->sample_aspect_ratio.den = get_bits(gb, 8);
  433. }
  434. else if (aspect_ratio == 0x0E)
  435. {
  436. av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
  437. }
  438. else
  439. {
  440. avctx->sample_aspect_ratio = vc9_pixel_aspect[aspect_ratio];
  441. }
  442. }
  443. }
  444. else
  445. {
  446. avctx->coded_width = avctx->width;
  447. avctx->coded_height = avctx->height;
  448. }
  449. /* 6.1.8, p23 */
  450. if ( get_bits(gb, 1) /* framerateflag */)
  451. {
  452. if ( get_bits(gb, 1) /* framerateind */)
  453. {
  454. nr = get_bits(gb, 8);
  455. dr = get_bits(gb, 4);
  456. if (nr<1)
  457. {
  458. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
  459. return -1;
  460. }
  461. if (nr>5)
  462. {
  463. av_log(avctx, AV_LOG_ERROR,
  464. "Reserved FRAMERATENR %i not handled\n", nr);
  465. }
  466. if (dr<1)
  467. {
  468. av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
  469. }
  470. if (dr>2)
  471. {
  472. av_log(avctx, AV_LOG_ERROR,
  473. "Reserved FRAMERATEDR %i not handled\n", dr);
  474. }
  475. avctx->frame_rate_base = fps_nr[dr];
  476. avctx->frame_rate = fps_nr[nr];
  477. }
  478. else
  479. {
  480. nr = get_bits(gb, 16);
  481. // 0.03125->2048Hz / 0.03125Hz
  482. avctx->frame_rate = 1000000;
  483. avctx->frame_rate_base = 31250*(1+nr);
  484. }
  485. }
  486. /* 6.1.9, p25 */
  487. if ( get_bits(gb, 1) /* color_format_flag */)
  488. {
  489. //Chromacity coordinates of color primaries
  490. //like ITU-R BT.709-2, BT.470-2, ...
  491. v->color_prim = get_bits(gb, 8);
  492. if (v->color_prim<1)
  493. {
  494. av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is reserved\n");
  495. return -1;
  496. }
  497. if (v->color_prim == 3 || v->color_prim>6)
  498. {
  499. av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
  500. v->color_prim);
  501. return -1;
  502. }
  503. //Opto-electronic transfer characteristics
  504. v->transfer_char = get_bits(gb, 8);
  505. if (v->transfer_char == 3 || v->transfer_char>8)
  506. {
  507. av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
  508. v->color_prim);
  509. return -1;
  510. }
  511. //Matrix coefficient for primariev->YCbCr
  512. v->matrix_coef = get_bits(gb, 8);
  513. if (v->matrix_coef < 1) return -1; //forbidden
  514. if ((v->matrix_coef>3 && v->matrix_coef<6) || v->matrix_coef>7)
  515. {
  516. av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
  517. v->color_prim);
  518. return -1;
  519. }
  520. }
  521. //Hypothetical reference decoder indicator flag
  522. v->hrd_param_flag = get_bits(gb, 1);
  523. if (v->hrd_param_flag)
  524. {
  525. if (decode_hrd(v, gb) < 0) return -1;
  526. }
  527. av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
  528. return -1;
  529. }
  530. #endif
  531. /* Figure 7-8, p16-17 */
  532. static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
  533. {
  534. VC9Context *v = avctx->priv_data;
  535. v->profile = get_bits(gb, 2);
  536. av_log(avctx, AV_LOG_DEBUG, "Profile: %i\n", v->profile);
  537. #if HAS_ADVANCED_PROFILE
  538. if (v->profile > PROFILE_MAIN)
  539. {
  540. v->level = get_bits(gb, 3);
  541. v->chromaformat = get_bits(gb, 2);
  542. if (v->chromaformat != 1)
  543. {
  544. av_log(avctx, AV_LOG_ERROR,
  545. "Only 4:2:0 chroma format supported\n");
  546. return -1;
  547. }
  548. }
  549. else
  550. #endif
  551. {
  552. v->res_sm = get_bits(gb, 2); //reserved
  553. if (v->res_sm)
  554. {
  555. av_log(avctx, AV_LOG_ERROR,
  556. "Reserved RES_SM=%i is forbidden\n", v->res_sm);
  557. //return -1;
  558. }
  559. }
  560. // (fps-2)/4 (->30)
  561. v->frmrtq_postproc = get_bits(gb, 3); //common
  562. // (bitrate-32kbps)/64kbps
  563. v->bitrtq_postproc = get_bits(gb, 5); //common
  564. v->loopfilter = get_bits(gb, 1); //common
  565. #if HAS_ADVANCED_PROFILE
  566. if (v->profile <= PROFILE_MAIN)
  567. #endif
  568. {
  569. v->res_x8 = get_bits(gb, 1); //reserved
  570. if (v->res_x8)
  571. {
  572. av_log(avctx, AV_LOG_ERROR,
  573. "1 for reserved RES_X8 is forbidden\n");
  574. return -1;
  575. }
  576. v->multires = get_bits(gb, 1);
  577. v->res_fasttx = get_bits(gb, 1);
  578. if (!v->res_fasttx)
  579. {
  580. av_log(avctx, AV_LOG_ERROR,
  581. "0 for reserved RES_FASTTX is forbidden\n");
  582. //return -1;
  583. }
  584. }
  585. v->fastuvmc = get_bits(gb, 1); //common
  586. if (!v->profile && !v->fastuvmc)
  587. {
  588. av_log(avctx, AV_LOG_ERROR,
  589. "FASTUVMC unavailable in Simple Profile\n");
  590. return -1;
  591. }
  592. v->extended_mv = get_bits(gb, 1); //common
  593. if (!v->profile && v->extended_mv)
  594. {
  595. av_log(avctx, AV_LOG_ERROR,
  596. "Extended MVs unavailable in Simple Profile\n");
  597. return -1;
  598. }
  599. v->dquant = get_bits(gb, 2); //common
  600. v->vstransform = get_bits(gb, 1); //common
  601. #if HAS_ADVANCED_PROFILE
  602. if (v->profile <= PROFILE_MAIN)
  603. #endif
  604. {
  605. v->res_transtab = get_bits(gb, 1);
  606. if (v->res_transtab)
  607. {
  608. av_log(avctx, AV_LOG_ERROR,
  609. "1 for reserved RES_TRANSTAB is forbidden\n");
  610. return -1;
  611. }
  612. }
  613. v->overlap = get_bits(gb, 1); //common
  614. #if HAS_ADVANCED_PROFILE
  615. if (v->profile <= PROFILE_MAIN)
  616. #endif
  617. {
  618. v->syncmarker = get_bits(gb, 1);
  619. v->rangered = get_bits(gb, 1);
  620. }
  621. avctx->max_b_frames = get_bits(gb, 3); //common
  622. v->quantizer_mode = get_bits(gb, 2); //common
  623. #if HAS_ADVANCED_PROFILE
  624. if (v->profile <= PROFILE_MAIN)
  625. #endif
  626. {
  627. v->finterpflag = get_bits(gb, 1); //common
  628. v->res_rtm_flag = get_bits(gb, 1); //reserved
  629. if (!v->res_rtm_flag)
  630. {
  631. av_log(avctx, AV_LOG_ERROR,
  632. "0 for reserved RES_RTM_FLAG is forbidden\n");
  633. //return -1;
  634. }
  635. #if TRACE
  636. av_log(avctx, AV_LOG_INFO,
  637. "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
  638. "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
  639. "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
  640. "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
  641. v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
  642. v->loopfilter, v->multires, v->fastuvmc, v->extended_mv,
  643. v->rangered, v->vstransform, v->overlap, v->syncmarker,
  644. v->dquant, v->quantizer_mode, avctx->max_b_frames
  645. );
  646. return 0;
  647. #endif
  648. }
  649. #if HAS_ADVANCED_PROFILE
  650. else return decode_advanced_sequence_header(avctx, gb);
  651. #endif
  652. }
  653. #if HAS_ADVANCED_PROFILE
  654. /*****************************************************************************/
  655. /* Entry point decoding (Advanced Profile) */
  656. /*****************************************************************************/
  657. static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
  658. {
  659. VC9Context *v = avctx->priv_data;
  660. int range_mapy_flag, range_mapuv_flag, i;
  661. if (v->profile != PROFILE_ADVANCED)
  662. {
  663. av_log(avctx, AV_LOG_ERROR,
  664. "Entry point are only defined in Advanced Profile!\n");
  665. return -1; //Only for advanced profile!
  666. }
  667. if (v->hrd_param_flag)
  668. {
  669. //Update buffer fullness
  670. av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
  671. for (i=0; i<v->hrd_num_leaky_buckets; i++)
  672. skip_bits(gb, 8);
  673. }
  674. if ((range_mapy_flag = get_bits(gb, 1)))
  675. {
  676. //RANGE_MAPY
  677. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
  678. skip_bits(gb, 3);
  679. }
  680. if ((range_mapuv_flag = get_bits(gb, 1)))
  681. {
  682. //RANGE_MAPUV
  683. av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
  684. skip_bits(gb, 3);
  685. }
  686. if (v->panscanflag)
  687. {
  688. //NUMPANSCANWIN
  689. v->numpanscanwin = get_bits(gb, 3);
  690. av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
  691. }
  692. return 0;
  693. }
  694. #endif
  695. /******************************************************************************/
  696. /* Bitplane decoding: 8.7, p56 */
  697. /******************************************************************************/
  698. #define IMODE_RAW 0
  699. #define IMODE_NORM2 1
  700. #define IMODE_DIFF2 2
  701. #define IMODE_NORM6 3
  702. #define IMODE_DIFF6 4
  703. #define IMODE_ROWSKIP 5
  704. #define IMODE_COLSKIP 6
  705. int alloc_bitplane(BitPlane *bp, int width, int height)
  706. {
  707. if (!bp || bp->width<0 || bp->height<0) return -1;
  708. bp->data = (uint8_t*)av_malloc(width*height);
  709. if (!bp->data) return -1;
  710. bp->width = bp->stride = width; //FIXME Needed for aligned data ?
  711. bp->height = height;
  712. return 0;
  713. }
  714. static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){
  715. int x, y;
  716. for (y=0; y<height; y++){
  717. if (!get_bits(&v->gb, 1)) //rowskip
  718. memset(plane, 0, width);
  719. else
  720. for (x=0; x<width; x++)
  721. plane[x] = get_bits(&v->gb, 1);
  722. plane += stride;
  723. }
  724. }
  725. //FIXME optimize
  726. static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){
  727. int x, y;
  728. for (x=0; x<width; x++){
  729. if (!get_bits(&v->gb, 1)) //colskip
  730. for (y=0; y<height; y++)
  731. plane[y*stride] = 0;
  732. else
  733. for (y=0; y<height; y++)
  734. plane[y*stride] = get_bits(&v->gb, 1);
  735. plane ++;
  736. }
  737. }
  738. //FIXME optimize
  739. //FIXME is this supposed to set elements to 0/FF or 0/1? 0/x!=0, not used for
  740. // prediction
  741. //FIXME Use BitPlane struct or return if table is raw (no bits read here but
  742. // later on)
  743. static int bitplane_decoding(BitPlane *bp, VC9Context *v)
  744. {
  745. int imode, x, y, code, use_vertical_tile, tile_w, tile_h;
  746. uint8_t invert, *planep = bp->data;
  747. invert = get_bits(&v->gb, 1);
  748. imode = get_vlc2(&v->gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
  749. bp->is_raw = 0;
  750. switch (imode)
  751. {
  752. case IMODE_RAW:
  753. //Data is actually read in the MB layer (same for all tests == "raw")
  754. bp->is_raw = 1; //invert ignored
  755. return invert;
  756. case IMODE_DIFF2:
  757. case IMODE_NORM2:
  758. if ((bp->height*bp->width) & 1) *(++planep) = get_bits(&v->gb, 1);
  759. for(x=0; x<(bp->height*bp->width)>>1; x++){
  760. code = get_vlc2(&v->gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
  761. *(++planep) = code&1; //lsb => left
  762. *(++planep) = code&2; //msb => right - bitplane => only !0 matters
  763. //FIXME width->stride
  764. }
  765. break;
  766. case IMODE_DIFF6:
  767. case IMODE_NORM6:
  768. use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
  769. tile_w= use_vertical_tile ? 2 : 3;
  770. tile_h= use_vertical_tile ? 3 : 2;
  771. for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
  772. for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
  773. code = get_vlc2(&v->gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
  774. if(code<0){
  775. av_log(v->avctx, AV_LOG_DEBUG, "inavlid NORM-6 VLC\n");
  776. return -1;
  777. }
  778. //FIXME following is a pure guess and probably wrong
  779. //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
  780. planep[x + 0*bp->stride]= (code>>0)&1;
  781. planep[x + 1 + 0*bp->stride]= (code>>1)&1;
  782. if(use_vertical_tile){
  783. planep[x + 0 + 1*bp->stride]= (code>>2)&1;
  784. planep[x + 1 + 1*bp->stride]= (code>>3)&1;
  785. planep[x + 0 + 2*bp->stride]= (code>>4)&1;
  786. planep[x + 1 + 2*bp->stride]= (code>>5)&1;
  787. }else{
  788. planep[x + 2 + 0*bp->stride]= (code>>2)&1;
  789. planep[x + 0 + 1*bp->stride]= (code>>3)&1;
  790. planep[x + 1 + 1*bp->stride]= (code>>4)&1;
  791. planep[x + 2 + 1*bp->stride]= (code>>5)&1;
  792. }
  793. }
  794. }
  795. x= bp->width % tile_w;
  796. decode_colskip(bp->data , x, bp->height , bp->stride, v);
  797. decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, v);
  798. break;
  799. case IMODE_ROWSKIP:
  800. decode_rowskip(bp->data, bp->width, bp->height, bp->stride, v);
  801. break;
  802. case IMODE_COLSKIP: //Teh ugly
  803. decode_colskip(bp->data, bp->width, bp->height, bp->stride, v);
  804. break;
  805. default: break;
  806. }
  807. /* Applying diff operator */
  808. if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
  809. {
  810. planep = bp->data;
  811. planep[0] ^= invert;
  812. for (x=1; x<bp->width; x++)
  813. planep[x] ^= planep[x-1];
  814. for (y=1; y<bp->height; y++)
  815. {
  816. planep += bp->stride;
  817. planep[0] ^= planep[-bp->stride];
  818. for (x=1; x<bp->width; x++)
  819. {
  820. if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
  821. else planep[x] ^= planep[x-1];
  822. }
  823. }
  824. }
  825. else if (invert)
  826. {
  827. planep = bp->data;
  828. for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
  829. }
  830. return (imode<<1) + invert;
  831. }
  832. /*****************************************************************************/
  833. /* VOP Dquant decoding */
  834. /*****************************************************************************/
  835. static int vop_dquant_decoding(VC9Context *v)
  836. {
  837. int pqdiff;
  838. //variable size
  839. if (v->dquant == 2)
  840. {
  841. pqdiff = get_bits(&v->gb, 3);
  842. if (pqdiff == 7) v->altpq = get_bits(&v->gb, 5);
  843. else v->altpq = v->pq + pqdiff + 1;
  844. }
  845. else
  846. {
  847. v->dquantfrm = get_bits(&v->gb, 1);
  848. if ( v->dquantfrm )
  849. {
  850. v->dqprofile = get_bits(&v->gb, 2);
  851. switch (v->dqprofile)
  852. {
  853. case DQPROFILE_SINGLE_EDGE:
  854. case DQPROFILE_DOUBLE_EDGES:
  855. v->dqsbedge = get_bits(&v->gb, 2);
  856. break;
  857. case DQPROFILE_ALL_MBS:
  858. v->dqbilevel = get_bits(&v->gb, 1);
  859. default: break; //Forbidden ?
  860. }
  861. if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
  862. {
  863. pqdiff = get_bits(&v->gb, 3);
  864. if (pqdiff == 7) v->altpq = get_bits(&v->gb, 5);
  865. else v->altpq = v->pq + pqdiff + 1;
  866. }
  867. }
  868. }
  869. return 0;
  870. }
  871. /*****************************************************************************/
  872. /* All Profiles picture header decoding specific functions */
  873. /* Only pro/epilog differs between Simple/Main and Advanced => check caller */
  874. /*****************************************************************************/
  875. static int decode_bi_picture_header(VC9Context *v)
  876. {
  877. /* Very particular case:
  878. - for S/M Profiles, decode_b_picture_header reads BF,
  879. bfraction then determine if this is a BI frame, calling
  880. this function afterwards
  881. - for A Profile, PTYPE already tells so and we can go
  882. directly there
  883. */
  884. int pqindex;
  885. /* Read the quantization stuff */
  886. pqindex = get_bits(&v->gb, 5);
  887. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  888. v->pq = pquant_table[0][pqindex];
  889. else
  890. {
  891. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  892. }
  893. if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1);
  894. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  895. v->pquantizer = get_bits(&v->gb, 1);
  896. /* Read the MV type/mode */
  897. if (v->extended_mv == 1)
  898. v->mvrange = get_prefix(&v->gb, 0, 3);
  899. /* FIXME: what table are used in that case ? */
  900. v->mv_diff_vlc = &vc9_mv_diff_vlc[0];
  901. v->cbpcy_vlc = &vc9_cbpcy_i_vlc;
  902. av_log(v->avctx, AV_LOG_DEBUG, "B frame, QP=%i\n", v->pq);
  903. av_log(v->avctx, AV_LOG_ERROR, "BI_TYPE not supported yet\n");
  904. /* Epilog should be done in caller */
  905. return -1;
  906. }
  907. /* Tables 11+12, p62-65 */
  908. static int decode_b_picture_header(VC9Context *v)
  909. {
  910. int pqindex, status;
  911. /* Prolog common to all frametypes should be done in caller */
  912. if (v->profile == PROFILE_SIMPLE)
  913. {
  914. av_log(v, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
  915. return FRAME_SKIPED;
  916. }
  917. v->bfraction = vc9_bfraction_lut[get_vlc2(&v->gb, vc9_bfraction_vlc.table,
  918. VC9_BFRACTION_VLC_BITS, 2)];
  919. if (v->bfraction < -1)
  920. {
  921. av_log(v, AV_LOG_ERROR, "Invalid BFRaction\n");
  922. return FRAME_SKIPED;
  923. }
  924. else if (!v->bfraction)
  925. {
  926. /* We actually have a BI frame */
  927. return decode_bi_picture_header(v);
  928. }
  929. /* Read the quantization stuff */
  930. pqindex = get_bits(&v->gb, 5);
  931. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  932. v->pq = pquant_table[0][pqindex];
  933. else
  934. {
  935. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  936. }
  937. if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1);
  938. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  939. v->pquantizer = get_bits(&v->gb, 1);
  940. /* Read the MV type/mode */
  941. if (v->extended_mv == 1)
  942. v->mvrange = get_prefix(&v->gb, 0, 3);
  943. v->mv_mode = get_bits(&v->gb, 1);
  944. if (v->pq < 13)
  945. {
  946. if (!v->mv_mode)
  947. {
  948. v->mv_mode = get_bits(&v->gb, 2);
  949. if (v->mv_mode)
  950. av_log(v, AV_LOG_ERROR,
  951. "mv_mode for lowquant B frame was %i\n", v->mv_mode);
  952. }
  953. }
  954. else
  955. {
  956. if (!v->mv_mode)
  957. {
  958. if (get_bits(&v->gb, 1))
  959. av_log(v, AV_LOG_ERROR,
  960. "mv_mode for highquant B frame was %i\n", v->mv_mode);
  961. }
  962. v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
  963. }
  964. if (v->mv_mode == MV_PMODE_MIXED_MV)
  965. {
  966. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  967. if (status < 0)
  968. return -1;
  969. #if TRACE
  970. av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  971. "Imode: %i, Invert: %i\n", status>>1, status&1);
  972. #endif
  973. }
  974. //bitplane
  975. status = bitplane_decoding(&v->direct_mb_plane, v);
  976. if (status < 0) return -1;
  977. #if TRACE
  978. av_log(v->avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
  979. "Imode: %i, Invert: %i\n", status>>1, status&1);
  980. #endif
  981. bitplane_decoding(&v->skip_mb_plane, v);
  982. if (status < 0) return -1;
  983. #if TRACE
  984. av_log(v->avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
  985. "Imode: %i, Invert: %i\n", status>>1, status&1);
  986. #endif
  987. /* FIXME: what is actually chosen for B frames ? */
  988. v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)];
  989. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)];
  990. if (v->dquant)
  991. {
  992. vop_dquant_decoding(v);
  993. }
  994. if (v->vstransform)
  995. {
  996. v->ttmbf = get_bits(&v->gb, 1);
  997. if (v->ttmbf)
  998. {
  999. v->ttfrm = get_bits(&v->gb, 2);
  1000. av_log(v, AV_LOG_INFO, "Transform used: %ix%i\n",
  1001. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1002. }
  1003. }
  1004. /* Epilog should be done in caller */
  1005. return 0;
  1006. }
  1007. /* Tables 5+7, p53-54 and 55-57 */
  1008. static int decode_i_picture_header(VC9Context *v)
  1009. {
  1010. int pqindex, status = 0, ac_pred;
  1011. /* Prolog common to all frametypes should be done in caller */
  1012. //BF = Buffer Fullness
  1013. if (v->profile <= PROFILE_MAIN && get_bits(&v->gb, 7))
  1014. {
  1015. av_log(v, AV_LOG_DEBUG, "I BufferFullness not 0\n");
  1016. }
  1017. /* Quantizer stuff */
  1018. pqindex = get_bits(&v->gb, 5);
  1019. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1020. v->pq = pquant_table[0][pqindex];
  1021. else
  1022. {
  1023. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1024. }
  1025. if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1);
  1026. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1027. v->pquantizer = get_bits(&v->gb, 1);
  1028. av_log(v->avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n",
  1029. v->pq, v->halfpq);
  1030. #if HAS_ADVANCED_PROFILE
  1031. if (v->profile <= PROFILE_MAIN)
  1032. #endif
  1033. {
  1034. if (v->extended_mv) v->mvrange = get_prefix(&v->gb, 0, 3);
  1035. if (v->multires) v->respic = get_bits(&v->gb, 2);
  1036. }
  1037. #if HAS_ADVANCED_PROFILE
  1038. else
  1039. {
  1040. ac_pred = get_bits(&v->gb, 1);
  1041. if (v->postprocflag) v->postproc = get_bits(&v->gb, 1);
  1042. /* 7.1.1.34 + 8.5.2 */
  1043. if (v->overlap && v->pq<9)
  1044. {
  1045. v->condover = get_bits(&v->gb, 1);
  1046. if (v->condover)
  1047. {
  1048. v->condover = 2+get_bits(&v->gb, 1);
  1049. if (v->condover == 3)
  1050. {
  1051. status = bitplane_decoding(&v->over_flags_plane, v);
  1052. if (status < 0) return -1;
  1053. #if TRACE
  1054. av_log(v->avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
  1055. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1056. #endif
  1057. }
  1058. }
  1059. }
  1060. }
  1061. #endif
  1062. /* Epilog should be done in caller */
  1063. return status;
  1064. }
  1065. /* Table 9, p58-60 */
  1066. static int decode_p_picture_header(VC9Context *v)
  1067. {
  1068. /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
  1069. int lowquant, pqindex, status = 0;
  1070. pqindex = get_bits(&v->gb, 5);
  1071. if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
  1072. v->pq = pquant_table[0][pqindex];
  1073. else
  1074. {
  1075. v->pq = pquant_table[v->quantizer_mode-1][pqindex];
  1076. }
  1077. if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1);
  1078. if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
  1079. v->pquantizer = get_bits(&v->gb, 1);
  1080. av_log(v->avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n",
  1081. v->pq, v->halfpq);
  1082. if (v->extended_mv == 1) v->mvrange = get_prefix(&v->gb, 0, 3);
  1083. #if HAS_ADVANCED_PROFILE
  1084. if (v->profile > PROFILE_MAIN)
  1085. {
  1086. if (v->postprocflag) v->postproc = get_bits(&v->gb, 1);
  1087. }
  1088. else
  1089. #endif
  1090. if (v->multires) v->respic = get_bits(&v->gb, 2);
  1091. lowquant = (v->pquantizer>12) ? 0 : 1;
  1092. v->mv_mode = mv_pmode_table[lowquant][get_prefix(&v->gb, 1, 4)];
  1093. if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
  1094. {
  1095. v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(&v->gb, 1, 3)];
  1096. v->lumscale = get_bits(&v->gb, 6);
  1097. v->lumshift = get_bits(&v->gb, 6);
  1098. }
  1099. if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
  1100. v->mv_mode2 == MV_PMODE_MIXED_MV)
  1101. || v->mv_mode == MV_PMODE_MIXED_MV)
  1102. {
  1103. status = bitplane_decoding(&v->mv_type_mb_plane, v);
  1104. if (status < 0) return -1;
  1105. #if TRACE
  1106. av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
  1107. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1108. #endif
  1109. }
  1110. status = bitplane_decoding(&v->skip_mb_plane, v);
  1111. if (status < 0) return -1;
  1112. #if TRACE
  1113. av_log(v->avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
  1114. "Imode: %i, Invert: %i\n", status>>1, status&1);
  1115. #endif
  1116. /* Hopefully this is correct for P frames */
  1117. v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)];
  1118. v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)];
  1119. if (v->dquant)
  1120. {
  1121. av_log(v->avctx, AV_LOG_INFO, "VOP DQuant info\n");
  1122. vop_dquant_decoding(v);
  1123. }
  1124. if (v->vstransform)
  1125. {
  1126. v->ttmbf = get_bits(&v->gb, 1);
  1127. if (v->ttmbf)
  1128. {
  1129. v->ttfrm = get_bits(&v->gb, 2);
  1130. av_log(v->avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
  1131. (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
  1132. }
  1133. }
  1134. /* Epilog should be done in caller */
  1135. return 0;
  1136. }
  1137. static int standard_decode_picture_header(VC9Context *v)
  1138. {
  1139. int status = 0, index;
  1140. if (v->finterpflag) v->interpfrm = get_bits(&v->gb, 1);
  1141. skip_bits(&v->gb, 2); //framecnt unused
  1142. if (v->rangered) v->rangeredfrm = get_bits(&v->gb, 1);
  1143. v->pict_type = get_bits(&v->gb, 1);
  1144. if (v->avctx->max_b_frames && !v->pict_type)
  1145. {
  1146. if (get_bits(&v->gb, 1)) v->pict_type = I_TYPE;
  1147. else v->pict_type = P_TYPE;
  1148. }
  1149. else v->pict_type++; //P_TYPE
  1150. switch (v->pict_type)
  1151. {
  1152. case I_TYPE: status = decode_i_picture_header(v); break;
  1153. case BI_TYPE: status = decode_b_picture_header(v); break;
  1154. case P_TYPE: status = decode_p_picture_header(v); break;
  1155. case B_TYPE: status = decode_b_picture_header(v); break;
  1156. }
  1157. if (status == FRAME_SKIPED)
  1158. {
  1159. av_log(v, AV_LOG_INFO, "Skipping frame...\n");
  1160. return status;
  1161. }
  1162. /* AC Syntax */
  1163. index = decode012(&v->gb);
  1164. v->luma_ac_vlc = NULL + index; //FIXME Add AC table
  1165. v->chroma_ac_vlc = NULL + index;
  1166. if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE)
  1167. {
  1168. index = decode012(&v->gb);
  1169. v->luma_ac2_vlc = NULL + index; //FIXME Add AC2 table
  1170. v->chroma_ac2_vlc = NULL + index;
  1171. }
  1172. /* DC Syntax */
  1173. index = decode012(&v->gb);
  1174. v->luma_dc_vlc = vc9_luma_dc_vlc + index;
  1175. v->chroma_dc_vlc = vc9_chroma_dc_vlc + index;
  1176. return 0;
  1177. }
  1178. #if HAS_ADVANCED_PROFILE
  1179. /******************************************************************************/
  1180. /* Advanced Profile picture header decoding specific functions */
  1181. /******************************************************************************/
  1182. static int advanced_decode_picture_header(VC9Context *v)
  1183. {
  1184. static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
  1185. int type, i, index;
  1186. if (v->interlace)
  1187. {
  1188. v->fcm = get_bits(&v->gb, 1);
  1189. if (v->fcm) v->fcm = 2+get_bits(&v->gb, 1);
  1190. }
  1191. type = get_prefix(&v->gb, 0, 4);
  1192. if (type > 4 || type < 0) return FRAME_SKIPED;
  1193. v->pict_type = type_table[type];
  1194. av_log(v->avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->pict_type);
  1195. if (v->tfcntrflag) v->tfcntr = get_bits(&v->gb, 8);
  1196. if (v->broadcast)
  1197. {
  1198. if (!v->interlace) v->rptfrm = get_bits(&v->gb, 2);
  1199. else
  1200. {
  1201. v->tff = get_bits(&v->gb, 1);
  1202. v->rff = get_bits(&v->gb, 1);
  1203. }
  1204. }
  1205. if (v->panscanflag)
  1206. {
  1207. #if 0
  1208. for (i=0; i<v->numpanscanwin; i++)
  1209. {
  1210. v->topleftx[i] = get_bits(&v->gb, 16);
  1211. v->toplefty[i] = get_bits(&v->gb, 16);
  1212. v->bottomrightx[i] = get_bits(&v->gb, 16);
  1213. v->bottomrighty[i] = get_bits(&v->gb, 16);
  1214. }
  1215. #else
  1216. skip_bits(&v->gb, 16*4*v->numpanscanwin);
  1217. #endif
  1218. }
  1219. v->rndctrl = get_bits(&v->gb, 1);
  1220. v->uvsamp = get_bits(&v->gb, 1);
  1221. if (v->finterpflag == 1) v->interpfrm = get_bits(&v->gb, 1);
  1222. switch(v->pict_type)
  1223. {
  1224. case I_TYPE: if (decode_i_picture_header(v) < 0) return -1;
  1225. case P_TYPE: if (decode_p_picture_header(v) < 0) return -1;
  1226. case BI_TYPE:
  1227. case B_TYPE: if (decode_b_picture_header(v) < 0) return FRAME_SKIPED;
  1228. default: break;
  1229. }
  1230. /* AC Syntax */
  1231. index = decode012(&v->gb);
  1232. v->luma_ac_vlc = NULL + index; //FIXME
  1233. v->chroma_ac_vlc = NULL + index; //FIXME
  1234. if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE)
  1235. {
  1236. index = decode012(&v->gb); //FIXME
  1237. v->luma_ac2_vlc = NULL + index;
  1238. v->chroma_ac2_vlc = NULL + index;
  1239. }
  1240. /* DC Syntax */
  1241. index = decode012(&v->gb);
  1242. v->luma_dc_vlc = vc9_luma_dc_vlc + index;
  1243. v->chroma_dc_vlc = vc9_chroma_dc_vlc + index;
  1244. return 0;
  1245. }
  1246. #endif
  1247. /******************************************************************************/
  1248. /* Block decoding functions */
  1249. /******************************************************************************/
  1250. /* 7.1.4, p91 and 8.1.1.7, p(1)04 */
  1251. /* FIXME proper integration (unusable and lots of parameters to send */
  1252. int decode_luma_intra_block(VC9Context *v, int mquant)
  1253. {
  1254. int dcdiff;
  1255. dcdiff = get_vlc2(&v->gb, v->luma_dc_vlc->table,
  1256. VC9_DC_VLC_BITS, 2);
  1257. if (dcdiff)
  1258. {
  1259. if (dcdiff == 119 /* ESC index value */)
  1260. {
  1261. /* TODO: Optimize */
  1262. if (mquant == 1) dcdiff = get_bits(&v->gb, 10);
  1263. else if (mquant == 2) dcdiff = get_bits(&v->gb, 9);
  1264. else dcdiff = get_bits(&v->gb, 8);
  1265. }
  1266. else
  1267. {
  1268. if (mquant == 1)
  1269. dcdiff = (dcdiff<<2) + get_bits(&v->gb, 2) - 3;
  1270. else if (mquant == 2)
  1271. dcdiff = (dcdiff<<1) + get_bits(&v->gb, 1) - 1;
  1272. }
  1273. if (get_bits(&v->gb, 1))
  1274. dcdiff = -dcdiff;
  1275. }
  1276. /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */
  1277. return 0;
  1278. }
  1279. /******************************************************************************/
  1280. /* MacroBlock decoding functions */
  1281. /******************************************************************************/
  1282. /* 8.1.1.5, p(1)02-(1)03 */
  1283. /* We only need to store 3 flags, but math with 4 is easier */
  1284. #define GET_CBPCY(table, bits) \
  1285. predicted_cbpcy = get_vlc2(&v->gb, table, bits, 2); \
  1286. cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
  1287. ? previous_cbpcy[1] : p_cbpcy[+2]; \
  1288. cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
  1289. cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \
  1290. cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \
  1291. cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) \
  1292. ? previous_cbpcy[3] : cbpcy[0]; \
  1293. cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
  1294. cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
  1295. cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
  1296. /* 8.1, p100 */
  1297. static int standard_decode_i_mbs(VC9Context *v)
  1298. {
  1299. int x, y, current_mb = 0; /* MB/Block Position info */
  1300. int ac_pred;
  1301. /* FIXME: better to use a pointer than using (x<<4) */
  1302. uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
  1303. *p_cbpcy /* Pointer to skip some math */;
  1304. /* Reset CBPCY predictors */
  1305. memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2);
  1306. /* Select ttmb table depending on pq */
  1307. if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
  1308. else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
  1309. else v->ttmb_vlc = &vc9_ttmb_vlc[2];
  1310. for (y=0; y<v->height_mb; y++)
  1311. {
  1312. /* Init CBPCY for line */
  1313. *((uint32_t*)previous_cbpcy) = 0x00000000;
  1314. p_cbpcy = v->previous_line_cbpcy+4;
  1315. for (x=0; x<v->width_mb; x++, p_cbpcy += 4)
  1316. {
  1317. /* Get CBPCY */
  1318. GET_CBPCY(vc9_cbpcy_i_vlc.table, VC9_CBPCY_I_VLC_BITS);
  1319. ac_pred = get_bits(&v->gb, 1);
  1320. /* TODO: Decode blocks from that mb wrt cbpcy */
  1321. /* Update for next block */
  1322. *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
  1323. *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
  1324. current_mb++;
  1325. }
  1326. }
  1327. return 0;
  1328. }
  1329. #define GET_MQUANT() \
  1330. if (v->dquantfrm) \
  1331. { \
  1332. if (v->dqprofile == DQPROFILE_ALL_MBS) \
  1333. { \
  1334. if (v->dqbilevel) \
  1335. { \
  1336. mquant = (get_bits(&v->gb, 1)) ? v->pq : v->altpq; \
  1337. } \
  1338. else \
  1339. { \
  1340. mqdiff = get_bits(&v->gb, 3); \
  1341. if (mqdiff != 7) mquant = v->pq + mqdiff; \
  1342. else mquant = get_bits(&v->gb, 5); \
  1343. } \
  1344. } \
  1345. }
  1346. /* MVDATA decoding from 8.3.5.2, p(1)20 */
  1347. #define GET_MVDATA(_dmv_x, _dmv_y) \
  1348. index = 1 + get_vlc2(&v->gb, v->mv_diff_vlc->table, \
  1349. VC9_MV_DIFF_VLC_BITS, 2); \
  1350. if (index > 36) \
  1351. { \
  1352. mb_has_coeffs = 1; \
  1353. index -= 37; \
  1354. } \
  1355. else mb_has_coeffs = 0; \
  1356. mb_is_intra = 0; \
  1357. if (!index) { _dmv_x = _dmv_y = 0; } \
  1358. else if (index == 35) \
  1359. { \
  1360. _dmv_x = get_bits(&v->gb, k_x); \
  1361. _dmv_y = get_bits(&v->gb, k_y); \
  1362. mb_is_intra = 1; \
  1363. } \
  1364. else \
  1365. { \
  1366. index1 = index%6; \
  1367. if (hpel_flag && index1 == 5) val = 1; \
  1368. else val = 0; \
  1369. val = get_bits(&v->gb, size_table[index1] - val); \
  1370. sign = 0 - (val&1); \
  1371. _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1372. \
  1373. index1 = index/6; \
  1374. if (hpel_flag && index1 == 5) val = 1; \
  1375. else val = 0; \
  1376. val = get_bits(&v->gb, size_table[index1] - val); \
  1377. sign = 0 - (val&1); \
  1378. _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
  1379. }
  1380. /* 8.1, p(1)15 */
  1381. static int decode_p_mbs(VC9Context *v)
  1382. {
  1383. int x, y, current_mb = 0, i; /* MB/Block Position info */
  1384. uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
  1385. *p_cbpcy /* Pointer to skip some math */;
  1386. int hybrid_pred, ac_pred; /* Prediction types */
  1387. int mv_mode_bit = 0;
  1388. int mqdiff, mquant; /* MB quantization */
  1389. int ttmb; /* MB Transform type */
  1390. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1391. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1392. int mb_has_coeffs = 1 /* last_flag */, mb_is_intra;
  1393. int dmv_x, dmv_y; /* Differential MV components */
  1394. int k_x, k_y; /* Long MV fixed bitlength */
  1395. int hpel_flag; /* Some MB properties */
  1396. int index, index1; /* LUT indices */
  1397. int val, sign; /* MVDATA temp values */
  1398. /* Select ttmb table depending on pq */
  1399. if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
  1400. else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
  1401. else v->ttmb_vlc = &vc9_ttmb_vlc[2];
  1402. /* Select proper long MV range */
  1403. switch (v->mvrange)
  1404. {
  1405. case 1: k_x = 10; k_y = 9; break;
  1406. case 2: k_x = 12; k_y = 10; break;
  1407. case 3: k_x = 13; k_y = 11; break;
  1408. default: /*case 0 too */ k_x = 9; k_y = 8; break;
  1409. }
  1410. hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
  1411. k_x -= hpel_flag;
  1412. k_y -= hpel_flag;
  1413. /* Reset CBPCY predictors */
  1414. memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2);
  1415. for (y=0; y<v->height_mb; y++)
  1416. {
  1417. /* Init CBPCY for line */
  1418. *((uint32_t*)previous_cbpcy) = 0x00000000;
  1419. p_cbpcy = v->previous_line_cbpcy+4;
  1420. for (x=0; x<v->width_mb; x++)
  1421. {
  1422. if (v->mv_type_mb_plane.is_raw)
  1423. v->mv_type_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
  1424. if (v->skip_mb_plane.is_raw)
  1425. v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
  1426. if (!mv_mode_bit) /* 1MV mode */
  1427. {
  1428. if (!v->skip_mb_plane.data[current_mb])
  1429. {
  1430. GET_MVDATA(dmv_x, dmv_y);
  1431. /* hybrid mv pred, 8.3.5.3.4 */
  1432. if (v->mv_mode == MV_PMODE_1MV ||
  1433. v->mv_mode == MV_PMODE_MIXED_MV)
  1434. hybrid_pred = get_bits(&v->gb, 1);
  1435. if (mb_is_intra && !mb_has_coeffs)
  1436. {
  1437. GET_MQUANT();
  1438. ac_pred = get_bits(&v->gb, 1);
  1439. }
  1440. else if (mb_has_coeffs)
  1441. {
  1442. if (mb_is_intra) ac_pred = get_bits(&v->gb, 1);
  1443. GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
  1444. GET_MQUANT();
  1445. }
  1446. if (!v->ttmbf)
  1447. ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
  1448. VC9_TTMB_VLC_BITS, 12);
  1449. /* TODO: decode blocks from that mb wrt cbpcy */
  1450. }
  1451. else //Skipped
  1452. {
  1453. /* hybrid mv pred, 8.3.5.3.4 */
  1454. if (v->mv_mode == MV_PMODE_1MV ||
  1455. v->mv_mode == MV_PMODE_MIXED_MV)
  1456. hybrid_pred = get_bits(&v->gb, 1);
  1457. }
  1458. } //1MV mode
  1459. else //4MV mode
  1460. {
  1461. if (!v->skip_mb_plane.data[current_mb] /* unskipped MB */)
  1462. {
  1463. /* Get CBPCY */
  1464. GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
  1465. for (i=0; i<4; i++) //For all 4 Y blocks
  1466. {
  1467. if (cbpcy[i] /* cbpcy set for this block */)
  1468. {
  1469. GET_MVDATA(dmv_x, dmv_y);
  1470. }
  1471. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1472. hybrid_pred = get_bits(&v->gb, 1);
  1473. GET_MQUANT();
  1474. if (mb_is_intra /* One of the 4 blocks is intra */ &&
  1475. index /* non-zero pred for that block */)
  1476. ac_pred = get_bits(&v->gb, 1);
  1477. if (!v->ttmbf)
  1478. ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
  1479. VC9_TTMB_VLC_BITS, 12);
  1480. /* TODO: Process blocks wrt cbpcy */
  1481. }
  1482. }
  1483. else //Skipped MB
  1484. {
  1485. for (i=0; i<4; i++) //All 4 Y blocks
  1486. {
  1487. if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
  1488. hybrid_pred = get_bits(&v->gb, 1);
  1489. /* TODO: do something */
  1490. }
  1491. }
  1492. }
  1493. /* Update for next block */
  1494. #if TRACE > 2
  1495. av_log(v->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
  1496. " cbpcy=%i%i%i%i\n", current_mb,
  1497. p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
  1498. previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
  1499. cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
  1500. #endif
  1501. *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
  1502. *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
  1503. current_mb++;
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. static int decode_b_mbs(VC9Context *v)
  1509. {
  1510. int x, y, current_mb = 0, i /* MB / B postion information */;
  1511. int ac_pred;
  1512. int b_mv_type = BMV_TYPE_BACKWARD;
  1513. int mquant, mqdiff; /* MB quant stuff */
  1514. int ttmb; /* MacroBlock transform type */
  1515. static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
  1516. offset_table[6] = { 0, 1, 3, 7, 15, 31 };
  1517. int mb_has_coeffs = 1 /* last_flag */, mb_is_intra = 1;
  1518. int dmv1_x, dmv1_y, dmv2_x, dmv2_y; /* Differential MV components */
  1519. int k_x, k_y; /* Long MV fixed bitlength */
  1520. int hpel_flag; /* Some MB properties */
  1521. int index, index1; /* LUT indices */
  1522. int val, sign; /* MVDATA temp values */
  1523. /* Select proper long MV range */
  1524. switch (v->mvrange)
  1525. {
  1526. case 1: k_x = 10; k_y = 9; break;
  1527. case 2: k_x = 12; k_y = 10; break;
  1528. case 3: k_x = 13; k_y = 11; break;
  1529. default: /*case 0 too */ k_x = 9; k_y = 8; break;
  1530. }
  1531. hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
  1532. k_x -= hpel_flag;
  1533. k_y -= hpel_flag;
  1534. /* Select ttmb table depending on pq */
  1535. if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
  1536. else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
  1537. else v->ttmb_vlc = &vc9_ttmb_vlc[2];
  1538. for (y=0; y<v->height_mb; y++)
  1539. {
  1540. for (x=0; x<v->width_mb; x++)
  1541. {
  1542. if (v->direct_mb_plane.is_raw)
  1543. v->direct_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
  1544. if (v->skip_mb_plane.is_raw)
  1545. v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
  1546. if (!v->direct_mb_plane.data[current_mb])
  1547. {
  1548. if (v->skip_mb_plane.data[current_mb])
  1549. {
  1550. b_mv_type = decode012(&v->gb);
  1551. if (v->bfraction > 420 /*1/2*/ &&
  1552. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  1553. }
  1554. else
  1555. {
  1556. /* FIXME getting tired commenting */
  1557. GET_MVDATA(dmv1_x, dmv1_y);
  1558. if (!mb_is_intra /* b_mv1 tells not intra */)
  1559. {
  1560. /* FIXME: actually read it */
  1561. b_mv_type = decode012(&v->gb);
  1562. if (v->bfraction > 420 /*1/2*/ &&
  1563. b_mv_type < 3) b_mv_type = 1-b_mv_type;
  1564. }
  1565. }
  1566. }
  1567. if (!v->skip_mb_plane.data[current_mb])
  1568. {
  1569. if (mb_has_coeffs /* BMV1 == "last" */)
  1570. {
  1571. GET_MQUANT();
  1572. if (mb_is_intra /* intra mb */)
  1573. ac_pred = get_bits(&v->gb, 1);
  1574. }
  1575. else
  1576. {
  1577. /* if bmv1 tells MVs are interpolated */
  1578. if (b_mv_type == BMV_TYPE_INTERPOLATED)
  1579. {
  1580. GET_MVDATA(dmv2_x, dmv2_y);
  1581. }
  1582. /* GET_MVDATA has reset some stuff */
  1583. if (mb_has_coeffs /* b_mv2 == "last" */)
  1584. {
  1585. if (mb_is_intra /* intra_mb */)
  1586. ac_pred = get_bits(&v->gb, 1);
  1587. GET_MQUANT();
  1588. }
  1589. }
  1590. }
  1591. //End1
  1592. if (v->ttmbf)
  1593. ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
  1594. VC9_TTMB_VLC_BITS, 12);
  1595. //End2
  1596. for (i=0; i<6; i++)
  1597. {
  1598. /* FIXME: process the block */
  1599. }
  1600. current_mb++;
  1601. }
  1602. }
  1603. return 0;
  1604. }
  1605. #if HAS_ADVANCED_PROFILE
  1606. static int advanced_decode_i_mbs(VC9Context *v)
  1607. {
  1608. int x, y, mqdiff, mquant, ac_pred, current_mb = 0, over_flags_mb = 0;
  1609. for (y=0; y<v->height_mb; y++)
  1610. {
  1611. for (x=0; x<v->width_mb; x++)
  1612. {
  1613. if (v->ac_pred_plane.data[current_mb])
  1614. ac_pred = get_bits(&v->gb, 1);
  1615. if (v->condover == 3 && v->over_flags_plane.is_raw)
  1616. over_flags_mb = get_bits(&v->gb, 1);
  1617. GET_MQUANT();
  1618. /* TODO: lots */
  1619. }
  1620. current_mb++;
  1621. }
  1622. return 0;
  1623. }
  1624. #endif
  1625. static int vc9_decode_init(AVCodecContext *avctx)
  1626. {
  1627. VC9Context *v = avctx->priv_data;
  1628. GetBitContext gb;
  1629. if (!avctx->extradata_size || !avctx->extradata) return -1;
  1630. avctx->pix_fmt = PIX_FMT_YUV420P;
  1631. v->avctx = avctx;
  1632. if (init_common(v) < 0) return -1;
  1633. avctx->coded_width = avctx->width;
  1634. avctx->coded_height = avctx->height;
  1635. if (avctx->codec_id == CODEC_ID_WMV3)
  1636. {
  1637. int count = 0;
  1638. // looks like WMV3 has a sequence header stored in the extradata
  1639. // advanced sequence header may be before the first frame
  1640. // the last byte of the extradata is a version number, 1 for the
  1641. // samples we can decode
  1642. init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
  1643. decode_sequence_header(avctx, &gb);
  1644. count = avctx->extradata_size*8 - get_bits_count(&gb);
  1645. if (count>0)
  1646. {
  1647. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  1648. count, get_bits(&gb, count));
  1649. }
  1650. else
  1651. {
  1652. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  1653. }
  1654. }
  1655. /* Done with header parsing */
  1656. //FIXME I feel like this is wrong
  1657. v->width_mb = (avctx->coded_width+15)>>4;
  1658. v->height_mb = (avctx->coded_height+15)>>4;
  1659. /* Allocate mb bitplanes */
  1660. if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0)
  1661. return -1;
  1662. if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0)
  1663. return -1;
  1664. if (alloc_bitplane(&v->skip_mb_plane, v->width_mb, v->height_mb) < 0)
  1665. return -1;
  1666. if (alloc_bitplane(&v->direct_mb_plane, v->width_mb, v->height_mb) < 0)
  1667. return -1;
  1668. /* For predictors */
  1669. v->previous_line_cbpcy = (uint8_t *)av_malloc((v->width_mb+1)*4);
  1670. if (!v->previous_line_cbpcy) return -1;
  1671. #if HAS_ADVANCED_PROFILE
  1672. if (v->profile > PROFILE_MAIN)
  1673. {
  1674. if (alloc_bitplane(&v->over_flags_plane, v->width_mb, v->height_mb) < 0)
  1675. return -1;
  1676. if (alloc_bitplane(&v->ac_pred_plane, v->width_mb, v->height_mb) < 0)
  1677. return -1;
  1678. }
  1679. #endif
  1680. return 0;
  1681. }
  1682. static int vc9_decode_frame(AVCodecContext *avctx,
  1683. void *data, int *data_size,
  1684. uint8_t *buf, int buf_size)
  1685. {
  1686. VC9Context *v = avctx->priv_data;
  1687. int ret = FRAME_SKIPED, len, start_code;
  1688. AVFrame *pict = data;
  1689. uint8_t *tmp_buf;
  1690. v->avctx = avctx;
  1691. //buf_size = 0 -> last frame
  1692. if (!buf_size) return 0;
  1693. len = avpicture_get_size(avctx->pix_fmt, avctx->width,
  1694. avctx->height);
  1695. tmp_buf = (uint8_t *)av_mallocz(len);
  1696. avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt,
  1697. avctx->width, avctx->height);
  1698. if (avctx->codec_id == CODEC_ID_WMV3)
  1699. {
  1700. //No IDU
  1701. init_get_bits(&v->gb, buf, buf_size*8);
  1702. #if HAS_ADVANCED_PROFILE
  1703. if (v->profile > PROFILE_MAIN)
  1704. {
  1705. if (advanced_decode_picture_header(v) == FRAME_SKIPED) return buf_size;
  1706. switch(v->pict_type)
  1707. {
  1708. case I_TYPE: ret = advanced_decode_i_mbs(v); break;
  1709. case P_TYPE: ret = decode_p_mbs(v); break;
  1710. case B_TYPE:
  1711. case BI_TYPE: ret = decode_b_mbs(v); break;
  1712. default: ret = FRAME_SKIPED;
  1713. }
  1714. if (ret == FRAME_SKIPED) return buf_size; //We ignore for now failures
  1715. }
  1716. else
  1717. #endif
  1718. {
  1719. if (standard_decode_picture_header(v) == FRAME_SKIPED) return buf_size;
  1720. switch(v->pict_type)
  1721. {
  1722. case I_TYPE: ret = standard_decode_i_mbs(v); break;
  1723. case P_TYPE: ret = decode_p_mbs(v); break;
  1724. case B_TYPE:
  1725. case BI_TYPE: ret = decode_b_mbs(v); break;
  1726. default: ret = FRAME_SKIPED;
  1727. }
  1728. if (ret == FRAME_SKIPED) return buf_size;
  1729. }
  1730. }
  1731. else
  1732. {
  1733. #if 0
  1734. // search for IDU's
  1735. // FIXME
  1736. uint32_t scp = 0;
  1737. int scs = 0, i = 0;
  1738. while (i < buf_size)
  1739. {
  1740. for (; i < buf_size && scp != 0x000001; i++)
  1741. scp = ((scp<<8)|buf[i])&0xffffff;
  1742. if (scp != 0x000001)
  1743. break; // eof ?
  1744. scs = buf[i++];
  1745. init_get_bits(&v->gb, buf+i, (buf_size-i)*8);
  1746. switch(scs)
  1747. {
  1748. case 0x0A: //Sequence End Code
  1749. return 0;
  1750. case 0x0B: //Slice Start Code
  1751. av_log(avctx, AV_LOG_ERROR, "Slice coding not supported\n");
  1752. return -1;
  1753. case 0x0C: //Field start code
  1754. av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
  1755. return -1;
  1756. case 0x0D: //Frame start code
  1757. break;
  1758. case 0x0E: //Entry point Start Code
  1759. if (v->profile <= MAIN_PROFILE)
  1760. av_log(avctx, AV_LOG_ERROR,
  1761. "Found an entry point in profile %i\n", v->profile);
  1762. advanced_entry_point_process(avctx, &v->gb);
  1763. break;
  1764. case 0x0F: //Sequence header Start Code
  1765. decode_sequence_header(avctx, &v->gb);
  1766. break;
  1767. default:
  1768. av_log(avctx, AV_LOG_ERROR,
  1769. "Unsupported IDU suffix %lX\n", scs);
  1770. }
  1771. i += get_bits_count(&v->gb)*8;
  1772. }
  1773. #else
  1774. av_abort();
  1775. #endif
  1776. }
  1777. av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
  1778. get_bits_count(&v->gb), buf_size*8);
  1779. /* Fake consumption of all data */
  1780. *data_size = len;
  1781. return buf_size; //Number of bytes consumed
  1782. }
  1783. static int vc9_decode_end(AVCodecContext *avctx)
  1784. {
  1785. VC9Context *v = avctx->priv_data;
  1786. #if HAS_ADVANCED_PROFILE
  1787. av_freep(&v->hrd_rate);
  1788. av_freep(&v->hrd_buffer);
  1789. #endif
  1790. av_freep(&v->mv_type_mb_plane);
  1791. av_freep(&v->skip_mb_plane);
  1792. av_freep(&v->direct_mb_plane);
  1793. return 0;
  1794. }
  1795. AVCodec vc9_decoder = {
  1796. "vc9",
  1797. CODEC_TYPE_VIDEO,
  1798. CODEC_ID_VC9,
  1799. sizeof(VC9Context),
  1800. vc9_decode_init,
  1801. NULL,
  1802. vc9_decode_end,
  1803. vc9_decode_frame,
  1804. CODEC_CAP_DELAY,
  1805. NULL
  1806. };
  1807. AVCodec wmv3_decoder = {
  1808. "wmv3",
  1809. CODEC_TYPE_VIDEO,
  1810. CODEC_ID_WMV3,
  1811. sizeof(VC9Context),
  1812. vc9_decode_init,
  1813. NULL,
  1814. vc9_decode_end,
  1815. vc9_decode_frame,
  1816. CODEC_CAP_DELAY,
  1817. NULL
  1818. };