]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h264.h
h264_refs: remove an unused parameter from ff_h264_fill_mbaff_ref_list()
[ffmpeg] / libavcodec / h264.h
index e3c0ac04f26df53f2a6aefb11d1855eb6614a81b..f551d79209a56456e9247048336bcf062019662e 100644 (file)
 #ifndef AVCODEC_H264_H
 #define AVCODEC_H264_H
 
+#include "libavutil/buffer.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/thread.h"
 #include "cabac.h"
-#include "dsputil.h"
 #include "error_resilience.h"
 #include "get_bits.h"
-#include "mpegvideo.h"
+#include "h264_parse.h"
+#include "h264_sei.h"
+#include "h2645_parse.h"
 #include "h264chroma.h"
 #include "h264dsp.h"
 #include "h264pred.h"
 #include "h264qpel.h"
+#include "internal.h"
 #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
@@ -61,7 +68,7 @@
  * The maximum number of slices supported by the decoder.
  * must be a power of 2
  */
-#define MAX_SLICES 16
+#define MAX_SLICES 32
 
 #ifdef ALLOW_INTERLACE
 #define MB_MBAFF(h)    h->mb_mbaff
 #define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))
 
 #ifndef CABAC
-#define CABAC(h) h->pps.cabac
+#define CABAC(h) h->ps.pps->cabac
 #endif
 
-#define CHROMA422(h) (h->sps.chroma_format_idc == 2)
-#define CHROMA444(h) (h->sps.chroma_format_idc == 3)
+#define CHROMA422(h) (h->ps.sps->chroma_format_idc == 2)
+#define CHROMA444(h) (h->ps.sps->chroma_format_idc == 3)
 
 #define EXTENDED_SAR       255
 
@@ -121,32 +128,6 @@ enum {
     NAL_FF_IGNORE       = 0xff0f001,
 };
 
-/**
- * SEI message types
- */
-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_UNREGISTERED = 5,   ///< unregistered user data
-    SEI_TYPE_RECOVERY_POINT         = 6,   ///< recovery point (frame # to decoder sync)
-    SEI_TYPE_FRAME_PACKING          = 45,  ///< frame packing arrangement
-} 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
  */
@@ -207,7 +188,6 @@ typedef struct SPS {
     int bit_depth_chroma;                 ///< bit_depth_chroma_minus8 + 8
     int residual_color_transform_flag;    ///< residual_colour_transform_flag
     int constraint_set_flags;             ///< constraint_set[0-3]_flag
-    int new;                              ///< flag to keep track if the decoder context needs re-init due to changed SPS
 } SPS;
 
 /**
@@ -233,8 +213,23 @@ typedef struct PPS {
     uint8_t scaling_matrix8[6][64];
     uint8_t chroma_qp_table[2][64]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table
     int chroma_qp_diff;
+
+    uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16];
+    uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
+    uint32_t(*dequant4_coeff[6])[16];
+    uint32_t(*dequant8_coeff[6])[64];
 } PPS;
 
+typedef struct H264ParamSets {
+    AVBufferRef *sps_list[MAX_SPS_COUNT];
+    AVBufferRef *pps_list[MAX_PPS_COUNT];
+
+    /* currently active parameters sets */
+    const PPS *pps;
+    // FIXME this should properly be const
+    SPS *sps;
+} H264ParamSets;
+
 /**
  * Memory management control operation opcode.
  */
