#include "cabac.h"
#include "get_bits.h"
#include "h2645_parse.h"
+#include "hevc.h"
+#include "hevc_ps.h"
+#include "hevc_sei.h"
#include "hevcdsp.h"
#include "internal.h"
#include "thread.h"
#include "videodsp.h"
-#define MAX_DPB_SIZE 16 // A.4.1
-#define MAX_REFS 16
-
-/**
- * 7.4.2.1
- */
-#define MAX_SUB_LAYERS 7
-#define MAX_VPS_COUNT 16
-#define MAX_SPS_COUNT 32
-#define MAX_PPS_COUNT 256
-#define MAX_SHORT_TERM_RPS_COUNT 64
-#define MAX_CU_SIZE 128
-
//TODO: check if this is really the maximum
#define MAX_TRANSFORM_DEPTH 5
#define MAX_TB_SIZE 32
#define MAX_PB_SIZE 64
-#define MAX_LOG2_CTB_SIZE 6
#define MAX_QP 51
#define DEFAULT_INTRA_TC_OFFSET 2
#define SAMPLE(tab, x, y) ((tab)[(y) * s->sps->width + (x)])
#define SAMPLE_CTB(tab, x, y) ((tab)[(y) * min_cb_width + (x)])
-#define IS_IDR(s) (s->nal_unit_type == NAL_IDR_W_RADL || s->nal_unit_type == NAL_IDR_N_LP)
-#define IS_BLA(s) (s->nal_unit_type == NAL_BLA_W_RADL || s->nal_unit_type == NAL_BLA_W_LP || \
- s->nal_unit_type == NAL_BLA_N_LP)
+#define IS_IDR(s) (s->nal_unit_type == HEVC_NAL_IDR_W_RADL || s->nal_unit_type == HEVC_NAL_IDR_N_LP)
+#define IS_BLA(s) (s->nal_unit_type == HEVC_NAL_BLA_W_RADL || s->nal_unit_type == HEVC_NAL_BLA_W_LP || \
+ s->nal_unit_type == HEVC_NAL_BLA_N_LP)
#define IS_IRAP(s) (s->nal_unit_type >= 16 && s->nal_unit_type <= 23)
#define FFUDIV(a,b) (((a) > 0 ? (a) : (a) - (b) + 1) / (b))
#define FFUMOD(a,b) ((a) - (b) * FFUDIV(a,b))
-/**
- * Table 7-3: NAL unit type codes
- */
-enum NALUnitType {
- NAL_TRAIL_N = 0,
- NAL_TRAIL_R = 1,
- NAL_TSA_N = 2,
- NAL_TSA_R = 3,
- NAL_STSA_N = 4,
- NAL_STSA_R = 5,
- NAL_RADL_N = 6,
- NAL_RADL_R = 7,
- NAL_RASL_N = 8,
- NAL_RASL_R = 9,
- NAL_BLA_W_LP = 16,
- NAL_BLA_W_RADL = 17,
- NAL_BLA_N_LP = 18,
- NAL_IDR_W_RADL = 19,
- NAL_IDR_N_LP = 20,
- NAL_CRA_NUT = 21,
- NAL_VPS = 32,
- NAL_SPS = 33,
- NAL_PPS = 34,
- NAL_AUD = 35,
- NAL_EOS_NUT = 36,
- NAL_EOB_NUT = 37,
- NAL_FD_NUT = 38,
- NAL_SEI_PREFIX = 39,
- NAL_SEI_SUFFIX = 40,
-};
-
enum RPSType {
ST_CURR_BEF = 0,
ST_CURR_AFT,
NB_RPS_TYPE,
};
-enum SliceType {
- B_SLICE = 0,
- P_SLICE = 1,
- I_SLICE = 2,
-};
-
enum SyntaxElement {
SAO_MERGE_FLAG = 0,
SAO_TYPE_IDX,
SCAN_VERT,
};
-typedef struct ShortTermRPS {
- unsigned int num_negative_pics;
- int num_delta_pocs;
- int rps_idx_num_delta_pocs;
- int32_t delta_poc[32];
- uint8_t used[32];
-} ShortTermRPS;
-
typedef struct LongTermRPS {
int poc[32];
uint8_t used[32];
} LongTermRPS;
typedef struct RefPicList {
- struct HEVCFrame *ref[MAX_REFS];
- int list[MAX_REFS];
- int isLongTerm[MAX_REFS];
+ struct HEVCFrame *ref[HEVC_MAX_REFS];
+ int list[HEVC_MAX_REFS];
+ int isLongTerm[HEVC_MAX_REFS];
int nb_refs;
} RefPicList;
RefPicList refPicList[2];
} RefPicListTab;
-typedef struct HEVCWindow {
- unsigned int left_offset;
- unsigned int right_offset;
- unsigned int top_offset;
- unsigned int bottom_offset;
-} HEVCWindow;
-
-typedef struct VUI {
- AVRational sar;
-
- int overscan_info_present_flag;
- int overscan_appropriate_flag;
-
- int video_signal_type_present_flag;
- int video_format;
- int video_full_range_flag;
- int colour_description_present_flag;
- uint8_t colour_primaries;
- uint8_t transfer_characteristic;
- uint8_t matrix_coeffs;
-
- int chroma_loc_info_present_flag;
- int chroma_sample_loc_type_top_field;
- int chroma_sample_loc_type_bottom_field;
- int neutra_chroma_indication_flag;
-
- int field_seq_flag;
- int frame_field_info_present_flag;
-
- int default_display_window_flag;
- HEVCWindow def_disp_win;
-
- int vui_timing_info_present_flag;
- uint32_t vui_num_units_in_tick;
- uint32_t vui_time_scale;
- int vui_poc_proportional_to_timing_flag;
- int vui_num_ticks_poc_diff_one_minus1;
- int vui_hrd_parameters_present_flag;
-
- int bitstream_restriction_flag;
- int tiles_fixed_structure_flag;
- int motion_vectors_over_pic_boundaries_flag;
- int restricted_ref_pic_lists_flag;
- int min_spatial_segmentation_idc;
- int max_bytes_per_pic_denom;
- int max_bits_per_min_cu_denom;
- int log2_max_mv_length_horizontal;
- int log2_max_mv_length_vertical;
-} VUI;
-
-typedef struct PTLCommon {
- uint8_t profile_space;
- uint8_t tier_flag;
- uint8_t profile_idc;
- uint8_t profile_compatibility_flag[32];
- uint8_t level_idc;
- uint8_t progressive_source_flag;
- uint8_t interlaced_source_flag;
- uint8_t non_packed_constraint_flag;
- uint8_t frame_only_constraint_flag;
-} PTLCommon;
-
-typedef struct PTL {
- PTLCommon general_ptl;
- PTLCommon sub_layer_ptl[MAX_SUB_LAYERS];
-
- uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
- uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
-} PTL;
-
-typedef struct HEVCVPS {
- uint8_t vps_temporal_id_nesting_flag;
- int vps_max_layers;
- int vps_max_sub_layers; ///< vps_max_temporal_layers_minus1 + 1
-
- PTL ptl;
- int vps_sub_layer_ordering_info_present_flag;
- unsigned int vps_max_dec_pic_buffering[MAX_SUB_LAYERS];
- unsigned int vps_num_reorder_pics[MAX_SUB_LAYERS];
- unsigned int vps_max_latency_increase[MAX_SUB_LAYERS];
- int vps_max_layer_id;
- int vps_num_layer_sets; ///< vps_num_layer_sets_minus1 + 1
- uint8_t vps_timing_info_present_flag;
- uint32_t vps_num_units_in_tick;
- uint32_t vps_time_scale;
- uint8_t vps_poc_proportional_to_timing_flag;
- int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
- int vps_num_hrd_parameters;
-} HEVCVPS;
-
-typedef struct ScalingList {
- /* This is a little wasteful, since sizeID 0 only needs 8 coeffs,
- * and size ID 3 only has 2 arrays, not 6. */
- uint8_t sl[4][6][64];
- uint8_t sl_dc[2][6];
-} ScalingList;
-
-typedef struct HEVCSPS {
- int vps_id;
- int chroma_format_idc;
- uint8_t separate_colour_plane_flag;
-
- ///< output (i.e. cropped) values
- int output_width, output_height;
- HEVCWindow output_window;
-
- HEVCWindow pic_conf_win;
-
- int bit_depth;
- int pixel_shift;
- enum AVPixelFormat pix_fmt;
-
- unsigned int log2_max_poc_lsb;
- int pcm_enabled_flag;
-
- int max_sub_layers;
- struct {
- int max_dec_pic_buffering;
- int num_reorder_pics;
- int max_latency_increase;
- } temporal_layer[MAX_SUB_LAYERS];
-
- VUI vui;
- PTL ptl;
-
- uint8_t scaling_list_enable_flag;
- ScalingList scaling_list;
-
- unsigned int nb_st_rps;
- ShortTermRPS st_rps[MAX_SHORT_TERM_RPS_COUNT];
-
- uint8_t amp_enabled_flag;
- uint8_t sao_enabled;
-
- uint8_t long_term_ref_pics_present_flag;
- uint16_t lt_ref_pic_poc_lsb_sps[32];
- uint8_t used_by_curr_pic_lt_sps_flag[32];
- uint8_t num_long_term_ref_pics_sps;
-
- struct {
- uint8_t bit_depth;
- uint8_t bit_depth_chroma;
- unsigned int log2_min_pcm_cb_size;
- unsigned int log2_max_pcm_cb_size;
- uint8_t loop_filter_disable_flag;
- } pcm;
- uint8_t sps_temporal_mvp_enabled_flag;
- uint8_t sps_strong_intra_smoothing_enable_flag;
-
- unsigned int log2_min_cb_size;
- unsigned int log2_diff_max_min_coding_block_size;
- unsigned int log2_min_tb_size;
- unsigned int log2_max_trafo_size;
- unsigned int log2_ctb_size;
- unsigned int log2_min_pu_size;
-
- int max_transform_hierarchy_depth_inter;
- int max_transform_hierarchy_depth_intra;
-
- ///< coded frame dimension in various units
- int width;
- int height;
- int ctb_width;
- int ctb_height;
- int ctb_size;
- int min_cb_width;
- int min_cb_height;
- int min_tb_width;
- int min_tb_height;
- int min_pu_width;
- int min_pu_height;
-
- int hshift[3];
- int vshift[3];
-
- int qp_bd_offset;
-} HEVCSPS;
-
-typedef struct HEVCPPS {
- unsigned int sps_id; ///< seq_parameter_set_id
-
- uint8_t sign_data_hiding_flag;
-
- uint8_t cabac_init_present_flag;
-
- int num_ref_idx_l0_default_active; ///< num_ref_idx_l0_default_active_minus1 + 1
- int num_ref_idx_l1_default_active; ///< num_ref_idx_l1_default_active_minus1 + 1
- int pic_init_qp_minus26;
-
- uint8_t constrained_intra_pred_flag;
- uint8_t transform_skip_enabled_flag;
-
- uint8_t cu_qp_delta_enabled_flag;
- int diff_cu_qp_delta_depth;
-
- int cb_qp_offset;
- int cr_qp_offset;
- uint8_t pic_slice_level_chroma_qp_offsets_present_flag;
- uint8_t weighted_pred_flag;
- uint8_t weighted_bipred_flag;
- uint8_t output_flag_present_flag;
- uint8_t transquant_bypass_enable_flag;
-
- uint8_t dependent_slice_segments_enabled_flag;
- uint8_t tiles_enabled_flag;
- uint8_t entropy_coding_sync_enabled_flag;
-
- int num_tile_columns; ///< num_tile_columns_minus1 + 1
- int num_tile_rows; ///< num_tile_rows_minus1 + 1
- uint8_t uniform_spacing_flag;
- uint8_t loop_filter_across_tiles_enabled_flag;
-
- uint8_t seq_loop_filter_across_slices_enabled_flag;
-
- uint8_t deblocking_filter_control_present_flag;
- uint8_t deblocking_filter_override_enabled_flag;
- uint8_t disable_dbf;
- int beta_offset; ///< beta_offset_div2 * 2
- int tc_offset; ///< tc_offset_div2 * 2
-
- uint8_t scaling_list_data_present_flag;
- ScalingList scaling_list;
-
- uint8_t lists_modification_present_flag;
- int log2_parallel_merge_level; ///< log2_parallel_merge_level_minus2 + 2
- int num_extra_slice_header_bits;
- uint8_t slice_header_extension_present_flag;
-
- // Inferred parameters
- unsigned int *column_width; ///< ColumnWidth
- unsigned int *row_height; ///< RowHeight
- unsigned int *col_bd; ///< ColBd
- unsigned int *row_bd; ///< RowBd
- int *col_idxX;
-
- int *ctb_addr_rs_to_ts; ///< CtbAddrRSToTS
- int *ctb_addr_ts_to_rs; ///< CtbAddrTSToRS
- int *tile_id; ///< TileId
- int *tile_pos_rs; ///< TilePosRS
- int *min_tb_addr_zs; ///< MinTbAddrZS
-} HEVCPPS;
-
-typedef struct HEVCParamSets {
- AVBufferRef *vps_list[MAX_VPS_COUNT];
- AVBufferRef *sps_list[MAX_SPS_COUNT];
- AVBufferRef *pps_list[MAX_PPS_COUNT];
-
- /* currently active parameter sets */
- const HEVCVPS *vps;
- const HEVCSPS *sps;
- const HEVCPPS *pps;
-} HEVCParamSets;
-
typedef struct SliceHeader {
unsigned int pps_id;
///< address (in raster order) of the first block in the current slice
unsigned int slice_addr;
- enum SliceType slice_type;
+ enum HEVCSliceType slice_type;
int pic_order_cnt_lsb;
int poc;
struct HEVCFrame *collocated_ref;
- HEVCWindow window;
-
AVBufferRef *tab_mvf_buf;
AVBufferRef *rpl_tab_buf;
AVBufferRef *rpl_buf;
AVFrame *output_frame;
HEVCParamSets ps;
+ HEVCSEI sei;
+ struct AVMD5 *md5_ctx;
AVBufferPool *tab_mvf_pool;
AVBufferPool *rpl_tab_pool;
SliceHeader sh;
SAOParams *sao;
DBParams *deblock;
- enum NALUnitType nal_unit_type;
+ enum HEVCNALUnitType nal_unit_type;
int temporal_id; ///< temporal_id_plus1 - 1
HEVCFrame *ref;
HEVCFrame DPB[32];
H2645Packet pkt;
// type of the first VCL NAL of the current frame
- enum NALUnitType first_nal_type;
-
- // for checking the frame checksums
- struct AVMD5 *md5_ctx;
- uint8_t md5[3][16];
- uint8_t is_md5;
+ enum HEVCNALUnitType first_nal_type;
uint8_t context_initialized;
uint8_t is_nalff; ///< this flag is != 0 if bitstream is encapsulated
int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
int nuh_layer_id;
-
- /** frame packing arrangement variables */
- int sei_frame_packing_present;
- int frame_packing_arrangement_type;
- int content_interpretation_type;
- int quincunx_subsampling;
-
- /** display orientation */
- int sei_display_orientation_present;
- int sei_anticlockwise_rotation;
- int sei_hflip, sei_vflip;
} HEVCContext;
-int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
- ShortTermRPS *rps, const HEVCSPS *sps, int is_slice_header);
-
-/**
- * Parse the SPS from the bitstream into the provided HEVCSPS struct.
- *
- * @param sps_id the SPS id will be written here
- * @param apply_defdispwin if set 1, the default display window from the VUI
- * will be applied to the video dimensions
- * @param vps_list if non-NULL, this function will validate that the SPS refers
- * to an existing VPS
- */
-int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id,
- int apply_defdispwin, AVBufferRef **vps_list, AVCodecContext *avctx);
-
-int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
- HEVCParamSets *ps);
-int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
- HEVCParamSets *ps, int apply_defdispwin);
-int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx,
- HEVCParamSets *ps);
-int ff_hevc_decode_nal_sei(HEVCContext *s);
-
/**
* Mark all frames in DPB as unused for reference.
*/
void ff_hevc_hls_filter(HEVCContext *s, int x, int y);
void ff_hevc_hls_filters(HEVCContext *s, int x_ctb, int y_ctb, int ctb_size);
-void ff_hevc_pps_free(HEVCPPS **ppps);
-
void ff_hevc_pred_init(HEVCPredContext *hpc, int bit_depth);
-int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
- uint8_t *buf, int buf_size);
-
extern const uint8_t ff_hevc_qpel_extra_before[4];
extern const uint8_t ff_hevc_qpel_extra_after[4];
extern const uint8_t ff_hevc_qpel_extra[4];
-extern const uint8_t ff_hevc_diag_scan4x4_x[16];
-extern const uint8_t ff_hevc_diag_scan4x4_y[16];
-extern const uint8_t ff_hevc_diag_scan8x8_x[64];
-extern const uint8_t ff_hevc_diag_scan8x8_y[64];
-
#endif /* AVCODEC_HEVCDEC_H */