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