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.

387 lines
11KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.h
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #ifndef H264_H
  28. #define H264_H
  29. #include "dsputil.h"
  30. #include "cabac.h"
  31. #include "mpegvideo.h"
  32. #define interlaced_dct interlaced_dct_is_a_bad_name
  33. #define mb_intra mb_intra_isnt_initalized_see_mb_type
  34. #define LUMA_DC_BLOCK_INDEX 25
  35. #define CHROMA_DC_BLOCK_INDEX 26
  36. #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
  37. #define COEFF_TOKEN_VLC_BITS 8
  38. #define TOTAL_ZEROS_VLC_BITS 9
  39. #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
  40. #define RUN_VLC_BITS 3
  41. #define RUN7_VLC_BITS 6
  42. #define MAX_SPS_COUNT 32
  43. #define MAX_PPS_COUNT 256
  44. #define MAX_MMCO_COUNT 66
  45. /* Compiling in interlaced support reduces the speed
  46. * of progressive decoding by about 2%. */
  47. #define ALLOW_INTERLACE
  48. #ifdef ALLOW_INTERLACE
  49. #define MB_MBAFF h->mb_mbaff
  50. #define MB_FIELD h->mb_field_decoding_flag
  51. #define FRAME_MBAFF h->mb_aff_frame
  52. #else
  53. #define MB_MBAFF 0
  54. #define MB_FIELD 0
  55. #define FRAME_MBAFF 0
  56. #undef IS_INTERLACED
  57. #define IS_INTERLACED(mb_type) 0
  58. #endif
  59. /**
  60. * Sequence parameter set
  61. */
  62. typedef struct SPS{
  63. int profile_idc;
  64. int level_idc;
  65. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  66. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  67. int poc_type; ///< pic_order_cnt_type
  68. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  69. int delta_pic_order_always_zero_flag;
  70. int offset_for_non_ref_pic;
  71. int offset_for_top_to_bottom_field;
  72. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  73. int ref_frame_count; ///< num_ref_frames
  74. int gaps_in_frame_num_allowed_flag;
  75. int mb_width; ///< frame_width_in_mbs_minus1 + 1
  76. int mb_height; ///< frame_height_in_mbs_minus1 + 1
  77. int frame_mbs_only_flag;
  78. int mb_aff; ///<mb_adaptive_frame_field_flag
  79. int direct_8x8_inference_flag;
  80. int crop; ///< frame_cropping_flag
  81. int crop_left; ///< frame_cropping_rect_left_offset
  82. int crop_right; ///< frame_cropping_rect_right_offset
  83. int crop_top; ///< frame_cropping_rect_top_offset
  84. int crop_bottom; ///< frame_cropping_rect_bottom_offset
  85. int vui_parameters_present_flag;
  86. AVRational sar;
  87. int timing_info_present_flag;
  88. uint32_t num_units_in_tick;
  89. uint32_t time_scale;
  90. int fixed_frame_rate_flag;
  91. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  92. int bitstream_restriction_flag;
  93. int num_reorder_frames;
  94. int scaling_matrix_present;
  95. uint8_t scaling_matrix4[6][16];
  96. uint8_t scaling_matrix8[2][64];
  97. }SPS;
  98. /**
  99. * Picture parameter set
  100. */
  101. typedef struct PPS{
  102. unsigned int sps_id;
  103. int cabac; ///< entropy_coding_mode_flag
  104. int pic_order_present; ///< pic_order_present_flag
  105. int slice_group_count; ///< num_slice_groups_minus1 + 1
  106. int mb_slice_group_map_type;
  107. unsigned int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  108. int weighted_pred; ///< weighted_pred_flag
  109. int weighted_bipred_idc;
  110. int init_qp; ///< pic_init_qp_minus26 + 26
  111. int init_qs; ///< pic_init_qs_minus26 + 26
  112. int chroma_qp_index_offset;
  113. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  114. int constrained_intra_pred; ///< constrained_intra_pred_flag
  115. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  116. int transform_8x8_mode; ///< transform_8x8_mode_flag
  117. uint8_t scaling_matrix4[6][16];
  118. uint8_t scaling_matrix8[2][64];
  119. }PPS;
  120. /**
  121. * Memory management control operation opcode.
  122. */
  123. typedef enum MMCOOpcode{
  124. MMCO_END=0,
  125. MMCO_SHORT2UNUSED,
  126. MMCO_LONG2UNUSED,
  127. MMCO_SHORT2LONG,
  128. MMCO_SET_MAX_LONG,
  129. MMCO_RESET,
  130. MMCO_LONG,
  131. } MMCOOpcode;
  132. /**
  133. * Memory management control operation.
  134. */
  135. typedef struct MMCO{
  136. MMCOOpcode opcode;
  137. int short_frame_num;
  138. int long_index;
  139. } MMCO;
  140. /**
  141. * H264Context
  142. */
  143. typedef struct H264Context{
  144. MpegEncContext s;
  145. int nal_ref_idc;
  146. int nal_unit_type;
  147. uint8_t *rbsp_buffer;
  148. unsigned int rbsp_buffer_size;
  149. /**
  150. * Used to parse AVC variant of h264
  151. */
  152. int is_avc; ///< this flag is != 0 if codec is avc1
  153. int got_avcC; ///< flag used to parse avcC data only once
  154. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  155. int chroma_qp; //QPc
  156. int prev_mb_skipped;
  157. int next_mb_skipped;
  158. //prediction stuff
  159. int chroma_pred_mode;
  160. int intra16x16_pred_mode;
  161. int top_mb_xy;
  162. int left_mb_xy[2];
  163. int8_t intra4x4_pred_mode_cache[5*8];
  164. int8_t (*intra4x4_pred_mode)[8];
  165. void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
  166. void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
  167. void (*pred8x8 [4+3])(uint8_t *src, int stride);
  168. void (*pred16x16[4+3])(uint8_t *src, int stride);
  169. unsigned int topleft_samples_available;
  170. unsigned int top_samples_available;
  171. unsigned int topright_samples_available;
  172. unsigned int left_samples_available;
  173. uint8_t (*top_borders[2])[16+2*8];
  174. uint8_t left_border[2*(17+2*9)];
  175. /**
  176. * non zero coeff count cache.
  177. * is 64 if not available.
  178. */
  179. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  180. uint8_t (*non_zero_count)[16];
  181. /**
  182. * Motion vector cache.
  183. */
  184. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  185. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  186. #define LIST_NOT_USED -1 //FIXME rename?
  187. #define PART_NOT_AVAILABLE -2
  188. /**
  189. * is 1 if the specific list MV&references are set to 0,0,-2.
  190. */
  191. int mv_cache_clean[2];
  192. /**
  193. * number of neighbors (top and/or left) that used 8x8 dct
  194. */
  195. int neighbor_transform_size;
  196. /**
  197. * block_offset[ 0..23] for frame macroblocks
  198. * block_offset[24..47] for field macroblocks
  199. */
  200. int block_offset[2*(16+8)];
  201. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  202. uint32_t *mb2b8_xy;
  203. int b_stride; //FIXME use s->b4_stride
  204. int b8_stride;
  205. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  206. int mb_uvlinesize;
  207. int emu_edge_width;
  208. int emu_edge_height;
  209. int halfpel_flag;
  210. int thirdpel_flag;
  211. int unknown_svq3_flag;
  212. int next_slice_index;
  213. SPS sps_buffer[MAX_SPS_COUNT];
  214. SPS sps; ///< current sps
  215. PPS pps_buffer[MAX_PPS_COUNT];
  216. /**
  217. * current pps
  218. */
  219. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  220. uint32_t dequant4_buffer[6][52][16];
  221. uint32_t dequant8_buffer[2][52][64];
  222. uint32_t (*dequant4_coeff[6])[16];
  223. uint32_t (*dequant8_coeff[2])[64];
  224. int dequant_coeff_pps; ///< reinit tables when pps changes
  225. int slice_num;
  226. uint8_t *slice_table_base;
  227. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  228. int slice_type;
  229. int slice_type_fixed;
  230. //interlacing specific flags
  231. int mb_aff_frame;
  232. int mb_field_decoding_flag;
  233. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  234. unsigned int sub_mb_type[4];
  235. //POC stuff
  236. int poc_lsb;
  237. int poc_msb;
  238. int delta_poc_bottom;
  239. int delta_poc[2];
  240. int frame_num;
  241. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  242. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  243. int frame_num_offset; ///< for POC type 2
  244. int prev_frame_num_offset; ///< for POC type 2
  245. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  246. /**
  247. * frame_num for frames or 2*frame_num for field pics.
  248. */
  249. int curr_pic_num;
  250. /**
  251. * max_frame_num or 2*max_frame_num for field pics.
  252. */
  253. int max_pic_num;
  254. //Weighted pred stuff
  255. int use_weight;
  256. int use_weight_chroma;
  257. int luma_log2_weight_denom;
  258. int chroma_log2_weight_denom;
  259. int luma_weight[2][48];
  260. int luma_offset[2][48];
  261. int chroma_weight[2][48][2];
  262. int chroma_offset[2][48][2];
  263. int implicit_weight[48][48];
  264. //deblock
  265. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  266. int slice_alpha_c0_offset;
  267. int slice_beta_offset;
  268. int redundant_pic_count;
  269. int direct_spatial_mv_pred;
  270. int dist_scale_factor[16];
  271. int dist_scale_factor_field[32];
  272. int map_col_to_list0[2][16];
  273. int map_col_to_list0_field[2][32];
  274. /**
  275. * num_ref_idx_l0/1_active_minus1 + 1
  276. */
  277. unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  278. unsigned int list_count;
  279. Picture *short_ref[32];
  280. Picture *long_ref[32];
  281. Picture default_ref_list[2][32];
  282. Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
  283. Picture *delayed_pic[18]; //FIXME size?
  284. Picture *delayed_output_pic;
  285. /**
  286. * memory management control operations buffer.
  287. */
  288. MMCO mmco[MAX_MMCO_COUNT];
  289. int mmco_index;
  290. int long_ref_count; ///< number of actual long term references
  291. int short_ref_count; ///< number of actual short term references
  292. //data partitioning
  293. GetBitContext intra_gb;
  294. GetBitContext inter_gb;
  295. GetBitContext *intra_gb_ptr;
  296. GetBitContext *inter_gb_ptr;
  297. DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
  298. DCTELEM mb_padding[256]; ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not to large or ensure that there is some unused stuff after mb
  299. /**
  300. * Cabac
  301. */
  302. CABACContext cabac;
  303. uint8_t cabac_state[460];
  304. int cabac_init_idc;
  305. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  306. uint16_t *cbp_table;
  307. int cbp;
  308. int top_cbp;
  309. int left_cbp;
  310. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  311. uint8_t *chroma_pred_mode_table;
  312. int last_qscale_diff;
  313. int16_t (*mvd_table[2])[2];
  314. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  315. uint8_t *direct_table;
  316. uint8_t direct_cache[5*8];
  317. uint8_t zigzag_scan[16];
  318. uint8_t zigzag_scan8x8[64];
  319. uint8_t zigzag_scan8x8_cavlc[64];
  320. uint8_t field_scan[16];
  321. uint8_t field_scan8x8[64];
  322. uint8_t field_scan8x8_cavlc[64];
  323. const uint8_t *zigzag_scan_q0;
  324. const uint8_t *zigzag_scan8x8_q0;
  325. const uint8_t *zigzag_scan8x8_cavlc_q0;
  326. const uint8_t *field_scan_q0;
  327. const uint8_t *field_scan8x8_q0;
  328. const uint8_t *field_scan8x8_cavlc_q0;
  329. int x264_build;
  330. }H264Context;
  331. #endif /* H264_H */