FFmpeg
Loading...
Searching...
No Matches
ffv1.h
Go to the documentation of this file.
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
32#include "avcodec.h"
33#include "get_bits.h"
34#include "mathops.h"
35#include "progressframe.h"
36#include "put_bits.h"
37#include "rangecoder.h"
38
39#ifdef __INTEL_COMPILER
40#undef av_flatten
41#define av_flatten
42#endif
43
44#define MAX_PLANES 4
45#define CONTEXT_SIZE 32
46
47#define MAX_QUANT_TABLES 8
48#define MAX_QUANT_TABLE_SIZE 256
49#define MAX_QUANT_TABLE_MASK (MAX_QUANT_TABLE_SIZE - 1)
50#define MAX_CONTEXT_INPUTS 5
51
52#define AC_GOLOMB_RICE 0
53#define AC_RANGE_DEFAULT_TAB 1
54#define AC_RANGE_CUSTOM_TAB 2
55#define AC_RANGE_DEFAULT_TAB_FORCE -2
56
57typedef struct VlcState {
58 uint32_t error_sum;
59 int16_t drift;
60 int8_t bias;
61 uint8_t count;
62} VlcState;
63
70
71#define MAX_SLICES 1024
72
73struct FFV1RemapBuffers;
74
75typedef struct FFV1SliceContext {
76 int16_t *sample_buffer;
78
83 int sx, sy;
84
89 int remap;
90
91 // RefStruct reference, array of MAX_PLANES elements
95
96 int ac_byte_count; ///< number of bytes used for AC coding
97
98 union {
99 // decoder-only
100 struct {
103 };
104
105 // encoder-only
106 struct {
107 uint64_t rc_stat[256][2];
108 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
109 };
110 };
112
113 uint32_t *bitmap [4]; //float encode
114 uint16_t *fltmap [4]; //halffloat encode & decode
115 uint32_t *fltmap32[4]; //float decode
116 unsigned int fltmap_size[4];
117 unsigned int fltmap32_size[4];
118 struct Unit {
119 uint32_t val; //this is unneeded if you accept a dereference on each access
120 uint32_t ndx;
121 } *unit[4];
122
123 struct FFV1RemapBuffers *remap_buf; ///< encoder-only, allocated for 32-bit float remap
125
126typedef struct FFV1Context {
127 AVClass *class;
129 uint64_t rc_stat[256][2];
130 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
138 int flags;
143 uint32_t crcref;
148
151 int ac; ///< 1=range coder <-> 0=golomb rice
154 uint8_t state_transition[256];
157 int flt;
160 int bayer;
161 int bayer_order; /* 0 = RGGB (only supported value for now) */
163
165
166 int ec;
167 int intra;
171
174
177
182
184 /* RefStruct object, per-slice damage flags shared between frame threads.
185 *
186 * After a frame thread marks some slice as finished with
187 * ff_progress_frame_report(), the corresponding array element must not be
188 * accessed by this thread anymore, as from then on it is owned by the next
189 * thread.
190 */
192 /* Frame damage flag, used to delay announcing progress, since ER is
193 * applied after all the slices are decoded.
194 * NOT shared between frame threads.
195 */
198
207int ff_need_new_slices(int width, int num_h_slices, int chroma_shift);
211 int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
212void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample);
213int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
214
215/**
216 * This is intended for both width and height
217 */
218int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift);
219
220static av_always_inline int fold(int diff, int bits)
221{
222 if (bits == 8)
223 diff = (int8_t)diff;
224 else {
226 }
227
228 return diff;
229}
230
231static inline void update_vlc_state(VlcState *const state, const int v)
232{
233 int drift = state->drift;
234 int count = state->count;
235 state->error_sum += FFABS(v);
236 drift += v;
237
238 if (count == 128) { // FIXME: variable
239 count >>= 1;
240 drift >>= 1;
241 state->error_sum >>= 1;
242 }
243 count++;
244
245 if (drift <= -count) {
246 state->bias = FFMAX(state->bias - 1, -128);
247
248 drift = FFMAX(drift + count, -count + 1);
249 } else if (drift > 0) {
250 state->bias = FFMIN(state->bias + 1, 127);
251
252 drift = FFMIN(drift - count, 0);
253 }
254
255 state->drift = drift;
256 state->count = count;
257}
258
259
260static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
261 int is_signed)
262{
263 if (get_rac(c, state + 0))
264 return 0;
265 else {
266 int e;
267 unsigned a;
268 e = 0;
269 while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
270 e++;
271 if (e > 31)
272 return AVERROR_INVALIDDATA;
273 }
274
275 a = 1;
276 for (int i = e - 1; i >= 0; i--)
277 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
278
279 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
280 return (a ^ e) - e;
281 }
282}
283
284#endif /* AVCODEC_FFV1_H */
int32_t
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
long long int64_t
Definition coverity.c:34
static const uint8_t bits[8]
Definition fastaudio.c:100
int ff_ffv1_read_extra_header(FFV1Context *f)
Definition ffv1_parse.c:70
int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1.c:72
#define CONTEXT_SIZE
Definition ffv1.h:45
int ff_ffv1_init_slice_contexts(FFV1Context *f)
Definition ffv1.c:142
int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
Definition ffv1.c:36
int ff_ffv1_init_slices_state(FFV1Context *f)
Definition ffv1.c:110
void ff_ffv1_close(FFV1Context *s)
Definition ffv1.c:268
PlaneContext * ff_ffv1_planes_alloc(void)
Definition ffv1.c:66
static void update_vlc_state(VlcState *const state, const int v)
Definition ffv1.h:231
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state)
Definition ffv1_parse.c:216
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift)
This is intended for both width and height.
Definition ffv1.c:127
#define MAX_QUANT_TABLE_SIZE
Definition ffv1.h:48
int ff_ffv1_read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition ffv1_parse.c:52
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition ffv1.h:260
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift)
Definition ffv1.c:120
int ff_ffv1_allocate_initial_states(FFV1Context *f)
Definition ffv1.c:185
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
Definition ffv1.c:200
#define MAX_QUANT_TABLES
Definition ffv1.h:47
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
Definition ffv1.c:224
static av_always_inline int fold(int diff, int bits)
Definition ffv1.h:220
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition ffv1.c:263
#define MAX_CONTEXT_INPUTS
Definition ffv1.h:50
bitstream reader API header.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
int a
static const int16_t quant_table[64]
Definition intrax8.c:511
unsigned offset
Definition libaomenc.c:763
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_flatten
Definition attributes.h:123
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
bitstream writer API
Range coder.
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition rangecoder.h:118
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int flags
Definition ffv1.h:138
int configured_height
Definition ffv1.h:146
uint32_t crcref
Definition ffv1.h:143
int bayer_order
Definition ffv1.h:161
uint64_t rc_stat[256][2]
Definition ffv1.h:129
int quant_table_count
Definition ffv1.h:176
ProgressFrame last_picture
Definition ffv1.h:141
int key_frame
Definition ffv1.h:140
int ac
1=range coder <-> 0=golomb rice
Definition ffv1.h:151
int colorspace
Definition ffv1.h:156
void * hwaccel_last_picture_private
Definition ffv1.h:142
int qtable
Definition ffv1.h:170
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition ffv1.h:130
int version
Definition ffv1.h:131
int transparency
Definition ffv1.h:137
int maxsize_warned
Definition ffv1.h:162
int num_v_slices
Definition ffv1.h:180
ProgressFrame picture
Definition ffv1.h:141
int configured_width
Definition ffv1.h:146
uint8_t * slice_damaged
Definition ffv1.h:191
uint8_t frame_damaged
Definition ffv1.h:196
int bayer
Definition ffv1.h:160
int max_slice_count
Definition ffv1.h:179
int chroma_planes
Definition ffv1.h:135
int num_h_slices
Definition ffv1.h:181
int gob_count
Definition ffv1.h:175
int plane_count
Definition ffv1.h:150
int slice_count
Definition ffv1.h:178
enum AVPixelFormat configured_pix_fmt
Definition ffv1.h:145
int remap_mode
Definition ffv1.h:158
int micro_version
Definition ffv1.h:132
int bits_per_raw_sample
Definition ffv1.h:172
int use32bit
Definition ffv1.h:164
void * hwaccel_picture_private
Definition ffv1.h:142
int width
Definition ffv1.h:134
int chroma_h_shift
Definition ffv1.h:136
int key_frame_ok
Definition ffv1.h:168
int configured_ac
Definition ffv1.h:147
int context_count[MAX_QUANT_TABLES]
Definition ffv1.h:153
int packed_at_lsb
Definition ffv1.h:173
AVCodecContext * avctx
Definition ffv1.h:128
int remap_optimizer
Definition ffv1.h:159
const AVFrame * cur_enc_frame
Definition ffv1.h:149
FFV1SliceContext * slices
Definition ffv1.h:183
int context_model
Definition ffv1.h:169
int chroma_v_shift
Definition ffv1.h:136
int combined_version
Definition ffv1.h:133
int ec
Definition ffv1.h:166
int64_t picture_number
Definition ffv1.h:139
uint8_t state_transition[256]
Definition ffv1.h:154
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]
Definition ffv1.h:152
int height
Definition ffv1.h:134
enum AVPixelFormat pix_fmt
Definition ffv1.h:144
int intra
Definition ffv1.h:167
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition ffv1.h:155
int flt
Definition ffv1.h:157
int slice_rct_ry_coef
Definition ffv1.h:88
int slice_damaged
Definition ffv1.h:102
int16_t * sample_buffer
Definition ffv1.h:76
struct FFV1RemapBuffers * remap_buf
encoder-only, allocated for 32-bit float remap
Definition ffv1.h:123
PutBitContext pb
Definition ffv1.h:93
unsigned int fltmap32_size[4]
Definition ffv1.h:117
int ac_byte_count
number of bytes used for AC coding
Definition ffv1.h:96
uint32_t * bitmap[4]
Definition ffv1.h:113
RangeCoder c
Definition ffv1.h:94
uint64_t rc_stat[256][2]
Definition ffv1.h:107
int slice_reset_contexts
Definition ffv1.h:101
uint32_t * fltmap32[4]
Definition ffv1.h:115
PlaneContext * plane
Definition ffv1.h:92
uint16_t * fltmap[4]
Definition ffv1.h:114
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition ffv1.h:108
int32_t * sample_buffer32
Definition ffv1.h:77
unsigned int fltmap_size[4]
Definition ffv1.h:116
int run_index
Definition ffv1.h:85
int remap_count[4]
Definition ffv1.h:111
int slice_height
Definition ffv1.h:80
int slice_width
Definition ffv1.h:79
int slice_coding_mode
Definition ffv1.h:86
struct FFV1SliceContext::Unit * unit[4]
int slice_rct_by_coef
Definition ffv1.h:87
VlcState * vlc_state
Definition ffv1.h:68
int quant_table_index
Definition ffv1.h:65
int context_count
Definition ffv1.h:66
uint8_t(* state)[CONTEXT_SIZE]
Definition ffv1.h:67
The ProgressFrame structure.
int8_t bias
Definition ffv1.h:60
uint8_t count
Definition ffv1.h:61
int16_t drift
Definition ffv1.h:59
uint32_t error_sum
Definition ffv1.h:58
#define width
Definition dsp.h:99
static struct @232240247175332074307271266321204261331170211232 state
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]