FFmpeg
ffv1dec_template.c
Go to the documentation of this file.
1 /*
2  * FFV1 decoder 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 
25 static av_always_inline int
27  GetBitContext *gb,
28  int w, TYPE *sample[2], int plane_index, int bits,
29  int ac)
30 {
31  PlaneContext *const p = &sc->plane[plane_index];
32  RangeCoder *const c = &sc->c;
33  const int16_t (*quant_table)[256] = f->quant_tables[p->quant_table_index];
34  int x;
35  int run_count = 0;
36  int run_mode = 0;
37  int run_index = sc->run_index;
38 
39  if (is_input_end(c, gb, ac))
40  return AVERROR_INVALIDDATA;
41 
42  if (sc->slice_coding_mode == 1) {
43  int i;
44  for (x = 0; x < w; x++) {
45  int v = 0;
46  for (i=0; i<bits; i++) {
47  uint8_t state = 128;
48  v += v + get_rac(c, &state);
49  }
50  sample[1][x] = v;
51  }
52  return 0;
53  }
54 
55  for (x = 0; x < w; x++) {
56  int diff, context, sign;
57 
58  if (!(x & 1023)) {
59  if (is_input_end(c, gb, ac))
60  return AVERROR_INVALIDDATA;
61  }
62 
63  context = RENAME(get_context)(quant_table,
64  sample[1] + x, sample[0] + x, sample[1] + x);
65  if (context < 0) {
66  context = -context;
67  sign = 1;
68  } else
69  sign = 0;
70 
71  av_assert2(context < p->context_count);
72 
73  if (ac != AC_GOLOMB_RICE) {
75  } else {
76  if (context == 0 && run_mode == 0)
77  run_mode = 1;
78 
79  if (run_mode) {
80  if (run_count == 0 && run_mode == 1) {
81  if (get_bits1(gb)) {
82  run_count = 1 << ff_log2_run[run_index];
83  if (x + run_count <= w)
84  run_index++;
85  } else {
86  if (ff_log2_run[run_index])
87  run_count = get_bits(gb, ff_log2_run[run_index]);
88  else
89  run_count = 0;
90  if (run_index)
91  run_index--;
92  run_mode = 2;
93  }
94  }
95  if (sample[1][x - 1] == sample[0][x - 1]) {
96  while (run_count > 1 && w-x > 1) {
97  sample[1][x] = sample[0][x];
98  x++;
99  run_count--;
100  }
101  } else {
102  while (run_count > 1 && w-x > 1) {
103  sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
104  x++;
105  run_count--;
106  }
107  }
108  run_count--;
109  if (run_count < 0) {
110  run_mode = 0;
111  run_count = 0;
113  bits);
114  if (diff >= 0)
115  diff++;
116  } else
117  diff = 0;
118  } else
120 
121  ff_dlog(f->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
122  run_count, run_index, run_mode, x, get_bits_count(gb));
123  }
124 
125  if (sign)
126  diff = -(unsigned)diff;
127 
128  sample[1][x] = av_zero_extend(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
129  }
130  sc->run_index = run_index;
131  return 0;
132 }
133 
135  GetBitContext *gb,
136  uint8_t *src[4], int w, int h, int stride[4])
137 {
138  int x, y, p;
139  TYPE *sample[4][2];
140  int lbd = f->avctx->bits_per_raw_sample <= 8;
141  int bits = f->avctx->bits_per_raw_sample > 0 ? f->avctx->bits_per_raw_sample : 8;
142  int offset = 1 << bits;
143  int transparency = f->transparency;
144  int ac = f->ac;
145 
146  for (x = 0; x < 4; x++) {
147  sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
148  sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
149  }
150 
151  sc->run_index = 0;
152 
153  memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
154 
155  for (y = 0; y < h; y++) {
156  for (p = 0; p < 3 + transparency; p++) {
157  int ret;
158  TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
159 
160  sample[p][0] = sample[p][1];
161  sample[p][1] = temp;
162 
163  sample[p][1][-1]= sample[p][0][0 ];
164  sample[p][0][ w]= sample[p][0][w-1];
165  if (lbd && sc->slice_coding_mode == 0)
166  ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, 9, ac);
167  else
168  ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits + (sc->slice_coding_mode != 1), ac);
169  if (ret < 0)
170  return ret;
171  }
172  for (x = 0; x < w; x++) {
173  int g = sample[0][1][x];
174  int b = sample[1][1][x];
175  int r = sample[2][1][x];
176  int a = sample[3][1][x];
177 
178  if (sc->slice_coding_mode != 1) {
179  b -= offset;
180  r -= offset;
181  g -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
182  b += g;
183  r += g;
184  }
185 
186  if (lbd)
187  *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
188  else if (sizeof(TYPE) == 4 || transparency) {
189  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
190  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
191  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
192  if (transparency)
193  *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
194  } else {
195  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
196  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
197  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
198  }
199  }
200  }
201  return 0;
202 }
r
const char * r
Definition: vf_curves.c:127
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
w
uint8_t w
Definition: llviddspenc.c:38
b
#define b
Definition: input.c:41
predict
static av_always_inline void predict(PredictorState *ps, int *coef, int output_enable)
Definition: aacdec_fixed_prediction.h:77
PlaneContext::state
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:66
decode_line
static av_always_inline int RENAME() decode_line(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, int w, TYPE *sample[2], int plane_index, int bits, int ac)
Definition: ffv1dec_template.c:26
state
static struct @464 state
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
GetBitContext
Definition: get_bits.h:108
get_symbol_inline
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:44
decode_rgb_frame
static int RENAME() decode_rgb_frame(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, uint8_t *src[4], int w, int h, int stride[4])
Definition: ffv1dec_template.c:134
g
const char * g
Definition: vf_curves.c:128
bits
uint8_t bits
Definition: vp3data.h:128
is_input_end
static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
Definition: ffv1dec.c:98
ffv1_template.c
get_vlc_symbol
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition: ffv1dec.c:73
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
quant_table
static const int16_t quant_table[64]
Definition: intrax8.c:517
TYPE
#define TYPE
Definition: ffv1dec.c:116
PlaneContext::vlc_state
VlcState * vlc_state
Definition: ffv1.h:67
AC_GOLOMB_RICE
#define AC_GOLOMB_RICE
Definition: ffv1.h:51
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
PlaneContext
Definition: ffv1.h:63
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
f
f
Definition: af_crystalizer.c:122
sample
#define sample
Definition: flacdsp_template.c:44
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
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
av_zero_extend
#define av_zero_extend
Definition: common.h:151
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_always_inline
#define av_always_inline
Definition: attributes.h:49
SUINT
#define SUINT
Definition: dct32_template.c:30
FFV1SliceContext
Definition: ffv1.h:72
get_rac
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:126
stride
#define stride
Definition: h264pred_template.c:537
ret
ret
Definition: filter_design.txt:187
RENAME
#define RENAME(element)
Definition: ac3enc_template.c:44
temp
else temp
Definition: vf_mcdeint.c:263
FFV1Context
Definition: ffv1.h:109
ff_log2_run
const uint8_t ff_log2_run[41]
Definition: mathtables.c:116
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
RangeCoder
Definition: mss3.c:63
src
#define src
Definition: vp8dsp.c:248