49 for (i = 1; i < 256; i++)
50 l2tab[i] =
log2(i / 256.0);
52 for (i = 0; i < 256; i++) {
56 for (j = 0; j < 256; j++)
57 best_len[j] = 1 << 30;
59 for (j =
FFMAX(i - 10, 1); j <
FFMIN(i + 11, 256); j++) {
60 double occ[256] = { 0 };
63 for (k = 0; k < 256; k++) {
64 double newocc[256] = { 0 };
65 for (m = 1; m < 256; m++)
67 len -= occ[m] * (p * l2tab[m] +
68 (1 - p) * l2tab[256 - m]);
70 if (len < best_len[k]) {
74 for (m = 0; m < 256; m++)
76 newocc[one_state[m]] += occ[m] * p;
77 newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
79 memcpy(occ, newocc,
sizeof(occ));
93 #define put_rac(C, S, B) \
97 rc_stat2[(S) - state][B]++; \
107 for (i = 0; i < e; i++)
111 for (i = e - 1; i >= 0; i--)
112 put_rac(c, state + 22 + i, (a >> i) & 1);
115 put_rac(c, state + 11 + e, v < 0);
117 for (i = 0; i < e; i++)
121 for (i = e - 1; i >= 0; i--)
125 put_rac(c, state + 11 + 10, v < 0);
134 int v,
int is_signed)
147 while (i < state->error_sum) {
155 if (k == 0 && 2 * state->
drift <= -state->
count)
160 code = v ^ ((2 * state->
drift + state->
count) >> 31);
163 av_dlog(
NULL,
"v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
172 int plane_index,
int bits)
193 for (x = 0; x < w; x++) {
196 context =
get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
197 diff = sample[0][x] -
predict(sample[0] + x, sample[1] + x);
204 diff =
fold(diff, bits);
237 av_dlog(s->
avctx,
"count:%d index:%d, mode:%d, x:%d pos:%d\n",
238 run_count, run_index, run_mode, x,
261 int stride,
int plane_index)
270 for (y = 0; y < h; y++) {
271 for (i = 0; i < ring_size; i++)
272 sample[i] = s->
sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
274 sample[0][-1] = sample[1][0];
275 sample[1][w] = sample[1][w - 1];
278 for (x = 0; x < w; x++)
279 sample[0][x] = src[x + stride * y];
283 for (x = 0; x < w; x++)
284 sample[0][x] = ((uint16_t *)(src + stride * y))[x];
286 for (x = 0; x < w; x++)
306 int offset = 1 <<
bits;
313 for (y = 0; y < h; y++) {
314 for (i = 0; i < ring_size; i++)
318 ((h + i - y) % ring_size) * (w + 6) + 3;
320 for (x = 0; x < w; x++) {
323 unsigned v = *((uint32_t *)(src[0] + x * 4 + stride[0] * y));
326 r = (v >> 16) & 0xFF;
329 b = *((uint16_t *)(src[0] + x * 2 + stride[0] * y));
330 g = *((uint16_t *)(src[1] + x * 2 + stride[1] * y));
331 r = *((uint16_t *)(src[2] + x * 2 + stride[2] * y));
346 sample[p][0][-1] = sample[p][1][0];
347 sample[p][1][w] = sample[p][1][w - 1];
351 encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
362 memset(state, 128,
sizeof(state));
364 for (i = 1; i < 128; i++)
365 if (quant_table[i] != quant_table[i - 1]) {
376 for (i = 0; i < 5; i++)
386 memset(state, 128,
sizeof(state));
392 for (i = 1; i < 256; i++)
435 memset(state2, 128,
sizeof(state2));
436 memset(state, 128,
sizeof(state));
439 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
453 for (i = 1; i < 256; i++)
502 int i, i2, changed, print = 0;
506 for (i = 12; i < 244; i++) {
507 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
509 #define COST(old, new) \
510 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
511 s->rc_stat[old][1] * -log2((new) / 256.0)
513 #define COST2(old, new) \
514 COST(old, new) + COST(256 - (old), 256 - (new))
518 if (sizeX < size0 && i != 128 && i2 != 128) {
520 FFSWAP(
int, stt[i], stt[i2]);
524 FFSWAP(
int, stt[256 - i], stt[256 - i2]);
528 for (j = 1; j < 256; j++) {
531 else if (stt[j] == i2)
534 if (stt[256 - j] == 256 - i)
535 stt[256 - j] = 256 - i2;
536 else if (stt[256 - j] == 256 - i2)
537 stt[256 - j] = 256 - i;
573 if (avctx->
level == 3) {
584 "Version %d requested, please set -strict experimental in "
585 "order to enable it\n",
620 "bits_per_raw_sample > 8, forcing coder 1\n");
626 "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
673 "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
677 "Invalid context model %d, valid values are 0 and 1\n",
683 for (i = 1; i < 256; i++)
686 for (i = 0; i < 256; i++) {
755 for (j = 0; j < 256; j++)
756 for (i = 0; i < 2; i++) {
757 s->
rc_stat[j][i] = strtol(p, &next, 0);
760 "2Pass file invalid at %d %d [%s]\n", j, i, p);
767 for (k = 0; k < 32; k++)
768 for (m = 0; m < 2; m++) {
769 s->
rc_stat2[i][j][k][m] = strtol(p, &next, 0);
772 "2Pass file invalid at %d %d %d %d [%s]\n",
779 gob_count = strtol(p, &next, 0);
780 if (next == p || gob_count <= 0) {
785 while (*p ==
'\n' || *p ==
' ')
796 for (k = 0; k < 32; k++) {
799 p = 256.0 * s->
rc_stat2[i][j][k][1] /
803 best_state[av_clip(
round(p), 1, 255)][av_clip((s->
rc_stat2[i][j][k][0] +
818 "Unsupported number %d of slices requested, please specify a "
819 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
831 #define STATS_OUT_SIZE 1024 * 1024 * 6
853 memset(state, 128,
sizeof(state));
900 if (f->colorspace == 0) {
901 const int chroma_width = -((-
width) >> f->chroma_h_shift);
902 const int chroma_height = -((-
height) >> f->chroma_v_shift);
903 const int cx = x >> f->chroma_h_shift;
904 const int cy = y >> f->chroma_v_shift;
906 encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
909 if (f->chroma_planes) {
910 encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
911 chroma_width, chroma_height, p->linesize[1], 1);
912 encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
913 chroma_width, chroma_height, p->linesize[2], 1);
915 if (fs->transparency)
917 height, p->linesize[3], 2);
919 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
920 p->data[1] + ps * x + y * p->linesize[1],
921 p->data[2] + ps * x + y * p->linesize[2] };
930 const AVFrame *pict,
int *got_packet)
943 ((8 * 2 + 1 + 1) * 4) / 8 +
964 for (i = 1; i < 256; i++) {
1021 for (i = 0; i < 256; i++) {
1027 for (m = 0; m < 32; m++) {
1034 for (j = 0; j < 256; j++) {
1035 snprintf(p, end - p,
"%" PRIu64
" %" PRIu64
" ",
1039 snprintf(p, end - p,
"\n");
1043 for (m = 0; m < 32; m++) {
1044 snprintf(p, end - p,
"%" PRIu64
" %" PRIu64
" ",
1049 snprintf(p, end - p,
"%d\n", f->
gob_count);
1068 #define OFFSET(x) offsetof(FFV1Context, x)
1069 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1072 { .i64 = -1 }, -1, 1,
VE },
1076 static const AVClass class = {
1112 .priv_class = &
class,
static av_always_inline int fold(int diff, int bits)
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
int ffv1_allocate_initial_states(FFV1Context *f)
const uint8_t ff_log2_run[41]
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
const int8_t ffv1_quant5[256]
static const AVCodecDefault ffv1_defaults[]
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index)
static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
av_cold int ffv1_common_init(AVCodecContext *avctx)
static av_cold int ffv1_encode_init(AVCodecContext *avctx)
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_LOG_WARNING
Something somehow does not look correct.
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
AVFrame * coded_frame
the picture in the bitstream
#define MAX_CONTEXT_INPUTS
#define AV_PIX_FMT_GBRP10
static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2])
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", 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]
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Macro definitions for various function/variable attributes.
int ff_rac_terminate(RangeCoder *c)
static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int context_model
context model
#define av_assert0(cond)
assert() equivalent, that is always enabled.
const int8_t ffv1_quant11[256]
static av_cold int ffv1_encode_close(AVCodecContext *avctx)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
const int8_t ffv1_quant5_10bit[256]
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
static int encode_slice(AVCodecContext *c, void *arg)
bitstream reader API header.
#define AV_PIX_FMT_YUV444P16
int interlaced_frame
The content of the picture is interlaced.
char * stats_out
pass1 encoding statistics output buffer
static int write_extradata(FFV1Context *f)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static int sort_stt(FFV1Context *s, uint8_t stt[256])
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256]
const int8_t ffv1_quant9_10bit[256]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
static av_always_inline av_const double round(double x)
#define AV_PIX_FMT_YUV444P10
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
int flags
A combination of AV_PKT_FLAG values.
static int put_bits_count(PutBitContext *s)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static av_always_inline int encode_line(FFV1Context *s, int w, int16_t *sample[3], int plane_index, int bits)
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
#define AV_PIX_FMT_YUV422P9
uint8_t state_transition[256]
uint8_t nb_components
The number of components each pixel has, (1-4)
enum AVPictureType pict_type
Picture type of the frame.
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GRAY16
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
int width
picture width / height.
static float quant_table[96]
const uint8_t ffv1_ver2_state[256]
static int get_context(PlaneContext *p, int16_t *src, int16_t *last, int16_t *last2)
static void update_vlc_state(VlcState *const state, const int v)
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
#define AV_PIX_FMT_YUV444P9
av_cold int ffv1_init_slice_contexts(FFV1Context *f)
static const AVOption options[]
static const float pred[4]
#define AV_PIX_FMT_YUV420P16
#define AV_LOG_INFO
Standard information.
int context_count[MAX_QUANT_TABLES]
Libavcodec external API header.
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure.
static void close(AVCodecParserContext *s)
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256])
void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
#define AV_PIX_FMT_YUV420P10
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
Describe the class of an AVClass context structure.
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
static void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (ffv1).
static av_cold int init_slices_state(FFV1Context *f)
#define AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV422P10
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
FF_ENABLE_DEPRECATION_WARNINGS int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
uint8_t(* state)[CONTEXT_SIZE]
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int init(AVCodecParserContext *s)
uint8_t * bytestream_start
int slices
Number of slices.
PlaneContext plane[MAX_PLANES]
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
int top_field_first
If the content is interlaced, is top field displayed first.
av_cold int ffv1_close(AVCodecContext *avctx)
int key_frame
1 -> keyframe, 0-> not
static void write_header(FFV1Context *f)
struct FFV1Context * slice_context[MAX_SLICES]
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
#define FFSWAP(type, a, b)
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
#define AV_PIX_FMT_YUV422P16