xref: /third_party/ffmpeg/libavcodec/vp6.c (revision cabdff1a)
1/*
2 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
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/**
22 * @file
23 * VP6 compatible video decoder
24 *
25 * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
26 *  - upper 4 bits: difference between encoded width and visible width
27 *  - lower 4 bits: difference between encoded height and visible height
28 */
29
30#include <stdlib.h>
31
32#include "avcodec.h"
33#include "codec_internal.h"
34#include "get_bits.h"
35#include "huffman.h"
36#include "internal.h"
37
38#include "vp56.h"
39#include "vp56data.h"
40#include "vp6data.h"
41
42#define VP6_MAX_HUFF_SIZE 12
43
44static int vp6_parse_coeff(VP56Context *s);
45static int vp6_parse_coeff_huffman(VP56Context *s);
46
47static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
48{
49    VP56RangeCoder *c = &s->c;
50    int parse_filter_info = 0;
51    int coeff_offset = 0;
52    int vrt_shift = 0;
53    int sub_version;
54    int rows, cols;
55    int res = 0;
56    int ret;
57    int separated_coeff = buf[0] & 1;
58
59    s->frames[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
60    ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
61
62    if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
63        sub_version = buf[1] >> 3;
64        if (sub_version > 8)
65            return AVERROR_INVALIDDATA;
66        s->filter_header = buf[1] & 0x06;
67        if (buf[1] & 1) {
68            avpriv_report_missing_feature(s->avctx, "Interlacing");
69            return AVERROR_PATCHWELCOME;
70        }
71        if (separated_coeff || !s->filter_header) {
72            coeff_offset = AV_RB16(buf+2) - 2;
73            buf += 2;
74            buf_size -= 2;
75        }
76
77        rows = buf[2];  /* number of stored macroblock rows */
78        cols = buf[3];  /* number of stored macroblock cols */
79        /* buf[4] is number of displayed macroblock rows */
80        /* buf[5] is number of displayed macroblock cols */
81        if (!rows || !cols) {
82            av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
83            return AVERROR_INVALIDDATA;
84        }
85
86        if (!s->macroblocks || /* first frame */
87            16*cols != s->avctx->coded_width ||
88            16*rows != s->avctx->coded_height) {
89            if (s->avctx->extradata_size == 0 &&
90                FFALIGN(s->avctx->width,  16) == 16 * cols &&
91                FFALIGN(s->avctx->height, 16) == 16 * rows) {
92                // We assume this is properly signalled container cropping,
93                // in an F4V file. Just set the coded_width/height, don't
94                // touch the cropped ones.
95                s->avctx->coded_width  = 16 * cols;
96                s->avctx->coded_height = 16 * rows;
97            } else {
98                ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
99                if (ret < 0)
100                    return ret;
101
102                if (s->avctx->extradata_size == 1) {
103                    s->avctx->width  -= s->avctx->extradata[0] >> 4;
104                    s->avctx->height -= s->avctx->extradata[0] & 0x0F;
105                }
106            }
107            res = VP56_SIZE_CHANGE;
108        }
109
110        ret = ff_vp56_init_range_decoder(c, buf+6, buf_size-6);
111        if (ret < 0)
112            goto fail;
113        vp56_rac_gets(c, 2);
114
115        parse_filter_info = s->filter_header;
116        if (sub_version < 8)
117            vrt_shift = 5;
118        s->sub_version = sub_version;
119        s->golden_frame = 0;
120    } else {
121        if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
122            return AVERROR_INVALIDDATA;
123
124        if (separated_coeff || !s->filter_header) {
125            coeff_offset = AV_RB16(buf+1) - 2;
126            buf += 2;
127            buf_size -= 2;
128        }
129        ret = ff_vp56_init_range_decoder(c, buf+1, buf_size-1);
130        if (ret < 0)
131            return ret;
132
133        s->golden_frame = vp56_rac_get(c);
134        if (s->filter_header) {
135            s->deblock_filtering = vp56_rac_get(c);
136            if (s->deblock_filtering)
137                vp56_rac_get(c);
138            if (s->sub_version > 7)
139                parse_filter_info = vp56_rac_get(c);
140        }
141    }
142
143    if (parse_filter_info) {
144        if (vp56_rac_get(c)) {
145            s->filter_mode = 2;
146            s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
147            s->max_vector_length = 2 << vp56_rac_gets(c, 3);
148        } else if (vp56_rac_get(c)) {
149            s->filter_mode = 1;
150        } else {
151            s->filter_mode = 0;
152        }
153        if (s->sub_version > 7)
154            s->filter_selection = vp56_rac_gets(c, 4);
155        else
156            s->filter_selection = 16;
157    }
158
159    s->use_huffman = vp56_rac_get(c);
160
161    s->parse_coeff = vp6_parse_coeff;
162    if (coeff_offset) {
163        buf      += coeff_offset;
164        buf_size -= coeff_offset;
165        if (buf_size < 0) {
166            ret = AVERROR_INVALIDDATA;
167            goto fail;
168        }
169        if (s->use_huffman) {
170            s->parse_coeff = vp6_parse_coeff_huffman;
171            ret = init_get_bits8(&s->gb, buf, buf_size);
172            if (ret < 0)
173                return ret;
174        } else {
175            ret = ff_vp56_init_range_decoder(&s->cc, buf, buf_size);
176            if (ret < 0)
177                goto fail;
178            s->ccp = &s->cc;
179        }
180    } else {
181        s->ccp = &s->c;
182    }
183
184    return res;
185fail:
186    if (res == VP56_SIZE_CHANGE)
187        ff_set_dimensions(s->avctx, 0, 0);
188    return ret;
189}
190
191static void vp6_coeff_order_table_init(VP56Context *s)
192{
193    int i, pos, idx = 1;
194
195    s->modelp->coeff_index_to_pos[0] = 0;
196    for (i=0; i<16; i++)
197        for (pos=1; pos<64; pos++)
198            if (s->modelp->coeff_reorder[pos] == i)
199                s->modelp->coeff_index_to_pos[idx++] = pos;
200
201    for (idx = 0; idx < 64; idx++) {
202        int max = 0;
203        for (i = 0; i <= idx; i++) {
204            int v = s->modelp->coeff_index_to_pos[i];
205            if (v > max)
206                max = v;
207        }
208        if (s->sub_version > 6)
209            max++;
210        s->modelp->coeff_index_to_idct_selector[idx] = max;
211    }
212}
213
214static void vp6_default_models_init(VP56Context *s)
215{
216    VP56Model *model = s->modelp;
217
218    model->vector_dct[0] = 0xA2;
219    model->vector_dct[1] = 0xA4;
220    model->vector_sig[0] = 0x80;
221    model->vector_sig[1] = 0x80;
222
223    memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
224    memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
225    memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
226    memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
227    memcpy(model->coeff_reorder, vp6_def_coeff_reorder, sizeof(model->coeff_reorder));
228
229    vp6_coeff_order_table_init(s);
230}
231
232static void vp6_parse_vector_models(VP56Context *s)
233{
234    VP56RangeCoder *c = &s->c;
235    VP56Model *model = s->modelp;
236    int comp, node;
237
238    for (comp=0; comp<2; comp++) {
239        if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
240            model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
241        if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
242            model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
243    }
244
245    for (comp=0; comp<2; comp++)
246        for (node=0; node<7; node++)
247            if (vp56_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
248                model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
249
250    for (comp=0; comp<2; comp++)
251        for (node=0; node<8; node++)
252            if (vp56_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
253                model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
254}
255
256/* nodes must ascend by count, but with descending symbol order */
257static int vp6_huff_cmp(const void *va, const void *vb)
258{
259    const Node *a = va, *b = vb;
260    return (a->count - b->count)*16 + (b->sym - a->sym);
261}
262
263static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
264                               const uint8_t *map, unsigned size, VLC *vlc)
265{
266    Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
267    int a, b, i;
268
269    /* first compute probabilities from model */
270    tmp[0].count = 256;
271    for (i=0; i<size-1; i++) {
272        a = tmp[i].count *        coeff_model[i]  >> 8;
273        b = tmp[i].count * (255 - coeff_model[i]) >> 8;
274        nodes[map[2*i  ]].count = a + !a;
275        nodes[map[2*i+1]].count = b + !b;
276    }
277
278    ff_free_vlc(vlc);
279    /* then build the huffman tree according to probabilities */
280    return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
281                              nodes, vp6_huff_cmp,
282                              FF_HUFFMAN_FLAG_HNODE_FIRST);
283}
284
285static int vp6_parse_coeff_models(VP56Context *s)
286{
287    VP56RangeCoder *c = &s->c;
288    VP56Model *model = s->modelp;
289    int def_prob[11];
290    int node, cg, ctx, pos;
291    int ct;    /* code type */
292    int pt;    /* plane type (0 for Y, 1 for U or V) */
293
294    memset(def_prob, 0x80, sizeof(def_prob));
295
296    for (pt=0; pt<2; pt++)
297        for (node=0; node<11; node++)
298            if (vp56_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
299                def_prob[node] = vp56_rac_gets_nn(c, 7);
300                model->coeff_dccv[pt][node] = def_prob[node];
301            } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
302                model->coeff_dccv[pt][node] = def_prob[node];
303            }
304
305    if (vp56_rac_get(c)) {
306        for (pos=1; pos<64; pos++)
307            if (vp56_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
308                model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
309        vp6_coeff_order_table_init(s);
310    }
311
312    for (cg=0; cg<2; cg++)
313        for (node=0; node<14; node++)
314            if (vp56_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
315                model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
316
317    for (ct=0; ct<3; ct++)
318        for (pt=0; pt<2; pt++)
319            for (cg=0; cg<6; cg++)
320                for (node=0; node<11; node++)
321                    if (vp56_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
322                        def_prob[node] = vp56_rac_gets_nn(c, 7);
323                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
324                    } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
325                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
326                    }
327
328    if (s->use_huffman) {
329        for (pt=0; pt<2; pt++) {
330            if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
331                                    vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
332                return -1;
333            if (vp6_build_huff_tree(s, model->coeff_runv[pt],
334                                    vp6_huff_run_map, 9, &s->runv_vlc[pt]))
335                return -1;
336            for (ct=0; ct<3; ct++)
337                for (cg = 0; cg < 6; cg++)
338                    if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
339                                            vp6_huff_coeff_map, 12,
340                                            &s->ract_vlc[pt][ct][cg]))
341                        return -1;
342        }
343        memset(s->nb_null, 0, sizeof(s->nb_null));
344    } else {
345    /* coeff_dcct is a linear combination of coeff_dccv */
346    for (pt=0; pt<2; pt++)
347        for (ctx=0; ctx<3; ctx++)
348            for (node=0; node<5; node++)
349                model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
350    }
351    return 0;
352}
353
354static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
355{
356    VP56RangeCoder *c = &s->c;
357    VP56Model *model = s->modelp;
358    int comp;
359
360    *vect = (VP56mv) {0,0};
361    if (s->vector_candidate_pos < 2)
362        *vect = s->vector_candidate[0];
363
364    for (comp=0; comp<2; comp++) {
365        int i, delta = 0;
366
367        if (vp56_rac_get_prob_branchy(c, model->vector_dct[comp])) {
368            static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
369            for (i=0; i<sizeof(prob_order); i++) {
370                int j = prob_order[i];
371                delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
372            }
373            if (delta & 0xF0)
374                delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
375            else
376                delta |= 8;
377        } else {
378            delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
379                                      model->vector_pdv[comp]);
380        }
381
382        if (delta && vp56_rac_get_prob_branchy(c, model->vector_sig[comp]))
383            delta = -delta;
384
385        if (!comp)
386            vect->x += delta;
387        else
388            vect->y += delta;
389    }
390}
391
392/**
393 * Read number of consecutive blocks with null DC or AC.
394 * This value is < 74.
395 */
396static unsigned vp6_get_nb_null(VP56Context *s)
397{
398    unsigned val = get_bits(&s->gb, 2);
399    if (val == 2)
400        val += get_bits(&s->gb, 2);
401    else if (val == 3) {
402        val = get_bits1(&s->gb) << 2;
403        val = 6+val + get_bits(&s->gb, 2+val);
404    }
405    return val;
406}
407
408static int vp6_parse_coeff_huffman(VP56Context *s)
409{
410    VP56Model *model = s->modelp;
411    uint8_t *permute = s->idct_scantable;
412    VLC *vlc_coeff;
413    int coeff, sign, coeff_idx;
414    int b, cg, idx;
415    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
416
417    for (b=0; b<6; b++) {
418        int ct = 0;    /* code type */
419        if (b > 3) pt = 1;
420        vlc_coeff = &s->dccv_vlc[pt];
421
422        for (coeff_idx = 0;;) {
423            int run = 1;
424            if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
425                s->nb_null[coeff_idx][pt]--;
426                if (coeff_idx)
427                    break;
428            } else {
429                if (get_bits_left(&s->gb) <= 0)
430                    return AVERROR_INVALIDDATA;
431                coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
432                if (coeff == 0) {
433                    if (coeff_idx) {
434                        int pt = (coeff_idx >= 6);
435                        run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
436                        if (run >= 9)
437                            run += get_bits(&s->gb, 6);
438                    } else
439                        s->nb_null[0][pt] = vp6_get_nb_null(s);
440                    ct = 0;
441                } else if (coeff == 11) {  /* end of block */
442                    if (coeff_idx == 1)    /* first AC coeff ? */
443                        s->nb_null[1][pt] = vp6_get_nb_null(s);
444                    break;
445                } else {
446                    int coeff2 = ff_vp56_coeff_bias[coeff];
447                    if (coeff > 4)
448                        coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
449                    ct = 1 + (coeff2 > 1);
450                    sign = get_bits1(&s->gb);
451                    coeff2 = (coeff2 ^ -sign) + sign;
452                    if (coeff_idx)
453                        coeff2 *= s->dequant_ac;
454                    idx = model->coeff_index_to_pos[coeff_idx];
455                    s->block_coeff[b][permute[idx]] = coeff2;
456                }
457            }
458            coeff_idx+=run;
459            if (coeff_idx >= 64)
460                break;
461            cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
462            vlc_coeff = &s->ract_vlc[pt][ct][cg];
463        }
464        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
465    }
466    return 0;
467}
468
469static int vp6_parse_coeff(VP56Context *s)
470{
471    VP56RangeCoder *c = s->ccp;
472    VP56Model *model = s->modelp;
473    uint8_t *permute = s->idct_scantable;
474    uint8_t *model1, *model2, *model3;
475    int coeff, sign, coeff_idx;
476    int b, i, cg, idx, ctx;
477    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
478
479    if (vpX_rac_is_end(c)) {
480        av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
481        return AVERROR_INVALIDDATA;
482    }
483
484    for (b=0; b<6; b++) {
485        int ct = 1;    /* code type */
486        int run = 1;
487
488        if (b > 3) pt = 1;
489
490        ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
491              + s->above_blocks[s->above_block_idx[b]].not_null_dc;
492        model1 = model->coeff_dccv[pt];
493        model2 = model->coeff_dcct[pt][ctx];
494
495        coeff_idx = 0;
496        for (;;) {
497            if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob_branchy(c, model2[0])) {
498                /* parse a coeff */
499                if (vp56_rac_get_prob_branchy(c, model2[2])) {
500                    if (vp56_rac_get_prob_branchy(c, model2[3])) {
501                        idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
502                        coeff = ff_vp56_coeff_bias[idx+5];
503                        for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
504                            coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
505                    } else {
506                        if (vp56_rac_get_prob_branchy(c, model2[4]))
507                            coeff = 3 + vp56_rac_get_prob(c, model1[5]);
508                        else
509                            coeff = 2;
510                    }
511                    ct = 2;
512                } else {
513                    ct = 1;
514                    coeff = 1;
515                }
516                sign = vp56_rac_get(c);
517                coeff = (coeff ^ -sign) + sign;
518                if (coeff_idx)
519                    coeff *= s->dequant_ac;
520                idx = model->coeff_index_to_pos[coeff_idx];
521                s->block_coeff[b][permute[idx]] = coeff;
522                run = 1;
523            } else {
524                /* parse a run */
525                ct = 0;
526                if (coeff_idx > 0) {
527                    if (!vp56_rac_get_prob_branchy(c, model2[1]))
528                        break;
529
530                    model3 = model->coeff_runv[coeff_idx >= 6];
531                    run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
532                    if (!run)
533                        for (run=9, i=0; i<6; i++)
534                            run += vp56_rac_get_prob(c, model3[i+8]) << i;
535                }
536            }
537            coeff_idx += run;
538            if (coeff_idx >= 64)
539                break;
540            cg = vp6_coeff_groups[coeff_idx];
541            model1 = model2 = model->coeff_ract[pt][ct][cg];
542        }
543
544        s->left_block[ff_vp56_b6to4[b]].not_null_dc =
545        s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
546        s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
547    }
548    return 0;
549}
550
551static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
552{
553    int sum = 0, square_sum = 0;
554    int y, x;
555
556    for (y=0; y<8; y+=2) {
557        for (x=0; x<8; x+=2) {
558            sum += src[x];
559            square_sum += src[x]*src[x];
560        }
561        src += 2*stride;
562    }
563    return (16*square_sum - sum*sum) >> 8;
564}
565
566static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
567                           int delta, const int16_t *weights)
568{
569    int x, y;
570
571    for (y=0; y<8; y++) {
572        for (x=0; x<8; x++) {
573            dst[x] = av_clip_uint8((  src[x-delta  ] * weights[0]
574                                 + src[x        ] * weights[1]
575                                 + src[x+delta  ] * weights[2]
576                                 + src[x+2*delta] * weights[3] + 64) >> 7);
577        }
578        src += stride;
579        dst += stride;
580    }
581}
582
583static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
584                             ptrdiff_t stride, int h_weight, int v_weight)
585{
586    uint8_t *tmp = s->edge_emu_buffer+16;
587    s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
588    s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
589}
590
591static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
592                       int offset1, int offset2, ptrdiff_t stride,
593                       VP56mv mv, int mask, int select, int luma)
594{
595    int filter4 = 0;
596    int x8 = mv.x & mask;
597    int y8 = mv.y & mask;
598
599    if (luma) {
600        x8 *= 2;
601        y8 *= 2;
602        filter4 = s->filter_mode;
603        if (filter4 == 2) {
604            if (s->max_vector_length &&
605                (FFABS(mv.x) > s->max_vector_length ||
606                 FFABS(mv.y) > s->max_vector_length)) {
607                filter4 = 0;
608            } else if (s->sample_variance_threshold
609                       && (vp6_block_variance(src+offset1, stride)
610                           < s->sample_variance_threshold)) {
611                filter4 = 0;
612            }
613        }
614    }
615
616    if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
617        offset1 = offset2;
618    }
619
620    if (filter4) {
621        if (!y8) {                      /* left or right combine */
622            vp6_filter_hv4(dst, src+offset1, stride, 1,
623                           vp6_block_copy_filter[select][x8]);
624        } else if (!x8) {               /* above or below combine */
625            vp6_filter_hv4(dst, src+offset1, stride, stride,
626                           vp6_block_copy_filter[select][y8]);
627        } else {
628            s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
629                             vp6_block_copy_filter[select][x8],
630                             vp6_block_copy_filter[select][y8]);
631        }
632    } else {
633        if (!x8 || !y8) {
634            s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
635        } else {
636            vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
637        }
638    }
639}
640
641static av_cold int vp6_decode_init_context(AVCodecContext *avctx,
642                                           VP56Context *s, int flip, int has_alpha)
643{
644    int ret = ff_vp56_init_context(avctx, s, flip, has_alpha);
645    if (ret < 0)
646        return ret;
647
648    ff_vp6dsp_init(&s->vp56dsp);
649
650    s->deblock_filtering = 0;
651    s->vp56_coord_div = vp6_coord_div;
652    s->parse_vector_adjustment = vp6_parse_vector_adjustment;
653    s->filter = vp6_filter;
654    s->default_models_init = vp6_default_models_init;
655    s->parse_vector_models = vp6_parse_vector_models;
656    s->parse_coeff_models = vp6_parse_coeff_models;
657    s->parse_header = vp6_parse_header;
658
659    return 0;
660}
661
662static av_cold int vp6_decode_init(AVCodecContext *avctx)
663{
664    VP56Context *s = avctx->priv_data;
665    int ret;
666
667    ret = vp6_decode_init_context(avctx, s, avctx->codec_id == AV_CODEC_ID_VP6,
668                                  avctx->codec_id == AV_CODEC_ID_VP6A);
669    if (ret < 0)
670        return ret;
671
672    if (s->has_alpha) {
673        /* Can only happen for ff_vp6a_decoder */
674        s->alpha_context = &s[1];
675        ret = vp6_decode_init_context(avctx, s->alpha_context,
676                                      s->flip == -1, s->has_alpha);
677        if (ret < 0)
678            return ret;
679    }
680
681    return 0;
682}
683
684static av_cold void vp6_decode_free_context(VP56Context *s);
685
686static av_cold int vp6_decode_free(AVCodecContext *avctx)
687{
688    VP56Context *s = avctx->priv_data;
689
690    vp6_decode_free_context(s);
691
692    if (s->alpha_context) {
693        vp6_decode_free_context(s->alpha_context);
694        s->alpha_context = NULL;
695    }
696
697    return 0;
698}
699
700static av_cold void vp6_decode_free_context(VP56Context *s)
701{
702    int pt, ct, cg;
703
704    ff_vp56_free_context(s);
705
706    for (pt=0; pt<2; pt++) {
707        ff_free_vlc(&s->dccv_vlc[pt]);
708        ff_free_vlc(&s->runv_vlc[pt]);
709        for (ct=0; ct<3; ct++)
710            for (cg=0; cg<6; cg++)
711                ff_free_vlc(&s->ract_vlc[pt][ct][cg]);
712    }
713}
714
715const FFCodec ff_vp6_decoder = {
716    .p.name         = "vp6",
717    .p.long_name    = NULL_IF_CONFIG_SMALL("On2 VP6"),
718    .p.type         = AVMEDIA_TYPE_VIDEO,
719    .p.id           = AV_CODEC_ID_VP6,
720    .priv_data_size = sizeof(VP56Context),
721    .init           = vp6_decode_init,
722    .close          = vp6_decode_free,
723    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
724    .p.capabilities = AV_CODEC_CAP_DR1,
725    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
726};
727
728/* flash version, not flipped upside-down */
729const FFCodec ff_vp6f_decoder = {
730    .p.name         = "vp6f",
731    .p.long_name    = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version)"),
732    .p.type         = AVMEDIA_TYPE_VIDEO,
733    .p.id           = AV_CODEC_ID_VP6F,
734    .priv_data_size = sizeof(VP56Context),
735    .init           = vp6_decode_init,
736    .close          = vp6_decode_free,
737    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
738    .p.capabilities = AV_CODEC_CAP_DR1,
739    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
740};
741
742/* flash version, not flipped upside-down, with alpha channel */
743const FFCodec ff_vp6a_decoder = {
744    .p.name         = "vp6a",
745    .p.long_name    = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version, with alpha channel)"),
746    .p.type         = AVMEDIA_TYPE_VIDEO,
747    .p.id           = AV_CODEC_ID_VP6A,
748    .priv_data_size = 2 /* Main context + alpha context */ * sizeof(VP56Context),
749    .init           = vp6_decode_init,
750    .close          = vp6_decode_free,
751    FF_CODEC_DECODE_CB(ff_vp56_decode_frame),
752    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
753    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
754};
755