~ubuntu-branches/ubuntu/utopic/libav/utopic-proposed

« back to all changes in this revision

Viewing changes to libavcodec/vp8.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler, Reinhard Tartler, Rico Tzschichholz
  • Date: 2014-08-30 11:02:45 UTC
  • mfrom: (1.3.47 sid)
  • Revision ID: package-import@ubuntu.com-20140830110245-io3dg7q85wfr7125
Tags: 6:11~beta1-2
[ Reinhard Tartler ]
* Make libavcodec-dev depend on libavresample-dev

[ Rico Tzschichholz ]
* Some fixes and leftovers from soname bumps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * VP8 compatible video decoder
 
2
 * VP7/VP8 compatible video decoder
3
3
 *
4
4
 * Copyright (C) 2010 David Conrad
5
5
 * Copyright (C) 2010 Ronald S. Bultje
6
 
 * Copyright (C) 2010 Jason Garrett-Glaser
 
6
 * Copyright (C) 2010 Fiona Glaser
7
7
 * Copyright (C) 2012 Daniel Kang
 
8
 * Copyright (C) 2014 Peter Ross
8
9
 *
9
10
 * This file is part of Libav.
10
11
 *
24
25
 */
25
26
 
26
27
#include "libavutil/imgutils.h"
 
28
 
27
29
#include "avcodec.h"
28
30
#include "internal.h"
 
31
#include "rectangle.h"
 
32
#include "thread.h"
29
33
#include "vp8.h"
30
34
#include "vp8data.h"
31
 
#include "rectangle.h"
32
 
#include "thread.h"
33
35
 
34
36
#if ARCH_ARM
35
37
#   include "arm/vp8.h"
74
76
    ff_thread_release_buffer(s->avctx, &f->tf);
75
77
}
76
78
 
 
79
#if CONFIG_VP8_DECODER
77
80
static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, VP8Frame *src)
78
81
{
79
82
    int ret;
90
93
 
91
94
    return 0;
92
95
}
93
 
 
 
96
#endif /* CONFIG_VP8_DECODER */
94
97
 
95
98
static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
96
99
{
110
113
    vp8_decode_flush_impl(avctx, 0);
111
114
}
112
115
 
113
 
static int update_dimensions(VP8Context *s, int width, int height)
 
116
static VP8Frame *vp8_find_free_buffer(VP8Context *s)
 
117
{
 
118
    VP8Frame *frame = NULL;
 
119
    int i;
 
120
 
 
121
    // find a free buffer
 
122
    for (i = 0; i < 5; i++)
 
123
        if (&s->frames[i] != s->framep[VP56_FRAME_CURRENT]  &&
 
124
            &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
 
125
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN]   &&
 
126
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
 
127
            frame = &s->frames[i];
 
128
            break;
 
129
        }
 
130
    if (i == 5) {
 
131
        av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
 
132
        abort();
 
133
    }
 
134
    if (frame->tf.f->data[0])
 
135
        vp8_release_frame(s, frame);
 
136
 
 
137
    return frame;
 
138
}
 
139
 
 
140
static av_always_inline
 
141
int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
114
142
{
115
143
    AVCodecContext *avctx = s->avctx;
116
144
    int i, ret;
124
152
            return ret;
125
153
    }
126
154
 
127
 
    s->mb_width  = (s->avctx->coded_width +15) / 16;
128
 
    s->mb_height = (s->avctx->coded_height+15) / 16;
 
155
    s->mb_width  = (s->avctx->coded_width  + 15) / 16;
 
156
    s->mb_height = (s->avctx->coded_height + 15) / 16;
129
157
 
130
 
    s->mb_layout = (avctx->active_thread_type == FF_THREAD_SLICE) && (FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1);
 
158
    s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
 
159
                   FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1;
131
160
    if (!s->mb_layout) { // Frame threading and one thread
132
 
        s->macroblocks_base       = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
133
 
        s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
134
 
    }
135
 
    else // Sliced threading
136
 
        s->macroblocks_base       = av_mallocz((s->mb_width+2)*(s->mb_height+2)*sizeof(*s->macroblocks));
137
 
    s->top_nnz                    = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
138
 
    s->top_border                 = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
139
 
    s->thread_data                = av_mallocz(MAX_THREADS*sizeof(VP8ThreadData));
 
161
        s->macroblocks_base       = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
 
162
                                               sizeof(*s->macroblocks));
 
163
        s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
 
164
    } else // Sliced threading
 
165
        s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
 
166
                                         sizeof(*s->macroblocks));
 
167
    s->top_nnz     = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
 
168
    s->top_border  = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
 
169
    s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
140
170
 
141
171
    for (i = 0; i < MAX_THREADS; i++) {
142
 
        s->thread_data[i].filter_strength = av_mallocz(s->mb_width*sizeof(*s->thread_data[0].filter_strength));
 
172
        s->thread_data[i].filter_strength =
 
173
            av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
143
174
#if HAVE_THREADS
144
175
        pthread_mutex_init(&s->thread_data[i].lock, NULL);
145
176
        pthread_cond_init(&s->thread_data[i].cond, NULL);
150
181
        (!s->intra4x4_pred_mode_top && !s->mb_layout))
151
182
        return AVERROR(ENOMEM);
152
183
 
153
 
    s->macroblocks        = s->macroblocks_base + 1;
 
184
    s->macroblocks = s->macroblocks_base + 1;
154
185
 
155
186
    return 0;
156
187
}
157
188
 
 
189
static int vp7_update_dimensions(VP8Context *s, int width, int height)
 
190
{
 
191
    return update_dimensions(s, width, height, IS_VP7);
 
192
}
 
193
 
 
194
static int vp8_update_dimensions(VP8Context *s, int width, int height)
 
195
{
 
196
    return update_dimensions(s, width, height, IS_VP8);
 
197
}
 
198
 
158
199
static void parse_segment_info(VP8Context *s)
159
200
{
160
201
    VP56RangeCoder *c = &s->c;
207
248
 
208
249
    s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
209
250
 
210
 
    buf      += 3*(s->num_coeff_partitions-1);
211
 
    buf_size -= 3*(s->num_coeff_partitions-1);
 
251
    buf      += 3 * (s->num_coeff_partitions - 1);
 
252
    buf_size -= 3 * (s->num_coeff_partitions - 1);
212
253
    if (buf_size < 0)
213
254
        return -1;
214
255
 
215
 
    for (i = 0; i < s->num_coeff_partitions-1; i++) {
216
 
        int size = AV_RL24(sizes + 3*i);
 
256
    for (i = 0; i < s->num_coeff_partitions - 1; i++) {
 
257
        int size = AV_RL24(sizes + 3 * i);
217
258
        if (buf_size - size < 0)
218
259
            return -1;
219
260
 
226
267
    return 0;
227
268
}
228
269
 
 
270
static void vp7_get_quants(VP8Context *s)
 
271
{
 
272
    VP56RangeCoder *c = &s->c;
 
273
 
 
274
    int yac_qi  = vp8_rac_get_uint(c, 7);
 
275
    int ydc_qi  = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
 
276
    int y2dc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
 
277
    int y2ac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
 
278
    int uvdc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
 
279
    int uvac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
 
280
 
 
281
    s->qmat[0].luma_qmul[0]    =       vp7_ydc_qlookup[ydc_qi];
 
282
    s->qmat[0].luma_qmul[1]    =       vp7_yac_qlookup[yac_qi];
 
283
    s->qmat[0].luma_dc_qmul[0] =       vp7_y2dc_qlookup[y2dc_qi];
 
284
    s->qmat[0].luma_dc_qmul[1] =       vp7_y2ac_qlookup[y2ac_qi];
 
285
    s->qmat[0].chroma_qmul[0]  = FFMIN(vp7_ydc_qlookup[uvdc_qi], 132);
 
286
    s->qmat[0].chroma_qmul[1]  =       vp7_yac_qlookup[uvac_qi];
 
287
}
 
288
 
229
289
static void get_quants(VP8Context *s)
230
290
{
231
291
    VP56RangeCoder *c = &s->c;
246
306
        } else
247
307
            base_qi = yac_qi;
248
308
 
249
 
        s->qmat[i].luma_qmul[0]    =           vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta , 7)];
250
 
        s->qmat[i].luma_qmul[1]    =           vp8_ac_qlookup[av_clip_uintp2(base_qi             , 7)];
251
 
        s->qmat[i].luma_dc_qmul[0] =       2 * vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)];
 
309
        s->qmat[i].luma_qmul[0]    = vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta,  7)];
 
310
        s->qmat[i].luma_qmul[1]    = vp8_ac_qlookup[av_clip_uintp2(base_qi,              7)];
 
311
        s->qmat[i].luma_dc_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)] * 2;
252
312
        /* 101581>>16 is equivalent to 155/100 */
253
 
        s->qmat[i].luma_dc_qmul[1] = (101581 * vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)]) >> 16;
254
 
        s->qmat[i].chroma_qmul[0]  =           vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
255
 
        s->qmat[i].chroma_qmul[1]  =           vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
 
313
        s->qmat[i].luma_dc_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)] * 101581 >> 16;
 
314
        s->qmat[i].chroma_qmul[0]  = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
 
315
        s->qmat[i].chroma_qmul[1]  = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
256
316
 
257
317
        s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
258
318
        s->qmat[i].chroma_qmul[0]  = FFMIN(s->qmat[i].chroma_qmul[0], 132);
288
348
    return VP56_FRAME_NONE;
289
349
}
290
350
 
 
351
static void vp78_reset_probability_tables(VP8Context *s)
 
352
{
 
353
    int i, j;
 
354
    for (i = 0; i < 4; i++)
 
355
        for (j = 0; j < 16; j++)
 
356
            memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
 
357
                   sizeof(s->prob->token[i][j]));
 
358
}
 
359
 
 
360
static void vp78_update_probability_tables(VP8Context *s)
 
361
{
 
362
    VP56RangeCoder *c = &s->c;
 
363
    int i, j, k, l, m;
 
364
 
 
365
    for (i = 0; i < 4; i++)
 
366
        for (j = 0; j < 8; j++)
 
367
            for (k = 0; k < 3; k++)
 
368
                for (l = 0; l < NUM_DCT_TOKENS-1; l++)
 
369
                    if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
 
370
                        int prob = vp8_rac_get_uint(c, 8);
 
371
                        for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
 
372
                            s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
 
373
                    }
 
374
}
 
375
 
 
376
#define VP7_MVC_SIZE 17
 
377
#define VP8_MVC_SIZE 19
 
378
 
 
379
static void vp78_update_pred16x16_pred8x8_mvc_probabilities(VP8Context *s,
 
380
                                                            int mvc_size)
 
381
{
 
382
    VP56RangeCoder *c = &s->c;
 
383
    int i, j;
 
384
 
 
385
    if (vp8_rac_get(c))
 
386
        for (i = 0; i < 4; i++)
 
387
            s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
 
388
    if (vp8_rac_get(c))
 
389
        for (i = 0; i < 3; i++)
 
390
            s->prob->pred8x8c[i]  = vp8_rac_get_uint(c, 8);
 
391
 
 
392
    // 17.2 MV probability update
 
393
    for (i = 0; i < 2; i++)
 
394
        for (j = 0; j < mvc_size; j++)
 
395
            if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
 
396
                s->prob->mvc[i][j] = vp8_rac_get_nn(c);
 
397
}
 
398
 
291
399
static void update_refs(VP8Context *s)
292
400
{
293
401
    VP56RangeCoder *c = &s->c;
299
407
    s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
300
408
}
301
409
 
302
 
static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
303
 
{
304
 
    VP56RangeCoder *c = &s->c;
305
 
    int header_size, hscale, vscale, i, j, k, l, m, ret;
 
410
static void copy_luma(AVFrame *dst, AVFrame *src, int width, int height)
 
411
{
 
412
    int i, j;
 
413
 
 
414
    for (j = 1; j < 3; j++) {
 
415
        for (i = 0; i < height / 2; i++)
 
416
            memcpy(dst->data[j] + i * dst->linesize[j],
 
417
                   src->data[j] + i * src->linesize[j], width / 2);
 
418
    }
 
419
}
 
420
 
 
421
static void fade(uint8_t *dst, uint8_t *src,
 
422
                 int width, int height, int linesize,
 
423
                 int alpha, int beta)
 
424
{
 
425
    int i, j;
 
426
 
 
427
    for (j = 0; j < height; j++) {
 
428
        for (i = 0; i < width; i++) {
 
429
            uint8_t y = src[j * linesize + i];
 
430
            dst[j * linesize + i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
 
431
        }
 
432
    }
 
433
}
 
434
 
 
435
static int vp7_fade_frame(VP8Context *s, VP56RangeCoder *c)
 
436
{
 
437
    int alpha = (int8_t) vp8_rac_get_uint(c, 8);
 
438
    int beta  = (int8_t) vp8_rac_get_uint(c, 8);
 
439
    int ret;
 
440
 
 
441
    if (!s->keyframe && (alpha || beta)) {
 
442
        int width  = s->mb_width * 16;
 
443
        int height = s->mb_height * 16;
 
444
        AVFrame *src, *dst;
 
445
 
 
446
        if (!s->framep[VP56_FRAME_PREVIOUS])
 
447
            return AVERROR_INVALIDDATA;
 
448
 
 
449
        dst =
 
450
        src = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
 
451
 
 
452
        /* preserve the golden frame, write a new previous frame */
 
453
        if (s->framep[VP56_FRAME_GOLDEN] == s->framep[VP56_FRAME_PREVIOUS]) {
 
454
            s->framep[VP56_FRAME_PREVIOUS] = vp8_find_free_buffer(s);
 
455
            if ((ret = vp8_alloc_frame(s, s->framep[VP56_FRAME_PREVIOUS], 1)) < 0)
 
456
               return ret;
 
457
 
 
458
            dst = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
 
459
 
 
460
            copy_luma(dst, src, width, height);
 
461
        }
 
462
 
 
463
        fade(dst->data[0], src->data[0],
 
464
             width, height, dst->linesize[0], alpha, beta);
 
465
    }
 
466
 
 
467
    return 0;
 
468
}
 
469
 
 
470
static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
 
471
{
 
472
    VP56RangeCoder *c = &s->c;
 
473
    int part1_size, hscale, vscale, i, j, ret;
 
474
    int width  = s->avctx->width;
 
475
    int height = s->avctx->height;
 
476
 
 
477
    s->profile = (buf[0] >> 1) & 7;
 
478
    if (s->profile > 1) {
 
479
        avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
 
480
        return AVERROR_INVALIDDATA;
 
481
    }
 
482
 
 
483
    s->keyframe  = !(buf[0] & 1);
 
484
    s->invisible = 0;
 
485
    part1_size   = AV_RL24(buf) >> 4;
 
486
 
 
487
    buf      += 4 - s->profile;
 
488
    buf_size -= 4 - s->profile;
 
489
 
 
490
    memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
 
491
 
 
492
    ff_vp56_init_range_decoder(c, buf, part1_size);
 
493
    buf      += part1_size;
 
494
    buf_size -= part1_size;
 
495
 
 
496
    /* A. Dimension information (keyframes only) */
 
497
    if (s->keyframe) {
 
498
        width  = vp8_rac_get_uint(c, 12);
 
499
        height = vp8_rac_get_uint(c, 12);
 
500
        hscale = vp8_rac_get_uint(c, 2);
 
501
        vscale = vp8_rac_get_uint(c, 2);
 
502
        if (hscale || vscale)
 
503
            avpriv_request_sample(s->avctx, "Upscaling");
 
504
 
 
505
        s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
 
506
        vp78_reset_probability_tables(s);
 
507
        memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
 
508
               sizeof(s->prob->pred16x16));
 
509
        memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
 
510
               sizeof(s->prob->pred8x8c));
 
511
        for (i = 0; i < 2; i++)
 
512
            memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
 
513
                   sizeof(vp7_mv_default_prob[i]));
 
514
        memset(&s->segmentation, 0, sizeof(s->segmentation));
 
515
        memset(&s->lf_delta, 0, sizeof(s->lf_delta));
 
516
        memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
 
517
    }
 
