00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef AVCODEC_FFV1_H
00024 #define AVCODEC_FFV1_H
00025
00031 #include "libavutil/avassert.h"
00032 #include "libavutil/crc.h"
00033 #include "libavutil/opt.h"
00034 #include "libavutil/imgutils.h"
00035 #include "libavutil/pixdesc.h"
00036 #include "libavutil/timer.h"
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "get_bits.h"
00040 #include "internal.h"
00041 #include "mathops.h"
00042 #include "put_bits.h"
00043 #include "rangecoder.h"
00044
00045 #ifdef __INTEL_COMPILER
00046 #undef av_flatten
00047 #define av_flatten
00048 #endif
00049
00050 #define MAX_PLANES 4
00051 #define CONTEXT_SIZE 32
00052
00053 #define MAX_QUANT_TABLES 8
00054 #define MAX_CONTEXT_INPUTS 5
00055
00056 extern const uint8_t ff_log2_run[41];
00057
00058 typedef struct VlcState {
00059 int16_t drift;
00060 uint16_t error_sum;
00061 int8_t bias;
00062 uint8_t count;
00063 } VlcState;
00064
00065 typedef struct PlaneContext {
00066 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00067 int quant_table_index;
00068 int context_count;
00069 uint8_t (*state)[CONTEXT_SIZE];
00070 VlcState *vlc_state;
00071 uint8_t interlace_bit_state[2];
00072 } PlaneContext;
00073
00074 #define MAX_SLICES 256
00075
00076 typedef struct FFV1Context {
00077 AVClass *class;
00078 AVCodecContext *avctx;
00079 RangeCoder c;
00080 GetBitContext gb;
00081 PutBitContext pb;
00082 uint64_t rc_stat[256][2];
00083 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
00084 int version;
00085 int minor_version;
00086 int width, height;
00087 int chroma_planes;
00088 int chroma_h_shift, chroma_v_shift;
00089 int transparency;
00090 int flags;
00091 int picture_number;
00092 AVFrame picture;
00093 AVFrame last_picture;
00094 int plane_count;
00095 int ac;
00096 int ac_byte_count;
00097 PlaneContext plane[MAX_PLANES];
00098 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
00099 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
00100 int context_count[MAX_QUANT_TABLES];
00101 uint8_t state_transition[256];
00102 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
00103 int run_index;
00104 int colorspace;
00105 int16_t *sample_buffer;
00106
00107 int ec;
00108 int slice_damaged;
00109 int key_frame_ok;
00110
00111 int bits_per_raw_sample;
00112 int packed_at_lsb;
00113
00114 int gob_count;
00115 int quant_table_count;
00116
00117 DSPContext dsp;
00118
00119 struct FFV1Context *slice_context[MAX_SLICES];
00120 int slice_count;
00121 int num_v_slices;
00122 int num_h_slices;
00123 int slice_width;
00124 int slice_height;
00125 int slice_x;
00126 int slice_y;
00127 } FFV1Context;
00128
00129 int ffv1_common_init(AVCodecContext *avctx);
00130 int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs);
00131 int ffv1_init_slices_state(FFV1Context *f);
00132 int ffv1_init_slice_contexts(FFV1Context *f);
00133 int ffv1_allocate_initial_states(FFV1Context *f);
00134 void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs);
00135 int ffv1_close(AVCodecContext *avctx);
00136
00137 static av_always_inline int fold(int diff, int bits)
00138 {
00139 if (bits == 8)
00140 diff = (int8_t)diff;
00141 else {
00142 diff += 1 << (bits - 1);
00143 diff &= (1 << bits) - 1;
00144 diff -= 1 << (bits - 1);
00145 }
00146
00147 return diff;
00148 }
00149
00150 static inline int predict(int16_t *src, int16_t *last)
00151 {
00152 const int LT = last[-1];
00153 const int T = last[0];
00154 const int L = src[-1];
00155
00156 return mid_pred(L, L + T - LT, T);
00157 }
00158
00159 static inline int get_context(PlaneContext *p, int16_t *src,
00160 int16_t *last, int16_t *last2)
00161 {
00162 const int LT = last[-1];
00163 const int T = last[0];
00164 const int RT = last[1];
00165 const int L = src[-1];
00166
00167 if (p->quant_table[3][127]) {
00168 const int TT = last2[0];
00169 const int LL = src[-2];
00170 return p->quant_table[0][(L - LT) & 0xFF] +
00171 p->quant_table[1][(LT - T) & 0xFF] +
00172 p->quant_table[2][(T - RT) & 0xFF] +
00173 p->quant_table[3][(LL - L) & 0xFF] +
00174 p->quant_table[4][(TT - T) & 0xFF];
00175 } else
00176 return p->quant_table[0][(L - LT) & 0xFF] +
00177 p->quant_table[1][(LT - T) & 0xFF] +
00178 p->quant_table[2][(T - RT) & 0xFF];
00179 }
00180
00181 static inline void update_vlc_state(VlcState *const state, const int v)
00182 {
00183 int drift = state->drift;
00184 int count = state->count;
00185 state->error_sum += FFABS(v);
00186 drift += v;
00187
00188 if (count == 128) {
00189 count >>= 1;
00190 drift >>= 1;
00191 state->error_sum >>= 1;
00192 }
00193 count++;
00194
00195 if (drift <= -count) {
00196 if (state->bias > -128)
00197 state->bias--;
00198
00199 drift += count;
00200 if (drift <= -count)
00201 drift = -count + 1;
00202 } else if (drift > 0) {
00203 if (state->bias < 127)
00204 state->bias++;
00205
00206 drift -= count;
00207 if (drift > 0)
00208 drift = 0;
00209 }
00210
00211 state->drift = drift;
00212 state->count = count;
00213 }
00214
00215 #endif