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.

370 lines
11KB

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