~ubuntu-branches/debian/sid/mplayer/sid

« back to all changes in this revision

Viewing changes to libavcodec/vp6.c

  • Committer: Bazaar Package Importer
  • Author(s): A Mennucc1
  • Date: 2009-03-23 10:05:45 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090323100545-x8h79obawnnte7kk
Tags: 1.0~rc2+svn20090303-5
debian/control : move docbook-xml,docbook-xsl,xsltproc from 
Build-Depends-Indep to Build-Depends, since they are needed to run
configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 * @file vp6.c
 
2
 * @file libavcodec/vp6.c
3
3
 * VP6 compatible video decoder
4
4
 *
5
5
 * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
30
30
#include "avcodec.h"
31
31
#include "dsputil.h"
32
32
#include "bitstream.h"
33
 
#include "mpegvideo.h"
 
33
#include "huffman.h"
34
34
 
35
35
#include "vp56.h"
36
36
#include "vp56data.h"
37
37
#include "vp6data.h"
38
38
 
39
39
 
40
 
static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
 
40
static void vp6_parse_coeff(VP56Context *s);
 
41
static void vp6_parse_coeff_huffman(VP56Context *s);
 
42
 
 
43
static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size,
41
44
                            int *golden_frame)
42
45
{
43
 
    vp56_range_coder_t *c = &s->c;
 
46
    VP56RangeCoder *c = &s->c;
44
47
    int parse_filter_info = 0;
45
48
    int coeff_offset = 0;
46
49
    int vrt_shift = 0;
72
75
        /* buf[4] is number of displayed macroblock rows */
73
76
        /* buf[5] is number of displayed macroblock cols */
74
77
 
75
 
        if (16*cols != s->avctx->coded_width ||
 
78
        if (!s->macroblocks || /* first frame */
 
79
            16*cols != s->avctx->coded_width ||
76
80
            16*rows != s->avctx->coded_height) {
77
81
            avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
78
82
            if (s->avctx->extradata_size == 1) {
126
130
            s->filter_selection = 16;
127
131
    }
128
132
 
129
 
    if (vp56_rac_get(c))
130
 
        av_log(s->avctx, AV_LOG_WARNING,
131
 
               "alternative entropy decoding not supported\n");
 
133
    s->use_huffman = vp56_rac_get(c);
132
134
 
 
135
    s->parse_coeff = vp6_parse_coeff;
133
136
    if (coeff_offset) {
134
 
        vp56_init_range_decoder(&s->cc, buf+coeff_offset,
135
 
                                buf_size-coeff_offset);
136
 
        s->ccp = &s->cc;
 
137
        buf      += coeff_offset;
 
138
        buf_size -= coeff_offset;
 
139
        if (buf_size < 0)
 
140
            return 0;
 
141
        if (s->use_huffman) {
 
142
            s->parse_coeff = vp6_parse_coeff_huffman;
 
143
            init_get_bits(&s->gb, buf, buf_size<<3);
 
144
        } else {
 
145
            vp56_init_range_decoder(&s->cc, buf, buf_size);
 
146
            s->ccp = &s->cc;
 
147
        }
137
148
    } else {
138
149
        s->ccp = &s->c;
139
150
    }
141
152
    return res;
142
153
}
143
154
 
144
 
static void vp6_coeff_order_table_init(vp56_context_t *s)
 
155
static void vp6_coeff_order_table_init(VP56Context *s)
145
156
{
146
157
    int i, pos, idx = 1;
147
158
 
152
163
                s->modelp->coeff_index_to_pos[idx++] = pos;
153
164
}
154
165
 
155
 
static void vp6_default_models_init(vp56_context_t *s)
 
166
static void vp6_default_models_init(VP56Context *s)
156
167
{
157
 
    vp56_model_t *model = s->modelp;
 
168
    VP56Model *model = s->modelp;
158
169
 
159
170
    model->vector_dct[0] = 0xA2;
160
171
    model->vector_dct[1] = 0xA4;
170
181
    vp6_coeff_order_table_init(s);
171
182
}
172
183
 
173
 
static void vp6_parse_vector_models(vp56_context_t *s)
 
184
static void vp6_parse_vector_models(VP56Context *s)
174
185
{
175
 
    vp56_range_coder_t *c = &s->c;
176
 
    vp56_model_t *model = s->modelp;
 
186
    VP56RangeCoder *c = &s->c;
 
187
    VP56Model *model = s->modelp;
177
188
    int comp, node;
178
189
 
179
190
    for (comp=0; comp<2; comp++) {
194
205
                model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
195
206
}
196
207
 
197
 
static void vp6_parse_coeff_models(vp56_context_t *s)
198
 
{
199
 
    vp56_range_coder_t *c = &s->c;
200
 
    vp56_model_t *model = s->modelp;
 
208
/* nodes must ascend by count, but with descending symbol order */
 
209
static int vp6_huff_cmp(const void *va, const void *vb)
 
210
{
 
211
    const Node *a = va, *b = vb;
 
212
    return (a->count - b->count)*16 + (b->sym - a->sym);
 
213
}
 
214
 
 
215
static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
 
216
                                const uint8_t *map, unsigned size, VLC *vlc)
 
217
{
 
218
    Node nodes[2*size], *tmp = &nodes[size];
 
219
    int a, b, i;
 
220
 
 
221
    /* first compute probabilities from model */
 
222
    tmp[0].count = 256;
 
223
    for (i=0; i<size-1; i++) {
 
224
        a = tmp[i].count *        coeff_model[i]  >> 8;
 
225
        b = tmp[i].count * (255 - coeff_model[i]) >> 8;
 
226
        nodes[map[2*i  ]].count = a + !a;
 
227
        nodes[map[2*i+1]].count = b + !b;
 
228
    }
 
229
 
 
230
    /* then build the huffman tree accodring to probabilities */
 
231
    ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp,
 
232
                       FF_HUFFMAN_FLAG_HNODE_FIRST);
 
233
}
 
234
 
 
235
static void vp6_parse_coeff_models(VP56Context *s)
 
236
{
 
237
    VP56RangeCoder *c = &s->c;
 
238
    VP56Model *model = s->modelp;
201
239
    int def_prob[11];
202
240
    int node, cg, ctx, pos;
203
241
    int ct;    /* code type */
237
275
                        model->coeff_ract[pt][ct][cg][node] = def_prob[node];
238
276
                    }
239
277
 
 
278
    if (s->use_huffman) {
 
279
        for (pt=0; pt<2; pt++) {
 
280
            vp6_build_huff_tree(s, model->coeff_dccv[pt],
 
281
                                vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]);
 
282
            vp6_build_huff_tree(s, model->coeff_runv[pt],
 
283
                                vp6_huff_run_map, 9, &s->runv_vlc[pt]);
 
284
            for (ct=0; ct<3; ct++)
 
285
                for (cg = 0; cg < 6; cg++)
 
286
                    vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
 
287
                                        vp6_huff_coeff_map, 12,
 
288
                                        &s->ract_vlc[pt][ct][cg]);
 
289
        }
 
290
        memset(s->nb_null, 0, sizeof(s->nb_null));
 
291
    } else {
240
292
    /* coeff_dcct is a linear combination of coeff_dccv */
241
293
    for (pt=0; pt<2; pt++)
242
294
        for (ctx=0; ctx<3; ctx++)
243
295
            for (node=0; node<5; node++)
244
296
                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);
 
