2 * FFV1 codec for libavcodec
4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef AVCODEC_FFV1_H
24 #define AVCODEC_FFV1_H
28 * FF Video Codec 1 (a lossless codec)
31 #include "libavutil/avassert.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/pixdesc.h"
41 #include "rangecoder.h"
44 #ifdef __INTEL_COMPILER
50 #define CONTEXT_SIZE 32
52 #define MAX_QUANT_TABLES 8
53 #define MAX_CONTEXT_INPUTS 5
55 #define AC_GOLOMB_RICE 0
56 #define AC_RANGE_DEFAULT_TAB 1
57 #define AC_RANGE_CUSTOM_TAB 2
58 #define AC_RANGE_DEFAULT_TAB_FORCE -2
60 typedef struct VlcState {
67 typedef struct PlaneContext {
68 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
69 int quant_table_index;
71 uint8_t (*state)[CONTEXT_SIZE];
73 uint8_t interlace_bit_state[2];
76 #define MAX_SLICES 1024
78 typedef struct FFV1Context {
80 AVCodecContext *avctx;
84 uint64_t rc_stat[256][2];
85 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
90 int chroma_h_shift, chroma_v_shift;
95 ThreadFrame picture, last_picture;
96 struct FFV1Context *fsrc;
100 int ac; ///< 1=range coder <-> 0=golomb rice
101 int ac_byte_count; ///< number of bytes used for AC coding
102 PlaneContext plane[MAX_PLANES];
103 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
104 int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
105 int context_count[MAX_QUANT_TABLES];
106 uint8_t state_transition[256];
107 uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
110 int16_t *sample_buffer;
111 int32_t *sample_buffer32;
121 int bits_per_raw_sample;
125 int quant_table_count;
127 struct FFV1Context *slice_context[MAX_SLICES];
136 int slice_reset_contexts;
137 int slice_coding_mode;
138 int slice_rct_by_coef;
139 int slice_rct_ry_coef;
142 int ff_ffv1_common_init(AVCodecContext *avctx);
143 int ff_ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs);
144 int ff_ffv1_init_slices_state(FFV1Context *f);
145 int ff_ffv1_init_slice_contexts(FFV1Context *f);
146 int ff_ffv1_allocate_initial_states(FFV1Context *f);
147 void ff_ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs);
148 int ff_ffv1_close(AVCodecContext *avctx);
150 static av_always_inline int fold(int diff, int bits)
155 diff = sign_extend(diff, bits);
161 static inline void update_vlc_state(VlcState *const state, const int v)
163 int drift = state->drift;
164 int count = state->count;
165 state->error_sum += FFABS(v);
168 if (count == 128) { // FIXME: variable
171 state->error_sum >>= 1;
175 if (drift <= -count) {
176 state->bias = FFMAX(state->bias - 1, -128);
178 drift = FFMAX(drift + count, -count + 1);
179 } else if (drift > 0) {
180 state->bias = FFMIN(state->bias + 1, 127);
182 drift = FFMIN(drift - count, 0);
185 state->drift = drift;
186 state->count = count;
190 #define RENAME(name) name
191 #include "ffv1_template.c"
196 #define RENAME(name) name ## 32
197 #include "ffv1_template.c"
201 #endif /* AVCODEC_FFV1_H */