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.

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