ffv1: split decoder and encoder
[libav.git] / libavcodec / ffv1enc.c
CommitLineData
71f7b22d
LB
1/*
2 * FFV1 encoder for libavcodec
3 *
4 * Copyright (c) 2012 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
23/**
24 * @file
25 * FF Video Codec 1 (a lossless codec) encoder
26 */
27
28#include "libavutil/avassert.h"
29#include "avcodec.h"
30#include "get_bits.h"
31#include "put_bits.h"
32#include "dsputil.h"
33#include "rangecoder.h"
34#include "golomb.h"
35#include "mathops.h"
36#include "ffv1.h"
37
38static void find_best_state(uint8_t best_state[256][256],
39 const uint8_t one_state[256])
40{
41 int i, j, k, m;
42 double l2tab[256];
43
44 for (i = 1; i < 256; i++)
45 l2tab[i] = log2(i / 256.0);
46
47 for (i = 0; i < 256; i++) {
48 double best_len[256];
49 double p = i / 256.0;
50
51 for (j = 0; j < 256; j++)
52 best_len[j] = 1 << 30;
53
54 for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
55 double occ[256] = { 0 };
56 double len = 0;
57 occ[j] = 1.0;
58 for (k = 0; k < 256; k++) {
59 double newocc[256] = { 0 };
60 for (m = 0; m < 256; m++)
61 if (occ[m]) {
62 len -= occ[m] * (p * l2tab[m] +
63 (1 - p) * l2tab[256 - m]);
64 }
65 if (len < best_len[k]) {
66 best_len[k] = len;
67 best_state[i][k] = j;
68 }
69 for (m = 0; m < 256; m++)
70 if (occ[m]) {
71 newocc[one_state[m]] += occ[m] * p;
72 newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
73 }
74 memcpy(occ, newocc, sizeof(occ));
75 }
76 }
77 }
78}
79
80static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
81 uint8_t *state, int v,
82 int is_signed,
83 uint64_t rc_stat[256][2],
84 uint64_t rc_stat2[32][2])
85{
86 int i;
87
88#define put_rac(C, S, B) \
89 do { \
90 if (rc_stat) { \
91 rc_stat[*(S)][B]++; \
92 rc_stat2[(S) - state][B]++; \
93 } \
94 put_rac(C, S, B); \
95 } while (0)
96
97 if (v) {
98 const int a = FFABS(v);
99 const int e = av_log2(a);
100 put_rac(c, state + 0, 0);
101 if (e <= 9) {
102 for (i = 0; i < e; i++)
103 put_rac(c, state + 1 + i, 1); // 1..10
104 put_rac(c, state + 1 + i, 0);
105
106 for (i = e - 1; i >= 0; i--)
107 put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
108
109 if (is_signed)
110 put_rac(c, state + 11 + e, v < 0); // 11..21
111 } else {
112 for (i = 0; i < e; i++)
113 put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
114 put_rac(c, state + 1 + 9, 0);
115
116 for (i = e - 1; i >= 0; i--)
117 put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
118
119 if (is_signed)
120 put_rac(c, state + 11 + 10, v < 0); // 11..21
121 }
122 } else {
123 put_rac(c, state + 0, 1);
124 }
125#undef put_rac
126}
127
128static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
129 int v, int is_signed)
130{
131 put_symbol_inline(c, state, v, is_signed, NULL, NULL);
132}
133
134static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
135 int v, int bits)
136{
137 int i, k, code;
138 v = fold(v - state->bias, bits);
139
140 i = state->count;
141 k = 0;
142 while (i < state->error_sum) { // FIXME: optimize
143 k++;
144 i += i;
145 }
146
147 assert(k <= 8);
148
149#if 0 // JPEG LS
150 if (k == 0 && 2 * state->drift <= -state->count)
151 code = v ^ (-1);
152 else
153 code = v;
154#else
155 code = v ^ ((2 * state->drift + state->count) >> 31);
156#endif
157
158 av_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
159 state->bias, state->error_sum, state->drift, state->count, k);
160 set_sr_golomb(pb, code, k, 12, bits);
161
162 update_vlc_state(state, v);
163}
164
165static av_always_inline int encode_line(FFV1Context *s, int w,
166 int16_t *sample[3],
167 int plane_index, int bits)
168{
169 PlaneContext *const p = &s->plane[plane_index];
170 RangeCoder *const c = &s->c;
171 int x;
172 int run_index = s->run_index;
173 int run_count = 0;
174 int run_mode = 0;
175
176 if (s->ac) {
177 if (c->bytestream_end - c->bytestream < w * 20) {
178 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
179 return -1;
180 }
181 } else {
182 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
183 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
184 return -1;
185 }
186 }
187
188 for (x = 0; x < w; x++) {
189 int diff, context;
190
191 context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
192 diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
193
194 if (context < 0) {
195 context = -context;
196 diff = -diff;
197 }
198
199 diff = fold(diff, bits);
200
201 if (s->ac) {
202 if (s->flags & CODEC_FLAG_PASS1) {
203 put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
204 s->rc_stat2[p->quant_table_index][context]);
205 } else {
206 put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
207 }
208 } else {
209 if (context == 0)
210 run_mode = 1;
211
212 if (run_mode) {
213 if (diff) {
214 while (run_count >= 1 << ff_log2_run[run_index]) {
215 run_count -= 1 << ff_log2_run[run_index];
216 run_index++;
217 put_bits(&s->pb, 1, 1);
218 }
219
220 put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
221 if (run_index)
222 run_index--;
223 run_count = 0;
224 run_mode = 0;
225 if (diff > 0)
226 diff--;
227 } else {
228 run_count++;
229 }
230 }
231
232 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
233 run_count, run_index, run_mode, x,
234 (int)put_bits_count(&s->pb));
235
236 if (run_mode == 0)
237 put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
238 }
239 }
240 if (run_mode) {
241 while (run_count >= 1 << ff_log2_run[run_index]) {
242 run_count -= 1 << ff_log2_run[run_index];
243 run_index++;
244 put_bits(&s->pb, 1, 1);
245 }
246
247 if (run_count)
248 put_bits(&s->pb, 1, 1);
249 }
250 s->run_index = run_index;
251
252 return 0;
253}
254
255static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
256 int stride, int plane_index)
257{
258 int x, y, i;
259 const int ring_size = s->avctx->context_model ? 3 : 2;
260 int16_t *sample[3];
261 s->run_index = 0;
262
263 memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
264
265 for (y = 0; y < h; y++) {
266 for (i = 0; i < ring_size; i++)
267 sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
268
269 sample[0][-1] = sample[1][0];
270 sample[1][w] = sample[1][w - 1];
271// { START_TIMER
272 if (s->avctx->bits_per_raw_sample <= 8) {
273 for (x = 0; x < w; x++)
274 sample[0][x] = src[x + stride * y];
275 encode_line(s, w, sample, plane_index, 8);
276 } else {
277 for (x = 0; x < w; x++)
278 sample[0][x] = ((uint16_t *)(src + stride * y))[x] >>
279 (16 - s->avctx->bits_per_raw_sample);
280 encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
281 }
282// STOP_TIMER("encode line") }
283 }
284}
285
286static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h,
287 int stride)
288{
289 int x, y, p, i;
290 const int ring_size = s->avctx->context_model ? 3 : 2;
291 int16_t *sample[3][3];
292 s->run_index = 0;
293
294 memset(s->sample_buffer, 0, ring_size * 3 * (w + 6) * sizeof(*s->sample_buffer));
295
296 for (y = 0; y < h; y++) {
297 for (i = 0; i < ring_size; i++)
298 for (p = 0; p < 3; p++)
299 sample[p][i] = s->sample_buffer + p * ring_size * (w + 6) +
300 ((h + i - y) % ring_size) * (w + 6) + 3;
301
302 for (x = 0; x < w; x++) {
303 int v = src[x + stride * y];
304 int b = v & 0xFF;
305 int g = (v >> 8) & 0xFF;
306 int r = (v >> 16) & 0xFF;
307
308 b -= g;
309 r -= g;
310 g += (b + r) >> 2;
311 b += 0x100;
312 r += 0x100;
313
314 sample[0][0][x] = g;
315 sample[1][0][x] = b;
316 sample[2][0][x] = r;
317 }
318 for (p = 0; p < 3; p++) {
319 sample[p][0][-1] = sample[p][1][0];
320 sample[p][1][w] = sample[p][1][w - 1];
321 encode_line(s, w, sample[p], FFMIN(p, 1), 9);
322 }
323 }
324}
325
326static void write_quant_table(RangeCoder *c, int16_t *quant_table)
327{
328 int last = 0;
329 int i;
330 uint8_t state[CONTEXT_SIZE];
331 memset(state, 128, sizeof(state));
332
333 for (i = 1; i < 128; i++)
334 if (quant_table[i] != quant_table[i - 1]) {
335 put_symbol(c, state, i - last - 1, 0);
336 last = i;
337 }
338 put_symbol(c, state, i - last - 1, 0);
339}
340
341static void write_quant_tables(RangeCoder *c,
342 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
343{
344 int i;
345 for (i = 0; i < 5; i++)
346 write_quant_table(c, quant_table[i]);
347}
348
349static void write_header(FFV1Context *f)
350{
351 uint8_t state[CONTEXT_SIZE];
352 int i, j;
353 RangeCoder *const c = &f->slice_context[0]->c;
354
355 memset(state, 128, sizeof(state));
356
357 if (f->version < 2) {
358 put_symbol(c, state, f->version, 0);
359 put_symbol(c, state, f->ac, 0);
360 if (f->ac > 1) {
361 for (i = 1; i < 256; i++)
362 put_symbol(c, state,
363 f->state_transition[i] - c->one_state[i], 1);
364 }
365 put_symbol(c, state, f->colorspace, 0); // YUV cs type
366 if (f->version > 0)
367 put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
368 put_rac(c, state, 1); // chroma planes
369 put_symbol(c, state, f->chroma_h_shift, 0);
370 put_symbol(c, state, f->chroma_v_shift, 0);
371 put_rac(c, state, 0); // no transparency plane
372
373 write_quant_tables(c, f->quant_table);
374 } else {
375 put_symbol(c, state, f->slice_count, 0);
376 for (i = 0; i < f->slice_count; i++) {
377 FFV1Context *fs = f->slice_context[i];
378 put_symbol(c, state,
379 (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
380 put_symbol(c, state,
381 (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
382 put_symbol(c, state,
383 (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
384 0);
385 put_symbol(c, state,
386 (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
387 0);
388 for (j = 0; j < f->plane_count; j++) {
389 put_symbol(c, state, f->plane[j].quant_table_index, 0);
390 av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
391 }
392 }
393 }
394}
395
396static int write_extra_header(FFV1Context *f)
397{
398 RangeCoder *const c = &f->c;
399 uint8_t state[CONTEXT_SIZE];
400 int i, j, k;
401 uint8_t state2[32][CONTEXT_SIZE];
402
403 memset(state2, 128, sizeof(state2));
404 memset(state, 128, sizeof(state));
405
406 f->avctx->extradata = av_malloc(f->avctx->extradata_size = 10000 +
407 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32);
408 ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
409 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
410
411 put_symbol(c, state, f->version, 0);
412 put_symbol(c, state, f->ac, 0);
413 if (f->ac > 1)
414 for (i = 1; i < 256; i++)
415 put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
416 put_symbol(c, state, f->colorspace, 0); // YUV cs type
417 put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
418 put_rac(c, state, 1); // chroma planes
419 put_symbol(c, state, f->chroma_h_shift, 0);
420 put_symbol(c, state, f->chroma_v_shift, 0);
421 put_rac(c, state, 0); // no transparency plane
422 put_symbol(c, state, f->num_h_slices - 1, 0);
423 put_symbol(c, state, f->num_v_slices - 1, 0);
424
425 put_symbol(c, state, f->quant_table_count, 0);
426 for (i = 0; i < f->quant_table_count; i++)
427 write_quant_tables(c, f->quant_tables[i]);
428
429 for (i = 0; i < f->quant_table_count; i++) {
430 for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
431 if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
432 break;
433 if (j < f->context_count[i] * CONTEXT_SIZE) {
434 put_rac(c, state, 1);
435 for (j = 0; j < f->context_count[i]; j++)
436 for (k = 0; k < CONTEXT_SIZE; k++) {
437 int pred = j ? f->initial_states[i][j - 1][k] : 128;
438 put_symbol(c, state2[k],
439 (int8_t)(f->initial_states[i][j][k] - pred), 1);
440 }
441 } else {
442 put_rac(c, state, 0);
443 }
444 }
445
446 f->avctx->extradata_size = ff_rac_terminate(c);
447
448 return 0;
449}
450
451static int sort_stt(FFV1Context *s, uint8_t stt[256])
452{
453 int i, i2, changed, print = 0;
454
455 do {
456 changed = 0;
457 for (i = 12; i < 244; i++) {
458 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
459
460#define COST(old, new) \
461 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
462 s->rc_stat[old][1] * -log2((new) / 256.0)
463
464#define COST2(old, new) \
465 COST(old, new) + COST(256 - (old), 256 - (new))
466
467 double size0 = COST2(i, i) + COST2(i2, i2);
468 double sizeX = COST2(i, i2) + COST2(i2, i);
469 if (sizeX < size0 && i != 128 && i2 != 128) {
470 int j;
471 FFSWAP(int, stt[i], stt[i2]);
472 FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
473 FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
474 if (i != 256 - i2) {
475 FFSWAP(int, stt[256 - i], stt[256 - i2]);
476 FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
477 FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
478 }
479 for (j = 1; j < 256; j++) {
480 if (stt[j] == i)
481 stt[j] = i2;
482 else if (stt[j] == i2)
483 stt[j] = i;
484 if (i != 256 - i2) {
485 if (stt[256 - j] == 256 - i)
486 stt[256 - j] = 256 - i2;
487 else if (stt[256 - j] == 256 - i2)
488 stt[256 - j] = 256 - i;
489 }
490 }
491 print = changed = 1;
492 }
493 }
494 }
495 } while (changed);
496 return print;
497}
498
499static av_cold int ffv1_encode_init(AVCodecContext *avctx)
500{
501 FFV1Context *s = avctx->priv_data;
502 int i, j, k, m;
503
504 ffv1_common_init(avctx);
505
506 s->version = 0;
507 s->ac = avctx->coder_type ? 2 : 0;
508
509 if (s->ac > 1)
510 for (i = 1; i < 256; i++)
511 s->state_transition[i] = ffv1_ver2_state[i];
512
513 s->plane_count = 2;
514 for (i = 0; i < 256; i++) {
515 s->quant_table_count = 2;
516 if (avctx->bits_per_raw_sample <= 8) {
517 s->quant_tables[0][0][i] = ffv1_quant11[i];
518 s->quant_tables[0][1][i] = ffv1_quant11[i] * 11;
519 s->quant_tables[0][2][i] = ffv1_quant11[i] * 11 * 11;
520 s->quant_tables[1][0][i] = ffv1_quant11[i];
521 s->quant_tables[1][1][i] = ffv1_quant11[i] * 11;
522 s->quant_tables[1][2][i] = ffv1_quant5[i] * 11 * 11;
523 s->quant_tables[1][3][i] = ffv1_quant5[i] * 5 * 11 * 11;
524 s->quant_tables[1][4][i] = ffv1_quant5[i] * 5 * 5 * 11 * 11;
525 } else {
526 s->quant_tables[0][0][i] = ffv1_quant9_10bit[i];
527 s->quant_tables[0][1][i] = ffv1_quant9_10bit[i] * 11;
528 s->quant_tables[0][2][i] = ffv1_quant9_10bit[i] * 11 * 11;
529 s->quant_tables[1][0][i] = ffv1_quant9_10bit[i];
530 s->quant_tables[1][1][i] = ffv1_quant9_10bit[i] * 11;
531 s->quant_tables[1][2][i] = ffv1_quant5_10bit[i] * 11 * 11;
532 s->quant_tables[1][3][i] = ffv1_quant5_10bit[i] * 5 * 11 * 11;
533 s->quant_tables[1][4][i] = ffv1_quant5_10bit[i] * 5 * 5 * 11 * 11;
534 }
535 }
536 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
537 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
538 memcpy(s->quant_table, s->quant_tables[avctx->context_model],
539 sizeof(s->quant_table));
540
541 for (i = 0; i < s->plane_count; i++) {
542 PlaneContext *const p = &s->plane[i];
543
544 memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
545 p->quant_table_index = avctx->context_model;
546 p->context_count = s->context_count[p->quant_table_index];
547 }
548
549 if (ffv1_allocate_initial_states(s) < 0)
550 return AVERROR(ENOMEM);
551
552 avctx->coded_frame = &s->picture;
553 switch (avctx->pix_fmt) {
554 case AV_PIX_FMT_YUV444P16:
555 case AV_PIX_FMT_YUV422P16:
556 case AV_PIX_FMT_YUV420P16:
557 if (avctx->bits_per_raw_sample <= 8) {
558 av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
559 return -1;
560 }
561 if (!s->ac) {
562 av_log(avctx, AV_LOG_ERROR,
563 "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
564 return -1;
565 }
566 s->version = FFMAX(s->version, 1);
567 case AV_PIX_FMT_YUV444P:
568 case AV_PIX_FMT_YUV422P:
569 case AV_PIX_FMT_YUV420P:
570 case AV_PIX_FMT_YUV411P:
571 case AV_PIX_FMT_YUV410P:
572 s->colorspace = 0;
573 break;
574 case AV_PIX_FMT_RGB32:
575 s->colorspace = 1;
576 break;
577 default:
578 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
579 return -1;
580 }
581 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
582 &s->chroma_v_shift);
583
584 s->picture_number = 0;
585
586 if (avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
587 for (i = 0; i < s->quant_table_count; i++) {
588 s->rc_stat2[i] = av_mallocz(s->context_count[i] *
589 sizeof(*s->rc_stat2[i]));
590 if (!s->rc_stat2[i])
591 return AVERROR(ENOMEM);
592 }
593 }
594 if (avctx->stats_in) {
595 char *p = avctx->stats_in;
596 uint8_t best_state[256][256];
597 int gob_count = 0;
598 char *next;
599
600 av_assert0(s->version >= 2);
601
602 for (;; ) {
603 for (j = 0; j < 256; j++)
604 for (i = 0; i < 2; i++) {
605 s->rc_stat[j][i] = strtol(p, &next, 0);
606 if (next == p) {
607 av_log(avctx, AV_LOG_ERROR,
608 "2Pass file invalid at %d %d [%s]\n", j, i, p);
609 return -1;
610 }
611 p = next;
612 }
613 for (i = 0; i < s->quant_table_count; i++)
614 for (j = 0; j < s->context_count[i]; j++) {
615 for (k = 0; k < 32; k++)
616 for (m = 0; m < 2; m++) {
617 s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
618 if (next == p) {
619 av_log(avctx, AV_LOG_ERROR,
620 "2Pass file invalid at %d %d %d %d [%s]\n",
621 i, j, k, m, p);
622 return -1;
623 }
624 p = next;
625 }
626 }
627 gob_count = strtol(p, &next, 0);
628 if (next == p || gob_count < 0) {
629 av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
630 return -1;
631 }
632 p = next;
633 while (*p == '\n' || *p == ' ')
634 p++;
635 if (p[0] == 0)
636 break;
637 }
638 sort_stt(s, s->state_transition);
639
640 find_best_state(best_state, s->state_transition);
641
642 for (i = 0; i < s->quant_table_count; i++) {
643 for (j = 0; j < s->context_count[i]; j++)
644 for (k = 0; k < 32; k++) {
645 double p = 128;
646 if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
647 p = 256.0 * s->rc_stat2[i][j][k][1] /
648 (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
649 }
650 s->initial_states[i][j][k] =
651 best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
652 s->rc_stat2[i][j][k][1]) /
653 gob_count, 0, 255)];
654 }
655 }
656 }
657
658 if (s->version > 1) {
659 s->num_h_slices = 2;
660 s->num_v_slices = 2;
661 write_extra_header(s);
662 }
663
664 if (ffv1_init_slice_contexts(s) < 0)
665 return -1;
666 if (ffv1_init_slice_state(s) < 0)
667 return -1;
668
669#define STATS_OUT_SIZE 1024 * 1024 * 6
670 if (avctx->flags & CODEC_FLAG_PASS1) {
671 avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
672 for (i = 0; i < s->quant_table_count; i++)
673 for (j = 0; j < s->slice_count; j++) {
674 FFV1Context *sf = s->slice_context[j];
675 av_assert0(!sf->rc_stat2[i]);
676 sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
677 sizeof(*sf->rc_stat2[i]));
678 if (!sf->rc_stat2[i])
679 return AVERROR(ENOMEM);
680 }
681 }
682
683 return 0;
684}
685
686static int encode_slice(AVCodecContext *c, void *arg)
687{
688 FFV1Context *fs = *(void **)arg;
689 FFV1Context *f = fs->avctx->priv_data;
690 int width = fs->slice_width;
691 int height = fs->slice_height;
692 int x = fs->slice_x;
693 int y = fs->slice_y;
694 AVFrame *const p = &f->picture;
695
696 if (f->colorspace == 0) {
697 const int chroma_width = -((-width) >> f->chroma_h_shift);
698 const int chroma_height = -((-height) >> f->chroma_v_shift);
699 const int cx = x >> f->chroma_h_shift;
700 const int cy = y >> f->chroma_v_shift;
701
702 encode_plane(fs, p->data[0] + x + y * p->linesize[0],
703 width, height, p->linesize[0], 0);
704
705 encode_plane(fs, p->data[1] + cx + cy * p->linesize[1],
706 chroma_width, chroma_height, p->linesize[1], 1);
707 encode_plane(fs, p->data[2] + cx + cy * p->linesize[2],
708 chroma_width, chroma_height, p->linesize[2], 1);
709 } else {
710 encode_rgb_frame(fs, (uint32_t *)(p->data[0]) +
711 x + y * (p->linesize[0] / 4),
712 width, height, p->linesize[0] / 4);
713 }
714 emms_c();
715
716 return 0;
717}
718
719static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
720 const AVFrame *pict, int *got_packet)
721{
722 FFV1Context *f = avctx->priv_data;
723 RangeCoder *const c = &f->slice_context[0]->c;
724 AVFrame *const p = &f->picture;
725 int used_count = 0;
726 uint8_t keystate = 128;
727 uint8_t *buf_p;
728 int i, ret;
729
730 if (!pkt->data &&
731 (ret = av_new_packet(pkt, avctx->width * avctx->height *
732 ((8 * 2 + 1 + 1) * 4) / 8 + FF_MIN_BUFFER_SIZE)) < 0) {
733 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
734 return ret;
735 }
736
737 ff_init_range_encoder(c, pkt->data, pkt->size);
738 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
739
740 *p = *pict;
741 p->pict_type = AV_PICTURE_TYPE_I;
742
743 if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
744 put_rac(c, &keystate, 1);
745 p->key_frame = 1;
746 f->gob_count++;
747 write_header(f);
748 ffv1_clear_state(f);
749 } else {
750 put_rac(c, &keystate, 0);
751 p->key_frame = 0;
752 }
753
754 if (!f->ac) {
755 used_count += ff_rac_terminate(c);
756 init_put_bits(&f->slice_context[0]->pb, pkt->data + used_count,
757 pkt->size - used_count);
758 } else if (f->ac > 1) {
759 int i;
760 for (i = 1; i < 256; i++) {
761 c->one_state[i] = f->state_transition[i];
762 c->zero_state[256 - i] = 256 - c->one_state[i];
763 }
764 }
765
766 for (i = 1; i < f->slice_count; i++) {
767 FFV1Context *fs = f->slice_context[i];
768 uint8_t *start = pkt->data + (pkt->size - used_count) * i / f->slice_count;
769 int len = pkt->size / f->slice_count;
770
771 if (fs->ac)
772 ff_init_range_encoder(&fs->c, start, len);
773 else
774 init_put_bits(&fs->pb, start, len);
775 }
776 avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
777 f->slice_count, sizeof(void *));
778
779 buf_p = pkt->data;
780 for (i = 0; i < f->slice_count; i++) {
781 FFV1Context *fs = f->slice_context[i];
782 int bytes;
783
784 if (fs->ac) {
785 uint8_t state = 128;
786 put_rac(&fs->c, &state, 0);
787 bytes = ff_rac_terminate(&fs->c);
788 } else {
789 flush_put_bits(&fs->pb); // FIXME: nicer padding
790 bytes = used_count + (put_bits_count(&fs->pb) + 7) / 8;
791 used_count = 0;
792 }
793 if (i > 0) {
794 av_assert0(bytes < pkt->size / f->slice_count);
795 memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
796 av_assert0(bytes < (1 << 24));
797 AV_WB24(buf_p + bytes, bytes);
798 bytes += 3;
799 }
800 buf_p += bytes;
801 }
802
803 if ((avctx->flags & CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
804 int j, k, m;
805 char *p = avctx->stats_out;
806 char *end = p + STATS_OUT_SIZE;
807
808 memset(f->rc_stat, 0, sizeof(f->rc_stat));
809 for (i = 0; i < f->quant_table_count; i++)
810 memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
811
812 for (j = 0; j < f->slice_count; j++) {
813 FFV1Context *fs = f->slice_context[j];
814 for (i = 0; i < 256; i++) {
815 f->rc_stat[i][0] += fs->rc_stat[i][0];
816 f->rc_stat[i][1] += fs->rc_stat[i][1];
817 }
818 for (i = 0; i < f->quant_table_count; i++) {
819 for (k = 0; k < f->context_count[i]; k++)
820 for (m = 0; m < 32; m++) {
821 f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
822 f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
823 }
824 }
825 }
826
827 for (j = 0; j < 256; j++) {
828 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
829 f->rc_stat[j][0], f->rc_stat[j][1]);
830 p += strlen(p);
831 }
832 snprintf(p, end - p, "\n");
833
834 for (i = 0; i < f->quant_table_count; i++) {
835 for (j = 0; j < f->context_count[i]; j++)
836 for (m = 0; m < 32; m++) {
837 snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
838 f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
839 p += strlen(p);
840 }
841 }
842 snprintf(p, end - p, "%d\n", f->gob_count);
843 } else if (avctx->flags & CODEC_FLAG_PASS1)
844 avctx->stats_out[0] = '\0';
845
846 f->picture_number++;
847 pkt->size = buf_p - pkt->data;
848 pkt->flags |= AV_PKT_FLAG_KEY * p->key_frame;
849 *got_packet = 1;
850
851 return 0;
852}
853
854AVCodec ff_ffv1_encoder = {
855 .name = "ffv1",
856 .type = AVMEDIA_TYPE_VIDEO,
857 .id = AV_CODEC_ID_FFV1,
858 .priv_data_size = sizeof(FFV1Context),
859 .init = ffv1_encode_init,
860 .encode2 = ffv1_encode_frame,
861 .close = ffv1_close,
862 .capabilities = CODEC_CAP_SLICE_THREADS,
863 .pix_fmts = (const enum AVPixelFormat[]) {
864 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
865 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
866 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
867 AV_PIX_FMT_RGB32,
868 AV_PIX_FMT_NONE
869 },
870 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
871};