297
    }
245
298
}
246
299
 
247
 
static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
 
300
static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
248
301
{
249
 
    vp56_range_coder_t *c = &s->c;
250
 
    vp56_model_t *model = s->modelp;
 
302
    VP56RangeCoder *c = &s->c;
 
303
    VP56Model *model = s->modelp;
251
304
    int comp;
252
305
 
253
 
    *vect = (vp56_mv_t) {0,0};
 
306
    *vect = (VP56mv) {0,0};
254
307
    if (s->vector_candidate_pos < 2)
255
308
        *vect = s->vector_candidate[0];
256
309
 
282
335
    }
283
336
}
284
337
 
285
 
static void vp6_parse_coeff(vp56_context_t *s)
286
 
{
287
 
    vp56_range_coder_t *c = s->ccp;
288
 
    vp56_model_t *model = s->modelp;
 
338
/**
 
339
 * Read number of consecutive blocks with null DC or AC.
 
340
 * This value is < 74.
 
341
 */
 
342
static unsigned vp6_get_nb_null(VP56Context *s)
 
343
{
 
344
    unsigned val = get_bits(&s->gb, 2);
 
345
    if (val == 2)
 
346
        val += get_bits(&s->gb, 2);
 
347
    else if (val == 3) {
 
348
        val = get_bits1(&s->gb) << 2;
 
349
        val = 6+val + get_bits(&s->gb, 2+val);
 
350
    }
 
351
    return val;
 
352
}
 
353
 
 
354
static void vp6_parse_coeff_huffman(VP56Context *s)
 
355
{
 
356
    VP56Model *model = s->modelp;
 
357
    uint8_t *permute = s->scantable.permutated;
 
358
    VLC *vlc_coeff;
 
359
    int coeff, sign, coeff_idx;
 
360
    int b, cg, idx;
 
361
    int pt = 0;    /* plane type (0 for Y, 1 for U or V) */
 
362
 
 
363
    for (b=0; b<6; b++) {
 
364
        int ct = 0;    /* code type */
 
365
        if (b > 3) pt = 1;
 
366
        vlc_coeff = &s->dccv_vlc[pt];
 
367
 
 
368
        for (coeff_idx=0; coeff_idx<64; ) {
 
369
            int run = 1;
 
370
            if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
 
371
                s->nb_null[coeff_idx][pt]--;
 
372
                if (coeff_idx)
 
373
                    break;
 
374
            } else {
 
375
                coeff = get_vlc2(&s->gb, vlc_coeff->table, 9, 3);
 
376
                if (coeff == 0) {
 
377
                    if (coeff_idx) {
 
378
                        int pt = (coeff_idx >= 6);
 
379
                        run += get_vlc2(&s->gb, s->runv_vlc[pt].table, 9, 3);
 
380
                        if (run >= 9)
 
381
                            run += get_bits(&s->gb, 6);
 
382
                    } else
 
383
                        s->nb_null[0][pt] = vp6_get_nb_null(s);
 
384
                    ct = 0;
 
385
                } else if (coeff == 11) {  /* end of block */
 
386
                    if (coeff_idx == 1)    /* first AC coeff ? */
 
387
                        s->nb_null[1][pt] = vp6_get_nb_null(s);
 
388
                    break;
 
389
                } else {
 
390
                    int coeff2 = vp56_coeff_bias[coeff];
 
391
                    if (coeff > 4)
 
392
                        coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
 
393
                    ct = 1 + (coeff2 > 1);
 
394
                    sign = get_bits1(&s->gb);
 
395
                    coeff2 = (coeff2 ^ -sign) + sign;
 
396
                    if (coeff_idx)
 
397
                        coeff2 *= s->dequant_ac;
 
398
                    idx = model->coeff_index_to_pos[coeff_idx];
 
399
                    s->block_coeff[b][permute[idx]] = coeff2;
 
400
                }
 
401
            }
 
402
            coeff_idx+=run;
 
403
            cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
 
404
            vlc_coeff = &s->ract_vlc[pt][ct][cg];
 
405
        }
 
406
    }
 
407
}
 
