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.

367 lines
12KB

  1. /*
  2. * VC-1 and WMV3 decoder
  3. * Copyright (c) 2006-2007 Konstantin Shishkov
  4. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /** Markers used in VC-1 AP frame data */
  23. //@{
  24. enum VC1Code{
  25. VC1_CODE_RES0 = 0x00000100,
  26. VC1_CODE_ENDOFSEQ = 0x0000010A,
  27. VC1_CODE_SLICE,
  28. VC1_CODE_FIELD,
  29. VC1_CODE_FRAME,
  30. VC1_CODE_ENTRYPOINT,
  31. VC1_CODE_SEQHDR,
  32. };
  33. //@}
  34. #define IS_MARKER(x) (((x) & ~0xFF) == VC1_CODE_RES0)
  35. #ifndef VC1_PARSER_ONLY
  36. /** Available Profiles */
  37. //@{
  38. enum Profile {
  39. PROFILE_SIMPLE,
  40. PROFILE_MAIN,
  41. PROFILE_COMPLEX, ///< TODO: WMV9 specific
  42. PROFILE_ADVANCED
  43. };
  44. //@}
  45. /** Sequence quantizer mode */
  46. //@{
  47. enum QuantMode {
  48. QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
  49. QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
  50. QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
  51. QUANT_UNIFORM ///< Uniform quant used for all frames
  52. };
  53. //@}
  54. /** Where quant can be changed */
  55. //@{
  56. enum DQProfile {
  57. DQPROFILE_FOUR_EDGES,
  58. DQPROFILE_DOUBLE_EDGES,
  59. DQPROFILE_SINGLE_EDGE,
  60. DQPROFILE_ALL_MBS
  61. };
  62. //@}
  63. /** @name Where quant can be changed
  64. */
  65. //@{
  66. enum DQSingleEdge {
  67. DQSINGLE_BEDGE_LEFT,
  68. DQSINGLE_BEDGE_TOP,
  69. DQSINGLE_BEDGE_RIGHT,
  70. DQSINGLE_BEDGE_BOTTOM
  71. };
  72. //@}
  73. /** Which pair of edges is quantized with ALTPQUANT */
  74. //@{
  75. enum DQDoubleEdge {
  76. DQDOUBLE_BEDGE_TOPLEFT,
  77. DQDOUBLE_BEDGE_TOPRIGHT,
  78. DQDOUBLE_BEDGE_BOTTOMRIGHT,
  79. DQDOUBLE_BEDGE_BOTTOMLEFT
  80. };
  81. //@}
  82. /** MV modes for P frames */
  83. //@{
  84. enum MVModes {
  85. MV_PMODE_1MV_HPEL_BILIN,
  86. MV_PMODE_1MV,
  87. MV_PMODE_1MV_HPEL,
  88. MV_PMODE_MIXED_MV,
  89. MV_PMODE_INTENSITY_COMP
  90. };
  91. //@}
  92. /** @name MV types for B frames */
  93. //@{
  94. enum BMVTypes {
  95. BMV_TYPE_BACKWARD,
  96. BMV_TYPE_FORWARD,
  97. BMV_TYPE_INTERPOLATED
  98. };
  99. //@}
  100. /** @name Block types for P/B frames */
  101. //@{
  102. enum TransformTypes {
  103. TT_8X8,
  104. TT_8X4_BOTTOM,
  105. TT_8X4_TOP,
  106. TT_8X4, //Both halves
  107. TT_4X8_RIGHT,
  108. TT_4X8_LEFT,
  109. TT_4X8, //Both halves
  110. TT_4X4
  111. };
  112. //@}
  113. /** Table for conversion between TTBLK and TTMB */
  114. static const int ttblk_to_tt[3][8] = {
  115. { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
  116. { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
  117. { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
  118. };
  119. static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
  120. /** MV P mode - the 5th element is only used for mode 1 */
  121. static const uint8_t mv_pmode_table[2][5] = {
  122. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
  123. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
  124. };
  125. static const uint8_t mv_pmode_table2[2][4] = {
  126. { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV },
  127. { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
  128. };
  129. /** One more frame type */
  130. #define BI_TYPE 7
  131. static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
  132. fps_dr[2] = { 1000, 1001 };
  133. static const uint8_t pquant_table[3][32] = {
  134. { /* Implicit quantizer */
  135. 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
  136. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
  137. },
  138. { /* Explicit quantizer, pquantizer uniform */
  139. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  140. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  141. },
  142. { /* Explicit quantizer, pquantizer non-uniform */
  143. 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  144. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
  145. }
  146. };
  147. /** @name VC-1 VLC tables and defines
  148. * @todo TODO move this into the context
  149. */
  150. //@{
  151. #define VC1_BFRACTION_VLC_BITS 7
  152. static VLC vc1_bfraction_vlc;
  153. #define VC1_IMODE_VLC_BITS 4
  154. static VLC vc1_imode_vlc;
  155. #define VC1_NORM2_VLC_BITS 3
  156. static VLC vc1_norm2_vlc;
  157. #define VC1_NORM6_VLC_BITS 9
  158. static VLC vc1_norm6_vlc;
  159. /* Could be optimized, one table only needs 8 bits */
  160. #define VC1_TTMB_VLC_BITS 9 //12
  161. static VLC vc1_ttmb_vlc[3];
  162. #define VC1_MV_DIFF_VLC_BITS 9 //15
  163. static VLC vc1_mv_diff_vlc[4];
  164. #define VC1_CBPCY_P_VLC_BITS 9 //14
  165. static VLC vc1_cbpcy_p_vlc[4];
  166. #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
  167. static VLC vc1_4mv_block_pattern_vlc[4];
  168. #define VC1_TTBLK_VLC_BITS 5
  169. static VLC vc1_ttblk_vlc[3];
  170. #define VC1_SUBBLKPAT_VLC_BITS 6
  171. static VLC vc1_subblkpat_vlc[3];
  172. static VLC vc1_ac_coeff_table[8];
  173. //@}
  174. enum CodingSet {
  175. CS_HIGH_MOT_INTRA = 0,
  176. CS_HIGH_MOT_INTER,
  177. CS_LOW_MOT_INTRA,
  178. CS_LOW_MOT_INTER,
  179. CS_MID_RATE_INTRA,
  180. CS_MID_RATE_INTER,
  181. CS_HIGH_RATE_INTRA,
  182. CS_HIGH_RATE_INTER
  183. };
  184. /** @name Overlap conditions for Advanced Profile */
  185. //@{
  186. enum COTypes {
  187. CONDOVER_NONE = 0,
  188. CONDOVER_ALL,
  189. CONDOVER_SELECT
  190. };
  191. //@}
  192. /** The VC1 Context
  193. * @fixme Change size wherever another size is more efficient
  194. * Many members are only used for Advanced Profile
  195. */
  196. typedef struct VC1Context{
  197. MpegEncContext s;
  198. int bits;
  199. /** Simple/Main Profile sequence header */
  200. //@{
  201. int res_sm; ///< reserved, 2b
  202. int res_x8; ///< reserved
  203. int multires; ///< frame-level RESPIC syntax element present
  204. int res_fasttx; ///< reserved, always 1
  205. int res_transtab; ///< reserved, always 0
  206. int rangered; ///< RANGEREDFRM (range reduction) syntax element present
  207. ///< at frame level
  208. int res_rtm_flag; ///< reserved, set to 1
  209. int reserved; ///< reserved
  210. //@}
  211. /** Advanced Profile */
  212. //@{
  213. int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
  214. int chromaformat; ///< 2bits, 2=4:2:0, only defined
  215. int postprocflag; ///< Per-frame processing suggestion flag present
  216. int broadcast; ///< TFF/RFF present
  217. int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
  218. int tfcntrflag; ///< TFCNTR present
  219. int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
  220. int extended_dmv; ///< Additional extended dmv range at P/B frame-level
  221. int color_prim; ///< 8bits, chroma coordinates of the color primaries
  222. int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
  223. int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
  224. int hrd_param_flag; ///< Presence of Hypothetical Reference
  225. ///< Decoder parameters
  226. int psf; ///< Progressive Segmented Frame
  227. //@}
  228. /** Sequence header data for all Profiles
  229. * TODO: choose between ints, uint8_ts and monobit flags
  230. */
  231. //@{
  232. int profile; ///< 2bits, Profile
  233. int frmrtq_postproc; ///< 3bits,
  234. int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
  235. int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
  236. int extended_mv; ///< Ext MV in P/B (not in Simple)
  237. int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
  238. int vstransform; ///< variable-size [48]x[48] transform type + info
  239. int overlap; ///< overlapped transforms in use
  240. int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
  241. int finterpflag; ///< INTERPFRM present
  242. //@}
  243. /** Frame decoding info for all profiles */
  244. //@{
  245. uint8_t mv_mode; ///< MV coding monde
  246. uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
  247. int k_x; ///< Number of bits for MVs (depends on MV range)
  248. int k_y; ///< Number of bits for MVs (depends on MV range)
  249. int range_x, range_y; ///< MV range
  250. uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
  251. /** pquant parameters */
  252. //@{
  253. uint8_t dquantfrm;
  254. uint8_t dqprofile;
  255. uint8_t dqsbedge;
  256. uint8_t dqbilevel;
  257. //@}
  258. /** AC coding set indexes
  259. * @see 8.1.1.10, p(1)10
  260. */
  261. //@{
  262. int c_ac_table_index; ///< Chroma index from ACFRM element
  263. int y_ac_table_index; ///< Luma index from AC2FRM element
  264. //@}
  265. int ttfrm; ///< Transform type info present at frame level
  266. uint8_t ttmbf; ///< Transform type flag
  267. uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
  268. int codingset; ///< index of current table set from 11.8 to use for luma block decoding
  269. int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
  270. int pqindex; ///< raw pqindex used in coding set selection
  271. int a_avail, c_avail;
  272. uint8_t *mb_type_base, *mb_type[3];
  273. /** Luma compensation parameters */
  274. //@{
  275. uint8_t lumscale;
  276. uint8_t lumshift;
  277. //@}
  278. int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
  279. uint8_t halfpq; ///< Uniform quant over image and qp+.5
  280. uint8_t respic; ///< Frame-level flag for resized images
  281. int buffer_fullness; ///< HRD info
  282. /** Ranges:
  283. * -# 0 -> [-64n 63.f] x [-32, 31.f]
  284. * -# 1 -> [-128, 127.f] x [-64, 63.f]
  285. * -# 2 -> [-512, 511.f] x [-128, 127.f]
  286. * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
  287. */
  288. uint8_t mvrange;
  289. uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
  290. VLC *cbpcy_vlc; ///< CBPCY VLC table
  291. int tt_index; ///< Index for Transform Type tables
  292. uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
  293. uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs
  294. int mv_type_is_raw; ///< mv type mb plane is not coded
  295. int dmb_is_raw; ///< direct mb plane is raw
  296. int skip_is_raw; ///< skip mb plane is not coded
  297. uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
  298. int use_ic; ///< use intensity compensation in B-frames
  299. int rnd; ///< rounding control
  300. /** Frame decoding info for S/M profiles only */
  301. //@{
  302. uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
  303. uint8_t interpfrm;
  304. //@}
  305. /** Frame decoding info for Advanced profile */
  306. //@{
  307. uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  308. uint8_t numpanscanwin;
  309. uint8_t tfcntr;
  310. uint8_t rptfrm, tff, rff;
  311. uint16_t topleftx;
  312. uint16_t toplefty;
  313. uint16_t bottomrightx;
  314. uint16_t bottomrighty;
  315. uint8_t uvsamp;
  316. uint8_t postproc;
  317. int hrd_num_leaky_buckets;
  318. uint8_t bit_rate_exponent;
  319. uint8_t buffer_size_exponent;
  320. uint8_t* acpred_plane; ///< AC prediction flags bitplane
  321. int acpred_is_raw;
  322. uint8_t* over_flags_plane; ///< Overflags bitplane
  323. int overflg_is_raw;
  324. uint8_t condover;
  325. uint16_t *hrd_rate, *hrd_buffer;
  326. uint8_t *hrd_fullness;
  327. uint8_t range_mapy_flag;
  328. uint8_t range_mapuv_flag;
  329. uint8_t range_mapy;
  330. uint8_t range_mapuv;
  331. //@}
  332. int p_frame_skipped;
  333. int bi_type;
  334. } VC1Context;
  335. #endif