FFmpeg
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 
23 #include "ffv1_template.c"
24 
26  TYPE *sample[3],
27  int plane_index, int bits)
28 {
29  PlaneContext *const p = &s->plane[plane_index];
30  RangeCoder *const c = &s->c;
31  int x;
32  int run_index = s->run_index;
33  int run_count = 0;
34  int run_mode = 0;
35 
36  if (s->ac != AC_GOLOMB_RICE) {
37  if (c->bytestream_end - c->bytestream < w * 35) {
38  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
39  return AVERROR_INVALIDDATA;
40  }
41  } else {
42  if (put_bytes_left(&s->pb, 0) < w * 4) {
43  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
44  return AVERROR_INVALIDDATA;
45  }
46  }
47 
48  if (s->slice_coding_mode == 1) {
49  for (x = 0; x < w; x++) {
50  int i;
51  int v = sample[0][x];
52  for (i = bits-1; i>=0; i--) {
53  uint8_t state = 128;
54  put_rac(c, &state, (v>>i) & 1);
55  }
56  }
57  return 0;
58  }
59 
60  for (x = 0; x < w; x++) {
61  int diff, context;
62 
63  context = RENAME(get_context)(p, sample[0] + x, sample[1] + x, sample[2] + x);
64  diff = sample[0][x] - RENAME(predict)(sample[0] + x, sample[1] + x);
65 
66  if (context < 0) {
67  context = -context;
68  diff = -diff;
69  }
70 
71  diff = fold(diff, bits);
72 
73  if (s->ac != AC_GOLOMB_RICE) {
74  if (s->flags & AV_CODEC_FLAG_PASS1) {
75  put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
76  s->rc_stat2[p->quant_table_index][context]);
77  } else {
79  }
80  } else {
81  if (context == 0)
82  run_mode = 1;
83 
84  if (run_mode) {
85  if (diff) {
86  while (run_count >= 1 << ff_log2_run[run_index]) {
87  run_count -= 1 << ff_log2_run[run_index];
88  run_index++;
89  put_bits(&s->pb, 1, 1);
90  }
91 
92  put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
93  if (run_index)
94  run_index--;
95  run_count = 0;
96  run_mode = 0;
97  if (diff > 0)
98  diff--;
99  } else {
100  run_count++;
101  }
102  }
103 
104  ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
105  run_count, run_index, run_mode, x,
106  (int)put_bits_count(&s->pb));
107 
108  if (run_mode == 0)
109  put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
110  }
111  }
112  if (run_mode) {
113  while (run_count >= 1 << ff_log2_run[run_index]) {
114  run_count -= 1 << ff_log2_run[run_index];
115  run_index++;
116  put_bits(&s->pb, 1, 1);
117  }
118 
119  if (run_count)
120  put_bits(&s->pb, 1, 1);
121  }
122  s->run_index = run_index;
123 
124  return 0;
125 }
126 
127 static int RENAME(encode_rgb_frame)(FFV1Context *s, const uint8_t *src[4],
128  int w, int h, const int stride[4])
129 {
130  int x, y, p, i;
131  const int ring_size = s->context_model ? 3 : 2;
132  TYPE *sample[4][3];
133  int lbd = s->bits_per_raw_sample <= 8;
134  int packed = !src[1];
135  int bits = s->bits_per_raw_sample > 0 ? s->bits_per_raw_sample : 8;
136  int offset = 1 << bits;
137  int transparency = s->transparency;
138  int packed_size = (3 + transparency)*2;
139 
140  s->run_index = 0;
141 
142  memset(RENAME(s->sample_buffer), 0, ring_size * MAX_PLANES *
143  (w + 6) * sizeof(*RENAME(s->sample_buffer)));
144 
145  for (y = 0; y < h; y++) {
146  for (i = 0; i < ring_size; i++)
147  for (p = 0; p < MAX_PLANES; p++)
148  sample[p][i]= RENAME(s->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
149 
150  for (x = 0; x < w; x++) {
151  int b, g, r, av_uninit(a);
152  if (lbd) {
153  unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
154  b = v & 0xFF;
155  g = (v >> 8) & 0xFF;
156  r = (v >> 16) & 0xFF;
157  a = v >> 24;
158  } else if (packed) {
159  const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
160  r = p[0];
161  g = p[1];
162  b = p[2];
163  if (transparency)
164  a = p[3];
165  } else if (sizeof(TYPE) == 4 || transparency) {
166  g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
167  b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
168  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
169  if (transparency)
170  a = *((const uint16_t *)(src[3] + x*2 + stride[3]*y));
171  } else {
172  b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
173  g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
174  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
175  }
176 
177  if (s->slice_coding_mode != 1) {
178  b -= g;
179  r -= g;
180  g += (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
181  b += offset;
182  r += offset;
183  }
184 
185  sample[0][0][x] = g;
186  sample[1][0][x] = b;
187  sample[2][0][x] = r;
188  sample[3][0][x] = a;
189  }
190  for (p = 0; p < 3 + transparency; p++) {
191  int ret;
192  sample[p][0][-1] = sample[p][1][0 ];
193  sample[p][1][ w] = sample[p][1][w-1];
194  if (lbd && s->slice_coding_mode == 0)
195  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, 9);
196  else
197  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, bits + (s->slice_coding_mode != 1));
198  if (ret < 0)
199  return ret;
200  }
201  }
202  return 0;
203 }
204 
r
const char * r
Definition: vf_curves.c:126
put_symbol_inline
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
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:221
w
uint8_t w
Definition: llviddspenc.c:38
b
#define b
Definition: input.c:41
PlaneContext::state
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:66
ring_size
static int ring_size(RingBuffer *ring)
Definition: async.c:105
put_bytes_left
static int put_bytes_left(const PutBitContext *s, int round_up)
Definition: put_bits.h:135
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:256
MAX_PLANES
#define MAX_PLANES
Definition: ffv1.h:44
g
const char * g
Definition: vf_curves.c:127
bits
uint8_t bits
Definition: vp3data.h:128
ffv1_template.c
fold
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:146
state
static struct @344 state
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
TYPE
#define TYPE
Definition: ffv1dec.c:116
NULL
#define NULL
Definition: coverity.c:32
PlaneContext::vlc_state
VlcState * vlc_state
Definition: ffv1.h:67
AC_GOLOMB_RICE
#define AC_GOLOMB_RICE
Definition: ffv1.h:50
PlaneContext
Definition: ffv1.h:62
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
sample
#define sample
Definition: flacdsp_template.c:44
encode_line
static av_always_inline int RENAME() encode_line(FFV1Context *s, int w, TYPE *sample[3], int plane_index, int bits)
Definition: ffv1enc_template.c:25
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
PlaneContext::quant_table_index
int quant_table_index
Definition: ffv1.h:64
put_vlc_symbol
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition: ffv1enc.c:239
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:80
predict
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:176
av_always_inline
#define av_always_inline
Definition: attributes.h:49
stride
#define stride
Definition: h264pred_template.c:537
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
ret
ret
Definition: filter_design.txt:187
put_rac
#define put_rac(C, S, B)
FFV1Context
Definition: ffv1.h:73
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:139
FFV1Context::transparency
int transparency
Definition: ffv1.h:86
encode_rgb_frame
static int RENAME() encode_rgb_frame(FFV1Context *s, const uint8_t *src[4], int w, int h, const int stride[4])
Definition: ffv1enc_template.c:127
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ff_log2_run
const uint8_t ff_log2_run[41]
Definition: mathtables.c:116
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
RangeCoder
Definition: mss3.c:62
FFV1Context::run_index
int run_index
Definition: ffv1.h:104
RENAME
#define RENAME(name)
Definition: ffv1dec.c:117
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:247