408
 
 
409
static void vp6_parse_coeff(VP56Context *s)
 
410
{
 
411
    VP56RangeCoder *c = s->ccp;
 
412
    VP56Model *model = s->modelp;
289
413
    uint8_t *permute = s->scantable.permutated;
290
414
    uint8_t *model1, *model2, *model3;
291
415
    int coeff, sign, coeff_idx;
309
433
                if (vp56_rac_get_prob(c, model2[2])) {
310
434
                    if (vp56_rac_get_prob(c, model2[3])) {
311
435
                        idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
312
 
                        coeff = vp56_coeff_bias[idx];
 
436
                        coeff = vp56_coeff_bias[idx+5];
313
437
                        for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
314
438
                            coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
315
439
                    } else {
399
523
    }
400
524
}
401
525
 
402
 
static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
 
526
static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
403
527
                             int stride, int h_weight, int v_weight)
404
528
{
405
529
    uint8_t *tmp = s->edge_emu_buffer+16;
407
531
    s->dsp.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
408
532
}
409
533
 
410
 
static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
411
 
                             const int16_t *h_weights,const int16_t *v_weights)
412
 
{
413
 
    int x, y;
414
 
    int tmp[8*11];
415
 
    int *t = tmp;
416
 
 
417
 
    src -= stride;
418
 
 
419
 
    for (y=0; y<11; y++) {
420
 
        for (x=0; x<8; x++) {
421
 
            t[x] = av_clip_uint8((  src[x-1] * h_weights[0]
422
 
                               + src[x  ] * h_weights[1]
423
 
                               + src[x+1] * h_weights[2]
424
 
                               + src[x+2] * h_weights[3] + 64) >> 7);
425
 
        }
426
 
        src += stride;
427
 
        t += 8;
428
 
    }
429
 
 
430
 
    t = tmp + 8;
431
 
    for (y=0; y<8; y++) {
432
 
        for (x=0; x<8; x++) {
433
 
            dst[x] = av_clip_uint8((  t[x-8 ] * v_weights[0]
434
 
                                 + t[x   ] * v_weights[1]
435
 
                                 + t[x+8 ] * v_weights[2]
436
 
                                 + t[x+16] * v_weights[3] + 64) >> 7);
437
 
        }
438
 
        dst += stride;
439
 
        t += 8;
440
 
    }
441
 
}
442
 
 
443
 
