FFmpeg  4.4.9
ffv1enc_template.c
Go to the documentation of this file.
1 /*
2  * FFV1 encoder template
3  *
4  * Copyright (c) 2003-2016 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 
24  TYPE *const sample[3],
25  int plane_index, int bits)
26 {
27  PlaneContext *const p = &s->plane[plane_index];
28  RangeCoder *const c = &s->c;
29  int x;
30  int run_index = s->run_index;
31  int run_count = 0;
32  int run_mode = 0;
33 
34  if (s->slice_coding_mode == 1) {
35  av_assert0(s->ac != AC_GOLOMB_RICE);
36  if (c->bytestream_end - c->bytestream < (w * bits + 7LL)>>3) {
37  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
38  return AVERROR_INVALIDDATA;
39  }
40 
41  for (x = 0; x < w; x++) {
42  int i;
43  int v = sample[0][x];
44  for (i = bits-1; i>=0; i--) {
45  uint8_t state = 128;
46  put_rac(c, &state, (v>>i) & 1);
47  }
48  }
49  return 0;
50  }
51 
52  if (s->ac != AC_GOLOMB_RICE) {
53  if (c->bytestream_end - c->bytestream < w * 35) {
54  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
55  return AVERROR_INVALIDDATA;
56  }
57  } else {
58  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
59  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
60  return AVERROR_INVALIDDATA;
61  }
62  }
63 
64  for (x = 0; x < w; x++) {
65  int diff, context;
66 
67  context = RENAME(get_context)(p, sample[0] + x, sample[1] + x, sample[2] + x);
68  diff = sample[0][x] - RENAME(predict)(sample[0] + x, sample[1] + x);
69 
70  if (context < 0) {
71  context = -context;
72  diff = -diff;
73  }
74 
75  diff = fold(diff, bits);
76 
77  if (s->ac != AC_GOLOMB_RICE) {
78  if (s->flags & AV_CODEC_FLAG_PASS1) {
79  put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
80  s->rc_stat2[p->quant_table_index][context]);
81  } else {
82  put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
83  }
84  } else {
85  if (context == 0)
86  run_mode = 1;
87 
88  if (run_mode) {
89  if (diff) {
90  while (run_count >= 1 << ff_log2_run[run_index]) {
91  run_count -= 1 << ff_log2_run[run_index];
92  run_index++;
93  put_bits(&s->pb, 1, 1);
94  }
95 
96  put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
97  if (run_index)
98  run_index--;
99  run_count = 0;
100  run_mode = 0;
101  if (diff > 0)
102  diff--;
103  } else {
104  run_count++;
105  }
106  }
107 
108  ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
109  run_count, run_index, run_mode, x,
110  (int)put_bits_count(&s->pb));
111 
112  if (run_mode == 0)
113  put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
114  }
115  }
116  if (run_mode) {
117  while (run_count >= 1 << ff_log2_run[run_index]) {
118  run_count -= 1 << ff_log2_run[run_index];
119  run_index++;
120  put_bits(&s->pb, 1, 1);
121  }
122 
123  if (run_count)
124  put_bits(&s->pb, 1, 1);
125  }
126  s->run_index = run_index;
127 
128  return 0;
129 }
130 
131 static int RENAME(encode_rgb_frame)(FFV1Context *s, const uint8_t *src[4],
132  int w, int h, const int stride[4])
133 {
134  int x, y, p, i;
135  const int ring_size = s->context_model ? 3 : 2;
136  TYPE *sample[4][3];
137  int lbd = s->bits_per_raw_sample <= 8;
138  int packed = !src[1];
139  int bits = s->bits_per_raw_sample > 0 ? s->bits_per_raw_sample : 8;
140  int offset = 1 << bits;
141  int transparency = s->transparency;
142  int packed_size = (3 + transparency)*2;
143 
144  s->run_index = 0;
145 
146  for (int p = 0; p < MAX_PLANES; ++p)
147  sample[p][2] = RENAME(s->sample_buffer);
148 
149  memset(RENAME(s->sample_buffer), 0, ring_size * MAX_PLANES *
150  (w + 6) * sizeof(*RENAME(s->sample_buffer)));
151 
152  for (y = 0; y < h; y++) {
153  for (i = 0; i < ring_size; i++)
154  for (p = 0; p < MAX_PLANES; p++)
155  sample[p][i]= RENAME(s->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
156 
157  for (x = 0; x < w; x++) {
158  int b, g, r, av_uninit(a);
159  if (lbd) {
160  unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
161  b = v & 0xFF;
162  g = (v >> 8) & 0xFF;
163  r = (v >> 16) & 0xFF;
164  a = v >> 24;
165  } else if (packed) {
166  const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
167  r = p[0];
168  g = p[1];
169  b = p[2];
170  if (transparency)
171  a = p[3];
172  } else if (sizeof(TYPE) == 4 || transparency) {
173  g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
174  b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
175  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
176  if (transparency)
177  a = *((const uint16_t *)(src[3] + x*2 + stride[3]*y));
178  } else {
179  b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
180  g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
181  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
182  }
183 
184  if (s->slice_coding_mode != 1) {
185  b -= g;
186  r -= g;
187  g += (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
188  b += offset;
189  r += offset;
190  }
191 
192  sample[0][0][x] = g;
193  sample[1][0][x] = b;
194  sample[2][0][x] = r;
195  sample[3][0][x] = a;
196  }
197  for (p = 0; p < 3 + transparency; p++) {
198  int ret;
199  sample[p][0][-1] = sample[p][1][0 ];
200  sample[p][1][ w] = sample[p][1][w-1];
201  if (lbd && s->slice_coding_mode == 0)
202  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, 9);
203  else
204  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, bits + (s->slice_coding_mode != 1));
205  if (ret < 0)
206  return ret;
207  }
208  }
209  return 0;
210 }
static int ring_size(RingBuffer *ring)
Definition: async.c:105
#define av_always_inline
Definition: attributes.h:45
#define av_uninit(x)
Definition: attributes.h:154
uint8_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const uint8_t ff_log2_run[41]
Definition: bitstream.c:39
#define s(width, name)
Definition: cbs_vp9.c:257
static struct @322 state
#define NULL
Definition: coverity.c:32
#define MAX_PLANES
Definition: ffv1.h:49
#define TYPE
Definition: ffv1.h:196
#define AC_GOLOMB_RICE
Definition: ffv1.h:55
#define RENAME(name)
Definition: ffv1.h:197
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:151
static int RENAME() get_context(PlaneContext *p, TYPE *src, TYPE *last, TYPE *last2)
Definition: ffv1_template.c:32
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition: ffv1enc.c:239
#define put_rac(C, S, B)
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])
Definition: ffv1enc.c:184
static int RENAME() encode_rgb_frame(FFV1Context *s, const uint8_t *src[4], int w, int h, const int stride[4])
static av_always_inline int RENAME() encode_line(FFV1Context *s, int w, TYPE *const sample[3], int plane_index, int bits)
#define sample
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:296
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:179
uint8_t w
Definition: llviddspenc.c:39
int stride
Definition: mace.c:144
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
VlcState * vlc_state
Definition: ffv1.h:72
int quant_table_index
Definition: ffv1.h:69
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:71
#define ff_dlog(a,...)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
uint8_t bits
Definition: vp3data.h:141
static double c[64]