Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/snowdec.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/emms.h"
22
#include "libavutil/intmath.h"
23
#include "libavutil/log.h"
24
#include "libavutil/mem.h"
25
#include "avcodec.h"
26
#include "codec_internal.h"
27
#include "decode.h"
28
#include "snow_dwt.h"
29
#include "snow.h"
30
31
#include "rangecoder.h"
32
#include "mathops.h"
33
34
static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
35
5.33M
{
36
5.33M
    if (get_rac(c, state + 0))
37
2.41M
        return 0;
38
2.92M
    else {
39
2.92M
        int e;
40
2.92M
        unsigned a;
41
2.92M
        e = 0;
42
4.97M
        while (get_rac(c, state + 1 + FFMIN(e, 9))) { //1..10
43
2.05M
            e++;
44
2.05M
            if (e > 31)
45
5.67k
                return AVERROR_INVALIDDATA;
46
2.05M
        }
47
48
2.91M
        a = 1;
49
4.78M
        for (int i = e - 1; i >= 0; i--)
50
1.87M
            a += a + get_rac(c, state + 22 + FFMIN(i, 9)); //22..31
51
52
2.91M
        e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); //11..21
53
2.91M
        return (a ^ e) - e;
54
2.92M
    }
55
5.33M
}
56
57
static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2)
58
15.9M
{
59
15.9M
    int r = log2 >= 0 ? 1 << log2 : 1;
60
15.9M
    int v = 0;
61
62
15.9M
    av_assert2(log2 >= -4);
63
64
24.8M
    while (log2 < 28 && get_rac(c, state + 4 + log2)) {
65
8.91M
        v += r;
66
8.91M
        log2++;
67
8.91M
        if (log2 > 0) r += r;
68
8.91M
    }
69
70
32.4M
    for (int i = log2 - 1; i >= 0; i--)
71
16.5M
        v += get_rac(c, state + 31 - i) << i;
72
73
15.9M
    return v;
74
15.9M
}
75
76
static void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation)
77
376k
{
78
376k
    const int w = b->width;
79
376k
    const int h = b->height;
80
81
376k
    int run, runs;
82
376k
    x_and_coeff *xc = b->x_coeff;
83
376k
    x_and_coeff *prev_xc = NULL;
84
376k
    x_and_coeff *prev2_xc = xc;
85
376k
    x_and_coeff *parent_xc = parent ? parent->x_coeff : NULL;
86
376k
    x_and_coeff *prev_parent_xc = parent_xc;
87
88
376k
    runs = get_symbol2(&s->c, b->state[30], 0);
89
376k
    if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3);
90
276k
    else            run = INT_MAX;
91
92
108M
    for (int y = 0; y < h; y++) {
93
108M
        int v = 0;
94
108M
        int lt = 0, t = 0, rt = 0;
95
96
108M
        if (y && prev_xc->x == 0)
97
528k
            rt = prev_xc->coeff;
98
99
252M
        for (int x = 0; x < w; x++) {
100
143M
            int p = 0;
101
143M
            const int l = v;
102
103
143M
            lt= t; t= rt;
104
105
143M
            if (y) {
106
142M
                if (prev_xc->x <= x)
107
11.3M
                    prev_xc++;
108
142M
                if (prev_xc->x == x + 1)
109
10.7M
                    rt = prev_xc->coeff;
110
131M
                else
111
131M
                    rt = 0;
112
142M
            }
113
143M
            if (parent_xc) {
114
69.2M
                if (x>>1 > parent_xc->x)
115
3.00M
                    parent_xc++;
116
69.2M
                if (x>>1 == parent_xc->x)
117
6.24M
                    p = parent_xc->coeff;
118
69.2M
            }
119
143M
            if (/*ll|*/l|lt|t|rt|p) {
120
30.5M
                int context = av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
121
122
30.5M
                v = get_rac(&s->c, &b->state[0][context]);
123
30.5M
                if (v) {
124
8.16M
                    v  = 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
125
8.16M
                    v += get_rac(&s->c, &b->state[0][16 + 1 + 3 + ff_quant3bA[l&0xFF] + 3 * ff_quant3bA[t&0xFF]]);
126
8.16M
                    if ((uint16_t)v != v) {
127
7.81k
                        av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
128
7.81k
                        v = 1;
129
7.81k
                    }
130
8.16M
                    xc->x = x;
131
8.16M
                    (xc++)->coeff = v;
132
8.16M
                }
133
113M
            } else {
134
113M
                if (!run) {
135
3.70M
                    if (runs-- > 0) run = get_symbol2(&s->c, b->state[1], 3);
136
67.2k
                    else            run = INT_MAX;
137
3.70M
                    v  = 2 * (get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
138
3.70M
                    v += get_rac(&s->c, &b->state[0][16 + 1 + 3]);
139
3.70M
                    if ((uint16_t)v != v) {
140
610
                        av_log(s->avctx, AV_LOG_ERROR, "Coefficient damaged\n");
141
610
                        v = 1;
142
610
                    }
143
144
3.70M
                    xc->x = x;
145
3.70M
                    (xc++)->coeff = v;
146
109M
                } else {
147
109M
                    int max_run;
148
109M
                    run--;
149
109M
                    v = 0;
150
109M
                    av_assert2(run >= 0);
151
109M
                    if (y) max_run = FFMIN(run, prev_xc->x - x - 2);
152
500k
                    else   max_run = FFMIN(run, w-x-1);
153
109M
                    if (parent_xc)
154
58.1M
                        max_run = FFMIN(max_run, 2*parent_xc->x - x - 1);
155
109M
                    av_assert2(max_run >= 0 && max_run <= run);
156
157
109M
                    x   += max_run;
158
109M
                    run -= max_run;
159
109M
                }
160
113M
            }
161
143M
        }
162
108M
        (xc++)->x = w+1; //end marker
163
108M
        prev_xc  = prev2_xc;
164
108M
        prev2_xc = xc;
165
166
108M
        if (parent_xc) {
167
56.9M
            if (y & 1) {
168
28.4M
                while (parent_xc->x != parent->width+1)
169
67.3k
                    parent_xc++;
170
28.4M
                parent_xc++;
171
28.4M
                prev_parent_xc= parent_xc;
172
28.4M
            } else {
173
28.4M
                parent_xc= prev_parent_xc;
174
28.4M
            }
175
56.9M
        }
176
108M
    }
177
178
376k
    (xc++)->x = w + 1; //end marker
179
376k
}
180
181
4.27M
static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
182
4.27M
    Plane *p= &s->plane[plane_index];
