Add APIchanges entry for the libavfilter media format generalization
[libav.git] / libavcodec / vp8.c
CommitLineData
3b636f21
DC
1/**
2 * VP8 compatible video decoder
3 *
4 * Copyright (C) 2010 David Conrad
5 * Copyright (C) 2010 Ronald S. Bultje
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include "avcodec.h"
25#include "vp56.h"
26#include "vp8data.h"
27#include "vp8dsp.h"
28#include "h264pred.h"
29#include "rectangle.h"
30
31typedef struct {
968570d6
JGG
32 uint8_t filter_level;
33 uint8_t inner_limit;
34} VP8FilterStrength;
35
36typedef struct {
3b636f21
DC
37 uint8_t segment;
38 uint8_t skip;
39 // todo: make it possible to check for at least (i4x4 or split_mv)
40 // in one op. are others needed?
41 uint8_t mode;
42 uint8_t ref_frame;
43 uint8_t partitioning;
44 VP56mv mv;
45 VP56mv bmv[16];
46} VP8Macroblock;
47
48typedef struct {
49 AVCodecContext *avctx;
50 DSPContext dsp;
51 VP8DSPContext vp8dsp;
52 H264PredContext hpc;
0ef1dbed 53 vp8_mc_func put_pixels_tab[3][3][3];
3b636f21
DC
54 AVFrame frames[4];
55 AVFrame *framep[4];
56 uint8_t *edge_emu_buffer;
57 VP56RangeCoder c; ///< header context, includes mb modes and motion vectors
58 int profile;
59
60 int mb_width; /* number of horizontal MB */
61 int mb_height; /* number of vertical MB */
62 int linesize;
63 int uvlinesize;
64
65 int keyframe;
66 int invisible;
67 int update_last; ///< update VP56_FRAME_PREVIOUS with the current one
68 int update_golden; ///< VP56_FRAME_NONE if not updated, or which frame to copy if so
69 int update_altref;
9ac831c2 70 int deblock_filter;
3b636f21
DC
71
72 /**
73 * If this flag is not set, all the probability updates
74 * are discarded after this frame is decoded.
75 */
76 int update_probabilities;
77
78 /**
79 * All coefficients are contained in separate arith coding contexts.
80 * There can be 1, 2, 4, or 8 of these after the header context.
81 */
82 int num_coeff_partitions;
83 VP56RangeCoder coeff_partition[8];
84
85 VP8Macroblock *macroblocks;
86 VP8Macroblock *macroblocks_base;
968570d6 87 VP8FilterStrength *filter_strength;
3b636f21
DC
88 int mb_stride;
89
90 uint8_t *intra4x4_pred_mode;
91 uint8_t *intra4x4_pred_mode_base;
92 int b4_stride;
93
94 /**
9ac831c2
DC
95 * Cache of the top row needed for intra prediction
96 * 16 for luma, 8 for each chroma plane
97 */
98 uint8_t (*top_border)[16+8+8];
99
100 /**
3b636f21
DC
101 * For coeff decode, we need to know whether the above block had non-zero
102 * coefficients. This means for each macroblock, we need data for 4 luma
103 * blocks, 2 u blocks, 2 v blocks, and the luma dc block, for a total of 9
104 * per macroblock. We keep the last row in top_nnz.
105 */
106 uint8_t (*top_nnz)[9];
107 DECLARE_ALIGNED(8, uint8_t, left_nnz)[9];
108
109 /**
110 * This is the index plus one of the last non-zero coeff
111 * for each of the blocks in the current macroblock.
112 * So, 0 -> no coeffs
113 * 1 -> dc-only (special transform)
114 * 2+-> full transform
115 */
116 DECLARE_ALIGNED(16, uint8_t, non_zero_count_cache)[6][4];
117 DECLARE_ALIGNED(16, DCTELEM, block)[6][4][16];
d1c58fce 118 uint8_t intra4x4_pred_mode_mb[16];
3b636f21
DC
119
120 int chroma_pred_mode; ///< 8x8c pred mode of the current macroblock
121
122 int mbskip_enabled;
123 int sign_bias[4]; ///< one state [0, 1] per ref frame type
124
125 /**
126 * Base parameters for segmentation, i.e. per-macroblock parameters.
127 * These must be kept unchanged even if segmentation is not used for
128 * a frame, since the values persist between interframes.
129 */
130 struct {
131 int enabled;
132 int absolute_vals;
133 int update_map;
134 int8_t base_quant[4];
135 int8_t filter_level[4]; ///< base loop filter level
136 } segmentation;
137
138 /**
139 * Macroblocks can have one of 4 different quants in a frame when
140 * segmentation is enabled.
141 * If segmentation is disabled, only the first segment's values are used.
142 */
143 struct {
144 // [0] - DC qmul [1] - AC qmul
145 int16_t luma_qmul[2];
146 int16_t luma_dc_qmul[2]; ///< luma dc-only block quant
147 int16_t chroma_qmul[2];
148 } qmat[4];
149
150 struct {
151 int simple;
152 int level;
153 int sharpness;
154 } filter;
155
156 struct {
157 int enabled; ///< whether each mb can have a different strength based on mode/ref
158
159 /**
160 * filter strength adjustment for the following macroblock modes:
161 * [0] - i4x4
162 * [1] - zero mv
163 * [2] - inter modes except for zero or split mv
164 * [3] - split mv
165 * i16x16 modes never have any adjustment
166 */
167 int8_t mode[4];
168
169 /**
170 * filter strength adjustment for macroblocks that reference:
171 * [0] - intra / VP56_FRAME_CURRENT
172 * [1] - VP56_FRAME_PREVIOUS
173 * [2] - VP56_FRAME_GOLDEN
174 * [3] - altref / VP56_FRAME_GOLDEN2
175 */
176 int8_t ref[4];
177 } lf_delta;
178
179 /**
180 * These are all of the updatable probabilities for binary decisions.
181 * They are only implictly reset on keyframes, making it quite likely
182 * for an interframe to desync if a prior frame's header was corrupt
183 * or missing outright!
184 */
185 struct {
186 uint8_t segmentid[3];
187 uint8_t mbskip;
188 uint8_t intra;
189 uint8_t last;
190 uint8_t golden;
191 uint8_t pred16x16[4];
192 uint8_t pred8x8c[3];
193 uint8_t token[4][8][3][NUM_DCT_TOKENS-1];
194 uint8_t mvc[2][19];
195 } prob[2];
196} VP8Context;
197
198#define RL24(p) (AV_RL16(p) + ((p)[2] << 16))
199
200static void vp8_decode_flush(AVCodecContext *avctx)
201{
202 VP8Context *s = avctx->priv_data;
203 int i;
204
205 for (i = 0; i < 4; i++)
206 if (s->frames[i].data[0])
207 avctx->release_buffer(avctx, &s->frames[i]);
208 memset(s->framep, 0, sizeof(s->framep));
209
210 av_freep(&s->macroblocks_base);
211 av_freep(&s->intra4x4_pred_mode_base);
212 av_freep(&s->top_nnz);
213 av_freep(&s->edge_emu_buffer);
9ac831c2 214 av_freep(&s->top_border);
3b636f21
DC
215
216 s->macroblocks = NULL;
217 s->intra4x4_pred_mode = NULL;
218}
219
220static int update_dimensions(VP8Context *s, int width, int height)
221{
222 int i;
223
224 if (avcodec_check_dimensions(s->avctx, width, height))
225 return AVERROR_INVALIDDATA;
226
227 vp8_decode_flush(s->avctx);
228
229 avcodec_set_dimensions(s->avctx, width, height);
230
231 s->mb_width = (s->avctx->coded_width +15) / 16;
232 s->mb_height = (s->avctx->coded_height+15) / 16;
233
234 // we allocate a border around the top/left of intra4x4 modes
235 // this is 4 blocks for intra4x4 to keep 4-byte alignment for fill_rectangle
236 s->mb_stride = s->mb_width+1;
237 s->b4_stride = 4*s->mb_stride;
238
239 s->macroblocks_base = av_mallocz(s->mb_stride*(s->mb_height+1)*sizeof(*s->macroblocks));
968570d6 240 s->filter_strength = av_mallocz(s->mb_stride*sizeof(*s->filter_strength));
3b636f21
DC
241 s->intra4x4_pred_mode_base = av_mallocz(s->b4_stride*(4*s->mb_height+1));
242 s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
9ac831c2 243 s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
3b636f21 244
968570d6 245 if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_base || !s->top_nnz || !s->top_border)
b6c420ce
DC
246 return AVERROR(ENOMEM);
247
3b636f21
DC
248 s->macroblocks = s->macroblocks_base + 1 + s->mb_stride;
249 s->intra4x4_pred_mode = s->intra4x4_pred_mode_base + 4 + s->b4_stride;
250
251 memset(s->intra4x4_pred_mode_base, DC_PRED, s->b4_stride);
252 for (i = 0; i < 4*s->mb_height; i++)
253 s->intra4x4_pred_mode[i*s->b4_stride-1] = DC_PRED;
254
255 return 0;
256}
257
258static void parse_segment_info(VP8Context *s)
259{
260 VP56RangeCoder *c = &s->c;
261 int i;
262
263 s->segmentation.update_map = vp8_rac_get(c);
264
265 if (vp8_rac_get(c)) { // update segment feature data
266 s->segmentation.absolute_vals = vp8_rac_get(c);
267
268 for (i = 0; i < 4; i++)
269 s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
270
271 for (i = 0; i < 4; i++)
272 s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
273 }
274 if (s->segmentation.update_map)
275 for (i = 0; i < 3; i++)
276 s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
277}
278
279static void update_lf_deltas(VP8Context *s)
280{
281 VP56RangeCoder *c = &s->c;
282 int i;
283
284 for (i = 0; i < 4; i++)
285 s->lf_delta.ref[i] = vp8_rac_get_sint(c, 6);
286
287 for (i = 0; i < 4; i++)
288 s->lf_delta.mode[i] = vp8_rac_get_sint(c, 6);
289}
290
291static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
292{
293 const uint8_t *sizes = buf;
294 int i;
295
296 s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
297
298 buf += 3*(s->num_coeff_partitions-1);
299 buf_size -= 3*(s->num_coeff_partitions-1);
300 if (buf_size < 0)
301 return -1;
302
303 for (i = 0; i < s->num_coeff_partitions-1; i++) {
304 int size = RL24(sizes + 3*i);
305 if (buf_size - size < 0)
306 return -1;
307
308 vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
309 buf += size;
310 buf_size -= size;
311 }
312 vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
313
314 return 0;
315}
316
317static void get_quants(VP8Context *s)
318{
319 VP56RangeCoder *c = &s->c;
320 int i, base_qi;
321
322 int yac_qi = vp8_rac_get_uint(c, 7);
323 int ydc_delta = vp8_rac_get_sint(c, 4);
324 int y2dc_delta = vp8_rac_get_sint(c, 4);
325 int y2ac_delta = vp8_rac_get_sint(c, 4);
326 int uvdc_delta = vp8_rac_get_sint(c, 4);
327 int uvac_delta = vp8_rac_get_sint(c, 4);
328
329 for (i = 0; i < 4; i++) {
330 if (s->segmentation.enabled) {
331 base_qi = s->segmentation.base_quant[i];
332 if (!s->segmentation.absolute_vals)
333 base_qi += yac_qi;
334 } else
335 base_qi = yac_qi;
336
337 s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + ydc_delta , 0, 127)];
338 s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi , 0, 127)];
339 s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip(base_qi + y2dc_delta, 0, 127)];
340 s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip(base_qi + y2ac_delta, 0, 127)] / 100;
341 s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip(base_qi + uvdc_delta, 0, 127)];
342 s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip(base_qi + uvac_delta, 0, 127)];
343
344 s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
345 s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
346 }
347}
348
349/**
350 * Determine which buffers golden and altref should be updated with after this frame.
351 * The spec isn't clear here, so I'm going by my understanding of what libvpx does
352 *
353 * Intra frames update all 3 references
354 * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
355 * If the update (golden|altref) flag is set, it's updated with the current frame
356 * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
357 * If the flag is not set, the number read means:
358 * 0: no update
359 * 1: VP56_FRAME_PREVIOUS
360 * 2: update golden with altref, or update altref with golden
361 */
362static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
363{
364 VP56RangeCoder *c = &s->c;
365
366 if (update)
367 return VP56_FRAME_CURRENT;
368
369 switch (vp8_rac_get_uint(c, 2)) {
370 case 1:
371 return VP56_FRAME_PREVIOUS;
372 case 2:
373 return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
374 }
375 return VP56_FRAME_NONE;
376}
377
378static void update_refs(VP8Context *s)
379{
380 VP56RangeCoder *c = &s->c;
381
382 int update_golden = vp8_rac_get(c);
383 int update_altref = vp8_rac_get(c);
384
385 s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
386 s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
387}
388
389static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
390{
391 VP56RangeCoder *c = &s->c;
392 int header_size, hscale, vscale, i, j, k, l, ret;
393 int width = s->avctx->width;
394 int height = s->avctx->height;
395
396 s->keyframe = !(buf[0] & 1);
397 s->profile = (buf[0]>>1) & 7;
398 s->invisible = !(buf[0] & 0x10);
399 header_size = RL24(buf) >> 5;
400 buf += 3;
401 buf_size -= 3;
402
0ef1dbed
DC
403 if (s->profile > 3)
404 av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
405
406 if (!s->profile)
407 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
408 else // profile 1-3 use bilinear, 4+ aren't defined so whatever
409 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
3b636f21
DC
410
411 if (header_size > buf_size - 7*s->keyframe) {
412 av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
413 return AVERROR_INVALIDDATA;
414 }
415
416 if (s->keyframe) {
417 if (RL24(buf) != 0x2a019d) {
418 av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", RL24(buf));
419 return AVERROR_INVALIDDATA;
420 }
421 width = AV_RL16(buf+3) & 0x3fff;
422 height = AV_RL16(buf+5) & 0x3fff;
423 hscale = buf[4] >> 6;
424 vscale = buf[6] >> 6;
425 buf += 7;
426 buf_size -= 7;
427
92a54426
MR
428 if (hscale || vscale)
429 av_log_missing_feature(s->avctx, "Upscaling", 1);
430
3b636f21
DC
431 s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
432 memcpy(s->prob->token , vp8_token_default_probs , sizeof(s->prob->token));
433 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
434 memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
435 memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
436 memset(&s->segmentation, 0, sizeof(s->segmentation));
437 }
438
439 if (!s->macroblocks_base || /* first frame */
440 width != s->avctx->width || height != s->avctx->height) {
441 if ((ret = update_dimensions(s, width, height) < 0))
442 return ret;
443 }
444
445 vp56_init_range_decoder(c, buf, header_size);
446 buf += header_size;
447 buf_size -= header_size;
448
449 if (s->keyframe) {
450 if (vp8_rac_get(c))
451 av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
452 vp8_rac_get(c); // whether we can skip clamping in dsp functions
453 }
454
455 if ((s->segmentation.enabled = vp8_rac_get(c)))
456 parse_segment_info(s);
457 else
458 s->segmentation.update_map = 0; // FIXME: move this to some init function?
459
460 s->filter.simple = vp8_rac_get(c);
461 s->filter.level = vp8_rac_get_uint(c, 6);
462 s->filter.sharpness = vp8_rac_get_uint(c, 3);
463
464 if ((s->lf_delta.enabled = vp8_rac_get(c)))
465 if (vp8_rac_get(c))
466 update_lf_deltas(s);
467
468 if (setup_partitions(s, buf, buf_size)) {
469 av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
470 return AVERROR_INVALIDDATA;
471 }
472
473 get_quants(s);
474
475 if (!s->keyframe) {
476 update_refs(s);
477 s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
478 s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
479 }
480
481 // if we aren't saving this frame's probabilities for future frames,
482 // make a copy of the current probabilities
483 if (!(s->update_probabilities = vp8_rac_get(c)))
484 s->prob[1] = s->prob[0];
485
486 s->update_last = s->keyframe || vp8_rac_get(c);
487
488 for (i = 0; i < 4; i++)
489 for (j = 0; j < 8; j++)
490 for (k = 0; k < 3; k++)
491 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
492 if (vp56_rac_get_prob(c, vp8_token_update_probs[i][j][k][l]))
493 s->prob->token[i][j][k][l] = vp8_rac_get_uint(c, 8);
494
495 if ((s->mbskip_enabled = vp8_rac_get(c)))
496 s->prob->mbskip = vp8_rac_get_uint(c, 8);
497
498 if (!s->keyframe) {
499 s->prob->intra = vp8_rac_get_uint(c, 8);
500 s->prob->last = vp8_rac_get_uint(c, 8);
501 s->prob->golden = vp8_rac_get_uint(c, 8);
502
503 if (vp8_rac_get(c))
504 for (i = 0; i < 4; i++)
505 s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
506 if (vp8_rac_get(c))
507 for (i = 0; i < 3; i++)
508 s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
509
510 // 17.2 MV probability update
511 for (i = 0; i < 2; i++)
512 for (j = 0; j < 19; j++)
513 if (vp56_rac_get_prob(c, vp8_mv_update_prob[i][j]))
514 s->prob->mvc[i][j] = vp8_rac_get_nn(c);
515 }
516
517 return 0;
518}
519
520static inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src,
521 int mb_x, int mb_y)
522{
523#define MARGIN (16 << 2)
524 dst->x = av_clip(src->x, -((mb_x << 6) + MARGIN),
525 ((s->mb_width - 1 - mb_x) << 6) + MARGIN);
526 dst->y = av_clip(src->y, -((mb_y << 6) + MARGIN),
527 ((s->mb_height - 1 - mb_y) << 6) + MARGIN);
528}
529
530static void find_near_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
702e8d33 531 VP56mv near[2], VP56mv *best, uint8_t cnt[4])
3b636f21 532{
702e8d33
JGG
533 int mb_stride = s->mb_stride;
534 VP8Macroblock *mb_edge[3] = { mb - mb_stride /* top */,
535 mb - 1 /* left */,
536 mb - mb_stride - 1 /* top-left */ };
3b636f21
DC
537 enum { EDGE_TOP, EDGE_LEFT, EDGE_TOPLEFT };
538 VP56mv near_mv[4] = {{ 0 }};
539 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
702e8d33 540 int idx = CNT_ZERO;
3b636f21 541 int best_idx = CNT_ZERO;
702e8d33
JGG
542 int cur_sign_bias = s->sign_bias[mb->ref_frame];
543 int *sign_bias = s->sign_bias;
3b636f21
DC
544
545 /* Process MB on top, left and top-left */
702e8d33
JGG
546 #define MV_EDGE_CHECK(n)\
547 {\
548 VP8Macroblock *edge = mb_edge[n];\
549 int edge_ref = edge->ref_frame;\
550 if (edge_ref != VP56_FRAME_CURRENT) {\
551 uint32_t mv = AV_RN32A(&edge->mv);\
552 if (mv) {\
553 if (cur_sign_bias != sign_bias[edge_ref]) {\
554 /* SWAR negate of the values in mv. */\
555 mv = ((mv&0x80008000) + 0x00010001) ^ (mv&0x7fff7fff);\
556 }\
557 if (!n || mv != AV_RN32A(&near_mv[idx]))\
558 AV_WN32A(&near_mv[++idx], mv);\
559 cnt[idx] += 1 + (n != 2);\
560 } else\
561 cnt[CNT_ZERO] += 1 + (n != 2);\
562 }\
3b636f21 563 }
702e8d33
JGG
564 MV_EDGE_CHECK(0)
565 MV_EDGE_CHECK(1)
566 MV_EDGE_CHECK(2)
3b636f21 567
702e8d33
JGG
568 /* If we have three distinct MVs, merge first and last if they're the same */
569 if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1+EDGE_TOP]) == AV_RN32A(&near_mv[1+EDGE_TOPLEFT]))
3b636f21
DC
570 cnt[CNT_NEAREST] += 1;
571
572 cnt[CNT_SPLITMV] = ((mb_edge[EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
573 (mb_edge[EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
574 (mb_edge[EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
575
576 /* Swap near and nearest if necessary */
577 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
702e8d33
JGG
578 FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
579 FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
3b636f21
DC
580 }
581
582 /* Choose the best mv out of 0,0 and the nearest mv */
583 if (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])
584 best_idx = CNT_NEAREST;
585
586 clamp_mv(s, best, &near_mv[best_idx], mb_x, mb_y);
587 near[0] = near_mv[CNT_NEAREST];
588 near[1] = near_mv[CNT_NEAR];
589}
590
591/**
592 * Motion vector coding, 17.1.
593 */
594static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
595{
596 int x = 0;
597
598 if (vp56_rac_get_prob(c, p[0])) {
599 int i;
600
601 for (i = 0; i < 3; i++)
602 x += vp56_rac_get_prob(c, p[9 + i]) << i;
603 for (i = 9; i > 3; i--)
604 x += vp56_rac_get_prob(c, p[9 + i]) << i;
605 if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
606 x += 8;
607 } else
608 x = vp8_rac_get_tree(c, vp8_small_mvtree, &p[2]);
609
610 return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
611}
612
7bf254c4 613static const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
3b636f21 614{
7bf254c4
JGG
615 if (left == top)
616 return vp8_submv_prob[4-!!left];
617 if (!top)
3b636f21 618 return vp8_submv_prob[2];
7bf254c4 619 return vp8_submv_prob[1-!!left];
3b636f21
DC
620}
621
622/**
623 * Split motion vector prediction, 16.4.
7ed06b2b 624 * @returns the number of motion vectors parsed (2, 4 or 16)
3b636f21 625 */
7ed06b2b 626static int decode_splitmvs(VP8Context *s, VP56RangeCoder *c,
3b636f21
DC
627 VP8Macroblock *mb, VP56mv *base_mv)
628{
629 int part_idx = mb->partitioning =
630 vp8_rac_get_tree(c, vp8_mbsplit_tree, vp8_mbsplit_prob);
631 int n, num = vp8_mbsplit_count[part_idx];
7bf254c4
JGG
632 VP8Macroblock *top_mb = &mb[-s->mb_stride];
633 VP8Macroblock *left_mb = &mb[-1];
634 const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
635 *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
636 *mbsplits_cur = vp8_mbsplits[part_idx],
7ed06b2b 637 *firstidx = vp8_mbfirstidx[part_idx];
7bf254c4
JGG
638 VP56mv *top_mv = top_mb->bmv;
639 VP56mv *left_mv = left_mb->bmv;
640 VP56mv *cur_mv = mb->bmv;
3b636f21
DC
641
642 for (n = 0; n < num; n++) {
7ed06b2b 643 int k = firstidx[n];
7bf254c4 644 uint32_t left, above;
7ed06b2b
RB
645 const uint8_t *submv_prob;
646
7bf254c4
JGG
647 if (!(k & 3))
648 left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
649 else
650 left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
651 if (k <= 3)
652 above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
653 else
654 above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
7ed06b2b
RB
655
656 submv_prob = get_submv_prob(left, above);
3b636f21
DC
657
658 switch (vp8_rac_get_tree(c, vp8_submv_ref_tree, submv_prob)) {
659 case VP8_SUBMVMODE_NEW4X4:
7ed06b2b
RB
660 mb->bmv[n].y = base_mv->y + read_mv_component(c, s->prob->mvc[0]);
661 mb->bmv[n].x = base_mv->x + read_mv_component(c, s->prob->mvc[1]);
3b636f21
DC
662 break;
663 case VP8_SUBMVMODE_ZERO4X4:
7bf254c4 664 AV_WN32A(&mb->bmv[n], 0);
3b636f21
DC
665 break;
666 case VP8_SUBMVMODE_LEFT4X4:
7bf254c4 667 AV_WN32A(&mb->bmv[n], left);
3b636f21
DC
668 break;
669 case VP8_SUBMVMODE_TOP4X4:
7bf254c4 670 AV_WN32A(&mb->bmv[n], above);
3b636f21
DC
671 break;
672 }
3b636f21 673 }
7ed06b2b
RB
674
675 return num;
3b636f21
DC
676}
677
678static inline void decode_intra4x4_modes(VP56RangeCoder *c, uint8_t *intra4x4,
679 int stride, int keyframe)
680{
d1c58fce 681 int x, y, t, l, i;
3b636f21 682
d1c58fce
JGG
683 if (keyframe) {
684 const uint8_t *ctx;
685 for (y = 0; y < 4; y++) {
686 for (x = 0; x < 4; x++) {
3b636f21
DC
687 t = intra4x4[x - stride];
688 l = intra4x4[x - 1];
689 ctx = vp8_pred4x4_prob_intra[t][l];
d1c58fce 690 intra4x4[x] = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
3b636f21 691 }
d1c58fce 692 intra4x4 += stride;
3b636f21 693 }
d1c58fce
JGG
694 } else {
695 for (i = 0; i < 16; i++)
696 intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
3b636f21
DC
697 }
698}
699
700static void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
701 uint8_t *intra4x4)
702{
703 VP56RangeCoder *c = &s->c;
3b636f21
DC
704
705 if (s->segmentation.update_map)
706 mb->segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
707
708 mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
709
710 if (s->keyframe) {
711 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
712
713 if (mb->mode == MODE_I4x4) {
714 decode_intra4x4_modes(c, intra4x4, s->b4_stride, 1);
715 } else
716 fill_rectangle(intra4x4, 4, 4, s->b4_stride, vp8_pred4x4_mode[mb->mode], 1);
717
718 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
719 mb->ref_frame = VP56_FRAME_CURRENT;
720 } else if (vp56_rac_get_prob(c, s->prob->intra)) {
721 VP56mv near[2], best;
702e8d33 722 uint8_t cnt[4] = { 0 };
3b636f21
DC
723 uint8_t p[4];
724
725 // inter MB, 16.2
726 if (vp56_rac_get_prob(c, s->prob->last))
727 mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
728 VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
729 else
730 mb->ref_frame = VP56_FRAME_PREVIOUS;
731
732 // motion vectors, 16.3
733 find_near_mvs(s, mb, mb_x, mb_y, near, &best, cnt);
702e8d33
JGG
734 p[0] = vp8_mode_contexts[cnt[0]][0];
735 p[1] = vp8_mode_contexts[cnt[1]][1];
736 p[2] = vp8_mode_contexts[cnt[2]][2];
737 p[3] = vp8_mode_contexts[cnt[3]][3];
3b636f21
DC
738 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_mvinter, p);
739 switch (mb->mode) {
740 case VP8_MVMODE_SPLIT:
7ed06b2b 741 mb->mv = mb->bmv[decode_splitmvs(s, c, mb, &best) - 1];
3b636f21
DC
742 break;
743 case VP8_MVMODE_ZERO:
744 mb->mv.x = 0;
745 mb->mv.y = 0;
746 break;
747 case VP8_MVMODE_NEAREST:
748 clamp_mv(s, &mb->mv, &near[0], mb_x, mb_y);
749 break;
750 case VP8_MVMODE_NEAR:
751 clamp_mv(s, &mb->mv, &near[1], mb_x, mb_y);
752 break;
753 case VP8_MVMODE_NEW:
754 mb->mv.y = best.y + read_mv_component(c, s->prob->mvc[0]);
755 mb->mv.x = best.x + read_mv_component(c, s->prob->mvc[1]);
756 break;
757 }
758 if (mb->mode != VP8_MVMODE_SPLIT) {
7ed06b2b
RB
759 mb->partitioning = VP8_SPLITMVMODE_NONE;
760 mb->bmv[0] = mb->mv;
3b636f21
DC
761 }
762 } else {
763 // intra MB, 16.1
764 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
765
158e062c 766 if (mb->mode == MODE_I4x4)
d1c58fce 767 decode_intra4x4_modes(c, intra4x4, 4, 0);
3b636f21
DC
768
769 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
770 mb->ref_frame = VP56_FRAME_CURRENT;
771 }
772}
773
774/**
e394953e
RB
775 * @param c arithmetic bitstream reader context
776 * @param block destination for block coefficients
777 * @param probs probabilities to use when reading trees from the bitstream
3b636f21
DC
778 * @param i initial coeff index, 0 unless a separate DC block is coded
779 * @param zero_nhood the initial prediction context for number of surrounding
780 * all-zero blocks (only left/top, so 0-2)
3fa76268 781 * @param qmul array holding the dc/ac dequant factor at position 0/1
3b636f21
DC
782 * @return 0 if no coeffs were decoded
783 * otherwise, the index of the last coeff decoded plus one
784 */
785static int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
786 uint8_t probs[8][3][NUM_DCT_TOKENS-1],
787 int i, int zero_nhood, int16_t qmul[2])
788{
789 int token, nonzero = 0;
790 int offset = 0;
791
792 for (; i < 16; i++) {
793 token = vp8_rac_get_tree_with_offset(c, vp8_coeff_tree, probs[vp8_coeff_band[i]][zero_nhood], offset);
794
795 if (token == DCT_EOB)
796 break;
797 else if (token >= DCT_CAT1) {
798 int cat = token-DCT_CAT1;
799 token = vp8_rac_get_coeff(c, vp8_dct_cat_prob[cat]);
800 token += vp8_dct_cat_offset[cat];
801 }
802
803 // after the first token, the non-zero prediction context becomes
804 // based on the last decoded coeff
805 if (!token) {
806 zero_nhood = 0;
807 offset = 1;
808 continue;
809 } else if (token == 1)
810 zero_nhood = 1;
811 else
812 zero_nhood = 2;
813
814 // todo: full [16] qmat? load into register?
815 block[zigzag_scan[i]] = (vp8_rac_get(c) ? -token : token) * qmul[!!i];
816 nonzero = i+1;
817 offset = 0;
818 }
819 return nonzero;
820}
821
822static void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
823 uint8_t t_nnz[9], uint8_t l_nnz[9])
824{
825 LOCAL_ALIGNED_16(DCTELEM, dc,[16]);
826 int i, x, y, luma_start = 0, luma_ctx = 3;
827 int nnz_pred, nnz, nnz_total = 0;
828 int segment = s->segmentation.enabled ? mb->segment : 0;
829
830 s->dsp.clear_blocks((DCTELEM *)s->block);
831
832 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
833 AV_ZERO128(dc);
834 AV_ZERO128(dc+8);
835 nnz_pred = t_nnz[8] + l_nnz[8];
836
837 // decode DC values and do hadamard
838 nnz = decode_block_coeffs(c, dc, s->prob->token[1], 0, nnz_pred,
839 s->qmat[segment].luma_dc_qmul);
840 l_nnz[8] = t_nnz[8] = !!nnz;
841 nnz_total += nnz;
842 s->vp8dsp.vp8_luma_dc_wht(s->block, dc);
843 luma_start = 1;
844 luma_ctx = 0;
845 }
846
847 // luma blocks
848 for (y = 0; y < 4; y++)
849 for (x = 0; x < 4; x++) {
850 nnz_pred = l_nnz[y] + t_nnz[x];
851 nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
852 nnz_pred, s->qmat[segment].luma_qmul);
853 // nnz+luma_start may be one more than the actual last index, but we don't care
854 s->non_zero_count_cache[y][x] = nnz + luma_start;
855 t_nnz[x] = l_nnz[y] = !!nnz;
856 nnz_total += nnz;
857 }
858
859 // chroma blocks
860 // TODO: what to do about dimensions? 2nd dim for luma is x,
861 // but for chroma it's (y<<1)|x
862 for (i = 4; i < 6; i++)
863 for (y = 0; y < 2; y++)
864 for (x = 0; x < 2; x++) {
865 nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
866 nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
867 nnz_pred, s->qmat[segment].chroma_qmul);
868 s->non_zero_count_cache[i][(y<<1)+x] = nnz;
869 t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
870 nnz_total += nnz;
871 }
872
873 // if there were no coded coeffs despite the macroblock not being marked skip,
874 // we MUST not do the inner loop filter and should not do IDCT
875 // Since skip isn't used for bitstream prediction, just manually set it.
876 if (!nnz_total)
877 mb->skip = 1;
878}
879
9ac831c2
DC
880static av_always_inline
881void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
882 int linesize, int uvlinesize, int simple)
883{
884 AV_COPY128(top_border, src_y + 15*linesize);
885 if (!simple) {
886 AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
887 AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
888 }
889}
890
891static av_always_inline
892void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
893 int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
894 int simple, int xchg)
895{
896 uint8_t *top_border_m1 = top_border-32; // for TL prediction
897 src_y -= linesize;
898 src_cb -= uvlinesize;
899 src_cr -= uvlinesize;
900
096971e8
MR
901#define XCHG(a,b,xchg) do { \
902 if (xchg) AV_SWAP64(b,a); \
903 else AV_COPY64(b,a); \
904 } while (0)
9ac831c2
DC
905
906 XCHG(top_border_m1+8, src_y-8, xchg);
907 XCHG(top_border, src_y, xchg);
908 XCHG(top_border+8, src_y+8, 1);
070ce7ef 909 if (mb_x < mb_width-1)
9ac831c2 910 XCHG(top_border+32, src_y+16, 1);
070ce7ef 911
9ac831c2
DC
912 // only copy chroma for normal loop filter
913 // or to initialize the top row to 127
914 if (!simple || !mb_y) {
915 XCHG(top_border_m1+16, src_cb-8, xchg);
916 XCHG(top_border_m1+24, src_cr-8, xchg);
917 XCHG(top_border+16, src_cb, 1);
918 XCHG(top_border+24, src_cr, 1);
919 }
920}
921
3b636f21
DC
922static int check_intra_pred_mode(int mode, int mb_x, int mb_y)
923{
924 if (mode == DC_PRED8x8) {
925 if (!(mb_x|mb_y))
926 mode = DC_128_PRED8x8;
927 else if (!mb_y)
928 mode = LEFT_DC_PRED8x8;
929 else if (!mb_x)
930 mode = TOP_DC_PRED8x8;
931 }
932 return mode;
933}
934
935static void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
d1c58fce 936 uint8_t *intra4x4, int mb_x, int mb_y)
3b636f21
DC
937{
938 int x, y, mode, nnz, tr;
939
9ac831c2
DC
940 // for the first row, we need to run xchg_mb_border to init the top edge to 127
941 // otherwise, skip it if we aren't going to deblock
942 if (s->deblock_filter || !mb_y)
943 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
944 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
945 s->filter.simple, 1);
946
3b636f21
DC
947 if (mb->mode < MODE_I4x4) {
948 mode = check_intra_pred_mode(mb->mode, mb_x, mb_y);
949 s->hpc.pred16x16[mode](dst[0], s->linesize);
950 } else {
951 uint8_t *ptr = dst[0];
d1c58fce 952 int stride = s->keyframe ? s->b4_stride : 4;
3b636f21
DC
953
954 // all blocks on the right edge of the macroblock use bottom edge
955 // the top macroblock for their topright edge
956 uint8_t *tr_right = ptr - s->linesize + 16;
957
958 // if we're on the right edge of the frame, said edge is extended
959 // from the top macroblock
960 if (mb_x == s->mb_width-1) {
961 tr = tr_right[-1]*0x01010101;
962 tr_right = (uint8_t *)&tr;
963 }
964
965 for (y = 0; y < 4; y++) {
966 uint8_t *topright = ptr + 4 - s->linesize;
967 for (x = 0; x < 4; x++) {
968 if (x == 3)
969 topright = tr_right;
970
d1c58fce 971 s->hpc.pred4x4[intra4x4[x]](ptr+4*x, topright, s->linesize);
3b636f21
DC
972
973 nnz = s->non_zero_count_cache[y][x];
974 if (nnz) {
975 if (nnz == 1)
976 s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
977 else
978 s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
979 }
980 topright += 4;
981 }
982
983 ptr += 4*s->linesize;
d1c58fce 984 intra4x4 += stride;
3b636f21
DC
985 }
986 }
987
988 mode = check_intra_pred_mode(s->chroma_pred_mode, mb_x, mb_y);
989 s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
990 s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
9ac831c2
DC
991
992 if (s->deblock_filter || !mb_y)
993 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
994 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
995 s->filter.simple, 0);
3b636f21
DC
996}
997
998/**
999 * Generic MC function.
1000 *
1001 * @param s VP8 decoding context
1002 * @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
1003 * @param dst target buffer for block data at block position
1004 * @param src reference picture buffer at origin (0, 0)
1005 * @param mv motion vector (relative to block position) to get pixel data from
1006 * @param x_off horizontal position of block from origin (0, 0)
1007 * @param y_off vertical position of block from origin (0, 0)
1008 * @param block_w width of block (16, 8 or 4)
1009 * @param block_h height of block (always same as block_w)
1010 * @param width width of src/dst plane data
1011 * @param height height of src/dst plane data
1012 * @param linesize size of a single line of plane data, including padding
e394953e 1013 * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
3b636f21
DC
1014 */
1015static inline void vp8_mc(VP8Context *s, int luma,
1016 uint8_t *dst, uint8_t *src, const VP56mv *mv,
1017 int x_off, int y_off, int block_w, int block_h,
1018 int width, int height, int linesize,
d6f8476b 1019 vp8_mc_func mc_func[3][3])
3b636f21 1020{
c0498b30
JGG
1021 if (AV_RN32A(mv)) {
1022 static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
1023 int mx = (mv->x << luma)&7, mx_idx = idx[mx];
1024 int my = (mv->y << luma)&7, my_idx = idx[my];
1025
1026 x_off += mv->x >> (3 - luma);
1027 y_off += mv->y >> (3 - luma);
1028
1029 // edge emulation
1030 src += y_off * linesize + x_off;
1031 if (x_off < 2 || x_off >= width - block_w - 3 ||
1032 y_off < 2 || y_off >= height - block_h - 3) {
1033 ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
1034 block_w + 5, block_h + 5,
1035 x_off - 2, y_off - 2, width, height);
1036 src = s->edge_emu_buffer + 2 + linesize * 2;
1037 }
1038 mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
1039 } else
1040 mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
3b636f21
DC
1041}
1042
7c4dcf81
RB
1043static inline void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
1044 AVFrame *ref_frame, int x_off, int y_off,
1045 int bx_off, int by_off,
1046 int block_w, int block_h,
1047 int width, int height, VP56mv *mv)
1048{
1049 VP56mv uvmv = *mv;
1050
1051 /* Y */
1052 vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,
1053 ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,
1054 block_w, block_h, width, height, s->linesize,
1055 s->put_pixels_tab[block_w == 8]);
1056
1057 /* U/V */
1058 if (s->profile == 3) {
1059 uvmv.x &= ~7;
1060 uvmv.y &= ~7;
1061 }
1062 x_off >>= 1; y_off >>= 1;
1063 bx_off >>= 1; by_off >>= 1;
1064 width >>= 1; height >>= 1;
1065 block_w >>= 1; block_h >>= 1;
1066 vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,
1067 ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,
1068 block_w, block_h, width, height, s->uvlinesize,
1069 s->put_pixels_tab[1 + (block_w == 4)]);
1070 vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,
1071 ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,
1072 block_w, block_h, width, height, s->uvlinesize,
1073 s->put_pixels_tab[1 + (block_w == 4)]);
1074}
1075
d864dee8
JGG
1076/* Fetch pixels for estimated mv 4 macroblocks ahead.
1077 * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1078static inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int x_off, int y_off, int ref)
1079{
1080 if (mb->ref_frame != VP56_FRAME_CURRENT) {
1081 int mx = mb->mv.x + x_off + 8;
1082 int my = mb->mv.y + y_off;
1083 uint8_t **src= s->framep[mb->ref_frame]->data;
1084 int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
1085 s->dsp.prefetch(src[0]+off, s->linesize, 4);
1086 off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1087 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
1088 }
1089}
1090
3b636f21
DC
1091/**
1092 * Apply motion vectors to prediction buffer, chapter 18.
1093 */
1094static void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
1095 int mb_x, int mb_y)
1096{
1097 int x_off = mb_x << 4, y_off = mb_y << 4;
1098 int width = 16*s->mb_width, height = 16*s->mb_height;
3b636f21 1099
d864dee8
JGG
1100 prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_PREVIOUS);
1101
3b636f21 1102 if (mb->mode < VP8_MVMODE_SPLIT) {
7c4dcf81
RB
1103 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1104 0, 0, 16, 16, width, height, &mb->mv);
1105 } else switch (mb->partitioning) {
1106 case VP8_SPLITMVMODE_4x4: {
3b636f21 1107 int x, y;
7c4dcf81 1108 VP56mv uvmv;
3b636f21
DC
1109
1110 /* Y */
1111 for (y = 0; y < 4; y++) {
1112 for (x = 0; x < 4; x++) {
1113 vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,
1114 s->framep[mb->ref_frame]->data[0], &mb->bmv[4*y + x],
1115 4*x + x_off, 4*y + y_off, 4, 4,
1116 width, height, s->linesize,
0ef1dbed 1117 s->put_pixels_tab[2]);
3b636f21
DC
1118 }
1119 }
1120
1121 /* U/V */
1122 x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
1123 for (y = 0; y < 2; y++) {
1124 for (x = 0; x < 2; x++) {
1125 uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
1126 mb->bmv[ 2*y * 4 + 2*x+1].x +
1127 mb->bmv[(2*y+1) * 4 + 2*x ].x +
1128 mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1129 uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
1130 mb->bmv[ 2*y * 4 + 2*x+1].y +
1131 mb->bmv[(2*y+1) * 4 + 2*x ].y +
1132 mb->bmv[(2*y+1) * 4 + 2*x+1].y;
8f910a56
SG
1133 uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1134 uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
3b636f21
DC
1135 if (s->profile == 3) {
1136 uvmv.x &= ~7;
1137 uvmv.y &= ~7;
1138 }
1139 vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,
1140 s->framep[mb->ref_frame]->data[1], &uvmv,
1141 4*x + x_off, 4*y + y_off, 4, 4,
1142 width, height, s->uvlinesize,
0ef1dbed 1143 s->put_pixels_tab[2]);
3b636f21
DC
1144 vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,
1145 s->framep[mb->ref_frame]->data[2], &uvmv,
1146 4*x + x_off, 4*y + y_off, 4, 4,
1147 width, height, s->uvlinesize,
0ef1dbed 1148 s->put_pixels_tab[2]);
3b636f21
DC
1149 }
1150 }
7c4dcf81
RB
1151 break;
1152 }
1153 case VP8_SPLITMVMODE_16x8:
1154 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1155 0, 0, 16, 8, width, height, &mb->bmv[0]);
1156 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
7ed06b2b 1157 0, 8, 16, 8, width, height, &mb->bmv[1]);
7c4dcf81
RB
1158 break;
1159 case VP8_SPLITMVMODE_8x16:
1160 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1161 0, 0, 8, 16, width, height, &mb->bmv[0]);
1162 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
7ed06b2b 1163 8, 0, 8, 16, width, height, &mb->bmv[1]);
7c4dcf81
RB
1164 break;
1165 case VP8_SPLITMVMODE_8x8:
1166 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
1167 0, 0, 8, 8, width, height, &mb->bmv[0]);
1168 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
7ed06b2b 1169 8, 0, 8, 8, width, height, &mb->bmv[1]);
7c4dcf81 1170 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
7ed06b2b 1171 0, 8, 8, 8, width, height, &mb->bmv[2]);
7c4dcf81 1172 vp8_mc_part(s, dst, s->framep[mb->ref_frame], x_off, y_off,
7ed06b2b 1173 8, 8, 8, 8, width, height, &mb->bmv[3]);
7c4dcf81 1174 break;
3b636f21 1175 }
d864dee8
JGG
1176
1177 prefetch_motion(s, mb, mb_x, mb_y, x_off, y_off, VP56_FRAME_GOLDEN);
3b636f21
DC
1178}
1179
1180static void idct_mb(VP8Context *s, uint8_t *y_dst, uint8_t *u_dst, uint8_t *v_dst,
1181 VP8Macroblock *mb)
1182{
1183 int x, y, nnz;
1184
1185 if (mb->mode != MODE_I4x4)
1186 for (y = 0; y < 4; y++) {
1187 for (x = 0; x < 4; x++) {
1188 nnz = s->non_zero_count_cache[y][x];
1189 if (nnz) {
1190 if (nnz == 1)
1191 s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
1192 else
1193 s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
1194 }
1195 }
1196 y_dst += 4*s->linesize;
1197 }
1198
1199 for (y = 0; y < 2; y++) {
1200 for (x = 0; x < 2; x++) {
1201 nnz = s->non_zero_count_cache[4][(y<<1)+x];
1202 if (nnz) {
1203 if (nnz == 1)
1204 s->vp8dsp.vp8_idct_dc_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1205 else
1206 s->vp8dsp.vp8_idct_add(u_dst+4*x, s->block[4][(y<<1)+x], s->uvlinesize);
1207 }
1208
1209 nnz = s->non_zero_count_cache[5][(y<<1)+x];
1210 if (nnz) {
1211 if (nnz == 1)
1212 s->vp8dsp.vp8_idct_dc_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1213 else
1214 s->vp8dsp.vp8_idct_add(v_dst+4*x, s->block[5][(y<<1)+x], s->uvlinesize);
1215 }
1216 }
1217 u_dst += 4*s->uvlinesize;
1218 v_dst += 4*s->uvlinesize;
1219 }
1220}
1221
968570d6 1222static void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
3b636f21
DC
1223{
1224 int interior_limit, filter_level;
1225
1226 if (s->segmentation.enabled) {
1227 filter_level = s->segmentation.filter_level[mb->segment];
1228 if (!s->segmentation.absolute_vals)
1229 filter_level += s->filter.level;
1230 } else
1231 filter_level = s->filter.level;
1232
1233 if (s->lf_delta.enabled) {
1234 filter_level += s->lf_delta.ref[mb->ref_frame];
1235
1236 if (mb->ref_frame == VP56_FRAME_CURRENT) {
1237 if (mb->mode == MODE_I4x4)
1238 filter_level += s->lf_delta.mode[0];
1239 } else {
1240 if (mb->mode == VP8_MVMODE_ZERO)
1241 filter_level += s->lf_delta.mode[1];
1242 else if (mb->mode == VP8_MVMODE_SPLIT)
1243 filter_level += s->lf_delta.mode[3];
1244 else
1245 filter_level += s->lf_delta.mode[2];
1246 }
1247 }
1248 filter_level = av_clip(filter_level, 0, 63);
1249
1250 interior_limit = filter_level;
1251 if (s->filter.sharpness) {
1252 interior_limit >>= s->filter.sharpness > 4 ? 2 : 1;
1253 interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1254 }
1255 interior_limit = FFMAX(interior_limit, 1);
1256
968570d6
JGG
1257 f->filter_level = filter_level;
1258 f->inner_limit = interior_limit;
3b636f21
DC
1259}
1260
968570d6 1261static void filter_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb, VP8FilterStrength *f, int mb_x, int mb_y)
3b636f21 1262{
968570d6
JGG
1263 int mbedge_lim, bedge_lim, hev_thresh;
1264 int filter_level = f->filter_level;
1265 int inner_limit = f->inner_limit;
3b636f21 1266
3b636f21
DC
1267 if (!filter_level)
1268 return;
1269
5245c04d
DC
1270 mbedge_lim = 2*(filter_level+2) + inner_limit;
1271 bedge_lim = 2* filter_level + inner_limit;
968570d6
JGG
1272 hev_thresh = filter_level >= 15;
1273
1274 if (s->keyframe) {
1275 if (filter_level >= 40)
1276 hev_thresh = 2;
1277 } else {
1278 if (filter_level >= 40)
1279 hev_thresh = 3;
1280 else if (filter_level >= 20)
1281 hev_thresh = 2;
1282 }
5245c04d 1283
3b636f21 1284 if (mb_x) {
3facfc99
RB
1285 s->vp8dsp.vp8_h_loop_filter16y(dst[0], s->linesize,
1286 mbedge_lim, inner_limit, hev_thresh);
1287 s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], s->uvlinesize,
1288 mbedge_lim, inner_limit, hev_thresh);
3b636f21
DC
1289 }
1290
1291 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
3facfc99
RB
1292 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, s->linesize, bedge_lim,
1293 inner_limit, hev_thresh);
1294 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, s->linesize, bedge_lim,
1295 inner_limit, hev_thresh);
1296 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, s->linesize, bedge_lim,
1297 inner_limit, hev_thresh);
1298 s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
1299 s->uvlinesize, bedge_lim,
1300 inner_limit, hev_thresh);
3b636f21
DC
1301 }
1302
1303 if (mb_y) {
3facfc99
RB
1304 s->vp8dsp.vp8_v_loop_filter16y(dst[0], s->linesize,
1305 mbedge_lim, inner_limit, hev_thresh);
1306 s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], s->uvlinesize,
1307 mbedge_lim, inner_limit, hev_thresh);
3b636f21
DC
1308 }
1309
1310 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
3facfc99
RB
1311 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*s->linesize,
1312 s->linesize, bedge_lim,
1313 inner_limit, hev_thresh);
1314 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*s->linesize,
1315 s->linesize, bedge_lim,
1316 inner_limit, hev_thresh);
1317 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*s->linesize,
1318 s->linesize, bedge_lim,
1319 inner_limit, hev_thresh);
1320 s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * s->uvlinesize,
1321 dst[2] + 4 * s->uvlinesize,
1322 s->uvlinesize, bedge_lim,
1323 inner_limit, hev_thresh);
3b636f21
DC
1324 }
1325}
1326
968570d6 1327static void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8Macroblock *mb, VP8FilterStrength *f, int mb_x, int mb_y)
3b636f21 1328{
968570d6
JGG
1329 int mbedge_lim, bedge_lim;
1330 int filter_level = f->filter_level;
1331 int inner_limit = f->inner_limit;
3b636f21 1332
3b636f21
DC
1333 if (!filter_level)
1334 return;
1335
1336 mbedge_lim = 2*(filter_level+2) + inner_limit;
1337 bedge_lim = 2* filter_level + inner_limit;
1338
1339 if (mb_x)
1340 s->vp8dsp.vp8_h_loop_filter_simple(dst, s->linesize, mbedge_lim);
1341 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1342 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, s->linesize, bedge_lim);
1343 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, s->linesize, bedge_lim);
1344 s->vp8dsp.vp8_h_loop_filter_simple(dst+12, s->linesize, bedge_lim);
1345 }
1346
1347 if (mb_y)
1348 s->vp8dsp.vp8_v_loop_filter_simple(dst, s->linesize, mbedge_lim);
1349 if (!mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT) {
1350 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*s->linesize, s->linesize, bedge_lim);
1351 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*s->linesize, s->linesize, bedge_lim);
1352 s->vp8dsp.vp8_v_loop_filter_simple(dst+12*s->linesize, s->linesize, bedge_lim);
1353 }
1354}
1355
1356static void filter_mb_row(VP8Context *s, int mb_y)
1357{
968570d6 1358 VP8FilterStrength *f = s->filter_strength;
3b636f21
DC
1359 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1360 uint8_t *dst[3] = {
1361 s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize,
1362 s->framep[VP56_FRAME_CURRENT]->data[1] + 8*mb_y*s->uvlinesize,
1363 s->framep[VP56_FRAME_CURRENT]->data[2] + 8*mb_y*s->uvlinesize
1364 };
1365 int mb_x;
1366
1367 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
9ac831c2 1368 backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
968570d6 1369 filter_mb(s, dst, mb++, f++, mb_x, mb_y);
3b636f21
DC
1370 dst[0] += 16;
1371 dst[1] += 8;
1372 dst[2] += 8;
1373 }
1374}
1375
1376static void filter_mb_row_simple(VP8Context *s, int mb_y)
1377{
968570d6 1378 VP8FilterStrength *f = s->filter_strength;
3b636f21 1379 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
968570d6 1380 uint8_t *dst = s->framep[VP56_FRAME_CURRENT]->data[0] + 16*mb_y*s->linesize;
3b636f21
DC
1381 int mb_x;
1382
1383 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
9ac831c2 1384 backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
968570d6 1385 filter_mb_simple(s, dst, mb++, f++, mb_x, mb_y);
3b636f21
DC
1386 dst += 16;
1387 }
1388}
1389
1390static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1391 AVPacket *avpkt)
1392{
1393 VP8Context *s = avctx->priv_data;
1394 int ret, mb_x, mb_y, i, y, referenced;
1395 enum AVDiscard skip_thresh;
1396 AVFrame *curframe;
1397
1398 if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1399 return ret;
1400
1401 referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1402 || s->update_altref == VP56_FRAME_CURRENT;
1403
1404 skip_thresh = !referenced ? AVDISCARD_NONREF :
1405 !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
1406
1407 if (avctx->skip_frame >= skip_thresh) {
1408 s->invisible = 1;
1409 goto skip_decode;
1410 }
9ac831c2 1411 s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
3b636f21
DC
1412
1413 for (i = 0; i < 4; i++)
1414 if (&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1415 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1416 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1417 curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1418 break;
1419 }
1420 if (curframe->data[0])
1421 avctx->release_buffer(avctx, curframe);
1422
1423 curframe->key_frame = s->keyframe;
1424 curframe->pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1425 curframe->reference = referenced ? 3 : 0;
1426 if ((ret = avctx->get_buffer(avctx, curframe))) {
1427 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1428 return ret;
1429 }
1430
1431 // Given that arithmetic probabilities are updated every frame, it's quite likely
1432 // that the values we have on a random interframe are complete junk if we didn't
1433 // start decode on a keyframe. So just don't display anything rather than junk.
1434 if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1435 !s->framep[VP56_FRAME_GOLDEN] ||
1436 !s->framep[VP56_FRAME_GOLDEN2])) {
1437 av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1438 return AVERROR_INVALIDDATA;
1439 }
1440
1441 s->linesize = curframe->linesize[0];
1442 s->uvlinesize = curframe->linesize[1];
1443
1444 if (!s->edge_emu_buffer)
1445 s->edge_emu_buffer = av_malloc(21*s->linesize);
1446
1447 memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1448
1449 // top edge of 127 for intra prediction
9ac831c2 1450 memset(s->top_border, 127, (s->mb_width+1)*sizeof(*s->top_border));
3b636f21
DC
1451
1452 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1453 VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1454 VP8Macroblock *mb = s->macroblocks + mb_y*s->mb_stride;
1455 uint8_t *intra4x4 = s->intra4x4_pred_mode + 4*mb_y*s->b4_stride;
1456 uint8_t *dst[3] = {
1457 curframe->data[0] + 16*mb_y*s->linesize,
1458 curframe->data[1] + 8*mb_y*s->uvlinesize,
1459 curframe->data[2] + 8*mb_y*s->uvlinesize
1460 };
1461
1462 memset(s->left_nnz, 0, sizeof(s->left_nnz));
1463
1464 // left edge of 129 for intra prediction
1465 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE))
1466 for (i = 0; i < 3; i++)
1467 for (y = 0; y < 16>>!!i; y++)
1468 dst[i][y*curframe->linesize[i]-1] = 129;
9ac831c2
DC
1469 if (mb_y)
1470 memset(s->top_border, 129, sizeof(*s->top_border));
3b636f21
DC
1471
1472 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
d1c58fce
JGG
1473 uint8_t *intra4x4_mb = s->keyframe ? intra4x4 + 4*mb_x : s->intra4x4_pred_mode_mb;
1474
d864dee8
JGG
1475 /* Prefetch the current frame, 4 MBs ahead */
1476 s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1477 s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
1478
d1c58fce 1479 decode_mb_mode(s, mb, mb_x, mb_y, intra4x4_mb);
3b636f21
DC
1480
1481 if (!mb->skip)
1482 decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
1483 else {
1484 AV_ZERO128(s->non_zero_count_cache); // luma
1485 AV_ZERO64(s->non_zero_count_cache[4]); // chroma
1486 }
1487
1488 if (mb->mode <= MODE_I4x4) {
d1c58fce 1489 intra_predict(s, dst, mb, intra4x4_mb, mb_x, mb_y);
3b636f21
DC
1490 memset(mb->bmv, 0, sizeof(mb->bmv));
1491 } else {
1492 inter_predict(s, dst, mb, mb_x, mb_y);
1493 }
1494
1495 if (!mb->skip) {
1496 idct_mb(s, dst[0], dst[1], dst[2], mb);
1497 } else {
1498 AV_ZERO64(s->left_nnz);
1499 AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
1500
1501 // Reset DC block predictors if they would exist if the mb had coefficients
1502 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1503 s->left_nnz[8] = 0;
1504 s->top_nnz[mb_x][8] = 0;
1505 }
1506 }
1507
968570d6
JGG
1508 if (s->deblock_filter)
1509 filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
1510
3b636f21
DC
1511 dst[0] += 16;
1512 dst[1] += 8;
1513 dst[2] += 8;
1514 mb++;
1515 }
9ac831c2 1516 if (s->deblock_filter) {
3b636f21 1517 if (s->filter.simple)
9ac831c2 1518 filter_mb_row_simple(s, mb_y);
3b636f21 1519 else
9ac831c2 1520 filter_mb_row(s, mb_y);
3b636f21
DC
1521 }
1522 }
3b636f21
DC
1523
1524skip_decode:
1525 // if future frames don't use the updated probabilities,
1526 // reset them to the values we saved
1527 if (!s->update_probabilities)
1528 s->prob[0] = s->prob[1];
1529
1530 // check if golden and altref are swapped
1531 if (s->update_altref == VP56_FRAME_GOLDEN &&
1532 s->update_golden == VP56_FRAME_GOLDEN2)
1533 FFSWAP(AVFrame *, s->framep[VP56_FRAME_GOLDEN], s->framep[VP56_FRAME_GOLDEN2]);
1534 else {
1535 if (s->update_altref != VP56_FRAME_NONE)
1536 s->framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1537
1538 if (s->update_golden != VP56_FRAME_NONE)
1539 s->framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1540 }
1541
1542 if (s->update_last) // move cur->prev
1543 s->framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_CURRENT];
1544
1545 // release no longer referenced frames
1546 for (i = 0; i < 4; i++)
1547 if (s->frames[i].data[0] &&
1548 &s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
1549 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1550 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1551 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1552 avctx->release_buffer(avctx, &s->frames[i]);
1553
1554 if (!s->invisible) {
1555 *(AVFrame*)data = *s->framep[VP56_FRAME_CURRENT];
1556 *data_size = sizeof(AVFrame);
1557 }
1558
1559 return avpkt->size;
1560}
1561
1562static av_cold int vp8_decode_init(AVCodecContext *avctx)
1563{
1564 VP8Context *s = avctx->priv_data;
1565
1566 s->avctx = avctx;
1567 avctx->pix_fmt = PIX_FMT_YUV420P;
1568
1569 dsputil_init(&s->dsp, avctx);
1570 ff_h264_pred_init(&s->hpc, CODEC_ID_VP8);
1571 ff_vp8dsp_init(&s->vp8dsp);
1572
1573 // intra pred needs edge emulation among other things
1574 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
03ac56e7 1575 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported\n");
3b636f21
DC
1576 return AVERROR_PATCHWELCOME;
1577 }
1578
1579 return 0;
1580}
1581
1582static av_cold int vp8_decode_free(AVCodecContext *avctx)
1583{
1584 vp8_decode_flush(avctx);
1585 return 0;
1586}
1587
1588AVCodec vp8_decoder = {
1589 "vp8",
1590 AVMEDIA_TYPE_VIDEO,
1591 CODEC_ID_VP8,
1592 sizeof(VP8Context),
1593 vp8_decode_init,
1594 NULL,
1595 vp8_decode_free,
1596 vp8_decode_frame,
1597 CODEC_CAP_DR1,
1598 .flush = vp8_decode_flush,
1599 .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
1600};