summaryrefslogtreecommitdiff
path: root/libavcodec/ffv1dec_template.c (plain)
blob: 21af155bba2e06d439565df7a09041a17e2aec62
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
23static av_always_inline void RENAME(decode_line)(FFV1Context *s, int w,
24 TYPE *sample[2],
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_count = 0;
31 int run_mode = 0;
32 int run_index = s->run_index;
33
34 if (s->slice_coding_mode == 1) {
35 int i;
36 for (x = 0; x < w; x++) {
37 int v = 0;
38 for (i=0; i<bits; i++) {
39 uint8_t state = 128;
40 v += v + get_rac(c, &state);
41 }
42 sample[1][x] = v;
43 }
44 return;
45 }
46
47 for (x = 0; x < w; x++) {
48 int diff, context, sign;
49
50 context = RENAME(get_context)(p, sample[1] + x, sample[0] + x, sample[1] + x);
51 if (context < 0) {
52 context = -context;
53 sign = 1;
54 } else
55 sign = 0;
56
57 av_assert2(context < p->context_count);
58
59 if (s->ac != AC_GOLOMB_RICE) {
60 diff = get_symbol_inline(c, p->state[context], 1);
61 } else {
62 if (context == 0 && run_mode == 0)
63 run_mode = 1;
64
65 if (run_mode) {
66 if (run_count == 0 && run_mode == 1) {
67 if (get_bits1(&s->gb)) {
68 run_count = 1 << ff_log2_run[run_index];
69 if (x + run_count <= w)
70 run_index++;
71 } else {
72 if (ff_log2_run[run_index])
73 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
74 else
75 run_count = 0;
76 if (run_index)
77 run_index--;
78 run_mode = 2;
79 }
80 }
81 run_count--;
82 if (run_count < 0) {
83 run_mode = 0;
84 run_count = 0;
85 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
86 bits);
87 if (diff >= 0)
88 diff++;
89 } else
90 diff = 0;
91 } else
92 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
93
94 ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
95 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
96 }
97
98 if (sign)
99 diff = -diff;
100
101 sample[1][x] = av_mod_uintp2(RENAME(predict)(sample[1] + x, sample[0] + x) + diff, bits);
102 }
103 s->run_index = run_index;
104}
105
106static void RENAME(decode_rgb_frame)(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
107{
108 int x, y, p;
109 TYPE *sample[4][2];
110 int lbd = s->avctx->bits_per_raw_sample <= 8;
111 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
112 int offset = 1 << bits;
113
114 for (x = 0; x < 4; x++) {
115 sample[x][0] = RENAME(s->sample_buffer) + x * 2 * (w + 6) + 3;
116 sample[x][1] = RENAME(s->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
117 }
118
119 s->run_index = 0;
120
121 memset(RENAME(s->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(s->sample_buffer)));
122
123 for (y = 0; y < h; y++) {
124 for (p = 0; p < 3 + s->transparency; p++) {
125 TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
126
127 sample[p][0] = sample[p][1];
128 sample[p][1] = temp;
129
130 sample[p][1][-1]= sample[p][0][0 ];
131 sample[p][0][ w]= sample[p][0][w-1];
132 if (lbd && s->slice_coding_mode == 0)
133 RENAME(decode_line)(s, w, sample[p], (p + 1)/2, 9);
134 else
135 RENAME(decode_line)(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
136 }
137 for (x = 0; x < w; x++) {
138 int g = sample[0][1][x];
139 int b = sample[1][1][x];
140 int r = sample[2][1][x];
141 int a = sample[3][1][x];
142
143 if (s->slice_coding_mode != 1) {
144 b -= offset;
145 r -= offset;
146 g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
147 b += g;
148 r += g;
149 }
150
151 if (lbd)
152 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
153 else if (sizeof(TYPE) == 4) {
154 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
155 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
156 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
157 } else {
158 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
159 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
160 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
161 }
162 }
163 }
164}
165