183
4.27M
    const int mb_w= s->b_width  << s->block_max_depth;
184
4.27M
    const int mb_h= s->b_height << s->block_max_depth;
185
4.27M
    int x, y, mb_x;
186
4.27M
    int block_size = MB_SIZE >> s->block_max_depth;
187
4.27M
    int block_w    = plane_index ? block_size>>s->chroma_h_shift : block_size;
188
4.27M
    int block_h    = plane_index ? block_size>>s->chroma_v_shift : block_size;
189
4.27M
    const uint8_t *obmc  = plane_index ? ff_obmc_tab[s->block_max_depth+s->chroma_h_shift] : ff_obmc_tab[s->block_max_depth];
190
4.27M
    int obmc_stride= plane_index ? (2*block_size)>>s->chroma_h_shift : 2*block_size;
191
4.27M
    int ref_stride= s->current_picture->linesize[plane_index];
192
4.27M
    uint8_t *dst8= s->current_picture->data[plane_index];
193
4.27M
    int w= p->width;
194
4.27M
    int h= p->height;
195
196
4.27M
    if(s->keyframe || (s->avctx->debug&512)){
197
3.89M
        if(mb_y==mb_h)
198
9.88k
            return;
199
200
3.88M
        if(add){
201
44.5M
            for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
202
//                DWTELEM * line = slice_buffer_get_line(sb, y);
203
40.7M
                IDWTELEM * line = sb->line[y];
204
9.01G
                for(x=0; x<w; x++){
205
//                    int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
206
8.97G
                    int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
207
8.97G
                    v >>= FRAC_BITS;
208
8.97G
                    if(v&(~255)) v= ~(v>>31);
209
8.97G
                    dst8[x + y*ref_stride]= v;
210
8.97G
                }
211
40.7M
            }
212
3.88M
        }else{
213
0
            for(y=block_h*mb_y; y<FFMIN(h,block_h*(mb_y+1)); y++){
214
//                DWTELEM * line = slice_buffer_get_line(sb, y);
215
0
                IDWTELEM * line = sb->line[y];
216
0
                for(x=0; x<w; x++){
217
0
                    line[x] -= 128 << FRAC_BITS;
218
//                    buf[x + y*w]-= 128<<FRAC_BITS;
219
0
                }
220
0
            }
221
0
        }
222
223
3.88M
        return;
224
3.89M
    }
225
226
5.05M
    for(mb_x=0; mb_x<=mb_w; mb_x++){
227
4.66M
        add_yblock(s, 1, sb, old_buffer, dst8, obmc,
228
4.66M
                   block_w*mb_x - block_w/2,
229
4.66M
                   block_h*mb_y - block_h/2,
230
4.66M
                   block_w, block_h,
231
4.66M
                   w, h,
232
4.66M
                   w, ref_stride, obmc_stride,
233
4.66M
                   mb_x - 1, mb_y - 1,
234
4.66M
                   add, 0, plane_index);
235
4.66M
    }