@@ -258,7 +253,7 @@ typedef struct MMCO {
 } MMCO;
 
 typedef struct H264Picture {
-    struct AVFrame f;
+    AVFrame *f;
     ThreadFrame tf;
 
     AVBufferRef *qscale_table_buf;
@@ -289,56 +284,52 @@ typedef struct H264Picture {
     int mbaff;              ///< 1 -> MBAFF frame 0-> not MBAFF
     int field_picture;      ///< whether or not picture was encoded in separate fields
 
-    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
 } H264Picture;
 
-/**
- * H264Context
- */
-typedef struct H264Context {
-    AVCodecContext *avctx;
-    DSPContext       dsp;
-    VideoDSPContext vdsp;
-    H264DSPContext h264dsp;
-    H264ChromaContext h264chroma;
-    H264QpelContext h264qpel;
-    ParseContext parse_context;
+typedef struct H264Ref {
+    uint8_t *data[3];
+    int linesize[3];
+
+    int reference;
+    int poc;
+    int pic_id;
+
+    H264Picture *parent;
+} H264Ref;
+
+typedef struct H264SliceContext {
+    struct H264Context *h264;
     GetBitContext gb;
     ERContext er;
 
-    H264Picture *DPB;
-    H264Picture *cur_pic_ptr;
-    H264Picture cur_pic;
+    int slice_num;
+    int slice_type;
+    int slice_type_nos;         ///< S free slice type (SI/SP are remapped to I/P)
+    int slice_type_fixed;
 
-    int pixel_shift;    ///< 0 for 8-bit H264, 1 for high-bit-depth H264
+    int qscale;
     int chroma_qp[2];   // QPc
-
     int qp_thresh;      ///< QP threshold to skip loopfilter
+    int last_qscale_diff;
 
-    /* coded dimensions -- 16 * mb w/h */
-    int width, height;
-    ptrdiff_t linesize, uvlinesize;
-    int chroma_x_shift, chroma_y_shift;
-
-    int qscale;
-    int droppable;
-    int data_partitioning;
-    int coded_picture_number;
-    int low_delay;
+    // deblock
+    int deblocking_filter;          ///< disable_deblocking_filter_idc with 1 <-> 0
+    int slice_alpha_c0_offset;
+    int slice_beta_offset;
 
-    int context_initialized;
-    int flags;
-    int workaround_bugs;
+    H264PredWeightTable pwt;
 
     int prev_mb_skipped;
     int next_mb_skipped;
 
-    // prediction stuff
     int chroma_pred_mode;
     int intra16x16_pred_mode;
 
+    int8_t intra4x4_pred_mode_cache[5 * 8];
+    int8_t(*intra4x4_pred_mode);
+
     int topleft_mb_xy;
     int top_mb_xy;
     int topright_mb_xy;
@@ -352,85 +343,42 @@ typedef struct H264Context {
     const uint8_t *left_block;
     int topleft_partition;
 
-    int8_t intra4x4_pred_mode_cache[5 * 8];
-    int8_t(*intra4x4_pred_mode);
-    H264PredContext hpc;
     unsigned int topleft_samples_available;
     unsigned int top_samples_available;
     unsigned int topright_samples_available;
     unsigned int left_samples_available;
-    uint8_t (*top_borders[2])[(16 * 3) * 2];
-
-    /**
-     * non zero coeff count cache.
-     * is 64 if not available.
-     */
-    DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
-
-    uint8_t (*non_zero_count)[48];
-
-    /**
-     * Motion vector cache.
-     */
-    DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
-    DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
-#define LIST_NOT_USED -1 // FIXME rename?
-#define PART_NOT_AVAILABLE -2
-
-    /**
-     * number of neighbors (top and/or left) that used 8x8 dct
-     */
-    int neighbor_transform_size;
-
-    /**
-     * block_offset[ 0..23] for frame macroblocks
-     * block_offset[24..47] for field macroblocks
-     */
-    int block_offset[2 * (16 * 3)];
-
-    uint32_t *mb2b_xy;  // FIXME are these 4 a good idea?
-    uint32_t *mb2br_xy;
-    int b_stride;       // FIXME use s->b4_stride
 
+    ptrdiff_t linesize, uvlinesize;
     ptrdiff_t mb_linesize;  ///< may be equal to s->linesize or s->linesize * 2, for mbaff
     ptrdiff_t mb_uvlinesize;
 
-    SPS sps; ///< current sps
-    PPS pps; ///< current pps
-
-    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];
-    uint32_t(*dequant8_coeff[6])[64];
-
-    int slice_num;
-    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;
+    int mb_x, mb_y;
+    int mb_xy;
+    int resync_mb_x;
+    int resync_mb_y;
+    // index of the first MB of the next slice
+    int next_slice_idx;
+    int mb_skip_run;
+    int is_complex;
 
-    // interlacing specific flags
-    int mb_aff_frame;
     int mb_field_decoding_flag;
     int mb_mbaff;               ///< mb_aff_frame && mb_field_decoding_flag
-    int picture_structure;
-    int first_field;
 
-    DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
+    int redundant_pic_count;
 
-    // Weighted pred stuff
-    int use_weight;
-    int use_weight_chroma;
-    int luma_log2_weight_denom;
-    int chroma_log2_weight_denom;
-    // The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
-    int luma_weight[48][2][2];
-    int chroma_weight[48][2][2][2];
-    int implicit_weight[48][48][2];
+    /**
+     * number of neighbors (top and/or left) that used 8x8 dct
+     */
+    int neighbor_transform_size;
 
     int direct_spatial_mv_pred;
     int col_parity;
     int col_fieldoff;
+
+    int cbp;
+    int top_cbp;
+    int left_cbp;
+
     int dist_scale_factor[32];
     int dist_scale_factor_field[2][32];
     int map_col_to_list0[2][16 + 32];
@@ -441,41 +389,127 @@ typedef struct H264Context {
      */
     unsigned int ref_count[2];          ///< counts frames or fields, depending on current mb mode
     unsigned int list_count;
-    uint8_t *list_counts;               ///< Array of list_count per MB specifying the slice type
-    H264Picture ref_list[2][48];        /**< 0..15: frame refs, 16..47: mbaff field refs.
+    H264Ref 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
 
-    // data partitioning
-    GetBitContext intra_gb;
-    GetBitContext inter_gb;
-    GetBitContext *intra_gb_ptr;
-    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.
+    int16_t *dc_val_base;
+
+    uint8_t *bipred_scratchpad;
+    uint8_t *edge_emu_buffer;
+    uint8_t (*top_borders[2])[(16 * 3) * 2];
+    int bipred_scratchpad_allocated;
+    int edge_emu_buffer_allocated;
+    int top_borders_allocated[2];
+
+    /**
+     * non zero coeff count cache.
+     * is 64 if not available.
+     */
+    DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
+
+    /**
+     * Motion vector cache.
+     */
+    DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
+    DECLARE_ALIGNED(8,  int8_t, ref_cache)[2][5 * 8];
+    DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
+    uint8_t direct_cache[5 * 8];
+
+    DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
+
+    ///< 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];
     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
+    ///< 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
+    int16_t mb_padding[256 * 2];
+
+    uint8_t (*mvd_table[2])[2];
 
     /**
      * Cabac
      */
     CABACContext cabac;
     uint8_t cabac_state[1024];
+    int cabac_init_idc;
+
+    // rbsp buffer used for this slice
+    uint8_t *rbsp_buffer;
+    unsigned int rbsp_buffer_size;
+} H264SliceContext;
+
+/**
+ * H264Context
+ */
+typedef struct H264Context {
+    const AVClass *class;
+    AVCodecContext *avctx;
+    VideoDSPContext vdsp;
+    H264DSPContext h264dsp;
+    H264ChromaContext h264chroma;
+    H264QpelContext h264qpel;
+    GetBitContext gb;
+
+    H264Picture DPB[H264_MAX_PICTURE_COUNT];
+    H264Picture *cur_pic_ptr;
+    H264Picture cur_pic;
+
+    H264SliceContext *slice_ctx;
+    int            nb_slice_ctx;
+
+    H2645Packet pkt;
+
+    int pixel_shift;    ///< 0 for 8-bit H264, 1 for high-bit-depth H264
+
+    /* coded dimensions -- 16 * mb w/h */
+    int width, height;
+    int chroma_x_shift, chroma_y_shift;
+
+    int droppable;
+    int coded_picture_number;
+    int low_delay;
+
+    int context_initialized;
+    int flags;
+    int workaround_bugs;
+
+    int8_t(*intra4x4_pred_mode);
+    H264PredContext hpc;
+
+    uint8_t (*non_zero_count)[48];
+
+#define LIST_NOT_USED -1 // FIXME rename?
+#define PART_NOT_AVAILABLE -2
+
+    /**
+     * block_offset[ 0..23] for frame macroblocks
+     * block_offset[24..47] for field macroblocks
+     */
+    int block_offset[2 * (16 * 3)];
+
+    uint32_t *mb2b_xy;  // FIXME are these 4 a good idea?
+    uint32_t *mb2br_xy;
+    int b_stride;       // FIXME use s->b4_stride
+
+    uint16_t *slice_table;      ///< slice_table_base + 2*mb_stride + 1
+
+    // interlacing specific flags
+    int mb_aff_frame;
+    int picture_structure;
+    int first_field;
+
+    uint8_t *list_counts;               ///< Array of list_count per MB specifying the slice type
 
     /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
     uint16_t *cbp_table;
-    int cbp;
-    int top_cbp;
-    int left_cbp;
+
     /* chroma_pred_mode for i4x4 or i16x16, else 0 */
     uint8_t *chroma_pred_mode_table;
-    int last_qscale_diff;
     uint8_t (*mvd_table[2])[2];
-    DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
     uint8_t *direct_table;
-    uint8_t direct_cache[5 * 8];
 
     uint8_t zigzag_scan[16];
     uint8_t zigzag_scan8x8[64];
@@ -490,60 +524,31 @@ typedef struct H264Context {
     const uint8_t *field_scan8x8_q0;
     const uint8_t *field_scan8x8_cavlc_q0;
 
-    int x264_build;
-
-    int mb_x, mb_y;
-    int resync_mb_x;
-    int resync_mb_y;
-    int mb_skip_run;
+    int mb_y;
     int mb_height, mb_width;
     int mb_stride;
     int mb_num;
-    int mb_xy;
-
-    int is_complex;
-
-    // deblock
-    int deblocking_filter;          ///< disable_deblocking_filter_idc with 1 <-> 0
-    int slice_alpha_c0_offset;
-    int slice_beta_offset;
 
     // =============================================================
     // Things below are not used in the MB or more inner code
 
     int nal_ref_idc;
     int nal_unit_type;
-    uint8_t *rbsp_buffer[2];
-    unsigned int rbsp_buffer_size[2];
 
     /**
      * Used to parse AVC variant of h264
      */
     int is_avc;           ///< this flag is != 0 if codec is avc1
     int nal_length_size;  ///< Number of bytes used for nal length (1, 2 or 4)
-    int got_first;        ///< this flag is != 0 if we've parsed a frame
 
     int bit_depth_luma;         ///< luma bit depth from sps to detect changes
     int chroma_format_idc;      ///< chroma format from sps to detect changes
 
-    SPS *sps_buffers[MAX_SPS_COUNT];
-    PPS *pps_buffers[MAX_PPS_COUNT];
-
-    int dequant_coeff_pps;      ///< reinit tables when pps changes
+    H264ParamSets ps;
 
     uint16_t *slice_table_base;
 
-    // POC stuff
-    int poc_lsb;
-    int poc_msb;
-    int delta_poc_bottom;
-    int delta_poc[2];
-    int frame_num;
-    int prev_poc_msb;           ///< poc_msb of the last reference pic for POC type 0
-    int prev_poc_lsb;           ///< poc_lsb of the last reference pic for POC type 0
-    int frame_num_offset;       ///< for POC type 2
-    int prev_frame_num_offset;  ///< for POC type 2
-    int prev_frame_num;         ///< frame_num of the last pic for POC type 1/2
+    H264POCContext poc;
 
     /**
      * frame_num for frames or 2 * frame_num + 1 for field pics.
@@ -555,15 +560,11 @@ typedef struct H264Context {
      */
     int max_pic_num;
 
-    int redundant_pic_count;
-
-    H264Picture default_ref_list[2][32]; ///< base reference list for all slices of a coded picture
     H264Picture *short_ref[32];
     H264Picture *long_ref[32];
     H264Picture *delayed_pic[MAX_DELAYED_PIC_COUNT + 2]; // FIXME size?
     int last_pocs[MAX_DELAYED_PIC_COUNT];
     H264Picture *next_output_pic;
-    int outputed_poc;
     int next_outputed_poc;
 
     /**
@@ -576,14 +577,10 @@ typedef struct H264Context {
     int long_ref_count;     ///< number of actual long term references
     int short_ref_count;    ///< number of actual short term references
 
-    int cabac_init_idc;
-
     /**
      * @name Members for slice based multithreading
      * @{
      */
-    struct H264Context *thread_context[H264_MAX_THREADS];
-
     /**
      * current slice number, used to initalize slice_num of each thread/context
      */
@@ -607,14 +604,8 @@ typedef struct H264Context {
 
     enum AVPictureType pict_type;
 
-    int last_slice_type;
     /** @} */
 
-    /**
-     * pic_struct in picture timing SEI message
-     */
-    SEI_PicStructType sei_pic_struct;
-
     /**
      * Complement sei_pic_struct
      * SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced frames.
@@ -623,40 +614,6 @@ typedef struct H264Context {
      */
     int prev_interlaced_frame;
 
-    /**
-     * frame_packing_arrangment SEI message
-     */
-    int sei_frame_packing_present;
-    int frame_packing_arrangement_type;
-    int content_interpretation_type;
-    int quincunx_subsampling;
-
-    /**
-     * 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;
-
     /**
      * recovery_frame is the frame_num at which the next frame should
      * be fully constructed.
@@ -678,17 +635,15 @@ typedef struct H264Context {
 
     int frame_recovered;    ///< Initial frame has been completely recovered
 
-    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
+    /* for frame threading, this is set to 1
+     * after finish_setup() has been called, so we cannot modify
+     * some context properties (which are supposed to stay constant between
+     * slices) anymore */
+    int setup_finished;
 
-    // Timestamp stuff
-    int sei_buffering_period_present;   ///< Buffering period SEI flag
-    int initial_cpb_removal_delay[32];  ///< Initial timestamps for CPBs
+    int enable_er;
 
-    int cur_chroma_format_idc;
-    uint8_t *bipred_scratchpad;
-    uint8_t *edge_emu_buffer;
-    int16_t *dc_val_base;
+    H264SEIContext sei;
 
     AVBufferPool *qscale_table_pool;
     AVBufferPool *mb_type_pool;
@@ -700,39 +655,19 @@ 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 uint16_t ff_h264_mb_sizes[4];
 
-/**
- * Decode SEI
- */
-int ff_h264_decode_sei(H264Context *h);
-
 /**
  * Decode SPS
  */
-int ff_h264_decode_seq_parameter_set(H264Context *h);
-
-/**
- * compute profile from sps
- */
-int ff_h264_get_profile(SPS *sps);
+int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
+                                     H264ParamSets *ps);
 
 /**
  * Decode PPS
  */
-int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
-
-/**
- * Decode 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?
- * @return 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);
+int ff_h264_decode_picture_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
+                                         H264ParamSets *ps, int bit_length);
 
 /**
  * Free any data that may have been allocated in the H264 context
@@ -743,7 +678,7 @@ void ff_h264_free_context(H264Context *h);
 /**
  * Reconstruct bitstream slice_type.
  */
-int ff_h264_get_slice_type(const H264Context *h);
+int ff_h264_get_slice_type(const H264SliceContext *sl);
 
 /**
  * Allocate tables.
@@ -751,13 +686,8 @@ int ff_h264_get_slice_type(const H264Context *h);
  */
 int ff_h264_alloc_tables(H264Context *h);
 
-/**
- * Fill the default_ref_list.
- */
-int ff_h264_fill_default_ref_list(H264Context *h);
-
-int ff_h264_decode_ref_pic_list_reordering(H264Context *h);
-void ff_h264_fill_mbaff_ref_list(H264Context *h);
+int ff_h264_decode_ref_pic_list_reordering(const H264Context *h, H264SliceContext *sl);
+void ff_h264_fill_mbaff_ref_list(H264SliceContext *sl);
 void ff_h264_remove_all_refs(H264Context *h);
 
 /**
@@ -770,20 +700,7 @@ int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
 
 int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice);
 
-/**
- * Check if the top & left blocks are available if needed & change the
- * dc mode so it only uses the available blocks.
- */
-int ff_h264_check_intra4x4_pred_mode(H264Context *h);
-
-/**
- * Check if the top & left blocks are available if needed & change the
- * dc mode so it only uses the available blocks.
- */
-int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
-
-void ff_h264_hl_decode_mb(H264Context *h);
-int ff_h264_decode_extradata(H264Context *h);
+void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl);
 int ff_h264_decode_init(AVCodecContext *avctx);
 void ff_h264_decode_init_vlc(void);
 
@@ -791,34 +708,30 @@ void ff_h264_decode_init_vlc(void);
  * Decode a macroblock
  * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
  */
-int ff_h264_decode_mb_cavlc(H264Context *h);
+int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl);
 
 /**
  * Decode a CABAC coded macroblock
  * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
  */
-int ff_h264_decode_mb_cabac(H264Context *h);
+int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl);
+
+void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl);
 
-void ff_h264_init_cabac_states(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);
-void ff_h264_pred_direct_motion(H264Context *const h, int *mb_type);
+void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl);
+void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl);
+void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
+                                int *mb_type);
 