static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
 
534
static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
444
535
                       int offset1, int offset2, int stride,
445
 
                       vp56_mv_t mv, int mask, int select, int luma)
 
536
                       VP56mv mv, int mask, int select, int luma)
446
537
{
447
538
    int filter4 = 0;
448
539
    int x8 = mv.x & mask;
477
568
            vp6_filter_hv4(dst, src+offset1, stride, stride,
478
569
                           vp6_block_copy_filter[select][y8]);
479
570
        } else {
480
 
            vp6_filter_diag4(dst, src+offset1 + ((mv.x^mv.y)>>31), stride,
 
571
            s->dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
481
572
                             vp6_block_copy_filter[select][x8],
482
573
                             vp6_block_copy_filter[select][y8]);
483
574
        }
490
581
    }
491
582
}
492
583
 
493
 
static int vp6_decode_init(AVCodecContext *avctx)
 
584
static av_cold int vp6_decode_init(AVCodecContext *avctx)
494
585
{
495
 
    vp56_context_t *s = avctx->priv_data;
 
586
    VP56Context *s = avctx->priv_data;
496
587
 
497
588
    vp56_init(avctx, avctx->codec->id == CODEC_ID_VP6,
498
589
                     avctx->codec->id == CODEC_ID_VP6A);
500
591
    s->parse_vector_adjustment = vp6_parse_vector_adjustment;
501
592
    s->adjust = vp6_adjust;
502
593
    s->filter = vp6_filter;
503
 
    s->parse_coeff = vp6_parse_coeff;
504
594
    s->default_models_init = vp6_default_models_init;
505
595
    s->parse_vector_models = vp6_parse_vector_models;
506
596
    s->parse_coeff_models = vp6_parse_coeff_models;
513
603
    "vp6",
514
604
    CODEC_TYPE_VIDEO,
515
605
    CODEC_ID_VP6,
516
 
    sizeof(vp56_context_t),
 
606
    sizeof(VP56Context),
517
607
    vp6_decode_init,
518
608
    NULL,
519
609
    vp56_free,
520
610
    vp56_decode_frame,
521
611
    CODEC_CAP_DR1,
 
612
    .long_name = NULL_IF_CONFIG_SMALL("On2 VP6"),
522
613
};
523
614
 
524
615
/* flash version, not flipped upside-down */
526
617
    "vp6f",
527
618
    CODEC_TYPE_VIDEO,
528
619
    CODEC_ID_VP6F,
529
 
    sizeof(vp56_context_t),
 
620
    sizeof(VP56Context),
530
621
    vp6_decode_init,
531
622
    NULL,
532
623
    vp56_free,
533
624
    vp56_decode_frame,
534
625
    CODEC_CAP_DR1,
 
626
    .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version)"),
535
627
};
536
628
 
537
629
/* flash version, not flipped upside-down, with alpha channel */
539
631
    "vp6a",
540
632
    CODEC_TYPE_VIDEO,
541
633
    CODEC_ID_VP6A,
542
 
    sizeof(vp56_context_t),
 
634
    sizeof(VP56Context),
543
635
    vp6_decode_init,
544
636
    NULL,
545
637
    vp56_free,
546
638
    vp56_decode_frame,
547
639
    CODEC_CAP_DR1,
 
640
    .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version, with alpha channel)"),
548
641
};