236
237
388k
    if(s->avmv && mb_y < mb_h && plane_index == 0)
238
870k
        for(mb_x=0; mb_x<mb_w; mb_x++){
239
721k
            AVMotionVector *avmv = s->avmv + s->avmv_index;
240
721k
            const int b_width = s->b_width  << s->block_max_depth;
241
721k
            const int b_stride= b_width;
242
721k
            BlockNode *bn= &s->block[mb_x + mb_y*b_stride];
243
244
721k
            if (bn->type)
245
251k
                continue;
246
247
469k
            s->avmv_index++;
248
249
469k
            avmv->w = block_w;
250
469k
            avmv->h = block_h;
251
469k
            avmv->dst_x = block_w*mb_x - block_w/2;
252
469k
            avmv->dst_y = block_h*mb_y - block_h/2;
253
469k
            avmv->motion_scale = 8;
254
469k
            avmv->motion_x = bn->mx * s->mv_scale;
255
469k
            avmv->motion_y = bn->my * s->mv_scale;
256
469k
            avmv->src_x = avmv->dst_x + avmv->motion_x / 8;
257
469k
            avmv->src_y = avmv->dst_y + avmv->motion_y / 8;
258
469k
            avmv->source= -1 - bn->ref;
259
469k
            avmv->flags = 0;
260
469k
        }
261
388k
}
262
263
28.7M
static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
264
28.7M
    const int w= b->width;
265
28.7M
    int y;
266
28.7M
    const int qlog= av_clip(s->qlog + (int64_t)b->qlog, 0, QROOT*16);
267
28.7M
    int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
268
28.7M
    int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
269
28.7M
    int new_index = 0;
270
271
28.7M
    if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
272
4.41M
        qadd= 0;
273
4.41M
        qmul= 1<<QEXPSHIFT;
274
4.41M
    }
275
276
    /* If we are on the second or later slice, restore our index. */
277
28.7M
    if (start_y != 0)
278
28.3M
        new_index = save_state[0];
279
280
281
137M
    for(y=start_y; y<h; y++){
282
108M
        int x = 0;
283
108M
        int v;
284
108M
        IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
285
108M
        memset(line, 0, b->width*sizeof(IDWTELEM));
286
108M
        v = b->x_coeff[new_index].coeff;
287
108M
        x = b->x_coeff[new_index++].x;
288
120M
        while(x < w){
289
11.8M
            register int t= (int)( (v>>1)*(unsigned)qmul + qadd)>>QEXPSHIFT;
290
11.8M
            register int u= -(v&1);
291
11.8M
            line[x] = (t^u) - u;
292
293
11.8M
            v = b->x_coeff[new_index].coeff;
294
11.8M
            x = b->x_coeff[new_index++].x;
295
11.8M
        }
296
108M
    }
297
298
    /* Save our variables for the next slice. */
299
28.7M
    save_state[0] = new_index;
300
301
28.7M
    return;
