FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 static av_always_inline int RENAME(encode_line)(FFV1Context *s, int w,
24  TYPE *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->ac != AC_GOLOMB_RICE) {
35  if (c->bytestream_end - c->bytestream < w * 35) {
36  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
37  return AVERROR_INVALIDDATA;
38  }
39  } else {
40  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
41  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
42  return AVERROR_INVALIDDATA;
43  }
44  }
45 
46  if (s->slice_coding_mode == 1) {
47  for (x = 0; x < w; x++) {
48  int i;
49  int v = sample[0][x];
50  for (i = bits-1; i>=0; i--) {
51  uint8_t state = 128;
52  put_rac(c, &state, (v>>i) & 1);
53  }
54  }
55  return 0;
56  }
57 
58  for (x = 0; x < w; x++) {
59  int diff, context;
60 
61  context = RENAME(get_context)(p, sample[0] + x, sample[1] + x, sample[2] + x);
62  diff = sample[0][x] - RENAME(predict)(sample[0] + x, sample[1] + x);
63 
64  if (context < 0) {
65  context = -context;
66  diff = -diff;
67  }
68 
69  diff = fold(diff, bits);
70 
71  if (s->ac != AC_GOLOMB_RICE) {
72  if (s->flags & AV_CODEC_FLAG_PASS1) {
73  put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
74  s->rc_stat2[p->quant_table_index][context]);
75  } else {
76  put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
77  }
78  } else {
79  if (context == 0)
80  run_mode = 1;
81 
82  if (run_mode) {
83  if (diff) {
84  while (run_count >= 1 << ff_log2_run[run_index]) {
85  run_count -= 1 << ff_log2_run[run_index];
86  run_index++;
87  put_bits(&s->pb, 1, 1);
88  }
89 
90  put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
91  if (run_index)
92  run_index--;
93  run_count = 0;
94  run_mode = 0;
95  if (diff > 0)
96  diff--;
97  } else {
98  run_count++;
99  }
100  }
101 
102  ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
103  run_count, run_index, run_mode, x,
104  (int)put_bits_count(&s->pb));
105 
106  if (run_mode == 0)
107  put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
108  }
109  }
110  if (run_mode) {
111  while (run_count >= 1 << ff_log2_run[run_index]) {
112  run_count -= 1 << ff_log2_run[run_index];
113  run_index++;
114  put_bits(&s->pb, 1, 1);
115  }
116 
117  if (run_count)
118  put_bits(&s->pb, 1, 1);
119  }
120  s->run_index = run_index;
121 
122  return 0;
123 }
124 
125 static int RENAME(encode_rgb_frame)(FFV1Context *s, const uint8_t *src[3],
126  int w, int h, const int stride[3])
127 {
128  int x, y, p, i;
129  const int ring_size = s->context_model ? 3 : 2;
130  TYPE *sample[4][3];
131  int lbd = s->bits_per_raw_sample <= 8;
132  int packed = !src[1];
133  int bits = s->bits_per_raw_sample > 0 ? s->bits_per_raw_sample : 8;
134  int offset = 1 << bits;
135 
136  s->run_index = 0;
137 
138  memset(RENAME(s->sample_buffer), 0, ring_size * MAX_PLANES *
139  (w + 6) * sizeof(*RENAME(s->sample_buffer)));
140 
141  for (y = 0; y < h; y++) {
142  for (i = 0; i < ring_size; i++)
143  for (p = 0; p < MAX_PLANES; p++)
144  sample[p][i]= RENAME(s->sample_buffer) + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
145 
146  for (x = 0; x < w; x++) {
147  int b, g, r, av_uninit(a);
148  if (lbd) {
149  unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
150  b = v & 0xFF;
151  g = (v >> 8) & 0xFF;
152  r = (v >> 16) & 0xFF;
153  a = v >> 24;
154  } else if (packed) {
155  const uint16_t *p = ((const uint16_t*)(src[0] + x*6 + stride[0]*y));
156  r = p[0];
157  g = p[1];
158  b = p[2];
159  } else if (sizeof(TYPE) == 4) {
160  g = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
161  b = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
162  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
163  } else {
164  b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
165  g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
166  r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
167  }
168 
169  if (s->slice_coding_mode != 1) {
170  b -= g;
171  r -= g;
172  g += (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
173  b += offset;
174  r += offset;
175  }
176 
177  sample[0][0][x] = g;
178  sample[1][0][x] = b;
179  sample[2][0][x] = r;
180  sample[3][0][x] = a;
181  }
182  for (p = 0; p < 3 + s->transparency; p++) {
183  int ret;
184  sample[p][0][-1] = sample[p][1][0 ];
185  sample[p][1][ w] = sample[p][1][w-1];
186  if (lbd && s->slice_coding_mode == 0)
187  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, 9);
188  else
189  ret = RENAME(encode_line)(s, w, sample[p], (p + 1) / 2, bits + (s->slice_coding_mode != 1));
190  if (ret < 0)
191  return ret;
192  }
193  }
194  return 0;
195 }
196 
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:151
#define NULL
Definition: coverity.c:32
const uint8_t ff_log2_run[41]
Definition: bitstream.c:40
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
const char * g
Definition: vf_curves.c:112
#define RENAME(name)
Definition: ffv1.h:205
uint8_t * bytestream_end
Definition: rangecoder.h:44
const char * b
Definition: vf_curves.c:113
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:185
static int ring_size(RingBuffer *ring)
Definition: async.c:105
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:174
#define sample
uint8_t bits
Definition: crc.c:296
uint8_t
#define ff_dlog(a,...)
VlcState * vlc_state
Definition: ffv1.h:73
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const char * r
Definition: vf_curves.c:111
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
uint8_t * bytestream
Definition: rangecoder.h:43
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
int run_index
Definition: ffv1.h:109
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:849
#define MAX_PLANES
Definition: ffv1.h:50
#define src
Definition: vp9dsp.c:530
static struct @246 state
#define TYPE
Definition: ffv1.h:204
#define AC_GOLOMB_RICE
Definition: ffv1.h:56
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition: ffv1enc.c:240
int quant_table_index
Definition: ffv1.h:70
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
static double c[64]
#define put_rac(C, S, B)
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:72
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_uninit(x)
Definition: attributes.h:149
#define av_always_inline
Definition: attributes.h:39