518
 
 
519
    if (s->keyframe || s->profile > 0)
 
520
        memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
 
521
 
 
522
    /* B. Decoding information for all four macroblock-level features */
 
523
    for (i = 0; i < 4; i++) {
 
524
        s->feature_enabled[i] = vp8_rac_get(c);
 
525
        if (s->feature_enabled[i]) {
 
526
             s->feature_present_prob[i] = vp8_rac_get_uint(c, 8);
 
527
 
 
528
             for (j = 0; j < 3; j++)
 
529
                 s->feature_index_prob[i][j] =
 
530
                     vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
 
531
 
 
532
             if (vp7_feature_value_size[i])
 
533
                 for (j = 0; j < 4; j++)
 
534
                     s->feature_value[i][j] =
 
535
                         vp8_rac_get(c) ? vp8_rac_get_uint(c, vp7_feature_value_size[s->profile][i]) : 0;
 
536
        }
 
537
    }
 
538
 
 
539
    s->segmentation.enabled    = 0;
 
540
    s->segmentation.update_map = 0;
 
541
    s->lf_delta.enabled        = 0;
 
542
 
 
543
    s->num_coeff_partitions = 1;
 
544
    ff_vp56_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
 
545
 
 
546
    if (!s->macroblocks_base || /* first frame */
 
547
        width != s->avctx->width || height != s->avctx->height ||
 
548
        (width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
 
549
        if ((ret = vp7_update_dimensions(s, width, height)) < 0)
 
550
            return ret;
 
551
    }
 
552
 
 
553
    /* C. Dequantization indices */
 
554
    vp7_get_quants(s);
 
555
 
 
556
    /* D. Golden frame update flag (a Flag) for interframes only */
 
557
    if (!s->keyframe) {
 
558
        s->update_golden = vp8_rac_get(c) ? VP56_FRAME_CURRENT : VP56_FRAME_NONE;
 
559
        s->sign_bias[VP56_FRAME_GOLDEN] = 0;
 
560
    }
 
561
 
 
562
    s->update_last          = 1;
 
563
    s->update_probabilities = 1;
 
564
    s->fade_present         = 1;
 
565
 
 
566
    if (s->profile > 0) {
 
567
        s->update_probabilities = vp8_rac_get(c);
 
568
        if (!s->update_probabilities)
 
569
            s->prob[1] = s->prob[0];
 
570
 
 
571
        if (!s->keyframe)
 
572
            s->fade_present = vp8_rac_get(c);
 
573
    }
 
574
 
 
575
    /* E. Fading information for previous frame */
 
576
    if (s->fade_present && vp8_rac_get(c)) {
 
577
        if ((ret = vp7_fade_frame(s ,c)) < 0)
 
578
            return ret;
 
579
    }
 
580
 
 
581
    /* F. Loop filter type */
 
582
    if (!s->profile)
 
583
        s->filter.simple = vp8_rac_get(c);
 
584
 
 
585
    /* G. DCT coefficient ordering specification */
 
586
    if (vp8_rac_get(c))
 
587
        for (i = 1; i < 16; i++)
 
588
            s->prob[0].scan[i] = zigzag_scan[vp8_rac_get_uint(c, 4)];
 
589
 
 
590
    /* H. Loop filter levels  */
 
591
    if (s->profile > 0)
 
592
        s->filter.simple = vp8_rac_get(c);
 
593
    s->filter.level     = vp8_rac_get_uint(c, 6);
 
594
    s->filter.sharpness = vp8_rac_get_uint(c, 3);
 
595
 
 
596
    /* I. DCT coefficient probability update; 13.3 Token Probability Updates */
 
597
    vp78_update_probability_tables(s);
 
598
 
 
599
    s->mbskip_enabled = 0;
 
600
 
 
601
    /* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
 
602
    if (!s->keyframe) {
 
603
        s->prob->intra  = vp8_rac_get_uint(c, 8);
 
604
        s->prob->last   = vp8_rac_get_uint(c, 8);
 
605
        vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP7_MVC_SIZE);
 
606
    }
 
607
 
 
608
    return 0;
 
609
}
 
610
 
 
611
static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
 
612
{
 
613
    VP56RangeCoder *c = &s->c;
 
614
    int header_size, hscale, vscale, ret;
306
615
    int width  = s->avctx->width;
307
616
    int height = s->avctx->height;
308
617
 
317
626
        av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
318
627
 
319
628
    if (!s->profile)
320
 
        memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
 
629
        memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
 
630
               sizeof(s->put_pixels_tab));
321
631
    else    // profile 1-3 use bilinear, 4+ aren't defined so whatever
322
 
        memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
 
632
        memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
 
633
               sizeof(s->put_pixels_tab));
323
634
 
324
 
    if (header_size > buf_size - 7*s->keyframe) {
 
635
    if (header_size > buf_size - 7 * s->keyframe) {
325
636
        av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
326
637
        return AVERROR_INVALIDDATA;
327
638
    }
328
639
 
329
640
    if (s->keyframe) {
330
641
        if (AV_RL24(buf) != 0x2a019d) {
331
 
            av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
 
642
            av_log(s->avctx, AV_LOG_ERROR,
 
643
                   "Invalid start code 0x%x\n", AV_RL24(buf));
332
644
            return AVERROR_INVALIDDATA;
333
645
        }
334
 
        width  = AV_RL16(buf+3) & 0x3fff;
335
 
        height = AV_RL16(buf+5) & 0x3fff;
336
 
        hscale = buf[4] >> 6;
337
 
        vscale = buf[6] >> 6;
 
646
        width     = AV_RL16(buf + 3) & 0x3fff;
 
647
        height    = AV_RL16(buf + 5) & 0x3fff;
 
648
        hscale    = buf[4] >> 6;
 
649
        vscale    = buf[6] >> 6;
338
650
        buf      += 7;
339
651
        buf_size -= 7;
340
652
 
342
654
            avpriv_request_sample(s->avctx, "Upscaling");
343
655
 
344
656
        s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
345
 
        for (i = 0; i < 4; i++)
346
 
            for (j = 0; j < 16; j++)
347
 
                memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
348
 
                       sizeof(s->prob->token[i][j]));
349
 
        memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
350
 
        memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
351
 
        memcpy(s->prob->mvc      , vp8_mv_default_prob     , sizeof(s->prob->mvc));
 
657
        vp78_reset_probability_tables(s);
 
658
        memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
 
659
               sizeof(s->prob->pred16x16));
 
660
        memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
 
661
               sizeof(s->prob->pred8x8c));
 
662
        memcpy(s->prob->mvc, vp8_mv_default_prob,
 
663
               sizeof(s->prob->mvc));
352
664
        memset(&s->segmentation, 0, sizeof(s->segmentation));
353
665
        memset(&s->lf_delta, 0, sizeof(s->lf_delta));
354
666
    }
382
694
    }
383
695
 
384
696
    if (!s->macroblocks_base || /* first frame */
385
 
        width != s->avctx->width || height != s->avctx->height) {
386
 
        if ((ret = update_dimensions(s, width, height)) < 0)
 
697
        width != s->avctx->width || height != s->avctx->height)
 
698
        if ((ret = vp8_update_dimensions(s, width, height)) < 0)
387
699
            return ret;
388
 
    }
389
700
 
390
701
    get_quants(s);
391
702
 
402
713
 
403
714
    s->update_last = s->keyframe || vp8_rac_get(c);
404
715
 
405
 
    for (i = 0; i < 4; i++)
406
 
        for (j = 0; j < 8; j++)
407
 
            for (k = 0; k < 3; k++)
408
 
                for (l = 0; l < NUM_DCT_TOKENS-1; l++)
409
 
                    if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
410
 
                        int prob = vp8_rac_get_uint(c, 8);
411
 
                        for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
412
 
                            s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
413
 
                    }
 
716
    vp78_update_probability_tables(s);
414
717
 
415
718
    if ((s->mbskip_enabled = vp8_rac_get(c)))
416
719
        s->prob->mbskip = vp8_rac_get_uint(c, 8);
419
722
        s->prob->intra  = vp8_rac_get_uint(c, 8);
420
723
        s->prob->last   = vp8_rac_get_uint(c, 8);
421
724
        s->prob->golden = vp8_rac_get_uint(c, 8);
422
 
 
423
 
        if (vp8_rac_get(c))
424
 
            for (i = 0; i < 4; i++)
425
 
                s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
426
 
        if (vp8_rac_get(c))
427
 
            for (i = 0; i < 3; i++)
428
 
                s->prob->pred8x8c[i]  = vp8_rac_get_uint(c, 8);
429
 
 
430
 
        // 17.2 MV probability update
431
 
        for (i = 0; i < 2; i++)
432
 
            for (j = 0; j < 19; j++)
433
 
                if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
434
 
                    s->prob->mvc[i][j] = vp8_rac_get_nn(c);
 
725
        vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP8_MVC_SIZE);
435
726
    }
436
727
 
437
728
    return 0;
438
729
}
439
730
 
440
 
static av_always_inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
 
731
static av_always_inline
 
732
void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
441
733
{
442
734
    dst->x = av_clip(src->x, s->mv_min.x, s->mv_max.x);
443
735
    dst->y = av_clip(src->y, s->mv_min.y, s->mv_max.y);
446
738
/**
447
739
 * Motion vector coding, 17.1.
448
740
 */
449
 
static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
 
741
static int read_mv_component(VP56RangeCoder *c, const uint8_t *p, int vp7)
450
742
{
451
743
    int bit, x = 0;
452
744
 
455
747
 
456
748
        for (i = 0; i < 3; i++)
457
749
            x += vp56_rac_get_prob(c, p[9 + i]) << i;
458
 
        for (i = 9; i > 3; i--)
 
750
        for (i = (vp7 ? 7 : 9); i > 3; i--)
459
751
            x += vp56_rac_get_prob(c, p[9 + i]) << i;
460
 
        if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
 
752
        if (!(x & (vp7 ? 0xF0 : 0xFFF0)) || vp56_rac_get_prob(c, p[12]))
461
753
            x += 8;
462
754
    } else {
463
755
        // small_mvtree
464
 
        const uint8_t *ps = p+2;
 
756
        const uint8_t *ps = p + 2;
465
757
        bit = vp56_rac_get_prob(c, *ps);
466
 
        ps += 1 + 3*bit;
467
 
        x  += 4*bit;
 
758
        ps += 1 + 3 * bit;
 
759
        x  += 4 * bit;
468
760
        bit = vp56_rac_get_prob(c, *ps);
469
761
        ps += 1 + bit;
470
 
        x  += 2*bit;
 
762
        x  += 2 * bit;
471
763
        x  += vp56_rac_get_prob(c, *ps);
472
764
    }
473
765
 
475
767
}
476
768
 
477
769
static av_always_inline
478
 
const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
 
770
const uint8_t *get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
479
771
{
 
772
    if (is_vp7)
 
773
        return vp7_submv_prob;
 
774
 
480
775
    if (left == top)
481
 
        return vp8_submv_prob[4-!!left];
 
776
        return vp8_submv_prob[4 - !!left];
482
777
    if (!top)
483
778
        return vp8_submv_prob[2];
484
 
    return vp8_submv_prob[1-!!left];
 
779
    return vp8_submv_prob[1 - !!left];
485
780
}
486
781
 
487
782
/**
489
784
 * @returns the number of motion vectors parsed (2, 4 or 16)
490
785
 */
491
786
static av_always_inline
492
 
int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb, int layout)
 
787
int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
 
788
                    int layout, int is_vp7)
493
789
{
494
790
    int part_idx;
495
791
    int n, num;
496
792
    VP8Macroblock *top_mb;
497
793
    VP8Macroblock *left_mb = &mb[-1];
498
 
    const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
499
 
                  *mbsplits_top,
500
 
                  *mbsplits_cur, *firstidx;
 
794
    const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning];
 
795
    const uint8_t *mbsplits_top, *mbsplits_cur, *firstidx;
501
796
    VP56mv *top_mv;
502
797
    VP56mv *left_mv = left_mb->bmv;
503
798
    VP56mv *cur_mv  = mb->bmv;
505
800
    if (!layout) // layout is inlined, s->mb_layout is not
506
801
        top_mb = &mb[2];
507
802
    else
508
 
        top_mb = &mb[-s->mb_width-1];
 
803
        top_mb = &mb[-s->mb_width - 1];
509
804
    mbsplits_top = vp8_mbsplits[top_mb->partitioning];
510
 
    top_mv = top_mb->bmv;
 
805
    top_mv       = top_mb->bmv;
511
806
 
512
807
    if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
513
 
        if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1])) {
 
808
        if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1]))
514
809
            part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
515
 
        } else {
 
810
        else
516
811
            part_idx = VP8_SPLITMVMODE_8x8;
517
 
        }
518
812
    } else {
519
813
        part_idx = VP8_SPLITMVMODE_4x4;
520
814
    }
521
815
 
522
 
    num = vp8_mbsplit_count[part_idx];
523
 
    mbsplits_cur = vp8_mbsplits[part_idx],
524
 
    firstidx = vp8_mbfirstidx[part_idx];
 
816
    num              = vp8_mbsplit_count[part_idx];
 
817
    mbsplits_cur     = vp8_mbsplits[part_idx],
 
818
    firstidx         = vp8_mbfirstidx[part_idx];
525
819
    mb->partitioning = part_idx;
526
820
 
527
821
    for (n = 0; n < num; n++) {
532
826
        if (!(k & 3))
533
827
            left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
534
828
        else
535
 
            left  = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
 
829
            left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
536
830
        if (k <= 3)
537
831
            above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
538
832
        else
539
833
            above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
540
834
 
541
 
        submv_prob = get_submv_prob(left, above);
 
835
        submv_prob = get_submv_prob(left, above, is_vp7);
542
836
 
543
837
        if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
544
838
            if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
545
839
                if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
546
 
                    mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
547
 
                    mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
 
840
                    mb->bmv[n].y = mb->mv.y +
 
841
                                   read_mv_component(c, s->prob->mvc[0], is_vp7);
 
842
                    mb->bmv[n].x = mb->mv.x +
 
843
                                   read_mv_component(c, s->prob->mvc[1], is_vp7);
548
844
                } else {
549
845
                    AV_ZERO32(&mb->bmv[n]);
550
846
                }
559
855
    return num;
560
856
}
561
857
 
562
 
static av_always_inline
563
 
void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
564
 
{
565
 
    VP8Macroblock *mb_edge[3] = { 0 /* top */,
 
858
/**
 
859
 * The vp7 reference decoder uses a padding macroblock column (added to right
 
860
 * edge of the frame) to guard against illegal macroblock offsets. The
 
861
 * algorithm has bugs that permit offsets to straddle the padding column.
 
862
 * This function replicates those bugs.
 
863
 *
 
864
 * @param[out] edge_x macroblock x address
 
865
 * @param[out] edge_y macroblock y address
 
866
 *
 
867
 * @return macroblock offset legal (boolean)
 
868
 */
 
869
static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
 
870
                                   int xoffset, int yoffset, int boundary,
 
871
                                   int *edge_x, int *edge_y)
 
872
{
 
873
    int vwidth = mb_width + 1;
 
874
    int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
 
875
    if (new < boundary || new % vwidth == vwidth - 1)
 
876
        return 0;
 
877
    *edge_y = new / vwidth;
 
878
    *edge_x = new % vwidth;
 
879
    return 1;
 
880
}
 
881
 
 
882
static const VP56mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
 
883
{
 
884
    return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
 
885
}
 
886
 
 
887
static av_always_inline
 
888
void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb,
 
889
                    int mb_x, int mb_y, int layout)
 