302
28.7M
}
303
304
19.9M
static int decode_q_branch(SnowContext *s, int level, int x, int y){
305
19.9M
    const int w= s->b_width << s->block_max_depth;
306
19.9M
    const int rem_depth= s->block_max_depth - level;
307
19.9M
    const int index= (x + y*w) << rem_depth;
308
19.9M
    int trx= (x+1)<<rem_depth;
309
19.9M
    const BlockNode *left  = x ? &s->block[index-1] : &null_block;
310
19.9M
    const BlockNode *top   = y ? &s->block[index-w] : &null_block;
311
19.9M
    const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
312
19.9M
    const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
313
19.9M
    int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
314
19.9M
    int res;
315
316
19.9M
    if(s->keyframe){
317
17.6M
        set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
318
17.6M
        return 0;
319
17.6M
    }
320
321
2.35M
    if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
322
2.17M
        int type, mx, my;
323
2.17M
        int l = left->color[0];
324
2.17M
        int cb= left->color[1];
325
2.17M
        int cr= left->color[2];
326
2.17M
        unsigned ref = 0;
327
2.17M
        int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
328
2.17M
        int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
329
2.17M
        int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
330
331
2.17M
        type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
332
2.17M
        if(type){
333
940k
            int ld, cbd, crd;
334
940k
            pred_mv(s, &mx, &my, 0, left, top, tr);
335
940k
            ld = get_symbol(&s->c, &s->block_state[32], 1);
336
940k
            if (ld < -255 || ld > 255) {
337
984
                return AVERROR_INVALIDDATA;
338
984
            }
339
939k
            l += ld;
340
939k
            if (s->nb_planes > 2) {
341
414k
                cbd = get_symbol(&s->c, &s->block_state[64], 1);
342
414k
                crd = get_symbol(&s->c, &s->block_state[96], 1);
343
414k
                if (cbd < -255 || cbd > 255 || crd < -255 || crd > 255) {
344
1.30k
                    return AVERROR_INVALIDDATA;
345
1.30k
                }
346
412k
                cb += cbd;
347
412k
                cr += crd;
348
412k
            }
349
1.23M
        }else{
350
1.23M
            if(s->ref_frames > 1)
351
35.5k
                ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
352
1.23M
            if (ref >= s->ref_frames) {
353
2.24k
                av_log(s->avctx, AV_LOG_ERROR, "Invalid ref\n");
354
2.24k
                return AVERROR_INVALIDDATA;
355
2.24k
            }
356
1.23M
            pred_mv(s, &mx, &my, ref, left, top, tr);
357
1.23M
            mx+= (unsigned)get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
358
1.23M
            my+= (unsigned)get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
359
1.23M
        }
360
2.16M
        set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
361
2.16M
    }else{
362
184k
        if ((res = decode_q_branch(s, level+1, 2*x+0, 2*y+0)) < 0 ||
363
184k
            (res = decode_q_branch(s, level+1, 2*x+1, 2*y+0)) < 0 ||
364
183k
            (res = decode_q_branch(s, level+1, 2*x+0, 2*y+1)) < 0 ||
365
183k
            (res = decode_q_branch(s, level+1, 2*x+1, 2*y+1)) < 0)
366
2.21k
            return res;
367
184k
    }
368
2.35M
    return 0;
369
2.35M
}
370
371
3.91M
static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
372
3.91M
    const int w= b->width;
373
3.91M
    const int qlog= av_clip(s->qlog + (int64_t)b->qlog, 0, QROOT*16);
374
3.91M
    const int qmul= ff_qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
375
3.91M
    const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
376
3.91M
    int x,y;
377
378
3.91M
    if(s->qlog == LOSSLESS_QLOG) return;
