avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / ffv1dec_template.c
blobe983d1ba64819e9107679a4241eab226f320c019
1 /*
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 #include "ffv1_template.c"
25 static av_always_inline int
26 RENAME(decode_line)(FFV1Context *f, FFV1SliceContext *sc,
27 GetBitContext *gb,
28 int w, TYPE *sample[2], int plane_index, int bits,
29 int ac)
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;
39 if (is_input_end(c, gb, ac))
40 return AVERROR_INVALIDDATA;
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);
50 sample[1][x] = v;
52 return 0;
55 for (x = 0; x < w; x++) {
56 int diff, context, sign;
58 if (!(x & 1023)) {
59 if (is_input_end(c, gb, ac))
60 return AVERROR_INVALIDDATA;
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;
71 av_assert2(context < p->context_count);
73 if (ac != AC_GOLOMB_RICE) {
74 diff = get_symbol_inline(c, p->state[context], 1);
75 } else {
76 if (context == 0 && run_mode == 0)
77 run_mode = 1;
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;
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--;
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--;
108 run_count--;
109 if (run_count < 0) {
110 run_mode = 0;
111 run_count = 0;
112 diff = get_vlc_symbol(gb, &p->vlc_state[context],
113 bits);
114 if (diff >= 0)
115 diff++;
116 } else
117 diff = 0;
118 } else
119 diff = get_vlc_symbol(gb, &p->vlc_state[context], bits);
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));
125 if (sign)
126 diff = -(unsigned)diff;
128 sample[1][x] = av_zero_extend(RENAME(predict)(sample[1] + x, sample[0] + x) + (SUINT)diff, bits);
130 sc->run_index = run_index;
131 return 0;
134 static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
135 GetBitContext *gb,
136 uint8_t *src[4], int w, int h, int stride[4])
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;
146 if (sc->slice_coding_mode == 1)
147 ac = 1;
149 for (x = 0; x < 4; x++) {
150 sample[x][0] = RENAME(sc->sample_buffer) + x * 2 * (w + 6) + 3;
151 sample[x][1] = RENAME(sc->sample_buffer) + (x * 2 + 1) * (w + 6) + 3;
154 sc->run_index = 0;
156 memset(RENAME(sc->sample_buffer), 0, 8 * (w + 6) * sizeof(*RENAME(sc->sample_buffer)));
158 for (y = 0; y < h; y++) {
159 for (p = 0; p < 3 + transparency; p++) {
160 int ret;
161 TYPE *temp = sample[p][0]; // FIXME: try a normal buffer
163 sample[p][0] = sample[p][1];
164 sample[p][1] = temp;
166 sample[p][1][-1]= sample[p][0][0 ];
167 sample[p][0][ w]= sample[p][0][w-1];
168 if (lbd && sc->slice_coding_mode == 0)
169 ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, 9, ac);
170 else
171 ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits + (sc->slice_coding_mode != 1), ac);
172 if (ret < 0)
173 return ret;
175 for (x = 0; x < w; x++) {
176 int g = sample[0][1][x];
177 int b = sample[1][1][x];
178 int r = sample[2][1][x];
179 int a = sample[3][1][x];
181 if (sc->slice_coding_mode != 1) {
182 b -= offset;
183 r -= offset;
184 g -= (b * sc->slice_rct_by_coef + r * sc->slice_rct_ry_coef) >> 2;
185 b += g;
186 r += g;
189 if (lbd)
190 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + ((unsigned)g<<8) + ((unsigned)r<<16) + ((unsigned)a<<24);
191 else if (sizeof(TYPE) == 4 || transparency) {
192 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = g;
193 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = b;
194 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
195 if (transparency)
196 *((uint16_t*)(src[3] + x*2 + stride[3]*y)) = a;
197 } else {
198 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
199 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
200 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
204 return 0;