890
{
 
891
    VP8Macroblock *mb_edge[12];
 
892
    enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
 
893
    enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
 
894
    int idx = CNT_ZERO;
 
895
    VP56mv near_mv[3];
 
896
    uint8_t cnt[3] = { 0 };
 
897
    VP56RangeCoder *c = &s->c;
 
898
    int i;
 
899
 
 
900
    AV_ZERO32(&near_mv[0]);
 
901
    AV_ZERO32(&near_mv[1]);
 
902
    AV_ZERO32(&near_mv[2]);
 
903
 
 
904
    for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
 
905
        const VP7MVPred * pred = &vp7_mv_pred[i];
 
906
        int edge_x, edge_y;
 
907
 
 
908
        if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
 
909
                                    pred->yoffset, !s->profile, &edge_x, &edge_y)) {
 
910
            VP8Macroblock *edge = mb_edge[i] = (s->mb_layout == 1)
 
911
                                             ? s->macroblocks_base + 1 + edge_x +
 
912
                                               (s->mb_width + 1) * (edge_y + 1)
 
913
                                             : s->macroblocks + edge_x +
 
914
                                               (s->mb_height - edge_y - 1) * 2;
 
915
            uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
 
916
            if (mv) {
 
917
                if (AV_RN32A(&near_mv[CNT_NEAREST])) {
 
918
                    if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
 
919
                        idx = CNT_NEAREST;
 
920
                    } else if (AV_RN32A(&near_mv[CNT_NEAR])) {
 
921
                        if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
 
922
                            continue;
 
923
                        idx = CNT_NEAR;
 
924
                    } else {
 
925
                        AV_WN32A(&near_mv[CNT_NEAR], mv);
 
926
                        idx = CNT_NEAR;
 
927
                    }
 
928
                } else {
 
929
                    AV_WN32A(&near_mv[CNT_NEAREST], mv);
 
930
                    idx = CNT_NEAREST;
 
931
                }
 
932
            } else {
 
933
                idx = CNT_ZERO;
 
934
            }
 
935
        } else {
 
936
            idx = CNT_ZERO;
 
937
        }
 
938
        cnt[idx] += vp7_mv_pred[i].score;
 
939
    }
 
940
 
 
941
    mb->partitioning = VP8_SPLITMVMODE_NONE;
 
942
 
 
943
    if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
 
944
        mb->mode = VP8_MVMODE_MV;
 
945
 
 
946
        if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
 
947
 
 
948
            if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
 
949
 
 
950
                if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
 
951
                    AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
 
952
                else
 
953
                    AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR]    ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
 
954
 
 
955
                if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
 
956
                    mb->mode = VP8_MVMODE_SPLIT;
 
957
                    mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
 
958
                } else {
 
959
                    mb->mv.y += read_mv_component(c, s->prob->mvc[0], IS_VP7);
 
960
                    mb->mv.x += read_mv_component(c, s->prob->mvc[1], IS_VP7);
 
961
                    mb->bmv[0] = mb->mv;
 
962
                }
 
963
            } else {
 
964
                mb->mv = near_mv[CNT_NEAR];
 
965
                mb->bmv[0] = mb->mv;
 
966
            }
 
967
        } else {
 
968
            mb->mv = near_mv[CNT_NEAREST];
 
969
            mb->bmv[0] = mb->mv;
 
970
        }
 
971
    } else {
 
972
        mb->mode = VP8_MVMODE_ZERO;
 
973
        AV_ZERO32(&mb->mv);
 
974
        mb->bmv[0] = mb->mv;
 
975
    }
 
976
}
 
977
 
 
978
static av_always_inline
 
979
void vp8_decode_mvs(VP8Context *s, VP8Macroblock *mb,
 
980
                    int mb_x, int mb_y, int layout)
 
981
{
 
982
    VP8Macroblock *mb_edge[3] = { 0      /* top */,
566
983
                                  mb - 1 /* left */,
567
 
                                  0 /* top-left */ };
 
984
                                  0      /* top-left */ };
568
985
    enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
569
986
    enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
570
987
    int idx = CNT_ZERO;
577
994
    if (!layout) { // layout is inlined (s->mb_layout is not)
578
995
        mb_edge[0] = mb + 2;
579
996
        mb_edge[2] = mb + 1;
580
 
    }
581
 
    else {
582
 
        mb_edge[0] = mb - s->mb_width-1;
583
 
        mb_edge[2] = mb - s->mb_width-2;
 
997
    } else {
 
998
        mb_edge[0] = mb - s->mb_width - 1;
 
999
        mb_edge[2] = mb - s->mb_width - 2;
584
1000
    }
585
1001
 
586
1002
    AV_ZERO32(&near_mv[0]);
588
1004
    AV_ZERO32(&near_mv[2]);
589
1005
 
590
1006
    /* Process MB on top, left and top-left */
591
 
    #define MV_EDGE_CHECK(n)\
592
 
    {\
593
 
        VP8Macroblock *edge = mb_edge[n];\
594
 
        int edge_ref = edge->ref_frame;\
595
 
        if (edge_ref != VP56_FRAME_CURRENT) {\
596
 
            uint32_t mv = AV_RN32A(&edge->mv);\
597
 
            if (mv) {\
598
 
                if (cur_sign_bias != sign_bias[edge_ref]) {\
599
 
                    /* SWAR negate of the values in mv. */\
600
 
                    mv = ~mv;\
601
 
                    mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
602
 
                }\
603
 
                if (!n || mv != AV_RN32A(&near_mv[idx]))\
604
 
                    AV_WN32A(&near_mv[++idx], mv);\
605
 
                cnt[idx]      += 1 + (n != 2);\
606
 
            } else\
607
 
                cnt[CNT_ZERO] += 1 + (n != 2);\
608
 
        }\
 
1007
#define MV_EDGE_CHECK(n)                                                      \
 
1008
    {                                                                         \
 
1009
        VP8Macroblock *edge = mb_edge[n];                                     \
 
1010
        int edge_ref = edge->ref_frame;                                       \
 
1011
        if (edge_ref != VP56_FRAME_CURRENT) {                                 \
 
1012
            uint32_t mv = AV_RN32A(&edge->mv);                                \
 
1013
            if (mv) {                                                         \
 
1014
                if (cur_sign_bias != sign_bias[edge_ref]) {                   \
 
1015
                    /* SWAR negate of the values in mv. */                    \
 
1016
                    mv = ~mv;                                                 \
 
1017
                    mv = ((mv & 0x7fff7fff) +                                 \
 
1018
                          0x00010001) ^ (mv & 0x80008000);                    \
 
1019
                }                                                             \
 
1020
                if (!n || mv != AV_RN32A(&near_mv[idx]))                      \
 
1021
                    AV_WN32A(&near_mv[++idx], mv);                            \
 
1022
                cnt[idx] += 1 + (n != 2);                                     \
 
1023
            } else                                                            \
 
1024
                cnt[CNT_ZERO] += 1 + (n != 2);                                \
 
1025
        }                                                                     \
609
1026
    }
610
1027
 
611
1028
    MV_EDGE_CHECK(0)
617
1034
        mb->mode = VP8_MVMODE_MV;
618
1035
 
619
1036
        /* If we have three distinct MVs, merge first and last if they're the same */
620
 
        if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
 
1037
        if (cnt[CNT_SPLITMV] &&
 
1038
            AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
621
1039
            cnt[CNT_NEAREST] += 1;
622
1040
 
623
1041
        /* Swap near and nearest if necessary */
628
1046
 
629
1047
        if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
630
1048
            if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
631
 
 
632
1049
                /* Choose the best mv out of 0,0 and the nearest mv */
633
1050
                clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
634
1051
                cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode    == VP8_MVMODE_SPLIT) +
637
1054
 
638
1055
                if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
639
1056
                    mb->mode = VP8_MVMODE_SPLIT;
640
 
                    mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout) - 1];
 
1057
                    mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP8) - 1];
641
1058
                } else {
642
 
                    mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
643
 
                    mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
 
1059
                    mb->mv.y  += read_mv_component(c, s->prob->mvc[0], IS_VP8);
 
1060
                    mb->mv.x  += read_mv_component(c, s->prob->mvc[1], IS_VP8);
644
1061
                    mb->bmv[0] = mb->mv;
645
1062
                }
646
1063
            } else {
670
1087
    }
671
1088
    if (keyframe) {
672
1089
        int x, y;
673
 
        uint8_t* top;
674
 
        uint8_t* const left = s->intra4x4_pred_mode_left;
 
1090
        uint8_t *top;
 
1091
        uint8_t *const left = s->intra4x4_pred_mode_left;
675
1092
        if (layout == 1)
676
1093
            top = mb->intra4x4_pred_mode_top;
677
1094
        else
679
1096
        for (y = 0; y < 4; y++) {
680
1097
            for (x = 0; x < 4; x++) {
681
1098
                const uint8_t *ctx;
682
 
                ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
 
1099
                ctx       = vp8_pred4x4_prob_intra[top[x]][left[y]];
683
1100
                *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
684
 
                left[y] = top[x] = *intra4x4;
 
1101
                left[y]   = top[x] = *intra4x4;
685
1102
                intra4x4++;
686
1103
            }
687
1104
        }
688
1105
    } else {
689
1106
        int i;
690
1107
        for (i = 0; i < 16; i++)
691
 
            intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
 
1108
            intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree,
 
1109
                                           vp8_pred4x4_prob_inter);
692
1110
    }
693
1111
}
694
1112
 
695
1113
static av_always_inline
696
1114
void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
697
 
                    uint8_t *segment, uint8_t *ref, int layout)
 
1115
                    uint8_t *segment, uint8_t *ref, int layout, int is_vp7)
698
1116
{
699
1117
    VP56RangeCoder *c = &s->c;
700
 
 
701
 
    if (s->segmentation.update_map)
 
1118
    const char *vp7_feature_name[] = { "q-index",
 
1119
                                       "lf-delta",
 
1120
                                       "partial-golden-update",
 
1121
                                       "blit-pitch" };
 
1122
    if (is_vp7) {
 
1123
        int i;
 
1124
        *segment = 0;
 
1125
        for (i = 0; i < 4; i++) {
 
1126
            if (s->feature_enabled[i]) {
 
1127
                if (vp56_rac_get_prob(c, s->feature_present_prob[i])) {
 
1128
                      int index = vp8_rac_get_tree(c, vp7_feature_index_tree,
 
1129
                                                   s->feature_index_prob[i]);
 
1130
                      av_log(s->avctx, AV_LOG_WARNING,
 
1131
                             "Feature %s present in macroblock (value 0x%x)\n",
 
1132
                             vp7_feature_name[i], s->feature_value[i][index]);
 
1133
                }
 
1134
           }
 
1135
        }
 
1136
    } else if (s->segmentation.update_map)
702
1137
        *segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
703
1138
    else if (s->segmentation.enabled)
704
1139
        *segment = ref ? *ref : *segment;
707
1142
    mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
708
1143
 
709
1144
    if (s->keyframe) {
710
 
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
 
1145
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra,
 
1146
                                    vp8_pred16x16_prob_intra);
711
1147
 
712
1148
        if (mb->mode == MODE_I4x4) {
713
1149
            decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
714
1150
        } else {
715
 
            const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
 
1151
            const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
 
1152
                                           : vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
716
1153
            if (s->mb_layout == 1)
717
1154
                AV_WN32A(mb->intra4x4_pred_mode_top, modes);
718
1155
            else
719
1156
                AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
720
 
            AV_WN32A( s->intra4x4_pred_mode_left, modes);
 
1157
            AV_WN32A(s->intra4x4_pred_mode_left, modes);
721
1158
        }
722
1159
 
723
 
        mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
724
 
        mb->ref_frame = VP56_FRAME_CURRENT;
 
1160
        mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
 
1161
                                                vp8_pred8x8c_prob_intra);
 
1162
        mb->ref_frame        = VP56_FRAME_CURRENT;
725
1163
    } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
726
1164
        // inter MB, 16.2
727
1165
        if (vp56_rac_get_prob_branchy(c, s->prob->last))
728
 
            mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
729
 
                VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
 
1166
            mb->ref_frame =
 
1167
                (!is_vp7 && vp56_rac_get_prob(c, s->prob->golden)) ? VP56_FRAME_GOLDEN2 /* altref */
 
1168
                                                                   : VP56_FRAME_GOLDEN;
730
1169
        else
731
1170
            mb->ref_frame = VP56_FRAME_PREVIOUS;
732
 
        s->ref_count[mb->ref_frame-1]++;
 
1171
        s->ref_count[mb->ref_frame - 1]++;
733
1172
 
734
1173
        // motion vectors, 16.3
735
 
        decode_mvs(s, mb, mb_x, mb_y, layout);
 
1174
        if (is_vp7)
 
1175
            vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
 
1176
        else
 
1177
            vp8_decode_mvs(s, mb, mb_x, mb_y, layout);
736
1178
    } else {
737
1179
        // intra MB, 16.1
738
1180
        mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
740
1182
        if (mb->mode == MODE_I4x4)
741
1183
            decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
742
1184
 
743
 
        mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
744
 
        mb->ref_frame = VP56_FRAME_CURRENT;
745
 
        mb->partitioning = VP8_SPLITMVMODE_NONE;
 
1185
        mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
 
1186
                                                s->prob->pred8x8c);
 
1187
        mb->ref_frame        = VP56_FRAME_CURRENT;
 
1188
        mb->partitioning     = VP8_SPLITMVMODE_NONE;
746
1189
        AV_ZERO32(&mb->bmv[0]);
747
1190
    }
748
1191
}
749
1192
 
750
 
#ifndef decode_block_coeffs_internal
751
1193
/**
752
 
 * @param r arithmetic bitstream reader context
 
1194
 * @param r     arithmetic bitstream reader context
753
1195
 * @param block destination for block coefficients
754
1196
 * @param probs probabilities to use when reading trees from the bitstream
755
 
 * @param i initial coeff index, 0 unless a separate DC block is coded
756
 
 * @param qmul array holding the dc/ac dequant factor at position 0/1
 
1197
 * @param i     initial coeff index, 0 unless a separate DC block is coded
 
1198
 * @param qmul  array holding the dc/ac dequant factor at position 0/1
 
1199
 *
757
1200
 * @return 0 if no coeffs were decoded
758
1201
 *         otherwise, the index of the last coeff decoded plus one
759
1202
 */
760
 
static int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
761
 
                                        uint8_t probs[16][3][NUM_DCT_TOKENS-1],
762
 
                                        int i, uint8_t *token_prob, int16_t qmul[2])
 
1203
static av_always_inline
 
1204
int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
 
1205
                                 uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
 
1206
                                 int i, uint8_t *token_prob, int16_t qmul[2],
 
1207
                                 const uint8_t scan[16], int vp7)
