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.

1724 lines
54KB

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