diff options
Diffstat (limited to 'libavcodec/h264.h')
-rw-r--r-- | libavcodec/h264.h | 164 |
1 files changed, 135 insertions, 29 deletions
diff --git a/libavcodec/h264.h b/libavcodec/h264.h index 1ba6e56..53dc477 100644 --- a/libavcodec/h264.h +++ b/libavcodec/h264.h @@ -2,20 +2,20 @@ * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -36,14 +36,15 @@ #include "h264dsp.h" #include "h264pred.h" #include "h264qpel.h" +#include "internal.h" // for avpriv_find_start_code() #include "mpegutils.h" #include "parser.h" #include "qpeldsp.h" #include "rectangle.h" #include "videodsp.h" -#define H264_MAX_PICTURE_COUNT 32 -#define H264_MAX_THREADS 16 +#define H264_MAX_PICTURE_COUNT 36 +#define H264_MAX_THREADS 32 #define MAX_SPS_COUNT 32 #define MAX_PPS_COUNT 256 @@ -52,6 +53,8 @@ #define MAX_DELAYED_PIC_COUNT 16 +#define MAX_MBPAIR_SIZE (256*1024) // a tighter bound could be calculated if someone cares about a few bytes + /* Compiling in interlaced support reduces the speed * of progressive decoding by about 2%. */ #define ALLOW_INTERLACE @@ -65,10 +68,10 @@ #define MAX_SLICES 32 #ifdef ALLOW_INTERLACE -#define MB_MBAFF(h) h->mb_mbaff -#define MB_FIELD(h) h->mb_field_decoding_flag -#define FRAME_MBAFF(h) h->mb_aff_frame -#define FIELD_PICTURE(h) (h->picture_structure != PICT_FRAME) +#define MB_MBAFF(h) (h)->mb_mbaff +#define MB_FIELD(h) (h)->mb_field_decoding_flag +#define FRAME_MBAFF(h) (h)->mb_aff_frame +#define FIELD_PICTURE(h) ((h)->picture_structure != PICT_FRAME) #define LEFT_MBS 2 #define LTOP 0 #define LBOT 1 @@ -88,11 +91,12 @@ #define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h)) #ifndef CABAC -#define CABAC(h) h->pps.cabac +#define CABAC(h) (h)->pps.cabac #endif -#define CHROMA422(h) (h->sps.chroma_format_idc == 2) -#define CHROMA444(h) (h->sps.chroma_format_idc == 3) +#define CHROMA(h) ((h)->sps.chroma_format_idc) +#define CHROMA422(h) ((h)->sps.chroma_format_idc == 2) +#define CHROMA444(h) ((h)->sps.chroma_format_idc == 3) #define EXTENDED_SAR 255 @@ -101,7 +105,7 @@ #define IS_REF0(a) ((a) & MB_TYPE_REF0) #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT) -#define QP_MAX_NUM (51 + 2 * 6) // The maximum supported qp +#define QP_MAX_NUM (51 + 6*6) // The maximum supported qp /* NAL unit types */ enum { @@ -128,6 +132,7 @@ enum { typedef enum { SEI_TYPE_BUFFERING_PERIOD = 0, ///< buffering period (H.264, D.1.1) SEI_TYPE_PIC_TIMING = 1, ///< picture timing + SEI_TYPE_USER_DATA_ITU_T_T35 = 4, ///< user data registered by ITU-T Recommendation T.35 SEI_TYPE_USER_DATA_UNREGISTERED = 5, ///< unregistered user data SEI_TYPE_RECOVERY_POINT = 6, ///< recovery point (frame # to decoder sync) SEI_TYPE_FRAME_PACKING = 45, ///< frame packing arrangement @@ -150,6 +155,19 @@ typedef enum { } SEI_PicStructType; /** + * frame_packing_arrangement types + */ +typedef enum { + SEI_FPA_TYPE_CHECKERBOARD = 0, + SEI_FPA_TYPE_INTERLEAVE_COLUMN = 1, + SEI_FPA_TYPE_INTERLEAVE_ROW = 2, + SEI_FPA_TYPE_SIDE_BY_SIDE = 3, + SEI_FPA_TYPE_TOP_BOTTOM = 4, + SEI_FPA_TYPE_INTERLEAVE_TEMPORAL = 5, + SEI_FPA_TYPE_2D = 6, +} SEI_FpaType; + +/** * Sequence parameter set */ typedef struct SPS { @@ -233,11 +251,23 @@ typedef struct PPS { int transform_8x8_mode; ///< transform_8x8_mode_flag uint8_t scaling_matrix4[6][16]; uint8_t scaling_matrix8[6][64]; - uint8_t chroma_qp_table[2][64]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table + uint8_t chroma_qp_table[2][QP_MAX_NUM+1]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table int chroma_qp_diff; } PPS; /** + * Frame Packing Arrangement Type + */ +typedef struct FPA { + int frame_packing_arrangement_id; + int frame_packing_arrangement_cancel_flag; ///< is previous arrangement canceled, -1 if never received + SEI_FpaType frame_packing_arrangement_type; + int frame_packing_arrangement_repetition_period; + int content_interpretation_type; + int quincunx_sampling_flag; +} FPA; + +/** * Memory management control operation opcode. */ typedef enum MMCOOpcode { @@ -261,6 +291,7 @@ typedef struct MMCO { typedef struct H264Picture { struct AVFrame f; + uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame ThreadFrame tf; AVBufferRef *qscale_table_buf; @@ -286,7 +317,7 @@ typedef struct H264Picture { int pic_id; /**< pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) */ int long_ref; ///< 1->long term reference 0->short term reference - int ref_poc[2][2][32]; ///< POCs of the frames used as reference (FIXME need per slice) + int ref_poc[2][2][32]; ///< POCs of the frames/fields used as reference (FIXME need per slice) int ref_count[2][2]; ///< number of entries in ref_poc (FIXME need per slice) int mbaff; ///< 1 -> MBAFF frame 0-> not MBAFF int field_picture; ///< whether or not picture was encoded in separate fields @@ -294,6 +325,12 @@ typedef struct H264Picture { int needs_realloc; ///< picture needs to be reallocated (eg due to a frame size change) int reference; int recovered; ///< picture at IDR or recovery point + recovery count + int invalid_gap; + int sei_recovery_frame_cnt; + + int crop; + int crop_left; + int crop_top; } H264Picture; typedef struct H264SliceContext { @@ -323,6 +360,7 @@ typedef struct H264SliceContext { * H264Context */ typedef struct H264Context { + AVClass *av_class; AVCodecContext *avctx; VideoDSPContext vdsp; H264DSPContext h264dsp; @@ -334,6 +372,7 @@ typedef struct H264Context { H264Picture *DPB; H264Picture *cur_pic_ptr; H264Picture cur_pic; + H264Picture last_pic_for_ec; H264SliceContext *slice_ctx; int nb_slice_ctx; @@ -413,9 +452,12 @@ typedef struct H264Context { ptrdiff_t mb_linesize; ///< may be equal to s->linesize or s->linesize * 2, for mbaff ptrdiff_t mb_uvlinesize; + unsigned current_sps_id; ///< id of the current SPS SPS sps; ///< current sps PPS pps; ///< current pps + int au_pps_id; ///< pps_id of current access unit + uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16]; // FIXME should these be moved down? uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64]; uint32_t(*dequant4_coeff[6])[16]; @@ -463,7 +505,7 @@ typedef struct H264Context { GetBitContext *inter_gb_ptr; const uint8_t *intra_pcm_ptr; - DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2]; ///< as a dct coeffecient is int32_t in high depth, we need to reserve twice the space. + DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2]; ///< as a dct coefficient is int32_t in high depth, we need to reserve twice the space. DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2]; int16_t mb_padding[256 * 2]; ///< 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 @@ -492,12 +534,12 @@ typedef struct H264Context { uint8_t field_scan[16]; uint8_t field_scan8x8[64]; uint8_t field_scan8x8_cavlc[64]; - const uint8_t *zigzag_scan_q0; - const uint8_t *zigzag_scan8x8_q0; - const uint8_t *zigzag_scan8x8_cavlc_q0; - const uint8_t *field_scan_q0; - const uint8_t *field_scan8x8_q0; - const uint8_t *field_scan8x8_cavlc_q0; + uint8_t zigzag_scan_q0[16]; + uint8_t zigzag_scan8x8_q0[64]; + uint8_t zigzag_scan8x8_cavlc_q0[64]; + uint8_t field_scan_q0[16]; + uint8_t field_scan8x8_q0[64]; + uint8_t field_scan8x8_cavlc_q0[64]; int x264_build; @@ -593,7 +635,7 @@ typedef struct H264Context { struct H264Context *thread_context[H264_MAX_THREADS]; /** - * current slice number, used to initalize slice_num of each thread/context + * current slice number, used to initialize slice_num of each thread/context */ int current_slice; @@ -616,6 +658,7 @@ typedef struct H264Context { enum AVPictureType pict_type; int last_slice_type; + unsigned int last_ref_count[2]; /** @} */ /** @@ -673,6 +716,13 @@ typedef struct H264Context { int sei_recovery_frame_cnt; /** + * Are the SEI recovery points looking valid. + */ + int valid_recovery_point; + + FPA sei_fpa; + + /** * recovery_frame is the frame_num at which the next frame should * be fully constructed. * @@ -693,6 +743,10 @@ typedef struct H264Context { int frame_recovered; ///< Initial frame has been completely recovered + int has_recovery_point; + + int missing_fields; + // Timestamp stuff int sei_buffering_period_present; ///< Buffering period SEI flag @@ -700,6 +754,12 @@ typedef struct H264Context { int cur_chroma_format_idc; uint8_t *bipred_scratchpad; + + int16_t slice_row[MAX_SLICES]; ///< to detect when MAX_SLICES is too low + + uint8_t parse_history[6]; + int parse_history_count; + int parse_last_mb; uint8_t *edge_emu_buffer; int16_t *dc_val_base; @@ -713,7 +773,7 @@ typedef struct H264Context { qpel_mc_func (*qpel_avg)[16]; } H264Context; -extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM + 1]; ///< One chroma qp table for each supported bit depth (8, 9, 10). +extern const uint8_t ff_h264_chroma_qp[7][QP_MAX_NUM + 1]; ///< One chroma qp table for each possible bit depth (8-14). extern const uint16_t ff_h264_mb_sizes[4]; /** @@ -796,7 +856,7 @@ int ff_h264_check_intra4x4_pred_mode(H264Context *h); int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma); void ff_h264_hl_decode_mb(H264Context *h, H264SliceContext *sl); -int ff_h264_decode_extradata(H264Context *h); +int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size); int ff_h264_decode_init(AVCodecContext *avctx); void ff_h264_decode_init_vlc(void); @@ -814,7 +874,7 @@ int ff_h264_decode_mb_cabac(H264Context *h, H264SliceContext *sl); void ff_h264_init_cabac_states(H264Context *h, H264SliceContext *sl); -void h264_init_dequant_tables(H264Context *h); +void ff_h264_init_dequant_tables(H264Context *h); void ff_h264_direct_dist_scale_factor(H264Context *const h); void ff_h264_direct_ref_list_init(H264Context *const h); @@ -834,6 +894,19 @@ void ff_h264_filter_mb(H264Context *h, H264SliceContext *sl, int mb_x, int mb_y, */ void ff_h264_reset_sei(H264Context *h); +/** + * Get stereo_mode string from the h264 frame_packing_arrangement + * @param h H.264 context. + */ +const char* ff_h264_sei_stereo_mode(H264Context *h); + +#define COPY_PICTURE(dst, src) \ +do {\ + *(dst) = *(src);\ + (dst)->f.extended_data = (dst)->f.data;\ + (dst)->tf.f = &(dst)->f;\ +} while (0) + /* * o-o o-o * / / / @@ -884,7 +957,7 @@ static const uint8_t scan8[16 * 3 + 3] = { 0 + 0 * 8, 0 + 5 * 8, 0 + 10 * 8 }; -static av_always_inline uint32_t pack16to32(int a, int b) +static av_always_inline uint32_t pack16to32(unsigned a, unsigned b) { #if HAVE_BIGENDIAN return (b & 0xFFFF) + (a << 16); @@ -893,7 +966,7 @@ static av_always_inline uint32_t pack16to32(int a, int b) #endif } -static av_always_inline uint16_t pack8to16(int a, int b) +static av_always_inline uint16_t pack8to16(unsigned a, unsigned b) { #if HAVE_BIGENDIAN return (b & 0xFF) + (a << 8); @@ -1034,6 +1107,34 @@ static av_always_inline int get_dct8x8_allowed(H264Context *h) 0x0001000100010001ULL)); } +static inline int find_start_code(const uint8_t *buf, int buf_size, + int buf_index, int next_avc) +{ + uint32_t state = -1; + + buf_index = avpriv_find_start_code(buf + buf_index, buf + next_avc + 1, &state) - buf - 1; + + return FFMIN(buf_index, buf_size); +} + +static inline int get_avc_nalsize(H264Context *h, const uint8_t *buf, + int buf_size, int *buf_index) +{ + int i, nalsize = 0; + + if (*buf_index >= buf_size - h->nal_length_size) + return -1; + + for (i = 0; i < h->nal_length_size; i++) + nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++]; + if (nalsize <= 0 || nalsize > buf_size - *buf_index) { + av_log(h->avctx, AV_LOG_ERROR, + "AVC: nal size %d\n", nalsize); + return -1; + } + return nalsize; +} + int ff_h264_field_end(H264Context *h, int in_setup); int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src); @@ -1048,6 +1149,9 @@ int ff_pred_weight_table(H264Context *h, H264SliceContext *sl); int ff_set_ref_count(H264Context *h); int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl, H264Context *h0); +#define SLICE_SINGLETHREAD 1 +#define SLICE_SKIPED 2 + int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count); int ff_h264_update_thread_context(AVCodecContext *dst, const AVCodecContext *src); @@ -1056,4 +1160,6 @@ void ff_h264_flush_change(H264Context *h); void ff_h264_free_tables(H264Context *h, int free_rbsp); +void ff_h264_set_erpic(ERPicture *dst, H264Picture *src); + #endif /* AVCODEC_H264_H */ |