763
1208
{
764
1209
    VP56RangeCoder c = *r;
765
1210
    goto skip_eob;
766
1211
    do {
767
1212
        int coeff;
 
1213
restart:
768
1214
        if (!vp56_rac_get_prob_branchy(&c, token_prob[0]))   // DCT_EOB
769
1215
            break;
770
1216
 
773
1219
            if (++i == 16)
774
1220
                break; // invalid input; blocks should end with EOB
775
1221
            token_prob = probs[i][0];
 
1222
            if (vp7)
 
1223
                goto restart;
776
1224
            goto skip_eob;
777
1225
        }
778
1226
 
779
1227
        if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
780
1228
            coeff = 1;
781
 
            token_prob = probs[i+1][1];
 
1229
            token_prob = probs[i + 1][1];
782
1230
        } else {
783
1231
            if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
784
1232
                coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);
789
1237
                // DCT_CAT*
790
1238
                if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
791
1239
                    if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
792
 
                        coeff  = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
 
1240
                        coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
793
1241
                    } else {                                    // DCT_CAT2
794
1242
                        coeff  = 7;
795
1243
                        coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
796
1244
                        coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
797
1245
                    }
798
1246
                } else {    // DCT_CAT3 and up
799
 
                    int a = vp56_rac_get_prob(&c, token_prob[8]);
800
 
                    int b = vp56_rac_get_prob(&c, token_prob[9+a]);
801
 
                    int cat = (a<<1) + b;
802
 
                    coeff  = 3 + (8<<cat);
 
1247
                    int a   = vp56_rac_get_prob(&c, token_prob[8]);
 
1248
                    int b   = vp56_rac_get_prob(&c, token_prob[9 + a]);
 
1249
                    int cat = (a << 1) + b;
 
1250
                    coeff  = 3 + (8 << cat);
803
1251
                    coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
804
1252
                }
805
1253
            }
806
 
            token_prob = probs[i+1][2];
 
1254
            token_prob = probs[i + 1][2];
807
1255
        }
808
 
        block[zigzag_scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
 
1256
        block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
809
1257
    } while (++i < 16);
810
1258
 
811
1259
    *r = c;
812
1260
    return i;
813
1261
}
 
1262
 
 
1263
static av_always_inline
 
1264
int inter_predict_dc(int16_t block[16], int16_t pred[2])
 
1265
{
 
1266
    int16_t dc = block[0];
 
1267
    int ret = 0;
 
1268
 
 
1269
    if (pred[1] > 3) {
 
1270
        dc += pred[0];
 
1271
        ret = 1;
 
1272
    }
 
1273
 
 
1274
    if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
 
1275
        block[0] = pred[0] = dc;
 
1276
        pred[1] = 0;
 
1277
    } else {
 
1278
        if (pred[0] == dc)
 
1279
            pred[1]++;
 
1280
        block[0] = pred[0] = dc;
 
1281
    }
 
1282
 
 
1283
    return ret;
 
1284
}
 
1285
 
 
1286
static int vp7_decode_block_coeffs_internal(VP56RangeCoder *r,
 
1287
                                            int16_t block[16],
 
1288
                                            uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
 
1289
                                            int i, uint8_t *token_prob,
 
1290
                                            int16_t qmul[2],
 
1291
                                            const uint8_t scan[16])
 
1292
{
 
1293
    return decode_block_coeffs_internal(r, block, probs, i,
 
1294
                                        token_prob, qmul, scan, IS_VP7);
 
1295
}
 
1296
 
 
1297
#ifndef vp8_decode_block_coeffs_internal
 
1298
static int vp8_decode_block_coeffs_internal(VP56RangeCoder *r,
 
1299
                                            int16_t block[16],
 
1300
                                            uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
 
1301
                                            int i, uint8_t *token_prob,
 
1302
                                            int16_t qmul[2])
 
1303
{
 
1304
    return decode_block_coeffs_internal(r, block, probs, i,
 
1305
                                        token_prob, qmul, zigzag_scan, IS_VP8);
 
1306
}
814
1307
#endif
815
1308
 
816
1309
/**
817
 
 * @param c arithmetic bitstream reader context
818
 
 * @param block destination for block coefficients
819
 
 * @param probs probabilities to use when reading trees from the bitstream
820
 
 * @param i initial coeff index, 0 unless a separate DC block is coded
 
1310
 * @param c          arithmetic bitstream reader context
 
1311
 * @param block      destination for block coefficients
 
1312
 * @param probs      probabilities to use when reading trees from the bitstream
 
1313
 * @param i          initial coeff index, 0 unless a separate DC block is coded
821
1314
 * @param zero_nhood the initial prediction context for number of surrounding
822
1315
 *                   all-zero blocks (only left/top, so 0-2)
823
 
 * @param qmul array holding the dc/ac dequant factor at position 0/1
 
1316
 * @param qmul       array holding the dc/ac dequant factor at position 0/1
 
1317
 *
824
1318
 * @return 0 if no coeffs were decoded
825
1319
 *         otherwise, the index of the last coeff decoded plus one
826
1320
 */
827
1321
static av_always_inline
828
1322
int decode_block_coeffs(VP56RangeCoder *c, int16_t block[16],
829
 
                        uint8_t probs[16][3][NUM_DCT_TOKENS-1],
830
 
                        int i, int zero_nhood, int16_t qmul[2])
 
1323
                        uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
 
1324
                        int i, int zero_nhood, int16_t qmul[2],
 
1325
                        const uint8_t scan[16], int vp7)
831
1326
{
832
1327
    uint8_t *token_prob = probs[i][zero_nhood];
833
1328
    if (!vp56_rac_get_prob_branchy(c, token_prob[0]))   // DCT_EOB
834
1329
        return 0;
835
 
    return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
 
1330
    return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
 
1331
                                                  token_prob, qmul, scan)
 
1332
               : vp8_decode_block_coeffs_internal(c, block, probs, i,
 
1333
                                                  token_prob, qmul);
836
1334
}
837
1335
 
838
1336
static av_always_inline
839
 
void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c, VP8Macroblock *mb,
840
 
                      uint8_t t_nnz[9], uint8_t l_nnz[9])
 
1337
void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c,
 
1338
                      VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
 
1339
                      int is_vp7)
841
1340
{
842
1341
    int i, x, y, luma_start = 0, luma_ctx = 3;
843
1342
    int nnz_pred, nnz, nnz_total = 0;
844
1343
    int segment = mb->segment;
845
1344
    int block_dc = 0;
846
1345
 
847
 
    if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
 
1346
    if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
848
1347
        nnz_pred = t_nnz[8] + l_nnz[8];
849
1348
 
850
1349
        // decode DC values and do hadamard
851
 
        nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0, nnz_pred,
852
 
                                  s->qmat[segment].luma_dc_qmul);
 
1350
        nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
 
1351
                                  nnz_pred, s->qmat[segment].luma_dc_qmul,
 
1352
                                  zigzag_scan, is_vp7);
853
1353
        l_nnz[8] = t_nnz[8] = !!nnz;
 
1354
 
 
1355
        if (is_vp7 && mb->mode > MODE_I4x4) {
 
1356
            nnz |=  inter_predict_dc(td->block_dc,
 
1357
                                     s->inter_dc_pred[mb->ref_frame - 1]);
 
1358
        }
 
1359
 
854
1360
        if (nnz) {
855
1361
            nnz_total += nnz;
856
 
            block_dc = 1;
 
1362
            block_dc   = 1;
857
1363
            if (nnz == 1)
858
1364
                s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
859
1365
            else
860
1366
                s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
861
1367
        }
862
1368
        luma_start = 1;
863
 
        luma_ctx = 0;
 
1369
        luma_ctx   = 0;
864
1370
    }
865
1371
 
866
1372
    // luma blocks
867
1373
    for (y = 0; y < 4; y++)
868
1374
        for (x = 0; x < 4; x++) {
869
1375
            nnz_pred = l_nnz[y] + t_nnz[x];
870
 
            nnz = decode_block_coeffs(c, td->block[y][x], s->prob->token[luma_ctx], luma_start,
871
 
                                      nnz_pred, s->qmat[segment].luma_qmul);
872
 
            // nnz+block_dc may be one more than the actual last index, but we don't care
 
1376
            nnz = decode_block_coeffs(c, td->block[y][x],
 
1377
                                      s->prob->token[luma_ctx],
 
1378
                                      luma_start, nnz_pred,
 
1379
                                      s->qmat[segment].luma_qmul,
 
1380
                                      s->prob[0].scan, is_vp7);
 
1381
            /* nnz+block_dc may be one more than the actual last index,
 
1382
             * but we don't care */
873
1383
            td->non_zero_count_cache[y][x] = nnz + block_dc;
874
1384
            t_nnz[x] = l_nnz[y] = !!nnz;
875
1385
            nnz_total += nnz;
881
1391
    for (i = 4; i < 6; i++)
882
1392
        for (y = 0; y < 2; y++)
883
1393
            for (x = 0; x < 2; x++) {
884
 
                nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
885
 
                nnz = decode_block_coeffs(c, td->block[i][(y<<1)+x], s->prob->token[2], 0,
886
 
                                          nnz_pred, s->qmat[segment].chroma_qmul);
887
 
                td->non_zero_count_cache[i][(y<<1)+x] = nnz;
888
 
                t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
 
1394
                nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];
 
1395
                nnz = decode_block_coeffs(c, td->block[i][(y << 1) + x],
 
1396
                                          s->prob->token[2], 0, nnz_pred,
 
1397
                                          s->qmat[segment].chroma_qmul,
 
1398
                                          s->prob[0].scan, is_vp7);
 
1399
                td->non_zero_count_cache[i][(y << 1) + x] = nnz;
 
1400
                t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;
889
1401
                nnz_total += nnz;
890
1402
            }
891
1403
 
897
1409
}
898
1410
 
899
1411
static av_always_inline
900
 
void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
 
1412
void backup_mb_border(uint8_t *top_border, uint8_t *src_y,
 
1413
                      uint8_t *src_cb, uint8_t *src_cr,
901
1414
                      int linesize, int uvlinesize, int simple)
902
1415
{
903
 
    AV_COPY128(top_border, src_y + 15*linesize);
 
1416
    AV_COPY128(top_border, src_y + 15 * linesize);
904
1417
    if (!simple) {
905
 
        AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
906
 
        AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
 
1418
        AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
 
1419
        AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
907
1420
    }
908
1421
}
909
1422
 
910
1423
static av_always_inline
911
 
void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
912
 
                    int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
913
 
                    int simple, int xchg)
 
1424
void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
 
1425
                    uint8_t *src_cr, int linesize, int uvlinesize, int mb_x,
 
1426
                    int mb_y, int mb_width, int simple, int xchg)
914
1427
{
915
 
    uint8_t *top_border_m1 = top_border-32;     // for TL prediction
916
 
    src_y  -=   linesize;
 
1428
    uint8_t *top_border_m1 = top_border - 32;     // for TL prediction
 
1429
    src_y  -= linesize;
917
1430
    src_cb -= uvlinesize;
918
1431
    src_cr -= uvlinesize;
919
1432
 
920
 
#define XCHG(a,b,xchg) do {                     \
921
 
        if (xchg) AV_SWAP64(b,a);               \
922
 
        else      AV_COPY64(b,a);               \
 
1433
#define XCHG(a, b, xchg)                                                      \
 
1434
    do {                                                                      \
 
1435
        if (xchg)                                                             \
 
1436
            AV_SWAP64(b, a);                                                  \
 
1437
        else                                                                  \
 
1438
            AV_COPY64(b, a);                                                  \
923
1439
    } while (0)
924
1440
 
925
 
    XCHG(top_border_m1+8, src_y-8, xchg);
926
 
    XCHG(top_border,      src_y,   xchg);
927
 
    XCHG(top_border+8,    src_y+8, 1);
928
 
    if (mb_x < mb_width-1)
929
 
        XCHG(top_border+32, src_y+16, 1);
 
1441
    XCHG(top_border_m1 + 8, src_y - 8, xchg);
 
1442
    XCHG(top_border, src_y, xchg);
 
1443
    XCHG(top_border + 8, src_y + 8, 1);
 
1444
    if (mb_x < mb_width - 1)
 
1445
        XCHG(top_border + 32, src_y + 16, 1);
930
1446
 
931
1447
    // only copy chroma for normal loop filter
932
1448
    // or to initialize the top row to 127
933
1449
    if (!simple || !mb_y) {
934
 
        XCHG(top_border_m1+16, src_cb-8, xchg);
935
 
        XCHG(top_border_m1+24, src_cr-8, xchg);
936
 
        XCHG(top_border+16,    src_cb, 1);
937
 
        XCHG(top_border+24,    src_cr, 1);
 
1450
        XCHG(top_border_m1 + 16, src_cb - 8, xchg);
 
1451
        XCHG(top_border_m1 + 24, src_cr - 8, xchg);
 
1452
        XCHG(top_border + 16, src_cb, 1);
 
1453
        XCHG(top_border + 24, src_cr, 1);
938
1454
    }
939
1455
}
940
1456
 
941
1457
static av_always_inline
942
1458
int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
943
1459
{
944
 
    if (!mb_x) {
 
1460
    if (!mb_x)
945
1461
        return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
946
 
    } else {
 
1462
    else
947
1463
        return mb_y ? mode : LEFT_DC_PRED8x8;
948
 
    }
949
1464
}
950
1465
 
951
1466
static av_always_inline
952
 
int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
 
1467
int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
953
1468
{
954
 
    if (!mb_x) {
955
 
        return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
956
 
    } else {
 
1469
    if (!mb_x)
 
1470
        return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
 
1471
    else
957
1472
        return mb_y ? mode : HOR_PRED8x8;
958
 
    }
959
1473
}
960
1474
 
961
1475
static av_always_inline
962
 
int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
 
1476
int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
963
1477
{
964
1478
    switch (mode) {
965
1479
    case DC_PRED8x8:
966
1480
        return check_dc_pred8x8_mode(mode, mb_x, mb_y);
967
1481
    case VERT_PRED8x8:
968
 
        return !mb_y ? DC_127_PRED8x8 : mode;
 
1482
        return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
969
1483
    case HOR_PRED8x8:
970
 
        return !mb_x ? DC_129_PRED8x8 : mode;
971
 
    case PLANE_PRED8x8 /*TM*/:
972
 
        return check_tm_pred8x8_mode(mode, mb_x, mb_y);
 
1484
        return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
 
1485
    case PLANE_PRED8x8: /* TM */
 
1486
        return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
973
1487
    }
974
1488
    return mode;
975
1489
}
976
1490
 
977
1491
static av_always_inline
978
 
int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
 
1492
int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
979
1493
{
980
1494
    if (!mb_x) {
981
 
        return mb_y ? VERT_VP8_PRED : DC_129_PRED;
 
1495
        return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
982
1496
    } else {
983
1497
        return mb_y ? mode : HOR_VP8_PRED;
984
1498
    }
985
1499
}
986
1500
 
987
1501
static av_always_inline
988
 
int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
 
1502
int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
 
1503
                                     int *copy_buf, int vp7)