-void ff_h264_filter_mb_fast(H264Context *h, int mb_x, int mb_y,
+void ff_h264_filter_mb_fast(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y,
                             uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
                             unsigned int linesize, unsigned int uvlinesize);
-void ff_h264_filter_mb(H264Context *h, int mb_x, int mb_y,
+void ff_h264_filter_mb(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y,
                        uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
                        unsigned int linesize, unsigned int uvlinesize);
 
-/**
- * Reset SEI values at the beginning of the frame.
- *
- * @param h H.264 context.
- */
-void ff_h264_reset_sei(H264Context *h);
-
 /*
  * o-o o-o
  *  / / /
@@ -890,22 +803,23 @@ static av_always_inline uint16_t pack8to16(int a, int b)
 /**
  * Get the chroma qp.
  */
-static av_always_inline int get_chroma_qp(H264Context *h, int t, int qscale)
+static av_always_inline int get_chroma_qp(const H264Context *h, int t, int qscale)
 {
-    return h->pps.chroma_qp_table[t][qscale];
+    return h->ps.pps->chroma_qp_table[t][qscale];
 }
 
 /**
  * Get the predicted intra4x4 prediction mode.
  */
-static av_always_inline int pred_intra_mode(H264Context *h, int n)
+static av_always_inline int pred_intra_mode(const H264Context *h,
+                                            H264SliceContext *sl, int n)
 {
     const int index8 = scan8[n];
-    const int left   = h->intra4x4_pred_mode_cache[index8 - 1];
-    const int top    = h->intra4x4_pred_mode_cache[index8 - 8];
+    const int left   = sl->intra4x4_pred_mode_cache[index8 - 1];
+    const int top    = sl->intra4x4_pred_mode_cache[index8 - 8];
     const int min    = FFMIN(left, top);
 
-    tprintf(h->avctx, "mode:%d %d min:%d\n", left, top, min);
+    ff_tlog(h->avctx, "mode:%d %d min:%d\n", left, top, min);
 
     if (min < 0)
         return DC_PRED;
@@ -913,10 +827,11 @@ static av_always_inline int pred_intra_mode(H264Context *h, int n)
         return min;
 }
 
-static av_always_inline void write_back_intra_pred_mode(H264Context *h)
+static av_always_inline void write_back_intra_pred_mode(const H264Context *h,
+                                                        H264SliceContext *sl)
 {
-    int8_t *i4x4       = h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
-    int8_t *i4x4_cache = h->intra4x4_pred_mode_cache;
+    int8_t *i4x4       = sl->intra4x4_pred_mode + h->mb2br_xy[sl->mb_xy];
+    int8_t *i4x4_cache = sl->intra4x4_pred_mode_cache;
 
     AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
     i4x4[4] = i4x4_cache[7 + 8 * 3];
@@ -924,11 +839,12 @@ static av_always_inline void write_back_intra_pred_mode(H264Context *h)
     i4x4[6] = i4x4_cache[7 + 8 * 1];
 }
 
-static av_always_inline void write_back_non_zero_count(H264Context *h)
+static av_always_inline void write_back_non_zero_count(const H264Context *h,
+                                                       H264SliceContext *sl)
 {
-    const int mb_xy    = h->mb_xy;
+    const int mb_xy    = sl->mb_xy;
     uint8_t *nnz       = h->non_zero_count[mb_xy];
-    uint8_t *nnz_cache = h->non_zero_count_cache;
+    uint8_t *nnz_cache = sl->non_zero_count_cache;
 
     AV_COPY32(&nnz[ 0], &nnz_cache[4 + 8 * 1]);
     AV_COPY32(&nnz[ 4], &nnz_cache[4 + 8 * 2]);
@@ -947,21 +863,22 @@ static av_always_inline void write_back_non_zero_count(H264Context *h)
     }
 }
 
-static av_always_inline void write_back_motion_list(H264Context *h,
+static av_always_inline void write_back_motion_list(const H264Context *h,
+                                                    H264SliceContext *sl,
                                                     int b_stride,
                                                     int b_xy, int b8_xy,
                                                     int mb_type, int list)
 {
     int16_t(*mv_dst)[2] = &h->cur_pic.motion_val[list][b_xy];
-    int16_t(*mv_src)[2] = &h->mv_cache[list][scan8[0]];
+    int16_t(*mv_src)[2] = &sl->mv_cache[list][scan8[0]];
     AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
     AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
     AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
     AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
     if (CABAC(h)) {
-        uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8 * h->mb_xy
-                                                        : h->mb2br_xy[h->mb_xy]];
-        uint8_t(*mvd_src)[2]  = &h->mvd_cache[list][scan8[0]];
+        uint8_t (*mvd_dst)[2] = &sl->mvd_table[list][FMO ? 8 * sl->mb_xy
+                                                        : h->mb2br_xy[sl->mb_xy]];
+        uint8_t(*mvd_src)[2]  = &sl->mvd_cache[list][scan8[0]];
         if (IS_SKIP(mb_type)) {
             AV_ZERO128(mvd_dst);
         } else {
@@ -974,7 +891,7 @@ static av_always_inline void write_back_motion_list(H264Context *h,
 
     {
         int8_t *ref_index = &h->cur_pic.ref_index[list][b8_xy];
-        int8_t *ref_cache = h->ref_cache[list];
+        int8_t *ref_cache = sl->ref_cache[list];
         ref_index[0 + 0 * 2] = ref_cache[scan8[0]];
         ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
         ref_index[0 + 1 * 2] = ref_cache[scan8[8]];
@@ -982,46 +899,61 @@ static av_always_inline void write_back_motion_list(H264Context *h,
     }
 }
 
-static av_always_inline void write_back_motion(H264Context *h, int mb_type)
+static av_always_inline void write_back_motion(const H264Context *h,
+                                               H264SliceContext *sl,
+                                               int mb_type)
 {
     const int b_stride      = h->b_stride;
-    const int b_xy  = 4 * h->mb_x + 4 * h->mb_y * h->b_stride; // try mb2b(8)_xy
-    const int b8_xy = 4 * h->mb_xy;
+    const int b_xy  = 4 * sl->mb_x + 4 * sl->mb_y * h->b_stride; // try mb2b(8)_xy
+    const int b8_xy = 4 * sl->mb_xy;
 
     if (USES_LIST(mb_type, 0)) {
-        write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 0);
+        write_back_motion_list(h, sl, b_stride, b_xy, b8_xy, mb_type, 0);
     } else {
         fill_rectangle(&h->cur_pic.ref_index[0][b8_xy],
                        2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
     }
     if (USES_LIST(mb_type, 1))
-        write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 1);
+        write_back_motion_list(h, sl, b_stride, b_xy, b8_xy, mb_type, 1);
 
-    if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC(h)) {
+    if (sl->slice_type_nos == AV_PICTURE_TYPE_B && CABAC(h)) {
         if (IS_8X8(mb_type)) {
-            uint8_t *direct_table = &h->direct_table[4 * h->mb_xy];
-            direct_table[1] = h->sub_mb_type[1] >> 1;
-            direct_table[2] = h->sub_mb_type[2] >> 1;
-            direct_table[3] = h->sub_mb_type[3] >> 1;
+            uint8_t *direct_table = &h->direct_table[4 * sl->mb_xy];
+            direct_table[1] = sl->sub_mb_type[1] >> 1;
+            direct_table[2] = sl->sub_mb_type[2] >> 1;
+            direct_table[3] = sl->sub_mb_type[3] >> 1;
         }
     }
 }
 
-static av_always_inline int get_dct8x8_allowed(H264Context *h)
+static av_always_inline int get_dct8x8_allowed(const H264Context *h, H264SliceContext *sl)
 {
-    if (h->sps.direct_8x8_inference_flag)
-        return !(AV_RN64A(h->sub_mb_type) &
+    if (h->ps.sps->direct_8x8_inference_flag)
+        return !(AV_RN64A(sl->sub_mb_type) &
                  ((MB_TYPE_16x8 | MB_TYPE_8x16 | MB_TYPE_8x8) *
                   0x0001000100010001ULL));
     else
-        return !(AV_RN64A(h->sub_mb_type) &
+        return !(AV_RN64A(sl->sub_mb_type) &
                  ((MB_TYPE_16x8 | MB_TYPE_8x16 | MB_TYPE_8x8 | MB_TYPE_DIRECT2) *
                   0x0001000100010001ULL));
 }
 
-void ff_h264_draw_horiz_band(H264Context *h, int y, int height);
-int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc);
-int ff_pred_weight_table(H264Context *h);
-int ff_set_ref_count(H264Context *h);
+int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup);
+
+int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src);
+void ff_h264_unref_picture(H264Context *h, H264Picture *pic);
+
+int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl);
+
+void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height);
+
+int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl);
+int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count);
+int ff_h264_update_thread_context(AVCodecContext *dst,
+                                  const AVCodecContext *src);
+
+void ff_h264_flush_change(H264Context *h);
+
+void ff_h264_free_tables(H264Context *h);
 
 #endif /* AVCODEC_H264_H */