379
380
16.7M
    for(y=start_y; y<end_y; y++){
381
//        DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
382
12.8M
        IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
383
558M
        for(x=0; x<w; x++){
384
545M
            int i= line[x];
385
545M
            if(i<0){
386
113M
                line[x]= -((-i*(unsigned)qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
387
432M
            }else if(i>0){
388
60.6M
                line[x]=  (( i*(unsigned)qmul + qadd)>>(QEXPSHIFT));
389
60.6M
            }
390
545M
        }
391
12.8M
    }
392
3.89M
}
393
394
3.91M
static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
395
3.91M
    const int w= b->width;
396
3.91M
    int x,y;
397
398
3.91M
    IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
399
3.91M
    IDWTELEM * prev;
400
401
3.91M
    if (start_y != 0)
402
3.85M
        line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
403
404
16.7M
    for(y=start_y; y<end_y; y++){
405
12.8M
        prev = line;
406
//        line = slice_buffer_get_line_from_address(sb, src + (y * stride));
407
12.8M
        line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
408
558M
        for(x=0; x<w; x++){
409
545M
            if(x){
410
533M
                if(use_median){
411
0
                    if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
412
0
                    else  line[x] += line[x - 1];
413
533M
                }else{
414
533M
                    if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
415
7.37M
                    else  line[x] += line[x - 1];
416
533M
                }
417
533M
            }else{
418
12.8M
                if(y) line[x] += prev[x];
419
12.8M
            }
420
545M
        }
421
12.8M
    }
422
3.91M
}
423
424
17.0k
static void decode_qlogs(SnowContext *s){
425
17.0k
    int plane_index, level, orientation;
426
427
59.0k
    for(plane_index=0; plane_index < s->nb_planes; plane_index++){
428
171k
        for(level=0; level<s->spatial_decomposition_count; level++){
429
558k
            for(orientation=level ? 1:0; orientation<4; orientation++){
430
429k
                int q;
431
429k
                if     (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
432
293k
                else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
433
205k
                else                    q= get_symbol(&s->c, s->header_state, 1);
434
429k
                s->plane[plane_index].band[level][orientation].qlog= q;
435
429k
            }
436
129k
        }
437
41.9k
    }
438
17.0k
}
439
440
#define GET_S(dst, check) \
441
79.6k
    tmp= get_symbol(&s->c, s->header_state, 0);\
442
79.6k
    if(!(check)){\
443
26.7k
        av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
444
26.7k
        return AVERROR_INVALIDDATA;\
445
26.7k
    }\
446
79.6k
    dst= tmp;
447
448
179k
static int decode_header(SnowContext *s){
449
179k
    int plane_index, tmp;
450
179k
    uint8_t kstate[32];
451
452
179k
    memset(kstate, MID_STATE, sizeof(kstate));
453
454
179k
    s->keyframe= get_rac(&s->c, kstate);
455
179k
    if(s->keyframe || s->always_reset){
456
126k
        ff_snow_reset_contexts(s);
457
126k
        s->spatial_decomposition_type=
458
126k
        s->qlog=
459
126k
        s->qbias=
460
126k
        s->mv_scale=
461
126k
        s->block_max_depth= 0;
462
126k
    }
463
179k
    if(s->keyframe){
464
27.7k
        GET_S(s->version, tmp <= 0U)
465
23.6k
        s->always_reset= get_rac(&s->c, s->header_state);
466
23.6k
        s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
467
23.6k
        s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
468
23.6k
        GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
469
12.1k
        s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
470
12.1k
        if (s->colorspace_type == 1) {
471
2.44k
            s->avctx->pix_fmt= AV_PIX_FMT_GRAY8;
472
2.44k
            s->nb_planes = 1;
473
9.69k
        } else if(s->colorspace_type == 0) {
474
8.89k
            s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
475
8.89k
            s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
476
477
8.89k
            if(s->chroma_h_shift == 1 && s->chroma_v_shift==1){
478
2.21k
                s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
479
6.67k
            }else if(s->chroma_h_shift == 0 && s->chroma_v_shift==0){
480
4.27k
                s->avctx->pix_fmt= AV_PIX_FMT_YUV444P;
481
4.27k
            }else if(s->chroma_h_shift == 2 && s->chroma_v_shift==2){
482
524
                s->avctx->pix_fmt= AV_PIX_FMT_YUV410P;
483
1.87k
            } else {
484
1.87k
                av_log(s->avctx, AV_LOG_ERROR,
485
1.87k
                       "unsupported color subsample mode %d %d\n",
486
1.87k
                       s->chroma_h_shift, s->chroma_v_shift);
487
1.87k
                s->chroma_h_shift = s->chroma_v_shift = 1;
488
1.87k
                s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
489
1.87k
                return AVERROR_INVALIDDATA;
490
1.87k
            }
491
7.01k
            s->nb_planes = 3;
492
7.01k
        } else {
493
800
            av_log(s->avctx, AV_LOG_ERROR, "unsupported color space\n");
494
800
            s->chroma_h_shift = s->chroma_v_shift = 1;
495
800
            s->avctx->pix_fmt= AV_PIX_FMT_YUV420P;
496
800
            return AVERROR_INVALIDDATA;
497
800
        }
498
499
500
9.45k
        s->spatial_scalability= get_rac(&s->c, s->header_state);
501
//        s->rate_scalability= get_rac(&s->c, s->header_state);
502
9.45k
        GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
503
9.18k
        s->max_ref_frames++;
504
505
9.18k
        decode_qlogs(s);
506
9.18k
    }
507
508
161k
    if(!s->keyframe){
509
151k
        if(get_rac(&s->c, s->header_state)){
510
158k
            for(plane_index=0; plane_index<FFMIN(s->nb_planes, 2); plane_index++){
511
99.4k
                int htaps, i, sum=0;
512
99.4k
                Plane *p= &s->plane[plane_index];
513
99.4k
                p->diag_mc= get_rac(&s->c, s->header_state);
514
99.4k
                htaps= get_symbol(&s->c, s->header_state, 0);
515
99.4k
                if((unsigned)htaps >= HTAPS_MAX/2 - 1)
516
4.46k
                    return AVERROR_INVALIDDATA;
517
94.9k
                htaps = htaps*2 + 2;
518
94.9k
                p->htaps= htaps;
519
232k
                for(i= htaps/2; i; i--){
520
147k
                    unsigned hcoeff = get_symbol(&s->c, s->header_state, 0);
521
147k
                    if (hcoeff > 127)
522
10.2k
                        return AVERROR_INVALIDDATA;
523
137k
                    p->hcoeff[i]= hcoeff * (1-2*(i&1));
524
137k
                    sum += p->hcoeff[i];
525
137k
                }
526
84.7k
                p->hcoeff[0]= 32-sum;
527
84.7k
            }
528
59.0k
            s->plane[2].diag_mc= s->plane[1].diag_mc;
529
59.0k
            s->plane[2].htaps  = s->plane[1].htaps;
530
59.0k
            memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
531
59.0k
        }
532
137k
        if(get_rac(&s->c, s->header_state)){
533
18.7k
            GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
534
7.85k
            decode_qlogs(s);
535
7.85k
        }
536
137k
    }
537
538
135k
    s->spatial_decomposition_type+= (unsigned)get_symbol(&s->c, s->header_state, 1);
539
135k
    if(s->spatial_decomposition_type > 1U){
540
45.3k
        av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported\n", s->spatial_decomposition_type);
541
45.3k
        return AVERROR_INVALIDDATA;
542
45.3k
    }
543
90.1k
    if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
544
90.1k
             s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 1){
545
9.73k
        av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size\n", s->spatial_decomposition_count);
546
9.73k
        return AVERROR_INVALIDDATA;
547
9.73k
    }
548
80.4k
    if (s->avctx->width > 65536-4) {
549
290
        av_log(s->avctx, AV_LOG_ERROR, "Width %d is too large\n", s->avctx->width);
550
290
        return AVERROR_INVALIDDATA;
551
290
    }
552
553
554
80.1k
    s->qlog           += (unsigned)get_symbol(&s->c, s->header_state, 1);
555
80.1k
    s->mv_scale       += (unsigned)get_symbol(&s->c, s->header_state, 1);
556
80.1k
    s->qbias          += (unsigned)get_symbol(&s->c, s->header_state, 1);
557
80.1k
    s->block_max_depth+= (unsigned)get_symbol(&s->c, s->header_state, 1);
558
80.1k
    if(s->block_max_depth > 1 || s->block_max_depth < 0 || s->mv_scale > 256U){
559
12.7k
        av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large\n", s->block_max_depth);
560
12.7k
        s->block_max_depth= 0;
561
12.7k
        s->mv_scale = 0;
562
12.7k
        return AVERROR_INVALIDDATA;
563
12.7k
    }
564
67.4k
    if (FFABS(s->qbias) > 127) {
565
435
        av_log(s->avctx, AV_LOG_ERROR, "qbias %d is too large\n", s->qbias);
566
435
        s->qbias = 0;
567
435
        return AVERROR_INVALIDDATA;
568
435
    }
569
570
66.9k
    return 0;
571
67.4k
}
572
573
63.4k
static int decode_blocks(SnowContext *s){
574
63.4k
    int x, y;
575
63.4k
    int w= s->b_width;
576
63.4k
    int h= s->b_height;
577
63.4k
    int res;
578
579
1.59M
    for(y=0; y<h; y++){
580
20.8M
        for(x=0; x<w; x++){
581
19.2M
            if (s->c.bytestream >= s->c.bytestream_end)
582
19.6k
                return AVERROR_INVALIDDATA;
583
19.2M
            if ((res = decode_q_branch(s, 0, x, y)) < 0)
584
4.53k
                return res;
585
19.2M
        }
586
1.55M
    }
587
39.2k
    return 0;
588
63.4k
}
589
590
static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
591
                        int *got_frame, AVPacket *avpkt)