989
1504
{
990
1505
    switch (mode) {
991
1506
    case VERT_PRED:
996
1511
        /* fall-through */
997
1512
    case DIAG_DOWN_LEFT_PRED:
998
1513
    case VERT_LEFT_PRED:
999
 
        return !mb_y ? DC_127_PRED : mode;
 
1514
        return !mb_y ? (vp7 ? DC_128_PRED : DC_127_PRED) : mode;
1000
1515
    case HOR_PRED:
1001
1516
        if (!mb_y) {
1002
1517
            *copy_buf = 1;
1004
1519
        }
1005
1520
        /* fall-through */
1006
1521
    case HOR_UP_PRED:
1007
 
        return !mb_x ? DC_129_PRED : mode;
 
1522
        return !mb_x ? (vp7 ? DC_128_PRED : DC_129_PRED) : mode;
1008
1523
    case TM_VP8_PRED:
1009
 
        return check_tm_pred4x4_mode(mode, mb_x, mb_y);
1010
 
    case DC_PRED: // 4x4 DC doesn't use the same "H.264-style" exceptions as 16x16/8x8 DC
 
1524
        return check_tm_pred4x4_mode(mode, mb_x, mb_y, vp7);
 
1525
    case DC_PRED: /* 4x4 DC doesn't use the same "H.264-style" exceptions
 
1526
                   * as 16x16/8x8 DC */
1011
1527
    case DIAG_DOWN_RIGHT_PRED:
1012
1528
    case VERT_RIGHT_PRED:
1013
1529
    case HOR_DOWN_PRED:
1020
1536
 
1021
1537
static av_always_inline
1022
1538
void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
1023
 
                   VP8Macroblock *mb, int mb_x, int mb_y)
 
1539
                   VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
1024
1540
{
1025
1541
    int x, y, mode, nnz;
1026
1542
    uint32_t tr;
1027
1543
 
1028
 
    // for the first row, we need to run xchg_mb_border to init the top edge to 127
1029
 
    // otherwise, skip it if we aren't going to deblock
 
1544
    /* for the first row, we need to run xchg_mb_border to init the top edge
 
1545
     * to 127 otherwise, skip it if we aren't going to deblock */
1030
1546
    if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1031
 
        xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
 
1547
        xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1032
1548
                       s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1033
1549
                       s->filter.simple, 1);
1034
1550
 
1035
1551
    if (mb->mode < MODE_I4x4) {
1036
 
        mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
 
1552
        mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
1037
1553
        s->hpc.pred16x16[mode](dst[0], s->linesize);
1038
1554
    } else {
1039
1555
        uint8_t *ptr = dst[0];
1040
1556
        uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1041
 
        uint8_t tr_top[4] = { 127, 127, 127, 127 };
 
1557
        const uint8_t lo = is_vp7 ? 128 : 127;
 
1558
        const uint8_t hi = is_vp7 ? 128 : 129;
 
1559
        uint8_t tr_top[4] = { lo, lo, lo, lo };
1042
1560
 
1043
1561
        // all blocks on the right edge of the macroblock use bottom edge
1044
1562
        // the top macroblock for their topright edge
1046
1564
 
1047
1565
        // if we're on the right edge of the frame, said edge is extended
1048
1566
        // from the top macroblock
1049
 
        if (mb_y &&
1050
 
            mb_x == s->mb_width-1) {
1051
 
            tr = tr_right[-1]*0x01010101u;
1052
 
            tr_right = (uint8_t *)&tr;
 
1567
        if (mb_y && mb_x == s->mb_width - 1) {
 
1568
            tr       = tr_right[-1] * 0x01010101u;
 
1569
            tr_right = (uint8_t *) &tr;
1053
1570
        }
1054
1571
 
1055
1572
        if (mb->skip)
1059
1576
            uint8_t *topright = ptr + 4 - s->linesize;
1060
1577
            for (x = 0; x < 4; x++) {
1061
1578
                int copy = 0, linesize = s->linesize;
1062
 
                uint8_t *dst = ptr+4*x;
1063
 
                DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
 
1579
                uint8_t *dst = ptr + 4 * x;
 
1580
                DECLARE_ALIGNED(4, uint8_t, copy_dst)[5 * 8];
1064
1581
 
1065
1582
                if ((y == 0 || x == 3) && mb_y == 0) {
1066
1583
                    topright = tr_top;
1067
1584
                } else if (x == 3)
1068
1585
                    topright = tr_right;
1069
1586
 
1070
 
                mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, &copy);
 
1587
                mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
 
1588
                                                        mb_y + y, &copy, is_vp7);
1071
1589
                if (copy) {
1072
 
                    dst = copy_dst + 12;
 
1590
                    dst      = copy_dst + 12;
1073
1591
                    linesize = 8;
1074
1592
                    if (!(mb_y + y)) {
1075
 
                        copy_dst[3] = 127U;
1076
 
                        AV_WN32A(copy_dst+4, 127U * 0x01010101U);
 
1593
                        copy_dst[3] = lo;
 
1594
                        AV_WN32A(copy_dst + 4, lo * 0x01010101U);
1077
1595
                    } else {
1078
 
                        AV_COPY32(copy_dst+4, ptr+4*x-s->linesize);
 
1596
                        AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
1079
1597
                        if (!(mb_x + x)) {
1080
 
                            copy_dst[3] = 129U;
 
1598
                            copy_dst[3] = hi;
1081
1599
                        } else {
1082
 
                            copy_dst[3] = ptr[4*x-s->linesize-1];
 
1600
                            copy_dst[3] = ptr[4 * x - s->linesize - 1];
1083
1601
                        }
1084
1602
                    }
1085
1603
                    if (!(mb_x + x)) {
1086
1604
                        copy_dst[11] =
1087
1605
                        copy_dst[19] =
1088
1606
                        copy_dst[27] =
1089
 
                        copy_dst[35] = 129U;
 
1607
                        copy_dst[35] = hi;
1090
1608
                    } else {
1091
 
                        copy_dst[11] = ptr[4*x              -1];
1092
 
                        copy_dst[19] = ptr[4*x+s->linesize  -1];
1093
 
                        copy_dst[27] = ptr[4*x+s->linesize*2-1];
1094
 
                        copy_dst[35] = ptr[4*x+s->linesize*3-1];
 
1609
                        copy_dst[11] = ptr[4 * x                   - 1];
 
1610
                        copy_dst[19] = ptr[4 * x + s->linesize     - 1];
 
1611
                        copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
 
1612
                        copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
1095
1613
                    }
1096
1614
                }
1097
1615
                s->hpc.pred4x4[mode](dst, topright, linesize);
1098
1616
                if (copy) {
1099
 
                    AV_COPY32(ptr+4*x              , copy_dst+12);
1100
 
                    AV_COPY32(ptr+4*x+s->linesize  , copy_dst+20);
1101
 
                    AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
1102
 
                    AV_COPY32(ptr+4*x+s->linesize*3, copy_dst+36);
 
1617
                    AV_COPY32(ptr + 4 * x,                   copy_dst + 12);
 
1618
                    AV_COPY32(ptr + 4 * x + s->linesize,     copy_dst + 20);
 
1619
                    AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
 
1620
                    AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
1103
1621
                }
1104
1622
 
1105
1623
                nnz = td->non_zero_count_cache[y][x];
1106
1624
                if (nnz) {
1107
1625
                    if (nnz == 1)
1108
 
                        s->vp8dsp.vp8_idct_dc_add(ptr+4*x, td->block[y][x], s->linesize);
 
1626
                        s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
 
1627
                                                  td->block[y][x], s->linesize);
1109
1628
                    else
1110
 
                        s->vp8dsp.vp8_idct_add(ptr+4*x, td->block[y][x], s->linesize);
 
1629
                        s->vp8dsp.vp8_idct_add(ptr + 4 * x,
 
1630
                                               td->block[y][x], s->linesize);
1111
1631
                }
1112
1632
                topright += 4;
1113
1633
            }
1114
1634
 
1115
 
            ptr   += 4*s->linesize;
 
1635
            ptr      += 4 * s->linesize;
1116
1636
            intra4x4 += 4;
1117
1637
        }
1118
1638
    }
1119
1639
 
1120
 
    mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode, mb_x, mb_y);
 
1640
    mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
 
1641
                                            mb_x, mb_y, is_vp7);
1121
1642
    s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
1122
1643
    s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
1123
1644
 
1124
1645
    if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1125
 
        xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
 
1646
        xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1126
1647
                       s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1127
1648
                       s->filter.simple, 0);
1128
1649
}
1137
1658
/**
1138
1659
 * luma MC function
1139
1660
 *
1140
 
 * @param s VP8 decoding context
1141
 
 * @param dst target buffer for block data at block position
1142
 
 * @param ref reference picture buffer at origin (0, 0)
1143
 
 * @param mv motion vector (relative to block position) to get pixel data from
1144
 
 * @param x_off horizontal position of block from origin (0, 0)
1145
 
 * @param y_off vertical position of block from origin (0, 0)
1146
 
 * @param block_w width of block (16, 8 or 4)
1147
 
 * @param block_h height of block (always same as block_w)
1148
 
 * @param width width of src/dst plane data
1149
 
 * @param height height of src/dst plane data
 
1661
 * @param s        VP8 decoding context
 
1662
 * @param dst      target buffer for block data at block position
 
1663
 * @param ref      reference picture buffer at origin (0, 0)
 
1664
 * @param mv       motion vector (relative to block position) to get pixel data from
 
1665
 * @param x_off    horizontal position of block from origin (0, 0)
 
1666
 * @param y_off    vertical position of block from origin (0, 0)
 
1667
 * @param block_w  width of block (16, 8 or 4)
 
1668
 * @param block_h  height of block (always same as block_w)
 
1669
 * @param width    width of src/dst plane data
 
1670
 * @param height   height of src/dst plane data
1150
1671
 * @param linesize size of a single line of plane data, including padding
1151
 
 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
 
1672
 * @param mc_func  motion compensation function pointers (bilinear or sixtap MC)
1152
1673
 */
1153
1674
static av_always_inline
1154
1675
void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
1162
1683
    if (AV_RN32A(mv)) {
1163
1684
        int src_linesize = linesize;
1164
1685
 
1165
 
        int mx = (mv->x << 1)&7, mx_idx = subpel_idx[0][mx];
1166
 
        int my = (mv->y << 1)&7, my_idx = subpel_idx[0][my];
 
1686
        int mx = (mv->x << 1) & 7, mx_idx = subpel_idx[0][mx];
 
1687
        int my = (mv->y << 1) & 7, my_idx = subpel_idx[0][my];
1167
1688
 
1168
1689
        x_off += mv->x >> 2;
1169
1690
        y_off += mv->y >> 2;
1176
1697
            s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1177
1698
                                     src - my_idx * linesize - mx_idx,
1178
1699
                                     EDGE_EMU_LINESIZE, linesize,
1179
 
                                     block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1180
 
                                     x_off - mx_idx, y_off - my_idx, width, height);
 
1700
                                     block_w + subpel_idx[1][mx],
 
1701
                                     block_h + subpel_idx[1][my],
 
1702
                                     x_off - mx_idx, y_off - my_idx,
 
1703
                                     width, height);
1181
1704
            src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1182
1705
            src_linesize = EDGE_EMU_LINESIZE;
1183
1706
        }
1184
1707
        mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
1185
1708
    } else {
1186
1709
        ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
1187
 
        mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
 
1710
        mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
 
1711
                      linesize, block_h, 0, 0);
1188
1712
    }
1189
1713
}
1190
1714
 
1191
1715
/**
1192
1716
 * chroma MC function
1193
1717
 *
1194
 
 * @param s VP8 decoding context
1195
 
 * @param dst1 target buffer for block data at block position (U plane)
1196
 
 * @param dst2 target buffer for block data at block position (V plane)
1197
 
 * @param ref reference picture buffer at origin (0, 0)
1198
 
 * @param mv motion vector (relative to block position) to get pixel data from
1199
 
 * @param x_off horizontal position of block from origin (0, 0)
1200
 
 * @param y_off vertical position of block from origin (0, 0)
1201
 
 * @param block_w width of block (16, 8 or 4)
1202
 
 * @param block_h height of block (always same as block_w)
1203
 
 * @param width width of src/dst plane data
1204
 
 * @param height height of src/dst plane data
 
1718
 * @param s        VP8 decoding context
 
1719
 * @param dst1     target buffer for block data at block position (U plane)
 
1720
 * @param dst2     target buffer for block data at block position (V plane)
 
1721
 * @param ref      reference picture buffer at origin (0, 0)
 
1722
 * @param mv       motion vector (relative to block position) to get pixel data from
 
1723
 * @param x_off    horizontal position of block from origin (0, 0)
 
1724
 * @param y_off    vertical position of block from origin (0, 0)
 
1725
 * @param block_w  width of block (16, 8 or 4)
 
1726
 * @param block_h  height of block (always same as block_w)
 
1727
 * @param width    width of src/dst plane data
 
1728
 * @param height   height of src/dst plane data
1205
1729
 * @param linesize size of a single line of plane data, including padding
1206
 
 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
 
1730
 * @param mc_func  motion compensation function pointers (bilinear or sixtap MC)
1207
1731
 */
1208
1732
static av_always_inline
1209
 
void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1, uint8_t *dst2,
1210
 
                   ThreadFrame *ref, const VP56mv *mv, int x_off, int y_off,
1211
 
                   int block_w, int block_h, int width, int height, ptrdiff_t linesize,
 
1733
void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
 
1734
                   uint8_t *dst2, ThreadFrame *ref, const VP56mv *mv,
 
1735
                   int x_off, int y_off, int block_w, int block_h,
 
1736
                   int width, int height, ptrdiff_t linesize,
1212
1737
                   vp8_mc_func mc_func[3][3])
1213
1738
{
1214
1739
    uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
1215
1740
 
1216
1741
    if (AV_RN32A(mv)) {
1217
 
        int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
1218
 
        int my = mv->y&7, my_idx = subpel_idx[0][my];
 
1742
        int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
 
1743
        int my = mv->y & 7, my_idx = subpel_idx[0][my];
1219
1744
 
1220
1745
        x_off += mv->x >> 3;
1221
1746
        y_off += mv->y >> 3;
1239
1764
                                     EDGE_EMU_LINESIZE, linesize,
1240
1765
                                     block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1241
1766
                                     x_off - mx_idx, y_off - my_idx, width, height);
1242
 
            src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE* my_idx;
 
1767
            src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1243
1768
            mc_func[my_idx][mx_idx](dst2, linesize, src2, EDGE_EMU_LINESIZE, block_h, mx, my);
1244
1769
        } else {
1245
1770
            mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
1255
1780
static av_always_inline
1256
1781
void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
1257
1782
                 ThreadFrame *ref_frame, int x_off, int y_off,
1258
 
                 int bx_off, int by_off,
1259
 
                 int block_w, int block_h,
 
1783
                 int bx_off, int by_off, int block_w, int block_h,
1260
1784
                 int width, int height, VP56mv *mv)
1261
1785
{
1262
1786
    VP56mv uvmv = *mv;
1269
1793
 
1270
1794
    /* U/V */
1271
1795
    if (s->profile == 3) {
 
1796
        /* this block only applies VP8; it is safe to check
 
1797
         * only the profile, as VP7 profile <= 1 */
1272
1798
        uvmv.x &= ~7;
1273
1799
        uvmv.y &= ~7;
1274
1800
    }
1275
 
    x_off   >>= 1; y_off   >>= 1;
1276
 
    bx_off  >>= 1; by_off  >>= 1;
1277
 
    width   >>= 1; height  >>= 1;
1278
 
    block_w >>= 1; block_h >>= 1;
 
1801
    x_off   >>= 1;
 
1802
    y_off   >>= 1;
 
1803
    bx_off  >>= 1;
 
1804
    by_off  >>= 1;
 
1805
    width   >>= 1;
 
1806
    height  >>= 1;
 
1807
    block_w >>= 1;
 
1808
    block_h >>= 1;
1279
1809
    vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
1280
1810
                  dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
1281
1811
                  &uvmv, x_off + bx_off, y_off + by_off,
1284
1814
}
1285
1815
 
1286
1816
/* Fetch pixels for estimated mv 4 macroblocks ahead.
1287
 
 * Optimized for 64-byte cache lines.  Inspired by ffh264 prefetch_motion. */
1288
 
static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
 
1817
 * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
 
1818
static av_always_inline
 
1819
void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
 
1820
                     int mb_xy, int ref)
