Libav
ffv1.c
Go to the documentation of this file.
1 /*
2  * FFV1 codec for libavcodec
3  *
4  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "libavutil/attributes.h"
29 
30 #include "avcodec.h"
31 #include "put_bits.h"
32 #include "rangecoder.h"
33 #include "mathops.h"
34 #include "ffv1.h"
35 
36 const int8_t ffv1_quant5_10bit[256] = {
37  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
38  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40  1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
42  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
43  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
44  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
46  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
47  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
48  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
49  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
50  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52  -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
53 };
54 
55 const int8_t ffv1_quant5[256] = {
56  0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
58  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
59  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
60  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
61  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
62  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
63  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
64  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
65  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
66  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
67  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
68  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
69  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
70  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
71  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
72 };
73 
74 const int8_t ffv1_quant9_10bit[256] = {
75  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
76  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
77  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
78  3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
79  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
80  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
81  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
82  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
83  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
84  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
85  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
86  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
87  -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
88  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
89  -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
90  -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
91 };
92 
93 const int8_t ffv1_quant11[256] = {
94  0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
95  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
96  4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
97  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
98  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
99  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
100  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
101  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
102  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
103  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
104  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
105  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
106  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
107  -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
108  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
109  -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
110 };
111 
112 const uint8_t ffv1_ver2_state[256] = {
113  0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
114  59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
115  40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
116  53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
117  87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
118  85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
119  105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
120  115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
121  165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
122  147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
123  172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
124  175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
125  197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
126  209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
127  226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
128  241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
129 };
130 
131 
133 {
134  FFV1Context *s = avctx->priv_data;
135 
136  s->avctx = avctx;
137  s->flags = avctx->flags;
138 
139  if (!avctx->width || !avctx->height)
140  return AVERROR_INVALIDDATA;
141 
142  s->width = avctx->width;
143  s->height = avctx->height;
144 
145  // defaults
146  s->num_h_slices = 1;
147  s->num_v_slices = 1;
148 
149  return 0;
150 }
151 
153 {
154  int j;
155 
156  fs->plane_count = f->plane_count;
157  fs->transparency = f->transparency;
158  for (j = 0; j < f->plane_count; j++) {
159  PlaneContext *const p = &fs->plane[j];
160 
161  if (fs->ac != AC_GOLOMB_RICE) {
162  if (!p->state)
164  sizeof(uint8_t));
165  if (!p->state)
166  return AVERROR(ENOMEM);
167  } else {
168  if (!p->vlc_state)
169  p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
170  if (!p->vlc_state)
171  return AVERROR(ENOMEM);
172  }
173  }
174 
175  if (fs->ac == AC_RANGE_CUSTOM_TAB) {
176  //FIXME only redo if state_transition changed
177  for (j = 1; j < 256; j++) {
178  fs->c.one_state[j] = f->state_transition[j];
179  fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
180  }
181  }
182 
183  return 0;
184 }
185 
187 {
188  int i, j;
189 
191  if (f->slice_count <= 0) {
192  av_log(f->avctx, AV_LOG_ERROR, "Invalid number of slices\n");
193  return AVERROR(EINVAL);
194  }
195 
196  for (i = 0; i < f->slice_count; i++) {
197  int sx = i % f->num_h_slices;
198  int sy = i / f->num_h_slices;
199  int sxs = f->avctx->width * sx / f->num_h_slices;
200  int sxe = f->avctx->width * (sx + 1) / f->num_h_slices;
201  int sys = f->avctx->height * sy / f->num_v_slices;
202  int sye = f->avctx->height * (sy + 1) / f->num_v_slices;
203  FFV1Context *fs = av_mallocz(sizeof(*fs));
204  if (!fs)
205  goto memfail;
206 
207  f->slice_context[i] = fs;
208  memcpy(fs, f, sizeof(*fs));
209  memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
210 
211  fs->slice_width = sxe - sxs;
212  fs->slice_height = sye - sys;
213  fs->slice_x = sxs;
214  fs->slice_y = sys;
215 
216  fs->sample_buffer = av_malloc(3 * MAX_PLANES * (fs->width + 6) *
217  sizeof(*fs->sample_buffer));
218  if (!fs->sample_buffer) {
219  av_free(fs);
220  goto memfail;
221  }
222  }
223  return 0;
224 
225 memfail:
226  for (j = 0; j < i; j++) {
228  av_free(f->slice_context[j]);
229  }
230  return AVERROR(ENOMEM);
231 }
232 
234 {
235  int i;
236 
237  for (i = 0; i < f->quant_table_count; i++) {
238  f->initial_states[i] = av_malloc(f->context_count[i] *
239  sizeof(*f->initial_states[i]));
240  if (!f->initial_states[i])
241  return AVERROR(ENOMEM);
242  memset(f->initial_states[i], 128,
243  f->context_count[i] * sizeof(*f->initial_states[i]));
244  }
245  return 0;
246 }
247 
249 {
250  int i, j;
251 
252  for (i = 0; i < f->plane_count; i++) {
253  PlaneContext *p = &fs->plane[i];
254 
255  p->interlace_bit_state[0] = 128;
256  p->interlace_bit_state[1] = 128;
257 
258  if (fs->ac != AC_GOLOMB_RICE) {
259  if (f->initial_states[p->quant_table_index]) {
260  memcpy(p->state, f->initial_states[p->quant_table_index],
262  } else
263  memset(p->state, 128, CONTEXT_SIZE * p->context_count);
264  } else {
265  for (j = 0; j < p->context_count; j++) {
266  p->vlc_state[j].drift = 0;
267  p->vlc_state[j].error_sum = 4; //FFMAX((RANGE + 32)/64, 2);
268  p->vlc_state[j].bias = 0;
269  p->vlc_state[j].count = 1;
270  }
271  }
272  }
273 }
274 
276 {
277  FFV1Context *s = avctx->priv_data;
278  int i, j;
279 
280  for (j = 0; j < s->slice_count; j++) {
281  FFV1Context *fs = s->slice_context[j];
282  for (i = 0; i < s->plane_count; i++) {
283  PlaneContext *p = &fs->plane[i];
284 
285  av_freep(&p->state);
286  av_freep(&p->vlc_state);
287  }
288  av_freep(&fs->sample_buffer);
289  }
290 
291  av_freep(&avctx->stats_out);
292  for (j = 0; j < s->quant_table_count; j++) {
293  av_freep(&s->initial_states[j]);
294  for (i = 0; i < s->slice_count; i++) {
295  FFV1Context *sf = s->slice_context[i];
296  av_freep(&sf->rc_stat2[j]);
297  }
298  av_freep(&s->rc_stat2[j]);
299  }
300 
301  for (i = 0; i < s->slice_count; i++)
302  av_freep(&s->slice_context[i]);
303 
304  return 0;
305 }
int ffv1_allocate_initial_states(FFV1Context *f)
Definition: ffv1.c:233
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
const int8_t ffv1_quant5[256]
Definition: ffv1.c:55
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
av_cold int ffv1_common_init(AVCodecContext *avctx)
Definition: ffv1.c:132
int flags
Definition: ffv1.h:83
int quant_table_count
Definition: ffv1.h:112
int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:152
int slice_height
Definition: ffv1.h:119
int16_t * sample_buffer
Definition: ffv1.h:101
uint8_t zero_state[256]
Definition: rangecoder.h:40
Range coder.
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition: ffv1.h:76
int height
Definition: ffv1.h:79
uint8_t one_state[256]
Definition: rangecoder.h:41
Macro definitions for various function/variable attributes.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int plane_count
Definition: ffv1.h:90
const int8_t ffv1_quant11[256]
Definition: ffv1.c:93
const int8_t ffv1_quant5_10bit[256]
Definition: ffv1.c:36
uint8_t
#define av_cold
Definition: attributes.h:66
int8_t bias
Definition: ffv1.h:54
RangeCoder c
Definition: ffv1.h:72
int slice_y
Definition: ffv1.h:121
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition: ffv1.h:98
uint8_t count
Definition: ffv1.h:55
VlcState * vlc_state
Definition: ffv1.h:63
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2556
int slice_width
Definition: ffv1.h:118
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
const int8_t ffv1_quant9_10bit[256]
Definition: ffv1.c:74
#define AVERROR(e)
Definition: error.h:43
int context_count
Definition: ffv1.h:61
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
int ac
Definition: ffv1.h:91
#define AC_RANGE_CUSTOM_TAB
Definition: ffv1.h:41
Definition: ffv1.h:51
uint8_t state_transition[256]
Definition: ffv1.h:97
int num_h_slices
Definition: ffv1.h:117
int width
picture width / height.
Definition: avcodec.h:1580
const uint8_t ffv1_ver2_state[256]
Definition: ffv1.c:112
#define MAX_PLANES
Definition: ffv1.h:33
int slice_count
Definition: ffv1.h:115
int16_t drift
Definition: ffv1.h:52
av_cold int ffv1_init_slice_contexts(FFV1Context *f)
Definition: ffv1.c:186
int context_count[MAX_QUANT_TABLES]
Definition: ffv1.h:96
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1409
void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:248
#define AC_GOLOMB_RICE
Definition: ffv1.h:39
uint16_t error_sum
Definition: ffv1.h:53
#define CONTEXT_SIZE
Definition: ffv1.h:34
int quant_table_index
Definition: ffv1.h:60
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:62
void * priv_data
Definition: avcodec.h:1451
PlaneContext plane[MAX_PLANES]
Definition: ffv1.h:93
int transparency
Definition: ffv1.h:82
av_cold int ffv1_close(AVCodecContext *avctx)
Definition: ffv1.c:275
struct FFV1Context * slice_context[MAX_SLICES]
Definition: ffv1.h:114
uint8_t interlace_bit_state[2]
Definition: ffv1.h:64
int num_v_slices
Definition: ffv1.h:116
AVCodecContext * avctx
Definition: ffv1.h:71
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int slice_x
Definition: ffv1.h:120
int width
Definition: ffv1.h:79
bitstream writer API