2 * FFV1 decoder template
4 * Copyright (c) 2003-2016 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
23 static av_always_inline int RENAME(decode_line)(FFV1Context *s, int w,
25 int plane_index, int bits)
27 PlaneContext *const p = &s->plane[plane_index];
28 RangeCoder *const c = &s->c;
32 int run_index = s->run_index;
35 return AVERROR_INVALIDDATA;
37 if (s->slice_coding_mode == 1) {
39 for (x = 0; x < w; x++) {
41 for (i=0; i<bits; i++) {
43 v += v + get_rac(c, &state);
50 for (x = 0; x < w; x++) {
51 int diff, context, sign;
55 return AVERROR_INVALIDDATA;
58 context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x);
65 av_assert2(context < p->context_count);
67 if (s->ac != AC_GOLOMB_RICE) {
68 diff = get_symbol_inline(c, p->state[context], 1);
70 if (context == 0 && run_mode == 0)
74 if (run_count == 0 && run_mode == 1) {
75 if (get_bits1(&s->gb)) {
76 run_count = 1 << ff_log2_run[run_index];
77 if (x + run_count <= w)
80 if (ff_log2_run[run_index])
81 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
89 if (sample[1][x - 1] == sample[0][x - 1]) {
90 while (run_count > 1 && w-x > 1) {
91 sample[1][x] = sample[0][x];
96 while (run_count > 1 && w-x > 1) {
97 sample[1][x] = RENAME(predict)(sample[1] + x, sample[0] + x);
106 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
113 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
115 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
116 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
120 diff = -(unsigned)diff;
122 sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
124 s->run_index = run_index;
128 static int RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[4], int w, int h, int stride[4])
132 int lbd = s->avctx->bits_per_raw_sample <= 8;
133 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
134 int offset = 1 << bits;
135 int transparency = s->transparency;
137 for (x = 0; x < 4; x++) {
138 sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
139 sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
144 memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
146 for (y = 0; y < h; y++) {
147 for (p = 0; p < 3 + transparency; p++) {
149 TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
151 sample[p][0] = sample[p][1];
154 sample[p][1][-1]= sample[p][0][0 ];
155 sample[p][0][ w]= sample[p][0][w-1];
156 if (lbd && s->slice_coding_mode == 0)
157 ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
159 ret = RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
163 for (x = 0; x < w; x++) {
164 int g = sample[0][1][x];
165 int b = sample[1][1][x];
166 int r = sample[2][1][x];
167 int a = sample[3][1][x];
169 if (s->slice_coding_mode != 1) {
172 g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
178 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
179 else if (sizeof(TYPE) == 4 || transparency) {
180 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
181 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
182 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
184 *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
186 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
187 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
188 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;