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