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.

2004 lines
64KB

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