1 /*
2 * FFV1 codec for libavcodec
3 *
4 * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #ifndef AVCODEC_FFV1_H
24 #define AVCODEC_FFV1_H
25
26 /**
27 * @file
28 * FF Video Codec 1 (a lossless codec)
29 */
30
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"
36 #include "avcodec.h"
37 #include "get_bits.h"
38 #include "internal.h"
39 #include "mathops.h"
40 #include "put_bits.h"
41 #include "rangecoder.h"
42 #include "thread.h"
43
44 #ifdef __INTEL_COMPILER
45 #undef av_flatten
46 #define av_flatten
47 #endif
48
49 #define MAX_PLANES 4
50 #define CONTEXT_SIZE 32
51
52 #define MAX_QUANT_TABLES 8
53 #define MAX_CONTEXT_INPUTS 5
54
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
59
60 typedef struct VlcState {
61 int16_t drift;
62 uint16_t error_sum;
63 int8_t bias;
64 uint8_t count;
65 } VlcState;
66
67 typedef struct PlaneContext {
68 int16_t quant_table[MAX_CONTEXT_INPUTS][256];
69 int quant_table_index;
70 int context_count;
71 uint8_t (*state)[CONTEXT_SIZE];
72 VlcState *vlc_state;
73 uint8_t interlace_bit_state[2];
74 } PlaneContext;
75
76 #define MAX_SLICES 1024
77
78 typedef struct FFV1Context {
79 AVClass *class;
80 AVCodecContext *avctx;
81 RangeCoder c;
82 GetBitContext gb;
83 PutBitContext pb;
84 uint64_t rc_stat[256][2];
85 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
86 int version;
87 int micro_version;
88 int width, height;
89 int chroma_planes;
90 int chroma_h_shift, chroma_v_shift;
91 int transparency;
92 int flags;
93 int picture_number;
94 int key_frame;
95 ThreadFrame picture, last_picture;
96 struct FFV1Context *fsrc;
97
98 AVFrame *cur;
99 int plane_count;
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];
108 int run_index;
109 int colorspace;
110 int16_t *sample_buffer;
111 int32_t *sample_buffer32;
112
113 int use32bit;
114
115 int ec;
116 int intra;
117 int slice_damaged;
118 int key_frame_ok;
119 int context_model;
120
121 int bits_per_raw_sample;
122 int packed_at_lsb;
123
124 int gob_count;
125 int quant_table_count;
126
127 struct FFV1Context *slice_context[MAX_SLICES];
128 int slice_count;
129 int max_slice_count;
130 int num_v_slices;
131 int num_h_slices;
132 int slice_width;
133 int slice_height;
134 int slice_x;
135 int slice_y;
136 int slice_reset_contexts;
137 int slice_coding_mode;
138 int slice_rct_by_coef;
139 int slice_rct_ry_coef;
140 } FFV1Context;
141
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);
149
fold(int diff,int bits)150 static av_always_inline int fold(int diff, int bits)
151 {
152 if (bits == 8)
153 diff = (int8_t)diff;
154 else {
155 diff = sign_extend(diff, bits);
156 }
157
158 return diff;
159 }
160
update_vlc_state(VlcState * const state,const int v)161 static inline void update_vlc_state(VlcState *const state, const int v)
162 {
163 int drift = state->drift;
164 int count = state->count;
165 state->error_sum += FFABS(v);
166 drift += v;
167
168 if (count == 128) { // FIXME: variable
169 count >>= 1;
170 drift >>= 1;
171 state->error_sum >>= 1;
172 }
173 count++;
174
175 if (drift <= -count) {
176 state->bias = FFMAX(state->bias - 1, -128);
177
178 drift = FFMAX(drift + count, -count + 1);
179 } else if (drift > 0) {
180 state->bias = FFMIN(state->bias + 1, 127);
181
182 drift = FFMIN(drift - count, 0);
183 }
184
185 state->drift = drift;
186 state->count = count;
187 }
188
189 #define TYPE int16_t
190 #define RENAME(name) name
191 #include "ffv1_template.c"
192 #undef TYPE
193 #undef RENAME
194
195 #define TYPE int32_t
196 #define RENAME(name) name ## 32
197 #include "ffv1_template.c"
198 #undef TYPE
199 #undef RENAME
200
201 #endif /* AVCODEC_FFV1_H */
202