1289
1821
{
1290
1822
    /* Don't prefetch refs that haven't been used very often this frame. */
1291
 
    if (s->ref_count[ref-1] > (mb_xy >> 5)) {
 
1823
    if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
1292
1824
        int x_off = mb_x << 4, y_off = mb_y << 4;
1293
 
        int mx = (mb->mv.x>>2) + x_off + 8;
1294
 
        int my = (mb->mv.y>>2) + y_off;
1295
 
        uint8_t **src= s->framep[ref]->tf.f->data;
1296
 
        int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
 
1825
        int mx = (mb->mv.x >> 2) + x_off + 8;
 
1826
        int my = (mb->mv.y >> 2) + y_off;
 
1827
        uint8_t **src = s->framep[ref]->tf.f->data;
 
1828
        int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
1297
1829
        /* For threading, a ff_thread_await_progress here might be useful, but
1298
1830
         * it actually slows down the decoder. Since a bad prefetch doesn't
1299
1831
         * generate bad decoder output, we don't run it here. */
1300
 
        s->vdsp.prefetch(src[0]+off, s->linesize, 4);
1301
 
        off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1302
 
        s->vdsp.prefetch(src[1]+off, src[2]-src[1], 2);
 
1832
        s->vdsp.prefetch(src[0] + off, s->linesize, 4);
 
1833
        off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
 
1834
        s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
1303
1835
    }
1304
1836
}
1305
1837
 
1311
1843
                   VP8Macroblock *mb, int mb_x, int mb_y)
1312
1844
{
1313
1845
    int x_off = mb_x << 4, y_off = mb_y << 4;
1314
 
    int width = 16*s->mb_width, height = 16*s->mb_height;
 
1846
    int width = 16 * s->mb_width, height = 16 * s->mb_height;
1315
1847
    ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
1316
1848
    VP56mv *bmv = mb->bmv;
1317
1849
 
1327
1859
        /* Y */
1328
1860
        for (y = 0; y < 4; y++) {
1329
1861
            for (x = 0; x < 4; x++) {
1330
 
                vp8_mc_luma(s, td, dst[0] + 4*y*s->linesize + x*4,
1331
 
                            ref, &bmv[4*y + x],
1332
 
                            4*x + x_off, 4*y + y_off, 4, 4,
 
1862
                vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
 
1863
                            ref, &bmv[4 * y + x],
 
1864
                            4 * x + x_off, 4 * y + y_off, 4, 4,
1333
1865
                            width, height, s->linesize,
1334
1866
                            s->put_pixels_tab[2]);
1335
1867
            }
1336
1868
        }
1337
1869
 
1338
1870
        /* U/V */
1339
 
        x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
 
1871
        x_off  >>= 1;
 
1872
        y_off  >>= 1;
 
1873
        width  >>= 1;
 
1874
        height >>= 1;
1340
1875
        for (y = 0; y < 2; y++) {
1341
1876
            for (x = 0; x < 2; x++) {
1342
 
                uvmv.x = mb->bmv[ 2*y    * 4 + 2*x  ].x +
1343
 
                         mb->bmv[ 2*y    * 4 + 2*x+1].x +
1344
 
                         mb->bmv[(2*y+1) * 4 + 2*x  ].x +
1345
 
                         mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1346
 
                uvmv.y = mb->bmv[ 2*y    * 4 + 2*x  ].y +
1347
 
                         mb->bmv[ 2*y    * 4 + 2*x+1].y +
1348
 
                         mb->bmv[(2*y+1) * 4 + 2*x  ].y +
1349
 
                         mb->bmv[(2*y+1) * 4 + 2*x+1].y;
1350
 
                uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1351
 
                uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
 
1877
                uvmv.x = mb->bmv[2 * y       * 4 + 2 * x    ].x +
 
1878
                         mb->bmv[2 * y       * 4 + 2 * x + 1].x +
 
1879
                         mb->bmv[(2 * y + 1) * 4 + 2 * x    ].x +
 
1880
                         mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
 
1881
                uvmv.y = mb->bmv[2 * y       * 4 + 2 * x    ].y +
 
1882
                         mb->bmv[2 * y       * 4 + 2 * x + 1].y +
 
1883
                         mb->bmv[(2 * y + 1) * 4 + 2 * x    ].y +
 
1884
                         mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
 
1885
                uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT - 1))) >> 2;
 
1886
                uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT - 1))) >> 2;
1352
1887
                if (s->profile == 3) {
1353
1888
                    uvmv.x &= ~7;
1354
1889
                    uvmv.y &= ~7;
1355
1890
                }
1356
 
                vp8_mc_chroma(s, td, dst[1] + 4*y*s->uvlinesize + x*4,
1357
 
                              dst[2] + 4*y*s->uvlinesize + x*4, ref, &uvmv,
1358
 
                              4*x + x_off, 4*y + y_off, 4, 4,
 
1891
                vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
 
1892
                              dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
 
1893
                              &uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
1359
1894
                              width, height, s->uvlinesize,
1360
1895
                              s->put_pixels_tab[2]);
1361
1896
            }
1387
1922
    }
1388
1923
}
1389
1924
 
1390
 
static av_always_inline void idct_mb(VP8Context *s, VP8ThreadData *td,
1391
 
                                     uint8_t *dst[3], VP8Macroblock *mb)
 
1925
static av_always_inline
 
1926
void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb)
1392
1927
{
1393
1928
    int x, y, ch;
1394
1929
 
1397
1932
        for (y = 0; y < 4; y++) {
1398
1933
            uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
1399
1934
            if (nnz4) {
1400
 
                if (nnz4&~0x01010101) {
 
1935
                if (nnz4 & ~0x01010101) {
1401
1936
                    for (x = 0; x < 4; x++) {
1402
 
                        if ((uint8_t)nnz4 == 1)
1403
 
                            s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, td->block[y][x], s->linesize);
1404
 
                        else if((uint8_t)nnz4 > 1)
1405
 
                            s->vp8dsp.vp8_idct_add(y_dst+4*x, td->block[y][x], s->linesize);
 
1937
                        if ((uint8_t) nnz4 == 1)
 
1938
                            s->vp8dsp.vp8_idct_dc_add(y_dst + 4 * x,
 
1939
                                                      td->block[y][x],
 
1940
                                                      s->linesize);
 
1941
                        else if ((uint8_t) nnz4 > 1)
 
1942
                            s->vp8dsp.vp8_idct_add(y_dst + 4 * x,
 
1943
                                                   td->block[y][x],
 
1944
                                                   s->linesize);
1406
1945
                        nnz4 >>= 8;
1407
1946
                        if (!nnz4)
1408
1947
                            break;
1411
1950
                    s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
1412
1951
                }
1413
1952
            }
1414
 
            y_dst += 4*s->linesize;
 
1953
            y_dst += 4 * s->linesize;
1415
1954
        }
1416
1955
    }
1417
1956
 
1418
1957
    for (ch = 0; ch < 2; ch++) {
1419
 
        uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4+ch]);
 
1958
        uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
1420
1959
        if (nnz4) {
1421
 
            uint8_t *ch_dst = dst[1+ch];
1422
 
            if (nnz4&~0x01010101) {
 
1960
            uint8_t *ch_dst = dst[1 + ch];
 
1961
            if (nnz4 & ~0x01010101) {
1423
1962
                for (y = 0; y < 2; y++) {
1424
1963
                    for (x = 0; x < 2; x++) {
1425
 
                        if ((uint8_t)nnz4 == 1)
1426
 
                            s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
1427
 
                        else if((uint8_t)nnz4 > 1)
1428
 
                            s->vp8dsp.vp8_idct_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
 
1964
                        if ((uint8_t) nnz4 == 1)
 
1965
                            s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
 
1966
                                                      td->block[4 + ch][(y << 1) + x],
 
1967
                                                      s->uvlinesize);
 
1968
                        else if ((uint8_t) nnz4 > 1)
 
1969
                            s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
 
1970
                                                   td->block[4 + ch][(y << 1) + x],
 
1971
                                                   s->uvlinesize);
1429
1972
                        nnz4 >>= 8;
1430
1973
                        if (!nnz4)
1431
1974
                            goto chroma_idct_end;
1432
1975
                    }
1433
 
                    ch_dst += 4*s->uvlinesize;
 
1976
                    ch_dst += 4 * s->uvlinesize;
1434
1977
                }
1435
1978
            } else {
1436
 
                s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4+ch], s->uvlinesize);
 
1979
                s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
1437
1980
            }
1438
1981
        }
1439
 
chroma_idct_end: ;
 
1982
chroma_idct_end:
 
1983
        ;
1440
1984
    }
1441
1985
}
1442
1986
 
1443
 
static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
 
1987
static av_always_inline
 
1988
void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb,
 
1989
                         VP8FilterStrength *f, int is_vp7)
1444
1990
{
1445
1991
    int interior_limit, filter_level;
1446
1992
 
1467
2013
 
1468
2014
    f->filter_level = filter_level;
1469
2015
    f->inner_limit = interior_limit;
1470
 
    f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
 
2016
    f->inner_filter = is_vp7 || !mb->skip || mb->mode == MODE_I4x4 ||
 
2017
                      mb->mode == VP8_MVMODE_SPLIT;
1471
2018
}
1472
2019
 
1473
 
static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
 
2020
static av_always_inline
 
2021
void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f,
 
2022
               int mb_x, int mb_y, int is_vp7)
1474
2023
{
1475
 
    int mbedge_lim, bedge_lim, hev_thresh;
 
2024
    int mbedge_lim, bedge_lim_y, bedge_lim_uv, hev_thresh;
1476
2025
    int filter_level = f->filter_level;
1477
2026
    int inner_limit = f->inner_limit;
1478
2027
    int inner_filter = f->inner_filter;
1492
2041
    if (!filter_level)
1493
2042
        return;
1494
2043
 
1495
 
     bedge_lim = 2*filter_level + inner_limit;
1496
 
    mbedge_lim = bedge_lim + 4;
 
2044
    if (is_vp7) {
 
2045
        bedge_lim_y  = filter_level;
 
2046
        bedge_lim_uv = filter_level * 2;
 
2047
        mbedge_lim   = filter_level + 2;
 
2048
    } else {
 
2049
        bedge_lim_y  =
 
2050
        bedge_lim_uv = filter_level * 2 + inner_limit;
 
2051
        mbedge_lim   = bedge_lim_y + 4;
 
2052
    }
1497
2053
 
1498
2054
    hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
1499
2055
 
1500
2056
    if (mb_x) {
1501
 
        s->vp8dsp.vp8_h_loop_filter16y(dst[0],     linesize,
1502
 
                                       mbedge_lim, inner_limit, hev_thresh);
1503
 
        s->vp8dsp.vp8_h_loop_filter8uv(dst[1],     dst[2],      uvlinesize,
1504
 
                                       mbedge_lim, inner_limit, hev_thresh);
1505
 
    }
1506
 
 
1507
 
    if (inner_filter) {
1508
 
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
1509
 
                                             inner_limit, hev_thresh);
1510
 
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
1511
 
                                             inner_limit, hev_thresh);
1512
 
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
1513
 
                                             inner_limit, hev_thresh);
1514
 
        s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
1515
 
                                             uvlinesize,  bedge_lim,
1516
 
                                             inner_limit, hev_thresh);
1517
 
    }
 
2057
        s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
 
2058
                                       mbedge_lim, inner_limit, hev_thresh);
 
2059
        s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
 
2060
                                       mbedge_lim, inner_limit, hev_thresh);
 
2061
    }
 
2062
 
 
2063
#define H_LOOP_FILTER_16Y_INNER(cond)                                         \
 
2064
    if (cond && inner_filter) {                                               \
 
2065
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] +  4, linesize,           \
 
2066
                                             bedge_lim_y, inner_limit,        \
 
2067
                                             hev_thresh);                     \
 
2068
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] +  8, linesize,           \
 
2069
                                             bedge_lim_y, inner_limit,        \
 
2070
                                             hev_thresh);                     \
 
2071
        s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize,           \
 
2072
                                             bedge_lim_y, inner_limit,        \
 
2073
                                             hev_thresh);                     \
 
2074
        s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] +  4, dst[2] + 4,         \
 
2075
                                             uvlinesize,  bedge_lim_uv,       \
 
2076
                                             inner_limit, hev_thresh);        \
 
2077
    }
 
2078
 
 
2079
    H_LOOP_FILTER_16Y_INNER(!is_vp7)
1518
2080
 
1519
2081
    if (mb_y) {
1520
 
        s->vp8dsp.vp8_v_loop_filter16y(dst[0],     linesize,
 
2082
        s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
1521
2083
                                       mbedge_lim, inner_limit, hev_thresh);
1522
 
        s->vp8dsp.vp8_v_loop_filter8uv(dst[1],     dst[2],      uvlinesize,
 
2084
        s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
1523
2085
                                       mbedge_lim, inner_limit, hev_thresh);
1524
2086
    }
1525
2087
 
1526
2088
    if (inner_filter) {
1527
 
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
1528
 
                                             linesize,    bedge_lim,
1529
 
                                             inner_limit, hev_thresh);
1530
 
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
1531
 
                                             linesize,    bedge_lim,
1532
 
                                             inner_limit, hev_thresh);
1533
 
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
1534
 
                                             linesize,    bedge_lim,
1535
 
                                             inner_limit, hev_thresh);
1536
 
        s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
1537
 
                                             dst[2] + 4 * uvlinesize,
1538
 
                                             uvlinesize,  bedge_lim,
 
2089
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] +  4 * linesize,
 
2090
                                             linesize, bedge_lim_y,
 
2091
                                             inner_limit, hev_thresh);
 
2092
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] +  8 * linesize,
 
2093
                                             linesize, bedge_lim_y,
 
2094
                                             inner_limit, hev_thresh);
 
2095
        s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
 
2096
                                             linesize, bedge_lim_y,
 
2097
                                             inner_limit, hev_thresh);
 
2098
        s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] +  4 * uvlinesize,
 
2099
                                             dst[2] +  4 * uvlinesize,
 
2100
                                             uvlinesize, bedge_lim_uv,
1539
2101
                                             inner_limit, hev_thresh);
1540
2102
    }
 
2103
 
 
2104
    H_LOOP_FILTER_16Y_INNER(is_vp7)
1541
2105
}
1542
2106
 
1543
 
static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
 
2107
static av_always_inline
 
2108
void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f,
 
2109
                      int mb_x, int mb_y)
1544
2110
{
1545
2111
    int mbedge_lim, bedge_lim;
1546
2112
    int filter_level = f->filter_level;
1547
 
    int inner_limit = f->inner_limit;
 
2113
    int inner_limit  = f->inner_limit;
1548
2114
    int inner_filter = f->inner_filter;
1549
 
    int linesize = s->linesize;
 
2115
    int linesize     = s->linesize;
1550
2116
 
1551
2117
    if (!filter_level)
1552
2118
        return;
1553
2119
 
1554
 
     bedge_lim = 2*filter_level + inner_limit;
 
2120
    bedge_lim  = 2 * filter_level + inner_limit;
1555
2121
    mbedge_lim = bedge_lim + 4;
1556
2122
 
1557
2123
    if (mb_x)
1558
2124
        s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
1559
2125
    if (inner_filter) {
1560
 
        s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
1561
 
        s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
1562
 
        s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
 
2126
        s->vp8dsp.vp8_h_loop_filter_simple(dst +  4, linesize, bedge_lim);
 
2127
        s->vp8dsp.vp8_h_loop_filter_simple(dst +  8, linesize, bedge_lim);
 
2128
        s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
1563
2129
    }
1564
2130
 
1565
2131
    if (mb_y)
1566
2132
        s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
1567
2133
    if (inner_filter) {
1568
 
        s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
1569
 
        s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
1570
 
        s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
 
2134
        s->vp8dsp.vp8_v_loop_filter_simple(dst +  4 * linesize, linesize, bedge_lim);
 
2135
        s->vp8dsp.vp8_v_loop_filter_simple(dst +  8 * linesize, linesize, bedge_lim);
 
2136
        s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
1571
2137
    }
1572
2138
}
1573
2139
 
