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.

303 lines
9.3KB

  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. #include "avcodec.h"
  23. #include "mpegvideo.h"
  24. /** Markers used in VC-1 AP frame data */
  25. //@{
  26. enum VC1Code{
  27. VC1_CODE_RES0 = 0x00000100,
  28. VC1_CODE_ENDOFSEQ = 0x0000010A,
  29. VC1_CODE_SLICE,
  30. VC1_CODE_FIELD,
  31. VC1_CODE_FRAME,
  32. VC1_CODE_ENTRYPOINT,
  33. VC1_CODE_SEQHDR,
  34. };
  35. //@}
  36. #define IS_MARKER(x) (((x) & ~0xFF) == VC1_CODE_RES0)
  37. /** Available Profiles */
  38. //@{
  39. enum Profile {
  40. PROFILE_SIMPLE,
  41. PROFILE_MAIN,
  42. PROFILE_COMPLEX, ///< TODO: WMV9 specific
  43. PROFILE_ADVANCED
  44. };
  45. //@}
  46. /** Sequence quantizer mode */
  47. //@{
  48. enum QuantMode {
  49. QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
  50. QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
  51. QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
  52. QUANT_UNIFORM ///< Uniform quant used for all frames
  53. };
  54. //@}
  55. /** Where quant can be changed */
  56. //@{
  57. enum DQProfile {
  58. DQPROFILE_FOUR_EDGES,
  59. DQPROFILE_DOUBLE_EDGES,
  60. DQPROFILE_SINGLE_EDGE,
  61. DQPROFILE_ALL_MBS
  62. };
  63. //@}
  64. /** @name Where quant can be changed
  65. */
  66. //@{
  67. enum DQSingleEdge {
  68. DQSINGLE_BEDGE_LEFT,
  69. DQSINGLE_BEDGE_TOP,
  70. DQSINGLE_BEDGE_RIGHT,
  71. DQSINGLE_BEDGE_BOTTOM
  72. };
  73. //@}
  74. /** Which pair of edges is quantized with ALTPQUANT */
  75. //@{
  76. enum DQDoubleEdge {
  77. DQDOUBLE_BEDGE_TOPLEFT,
  78. DQDOUBLE_BEDGE_TOPRIGHT,
  79. DQDOUBLE_BEDGE_BOTTOMRIGHT,
  80. DQDOUBLE_BEDGE_BOTTOMLEFT
  81. };
  82. //@}
  83. /** MV modes for P frames */
  84. //@{
  85. enum MVModes {
  86. MV_PMODE_1MV_HPEL_BILIN,
  87. MV_PMODE_1MV,
  88. MV_PMODE_1MV_HPEL,
  89. MV_PMODE_MIXED_MV,
  90. MV_PMODE_INTENSITY_COMP
  91. };
  92. //@}
  93. /** @name MV types for B frames */
  94. //@{
  95. enum BMVTypes {
  96. BMV_TYPE_BACKWARD,
  97. BMV_TYPE_FORWARD,
  98. BMV_TYPE_INTERPOLATED
  99. };
  100. //@}
  101. /** @name Block types for P/B frames */
  102. //@{
  103. enum TransformTypes {
  104. TT_8X8,
  105. TT_8X4_BOTTOM,
  106. TT_8X4_TOP,
  107. TT_8X4, //Both halves
  108. TT_4X8_RIGHT,
  109. TT_4X8_LEFT,
  110. TT_4X8, //Both halves
  111. TT_4X4
  112. };
  113. //@}
  114. /** One more frame type */
  115. #define BI_TYPE 7
  116. enum CodingSet {
  117. CS_HIGH_MOT_INTRA = 0,
  118. CS_HIGH_MOT_INTER,
  119. CS_LOW_MOT_INTRA,
  120. CS_LOW_MOT_INTER,
  121. CS_MID_RATE_INTRA,
  122. CS_MID_RATE_INTER,
  123. CS_HIGH_RATE_INTRA,
  124. CS_HIGH_RATE_INTER
  125. };
  126. /** @name Overlap conditions for Advanced Profile */
  127. //@{
  128. enum COTypes {
  129. CONDOVER_NONE = 0,
  130. CONDOVER_ALL,
  131. CONDOVER_SELECT
  132. };
  133. //@}
  134. /** The VC1 Context
  135. * @fixme Change size wherever another size is more efficient
  136. * Many members are only used for Advanced Profile
  137. */
  138. typedef struct VC1Context{
  139. MpegEncContext s;
  140. int bits;
  141. /** Simple/Main Profile sequence header */
  142. //@{
  143. int res_sm; ///< reserved, 2b
  144. int res_x8; ///< reserved
  145. int multires; ///< frame-level RESPIC syntax element present
  146. int res_fasttx; ///< reserved, always 1
  147. int res_transtab; ///< reserved, always 0
  148. int rangered; ///< RANGEREDFRM (range reduction) syntax element present
  149. ///< at frame level
  150. int res_rtm_flag; ///< reserved, set to 1
  151. int reserved; ///< reserved
  152. //@}
  153. /** Advanced Profile */
  154. //@{
  155. int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
  156. int chromaformat; ///< 2bits, 2=4:2:0, only defined
  157. int postprocflag; ///< Per-frame processing suggestion flag present
  158. int broadcast; ///< TFF/RFF present
  159. int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
  160. int tfcntrflag; ///< TFCNTR present
  161. int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
  162. int extended_dmv; ///< Additional extended dmv range at P/B frame-level
  163. int color_prim; ///< 8bits, chroma coordinates of the color primaries
  164. int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
  165. int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
  166. int hrd_param_flag; ///< Presence of Hypothetical Reference
  167. ///< Decoder parameters
  168. int psf; ///< Progressive Segmented Frame
  169. //@}
  170. /** Sequence header data for all Profiles
  171. * TODO: choose between ints, uint8_ts and monobit flags
  172. */
  173. //@{
  174. int profile; ///< 2bits, Profile
  175. int frmrtq_postproc; ///< 3bits,
  176. int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
  177. int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
  178. int extended_mv; ///< Ext MV in P/B (not in Simple)
  179. int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
  180. int vstransform; ///< variable-size [48]x[48] transform type + info
  181. int overlap; ///< overlapped transforms in use
  182. int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
  183. int finterpflag; ///< INTERPFRM present
  184. //@}
  185. /** Frame decoding info for all profiles */
  186. //@{
  187. uint8_t mv_mode; ///< MV coding monde
  188. uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
  189. int k_x; ///< Number of bits for MVs (depends on MV range)
  190. int k_y; ///< Number of bits for MVs (depends on MV range)
  191. int range_x, range_y; ///< MV range
  192. uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
  193. /** pquant parameters */
  194. //@{
  195. uint8_t dquantfrm;
  196. uint8_t dqprofile;
  197. uint8_t dqsbedge;
  198. uint8_t dqbilevel;
  199. //@}
  200. /** AC coding set indexes
  201. * @see 8.1.1.10, p(1)10
  202. */
  203. //@{
  204. int c_ac_table_index; ///< Chroma index from ACFRM element
  205. int y_ac_table_index; ///< Luma index from AC2FRM element
  206. //@}
  207. int ttfrm; ///< Transform type info present at frame level
  208. uint8_t ttmbf; ///< Transform type flag
  209. uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
  210. int codingset; ///< index of current table set from 11.8 to use for luma block decoding
  211. int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
  212. int pqindex; ///< raw pqindex used in coding set selection
  213. int a_avail, c_avail;
  214. uint8_t *mb_type_base, *mb_type[3];
  215. /** Luma compensation parameters */
  216. //@{
  217. uint8_t lumscale;
  218. uint8_t lumshift;
  219. //@}
  220. int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
  221. uint8_t halfpq; ///< Uniform quant over image and qp+.5
  222. uint8_t respic; ///< Frame-level flag for resized images
  223. int buffer_fullness; ///< HRD info
  224. /** Ranges:
  225. * -# 0 -> [-64n 63.f] x [-32, 31.f]
  226. * -# 1 -> [-128, 127.f] x [-64, 63.f]
  227. * -# 2 -> [-512, 511.f] x [-128, 127.f]
  228. * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
  229. */
  230. uint8_t mvrange;
  231. uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
  232. VLC *cbpcy_vlc; ///< CBPCY VLC table
  233. int tt_index; ///< Index for Transform Type tables
  234. uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
  235. uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs
  236. int mv_type_is_raw; ///< mv type mb plane is not coded
  237. int dmb_is_raw; ///< direct mb plane is raw
  238. int skip_is_raw; ///< skip mb plane is not coded
  239. uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
  240. int use_ic; ///< use intensity compensation in B-frames
  241. int rnd; ///< rounding control
  242. /** Frame decoding info for S/M profiles only */
  243. //@{
  244. uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
  245. uint8_t interpfrm;
  246. //@}
  247. /** Frame decoding info for Advanced profile */
  248. //@{
  249. uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
  250. uint8_t numpanscanwin;
  251. uint8_t tfcntr;
  252. uint8_t rptfrm, tff, rff;
  253. uint16_t topleftx;
  254. uint16_t toplefty;
  255. uint16_t bottomrightx;
  256. uint16_t bottomrighty;
  257. uint8_t uvsamp;
  258. uint8_t postproc;
  259. int hrd_num_leaky_buckets;
  260. uint8_t bit_rate_exponent;
  261. uint8_t buffer_size_exponent;
  262. uint8_t* acpred_plane; ///< AC prediction flags bitplane
  263. int acpred_is_raw;
  264. uint8_t* over_flags_plane; ///< Overflags bitplane
  265. int overflg_is_raw;
  266. uint8_t condover;
  267. uint16_t *hrd_rate, *hrd_buffer;
  268. uint8_t *hrd_fullness;
  269. uint8_t range_mapy_flag;
  270. uint8_t range_mapuv_flag;
  271. uint8_t range_mapy;
  272. uint8_t range_mapuv;
  273. //@}
  274. int p_frame_skipped;
  275. int bi_type;
  276. } VC1Context;