592
179k
{
593
179k
    const uint8_t *buf = avpkt->data;
594
179k
    int buf_size = avpkt->size;
595
179k
    SnowContext *s = avctx->priv_data;
596
179k
    RangeCoder * const c= &s->c;
597
179k
    int bytes_read;
598
179k
    int level, orientation, plane_index;
599
179k
    int res;
600
601
179k
    ff_init_range_decoder(c, buf, buf_size);
602
179k
    ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
603
604
179k
    s->current_picture->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
605
179k
    if ((res = decode_header(s)) < 0)
606
112k
        return res;
607
608
66.9k
    if (!s->mconly_picture->data[0]) {
609
3.68k
        res = ff_get_buffer(avctx, s->mconly_picture, AV_GET_BUFFER_FLAG_REF);
610
3.68k
        if (res < 0)
611
1.38k
            return res;
612
3.68k
    }
613
65.6k
    if (s->mconly_picture->format != avctx->pix_fmt) {
614
1.20k
        av_log(avctx, AV_LOG_ERROR, "pixel format changed\n");
615
1.20k
        return AVERROR_INVALIDDATA;
616
1.20k
    }
617
618
64.4k
    if ((res=ff_snow_common_init_after_header(avctx)) < 0)
619
0
        return res;
620
621
    // realloc slice buffer for the case that spatial_decomposition_count changed
622
64.4k
    ff_slice_buffer_destroy(&s->sb);
623
64.4k
    if ((res = ff_slice_buffer_init(&s->sb, s->plane[0].height,
624
64.4k
                                    (MB_SIZE >> s->block_max_depth) +
625
64.4k
                                    s->spatial_decomposition_count * 11 + 1,
626
64.4k
                                    s->plane[0].width,
627
64.4k
                                    s->spatial_idwt_buffer)) < 0)
628
0
        return res;
629
630
171k
    for(plane_index=0; plane_index < s->nb_planes; plane_index++){
631
107k
        Plane *p= &s->plane[plane_index];
632
107k
        p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
633
16.2k
                                              && p->hcoeff[1]==-10
634
16.0k
                                              && p->hcoeff[2]==2;
635
107k
    }
636
637
64.4k
    ff_snow_alloc_blocks(s);
638
639
64.4k
    if ((res = ff_snow_frames_prepare(s)) < 0)
640
957
        return res;
641
642
63.4k
    s->current_picture->width  = s->avctx->width;
643
63.4k
    s->current_picture->height = s->avctx->height;
644
63.4k
    res = ff_get_buffer(s->avctx, s->current_picture, AV_GET_BUFFER_FLAG_REF);
645
63.4k
    if (res < 0)
646
0
        return res;
647
648
63.4k
    s->current_picture->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
649
650
    //keyframe flag duplication mess FIXME
651
63.4k
    if(avctx->debug&FF_DEBUG_PICT_INFO)
652
0
        av_log(avctx, AV_LOG_ERROR,
653
0
               "keyframe:%d qlog:%d qbias: %d mvscale: %d "
654
0
               "decomposition_type:%d decomposition_count:%d\n",
655
0
               s->keyframe, s->qlog, s->qbias, s->mv_scale,
656
0
               s->spatial_decomposition_type,
657
0
               s->spatial_decomposition_count
658
0
              );
659
660
63.4k
    if (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) {
661
42.3k
        size_t size;
662
42.3k
        res = av_size_mult(s->b_width * s->b_height, sizeof(AVMotionVector) << (s->block_max_depth*2), &size);
663
42.3k
        if (res)
664
0
            return res;
665
42.3k
        av_fast_malloc(&s->avmv, &s->avmv_size, size);
666
42.3k
        if (!s->avmv)
667
0
            return AVERROR(ENOMEM);
668
42.3k
    } else {
669
21.0k
        s->avmv_size = 0;
670
21.0k
        av_freep(&s->avmv);
671
21.0k
    }
672
63.4k
    s->avmv_index = 0;
673
674
63.4k
    if ((res = decode_blocks(s)) < 0)
675
24.2k
        return res;
676
677
96.6k
    for(plane_index=0; plane_index < s->nb_planes; plane_index++){
678
57.4k
        Plane *p= &s->plane[plane_index];
679
57.4k
        int w= p->width;
680
57.4k
        int h= p->height;
681
57.4k
        int x, y;
682
57.4k
        int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
683
684
57.4k
        if(s->avctx->debug&2048){
685
0
            memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
686
0
            predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
687
688
0
            for(y=0; y<h; y++){
689
0
                for(x=0; x<w; x++){
690
0
                    int v= s->current_picture->data[plane_index][y*s->current_picture->linesize[plane_index] + x];
691
0
                    s->mconly_picture->data[plane_index][y*s->mconly_picture->linesize[plane_index] + x]= v;
692
0
                }
693
0
            }
694
0
        }
695
696
163k
        for(level=0; level<s->spatial_decomposition_count; level++){
697
482k
            for(orientation=level ? 1 : 0; orientation<4; orientation++){
698
376k
                SubBand *b= &p->band[level][orientation];
699
376k
                unpack_coeffs(s, b, b->parent, orientation);
700
376k
            }
701
106k
        }
702
703
57.4k
        {
704
57.4k
        const int mb_h= s->b_height << s->block_max_depth;
705
57.4k
        const int block_size = MB_SIZE >> s->block_max_depth;
706
57.4k
        const int block_h    = plane_index ? block_size>>s->chroma_v_shift : block_size;
707
57.4k
        int mb_y;
708
57.4k
        DWTCompose cs[MAX_DECOMPOSITIONS];
709
57.4k
        int yd=0, yq=0;
710
57.4k
        int y;
711
57.4k
        int end_y;
712
713
57.4k
        ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
714
4.33M
        for(mb_y=0; mb_y<=mb_h; mb_y++){
715
716
4.27M
            int slice_starty = block_h*mb_y;
717
4.27M
            int slice_h = block_h*(mb_y+1);
718
719
4.27M
            if (!(s->keyframe || s->avctx->debug&512)){
720
388k
                slice_starty = FFMAX(0, slice_starty - (block_h >> 1));
721
388k
                slice_h -= (block_h >> 1);
722
388k
            }
723
724
13.2M
            for(level=0; level<s->spatial_decomposition_count; level++){
725
40.1M
                for(orientation=level ? 1 : 0; orientation<4; orientation++){
726
31.2M
                    SubBand *b= &p->band[level][orientation];
727
31.2M
                    int start_y;
728
31.2M
                    int end_y;
729
31.2M
                    int our_mb_start = mb_y;
730
31.2M
                    int our_mb_end = (mb_y + 1);
731
31.2M
                    const int extra= 3;
732
31.2M
                    start_y = (mb_y ? ((block_h * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
733
31.2M
                    end_y = (((block_h * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
734
31.2M
                    if (!(s->keyframe || s->avctx->debug&512)){
735
3.07M
                        start_y = FFMAX(0, start_y - (block_h >> (1+s->spatial_decomposition_count - level)));
736
3.07M
                        end_y = FFMAX(0, end_y - (block_h >> (1+s->spatial_decomposition_count - level)));
737
3.07M
                    }
738
31.2M
                    start_y = FFMIN(b->height, start_y);
739
31.2M
                    end_y = FFMIN(b->height, end_y);
740
741
31.2M
                    if (start_y != end_y){
742
28.7M
                        if (orientation == 0){
743
3.91M
                            SubBand * correlate_band = &p->band[0][0];
744
3.91M
                            int correlate_end_y = FFMIN(b->height, end_y + 1);
745
3.91M
                            int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
746
3.91M
                            decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
747
3.91M
                            correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
748
3.91M
                            dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
749
3.91M
                        }
750
24.8M
                        else
751
24.8M
                            decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
752
28.7M
                    }
753
31.2M
                }
754
8.97M
            }
755
756
15.6M
            for(; yd<slice_h; yd+=4){
757
11.3M
                ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, s->temp_idwt_buffer, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
758
11.3M
            }
759
760
4.27M
            if(s->qlog == LOSSLESS_QLOG){
761
498k
                for(; yq<slice_h && yq<h; yq++){
762
455k
                    IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
763
90.2M
                    for(x=0; x<w; x++){
764
89.7M
                        line[x] *= 1<<FRAC_BITS;
765
89.7M
                    }
766
455k
                }
767
42.6k
            }
768
769
4.27M
            predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
770
771
4.27M
            y = FFMIN(p->height, slice_starty);
772
4.27M
            end_y = FFMIN(p->height, slice_h);
773
48.9M
            while(y < end_y)
774
44.7M
                ff_slice_buffer_release(&s->sb, y++);
775
4.27M
        }
776
777
57.4k
        ff_slice_buffer_flush(&s->sb);
778
57.4k
        }
779
780
57.4k
    }
781
782
39.2k
    emms_c();
783
784
39.2k
    av_frame_unref(s->last_picture[s->max_ref_frames - 1]);
785
786
39.2k
    if(!(s->avctx->debug&2048))
787
39.2k
        res = av_frame_ref(picture, s->current_picture);
788
0
    else
789
0
        res = av_frame_ref(picture, s->mconly_picture);
790
39.2k
    if (res >= 0 && s->avmv_index) {
791
28.8k
        AVFrameSideData *sd;
792
793
28.8k
        sd = av_frame_new_side_data(picture, AV_FRAME_DATA_MOTION_VECTORS, s->avmv_index * sizeof(AVMotionVector));
794
28.8k
        if (!sd)
795
0
            return AVERROR(ENOMEM);
796
28.8k
        memcpy(sd->data, s->avmv, s->avmv_index * sizeof(AVMotionVector));
797
28.8k
    }
798
799
39.2k
    if (res < 0)
800
0
        return res;
801
802
39.2k
    *got_frame = 1;
803
804
39.2k
    bytes_read= c->bytestream - c->bytestream_start;
805
39.2k
    if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
806
807
39.2k
    return bytes_read;
808
39.2k
}
809
810
static av_cold int decode_end(AVCodecContext *avctx)
811
4.00k
{
812
4.00k
    SnowContext *s = avctx->priv_data;
813
814
4.00k
    ff_slice_buffer_destroy(&s->sb);
815
816
4.00k
    ff_snow_common_end(s);
817
818
4.00k
    s->avmv_size = 0;
819
4.00k
    av_freep(&s->avmv);
820
821
4.00k
    return 0;
822
4.00k
}
823
824
const FFCodec ff_snow_decoder = {
825
    .p.name         = "snow",
826
    CODEC_LONG_NAME("Snow"),
827
    .p.type         = AVMEDIA_TYPE_VIDEO,
828
    .p.id           = AV_CODEC_ID_SNOW,
829
    .priv_data_size = sizeof(SnowContext),
830
    .init           = ff_snow_common_init,
831
    .close          = decode_end,
832
    FF_CODEC_DECODE_CB(decode_frame),
833
    .p.capabilities = AV_CODEC_CAP_DR1,
834
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
835
};