* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
*/
/**
- * @file h264.h
+ * @file libavcodec/h264.h
* H.264 / AVC / MPEG4 part10 codec.
* @author Michael Niedermayer <michaelni@gmx.at>
*/
-#ifndef H264_H
-#define H264_H
+#ifndef AVCODEC_H264_H
+#define AVCODEC_H264_H
#include "dsputil.h"
#include "cabac.h"
#include "mpegvideo.h"
+#include "h264pred.h"
#define interlaced_dct interlaced_dct_is_a_bad_name
-#define mb_intra mb_intra_isnt_initalized_see_mb_type
+#define mb_intra mb_intra_is_not_initialized_see_mb_type
#define LUMA_DC_BLOCK_INDEX 25
#define CHROMA_DC_BLOCK_INDEX 26
#define MAX_MMCO_COUNT 66
+#define MAX_DELAYED_PIC_COUNT 16
+
/* Compiling in interlaced support reduces the speed
* of progressive decoding by about 2%. */
#define ALLOW_INTERLACE
+#define ALLOW_NOCHROMA
+
+/**
+ * The maximum number of slices supported by the decoder.
+ * must be a power of 2
+ */
+#define MAX_SLICES 16
+
#ifdef ALLOW_INTERLACE
#define MB_MBAFF h->mb_mbaff
#define MB_FIELD h->mb_field_decoding_flag
#define FRAME_MBAFF h->mb_aff_frame
+#define FIELD_PICTURE (s->picture_structure != PICT_FRAME)
#else
#define MB_MBAFF 0
#define MB_FIELD 0
#define FRAME_MBAFF 0
+#define FIELD_PICTURE 0
#undef IS_INTERLACED
#define IS_INTERLACED(mb_type) 0
#endif
+#define FIELD_OR_MBAFF_PICTURE (FRAME_MBAFF || FIELD_PICTURE)
+
+#ifdef ALLOW_NOCHROMA
+#define CHROMA h->sps.chroma_format_idc
+#else
+#define CHROMA 1
+#endif
+
+#define EXTENDED_SAR 255
+
+#define MB_TYPE_REF0 MB_TYPE_ACPRED //dirty but it fits in 16 bit
+#define MB_TYPE_8x8DCT 0x01000000
+#define IS_REF0(a) ((a) & MB_TYPE_REF0)
+#define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
+
+/* NAL unit types */
+enum {
+ NAL_SLICE=1,
+ NAL_DPA,
+ NAL_DPB,
+ NAL_DPC,
+ NAL_IDR_SLICE,
+ NAL_SEI,
+ NAL_SPS,
+ NAL_PPS,
+ NAL_AUD,
+ NAL_END_SEQUENCE,
+ NAL_END_STREAM,
+ NAL_FILLER_DATA,
+ NAL_SPS_EXT,
+ NAL_AUXILIARY_SLICE=19
+};
+
+/**
+ * SEI message types
+ */
+typedef enum {
+ SEI_BUFFERING_PERIOD = 0, ///< buffering period (H.264, D.1.1)
+ SEI_TYPE_PIC_TIMING = 1, ///< picture timing
+ SEI_TYPE_USER_DATA_UNREGISTERED = 5, ///< unregistered user data
+ SEI_TYPE_RECOVERY_POINT = 6 ///< recovery point (frame # to decoder sync)
+} SEI_Type;
+
+/**
+ * pic_struct in picture timing SEI message
+ */
+typedef enum {
+ SEI_PIC_STRUCT_FRAME = 0, ///< 0: %frame
+ SEI_PIC_STRUCT_TOP_FIELD = 1, ///< 1: top field
+ SEI_PIC_STRUCT_BOTTOM_FIELD = 2, ///< 2: bottom field
+ SEI_PIC_STRUCT_TOP_BOTTOM = 3, ///< 3: top field, bottom field, in that order
+ SEI_PIC_STRUCT_BOTTOM_TOP = 4, ///< 4: bottom field, top field, in that order
+ SEI_PIC_STRUCT_TOP_BOTTOM_TOP = 5, ///< 5: top field, bottom field, top field repeated, in that order
+ SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM = 6, ///< 6: bottom field, top field, bottom field repeated, in that order
+ SEI_PIC_STRUCT_FRAME_DOUBLING = 7, ///< 7: %frame doubling
+ SEI_PIC_STRUCT_FRAME_TRIPLING = 8 ///< 8: %frame tripling
+} SEI_PicStructType;
/**
* Sequence parameter set
int profile_idc;
int level_idc;
+ int chroma_format_idc;
int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag
int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
int poc_type; ///< pic_order_cnt_type
int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
int ref_frame_count; ///< num_ref_frames
int gaps_in_frame_num_allowed_flag;
- int mb_width; ///< frame_width_in_mbs_minus1 + 1
- int mb_height; ///< frame_height_in_mbs_minus1 + 1
+ int mb_width; ///< pic_width_in_mbs_minus1 + 1
+ int mb_height; ///< pic_height_in_map_units_minus1 + 1
int frame_mbs_only_flag;
int mb_aff; ///<mb_adaptive_frame_field_flag
int direct_8x8_inference_flag;
int crop; ///< frame_cropping_flag
- int crop_left; ///< frame_cropping_rect_left_offset
- int crop_right; ///< frame_cropping_rect_right_offset
- int crop_top; ///< frame_cropping_rect_top_offset
- int crop_bottom; ///< frame_cropping_rect_bottom_offset
+ unsigned int crop_left; ///< frame_cropping_rect_left_offset
+ unsigned int crop_right; ///< frame_cropping_rect_right_offset
+ unsigned int crop_top; ///< frame_cropping_rect_top_offset
+ unsigned int crop_bottom; ///< frame_cropping_rect_bottom_offset
int vui_parameters_present_flag;
AVRational sar;
int timing_info_present_flag;
int scaling_matrix_present;
uint8_t scaling_matrix4[6][16];
uint8_t scaling_matrix8[2][64];
+ int nal_hrd_parameters_present_flag;
+ int vcl_hrd_parameters_present_flag;
+ int pic_struct_present_flag;
+ int time_offset_length;
+ int cpb_cnt; ///< See H.264 E.1.2
+ int initial_cpb_removal_delay_length; ///< initial_cpb_removal_delay_length_minus1 +1
+ int cpb_removal_delay_length; ///< cpb_removal_delay_length_minus1 + 1
+ int dpb_output_delay_length; ///< dpb_output_delay_length_minus1 + 1
+ int bit_depth_luma; ///< bit_depth_luma_minus8 + 8
+ int bit_depth_chroma; ///< bit_depth_chroma_minus8 + 8
+ int residual_color_transform_flag; ///< residual_colour_transform_flag
}SPS;
/**
int weighted_bipred_idc;
int init_qp; ///< pic_init_qp_minus26 + 26
int init_qs; ///< pic_init_qs_minus26 + 26
- int chroma_qp_index_offset;
+ int chroma_qp_index_offset[2];
int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
int constrained_intra_pred; ///< constrained_intra_pred_flag
int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
int transform_8x8_mode; ///< transform_8x8_mode_flag
uint8_t scaling_matrix4[6][16];
uint8_t scaling_matrix8[2][64];
+ uint8_t chroma_qp_table[2][64]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table
+ int chroma_qp_diff;
}PPS;
/**
*/
typedef struct MMCO{
MMCOOpcode opcode;
- int short_frame_num;
- int long_index;
+ int short_pic_num; ///< pic_num without wrapping (pic_num & max_pic_num)
+ int long_arg; ///< index, pic_num, or num long refs depending on opcode
} MMCO;
/**
MpegEncContext s;
int nal_ref_idc;
int nal_unit_type;
- uint8_t *rbsp_buffer;
- unsigned int rbsp_buffer_size;
+ uint8_t *rbsp_buffer[2];
+ unsigned int rbsp_buffer_size[2];
/**
* Used to parse AVC variant of h264
int got_avcC; ///< flag used to parse avcC data only once
int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
- int chroma_qp; //QPc
+ int chroma_qp[2]; //QPc
int prev_mb_skipped;
int next_mb_skipped;
int8_t intra4x4_pred_mode_cache[5*8];
int8_t (*intra4x4_pred_mode)[8];
- void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
- void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride);
- void (*pred8x8 [4+3])(uint8_t *src, int stride);
- void (*pred16x16[4+3])(uint8_t *src, int stride);
+ H264PredContext hpc;
unsigned int topleft_samples_available;
unsigned int top_samples_available;
unsigned int topright_samples_available;
int unknown_svq3_flag;
int next_slice_index;
- SPS sps_buffer[MAX_SPS_COUNT];
+ SPS *sps_buffers[MAX_SPS_COUNT];
SPS sps; ///< current sps
- PPS pps_buffer[MAX_PPS_COUNT];
+ PPS *pps_buffers[MAX_PPS_COUNT];
/**
* current pps
*/
int dequant_coeff_pps; ///< reinit tables when pps changes
int slice_num;
- uint8_t *slice_table_base;
- uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
+ uint16_t *slice_table_base;
+ uint16_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
int slice_type;
+ int slice_type_nos; ///< S free slice type (SI/SP are remapped to I/P)
int slice_type_fixed;
//interlacing specific flags
int mb_field_decoding_flag;
int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
- unsigned int sub_mb_type[4];
+ DECLARE_ALIGNED_8(uint16_t, sub_mb_type[4]);
//POC stuff
int poc_lsb;
int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
/**
- * frame_num for frames or 2*frame_num for field pics.
+ * frame_num for frames or 2*frame_num+1 for field pics.
*/
int curr_pic_num;
int direct_spatial_mv_pred;
int dist_scale_factor[16];
- int dist_scale_factor_field[32];
- int map_col_to_list0[2][16];
- int map_col_to_list0_field[2][32];
+ int dist_scale_factor_field[2][32];
+ int map_col_to_list0[2][16+32];
+ int map_col_to_list0_field[2][2][16+32];
/**
* num_ref_idx_l0/1_active_minus1 + 1
unsigned int list_count;
Picture *short_ref[32];
Picture *long_ref[32];
- Picture default_ref_list[2][32];
- Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs
- Picture *delayed_pic[18]; //FIXME size?
- Picture *delayed_output_pic;
+ Picture default_ref_list[2][32]; ///< base reference list for all slices of a coded picture
+ Picture ref_list[2][48]; /**< 0..15: frame refs, 16..47: mbaff field refs.
+ Reordered version of default_ref_list
+ according to picture reordering in slice header */
+ int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
+ Picture *delayed_pic[MAX_DELAYED_PIC_COUNT+2]; //FIXME size?
+ int outputed_poc;
/**
* memory management control operations buffer.
GetBitContext *intra_gb_ptr;
GetBitContext *inter_gb_ptr;
- DECLARE_ALIGNED_8(DCTELEM, mb[16*24]);
- 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
+ DECLARE_ALIGNED_16(DCTELEM, mb[16*24]);
+ 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
/**
* Cabac
const uint8_t *field_scan8x8_cavlc_q0;
int x264_build;
+
+ /**
+ * @defgroup multithreading Members for slice based multithreading
+ * @{
+ */
+ struct H264Context *thread_context[MAX_THREADS];
+
+ /**
+ * current slice number, used to initalize slice_num of each thread/context
+ */
+ int current_slice;
+
+ /**
+ * Max number of threads / contexts.
+ * This is equal to AVCodecContext.thread_count unless
+ * multithreaded decoding is impossible, in which case it is
+ * reduced to 1.
+ */
+ int max_contexts;
+
+ /**
+ * 1 if the single thread fallback warning has already been
+ * displayed, 0 otherwise.
+ */
+ int single_decode_warning;
+
+ int last_slice_type;
+ /** @} */
+
+ int mb_xy;
+
+ uint32_t svq3_watermark_key;
+
+ /**
+ * pic_struct in picture timing SEI message
+ */
+ SEI_PicStructType sei_pic_struct;
+
+ /**
+ * Bit set of clock types for fields/frames in picture timing SEI message.
+ * For each found ct_type, appropriate bit is set (e.g., bit 1 for
+ * interlaced).
+ */
+ int sei_ct_type;
+
+ /**
+ * dpb_output_delay in picture timing SEI message, see H.264 C.2.2
+ */
+ int sei_dpb_output_delay;
+
+ /**
+ * cpb_removal_delay in picture timing SEI message, see H.264 C.1.2
+ */
+ int sei_cpb_removal_delay;
+
+ /**
+ * recovery_frame_cnt from SEI message
+ *
+ * Set to -1 if no recovery point SEI message found or to number of frames
+ * before playback synchronizes. Frames having recovery point are key
+ * frames.
+ */
+ int sei_recovery_frame_cnt;
+
+ int is_complex;
+
+ int luma_weight_flag[2]; ///< 7.4.3.2 luma_weight_lX_flag
+ int chroma_weight_flag[2]; ///< 7.4.3.2 chroma_weight_lX_flag
+
+ // Timestamp stuff
+ int sei_buffering_period_present; ///< Buffering period SEI flag
+ int initial_cpb_removal_delay[32]; ///< Initial timestamps for CPBs
}H264Context;
-#endif /* H264_H */
+/**
+ * Decode SEI
+ */
+int ff_h264_decode_sei(H264Context *h);
+
+/**
+ * Decode SPS
+ */
+int ff_h264_decode_seq_parameter_set(H264Context *h);
+
+/**
+ * Decode PPS
+ */
+int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
+
+/**
+ * Decodes a network abstraction layer unit.
+ * @param consumed is the number of bytes used as input
+ * @param length is the length of the array
+ * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
+ * @returns decoded bytes, might be src+1 if no escapes
+ */
+const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
+
+/**
+ * identifies the exact end of the bitstream
+ * @return the length of the trailing, or 0 if damaged
+ */
+int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src);
+
+#endif /* AVCODEC_H264_H */