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
73typedef struct FFV1SliceContext {
74 int16_t *sample_buffer;
76
81 int sx, sy;
82
87 int remap;
88
89 // RefStruct reference, array of MAX_PLANES elements
93
94 int ac_byte_count; ///< number of bytes used for AC coding
95
96 union {
97 // decoder-only
98 struct {
101 };
102
103 // encoder-only
104 struct {
105 uint64_t rc_stat[256][2];
106 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
107 };
108 };
110
111 uint32_t *bitmap [4]; //float encode
112 uint16_t *fltmap [4]; //halffloat encode & decode
113 uint32_t *fltmap32[4]; //float decode
114 unsigned int fltmap_size[4];
115 unsigned int fltmap32_size[4];
116 struct Unit {
117 uint32_t val; //this is unneeded if you accept a dereference on each access
118 uint32_t ndx;
119 } *unit[4];
121
122typedef struct FFV1Context {
123 AVClass *class;
125 uint64_t rc_stat[256][2];
126 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
134 int flags;
139 uint32_t crcref;
144
147 int ac; ///< 1=range coder <-> 0=golomb rice
150 uint8_t state_transition[256];
153 int flt;
156 int bayer;
157 int bayer_order; /* 0 = RGGB (only supported value for now) */
159
161
162 int ec;
163 int intra;
167
170
173
178
180 /* RefStruct object, per-slice damage flags shared between frame threads.
181 *
182 * After a frame thread marks some slice as finished with
183 * ff_progress_frame_report(), the corresponding array element must not be
184 * accessed by this thread anymore, as from then on it is owned by the next
185 * thread.
186 */
188 /* Frame damage flag, used to delay announcing progress, since ER is
189 * applied after all the slices are decoded.
190 * NOT shared between frame threads.
191 */
194
203int ff_need_new_slices(int width, int num_h_slices, int chroma_shift);
207 int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
208void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample);
209int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
210
211/**
212 * This is intended for both width and height
213 */
214int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift);
215
216static av_always_inline int fold(int diff, int bits)
217{
218 if (bits == 8)
219 diff = (int8_t)diff;
220 else {
222 }
223
224 return diff;
225}
226
227static inline void update_vlc_state(VlcState *const state, const int v)
228{
229 int drift = state->drift;
230 int count = state->count;
231 state->error_sum += FFABS(v);
232 drift += v;
233
234 if (count == 128) { // FIXME: variable
235 count >>= 1;
236 drift >>= 1;
237 state->error_sum >>= 1;
238 }
239 count++;
240
241 if (drift <= -count) {
242 state->bias = FFMAX(state->bias - 1, -128);
243
244 drift = FFMAX(drift + count, -count + 1);
245 } else if (drift > 0) {
246 state->bias = FFMIN(state->bias + 1, 127);
247
248 drift = FFMIN(drift - count, 0);
249 }
250
251 state->drift = drift;
252 state->count = count;
253}
254
255
256static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
257 int is_signed)
258{
259 if (get_rac(c, state + 0))
260 return 0;
261 else {
262 int e;
263 unsigned a;
264 e = 0;
265 while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
266 e++;
267 if (e > 31)
268 return AVERROR_INVALIDDATA;
269 }
270
271 a = 1;
272 for (int i = e - 1; i >= 0; i--)
273 a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
274
275 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
276 return (a ^ e) - e;
277 }
278}
279
280#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 struct @346255127015250356166251341105367306144006377143 state
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:227
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:256
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:216
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:472
int flags
Definition ffv1.h:134
int configured_height
Definition ffv1.h:142
uint32_t crcref
Definition ffv1.h:139
int bayer_order
Definition ffv1.h:157
uint64_t rc_stat[256][2]
Definition ffv1.h:125
int quant_table_count
Definition ffv1.h:172
ProgressFrame last_picture
Definition ffv1.h:137
int key_frame
Definition ffv1.h:136
int ac
1=range coder <-> 0=golomb rice
Definition ffv1.h:147
int colorspace
Definition ffv1.h:152
void * hwaccel_last_picture_private
Definition ffv1.h:138
int qtable
Definition ffv1.h:166
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition ffv1.h:126
int version
Definition ffv1.h:127
int transparency
Definition ffv1.h:133
int maxsize_warned
Definition ffv1.h:158
int num_v_slices
Definition ffv1.h:176
ProgressFrame picture
Definition ffv1.h:137
int configured_width
Definition ffv1.h:142
uint8_t * slice_damaged
Definition ffv1.h:187
uint8_t frame_damaged
Definition ffv1.h:192
int bayer
Definition ffv1.h:156
int max_slice_count
Definition ffv1.h:175
int chroma_planes
Definition ffv1.h:131
int num_h_slices
Definition ffv1.h:177
int gob_count
Definition ffv1.h:171
int plane_count
Definition ffv1.h:146
int slice_count
Definition ffv1.h:174
enum AVPixelFormat configured_pix_fmt
Definition ffv1.h:141
int remap_mode
Definition ffv1.h:154
int micro_version
Definition ffv1.h:128
int bits_per_raw_sample
Definition ffv1.h:168
int use32bit
Definition ffv1.h:160
void * hwaccel_picture_private
Definition ffv1.h:138
int width
Definition ffv1.h:130
int chroma_h_shift
Definition ffv1.h:132
int key_frame_ok
Definition ffv1.h:164
int configured_ac
Definition ffv1.h:143
int context_count[MAX_QUANT_TABLES]
Definition ffv1.h:149
int packed_at_lsb
Definition ffv1.h:169
AVCodecContext * avctx
Definition ffv1.h:124
int remap_optimizer
Definition ffv1.h:155
const AVFrame * cur_enc_frame
Definition ffv1.h:145
FFV1SliceContext * slices
Definition ffv1.h:179
int context_model
Definition ffv1.h:165
int chroma_v_shift
Definition ffv1.h:132
int combined_version
Definition ffv1.h:129
int ec
Definition ffv1.h:162
int64_t picture_number
Definition ffv1.h:135
uint8_t state_transition[256]
Definition ffv1.h:150
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]
Definition ffv1.h:148
int height
Definition ffv1.h:130
enum AVPixelFormat pix_fmt
Definition ffv1.h:140
int intra
Definition ffv1.h:163
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition ffv1.h:151
int flt
Definition ffv1.h:153
int slice_rct_ry_coef
Definition ffv1.h:86
int slice_damaged
Definition ffv1.h:100
int16_t * sample_buffer
Definition ffv1.h:74
PutBitContext pb
Definition ffv1.h:91
unsigned int fltmap32_size[4]
Definition ffv1.h:115
int ac_byte_count
number of bytes used for AC coding
Definition ffv1.h:94
uint32_t * bitmap[4]
Definition ffv1.h:111
RangeCoder c
Definition ffv1.h:92
uint64_t rc_stat[256][2]
Definition ffv1.h:105
int slice_reset_contexts
Definition ffv1.h:99
uint32_t * fltmap32[4]
Definition ffv1.h:113
PlaneContext * plane
Definition ffv1.h:90
uint16_t * fltmap[4]
Definition ffv1.h:112
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition ffv1.h:106
int32_t * sample_buffer32
Definition ffv1.h:75
unsigned int fltmap_size[4]
Definition ffv1.h:114
int run_index
Definition ffv1.h:83
int remap_count[4]
Definition ffv1.h:109
int slice_height
Definition ffv1.h:78
int slice_width
Definition ffv1.h:77
int slice_coding_mode
Definition ffv1.h:84
struct FFV1SliceContext::Unit * unit[4]
int slice_rct_by_coef
Definition ffv1.h:85
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:89
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]