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.

2196 lines
70KB

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