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.

439 lines
13KB

  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. * @file h264.h
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #ifndef AVCODEC_H264_H
  27. #define AVCODEC_H264_H
  28. #include "dsputil.h"
  29. #include "cabac.h"
  30. #include "mpegvideo.h"
  31. #include "h264pred.h"
  32. #define interlaced_dct interlaced_dct_is_a_bad_name
  33. #define mb_intra mb_intra_is_not_initialized_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. #define MAX_DELAYED_PIC_COUNT 16
  46. /* Compiling in interlaced support reduces the speed
  47. * of progressive decoding by about 2%. */
  48. #define ALLOW_INTERLACE
  49. #define ALLOW_NOCHROMA
  50. #ifdef ALLOW_INTERLACE
  51. #define MB_MBAFF h->mb_mbaff
  52. #define MB_FIELD h->mb_field_decoding_flag
  53. #define FRAME_MBAFF h->mb_aff_frame
  54. #define FIELD_PICTURE (s->picture_structure != PICT_FRAME)
  55. #else
  56. #define MB_MBAFF 0
  57. #define MB_FIELD 0
  58. #define FRAME_MBAFF 0
  59. #define FIELD_PICTURE 0
  60. #undef IS_INTERLACED
  61. #define IS_INTERLACED(mb_type) 0
  62. #endif
  63. #define FIELD_OR_MBAFF_PICTURE (FRAME_MBAFF || FIELD_PICTURE)
  64. #ifdef ALLOW_NOCHROMA
  65. #define CHROMA h->sps.chroma_format_idc
  66. #else
  67. #define CHROMA 1
  68. #endif
  69. #ifndef ENABLE_H264_ENCODER
  70. #define ENABLE_H264_ENCODER 0
  71. #endif
  72. /**
  73. * Sequence parameter set
  74. */
  75. typedef struct SPS{
  76. int profile_idc;
  77. int level_idc;
  78. int chroma_format_idc;
  79. int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
  80. int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
  81. int poc_type; ///< pic_order_cnt_type
  82. int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
  83. int delta_pic_order_always_zero_flag;
  84. int offset_for_non_ref_pic;
  85. int offset_for_top_to_bottom_field;
  86. int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
  87. int ref_frame_count; ///< num_ref_frames
  88. int gaps_in_frame_num_allowed_flag;
  89. int mb_width; ///< pic_width_in_mbs_minus1 + 1
  90. int mb_height; ///< pic_height_in_map_units_minus1 + 1
  91. int frame_mbs_only_flag;
  92. int mb_aff; ///<mb_adaptive_frame_field_flag
  93. int direct_8x8_inference_flag;
  94. int crop; ///< frame_cropping_flag
  95. unsigned int crop_left; ///< frame_cropping_rect_left_offset
  96. unsigned int crop_right; ///< frame_cropping_rect_right_offset
  97. unsigned int crop_top; ///< frame_cropping_rect_top_offset
  98. unsigned int crop_bottom; ///< frame_cropping_rect_bottom_offset
  99. int vui_parameters_present_flag;
  100. AVRational sar;
  101. int timing_info_present_flag;
  102. uint32_t num_units_in_tick;
  103. uint32_t time_scale;
  104. int fixed_frame_rate_flag;
  105. short offset_for_ref_frame[256]; //FIXME dyn aloc?
  106. int bitstream_restriction_flag;
  107. int num_reorder_frames;
  108. int scaling_matrix_present;
  109. uint8_t scaling_matrix4[6][16];
  110. uint8_t scaling_matrix8[2][64];
  111. }SPS;
  112. /**
  113. * Picture parameter set
  114. */
  115. typedef struct PPS{
  116. unsigned int sps_id;
  117. int cabac; ///< entropy_coding_mode_flag
  118. int pic_order_present; ///< pic_order_present_flag
  119. int slice_group_count; ///< num_slice_groups_minus1 + 1
  120. int mb_slice_group_map_type;
  121. unsigned int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
  122. int weighted_pred; ///< weighted_pred_flag
  123. int weighted_bipred_idc;
  124. int init_qp; ///< pic_init_qp_minus26 + 26
  125. int init_qs; ///< pic_init_qs_minus26 + 26
  126. int chroma_qp_index_offset[2];
  127. int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
  128. int constrained_intra_pred; ///< constrained_intra_pred_flag
  129. int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
  130. int transform_8x8_mode; ///< transform_8x8_mode_flag
  131. uint8_t scaling_matrix4[6][16];
  132. uint8_t scaling_matrix8[2][64];
  133. uint8_t chroma_qp_table[2][64]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table
  134. int chroma_qp_diff;
  135. }PPS;
  136. /**
  137. * Memory management control operation opcode.
  138. */
  139. typedef enum MMCOOpcode{
  140. MMCO_END=0,
  141. MMCO_SHORT2UNUSED,
  142. MMCO_LONG2UNUSED,
  143. MMCO_SHORT2LONG,
  144. MMCO_SET_MAX_LONG,
  145. MMCO_RESET,
  146. MMCO_LONG,
  147. } MMCOOpcode;
  148. /**
  149. * Memory management control operation.
  150. */
  151. typedef struct MMCO{
  152. MMCOOpcode opcode;
  153. int short_pic_num; ///< pic_num without wrapping (pic_num & max_pic_num)
  154. int long_arg; ///< index, pic_num, or num long refs depending on opcode
  155. } MMCO;
  156. /**
  157. * H264Context
  158. */
  159. typedef struct H264Context{
  160. MpegEncContext s;
  161. int nal_ref_idc;
  162. int nal_unit_type;
  163. uint8_t *rbsp_buffer[2];
  164. unsigned int rbsp_buffer_size[2];
  165. /**
  166. * Used to parse AVC variant of h264
  167. */
  168. int is_avc; ///< this flag is != 0 if codec is avc1
  169. int got_avcC; ///< flag used to parse avcC data only once
  170. int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
  171. int chroma_qp[2]; //QPc
  172. int prev_mb_skipped;
  173. int next_mb_skipped;
  174. //prediction stuff
  175. int chroma_pred_mode;
  176. int intra16x16_pred_mode;
  177. int top_mb_xy;
  178. int left_mb_xy[2];
  179. int8_t intra4x4_pred_mode_cache[5*8];
  180. int8_t (*intra4x4_pred_mode)[8];
  181. H264PredContext hpc;
  182. unsigned int topleft_samples_available;
  183. unsigned int top_samples_available;
  184. unsigned int topright_samples_available;
  185. unsigned int left_samples_available;
  186. uint8_t (*top_borders[2])[16+2*8];
  187. uint8_t left_border[2*(17+2*9)];
  188. /**
  189. * non zero coeff count cache.
  190. * is 64 if not available.
  191. */
  192. DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]);
  193. uint8_t (*non_zero_count)[16];
  194. /**
  195. * Motion vector cache.
  196. */
  197. DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]);
  198. DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]);
  199. #define LIST_NOT_USED -1 //FIXME rename?
  200. #define PART_NOT_AVAILABLE -2
  201. /**
  202. * is 1 if the specific list MV&references are set to 0,0,-2.
  203. */
  204. int mv_cache_clean[2];
  205. /**
  206. * number of neighbors (top and/or left) that used 8x8 dct
  207. */
  208. int neighbor_transform_size;
  209. /**
  210. * block_offset[ 0..23] for frame macroblocks
  211. * block_offset[24..47] for field macroblocks
  212. */
  213. int block_offset[2*(16+8)];
  214. uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
  215. uint32_t *mb2b8_xy;
  216. int b_stride; //FIXME use s->b4_stride
  217. int b8_stride;
  218. int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff
  219. int mb_uvlinesize;
  220. int emu_edge_width;
  221. int emu_edge_height;
  222. int halfpel_flag;
  223. int thirdpel_flag;
  224. int unknown_svq3_flag;
  225. int next_slice_index;
  226. SPS *sps_buffers[MAX_SPS_COUNT];
  227. SPS sps; ///< current sps
  228. PPS *pps_buffers[MAX_PPS_COUNT];
  229. /**
  230. * current pps
  231. */
  232. PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
  233. uint32_t dequant4_buffer[6][52][16];
  234. uint32_t dequant8_buffer[2][52][64];
  235. uint32_t (*dequant4_coeff[6])[16];
  236. uint32_t (*dequant8_coeff[2])[64];
  237. int dequant_coeff_pps; ///< reinit tables when pps changes
  238. int slice_num;
  239. uint8_t *slice_table_base;
  240. uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
  241. int slice_type;
  242. int slice_type_nos; ///< S free slice type (SI/SP are remapped to I/P)
  243. int slice_type_fixed;
  244. //interlacing specific flags
  245. int mb_aff_frame;
  246. int mb_field_decoding_flag;
  247. int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
  248. unsigned int sub_mb_type[4];
  249. //POC stuff
  250. int poc_lsb;
  251. int poc_msb;
  252. int delta_poc_bottom;
  253. int delta_poc[2];
  254. int frame_num;
  255. int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
  256. int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
  257. int frame_num_offset; ///< for POC type 2
  258. int prev_frame_num_offset; ///< for POC type 2
  259. int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
  260. /**
  261. * frame_num for frames or 2*frame_num+1 for field pics.
  262. */
  263. int curr_pic_num;
  264. /**
  265. * max_frame_num or 2*max_frame_num for field pics.
  266. */
  267. int max_pic_num;
  268. //Weighted pred stuff
  269. int use_weight;
  270. int use_weight_chroma;
  271. int luma_log2_weight_denom;
  272. int chroma_log2_weight_denom;
  273. int luma_weight[2][48];
  274. int luma_offset[2][48];
  275. int chroma_weight[2][48][2];
  276. int chroma_offset[2][48][2];
  277. int implicit_weight[48][48];
  278. //deblock
  279. int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
  280. int slice_alpha_c0_offset;
  281. int slice_beta_offset;
  282. int redundant_pic_count;
  283. int direct_spatial_mv_pred;
  284. int dist_scale_factor[16];
  285. int dist_scale_factor_field[2][32];
  286. int map_col_to_list0[2][16+32];
  287. int map_col_to_list0_field[2][2][16+32];
  288. /**
  289. * num_ref_idx_l0/1_active_minus1 + 1
  290. */
  291. unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode
  292. unsigned int list_count;
  293. Picture *short_ref[32];
  294. Picture *long_ref[32];
  295. Picture default_ref_list[2][32]; ///< base reference list for all slices of a coded picture
  296. Picture ref_list[2][48]; /**< 0..15: frame refs, 16..47: mbaff field refs.
  297. Reordered version of default_ref_list
  298. according to picture reordering in slice header */
  299. int ref2frm[16][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
  300. Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2]; //FIXME size?
  301. int outputed_poc;
  302. /**
  303. * memory management control operations buffer.
  304. */
  305. MMCO mmco[MAX_MMCO_COUNT];
  306. int mmco_index;
  307. int long_ref_count; ///< number of actual long term references
  308. int short_ref_count; ///< number of actual short term references
  309. //data partitioning
  310. GetBitContext intra_gb;
  311. GetBitContext inter_gb;
  312. GetBitContext *intra_gb_ptr;
  313. GetBitContext *inter_gb_ptr;
  314. DECLARE_ALIGNED_16(DCTELEM, mb[16*24]);
  315. DCTELEM mb_padding[256]; ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not too large or ensure that there is some unused stuff after mb
  316. /**
  317. * Cabac
  318. */
  319. CABACContext cabac;
  320. uint8_t cabac_state[460];
  321. int cabac_init_idc;
  322. /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
  323. uint16_t *cbp_table;
  324. int cbp;
  325. int top_cbp;
  326. int left_cbp;
  327. /* chroma_pred_mode for i4x4 or i16x16, else 0 */
  328. uint8_t *chroma_pred_mode_table;
  329. int last_qscale_diff;
  330. int16_t (*mvd_table[2])[2];
  331. DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]);
  332. uint8_t *direct_table;
  333. uint8_t direct_cache[5*8];
  334. uint8_t zigzag_scan[16];
  335. uint8_t zigzag_scan8x8[64];
  336. uint8_t zigzag_scan8x8_cavlc[64];
  337. uint8_t field_scan[16];
  338. uint8_t field_scan8x8[64];
  339. uint8_t field_scan8x8_cavlc[64];
  340. const uint8_t *zigzag_scan_q0;
  341. const uint8_t *zigzag_scan8x8_q0;
  342. const uint8_t *zigzag_scan8x8_cavlc_q0;
  343. const uint8_t *field_scan_q0;
  344. const uint8_t *field_scan8x8_q0;
  345. const uint8_t *field_scan8x8_cavlc_q0;
  346. int x264_build;
  347. /**
  348. * @defgroup multithreading Members for slice based multithreading
  349. * @{
  350. */
  351. struct H264Context *thread_context[MAX_THREADS];
  352. /**
  353. * current slice number, used to initalize slice_num of each thread/context
  354. */
  355. int current_slice;
  356. /**
  357. * Max number of threads / contexts.
  358. * This is equal to AVCodecContext.thread_count unless
  359. * multithreaded decoding is impossible, in which case it is
  360. * reduced to 1.
  361. */
  362. int max_contexts;
  363. /**
  364. * 1 if the single thread fallback warning has already been
  365. * displayed, 0 otherwise.
  366. */
  367. int single_decode_warning;
  368. int last_slice_type;
  369. /** @} */
  370. int mb_xy;
  371. }H264Context;
  372. #endif /* AVCODEC_H264_H */