1574
2140
#define MARGIN (16 << 2)
1575
 
static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
1576
 
                                   VP8Frame *prev_frame)
 
2141
static av_always_inline
 
2142
void vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
 
2143
                                    VP8Frame *prev_frame, int is_vp7)
1577
2144
{
1578
2145
    VP8Context *s = avctx->priv_data;
1579
2146
    int mb_x, mb_y;
1581
2148
    s->mv_min.y = -MARGIN;
1582
2149
    s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
1583
2150
    for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1584
 
        VP8Macroblock *mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
1585
 
        int mb_xy = mb_y*s->mb_width;
 
2151
        VP8Macroblock *mb = s->macroblocks_base +
 
2152
                            ((s->mb_width + 1) * (mb_y + 1) + 1);
 
2153
        int mb_xy = mb_y * s->mb_width;
1586
2154
 
1587
 
        AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
 
2155
        AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
1588
2156
 
1589
2157
        s->mv_min.x = -MARGIN;
1590
2158
        s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
1591
2159
        for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
1592
2160
            if (mb_y == 0)
1593
 
                AV_WN32A((mb-s->mb_width-1)->intra4x4_pred_mode_top, DC_PRED*0x01010101);
 
2161
                AV_WN32A((mb - s->mb_width - 1)->intra4x4_pred_mode_top,
 
2162
                         DC_PRED * 0x01010101);
1594
2163
            decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
1595
2164
                           prev_frame && prev_frame->seg_map ?
1596
 
                           prev_frame->seg_map->data + mb_xy : NULL, 1);
 
2165
                           prev_frame->seg_map->data + mb_xy : NULL, 1, is_vp7);
1597
2166
            s->mv_min.x -= 64;
1598
2167
            s->mv_max.x -= 64;
1599
2168
        }
1602
2171
    }
1603
2172
}
1604
2173
 
 
2174
static void vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
 
2175
                                   VP8Frame *prev_frame)
 
2176
{
 
2177
    vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
 
2178
}
 
2179
 
 
2180
static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
 
2181
                                   VP8Frame *prev_frame)
 
2182
{
 
2183
    vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
 
2184
}
 
2185
 
1605
2186
#if HAVE_THREADS
1606
 
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)\
1607
 
    do {\
1608
 
        int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF);\
1609
 
        if (otd->thread_mb_pos < tmp) {\
1610
 
            pthread_mutex_lock(&otd->lock);\
1611
 
            td->wait_mb_pos = tmp;\
1612
 
            do {\
1613
 
                if (otd->thread_mb_pos >= tmp)\
1614
 
                    break;\
1615
 
                pthread_cond_wait(&otd->cond, &otd->lock);\
1616
 
            } while (1);\
1617
 
            td->wait_mb_pos = INT_MAX;\
1618
 
            pthread_mutex_unlock(&otd->lock);\
1619
 
        }\
1620
 
    } while(0);
 
2187
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)                     \
 
2188
    do {                                                                      \
 
2189
        int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF);                 \
 
2190
        if (otd->thread_mb_pos < tmp) {                                       \
 
2191
            pthread_mutex_lock(&otd->lock);                                   \
 
2192
            td->wait_mb_pos = tmp;                                            \
 
2193
            do {                                                              \
 
2194
                if (otd->thread_mb_pos >= tmp)                                \
 
2195
                    break;                                                    \
 
2196
                pthread_cond_wait(&otd->cond, &otd->lock);                    \
 
2197
            } while (1);                                                      \
 
2198
            td->wait_mb_pos = INT_MAX;                                        \
 
2199
            pthread_mutex_unlock(&otd->lock);                                 \
 
2200
        }                                                                     \
 
2201
    } while (0);
1621
2202
 
1622
 
#define update_pos(td, mb_y, mb_x)\
1623
 
    do {\
1624
 
    int pos              = (mb_y << 16) | (mb_x & 0xFFFF);\
1625
 
    int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && (num_jobs > 1);\
1626
 
    int is_null          = (next_td == NULL) || (prev_td == NULL);\
1627
 
    int pos_check        = (is_null) ? 1 :\
1628
 
                            (next_td != td && pos >= next_td->wait_mb_pos) ||\
1629
 
                            (prev_td != td && pos >= prev_td->wait_mb_pos);\
1630
 
    td->thread_mb_pos = pos;\
1631
 
    if (sliced_threading && pos_check) {\
1632
 
        pthread_mutex_lock(&td->lock);\
1633
 
        pthread_cond_broadcast(&td->cond);\
1634
 
        pthread_mutex_unlock(&td->lock);\
1635
 
    }\
1636
 
    } while(0);
 
2203
#define update_pos(td, mb_y, mb_x)                                            \
 
2204
    do {                                                                      \
 
2205
        int pos              = (mb_y << 16) | (mb_x & 0xFFFF);                \
 
2206
        int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
 
2207
                               (num_jobs > 1);                                \
 
2208
        int is_null          = !next_td || !prev_td;                          \
 
2209
        int pos_check        = (is_null) ? 1                                  \
 
2210
                                         : (next_td != td &&                  \
 
2211
                                            pos >= next_td->wait_mb_pos) ||   \
 
2212
                                           (prev_td != td &&                  \
 
2213
                                            pos >= prev_td->wait_mb_pos);     \
 
2214
        td->thread_mb_pos = pos;                                              \
 
2215
        if (sliced_threading && pos_check) {                                  \
 
2216
            pthread_mutex_lock(&td->lock);                                    \
 
2217
            pthread_cond_broadcast(&td->cond);                                \
 
2218
            pthread_mutex_unlock(&td->lock);                                  \
 
2219
        }                                                                     \
 
2220
    } while (0);
1637
2221
#else
1638
2222
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)
1639
2223
#define update_pos(td, mb_y, mb_x)
1640
2224
#endif
1641
2225
 
1642
2226
static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
1643
 
                                        int jobnr, int threadnr)
 
2227
                                        int jobnr, int threadnr, int is_vp7)
1644
2228
{
1645
2229
    VP8Context *s = avctx->priv_data;
1646
2230
    VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
1647
 
    int mb_y = td->thread_mb_pos>>16;
1648
 
    int mb_x, mb_xy = mb_y*s->mb_width;
 
2231
    int mb_y = td->thread_mb_pos >> 16;
 
2232
    int mb_x, mb_xy = mb_y * s->mb_width;
1649
2233
    int num_jobs = s->num_jobs;
1650
2234
    VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame;
1651
 
    VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
 
2235
    VP56RangeCoder *c  = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
1652
2236
    VP8Macroblock *mb;
1653
2237
    uint8_t *dst[3] = {
1654
 
        curframe->tf.f->data[0] + 16*mb_y*s->linesize,
1655
 
        curframe->tf.f->data[1] +  8*mb_y*s->uvlinesize,
1656
 
        curframe->tf.f->data[2] +  8*mb_y*s->uvlinesize
 
2238
        curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
 
2239
        curframe->tf.f->data[1] +  8 * mb_y * s->uvlinesize,
 
2240
        curframe->tf.f->data[2] +  8 * mb_y * s->uvlinesize
1657
2241
    };
1658
 
    if (mb_y == 0) prev_td = td;
1659
 
    else           prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1660
 
    if (mb_y == s->mb_height-1) next_td = td;
1661
 
    else                        next_td = &s->thread_data[(jobnr + 1)%num_jobs];
 
2242
    if (mb_y == 0)
 
2243
        prev_td = td;
 
2244
    else
 
2245
        prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
 
2246
    if (mb_y == s->mb_height - 1)
 
2247
        next_td = td;
 
2248
    else
 
2249
        next_td = &s->thread_data[(jobnr + 1) % num_jobs];
1662
2250
    if (s->mb_layout == 1)
1663
 
        mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
 
2251
        mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
1664
2252
    else {
1665
2253
        // Make sure the previous frame has read its segmentation map,
1666
2254
        // if we re-use the same map.
1667
2255
        if (prev_frame && s->segmentation.enabled &&
1668
2256
            !s->segmentation.update_map)
1669
2257
            ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
1670
 
        mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
 
2258
        mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
1671
2259
        memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
1672
 
        AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
 
2260
        AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
1673
2261
    }
1674
2262
 
1675
 
    memset(td->left_nnz, 0, sizeof(td->left_nnz));
 
2263
    if (!is_vp7 || mb_y == 0)
 
2264
        memset(td->left_nnz, 0, sizeof(td->left_nnz));
1676
2265
 
1677
2266
    s->mv_min.x = -MARGIN;
1678
 
    s->mv_max.x = ((s->mb_width  - 1) << 6) + MARGIN;
 
2267
    s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
1679
2268
 
1680
2269
    for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
1681
2270
        // Wait for previous thread to read mb_x+2, and reach mb_y-1.
1682
2271
        if (prev_td != td) {
1683
2272
            if (threadnr != 0) {
1684
 
                check_thread_pos(td, prev_td, mb_x+1, mb_y-1);
 
2273
                check_thread_pos(td, prev_td,
 
2274
                                 mb_x + (is_vp7 ? 2 : 1),
 
2275
                                 mb_y - (is_vp7 ? 2 : 1));
1685
2276
            } else {
1686
 
                check_thread_pos(td, prev_td, (s->mb_width+3) + (mb_x+1), mb_y-1);
 
2277
                check_thread_pos(td, prev_td,
 
2278
                                 mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
 
2279
                                 mb_y - (is_vp7 ? 2 : 1));
1687
2280
            }
1688
2281
        }
1689
2282
 
1690
 
        s->vdsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1691
 
        s->vdsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
 
2283
        s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
 
2284
                         s->linesize, 4);
 
2285
        s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
 
2286
                         dst[2] - dst[1], 2);
1692
2287
 
1693
2288
        if (!s->mb_layout)
1694
2289
            decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
1695
2290
                           prev_frame && prev_frame->seg_map ?
1696
 
                           prev_frame->seg_map->data + mb_xy : NULL, 0);
 
2291
                           prev_frame->seg_map->data + mb_xy : NULL, 0, is_vp7);
1697
2292
 
1698
2293
        prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
1699
2294
 
1700
2295
        if (!mb->skip)
1701
 
            decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz);
 
2296
            decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
1702
2297
 
1703
2298
        if (mb->mode <= MODE_I4x4)
1704
 
            intra_predict(s, td, dst, mb, mb_x, mb_y);
 
2299
            intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
1705
2300
        else
1706
2301
            inter_predict(s, td, dst, mb, mb_x, mb_y);
1707
2302
 
1713
2308
            AV_ZERO64(td->left_nnz);
1714
2309
            AV_WN64(s->top_nnz[mb_x], 0);   // array of 9, so unaligned
1715
2310
 
1716
 
            // Reset DC block predictors if they would exist if the mb had coefficients
 
2311
            /* Reset DC block predictors if they would exist
 
2312
             * if the mb had coefficients */
1717
2313
            if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1718
2314
                td->left_nnz[8]     = 0;
1719
2315
                s->top_nnz[mb_x][8] = 0;
1721
2317
        }
1722
2318
 
1723
2319
        if (s->deblock_filter)
1724
 
            filter_level_for_mb(s, mb, &td->filter_strength[mb_x]);
 
2320
            filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
1725
2321
 
1726
 
        if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs-1) {
 
2322
        if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
1727
2323
            if (s->filter.simple)
1728
 
                backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
 
2324
                backup_mb_border(s->top_border[mb_x + 1], dst[0],
 
2325
                                 NULL, NULL, s->linesize, 0, 1);
1729
2326
            else
1730
 
                backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
 
2327
                backup_mb_border(s->top_border[mb_x + 1], dst[0],
 
2328
                                 dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1731
2329
        }
1732
2330
 
1733
2331
        prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
1734
2332
 
1735
 
        dst[0] += 16;
1736
 
        dst[1] += 8;
1737
 
        dst[2] += 8;
 
2333
        dst[0]      += 16;
 
2334
        dst[1]      += 8;
 
2335
        dst[2]      += 8;
1738
2336
        s->mv_min.x -= 64;
1739
2337
        s->mv_max.x -= 64;
1740
2338
 
1741
 
        if (mb_x == s->mb_width+1) {
1742
 
            update_pos(td, mb_y, s->mb_width+3);
 
2339
        if (mb_x == s->mb_width + 1) {
 
2340
            update_pos(td, mb_y, s->mb_width + 3);
1743
2341
        } else {
1744
2342
            update_pos(td, mb_y, mb_x);
1745
2343
        }
1747
2345
}
1748
2346
 
1749
2347
static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
1750
 
                              int jobnr, int threadnr)
 
2348
                              int jobnr, int threadnr, int is_vp7)
1751
2349
{
1752
2350
    VP8Context *s = avctx->priv_data;
1753
2351
    VP8ThreadData *td = &s->thread_data[threadnr];
1754
 
    int mb_x, mb_y = td->thread_mb_pos>>16, num_jobs = s->num_jobs;
 
2352
    int mb_x, mb_y = td->thread_mb_pos >> 16, num_jobs = s->num_jobs;
1755
2353
    AVFrame *curframe = s->curframe->tf.f;
1756
2354
    VP8Macroblock *mb;
1757
2355
    VP8ThreadData *prev_td, *next_td;
1758
2356
    uint8_t *dst[3] = {
1759
 
        curframe->data[0] + 16*mb_y*s->linesize,
1760
 
        curframe->data[1] +  8*mb_y*s->uvlinesize,
1761
 
        curframe->data[2] +  8*mb_y*s->uvlinesize
 
2357
        curframe->data[0] + 16 * mb_y * s->linesize,
 
2358
        curframe->data[1] +  8 * mb_y * s->uvlinesize,
 
2359
        curframe->data[2] +  8 * mb_y * s->uvlinesize
1762
2360
    };
1763
2361
 
1764
2362
    if (s->mb_layout == 1)
1765
 
        mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
 
2363
        mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
1766
2364
    else
1767
 
        mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
 
2365
        mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
1768
2366
 
1769
 
    if (mb_y == 0) prev_td = td;
1770
 
    else           prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1771
 
    if (mb_y == s->mb_height-1) next_td = td;
1772
 
    else                        next_td = &s->thread_data[(jobnr + 1)%num_jobs];
 
2367
    if (mb_y == 0)
 
2368
        prev_td = td;
 
2369
    else
 
2370
        prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
 
2371
    if (mb_y == s->mb_height - 1)
 
2372
        next_td = td;
 
2373
    else
 
2374
        next_td = &s->thread_data[(jobnr + 1) % num_jobs];
1773
2375
 
1774
2376
    for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
1775
2377
        VP8FilterStrength *f = &td->filter_strength[mb_x];
1776
 
        if (prev_td != td) {
1777
 
            check_thread_pos(td, prev_td, (mb_x+1) + (s->mb_width+3), mb_y-1);
1778
 
        }
 
2378
        if (prev_td != td)
 
2379
            check_thread_pos(td, prev_td,
 
2380
                             (mb_x + 1) + (s->mb_width + 3), mb_y - 1);
1779
2381
        if (next_td != td)
1780
 
            if (next_td != &s->thread_data[0]) {
1781
 
                check_thread_pos(td, next_td, mb_x+1, mb_y+1);
1782
 
            }
 
2382
            if (next_td != &s->thread_data[0])
 
2383
                check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
1783
2384
 
1784
2385
        if (num_jobs == 1) {
1785
2386
            if (s->filter.simple)
1786
 
                backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
 
2387
                backup_mb_border(s->top_border[mb_x + 1], dst[0],
 
2388
                                 NULL, NULL, s->linesize, 0, 1);
1787
2389
            else
1788
 
                backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
 
2390
                backup_mb_border(s->top_border[mb_x + 1], dst[0],
 
2391
                                 dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1789
2392
        }
1790
2393
 
1791
2394
        if (s->filter.simple)
1792
2395
            filter_mb_simple(s, dst[0], f, mb_x, mb_y);
1793
2396
        else
1794
 
            filter_mb(s, dst, f, mb_x, mb_y);
 
2397
            filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
1795
2398
        dst[0] += 16;
1796
2399
        dst[1] += 8;
1797
2400
        dst[2] += 8;
1798
2401
 
1799
 
        update_pos(td, mb_y, (s->mb_width+3) + mb_x);
 
2402
        update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
1800
2403
    }
1801
2404
}
1802
2405
 
