FFmpeg
Loading...
Searching...
No Matches
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
25static 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 (bits == 0) {
40 for (x = 0; x < w; x++)
41 sample[1][x] = 0;
42 return 0;
43 }
44
45 if (is_input_end(c, gb, ac))
47
48 if (sc->slice_coding_mode == 1) {
49 int i;
50 for (x = 0; x < w; x++) {
51 int v = 0;
52 for (i=0; i<bits; i++) {
53 uint8_t state = 128;
54 v += v + get_rac(c, &state);
55 }
56 sample[1][x] = v;
57 }
58 return 0;
59 }
60
61 for (x = 0; x < w; x++) {
62 int diff, context, sign;
63
64 if (!(x & 1023)) {
65 if (is_input_end(c, gb, ac))
67 }
68
69 context = RENAME(get_context)(quant_table,
70 sample[1] + x, sample[0] + x, sample[1] + x);
71 if (context < 0) {
72 context = -context;
73 sign = 1;
74 } else
75 sign = 0;
76
77 av_assert2(context < p->context_count);
78
79 if (ac != AC_GOLOMB_RICE) {
80 diff = get_symbol_inline(c, p->state[context], 1);
81 } else {
82 if (context == 0 && run_mode == 0)
83 run_mode = 1;
84
85 if (run_mode) {
86 if (run_count == 0 && run_mode == 1) {
87 if (get_bits1(gb)) {
88 run_count = 1 << ff_log2_run[run_index];
89 if (x + run_count <= w)
90 run_index++;
91 } else {
92 if (ff_log2_run[run_index])
93 run_count = get_bits(gb, ff_log2_run[run_index]);
94 else
95 run_count = 0;
96 if (run_index)
97 run_index--;
98 run_mode = 2;
99 }
100 }
101 if (sample[1][x - 1] == sample[0][x - 1]) {
102 while (run_count > 1 && w-x > 1) {
103 sample[1][x] = sample[0][x];
104 x++;
105 run_count--;
106 }
107 } else {
108 while (run_count > 1 && w-x > 1) {
109 sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
110 x++;
111 run_count--;
112 }
113 }
114 run_count--;
115 if (run_count < 0) {
116 run_mode = 0;
117 run_count = 0;
118 diff = get_vlc_symbol(gb, &p->vlc_state[context],
119 bits);
120 if (diff >= 0)
121 diff++;
122 } else
123 diff = 0;
124 } else
125 diff = get_vlc_symbol(gb, &p->vlc_state[context], bits);
126
127 ff_dlog(f->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
128 run_count, run_index, run_mode, x, get_bits_count(gb));
129 }
130
131 if (sign)
132 diff = -(unsigned)diff;
133
134 sample[1][x] = av_zero_extend(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
135 }
136 sc->run_index = run_index;
137 return 0;
138}
139
140static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
141 GetBitContext *gb,
142 uint8_t *src[4], int w, int h, int stride[4])
143{
144 int x, y, p;
145 TYPE *sample[4][2];
146 int lbd = f->avctx->bits_per_raw_sample <= 8;
147 int bits[4], offset;
148 int transparency = f->transparency;
149 int ac = f->ac;
150 unsigned mask[4];
151
152 ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, mask, f->avctx->bits_per_raw_sample);
153
154 if (sc->remap)
155 for (int p=0; p<3+f->transparency; p++) {
156 if (f->avctx->bits_per_raw_sample == 32) {
157 av_assert0(sc->fltmap32_size[p] >= (mask[p] + 1LL) * sizeof(*sc->fltmap32[p]));
158 } else
159 av_assert0(sc->fltmap_size[p] >= (mask[p] + 1LL) * sizeof(*sc->fltmap[p]));
160 }
161
162 if (sc->slice_coding_mode == 1)
163 ac = 1;
164
165 for (x = 0; x < 4; x++) {
166 sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
167 sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
168 }
169
170 sc->run_index = 0;
171
172 memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
173
174 for (y = 0; y < h; y++) {
175 for (p = 0; p < 3 + transparency; p++) {
176 int ret;
177 TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
178
179 sample[p][0] = sample[p][1];
180 sample[p][1] = temp;
181
182 sample[p][1][-1]= sample[p][0][0 ];
183 sample[p][0][ w]= sample[p][0][w-1];
184 if (bits[p] == 9)
185 ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, 9, ac);
186 else
187 ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits[p], ac);
188 if (ret < 0)
189 return ret;
190 }
191 for (x = 0; x < w; x++) {
192 int g = sample[0][1][x];
193 int b = sample[1][1][x];
194 int r = sample[2][1][x];
195 int a = sample[3][1][x];
196
197 if (sc->slice_coding_mode != 1) {
198 b -= offset;
199 r -= offset;
200 g -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
201 b += g;
202 r += g;
203 }
204 if (sc->remap) {
205 if (f->avctx->bits_per_raw_sample == 32) {
206 g = sc->fltmap32[0][g & mask[0]];
207 b = sc->fltmap32[1][b & mask[1]];
208 r = sc->fltmap32[2][r & mask[2]];
209 if (transparency)
210 a = sc->fltmap32[3][a & mask[3]];
211 } else {
212 g = sc->fltmap[0][g & mask[0]];
213 b = sc->fltmap[1][b & mask[1]];
214 r = sc->fltmap[2][r & mask[2]];
215 if (transparency)
216 a = sc->fltmap[3][a & mask[3]];
217 }
218 }
219
220 if (lbd) {
221 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
222 } else if (f->avctx->bits_per_raw_sample == 32) {
223 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = g;
224 *((uint32_t*)(src[1] + x*4 + stride[1]*y)) = b;
225 *((uint32_t*)(src[2] + x*4 + stride[2]*y)) = r;
226 if (transparency)
227 *((uint32_t*)(src[3] + x*4 + stride[3]*y)) = a;
228 } else if (sizeof(TYPE) == 4 || transparency) {
229 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
230 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
231 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
232 if (transparency)
233 *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
234 } else {
235 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
236 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
237 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
238 }
239 }
240 }
241 return 0;
242}
static av_always_inline void predict(PredictorState *ps, int *coef, int output_enable)
#define RENAME(element)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define av_zero_extend
Definition common.h:151
#define SUINT
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
Definition fastaudio.c:100
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
Definition ffv1.c:224
#define AC_GOLOMB_RICE
Definition ffv1.h:52
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition ffv1.h:256
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition ffv1dec.c:48
static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
Definition ffv1dec.c:78
#define TYPE
Definition ffv1dec.c:90
#define sample
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
static const int16_t quant_table[64]
Definition intrax8.c:511
unsigned offset
Definition libaomenc.c:763
#define av_always_inline
Definition attributes.h:72
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
const uint8_t ff_log2_run[41]
Definition mathtables.c:155
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition rangecoder.h:118
#define stride
#define ff_dlog(a,...)
#define src
Definition vp8dsp.c:248
const char * g
Definition vf_curves.c:128
else temp
Definition vf_mcdeint.c:275
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]