2 * VP9 compatible video decoder
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
7 * This file is part of Libav.
9 * Libav 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.
14 * Libav 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avassert.h"
34 #define VP9_SYNCCODE 0x498342
37 static void vp9_decode_flush(AVCodecContext *avctx)
39 VP9Context *s = avctx->priv_data;
42 for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++)
43 av_frame_unref(s->refs[i]);
46 static int update_size(AVCodecContext *avctx, int w, int h)
48 VP9Context *s = avctx->priv_data;
51 if (s->above_partition_ctx && w == avctx->width && h == avctx->height)
54 vp9_decode_flush(avctx);
57 return AVERROR_INVALIDDATA;
61 s->sb_cols = (w + 63) >> 6;
62 s->sb_rows = (h + 63) >> 6;
63 s->cols = (w + 7) >> 3;
64 s->rows = (h + 7) >> 3;
66 #define assign(var, type, n) var = (type)p; p += s->sb_cols * n * sizeof(*var)
67 av_free(s->above_partition_ctx);
68 p = av_malloc(s->sb_cols *
69 (240 + sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx) +
70 64 * s->sb_rows * (1 + sizeof(*s->mv[0]) * 2)));
72 return AVERROR(ENOMEM);
73 assign(s->above_partition_ctx, uint8_t *, 8);
74 assign(s->above_skip_ctx, uint8_t *, 8);
75 assign(s->above_txfm_ctx, uint8_t *, 8);
76 assign(s->above_mode_ctx, uint8_t *, 16);
77 assign(s->above_y_nnz_ctx, uint8_t *, 16);
78 assign(s->above_uv_nnz_ctx[0], uint8_t *, 8);
79 assign(s->above_uv_nnz_ctx[1], uint8_t *, 8);
80 assign(s->intra_pred_data[0], uint8_t *, 64);
81 assign(s->intra_pred_data[1], uint8_t *, 32);
82 assign(s->intra_pred_data[2], uint8_t *, 32);
83 assign(s->above_segpred_ctx, uint8_t *, 8);
84 assign(s->above_intra_ctx, uint8_t *, 8);
85 assign(s->above_comp_ctx, uint8_t *, 8);
86 assign(s->above_ref_ctx, uint8_t *, 8);
87 assign(s->above_filter_ctx, uint8_t *, 8);
88 assign(s->lflvl, VP9Filter *, 1);
89 assign(s->above_mv_ctx, VP56mv(*)[2], 16);
90 assign(s->segmentation_map, uint8_t *, 64 * s->sb_rows);
91 assign(s->mv[0], VP9MVRefPair *, 64 * s->sb_rows);
92 assign(s->mv[1], VP9MVRefPair *, 64 * s->sb_rows);
98 // The sign bit is at the end, not the start, of a bit sequence
99 static av_always_inline int get_bits_with_sign(GetBitContext *gb, int n)
101 int v = get_bits(gb, n);
102 return get_bits1(gb) ? -v : v;
105 static av_always_inline int inv_recenter_nonneg(int v, int m)
110 return m - ((v + 1) >> 1);
114 // differential forward probability updates
115 static int update_prob(VP56RangeCoder *c, int p)
117 static const int inv_map_table[MAX_PROB - 1] = {
118 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
119 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
120 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
121 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
122 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
123 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
124 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
125 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
126 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
127 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
128 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
129 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
130 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
131 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
132 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
133 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
134 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
135 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
140 /* This code is trying to do a differential probability update. For a
141 * current probability A in the range [1, 255], the difference to a new
142 * probability of any value can be expressed differentially as 1-A, 255-A
143 * where some part of this (absolute range) exists both in positive as
144 * well as the negative part, whereas another part only exists in one
145 * half. We're trying to code this shared part differentially, i.e.
146 * times two where the value of the lowest bit specifies the sign, and
147 * the single part is then coded on top of this. This absolute difference
148 * then again has a value of [0, 254], but a bigger value in this range
149 * indicates that we're further away from the original value A, so we
150 * can code this as a VLC code, since higher values are increasingly
151 * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
152 * updates vs. the 'fine, exact' updates further down the range, which
153 * adds one extra dimension to this differential update model. */
155 if (!vp8_rac_get(c)) {
156 d = vp8_rac_get_uint(c, 4) + 0;
157 } else if (!vp8_rac_get(c)) {
158 d = vp8_rac_get_uint(c, 4) + 16;
159 } else if (!vp8_rac_get(c)) {
160 d = vp8_rac_get_uint(c, 5) + 32;
162 d = vp8_rac_get_uint(c, 7);
164 d = (d << 1) - 65 + vp8_rac_get(c);
165 d = av_clip(d, 0, MAX_PROB - 65 - 1);
171 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1)
172 : 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
175 static int decode_frame_header(AVCodecContext *avctx,
176 const uint8_t *data, int size, int *ref)
178 VP9Context *s = avctx->priv_data;
179 int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
181 const uint8_t *data2;
184 if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
185 av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
188 if (get_bits(&s->gb, 2) != 0x2) { // frame marker
189 av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
190 return AVERROR_INVALIDDATA;
192 s->profile = get_bits1(&s->gb);
193 if (get_bits1(&s->gb)) { // reserved bit
194 av_log(avctx, AV_LOG_ERROR, "Reserved bit should be zero\n");
195 return AVERROR_INVALIDDATA;
197 if (get_bits1(&s->gb)) {
198 *ref = get_bits(&s->gb, 3);
202 s->last_keyframe = s->keyframe;
203 s->keyframe = !get_bits1(&s->gb);
205 last_invisible = s->invisible;
206 s->invisible = !get_bits1(&s->gb);
207 s->errorres = get_bits1(&s->gb);
208 // FIXME disable this upon resolution change
209 s->use_last_frame_mvs = !s->errorres && !last_invisible;
212 if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
213 av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
214 return AVERROR_INVALIDDATA;
216 s->colorspace = get_bits(&s->gb, 3);
217 if (s->colorspace == 7) { // RGB = profile 1
218 av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile 0\n");
219 return AVERROR_INVALIDDATA;
221 s->fullrange = get_bits1(&s->gb);
224 if (s->profile == 1 || s->profile == 3) {
225 s->sub_x = get_bits1(&s->gb);
226 s->sub_y = get_bits1(&s->gb);
227 if (s->sub_x && s->sub_y) {
228 av_log(avctx, AV_LOG_ERROR,
229 "4:2:0 color not supported in profile 1 or 3\n");
230 return AVERROR_INVALIDDATA;
232 if (get_bits1(&s->gb)) { // reserved bit
233 av_log(avctx, AV_LOG_ERROR, "Reserved bit should be zero\n");
234 return AVERROR_INVALIDDATA;
237 s->sub_x = s->sub_y = 1;
239 if (!s->sub_x || !s->sub_y) {
240 avpriv_report_missing_feature(avctx, "Subsampling %d:%d",
242 return AVERROR_PATCHWELCOME;
245 s->refreshrefmask = 0xff;
246 w = get_bits(&s->gb, 16) + 1;
247 h = get_bits(&s->gb, 16) + 1;
248 if (get_bits1(&s->gb)) // display size
249 skip_bits(&s->gb, 32);
251 s->intraonly = s->invisible ? get_bits1(&s->gb) : 0;
252 s->resetctx = s->errorres ? 0 : get_bits(&s->gb, 2);
254 if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
255 av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
256 return AVERROR_INVALIDDATA;
258 s->refreshrefmask = get_bits(&s->gb, 8);
259 w = get_bits(&s->gb, 16) + 1;
260 h = get_bits(&s->gb, 16) + 1;
261 if (get_bits1(&s->gb)) // display size
262 skip_bits(&s->gb, 32);
264 s->refreshrefmask = get_bits(&s->gb, 8);
265 s->refidx[0] = get_bits(&s->gb, 3);
266 s->signbias[0] = get_bits1(&s->gb);
267 s->refidx[1] = get_bits(&s->gb, 3);
268 s->signbias[1] = get_bits1(&s->gb);
269 s->refidx[2] = get_bits(&s->gb, 3);
270 s->signbias[2] = get_bits1(&s->gb);
271 if (!s->refs[s->refidx[0]]->buf[0] ||
272 !s->refs[s->refidx[1]]->buf[0] ||
273 !s->refs[s->refidx[2]]->buf[0]) {
274 av_log(avctx, AV_LOG_ERROR,
275 "Not all references are available\n");
276 return AVERROR_INVALIDDATA;
278 if (get_bits1(&s->gb)) {
279 w = s->refs[s->refidx[0]]->width;
280 h = s->refs[s->refidx[0]]->height;
281 } else if (get_bits1(&s->gb)) {
282 w = s->refs[s->refidx[1]]->width;
283 h = s->refs[s->refidx[1]]->height;
284 } else if (get_bits1(&s->gb)) {
285 w = s->refs[s->refidx[2]]->width;
286 h = s->refs[s->refidx[2]]->height;
288 w = get_bits(&s->gb, 16) + 1;
289 h = get_bits(&s->gb, 16) + 1;
291 if (get_bits1(&s->gb)) // display size
292 skip_bits(&s->gb, 32);
293 s->highprecisionmvs = get_bits1(&s->gb);
294 s->filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
296 s->allowcompinter = s->signbias[0] != s->signbias[1] ||
297 s->signbias[0] != s->signbias[2];
298 if (s->allowcompinter) {
299 if (s->signbias[0] == s->signbias[1]) {
301 s->varcompref[0] = 0;
302 s->varcompref[1] = 1;
303 } else if (s->signbias[0] == s->signbias[2]) {
305 s->varcompref[0] = 0;
306 s->varcompref[1] = 2;
309 s->varcompref[0] = 1;
310 s->varcompref[1] = 2;
316 s->refreshctx = s->errorres ? 0 : get_bits1(&s->gb);
317 s->parallelmode = s->errorres ? 1 : get_bits1(&s->gb);
318 s->framectxid = c = get_bits(&s->gb, 2);
320 /* loopfilter header data */
321 s->filter.level = get_bits(&s->gb, 6);
322 sharp = get_bits(&s->gb, 3);
323 /* If sharpness changed, reinit lim/mblim LUTs. if it didn't change,
324 * keep the old cache values since they are still valid. */
325 if (s->filter.sharpness != sharp)
326 memset(s->filter.lim_lut, 0, sizeof(s->filter.lim_lut));
327 s->filter.sharpness = sharp;
328 if ((s->lf_delta.enabled = get_bits1(&s->gb))) {
329 if (get_bits1(&s->gb)) {
330 for (i = 0; i < 4; i++)
331 if (get_bits1(&s->gb))
332 s->lf_delta.ref[i] = get_bits_with_sign(&s->gb, 6);
333 for (i = 0; i < 2; i++)
334 if (get_bits1(&s->gb))
335 s->lf_delta.mode[i] = get_bits_with_sign(&s->gb, 6);
338 memset(&s->lf_delta, 0, sizeof(s->lf_delta));
341 /* quantization header data */
342 s->yac_qi = get_bits(&s->gb, 8);
343 s->ydc_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
344 s->uvdc_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
345 s->uvac_qdelta = get_bits1(&s->gb) ? get_bits_with_sign(&s->gb, 4) : 0;
346 s->lossless = s->yac_qi == 0 && s->ydc_qdelta == 0 &&
347 s->uvdc_qdelta == 0 && s->uvac_qdelta == 0;
349 /* segmentation header info */
350 if ((s->segmentation.enabled = get_bits1(&s->gb))) {
351 if ((s->segmentation.update_map = get_bits1(&s->gb))) {
352 for (i = 0; i < 7; i++)
353 s->prob.seg[i] = get_bits1(&s->gb) ?
354 get_bits(&s->gb, 8) : 255;
355 if ((s->segmentation.temporal = get_bits1(&s->gb)))
356 for (i = 0; i < 3; i++)
357 s->prob.segpred[i] = get_bits1(&s->gb) ?
358 get_bits(&s->gb, 8) : 255;
361 if (get_bits1(&s->gb)) {
362 s->segmentation.absolute_vals = get_bits1(&s->gb);
363 for (i = 0; i < 8; i++) {
364 if ((s->segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
365 s->segmentation.feat[i].q_val = get_bits_with_sign(&s->gb, 8);
366 if ((s->segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
367 s->segmentation.feat[i].lf_val = get_bits_with_sign(&s->gb, 6);
368 if ((s->segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
369 s->segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
370 s->segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
374 s->segmentation.feat[0].q_enabled = 0;
375 s->segmentation.feat[0].lf_enabled = 0;
376 s->segmentation.feat[0].skip_enabled = 0;
377 s->segmentation.feat[0].ref_enabled = 0;
380 // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
381 for (i = 0; i < (s->segmentation.enabled ? 8 : 1); i++) {
382 int qyac, qydc, quvac, quvdc, lflvl, sh;
384 if (s->segmentation.feat[i].q_enabled) {
385 if (s->segmentation.absolute_vals)
386 qyac = s->segmentation.feat[i].q_val;
388 qyac = s->yac_qi + s->segmentation.feat[i].q_val;
392 qydc = av_clip_uintp2(qyac + s->ydc_qdelta, 8);
393 quvdc = av_clip_uintp2(qyac + s->uvdc_qdelta, 8);
394 quvac = av_clip_uintp2(qyac + s->uvac_qdelta, 8);
395 qyac = av_clip_uintp2(qyac, 8);
397 s->segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[qydc];
398 s->segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[qyac];
399 s->segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[quvdc];
400 s->segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[quvac];
402 sh = s->filter.level >= 32;
403 if (s->segmentation.feat[i].lf_enabled) {
404 if (s->segmentation.absolute_vals)
405 lflvl = s->segmentation.feat[i].lf_val;
407 lflvl = s->filter.level + s->segmentation.feat[i].lf_val;
409 lflvl = s->filter.level;
411 s->segmentation.feat[i].lflvl[0][0] =
412 s->segmentation.feat[i].lflvl[0][1] =
413 av_clip_uintp2(lflvl + (s->lf_delta.ref[0] << sh), 6);
414 for (j = 1; j < 4; j++) {
415 s->segmentation.feat[i].lflvl[j][0] =
416 av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
417 s->lf_delta.mode[0]) << sh), 6);
418 s->segmentation.feat[i].lflvl[j][1] =
419 av_clip_uintp2(lflvl + ((s->lf_delta.ref[j] +
420 s->lf_delta.mode[1]) << sh), 6);
425 if ((ret = update_size(avctx, w, h)) < 0) {
426 av_log(avctx, AV_LOG_ERROR,
427 "Failed to initialize decoder for %dx%d\n", w, h);
430 for (s->tiling.log2_tile_cols = 0;
431 (s->sb_cols >> s->tiling.log2_tile_cols) > 64;
432 s->tiling.log2_tile_cols++) ;
433 for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
434 max = FFMAX(0, max - 1);
435 while (max > s->tiling.log2_tile_cols) {
436 if (get_bits1(&s->gb))
437 s->tiling.log2_tile_cols++;
441 s->tiling.log2_tile_rows = decode012(&s->gb);
442 s->tiling.tile_rows = 1 << s->tiling.log2_tile_rows;
443 if (s->tiling.tile_cols != (1 << s->tiling.log2_tile_cols)) {
444 s->tiling.tile_cols = 1 << s->tiling.log2_tile_cols;
445 s->c_b = av_fast_realloc(s->c_b, &s->c_b_size,
446 sizeof(VP56RangeCoder) *
447 s->tiling.tile_cols);
449 av_log(avctx, AV_LOG_ERROR,
450 "Ran out of memory during range coder init\n");
451 return AVERROR(ENOMEM);
455 if (s->keyframe || s->errorres || s->intraonly) {
459 s->prob_ctx[3].p = ff_vp9_default_probs;
460 memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
461 sizeof(ff_vp9_default_coef_probs));
462 memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
463 sizeof(ff_vp9_default_coef_probs));
464 memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
465 sizeof(ff_vp9_default_coef_probs));
466 memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
467 sizeof(ff_vp9_default_coef_probs));
470 // next 16 bits is size of the rest of the header (arith-coded)
471 size2 = get_bits(&s->gb, 16);
472 data2 = align_get_bits(&s->gb);
473 if (size2 > size - (data2 - data)) {
474 av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
475 return AVERROR_INVALIDDATA;
477 ff_vp56_init_range_decoder(&s->c, data2, size2);
478 if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
479 av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
480 return AVERROR_INVALIDDATA;
483 if (s->keyframe || s->intraonly)
484 memset(s->counts.coef, 0,
485 sizeof(s->counts.coef) + sizeof(s->counts.eob));
487 memset(&s->counts, 0, sizeof(s->counts));
489 /* FIXME is it faster to not copy here, but do it down in the fw updates
490 * as explicit copies if the fw update is missing (and skip the copy upon
492 s->prob.p = s->prob_ctx[c].p;
496 s->txfmmode = TX_4X4;
498 s->txfmmode = vp8_rac_get_uint(&s->c, 2);
499 if (s->txfmmode == 3)
500 s->txfmmode += vp8_rac_get(&s->c);
502 if (s->txfmmode == TX_SWITCHABLE) {
503 for (i = 0; i < 2; i++)
504 if (vp56_rac_get_prob_branchy(&s->c, 252))
505 s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
506 for (i = 0; i < 2; i++)
507 for (j = 0; j < 2; j++)
508 if (vp56_rac_get_prob_branchy(&s->c, 252))
509 s->prob.p.tx16p[i][j] =
510 update_prob(&s->c, s->prob.p.tx16p[i][j]);
511 for (i = 0; i < 2; i++)
512 for (j = 0; j < 3; j++)
513 if (vp56_rac_get_prob_branchy(&s->c, 252))
514 s->prob.p.tx32p[i][j] =
515 update_prob(&s->c, s->prob.p.tx32p[i][j]);
520 for (i = 0; i < 4; i++) {
521 uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
522 if (vp8_rac_get(&s->c)) {
523 for (j = 0; j < 2; j++)
524 for (k = 0; k < 2; k++)
525 for (l = 0; l < 6; l++)
526 for (m = 0; m < 6; m++) {
527 uint8_t *p = s->prob.coef[i][j][k][l][m];
528 uint8_t *r = ref[j][k][l][m];
529 if (m >= 3 && l == 0) // dc only has 3 pt
531 for (n = 0; n < 3; n++) {
532 if (vp56_rac_get_prob_branchy(&s->c, 252))
533 p[n] = update_prob(&s->c, r[n]);
540 for (j = 0; j < 2; j++)
541 for (k = 0; k < 2; k++)
542 for (l = 0; l < 6; l++)
543 for (m = 0; m < 6; m++) {
544 uint8_t *p = s->prob.coef[i][j][k][l][m];
545 uint8_t *r = ref[j][k][l][m];
546 if (m > 3 && l == 0) // dc only has 3 pt
552 if (s->txfmmode == i)
557 for (i = 0; i < 3; i++)
558 if (vp56_rac_get_prob_branchy(&s->c, 252))
559 s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
560 if (!s->keyframe && !s->intraonly) {
561 for (i = 0; i < 7; i++)
562 for (j = 0; j < 3; j++)
563 if (vp56_rac_get_prob_branchy(&s->c, 252))
564 s->prob.p.mv_mode[i][j] =
565 update_prob(&s->c, s->prob.p.mv_mode[i][j]);
567 if (s->filtermode == FILTER_SWITCHABLE)
568 for (i = 0; i < 4; i++)
569 for (j = 0; j < 2; j++)
570 if (vp56_rac_get_prob_branchy(&s->c, 252))
571 s->prob.p.filter[i][j] =
572 update_prob(&s->c, s->prob.p.filter[i][j]);
574 for (i = 0; i < 4; i++)
575 if (vp56_rac_get_prob_branchy(&s->c, 252))
576 s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
578 if (s->allowcompinter) {
579 s->comppredmode = vp8_rac_get(&s->c);
581 s->comppredmode += vp8_rac_get(&s->c);
582 if (s->comppredmode == PRED_SWITCHABLE)
583 for (i = 0; i < 5; i++)
584 if (vp56_rac_get_prob_branchy(&s->c, 252))
586 update_prob(&s->c, s->prob.p.comp[i]);
588 s->comppredmode = PRED_SINGLEREF;
591 if (s->comppredmode != PRED_COMPREF) {
592 for (i = 0; i < 5; i++) {
593 if (vp56_rac_get_prob_branchy(&s->c, 252))
594 s->prob.p.single_ref[i][0] =
595 update_prob(&s->c, s->prob.p.single_ref[i][0]);
596 if (vp56_rac_get_prob_branchy(&s->c, 252))
597 s->prob.p.single_ref[i][1] =
598 update_prob(&s->c, s->prob.p.single_ref[i][1]);
602 if (s->comppredmode != PRED_SINGLEREF) {
603 for (i = 0; i < 5; i++)
604 if (vp56_rac_get_prob_branchy(&s->c, 252))
605 s->prob.p.comp_ref[i] =
606 update_prob(&s->c, s->prob.p.comp_ref[i]);
609 for (i = 0; i < 4; i++)
610 for (j = 0; j < 9; j++)
611 if (vp56_rac_get_prob_branchy(&s->c, 252))
612 s->prob.p.y_mode[i][j] =
613 update_prob(&s->c, s->prob.p.y_mode[i][j]);
615 for (i = 0; i < 4; i++)
616 for (j = 0; j < 4; j++)
617 for (k = 0; k < 3; k++)
618 if (vp56_rac_get_prob_branchy(&s->c, 252))
619 s->prob.p.partition[3 - i][j][k] =
621 s->prob.p.partition[3 - i][j][k]);
623 // mv fields don't use the update_prob subexp model for some reason
624 for (i = 0; i < 3; i++)
625 if (vp56_rac_get_prob_branchy(&s->c, 252))
626 s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
628 for (i = 0; i < 2; i++) {
629 if (vp56_rac_get_prob_branchy(&s->c, 252))
630 s->prob.p.mv_comp[i].sign =
631 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
633 for (j = 0; j < 10; j++)
634 if (vp56_rac_get_prob_branchy(&s->c, 252))
635 s->prob.p.mv_comp[i].classes[j] =
636 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
638 if (vp56_rac_get_prob_branchy(&s->c, 252))
639 s->prob.p.mv_comp[i].class0 =
640 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
642 for (j = 0; j < 10; j++)
643 if (vp56_rac_get_prob_branchy(&s->c, 252))
644 s->prob.p.mv_comp[i].bits[j] =
645 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
648 for (i = 0; i < 2; i++) {
649 for (j = 0; j < 2; j++)
650 for (k = 0; k < 3; k++)
651 if (vp56_rac_get_prob_branchy(&s->c, 252))
652 s->prob.p.mv_comp[i].class0_fp[j][k] =
653 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
655 for (j = 0; j < 3; j++)
656 if (vp56_rac_get_prob_branchy(&s->c, 252))
657 s->prob.p.mv_comp[i].fp[j] =
658 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
661 if (s->highprecisionmvs) {
662 for (i = 0; i < 2; i++) {
663 if (vp56_rac_get_prob_branchy(&s->c, 252))
664 s->prob.p.mv_comp[i].class0_hp =
665 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
667 if (vp56_rac_get_prob_branchy(&s->c, 252))
668 s->prob.p.mv_comp[i].hp =
669 (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
674 return (data2 - data) + size2;
677 static int decode_subblock(AVCodecContext *avctx, int row, int col,
679 ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
681 VP9Context *s = avctx->priv_data;
682 int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
683 (((s->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
685 const uint8_t *p = s->keyframe ? ff_vp9_default_kf_partition_probs[bl][c]
686 : s->prob.p.partition[bl][c];
687 enum BlockPartition bp;
688 ptrdiff_t hbs = 4 >> bl;
691 bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
692 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff, bl, bp);
693 } else if (col + hbs < s->cols) {
694 if (row + hbs < s->rows) {
695 bp = vp8_rac_get_tree(&s->c, ff_vp9_partition_tree, p);
698 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
702 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
705 yoff += hbs * 8 * s->cur_frame->linesize[0];
706 uvoff += hbs * 4 * s->cur_frame->linesize[1];
707 ret = ff_vp9_decode_block(avctx, row + hbs, col, lflvl,
708 yoff, uvoff, bl, bp);
712 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
717 ret = ff_vp9_decode_block(avctx, row, col + hbs, lflvl,
718 yoff, uvoff, bl, bp);
721 case PARTITION_SPLIT:
722 ret = decode_subblock(avctx, row, col, lflvl,
723 yoff, uvoff, bl + 1);
725 ret = decode_subblock(avctx, row, col + hbs, lflvl,
726 yoff + 8 * hbs, uvoff + 4 * hbs,
729 yoff += hbs * 8 * s->cur_frame->linesize[0];
730 uvoff += hbs * 4 * s->cur_frame->linesize[1];
731 ret = decode_subblock(avctx, row + hbs, col, lflvl,
732 yoff, uvoff, bl + 1);
734 ret = decode_subblock(avctx, row + hbs, col + hbs,
735 lflvl, yoff + 8 * hbs,
736 uvoff + 4 * hbs, bl + 1);
742 av_log(avctx, AV_LOG_ERROR, "Unexpected partition %d.", bp);
743 return AVERROR_INVALIDDATA;
745 } else if (vp56_rac_get_prob_branchy(&s->c, p[1])) {
746 bp = PARTITION_SPLIT;
747 ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
749 ret = decode_subblock(avctx, row, col + hbs, lflvl,
750 yoff + 8 * hbs, uvoff + 4 * hbs, bl + 1);
753 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
756 } else if (row + hbs < s->rows) {
757 if (vp56_rac_get_prob_branchy(&s->c, p[2])) {
758 bp = PARTITION_SPLIT;
759 ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
761 yoff += hbs * 8 * s->cur_frame->linesize[0];
762 uvoff += hbs * 4 * s->cur_frame->linesize[1];
763 ret = decode_subblock(avctx, row + hbs, col, lflvl,
764 yoff, uvoff, bl + 1);
768 ret = ff_vp9_decode_block(avctx, row, col, lflvl, yoff, uvoff,
772 bp = PARTITION_SPLIT;
773 ret = decode_subblock(avctx, row, col, lflvl, yoff, uvoff, bl + 1);
775 s->counts.partition[bl][c][bp]++;
780 static void loopfilter_subblock(AVCodecContext *avctx, VP9Filter *lflvl,
782 ptrdiff_t yoff, ptrdiff_t uvoff)
784 VP9Context *s = avctx->priv_data;
785 uint8_t *dst = s->cur_frame->data[0] + yoff, *lvl = lflvl->level;
786 ptrdiff_t ls_y = s->cur_frame->linesize[0], ls_uv = s->cur_frame->linesize[1];
789 /* FIXME: In how far can we interleave the v/h loopfilter calls? E.g.
790 * if you think of them as acting on a 8x8 block max, we can interleave
791 * each v/h within the single x loop, but that only works if we work on
792 * 8 pixel blocks, and we won't always do that (we want at least 16px
793 * to use SSE2 optimizations, perhaps 32 for AVX2). */
795 // filter edges between columns, Y plane (e.g. block1 | block2)
796 for (y = 0; y < 8; y += 2, dst += 16 * ls_y, lvl += 16) {
797 uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[0][0][y];
798 uint8_t *hmask2 = lflvl->mask[0][0][y + 1];
799 unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2], hm13 = hmask1[3];
800 unsigned hm2 = hmask2[1] | hmask2[2], hm23 = hmask2[3];
801 unsigned hm = hm1 | hm2 | hm13 | hm23;
803 for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 8, l++) {
805 int L = *l, H = L >> 4;
806 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
811 av_assert2(l[8] == L);
812 s->dsp.loop_filter_16[0](ptr, ls_y, E, I, H);
814 s->dsp.loop_filter_8[2][0](ptr, ls_y, E, I, H);
816 } else if (hm2 & x) {
819 E |= s->filter.mblim_lut[L] << 8;
820 I |= s->filter.lim_lut[L] << 8;
821 s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
823 [0](ptr, ls_y, E, I, H);
825 s->dsp.loop_filter_8[!!(hmask1[1] & x)]
826 [0](ptr, ls_y, E, I, H);
829 } else if (hm2 & x) {
830 int L = l[8], H = L >> 4;
831 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
834 s->dsp.loop_filter_8[!!(hmask2[1] & x)]
835 [0](ptr + 8 * ls_y, ls_y, E, I, H);
839 int L = *l, H = L >> 4;
840 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
845 E |= s->filter.mblim_lut[L] << 8;
846 I |= s->filter.lim_lut[L] << 8;
847 s->dsp.loop_filter_mix2[0][0][0](ptr + 4, ls_y, E, I, H);
849 s->dsp.loop_filter_8[0][0](ptr + 4, ls_y, E, I, H);
851 } else if (hm23 & x) {
852 int L = l[8], H = L >> 4;
853 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
855 s->dsp.loop_filter_8[0][0](ptr + 8 * ls_y + 4, ls_y, E, I, H);
861 // filter edges between rows, Y plane (e.g. ------)
863 dst = s->cur_frame->data[0] + yoff;
865 for (y = 0; y < 8; y++, dst += 8 * ls_y, lvl += 8) {
866 uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[0][1][y];
867 unsigned vm = vmask[0] | vmask[1] | vmask[2], vm3 = vmask[3];
869 for (x = 1; vm & ~(x - 1); x <<= 2, ptr += 16, l += 2) {
872 int L = *l, H = L >> 4;
873 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
876 if (vmask[0] & (x << 1)) {
877 av_assert2(l[1] == L);
878 s->dsp.loop_filter_16[1](ptr, ls_y, E, I, H);
880 s->dsp.loop_filter_8[2][1](ptr, ls_y, E, I, H);
882 } else if (vm & (x << 1)) {
885 E |= s->filter.mblim_lut[L] << 8;
886 I |= s->filter.lim_lut[L] << 8;
887 s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
888 [!!(vmask[1] & (x << 1))]
889 [1](ptr, ls_y, E, I, H);
891 s->dsp.loop_filter_8[!!(vmask[1] & x)]
892 [1](ptr, ls_y, E, I, H);
894 } else if (vm & (x << 1)) {
895 int L = l[1], H = L >> 4;
896 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
898 s->dsp.loop_filter_8[!!(vmask[1] & (x << 1))]
899 [1](ptr + 8, ls_y, E, I, H);
903 int L = *l, H = L >> 4;
904 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
906 if (vm3 & (x << 1)) {
909 E |= s->filter.mblim_lut[L] << 8;
910 I |= s->filter.lim_lut[L] << 8;
911 s->dsp.loop_filter_mix2[0][0][1](ptr + ls_y * 4, ls_y, E, I, H);
913 s->dsp.loop_filter_8[0][1](ptr + ls_y * 4, ls_y, E, I, H);
915 } else if (vm3 & (x << 1)) {
916 int L = l[1], H = L >> 4;
917 int E = s->filter.mblim_lut[L], I = s->filter.lim_lut[L];
919 s->dsp.loop_filter_8[0][1](ptr + ls_y * 4 + 8, ls_y, E, I, H);
924 // same principle but for U/V planes
925 for (p = 0; p < 2; p++) {
927 dst = s->cur_frame->data[1 + p] + uvoff;
928 for (y = 0; y < 8; y += 4, dst += 16 * ls_uv, lvl += 32) {
929 uint8_t *ptr = dst, *l = lvl, *hmask1 = lflvl->mask[1][0][y];
930 uint8_t *hmask2 = lflvl->mask[1][0][y + 2];
931 unsigned hm1 = hmask1[0] | hmask1[1] | hmask1[2];
932 unsigned hm2 = hmask2[1] | hmask2[2], hm = hm1 | hm2;
934 for (x = 1; hm & ~(x - 1); x <<= 1, ptr += 4) {
937 int L = *l, H = L >> 4;
938 int E = s->filter.mblim_lut[L];
939 int I = s->filter.lim_lut[L];
943 av_assert2(l[16] == L);
944 s->dsp.loop_filter_16[0](ptr, ls_uv, E, I, H);
946 s->dsp.loop_filter_8[2][0](ptr, ls_uv, E, I, H);
948 } else if (hm2 & x) {
951 E |= s->filter.mblim_lut[L] << 8;
952 I |= s->filter.lim_lut[L] << 8;
953 s->dsp.loop_filter_mix2[!!(hmask1[1] & x)]
955 [0](ptr, ls_uv, E, I, H);
957 s->dsp.loop_filter_8[!!(hmask1[1] & x)]
958 [0](ptr, ls_uv, E, I, H);
960 } else if (hm2 & x) {
961 int L = l[16], H = L >> 4;
962 int E = s->filter.mblim_lut[L];
963 int I = s->filter.lim_lut[L];
965 s->dsp.loop_filter_8[!!(hmask2[1] & x)]
966 [0](ptr + 8 * ls_uv, ls_uv, E, I, H);
974 dst = s->cur_frame->data[1 + p] + uvoff;
975 for (y = 0; y < 8; y++, dst += 4 * ls_uv) {
976 uint8_t *ptr = dst, *l = lvl, *vmask = lflvl->mask[1][1][y];
977 unsigned vm = vmask[0] | vmask[1] | vmask[2];
979 for (x = 1; vm & ~(x - 1); x <<= 4, ptr += 16, l += 4) {
982 int L = *l, H = L >> 4;
983 int E = s->filter.mblim_lut[L];
984 int I = s->filter.lim_lut[L];
987 if (vmask[0] & (x << 2)) {
988 av_assert2(l[2] == L);
989 s->dsp.loop_filter_16[1](ptr, ls_uv, E, I, H);
991 s->dsp.loop_filter_8[2][1](ptr, ls_uv, E, I, H);
993 } else if (vm & (x << 2)) {
996 E |= s->filter.mblim_lut[L] << 8;
997 I |= s->filter.lim_lut[L] << 8;
998 s->dsp.loop_filter_mix2[!!(vmask[1] & x)]
999 [!!(vmask[1] & (x << 2))]
1000 [1](ptr, ls_uv, E, I, H);
1002 s->dsp.loop_filter_8[!!(vmask[1] & x)]
1003 [1](ptr, ls_uv, E, I, H);
1005 } else if (vm & (x << 2)) {
1006 int L = l[2], H = L >> 4;
1007 int E = s->filter.mblim_lut[L];
1008 int I = s->filter.lim_lut[L];
1010 s->dsp.loop_filter_8[!!(vmask[1] & (x << 2))]
1011 [1](ptr + 8, ls_uv, E, I, H);
1021 static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
1023 int sb_start = (idx * n) >> log2_n;
1024 int sb_end = ((idx + 1) * n) >> log2_n;
1025 *start = FFMIN(sb_start, n) << 3;
1026 *end = FFMIN(sb_end, n) << 3;
1029 static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1030 int *got_frame, const uint8_t *data, int size)
1032 VP9Context *s = avctx->priv_data;
1033 int ret, tile_row, tile_col, i, ref = -1, row, col;
1034 ptrdiff_t yoff = 0, uvoff = 0;
1036 ret = decode_frame_header(avctx, data, size, &ref);
1040 if (!s->refs[ref]->buf[0]) {
1041 av_log(avctx, AV_LOG_ERROR,
1042 "Requested reference %d not available\n", ref);
1043 return AVERROR_INVALIDDATA;
1046 ret = av_frame_ref(frame, s->refs[ref]);
1055 s->cur_frame = frame;
1057 av_frame_unref(s->cur_frame);
1058 if ((ret = ff_get_buffer(avctx, s->cur_frame,
1059 s->refreshrefmask ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
1061 s->cur_frame->key_frame = s->keyframe;
1062 s->cur_frame->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
1063 : AV_PICTURE_TYPE_P;
1066 avctx->color_range = AVCOL_RANGE_JPEG;
1068 avctx->color_range = AVCOL_RANGE_MPEG;
1070 switch (s->colorspace) {
1071 case 1: avctx->colorspace = AVCOL_SPC_BT470BG; break;
1072 case 2: avctx->colorspace = AVCOL_SPC_BT709; break;
1073 case 3: avctx->colorspace = AVCOL_SPC_SMPTE170M; break;
1074 case 4: avctx->colorspace = AVCOL_SPC_SMPTE240M; break;
1077 // main tile decode loop
1078 memset(s->above_partition_ctx, 0, s->cols);
1079 memset(s->above_skip_ctx, 0, s->cols);
1080 if (s->keyframe || s->intraonly)
1081 memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
1083 memset(s->above_mode_ctx, NEARESTMV, s->cols);
1084 memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
1085 memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 8);
1086 memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 8);
1087 memset(s->above_segpred_ctx, 0, s->cols);
1088 for (tile_row = 0; tile_row < s->tiling.tile_rows; tile_row++) {
1089 set_tile_offset(&s->tiling.tile_row_start, &s->tiling.tile_row_end,
1090 tile_row, s->tiling.log2_tile_rows, s->sb_rows);
1091 for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
1094 if (tile_col == s->tiling.tile_cols - 1 &&
1095 tile_row == s->tiling.tile_rows - 1) {
1098 tile_size = AV_RB32(data);
1102 if (tile_size > size)
1103 return AVERROR_INVALIDDATA;
1104 ff_vp56_init_range_decoder(&s->c_b[tile_col], data, tile_size);
1105 if (vp56_rac_get_prob_branchy(&s->c_b[tile_col], 128)) // marker bit
1106 return AVERROR_INVALIDDATA;
1111 for (row = s->tiling.tile_row_start;
1112 row < s->tiling.tile_row_end;
1113 row += 8, yoff += s->cur_frame->linesize[0] * 64,
1114 uvoff += s->cur_frame->linesize[1] * 32) {
1115 VP9Filter *lflvl = s->lflvl;
1116 ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1118 for (tile_col = 0; tile_col < s->tiling.tile_cols; tile_col++) {
1119 set_tile_offset(&s->tiling.tile_col_start,
1120 &s->tiling.tile_col_end,
1121 tile_col, s->tiling.log2_tile_cols, s->sb_cols);
1123 memset(s->left_partition_ctx, 0, 8);
1124 memset(s->left_skip_ctx, 0, 8);
1125 if (s->keyframe || s->intraonly)
1126 memset(s->left_mode_ctx, DC_PRED, 16);
1128 memset(s->left_mode_ctx, NEARESTMV, 8);
1129 memset(s->left_y_nnz_ctx, 0, 16);
1130 memset(s->left_uv_nnz_ctx, 0, 16);
1131 memset(s->left_segpred_ctx, 0, 8);
1133 memcpy(&s->c, &s->c_b[tile_col], sizeof(s->c));
1134 for (col = s->tiling.tile_col_start;
1135 col < s->tiling.tile_col_end;
1136 col += 8, yoff2 += 64, uvoff2 += 32, lflvl++) {
1137 // FIXME integrate with lf code (i.e. zero after each
1138 // use, similar to invtxfm coefficients, or similar)
1139 memset(lflvl->mask, 0, sizeof(lflvl->mask));
1141 if ((ret = decode_subblock(avctx, row, col, lflvl,
1142 yoff2, uvoff2, BL_64X64)) < 0)
1145 memcpy(&s->c_b[tile_col], &s->c, sizeof(s->c));
1148 // backup pre-loopfilter reconstruction data for intra
1149 // prediction of next row of sb64s
1150 if (row + 8 < s->rows) {
1151 memcpy(s->intra_pred_data[0],
1152 s->cur_frame->data[0] + yoff +
1153 63 * s->cur_frame->linesize[0],
1155 memcpy(s->intra_pred_data[1],
1156 s->cur_frame->data[1] + uvoff +
1157 31 * s->cur_frame->linesize[1],
1159 memcpy(s->intra_pred_data[2],
1160 s->cur_frame->data[2] + uvoff +
1161 31 * s->cur_frame->linesize[2],
1165 // loopfilter one row
1166 if (s->filter.level) {
1170 for (col = 0; col < s->cols;
1171 col += 8, yoff2 += 64, uvoff2 += 32, lflvl++)
1172 loopfilter_subblock(avctx, lflvl, row, col, yoff2, uvoff2);
1177 // bw adaptivity (or in case of parallel decoding mode, fw adaptivity
1178 // probability maintenance between frames)
1179 if (s->refreshctx) {
1180 if (s->parallelmode) {
1182 for (i = 0; i < 4; i++) {
1183 for (j = 0; j < 2; j++)
1184 for (k = 0; k < 2; k++)
1185 for (l = 0; l < 6; l++)
1186 for (m = 0; m < 6; m++)
1187 memcpy(s->prob_ctx[s->framectxid].coef[i][j][k][l][m],
1188 s->prob.coef[i][j][k][l][m], 3);
1189 if (s->txfmmode == i)
1192 s->prob_ctx[s->framectxid].p = s->prob.p;
1194 ff_vp9_adapt_probs(s);
1197 FFSWAP(VP9MVRefPair *, s->mv[0], s->mv[1]);
1200 for (i = 0; i < 8; i++)
1201 if (s->refreshrefmask & (1 << i)) {
1202 av_frame_unref(s->refs[i]);
1203 ret = av_frame_ref(s->refs[i], s->cur_frame);
1209 av_frame_unref(s->cur_frame);
1216 static int vp9_decode_packet(AVCodecContext *avctx, void *frame,
1217 int *got_frame, AVPacket *avpkt)
1219 const uint8_t *data = avpkt->data;
1220 int size = avpkt->size;
1223 /* Read superframe index - this is a collection of individual frames
1224 * that together lead to one visible frame */
1225 marker = data[size - 1];
1226 if ((marker & 0xe0) == 0xc0) {
1227 int nbytes = 1 + ((marker >> 3) & 0x3);
1228 int n_frames = 1 + (marker & 0x7);
1229 int idx_sz = 2 + n_frames * nbytes;
1231 if (size >= idx_sz && data[size - idx_sz] == marker) {
1232 const uint8_t *idx = data + size + 1 - idx_sz;
1234 while (n_frames--) {
1235 unsigned sz = AV_RL32(idx);
1238 sz &= (1 << (8 * nbytes)) - 1;
1242 av_log(avctx, AV_LOG_ERROR,
1243 "Superframe packet size too big: %u > %d\n",
1245 return AVERROR_INVALIDDATA;
1248 ret = vp9_decode_frame(avctx, frame, got_frame, data, sz);
1258 /* If we get here, there was no valid superframe index, i.e. this is just
1259 * one whole single frame. Decode it as such from the complete input buf. */
1260 if ((ret = vp9_decode_frame(avctx, frame, got_frame, data, size)) < 0)
1265 static av_cold int vp9_decode_free(AVCodecContext *avctx)
1267 VP9Context *s = avctx->priv_data;
1270 for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++)
1271 av_frame_free(&s->refs[i]);
1274 av_freep(&s->above_partition_ctx);
1279 static av_cold int vp9_decode_init(AVCodecContext *avctx)
1281 VP9Context *s = avctx->priv_data;
1284 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1286 ff_vp9dsp_init(&s->dsp);
1287 ff_videodsp_init(&s->vdsp, 8);
1289 for (i = 0; i < FF_ARRAY_ELEMS(s->refs); i++) {
1290 s->refs[i] = av_frame_alloc();
1292 vp9_decode_free(avctx);
1293 return AVERROR(ENOMEM);
1297 s->filter.sharpness = -1;
1302 AVCodec ff_vp9_decoder = {
1304 .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
1305 .type = AVMEDIA_TYPE_VIDEO,
1306 .id = AV_CODEC_ID_VP9,
1307 .priv_data_size = sizeof(VP9Context),
1308 .init = vp9_decode_init,
1309 .decode = vp9_decode_packet,
1310 .flush = vp9_decode_flush,
1311 .close = vp9_decode_free,
1312 .capabilities = AV_CODEC_CAP_DR1,