1803
 
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
1804
 
                                    int jobnr, int threadnr)
 
2406
static av_always_inline
 
2407
int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
 
2408
                              int threadnr, int is_vp7)
1805
2409
{
1806
2410
    VP8Context *s = avctx->priv_data;
1807
2411
    VP8ThreadData *td = &s->thread_data[jobnr];
1808
2412
    VP8ThreadData *next_td = NULL, *prev_td = NULL;
1809
2413
    VP8Frame *curframe = s->curframe;
1810
2414
    int mb_y, num_jobs = s->num_jobs;
 
2415
 
1811
2416
    td->thread_nr = threadnr;
1812
2417
    for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
1813
 
        if (mb_y >= s->mb_height) break;
1814
 
        td->thread_mb_pos = mb_y<<16;
1815
 
        vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
 
2418
        if (mb_y >= s->mb_height)
 
2419
            break;
 
2420
        td->thread_mb_pos = mb_y << 16;
 
2421
        vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, is_vp7);
1816
2422
        if (s->deblock_filter)
1817
 
            vp8_filter_mb_row(avctx, tdata, jobnr, threadnr);
 
2423
            vp8_filter_mb_row(avctx, tdata, jobnr, threadnr, is_vp7);
1818
2424
        update_pos(td, mb_y, INT_MAX & 0xFFFF);
1819
2425
 
1820
2426
        s->mv_min.y -= 64;
1827
2433
    return 0;
1828
2434
}
1829
2435
 
1830
 
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1831
 
                        AVPacket *avpkt)
 
2436
static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
 
2437
                                    int jobnr, int threadnr)
 
2438
{
 
2439
    return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
 
2440
}
 
2441
 
 
2442
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
 
2443
                                    int jobnr, int threadnr)
 
2444
{
 
2445
    return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
 
2446
}
 
2447
 
 
2448
 
 
2449
static av_always_inline
 
2450
int vp78_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
2451
                      AVPacket *avpkt, int is_vp7)
1832
2452
{
1833
2453
    VP8Context *s = avctx->priv_data;
1834
2454
    int ret, i, referenced, num_jobs;
1835
2455
    enum AVDiscard skip_thresh;
1836
2456
    VP8Frame *av_uninit(curframe), *prev_frame;
1837
2457
 
1838
 
    if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
 
2458
    if (is_vp7)
 
2459
        ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
 
2460
    else
 
2461
        ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
 
2462
 
 
2463
    if (ret < 0)
1839
2464
        goto err;
1840
2465
 
1841
2466
    prev_frame = s->framep[VP56_FRAME_CURRENT];
1842
2467
 
1843
 
    referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1844
 
                                || s->update_altref == VP56_FRAME_CURRENT;
 
2468
    referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT ||
 
2469
                 s->update_altref == VP56_FRAME_CURRENT;
1845
2470
 
1846
 
    skip_thresh = !referenced ? AVDISCARD_NONREF :
1847
 
                    !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
 
2471
    skip_thresh = !referenced ? AVDISCARD_NONREF
 
2472
                              : !s->keyframe ? AVDISCARD_NONKEY
 
2473
                                             : AVDISCARD_ALL;
1848
2474
 
1849
2475
    if (avctx->skip_frame >= skip_thresh) {
1850
2476
        s->invisible = 1;
1858
2484
        if (s->frames[i].tf.f->data[0] &&
1859
2485
            &s->frames[i] != prev_frame &&
1860
2486
            &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1861
 
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
 
2487
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN]   &&
1862
2488
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1863
2489
            vp8_release_frame(s, &s->frames[i]);
1864
2490
 
1865
 
    // find a free buffer
1866
 
    for (i = 0; i < 5; i++)
1867
 
        if (&s->frames[i] != prev_frame &&
1868
 
            &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1869
 
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1870
 
            &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1871
 
            curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1872
 
            break;
1873
 
        }
1874
 
    if (i == 5) {
1875
 
        av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
1876
 
        abort();
1877
 
    }
1878
 
    if (curframe->tf.f->data[0])
1879
 
        vp8_release_frame(s, curframe);
 
2491
    curframe = s->framep[VP56_FRAME_CURRENT] = vp8_find_free_buffer(s);
1880
2492
 
1881
 
    // Given that arithmetic probabilities are updated every frame, it's quite likely
1882
 
    // that the values we have on a random interframe are complete junk if we didn't
1883
 
    // start decode on a keyframe. So just don't display anything rather than junk.
 
2493
    /* Given that arithmetic probabilities are updated every frame, it's quite
 
2494
     * likely that the values we have on a random interframe are complete
 
2495
     * junk if we didn't start decode on a keyframe. So just don't display
 
2496
     * anything rather than junk. */
1884
2497
    if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1885
 
                         !s->framep[VP56_FRAME_GOLDEN] ||
 
2498
                         !s->framep[VP56_FRAME_GOLDEN]   ||
1886
2499
                         !s->framep[VP56_FRAME_GOLDEN2])) {
1887
 
        av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
 
2500
        av_log(avctx, AV_LOG_WARNING,
 
2501
               "Discarding interframe without a prior keyframe!\n");
1888
2502
        ret = AVERROR_INVALIDDATA;
1889
2503
        goto err;
1890
2504
    }
1891
2505
 
1892
2506
    curframe->tf.f->key_frame = s->keyframe;
1893
 
    curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
 
2507
    curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
 
2508
                                            : AV_PICTURE_TYPE_P;
1894
2509
    if ((ret = vp8_alloc_frame(s, curframe, referenced))) {
1895
2510
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1896
2511
        goto err;
1897
2512
    }
1898
2513
 
1899
2514
    // check if golden and altref are swapped
1900
 
    if (s->update_altref != VP56_FRAME_NONE) {
1901
 
        s->next_framep[VP56_FRAME_GOLDEN2]  = s->framep[s->update_altref];
1902
 
    } else {
1903
 
        s->next_framep[VP56_FRAME_GOLDEN2]  = s->framep[VP56_FRAME_GOLDEN2];
1904
 
    }
1905
 
    if (s->update_golden != VP56_FRAME_NONE) {
1906
 
        s->next_framep[VP56_FRAME_GOLDEN]   = s->framep[s->update_golden];
1907
 
    } else {
1908
 
        s->next_framep[VP56_FRAME_GOLDEN]   = s->framep[VP56_FRAME_GOLDEN];
1909
 
    }
1910
 
    if (s->update_last) {
 
2515
    if (s->update_altref != VP56_FRAME_NONE)
 
2516
        s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
 
2517
    else
 
2518
        s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
 
2519
 
 
2520
    if (s->update_golden != VP56_FRAME_NONE)
 
2521
        s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
 
2522
    else
 
2523
        s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
 
2524
 
 
2525
    if (s->update_last)
1911
2526
        s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
1912
 
    } else {
 
2527
    else
1913
2528
        s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
1914
 
    }
1915
 
    s->next_framep[VP56_FRAME_CURRENT]      = curframe;
 
2529
 
 
2530
    s->next_framep[VP56_FRAME_CURRENT] = curframe;
1916
2531
 
1917
2532
    ff_thread_finish_setup(avctx);
1918
2533
 
1919
2534
    s->linesize   = curframe->tf.f->linesize[0];
1920
2535
    s->uvlinesize = curframe->tf.f->linesize[1];
1921
2536
 
1922
 
    memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1923
 
    /* Zero macroblock structures for top/top-left prediction from outside the frame. */
 
2537
    memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
 
2538
    /* Zero macroblock structures for top/top-left prediction
 
2539
     * from outside the frame. */
1924
2540
    if (!s->mb_layout)
1925
 
        memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
 
2541
        memset(s->macroblocks + s->mb_height * 2 - 1, 0,
 
2542
               (s->mb_width + 1) * sizeof(*s->macroblocks));
1926
2543
    if (!s->mb_layout && s->keyframe)
1927
 
        memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
 
2544
        memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
1928
2545
 
1929
2546
    memset(s->ref_count, 0, sizeof(s->ref_count));
1930
2547
 
1931
 
 
1932
2548
    if (s->mb_layout == 1) {
1933
2549
        // Make sure the previous frame has read its segmentation map,
1934
2550
        // if we re-use the same map.
1935
2551
        if (prev_frame && s->segmentation.enabled &&
1936
2552
            !s->segmentation.update_map)
1937
2553
            ff_thread_await_progress(&prev_frame->tf, 1, 0);
1938
 
        vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
 
2554
        if (is_vp7)
 
2555
            vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
 
2556
        else
 
2557
            vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
1939
2558
    }
1940
2559
 
1941
2560
    if (avctx->active_thread_type == FF_THREAD_FRAME)
1949
2568
    s->mv_max.y   = ((s->mb_height - 1) << 6) + MARGIN;
1950
2569
    for (i = 0; i < MAX_THREADS; i++) {
1951
2570
        s->thread_data[i].thread_mb_pos = 0;
1952
 
        s->thread_data[i].wait_mb_pos = INT_MAX;
 
2571
        s->thread_data[i].wait_mb_pos   = INT_MAX;
1953
2572
    }
1954
 
    avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL, num_jobs);
 
2573
    if (is_vp7)
 
2574
        avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL,
 
2575
                        num_jobs);
 
2576
    else
 
2577
        avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL,
 
2578
                        num_jobs);
1955
2579
 
1956
2580
    ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
1957
2581
    memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
1965
2589
    if (!s->invisible) {
1966
2590
        if ((ret = av_frame_ref(data, curframe->tf.f)) < 0)
1967
2591
            return ret;
1968
 
        *got_frame      = 1;
 
2592
        *got_frame = 1;
1969
2593
    }
1970
2594
 
1971
2595
    return avpkt->size;
1974
2598
    return ret;
1975
2599
}
1976
2600
 
 
2601
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
2602
                        AVPacket *avpkt)
 
2603
{
 
2604
    return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP8);
 
2605
}
 
2606
 
 
2607
#if CONFIG_VP7_DECODER
 
2608
static int vp7_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
2609
                            AVPacket *avpkt)
 
2610
{
 
2611
    return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP7);
 
2612
}
 
2613
#endif /* CONFIG_VP7_DECODER */
 
2614
 
1977
2615
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
1978
2616
{
1979
2617
    VP8Context *s = avctx->priv_data;
1997
2635
    return 0;
1998
2636
}
1999
2637
 
2000
 
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
 
2638
static av_always_inline
 
2639
int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
2001
2640
{
2002
2641
    VP8Context *s = avctx->priv_data;
2003
2642
    int ret;
2007
2646
    avctx->internal->allocate_progress = 1;
2008
2647
 
2009
2648
    ff_videodsp_init(&s->vdsp, 8);
2010
 
    ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
2011
 
    ff_vp8dsp_init(&s->vp8dsp);
 
2649
 
 
2650
    ff_vp78dsp_init(&s->vp8dsp);
 
2651
    if (CONFIG_VP7_DECODER && is_vp7) {
 
2652
        ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP7, 8, 1);
 
2653
        ff_vp7dsp_init(&s->vp8dsp);
 
2654
    } else if (CONFIG_VP8_DECODER && !is_vp7) {
 
2655
        ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
 
2656
        ff_vp8dsp_init(&s->vp8dsp);
 
2657
    }
 
2658
 
 
2659
    /* does not change for VP8 */
 
2660
    memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
2012
2661
 
2013
2662
    if ((ret = vp8_init_frames(s)) < 0) {
2014
2663
        ff_vp8_decode_free(avctx);
2018
2667
    return 0;
2019
2668
}
2020
2669
 
 
2670
#if CONFIG_VP7_DECODER
 
2671
static int vp7_decode_init(AVCodecContext *avctx)
 
2672
{
 
2673
    return vp78_decode_init(avctx, IS_VP7);
 
2674
}
 
2675
#endif /* CONFIG_VP7_DECODER */
 
2676
 
 
2677
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
 
2678
{
 
2679
    return vp78_decode_init(avctx, IS_VP8);
 
2680
}
 
2681
 
 
2682
#if CONFIG_VP8_DECODER
2021
2683
static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
2022
2684
{
2023
2685
    VP8Context *s = avctx->priv_data;
2033
2695
    return 0;
2034
2696
}
2035
2697
 
2036
 
#define REBASE(pic) \
2037
 
    pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
 
2698
#define REBASE(pic) pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
2038
2699
 
2039
 
static int vp8_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
 
2700
static int vp8_decode_update_thread_context(AVCodecContext *dst,
 
2701
                                            const AVCodecContext *src)
2040
2702
{
2041
2703
    VP8Context *s = dst->priv_data, *s_src = src->priv_data;
2042
2704
    int i;
2048
2710
        s->mb_height = s_src->mb_height;
2049
2711
    }
2050
2712
 
2051
 
    s->prob[0] = s_src->prob[!s_src->update_probabilities];
 
2713
    s->prob[0]      = s_src->prob[!s_src->update_probabilities];
2052
2714
    s->segmentation = s_src->segmentation;
2053
 
    s->lf_delta = s_src->lf_delta;
 
2715
    s->lf_delta     = s_src->lf_delta;
2054
2716
    memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
2055
2717
 
2056
2718
    for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
2068
2730
 
2069
2731
    return 0;
2070
2732
}
2071
 
 
 
2733
#endif /* CONFIG_VP8_DECODER */
 
2734
 
 
2735
#if CONFIG_VP7_DECODER
 
2736
AVCodec ff_vp7_decoder = {
 
2737
    .name                  = "vp7",
 
2738
    .long_name             = NULL_IF_CONFIG_SMALL("On2 VP7"),
 
2739
    .type                  = AVMEDIA_TYPE_VIDEO,
 
2740
    .id                    = AV_CODEC_ID_VP7,
 
2741
    .priv_data_size        = sizeof(VP8Context),
 
2742
    .init                  = vp7_decode_init,
 
2743
    .close                 = ff_vp8_decode_free,
 
2744
    .decode                = vp7_decode_frame,
 
2745
    .capabilities          = CODEC_CAP_DR1,
 
2746
    .flush                 = vp8_decode_flush,
 
2747
};
 
2748
#endif /* CONFIG_VP7_DECODER */
 
2749
 
 
2750
#if CONFIG_VP8_DECODER
2072
2751
AVCodec ff_vp8_decoder = {
2073
2752
    .name                  = "vp8",
2074
2753
    .long_name             = NULL_IF_CONFIG_SMALL("On2 VP8"),
2083
2762
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
2084
2763
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
2085
2764
};
 
2765
#endif /* CONFIG_VP7_DECODER */