FFmpeg
Loading...
Searching...
No Matches
vp9.c
Go to the documentation of this file.
1/*
2 * VP9 compatible video decoder
3 *
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
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 "config_components.h"
25
26#include "avcodec.h"
27#include "codec_internal.h"
28#include "decode.h"
29#include "get_bits.h"
30#include "hwaccel_internal.h"
31#include "hwconfig.h"
32#include "profiles.h"
33#include "progressframe.h"
34#include "libavutil/refstruct.h"
35#include "thread.h"
36#include "pthread_internal.h"
37
38#include "videodsp.h"
39#include "vp89_rac.h"
40#include "vp9.h"
41#include "vp9data.h"
42#include "vp9dec.h"
43#include "vpx_rac.h"
45#include "libavutil/avassert.h"
47#include "libavutil/mem.h"
48#include "libavutil/pixdesc.h"
50
51#define VP9_SYNCCODE 0x498342
52
53#if HAVE_THREADS
54DEFINE_OFFSET_ARRAY(VP9Context, vp9_context, pthread_init_cnt,
55 (offsetof(VP9Context, progress_mutex)),
56 (offsetof(VP9Context, progress_cond)));
57
58static int vp9_alloc_entries(AVCodecContext *avctx, int n) {
59 VP9Context *s = avctx->priv_data;
60
62 if (s->entries)
63 av_freep(&s->entries);
64
65 s->entries = av_malloc_array(n, sizeof(atomic_int));
66 if (!s->entries)
67 return AVERROR(ENOMEM);
68 }
69 return 0;
70}
71
72static void vp9_report_tile_progress(VP9Context *s, int field, int n) {
73 pthread_mutex_lock(&s->progress_mutex);
75 pthread_cond_signal(&s->progress_cond);
76 pthread_mutex_unlock(&s->progress_mutex);
77}
78
79static void vp9_await_tile_progress(VP9Context *s, int field, int n) {
80 if (atomic_load_explicit(&s->entries[field], memory_order_acquire) >= n)
81 return;
82
83 pthread_mutex_lock(&s->progress_mutex);
84 while (atomic_load_explicit(&s->entries[field], memory_order_relaxed) != n)
85 pthread_cond_wait(&s->progress_cond, &s->progress_mutex);
86 pthread_mutex_unlock(&s->progress_mutex);
87}
88#else
89static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; }
90#endif
91
93{
94 av_freep(&td->b_base);
95 av_freep(&td->block_base);
97}
98
100{
101 av_refstruct_unref(&f->hwaccel_picture_private);
103 av_refstruct_unref(&f->header_ref);
104 av_refstruct_unref(&f->extradata);
105 f->segmentation_map = NULL;
106}
107
109{
110 VP9Context *s = avctx->priv_data;
111 int ret, sz;
112
114 if (ret < 0)
115 return ret;
116
117 if (!avctx->hwaccel && f->tf.f->linesize[1] != f->tf.f->linesize[2]) {
118 av_log(avctx, AV_LOG_ERROR, "uv stride mismatch unsupported\n");
120 goto fail;
121 }
122
123 sz = 64 * s->sb_cols * s->sb_rows;
124 if (sz != s->frame_extradata_pool_size) {
125 av_refstruct_pool_uninit(&s->frame_extradata_pool);
126 s->frame_extradata_pool = av_refstruct_pool_alloc(sz * (1 + sizeof(VP9mvrefPair)),
128 if (!s->frame_extradata_pool) {
129 s->frame_extradata_pool_size = 0;
130 ret = AVERROR(ENOMEM);
131 goto fail;
132 }
133 s->frame_extradata_pool_size = sz;
134 }
135 f->extradata = av_refstruct_pool_get(s->frame_extradata_pool);
136 if (!f->extradata) {
137 ret = AVERROR(ENOMEM);
138 goto fail;
139 }
140
141 f->segmentation_map = f->extradata;
142 f->mv = (VP9mvrefPair *) ((char*)f->extradata + sz);
143
144 ret = ff_hwaccel_frame_priv_alloc(avctx, &f->hwaccel_picture_private);
145 if (ret < 0)
146 goto fail;
147
148 return 0;
149
150fail:
152 return ret;
153}
154
156{
157 av_refstruct_replace(&dst->header_ref, src->header_ref);
158 dst->frame_header = src->frame_header;
159
160 av_refstruct_replace(&dst->hwaccel_picture_private,
161 src->hwaccel_picture_private);
163
164 av_refstruct_replace(&dst->extradata, src->extradata);
165
166 dst->segmentation_map = src->segmentation_map;
167 dst->mv = src->mv;
168 dst->uses_2pass = src->uses_2pass;
169}
170
171static int update_size(AVCodecContext *avctx, int w, int h)
172{
173#define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
174 CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
175 CONFIG_VP9_D3D12VA_HWACCEL + \
176 CONFIG_VP9_NVDEC_HWACCEL + \
177 CONFIG_VP9_NVDEC_CUARRAY_HWACCEL + \
178 CONFIG_VP9_VAAPI_HWACCEL + \
179 CONFIG_VP9_VDPAU_HWACCEL + \
180 CONFIG_VP9_VIDEOTOOLBOX_HWACCEL + \
181 CONFIG_VP9_VULKAN_HWACCEL)
182 enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
183 VP9Context *s = avctx->priv_data;
184 uint8_t *p;
185 int bytesperpixel = s->bytesperpixel, ret, cols, rows;
186 int lflvl_len, i;
187 int changed = 0;
188
189 av_assert0(w > 0 && h > 0);
190
191 if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
192 changed = 1;
193 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
194 return ret;
195
196 switch (s->pix_fmt) {
199#if CONFIG_VP9_DXVA2_HWACCEL
200 *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
201#endif
202#if CONFIG_VP9_D3D11VA_HWACCEL
203 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
204 *fmtp++ = AV_PIX_FMT_D3D11;
205#endif
206#if CONFIG_VP9_D3D12VA_HWACCEL
207 *fmtp++ = AV_PIX_FMT_D3D12;
208#endif
209#if CONFIG_VP9_NVDEC_HWACCEL
210 *fmtp++ = AV_PIX_FMT_CUDA;
211#endif
212#if CONFIG_VP9_NVDEC_CUARRAY_HWACCEL
213 *fmtp++ = AV_PIX_FMT_CUARRAY;
214#endif
215#if CONFIG_VP9_VAAPI_HWACCEL
216 *fmtp++ = AV_PIX_FMT_VAAPI;
217#endif
218#if CONFIG_VP9_VDPAU_HWACCEL
219 *fmtp++ = AV_PIX_FMT_VDPAU;
220#endif
221#if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
222 *fmtp++ = AV_PIX_FMT_VIDEOTOOLBOX;
223#endif
224#if CONFIG_VP9_VULKAN_HWACCEL
225 *fmtp++ = AV_PIX_FMT_VULKAN;
226#endif
227 break;
229#if CONFIG_VP9_NVDEC_HWACCEL
230 *fmtp++ = AV_PIX_FMT_CUDA;
231#endif
232#if CONFIG_VP9_NVDEC_CUARRAY_HWACCEL
233 *fmtp++ = AV_PIX_FMT_CUARRAY;
234#endif
235#if CONFIG_VP9_VAAPI_HWACCEL
236 *fmtp++ = AV_PIX_FMT_VAAPI;
237#endif
238#if CONFIG_VP9_VDPAU_HWACCEL
239 *fmtp++ = AV_PIX_FMT_VDPAU;
240#endif
241#if CONFIG_VP9_VULKAN_HWACCEL
242 *fmtp++ = AV_PIX_FMT_VULKAN;
243#endif
244 break;
248#if CONFIG_VP9_VAAPI_HWACCEL
249 *fmtp++ = AV_PIX_FMT_VAAPI;
250#endif
251#if CONFIG_VP9_VULKAN_HWACCEL
252 *fmtp++ = AV_PIX_FMT_VULKAN;
253#endif
254 break;
255 case AV_PIX_FMT_GBRP:
258#if CONFIG_VP9_VAAPI_HWACCEL
259 *fmtp++ = AV_PIX_FMT_VAAPI;
260#endif
261#if CONFIG_VP9_VULKAN_HWACCEL
262 *fmtp++ = AV_PIX_FMT_VULKAN;
263#endif
264 break;
265 }
266
267 *fmtp++ = s->pix_fmt;
268 *fmtp = AV_PIX_FMT_NONE;
269
270 ret = ff_get_format(avctx, pix_fmts);
271 if (ret < 0) {
272 ff_set_dimensions(avctx, s->w, s->h);
273 return ret;
274 }
275
276 avctx->pix_fmt = ret;
277 s->gf_fmt = s->pix_fmt;
278 s->w = w;
279 s->h = h;
280 }
281
282 cols = (w + 7) >> 3;
283 rows = (h + 7) >> 3;
284
285 if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
286 return changed;
287
288 s->last_fmt = s->pix_fmt;
289 s->sb_cols = (w + 63) >> 6;
290 s->sb_rows = (h + 63) >> 6;
291 s->cols = (w + 7) >> 3;
292 s->rows = (h + 7) >> 3;
293 lflvl_len = avctx->active_thread_type == FF_THREAD_SLICE ? s->sb_rows : 1;
294
295#define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
296 av_freep(&s->intra_pred_data[0]);
297 // FIXME we slightly over-allocate here for subsampled chroma, but a little
298 // bit of padding shouldn't affect performance...
299 p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
300 lflvl_len * sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
301 if (!p)
302 return AVERROR(ENOMEM);
303 assign(s->intra_pred_data[0], uint8_t *, 64 * bytesperpixel);
304 assign(s->intra_pred_data[1], uint8_t *, 64 * bytesperpixel);
305 assign(s->intra_pred_data[2], uint8_t *, 64 * bytesperpixel);
306 assign(s->above_y_nnz_ctx, uint8_t *, 16);
307 assign(s->above_mode_ctx, uint8_t *, 16);
308 assign(s->above_mv_ctx, VP9mv(*)[2], 16);
309 assign(s->above_uv_nnz_ctx[0], uint8_t *, 16);
310 assign(s->above_uv_nnz_ctx[1], uint8_t *, 16);
311 assign(s->above_partition_ctx, uint8_t *, 8);
312 assign(s->above_skip_ctx, uint8_t *, 8);
313 assign(s->above_txfm_ctx, uint8_t *, 8);
314 assign(s->above_segpred_ctx, uint8_t *, 8);
315 assign(s->above_intra_ctx, uint8_t *, 8);
316 assign(s->above_comp_ctx, uint8_t *, 8);
317 assign(s->above_ref_ctx, uint8_t *, 8);
318 assign(s->above_filter_ctx, uint8_t *, 8);
319 assign(s->lflvl, VP9Filter *, lflvl_len);
320#undef assign
321
322 if (s->td) {
323 for (i = 0; i < s->active_tile_cols; i++)
324 vp9_tile_data_free(&s->td[i]);
325 }
326
327 if (s->s.h.bpp != s->last_bpp) {
328 ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
329 ff_videodsp_init(&s->vdsp, s->s.h.bpp);
330 s->last_bpp = s->s.h.bpp;
331 changed = 1;
332 }
333
334 return changed;
335}
336
338{
339 int i;
340 VP9Context *s = avctx->priv_data;
341 int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
342 VP9TileData *td = &s->td[0];
343
344 if (td->b_base && td->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass)
345 return 0;
346
348 chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
349 chroma_eobs = 16 * 16 >> (s->ss_h + s->ss_v);
350 if (s->s.frames[CUR_FRAME].uses_2pass) {
351 int sbs = s->sb_cols * s->sb_rows;
352
353 td->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
354 td->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
355 16 * 16 + 2 * chroma_eobs) * sbs);
356 if (!td->b_base || !td->block_base)
357 return AVERROR(ENOMEM);
358 td->uvblock_base[0] = td->block_base + sbs * 64 * 64 * bytesperpixel;
359 td->uvblock_base[1] = td->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
360 td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
361 td->uveob_base[0] = td->eob_base + 16 * 16 * sbs;
362 td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs;
363
365 td->block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
366 if (!td->block_structure)
367 return AVERROR(ENOMEM);
368 }
369 } else {
370 for (i = 1; i < s->active_tile_cols; i++)
371 vp9_tile_data_free(&s->td[i]);
372
373 for (i = 0; i < s->active_tile_cols; i++) {
374 s->td[i].b_base = av_malloc(sizeof(VP9Block));
375 s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
376 16 * 16 + 2 * chroma_eobs);
377 if (!s->td[i].b_base || !s->td[i].block_base)
378 return AVERROR(ENOMEM);
379 s->td[i].uvblock_base[0] = s->td[i].block_base + 64 * 64 * bytesperpixel;
380 s->td[i].uvblock_base[1] = s->td[i].uvblock_base[0] + chroma_blocks * bytesperpixel;
381 s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel);
382 s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16;
383 s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs;
384
386 s->td[i].block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure));
387 if (!s->td[i].block_structure)
388 return AVERROR(ENOMEM);
389 }
390 }
391 }
392 s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass;
393
394 return 0;
395}
396
397// The sign bit is at the end, not the start, of a bit sequence
399{
400 int v = get_bits(gb, n);
401 return get_bits1(gb) ? -v : v;
402}
403
404static av_always_inline int inv_recenter_nonneg(int v, int m)
405{
406 if (v > 2 * m)
407 return v;
408 if (v & 1)
409 return m - ((v + 1) >> 1);
410 return m + (v >> 1);
411}
412
413// differential forward probability updates
414static int update_prob(VPXRangeCoder *c, int p)
415{
416 static const uint8_t inv_map_table[255] = {
417 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
418 189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
419 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
420 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
421 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
422 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
423 70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
424 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
425 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
426 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
427 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
428 146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
429 161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
430 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
431 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
432 207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
433 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
434 237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
435 252, 253, 253,
436 };
437 int d;
438
439 /* This code is trying to do a differential probability update. For a
440 * current probability A in the range [1, 255], the difference to a new
441 * probability of any value can be expressed differentially as 1-A, 255-A
442 * where some part of this (absolute range) exists both in positive as
443 * well as the negative part, whereas another part only exists in one
444 * half. We're trying to code this shared part differentially, i.e.
445 * times two where the value of the lowest bit specifies the sign, and
446 * the single part is then coded on top of this. This absolute difference
447 * then again has a value of [0, 254], but a bigger value in this range
448 * indicates that we're further away from the original value A, so we
449 * can code this as a VLC code, since higher values are increasingly
450 * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
451 * updates vs. the 'fine, exact' updates further down the range, which
452 * adds one extra dimension to this differential update model. */
453
454 if (!vp89_rac_get(c)) {
455 d = vp89_rac_get_uint(c, 4) + 0;
456 } else if (!vp89_rac_get(c)) {
457 d = vp89_rac_get_uint(c, 4) + 16;
458 } else if (!vp89_rac_get(c)) {
459 d = vp89_rac_get_uint(c, 5) + 32;
460 } else {
461 d = vp89_rac_get_uint(c, 7);
462 if (d >= 65)
463 d = (d << 1) - 65 + vp89_rac_get(c);
464 d += 64;
465 av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
466 }
467
468 return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
469 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
470}
471
473{
474 static const enum AVColorSpace colorspaces[8] = {
477 };
478 VP9Context *s = avctx->priv_data;
479 int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
480
481 s->bpp_index = bits;
482 s->s.h.bpp = 8 + bits * 2;
483 s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
484 avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
485 if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
486 static const enum AVPixelFormat pix_fmt_rgb[3] = {
488 };
489 s->ss_h = s->ss_v = 0;
491 s->pix_fmt = pix_fmt_rgb[bits];
492 if (avctx->profile & 1) {
493 if (get_bits1(&s->gb)) {
494 av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
495 return AVERROR_INVALIDDATA;
496 }
497 } else {
498 av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
499 avctx->profile);
500 return AVERROR_INVALIDDATA;
501 }
502 } else {
503 static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
510 };
512 if (avctx->profile & 1) {
513 s->ss_h = get_bits1(&s->gb);
514 s->ss_v = get_bits1(&s->gb);
515 s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
516 if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
517 av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
518 avctx->profile);
519 return AVERROR_INVALIDDATA;
520 } else if (get_bits1(&s->gb)) {
521 av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
522 avctx->profile);
523 return AVERROR_INVALIDDATA;
524 }
525 } else {
526 s->ss_h = s->ss_v = 1;
527 s->pix_fmt = pix_fmt_for_ss[bits][1][1];
528 }
529 }
530
531 return 0;
532}
533
535 const uint8_t *data, int size, int *ref)
536{
537 VP9Context *s = avctx->priv_data;
538 int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
539 int last_invisible;
540 const uint8_t *data2;
541 int changed;
542
543 /* general header */
544 if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
545 av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
546 return ret;
547 }
548 if (get_bits(&s->gb, 2) != 0x2) { // frame marker
549 av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
550 return AVERROR_INVALIDDATA;
551 }
552 avctx->profile = get_bits1(&s->gb);
553 avctx->profile |= get_bits1(&s->gb) << 1;
554 if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
555 if (avctx->profile > 3) {
556 av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
557 return AVERROR_INVALIDDATA;
558 }
559 s->s.h.profile = avctx->profile;
560 if (get_bits1(&s->gb)) {
561 *ref = get_bits(&s->gb, 3);
562 return 0;
563 }
564
565 s->last_keyframe = s->s.h.keyframe;
566 s->s.h.keyframe = !get_bits1(&s->gb);
567
568 last_invisible = s->s.h.invisible;
569 s->s.h.invisible = !get_bits1(&s->gb);
570 s->s.h.errorres = get_bits1(&s->gb);
571 s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
572
573 if (s->s.h.keyframe) {
574 if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
575 av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
576 return AVERROR_INVALIDDATA;
577 }
578 if ((ret = read_colorspace_details(avctx)) < 0)
579 return ret;
580 // for profile 1, here follows the subsampling bits
581 s->s.h.refreshrefmask = 0xff;
582 w = get_bits(&s->gb, 16) + 1;
583 h = get_bits(&s->gb, 16) + 1;
584 if (get_bits1(&s->gb)) // display size
585 skip_bits(&s->gb, 32);
586 } else {
587 s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
588 s->s.h.resetctx = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
589 if (s->s.h.intraonly) {
590 if (get_bits(&s->gb, 24) != VP9_SYNCCODE) { // synccode
591 av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
592 return AVERROR_INVALIDDATA;
593 }
594 if (avctx->profile >= 1) {
595 if ((ret = read_colorspace_details(avctx)) < 0)
596 return ret;
597 } else {
598 s->ss_h = s->ss_v = 1;
599 s->s.h.bpp = 8;
600 s->bpp_index = 0;
601 s->bytesperpixel = 1;
602 s->pix_fmt = AV_PIX_FMT_YUV420P;
605 }
606 s->s.h.refreshrefmask = get_bits(&s->gb, 8);
607 w = get_bits(&s->gb, 16) + 1;
608 h = get_bits(&s->gb, 16) + 1;
609 if (get_bits1(&s->gb)) // display size
610 skip_bits(&s->gb, 32);
611 } else {
612 s->s.h.refreshrefmask = get_bits(&s->gb, 8);
613 s->s.h.refidx[0] = get_bits(&s->gb, 3);
614 s->s.h.signbias[0] = get_bits1(&s->gb) && !s->s.h.errorres;
615 s->s.h.refidx[1] = get_bits(&s->gb, 3);
616 s->s.h.signbias[1] = get_bits1(&s->gb) && !s->s.h.errorres;
617 s->s.h.refidx[2] = get_bits(&s->gb, 3);
618 s->s.h.signbias[2] = get_bits1(&s->gb) && !s->s.h.errorres;
619 if (!s->s.refs[s->s.h.refidx[0]].f ||
620 !s->s.refs[s->s.h.refidx[1]].f ||
621 !s->s.refs[s->s.h.refidx[2]].f) {
622 av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
623 return AVERROR_INVALIDDATA;
624 }
625 if (get_bits1(&s->gb)) {
626 w = s->s.refs[s->s.h.refidx[0]].f->width;
627 h = s->s.refs[s->s.h.refidx[0]].f->height;
628 } else if (get_bits1(&s->gb)) {
629 w = s->s.refs[s->s.h.refidx[1]].f->width;
630 h = s->s.refs[s->s.h.refidx[1]].f->height;
631 } else if (get_bits1(&s->gb)) {
632 w = s->s.refs[s->s.h.refidx[2]].f->width;
633 h = s->s.refs[s->s.h.refidx[2]].f->height;
634 } else {
635 w = get_bits(&s->gb, 16) + 1;
636 h = get_bits(&s->gb, 16) + 1;
637 }
638 // Note that in this code, "CUR_FRAME" is actually before we
639 // have formally allocated a frame, and thus actually represents
640 // the _last_ frame
641 s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f &&
642 s->s.frames[CUR_FRAME].tf.f->width == w &&
643 s->s.frames[CUR_FRAME].tf.f->height == h;
644 if (get_bits1(&s->gb)) // display size
645 skip_bits(&s->gb, 32);
646 s->s.h.highprecisionmvs = get_bits1(&s->gb);
647 s->s.h.filtermode = get_bits1(&s->gb) ? FILTER_SWITCHABLE :
648 get_bits(&s->gb, 2);
649 s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
650 s->s.h.signbias[0] != s->s.h.signbias[2];
651 if (s->s.h.allowcompinter) {
652 if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
653 s->s.h.fixcompref = 2;
654 s->s.h.varcompref[0] = 0;
655 s->s.h.varcompref[1] = 1;
656 } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
657 s->s.h.fixcompref = 1;
658 s->s.h.varcompref[0] = 0;
659 s->s.h.varcompref[1] = 2;
660 } else {
661 s->s.h.fixcompref = 0;
662 s->s.h.varcompref[0] = 1;
663 s->s.h.varcompref[1] = 2;
664 }
665 }
666 }
667 }
668 s->s.h.refreshctx = s->s.h.errorres ? 0 : get_bits1(&s->gb);
669 s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
670 s->s.h.framectxid = c = get_bits(&s->gb, 2);
671 if (s->s.h.keyframe || s->s.h.intraonly)
672 s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
673
674 /* loopfilter header data */
675 if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
676 // reset loopfilter defaults
677 s->s.h.lf_delta.ref[0] = 1;
678 s->s.h.lf_delta.ref[1] = 0;
679 s->s.h.lf_delta.ref[2] = -1;
680 s->s.h.lf_delta.ref[3] = -1;
681 s->s.h.lf_delta.mode[0] = 0;
682 s->s.h.lf_delta.mode[1] = 0;
683 memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
684 }
685 s->s.h.filter.level = get_bits(&s->gb, 6);
686 sharp = get_bits(&s->gb, 3);
687 // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
688 // the old cache values since they are still valid
689 if (s->s.h.filter.sharpness != sharp) {
690 for (i = 1; i <= 63; i++) {
691 int limit = i;
692
693 if (sharp > 0) {
694 limit >>= (sharp + 3) >> 2;
695 limit = FFMIN(limit, 9 - sharp);
696 }
697 limit = FFMAX(limit, 1);
698
699 s->filter_lut.lim_lut[i] = limit;
700 s->filter_lut.mblim_lut[i] = 2 * (i + 2) + limit;
701 }
702 }
703 s->s.h.filter.sharpness = sharp;
704 if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
705 if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
706 for (i = 0; i < 4; i++)
707 if (get_bits1(&s->gb))
708 s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
709 for (i = 0; i < 2; i++)
710 if (get_bits1(&s->gb))
711 s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
712 }
713 }
714
715 /* quantization header data */
716 s->s.h.yac_qi = get_bits(&s->gb, 8);
717 s->s.h.ydc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
718 s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
719 s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
720 s->s.h.lossless = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
721 s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
722
723 /* segmentation header info */
724 if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
725 if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
726 for (i = 0; i < 7; i++)
727 s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
728 get_bits(&s->gb, 8) : 255;
729 if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
730 for (i = 0; i < 3; i++)
731 s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
732 get_bits(&s->gb, 8) : 255;
733 }
734
735 if (get_bits1(&s->gb)) {
736 s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
737 for (i = 0; i < 8; i++) {
738 if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
739 s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
740 if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
741 s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
742 if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
743 s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
744 s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
745 }
746 }
747 } else {
748 // Reset fields under segmentation switch if segmentation is disabled.
749 // This is necessary because some hwaccels don't ignore these fields
750 // if segmentation is disabled.
751 s->s.h.segmentation.temporal = 0;
752 s->s.h.segmentation.update_map = 0;
753 }
754
755 // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
756 for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
757 int qyac, qydc, quvac, quvdc, lflvl, sh;
758
759 if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
760 if (s->s.h.segmentation.absolute_vals)
761 qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
762 else
763 qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
764 } else {
765 qyac = s->s.h.yac_qi;
766 }
767 qydc = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
768 quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
769 quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
770 qyac = av_clip_uintp2(qyac, 8);
771
772 s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
773 s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
774 s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
775 s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
776
777 sh = s->s.h.filter.level >= 32;
778 if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
779 if (s->s.h.segmentation.absolute_vals)
780 lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
781 else
782 lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
783 } else {
784 lflvl = s->s.h.filter.level;
785 }
786 if (s->s.h.lf_delta.enabled) {
787 s->s.h.segmentation.feat[i].lflvl[0][0] =
788 s->s.h.segmentation.feat[i].lflvl[0][1] =
789 av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
790 for (j = 1; j < 4; j++) {
791 s->s.h.segmentation.feat[i].lflvl[j][0] =
792 av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
793 s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
794 s->s.h.segmentation.feat[i].lflvl[j][1] =
795 av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
796 s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
797 }
798 } else {
799 memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
800 sizeof(s->s.h.segmentation.feat[i].lflvl));
801 }
802 }
803
804 /* tiling info */
805 if ((changed = update_size(avctx, w, h)) < 0) {
806 av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
807 w, h, s->pix_fmt);
808 return changed;
809 }
810 for (s->s.h.tiling.log2_tile_cols = 0;
811 s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
812 s->s.h.tiling.log2_tile_cols++) ;
813 for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
814 max = FFMAX(0, max - 1);
815 while (max > s->s.h.tiling.log2_tile_cols) {
816 if (get_bits1(&s->gb))
817 s->s.h.tiling.log2_tile_cols++;
818 else
819 break;
820 }
821 s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
822 s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
823 if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols) || changed) {
824 int n_range_coders;
825 VPXRangeCoder *rc;
826
827 if (s->td) {
828 for (i = 0; i < s->active_tile_cols; i++)
829 vp9_tile_data_free(&s->td[i]);
830 av_freep(&s->td);
831 }
832
833 s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
834 s->active_tile_cols = avctx->active_thread_type == FF_THREAD_SLICE ?
835 s->s.h.tiling.tile_cols : 1;
836 vp9_alloc_entries(avctx, s->sb_rows);
837 if (avctx->active_thread_type == FF_THREAD_SLICE) {
838 n_range_coders = 4; // max_tile_rows
839 } else {
840 n_range_coders = s->s.h.tiling.tile_cols;
841 }
842 s->td = av_calloc(s->active_tile_cols, sizeof(VP9TileData) +
843 n_range_coders * sizeof(VPXRangeCoder));
844 if (!s->td)
845 return AVERROR(ENOMEM);
846 rc = (VPXRangeCoder *) &s->td[s->active_tile_cols];
847 for (i = 0; i < s->active_tile_cols; i++) {
848 s->td[i].s = s;
849 s->td[i].c_b = rc;
850 rc += n_range_coders;
851 }
852 }
853
854 /* check reference frames */
855 if (!s->s.h.keyframe && !s->s.h.intraonly) {
856 int valid_ref_frame = 0;
857 for (i = 0; i < 3; i++) {
858 AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
859 int refw = ref->width, refh = ref->height;
860
861 if (ref->format != avctx->pix_fmt) {
862 av_log(avctx, AV_LOG_ERROR,
863 "Ref pixfmt (%s) did not match current frame (%s)",
864 av_get_pix_fmt_name(ref->format),
866 return AVERROR_INVALIDDATA;
867 } else if (refw == w && refh == h) {
868 s->mvscale[i][0] = s->mvscale[i][1] = 0;
869 } else {
870 /* Check to make sure at least one of frames that */
871 /* this frame references has valid dimensions */
872 if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
873 av_log(avctx, AV_LOG_WARNING,
874 "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
875 refw, refh, w, h);
876 s->mvscale[i][0] = s->mvscale[i][1] = REF_INVALID_SCALE;
877 continue;
878 }
879 s->mvscale[i][0] = (refw << 14) / w;
880 s->mvscale[i][1] = (refh << 14) / h;
881 s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
882 s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
883 }
884 valid_ref_frame++;
885 }
886 if (!valid_ref_frame) {
887 av_log(avctx, AV_LOG_ERROR, "No valid reference frame is found, bitstream not supported\n");
888 return AVERROR_INVALIDDATA;
889 }
890 }
891
892 if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
893 s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
894 s->prob_ctx[3].p = ff_vp9_default_probs;
895 memcpy(s->prob_ctx[0].coef, ff_vp9_default_coef_probs,
897 memcpy(s->prob_ctx[1].coef, ff_vp9_default_coef_probs,
899 memcpy(s->prob_ctx[2].coef, ff_vp9_default_coef_probs,
901 memcpy(s->prob_ctx[3].coef, ff_vp9_default_coef_probs,
903 } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
904 s->prob_ctx[c].p = ff_vp9_default_probs;
905 memcpy(s->prob_ctx[c].coef, ff_vp9_default_coef_probs,
907 }
908
909 // next 16 bits is size of the rest of the header (arith-coded)
910 s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
911 s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
912
913 data2 = align_get_bits(&s->gb);
914 if (size2 > size - (data2 - data)) {
915 av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
916 return AVERROR_INVALIDDATA;
917 }
918 ret = ff_vpx_init_range_decoder(&s->c, data2, size2);
919 if (ret < 0)
920 return ret;
921
922 if (vpx_rac_get_prob_branchy(&s->c, 128)) { // marker bit
923 av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
924 return AVERROR_INVALIDDATA;
925 }
926
927 for (i = 0; i < s->active_tile_cols; i++) {
928 if (s->s.h.keyframe || s->s.h.intraonly) {
929 memset(s->td[i].counts.coef, 0, sizeof(s->td[0].counts.coef));
930 memset(s->td[i].counts.eob, 0, sizeof(s->td[0].counts.eob));
931 } else {
932 memset(&s->td[i].counts, 0, sizeof(s->td[0].counts));
933 }
934 s->td[i].nb_block_structure = 0;
935 }
936
937 /* FIXME is it faster to not copy here, but do it down in the fw updates
938 * as explicit copies if the fw update is missing (and skip the copy upon
939 * fw update)? */
940 s->prob.p = s->prob_ctx[c].p;
941
942 // txfm updates
943 if (s->s.h.lossless) {
944 s->s.h.txfmmode = TX_4X4;
945 } else {
946 s->s.h.txfmmode = vp89_rac_get_uint(&s->c, 2);
947 if (s->s.h.txfmmode == 3)
948 s->s.h.txfmmode += vp89_rac_get(&s->c);
949
950 if (s->s.h.txfmmode == TX_SWITCHABLE) {
951 for (i = 0; i < 2; i++)
952 if (vpx_rac_get_prob_branchy(&s->c, 252))
953 s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
954 for (i = 0; i < 2; i++)
955 for (j = 0; j < 2; j++)
956 if (vpx_rac_get_prob_branchy(&s->c, 252))
957 s->prob.p.tx16p[i][j] =
958 update_prob(&s->c, s->prob.p.tx16p[i][j]);
959 for (i = 0; i < 2; i++)
960 for (j = 0; j < 3; j++)
961 if (vpx_rac_get_prob_branchy(&s->c, 252))
962 s->prob.p.tx32p[i][j] =
963 update_prob(&s->c, s->prob.p.tx32p[i][j]);
964 }
965 }
966
967 // coef updates
968 for (i = 0; i < 4; i++) {
969 uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
970 if (vp89_rac_get(&s->c)) {
971 for (j = 0; j < 2; j++)
972 for (k = 0; k < 2; k++)
973 for (l = 0; l < 6; l++)
974 for (m = 0; m < 6; m++) {
975 uint8_t *p = s->prob.coef[i][j][k][l][m];
976 uint8_t *r = ref[j][k][l][m];
977 if (m >= 3 && l == 0) // dc only has 3 pt
978 break;
979 for (n = 0; n < 3; n++) {
980 if (vpx_rac_get_prob_branchy(&s->c, 252))
981 p[n] = update_prob(&s->c, r[n]);
982 else
983 p[n] = r[n];
984 }
985 memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
986 }
987 } else {
988 for (j = 0; j < 2; j++)
989 for (k = 0; k < 2; k++)
990 for (l = 0; l < 6; l++)
991 for (m = 0; m < 6; m++) {
992 uint8_t *p = s->prob.coef[i][j][k][l][m];
993 uint8_t *r = ref[j][k][l][m];
994 if (m > 3 && l == 0) // dc only has 3 pt
995 break;
996 memcpy(p, r, 3);
997 memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
998 }
999 }
1000 if (s->s.h.txfmmode == i)
1001 break;
1002 }
1003
1004 // mode updates
1005 for (i = 0; i < 3; i++)
1006 if (vpx_rac_get_prob_branchy(&s->c, 252))
1007 s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
1008 if (!s->s.h.keyframe && !s->s.h.intraonly) {
1009 for (i = 0; i < 7; i++)
1010 for (j = 0; j < 3; j++)
1011 if (vpx_rac_get_prob_branchy(&s->c, 252))
1012 s->prob.p.mv_mode[i][j] =
1013 update_prob(&s->c, s->prob.p.mv_mode[i][j]);
1014
1015 if (s->s.h.filtermode == FILTER_SWITCHABLE)
1016 for (i = 0; i < 4; i++)
1017 for (j = 0; j < 2; j++)
1018 if (vpx_rac_get_prob_branchy(&s->c, 252))
1019 s->prob.p.filter[i][j] =
1020 update_prob(&s->c, s->prob.p.filter[i][j]);
1021
1022 for (i = 0; i < 4; i++)
1023 if (vpx_rac_get_prob_branchy(&s->c, 252))
1024 s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
1025
1026 if (s->s.h.allowcompinter) {
1027 s->s.h.comppredmode = vp89_rac_get(&s->c);
1028 if (s->s.h.comppredmode)
1029 s->s.h.comppredmode += vp89_rac_get(&s->c);
1030 if (s->s.h.comppredmode == PRED_SWITCHABLE)
1031 for (i = 0; i < 5; i++)
1032 if (vpx_rac_get_prob_branchy(&s->c, 252))
1033 s->prob.p.comp[i] =
1034 update_prob(&s->c, s->prob.p.comp[i]);
1035 } else {
1036 s->s.h.comppredmode = PRED_SINGLEREF;
1037 }
1038
1039 if (s->s.h.comppredmode != PRED_COMPREF) {
1040 for (i = 0; i < 5; i++) {
1041 if (vpx_rac_get_prob_branchy(&s->c, 252))
1042 s->prob.p.single_ref[i][0] =
1043 update_prob(&s->c, s->prob.p.single_ref[i][0]);
1044 if (vpx_rac_get_prob_branchy(&s->c, 252))
1045 s->prob.p.single_ref[i][1] =
1046 update_prob(&s->c, s->prob.p.single_ref[i][1]);
1047 }
1048 }
1049
1050 if (s->s.h.comppredmode != PRED_SINGLEREF) {
1051 for (i = 0; i < 5; i++)
1052 if (vpx_rac_get_prob_branchy(&s->c, 252))
1053 s->prob.p.comp_ref[i] =
1054 update_prob(&s->c, s->prob.p.comp_ref[i]);
1055 }
1056
1057 for (i = 0; i < 4; i++)
1058 for (j = 0; j < 9; j++)
1059 if (vpx_rac_get_prob_branchy(&s->c, 252))
1060 s->prob.p.y_mode[i][j] =
1061 update_prob(&s->c, s->prob.p.y_mode[i][j]);
1062
1063 for (i = 0; i < 4; i++)
1064 for (j = 0; j < 4; j++)
1065 for (k = 0; k < 3; k++)
1066 if (vpx_rac_get_prob_branchy(&s->c, 252))
1067 s->prob.p.partition[3 - i][j][k] =
1068 update_prob(&s->c,
1069 s->prob.p.partition[3 - i][j][k]);
1070
1071 // mv fields don't use the update_prob subexp model for some reason
1072 for (i = 0; i < 3; i++)
1073 if (vpx_rac_get_prob_branchy(&s->c, 252))
1074 s->prob.p.mv_joint[i] = (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1075
1076 for (i = 0; i < 2; i++) {
1077 if (vpx_rac_get_prob_branchy(&s->c, 252))
1078 s->prob.p.mv_comp[i].sign =
1079 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1080
1081 for (j = 0; j < 10; j++)
1082 if (vpx_rac_get_prob_branchy(&s->c, 252))
1083 s->prob.p.mv_comp[i].classes[j] =
1084 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1085
1086 if (vpx_rac_get_prob_branchy(&s->c, 252))
1087 s->prob.p.mv_comp[i].class0 =
1088 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1089
1090 for (j = 0; j < 10; j++)
1091 if (vpx_rac_get_prob_branchy(&s->c, 252))
1092 s->prob.p.mv_comp[i].bits[j] =
1093 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1094 }
1095
1096 for (i = 0; i < 2; i++) {
1097 for (j = 0; j < 2; j++)
1098 for (k = 0; k < 3; k++)
1099 if (vpx_rac_get_prob_branchy(&s->c, 252))
1100 s->prob.p.mv_comp[i].class0_fp[j][k] =
1101 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1102
1103 for (j = 0; j < 3; j++)
1104 if (vpx_rac_get_prob_branchy(&s->c, 252))
1105 s->prob.p.mv_comp[i].fp[j] =
1106 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1107 }
1108
1109 if (s->s.h.highprecisionmvs) {
1110 for (i = 0; i < 2; i++) {
1111 if (vpx_rac_get_prob_branchy(&s->c, 252))
1112 s->prob.p.mv_comp[i].class0_hp =
1113 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1114
1115 if (vpx_rac_get_prob_branchy(&s->c, 252))
1116 s->prob.p.mv_comp[i].hp =
1117 (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
1118 }
1119 }
1120 }
1121
1122 return (data2 - data) + size2;
1123}
1124
1125static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1126 ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1127{
1128 const VP9Context *s = td->s;
1129 int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
1130 (((td->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
1131 const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
1132 s->prob.p.partition[bl][c];
1133 enum BlockPartition bp;
1134 ptrdiff_t hbs = 4 >> bl;
1135 AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1136 ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1137 int bytesperpixel = s->bytesperpixel;
1138
1139 if (bl == BL_8X8) {
1141 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1142 } else if (col + hbs < s->cols) { // FIXME why not <=?
1143 if (row + hbs < s->rows) { // FIXME why not <=?
1145 switch (bp) {
1146 case PARTITION_NONE:
1147 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1148 break;
1149 case PARTITION_H:
1150 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1151 yoff += hbs * 8 * y_stride;
1152 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1153 ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
1154 break;
1155 case PARTITION_V:
1156 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1157 yoff += hbs * 8 * bytesperpixel;
1158 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1159 ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
1160 break;
1161 case PARTITION_SPLIT:
1162 decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1163 decode_sb(td, row, col + hbs, lflvl,
1164 yoff + 8 * hbs * bytesperpixel,
1165 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1166 yoff += hbs * 8 * y_stride;
1167 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1168 decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1169 decode_sb(td, row + hbs, col + hbs, lflvl,
1170 yoff + 8 * hbs * bytesperpixel,
1171 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1172 break;
1173 default:
1174 av_unreachable("ff_vp9_partition_tree only has "
1175 "the four PARTITION_* terminal codes");
1176 }
1177 } else if (vpx_rac_get_prob_branchy(td->c, p[1])) {
1178 bp = PARTITION_SPLIT;
1179 decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1180 decode_sb(td, row, col + hbs, lflvl,
1181 yoff + 8 * hbs * bytesperpixel,
1182 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1183 } else {
1184 bp = PARTITION_H;
1185 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1186 }
1187 } else if (row + hbs < s->rows) { // FIXME why not <=?
1188 if (vpx_rac_get_prob_branchy(td->c, p[2])) {
1189 bp = PARTITION_SPLIT;
1190 decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1191 yoff += hbs * 8 * y_stride;
1192 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1193 decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1194 } else {
1195 bp = PARTITION_V;
1196 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1197 }
1198 } else {
1199 bp = PARTITION_SPLIT;
1200 decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1201 }
1202 td->counts.partition[bl][c][bp]++;
1203}
1204
1205static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1206 ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1207{
1208 const VP9Context *s = td->s;
1209 VP9Block *b = td->b;
1210 ptrdiff_t hbs = 4 >> bl;
1211 AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1212 ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1213 int bytesperpixel = s->bytesperpixel;
1214
1215 if (bl == BL_8X8) {
1216 av_assert2(b->bl == BL_8X8);
1217 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1218 } else if (td->b->bl == bl) {
1219 ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1220 if (b->bp == PARTITION_H && row + hbs < s->rows) {
1221 yoff += hbs * 8 * y_stride;
1222 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1223 ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, b->bl, b->bp);
1224 } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
1225 yoff += hbs * 8 * bytesperpixel;
1226 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1227 ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
1228 }
1229 } else {
1230 decode_sb_mem(td, row, col, lflvl, yoff, uvoff, bl + 1);
1231 if (col + hbs < s->cols) { // FIXME why not <=?
1232 if (row + hbs < s->rows) {
1233 decode_sb_mem(td, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
1234 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1235 yoff += hbs * 8 * y_stride;
1236 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1237 decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1238 decode_sb_mem(td, row + hbs, col + hbs, lflvl,
1239 yoff + 8 * hbs * bytesperpixel,
1240 uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1241 } else {
1242 yoff += hbs * 8 * bytesperpixel;
1243 uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1244 decode_sb_mem(td, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
1245 }
1246 } else if (row + hbs < s->rows) {
1247 yoff += hbs * 8 * y_stride;
1248 uvoff += hbs * 8 * uv_stride >> s->ss_v;
1249 decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1250 }
1251 }
1252}
1253
1254static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
1255{
1256 int sb_start = ( idx * n) >> log2_n;
1257 int sb_end = ((idx + 1) * n) >> log2_n;
1258 *start = FFMIN(sb_start, n) << 3;
1259 *end = FFMIN(sb_end, n) << 3;
1260}
1261
1263{
1264 int i;
1265
1266 av_freep(&s->intra_pred_data[0]);
1267 for (i = 0; i < s->active_tile_cols; i++)
1268 vp9_tile_data_free(&s->td[i]);
1269}
1270
1272{
1273 VP9Context *s = avctx->priv_data;
1274 int i;
1275
1276 for (int i = 0; i < 3; i++)
1277 vp9_frame_unref(&s->s.frames[i]);
1278 av_refstruct_pool_uninit(&s->frame_extradata_pool);
1279 for (i = 0; i < 8; i++) {
1280 ff_progress_frame_unref(&s->s.refs[i]);
1281 ff_progress_frame_unref(&s->next_refs[i]);
1282 vp9_frame_unref(&s->s.ref_frames[i]);
1283 }
1284
1285 free_buffers(s);
1286#if HAVE_THREADS
1287 av_freep(&s->entries);
1288 ff_pthread_free(s, vp9_context_offsets);
1289#endif
1290
1291 av_refstruct_unref(&s->header_ref);
1292 ff_cbs_fragment_free(&s->current_frag);
1293 ff_cbs_close(&s->cbc);
1294
1295 av_freep(&s->td);
1296 return 0;
1297}
1298
1300 const uint8_t *data, int size)
1301{
1302 VP9Context *s = avctx->priv_data;
1303 VP9TileData *td = &s->td[0];
1304 int row, col, tile_row, tile_col, ret;
1305 int bytesperpixel;
1306 int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1307 AVFrame *f;
1308 ptrdiff_t yoff, uvoff, ls_y, ls_uv;
1309
1310 f = s->s.frames[CUR_FRAME].tf.f;
1311 ls_y = f->linesize[0];
1312 ls_uv =f->linesize[1];
1313 bytesperpixel = s->bytesperpixel;
1314
1315 yoff = uvoff = 0;
1316 for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1317 set_tile_offset(&tile_row_start, &tile_row_end,
1318 tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1319
1320 for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1321 int64_t tile_size;
1322
1323 if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1324 tile_row == s->s.h.tiling.tile_rows - 1) {
1325 tile_size = size;
1326 } else {
1327 tile_size = AV_RB32(data);
1328 data += 4;
1329 size -= 4;
1330 }
1331 if (tile_size > size)
1332 return AVERROR_INVALIDDATA;
1333 ret = ff_vpx_init_range_decoder(&td->c_b[tile_col], data, tile_size);
1334 if (ret < 0)
1335 return ret;
1336 if (vpx_rac_get_prob_branchy(&td->c_b[tile_col], 128)) // marker bit
1337 return AVERROR_INVALIDDATA;
1338 data += tile_size;
1339 size -= tile_size;
1340 }
1341
1342 for (row = tile_row_start; row < tile_row_end;
1343 row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1344 VP9Filter *lflvl_ptr = s->lflvl;
1345 ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1346
1347 for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1348 set_tile_offset(&tile_col_start, &tile_col_end,
1349 tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1350 td->tile_col_start = tile_col_start;
1351 if (s->pass != 2) {
1352 memset(td->left_partition_ctx, 0, 8);
1353 memset(td->left_skip_ctx, 0, 8);
1354 if (s->s.h.keyframe || s->s.h.intraonly) {
1355 memset(td->left_mode_ctx, DC_PRED, 16);
1356 } else {
1357 memset(td->left_mode_ctx, NEARESTMV, 8);
1358 }
1359 memset(td->left_y_nnz_ctx, 0, 16);
1360 memset(td->left_uv_nnz_ctx, 0, 32);
1361 memset(td->left_segpred_ctx, 0, 8);
1362
1363 td->c = &td->c_b[tile_col];
1364 }
1365
1366 for (col = tile_col_start;
1367 col < tile_col_end;
1368 col += 8, yoff2 += 64 * bytesperpixel,
1369 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1370 // FIXME integrate with lf code (i.e. zero after each
1371 // use, similar to invtxfm coefficients, or similar)
1372 if (s->pass != 1) {
1373 memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1374 }
1375
1376 if (s->pass == 2) {
1377 decode_sb_mem(td, row, col, lflvl_ptr,
1378 yoff2, uvoff2, BL_64X64);
1379 } else {
1380 if (vpx_rac_is_end(td->c)) {
1381 return AVERROR_INVALIDDATA;
1382 }
1383 decode_sb(td, row, col, lflvl_ptr,
1384 yoff2, uvoff2, BL_64X64);
1385 }
1386 }
1387 }
1388
1389 if (s->pass == 1)
1390 continue;
1391
1392 // backup pre-loopfilter reconstruction data for intra
1393 // prediction of next row of sb64s
1394 if (row + 8 < s->rows) {
1395 memcpy(s->intra_pred_data[0],
1396 f->data[0] + yoff + 63 * ls_y,
1397 8 * s->cols * bytesperpixel);
1398 memcpy(s->intra_pred_data[1],
1399 f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1400 8 * s->cols * bytesperpixel >> s->ss_h);
1401 memcpy(s->intra_pred_data[2],
1402 f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1403 8 * s->cols * bytesperpixel >> s->ss_h);
1404 }
1405
1406 // loopfilter one row
1407 if (s->s.h.filter.level) {
1408 yoff2 = yoff;
1409 uvoff2 = uvoff;
1410 lflvl_ptr = s->lflvl;
1411 for (col = 0; col < s->cols;
1412 col += 8, yoff2 += 64 * bytesperpixel,
1413 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1414 ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
1415 yoff2, uvoff2);
1416 }
1417 }
1418
1419 // FIXME maybe we can make this more finegrained by running the
1420 // loopfilter per-block instead of after each sbrow
1421 // In fact that would also make intra pred left preparation easier?
1422 ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, row >> 3);
1423 }
1424 }
1425 return 0;
1426}
1427
1428#if HAVE_THREADS
1429static av_always_inline
1430int decode_tiles_mt(AVCodecContext *avctx, void *tdata, int jobnr,
1431 int threadnr)
1432{
1433 VP9Context *s = avctx->priv_data;
1434 VP9TileData *td = &s->td[jobnr];
1435 ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1436 int bytesperpixel = s->bytesperpixel, row, col, tile_row;
1437 unsigned tile_cols_len;
1438 int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1439 VP9Filter *lflvl_ptr_base;
1440 AVFrame *f;
1441
1442 f = s->s.frames[CUR_FRAME].tf.f;
1443 ls_y = f->linesize[0];
1444 ls_uv =f->linesize[1];
1445
1446 set_tile_offset(&tile_col_start, &tile_col_end,
1447 jobnr, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1448 td->tile_col_start = tile_col_start;
1449 uvoff = (64 * bytesperpixel >> s->ss_h)*(tile_col_start >> 3);
1450 yoff = (64 * bytesperpixel)*(tile_col_start >> 3);
1451 lflvl_ptr_base = s->lflvl+(tile_col_start >> 3);
1452
1453 for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1454 set_tile_offset(&tile_row_start, &tile_row_end,
1455 tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1456
1457 td->c = &td->c_b[tile_row];
1458 for (row = tile_row_start; row < tile_row_end;
1459 row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1460 ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1461 VP9Filter *lflvl_ptr = lflvl_ptr_base+s->sb_cols*(row >> 3);
1462
1463 memset(td->left_partition_ctx, 0, 8);
1464 memset(td->left_skip_ctx, 0, 8);
1465 if (s->s.h.keyframe || s->s.h.intraonly) {
1466 memset(td->left_mode_ctx, DC_PRED, 16);
1467 } else {
1468 memset(td->left_mode_ctx, NEARESTMV, 8);
1469 }
1470 memset(td->left_y_nnz_ctx, 0, 16);
1471 memset(td->left_uv_nnz_ctx, 0, 32);
1472 memset(td->left_segpred_ctx, 0, 8);
1473
1474 for (col = tile_col_start;
1475 col < tile_col_end;
1476 col += 8, yoff2 += 64 * bytesperpixel,
1477 uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1478 // FIXME integrate with lf code (i.e. zero after each
1479 // use, similar to invtxfm coefficients, or similar)
1480 memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1481 decode_sb(td, row, col, lflvl_ptr,
1482 yoff2, uvoff2, BL_64X64);
1483 }
1484
1485 // backup pre-loopfilter reconstruction data for intra
1486 // prediction of next row of sb64s
1487 tile_cols_len = tile_col_end - tile_col_start;
1488 if (row + 8 < s->rows) {
1489 memcpy(s->intra_pred_data[0] + (tile_col_start * 8 * bytesperpixel),
1490 f->data[0] + yoff + 63 * ls_y,
1491 8 * tile_cols_len * bytesperpixel);
1492 memcpy(s->intra_pred_data[1] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1493 f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1494 8 * tile_cols_len * bytesperpixel >> s->ss_h);
1495 memcpy(s->intra_pred_data[2] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1496 f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1497 8 * tile_cols_len * bytesperpixel >> s->ss_h);
1498 }
1499
1500 vp9_report_tile_progress(s, row >> 3, 1);
1501 }
1502 }
1503 return 0;
1504}
1505
1506static av_always_inline
1507int loopfilter_proc(AVCodecContext *avctx)
1508{
1509 VP9Context *s = avctx->priv_data;
1510 ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1511 VP9Filter *lflvl_ptr;
1512 int bytesperpixel = s->bytesperpixel, col, i;
1513 AVFrame *f;
1514
1515 f = s->s.frames[CUR_FRAME].tf.f;
1516 ls_y = f->linesize[0];
1517 ls_uv =f->linesize[1];
1518
1519 for (i = 0; i < s->sb_rows; i++) {
1520 vp9_await_tile_progress(s, i, s->s.h.tiling.tile_cols);
1521
1522 if (s->s.h.filter.level) {
1523 yoff = (ls_y * 64)*i;
1524 uvoff = (ls_uv * 64 >> s->ss_v)*i;
1525 lflvl_ptr = s->lflvl+s->sb_cols*i;
1526 for (col = 0; col < s->cols;
1527 col += 8, yoff += 64 * bytesperpixel,
1528 uvoff += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1529 ff_vp9_loopfilter_sb(avctx, lflvl_ptr, i << 3, col,
1530 yoff, uvoff);
1531 }
1532 }
1533 }
1534 return 0;
1535}
1536#endif
1537
1539{
1540 AVVideoEncParams *par;
1541 unsigned int tile, nb_blocks = 0;
1542
1543 if (s->s.h.segmentation.enabled) {
1544 for (tile = 0; tile < s->active_tile_cols; tile++)
1545 nb_blocks += s->td[tile].nb_block_structure;
1546 }
1547
1549 AV_VIDEO_ENC_PARAMS_VP9, nb_blocks);
1550 if (!par)
1551 return AVERROR(ENOMEM);
1552
1553 par->qp = s->s.h.yac_qi;
1554 par->delta_qp[0][0] = s->s.h.ydc_qdelta;
1555 par->delta_qp[1][0] = s->s.h.uvdc_qdelta;
1556 par->delta_qp[2][0] = s->s.h.uvdc_qdelta;
1557 par->delta_qp[1][1] = s->s.h.uvac_qdelta;
1558 par->delta_qp[2][1] = s->s.h.uvac_qdelta;
1559
1560 if (nb_blocks) {
1561 unsigned int block = 0;
1562 unsigned int tile, block_tile;
1563
1564 for (tile = 0; tile < s->active_tile_cols; tile++) {
1565 VP9TileData *td = &s->td[tile];
1566
1567 for (block_tile = 0; block_tile < td->nb_block_structure; block_tile++) {
1569 unsigned int row = td->block_structure[block_tile].row;
1570 unsigned int col = td->block_structure[block_tile].col;
1571 uint8_t seg_id = frame->segmentation_map[row * 8 * s->sb_cols + col];
1572
1573 b->src_x = col * 8;
1574 b->src_y = row * 8;
1575 b->w = 1 << (3 + td->block_structure[block_tile].block_size_idx_x);
1576 b->h = 1 << (3 + td->block_structure[block_tile].block_size_idx_y);
1577
1578 if (s->s.h.segmentation.feat[seg_id].q_enabled) {
1579 b->delta_qp = s->s.h.segmentation.feat[seg_id].q_val;
1580 if (s->s.h.segmentation.absolute_vals)
1581 b->delta_qp -= par->qp;
1582 }
1583 }
1584 }
1585 }
1586
1587 return 0;
1588}
1589
1591 const AVPacket *pkt)
1592{
1593 VP9Context *s = avctx->priv_data;
1594 const uint8_t *sd;
1595 size_t sd_size;
1596
1598 &sd_size);
1599 if (!sd || sd_size < 8 || AV_RB64(sd) != 1)
1600 return;
1601
1603 &s->webm_alpha_warned,
1604 "Ignoring unsupported WebM alpha channel side data; use the "
1605 "libvpx-vp9 decoder to decode it.\n");
1606}
1607
1609 int *got_frame, AVPacket *pkt)
1610{
1611 const uint8_t *data = pkt->data;
1612 int size = pkt->size;
1613 VP9Context *s = avctx->priv_data;
1614 int ret, i, j, ref;
1615 CodedBitstreamUnit *unit;
1616 VP9RawFrame *rf;
1617
1618 int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
1619 (!s->s.h.segmentation.enabled || !s->s.h.segmentation.update_map);
1620 const VP9Frame *src;
1621 AVFrame *f;
1622
1624
1625 ret = ff_cbs_read_packet(s->cbc, &s->current_frag, pkt);
1626 if (ret < 0) {
1627 ff_cbs_fragment_reset(&s->current_frag);
1628 av_log(avctx, AV_LOG_ERROR, "Failed to read frame header.\n");
1629 return ret;
1630 }
1631
1632 unit = &s->current_frag.units[0];
1633 rf = unit->content;
1634
1635 av_refstruct_replace(&s->header_ref, unit->content_ref);
1636 s->frame_header = &rf->header;
1637
1638 if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
1639 ff_cbs_fragment_reset(&s->current_frag);
1640 return ret;
1641 } else if (ret == 0) {
1642 if (!s->s.refs[ref].f) {
1643 av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
1644 ff_cbs_fragment_reset(&s->current_frag);
1645 return AVERROR_INVALIDDATA;
1646 }
1647 for (int i = 0; i < 8; i++)
1648 ff_progress_frame_replace(&s->next_refs[i], &s->s.refs[i]);
1650 ff_progress_frame_await(&s->s.refs[ref], INT_MAX);
1651 ff_cbs_fragment_reset(&s->current_frag);
1652
1653 if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
1654 return ret;
1655 frame->pts = pkt->pts;
1656 frame->pkt_dts = pkt->dts;
1657 if (pkt->flags & AV_PKT_FLAG_DISCARD)
1658 frame->flags |= AV_FRAME_FLAG_DISCARD;
1659 else
1660 frame->flags &= ~AV_FRAME_FLAG_DISCARD;
1661 *got_frame = 1;
1662 return pkt->size;
1663 }
1664 data += ret;
1665 size -= ret;
1666
1667 src = !s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres ?
1668 &s->s.frames[CUR_FRAME] : &s->s.frames[BLANK_FRAME];
1669 if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly)
1672 vp9_frame_unref(&s->s.frames[CUR_FRAME]);
1673 if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0) {
1674 ff_cbs_fragment_reset(&s->current_frag);
1675 return ret;
1676 }
1677
1678 s->s.frames[CUR_FRAME].header_ref = av_refstruct_ref(s->header_ref);
1679 s->s.frames[CUR_FRAME].frame_header = s->frame_header;
1680
1681 f = s->s.frames[CUR_FRAME].tf.f;
1682 if (s->s.h.keyframe)
1683 f->flags |= AV_FRAME_FLAG_KEY;
1684 else
1685 f->flags &= ~AV_FRAME_FLAG_KEY;
1686 if (s->s.h.lossless)
1687 f->flags |= AV_FRAME_FLAG_LOSSLESS;
1688 else
1689 f->flags &= ~AV_FRAME_FLAG_LOSSLESS;
1690 f->pict_type = (s->s.h.keyframe || s->s.h.intraonly) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1691
1692 // Non-existent frames have the implicit dimension 0x0 != CUR_FRAME
1693 if (!s->s.frames[REF_FRAME_MVPAIR].tf.f ||
1694 (s->s.frames[REF_FRAME_MVPAIR].tf.f->width != s->s.frames[CUR_FRAME].tf.f->width ||
1695 s->s.frames[REF_FRAME_MVPAIR].tf.f->height != s->s.frames[CUR_FRAME].tf.f->height)) {
1696 vp9_frame_unref(&s->s.frames[REF_FRAME_SEGMAP]);
1697 }
1698
1699 // ref frame setup
1700 for (i = 0; i < 8; i++) {
1701 ff_progress_frame_replace(&s->next_refs[i],
1702 s->s.h.refreshrefmask & (1 << i) ?
1703 &s->s.frames[CUR_FRAME].tf : &s->s.refs[i]);
1704 }
1705
1706 if (avctx->hwaccel) {
1707 const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
1708 ret = hwaccel->start_frame(avctx, pkt->buf, pkt->data, pkt->size);
1709 if (ret < 0)
1710 goto fail;
1711 ret = hwaccel->decode_slice(avctx, pkt->data, pkt->size);
1712 if (ret < 0)
1713 goto fail;
1714 ret = hwaccel->end_frame(avctx);
1715 if (ret < 0)
1716 goto fail;
1717
1718 for (i = 0; i < 8; i++) {
1719 vp9_frame_replace(&s->s.ref_frames[i],
1720 s->s.h.refreshrefmask & (1 << i) ?
1721 &s->s.frames[CUR_FRAME] : &s->s.ref_frames[i]);
1722 }
1723
1724 goto finish;
1725 }
1726
1727 // main tile decode loop
1728 memset(s->above_partition_ctx, 0, s->cols);
1729 memset(s->above_skip_ctx, 0, s->cols);
1730 if (s->s.h.keyframe || s->s.h.intraonly) {
1731 memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
1732 } else {
1733 memset(s->above_mode_ctx, NEARESTMV, s->cols);
1734 }
1735 memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
1736 memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
1737 memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
1738 memset(s->above_segpred_ctx, 0, s->cols);
1739 s->pass = s->s.frames[CUR_FRAME].uses_2pass =
1740 avctx->active_thread_type == FF_THREAD_FRAME && s->s.h.refreshctx && !s->s.h.parallelmode;
1741 if ((ret = update_block_buffers(avctx)) < 0) {
1742 av_log(avctx, AV_LOG_ERROR,
1743 "Failed to allocate block buffers\n");
1744 goto fail;
1745 }
1746 if (s->s.h.refreshctx && s->s.h.parallelmode) {
1747 int j, k, l, m;
1748
1749 for (i = 0; i < 4; i++) {
1750 for (j = 0; j < 2; j++)
1751 for (k = 0; k < 2; k++)
1752 for (l = 0; l < 6; l++)
1753 for (m = 0; m < 6; m++)
1754 memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
1755 s->prob.coef[i][j][k][l][m], 3);
1756 if (s->s.h.txfmmode == i)
1757 break;
1758 }
1759 s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
1761 } else if (!s->s.h.refreshctx) {
1763 }
1764
1765#if HAVE_THREADS
1766 if (avctx->active_thread_type & FF_THREAD_SLICE) {
1767 for (i = 0; i < s->sb_rows; i++)
1768 atomic_init(&s->entries[i], 0);
1769 }
1770#endif
1771
1772 do {
1773 for (i = 0; i < s->active_tile_cols; i++) {
1774 s->td[i].b = s->td[i].b_base;
1775 s->td[i].block = s->td[i].block_base;
1776 s->td[i].uvblock[0] = s->td[i].uvblock_base[0];
1777 s->td[i].uvblock[1] = s->td[i].uvblock_base[1];
1778 s->td[i].eob = s->td[i].eob_base;
1779 s->td[i].uveob[0] = s->td[i].uveob_base[0];
1780 s->td[i].uveob[1] = s->td[i].uveob_base[1];
1781 s->td[i].error_info = 0;
1782 }
1783
1784#if HAVE_THREADS
1785 if (avctx->active_thread_type == FF_THREAD_SLICE) {
1786 int tile_row, tile_col;
1787
1788 av_assert1(!s->pass);
1789
1790 for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1791 for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1792 int64_t tile_size;
1793
1794 if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1795 tile_row == s->s.h.tiling.tile_rows - 1) {
1796 tile_size = size;
1797 } else {
1798 tile_size = AV_RB32(data);
1799 data += 4;
1800 size -= 4;
1801 }
1802 if (tile_size > size) {
1804 goto fail;
1805 }
1806 ret = ff_vpx_init_range_decoder(&s->td[tile_col].c_b[tile_row], data, tile_size);
1807 if (ret < 0)
1808 goto fail;
1809 if (vpx_rac_get_prob_branchy(&s->td[tile_col].c_b[tile_row], 128)) { // marker bit
1811 goto fail;
1812 }
1813 data += tile_size;
1814 size -= tile_size;
1815 }
1816 }
1817
1818 ff_slice_thread_execute_with_mainfunc(avctx, decode_tiles_mt, loopfilter_proc, s->td, NULL, s->s.h.tiling.tile_cols);
1819 } else
1820#endif
1821 {
1822 ret = decode_tiles(avctx, data, size);
1823 if (ret < 0)
1824 goto fail;
1825 }
1826
1827 // Sum all counts fields into td[0].counts for tile threading
1828 if (avctx->active_thread_type == FF_THREAD_SLICE)
1829 for (i = 1; i < s->s.h.tiling.tile_cols; i++)
1830 for (j = 0; j < sizeof(s->td[i].counts) / sizeof(unsigned); j++)
1831 ((unsigned *)&s->td[0].counts)[j] += ((unsigned *)&s->td[i].counts)[j];
1832
1833 if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
1836 }
1837 } while (s->pass++ == 1);
1838
1839 if (s->td->error_info < 0) {
1840 av_log(avctx, AV_LOG_ERROR, "Failed to decode tile data\n");
1841 s->td->error_info = 0;
1843 goto fail;
1844 }
1846 ret = vp9_export_enc_params(s, &s->s.frames[CUR_FRAME]);
1847 if (ret < 0)
1848 goto fail;
1849 }
1850
1851finish:
1852 ff_cbs_fragment_reset(&s->current_frag);
1853
1854 ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, INT_MAX);
1855 // ref frame setup
1856 for (int i = 0; i < 8; i++)
1857 ff_progress_frame_replace(&s->s.refs[i], &s->next_refs[i]);
1858
1859 if (!s->s.h.invisible) {
1860 if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
1861 return ret;
1862 *got_frame = 1;
1863 }
1864
1865 return pkt->size;
1866fail:
1867 ff_cbs_fragment_reset(&s->current_frag);
1868 ff_progress_frame_report(&s->s.frames[CUR_FRAME].tf, INT_MAX);
1869 return ret;
1870}
1871
1873{
1874 VP9Context *s = avctx->priv_data;
1875 int i;
1876
1877 for (i = 0; i < 3; i++)
1878 vp9_frame_unref(&s->s.frames[i]);
1879
1880 for (i = 0; i < 8; i++) {
1881 ff_progress_frame_unref(&s->s.refs[i]);
1882 ff_progress_frame_unref(&s->next_refs[i]);
1883 vp9_frame_unref(&s->s.ref_frames[i]);
1884 }
1885
1886 ff_cbs_fragment_reset(&s->current_frag);
1887 ff_cbs_flush(s->cbc);
1888
1889 if (FF_HW_HAS_CB(avctx, flush))
1890 FF_HW_SIMPLE_CALL(avctx, flush);
1891}
1892
1894{
1895 VP9Context *s = avctx->priv_data;
1896 int ret;
1897
1898 s->last_bpp = 0;
1899 s->s.h.filter.sharpness = -1;
1900
1901 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_VP9, avctx);
1902 if (ret < 0)
1903 return ret;
1904
1905#if HAVE_THREADS
1906 if (avctx->active_thread_type & FF_THREAD_SLICE) {
1907 ret = ff_pthread_init(s, vp9_context_offsets);
1908 if (ret < 0)
1909 return ret;
1910 }
1911#endif
1912
1913 return 0;
1914}
1915
1916#if HAVE_THREADS
1917static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1918{
1919 VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
1920
1921 for (int i = 0; i < 3; i++)
1922 vp9_frame_replace(&s->s.frames[i], &ssrc->s.frames[i]);
1923 for (int i = 0; i < 8; i++)
1924 ff_progress_frame_replace(&s->s.refs[i], &ssrc->next_refs[i]);
1925 av_refstruct_replace(&s->frame_extradata_pool, ssrc->frame_extradata_pool);
1926 s->frame_extradata_pool_size = ssrc->frame_extradata_pool_size;
1927
1928 av_refstruct_replace(&s->header_ref, ssrc->header_ref);
1929 for (int i = 0; i < 8; i++)
1930 vp9_frame_replace(&s->s.ref_frames[i], &ssrc->s.ref_frames[i]);
1931
1932 s->frame_header = ssrc->frame_header;
1933 memcpy(s->cbc->priv_data, ssrc->cbc->priv_data, sizeof(CodedBitstreamVP9Context));
1934
1935 s->s.h.invisible = ssrc->s.h.invisible;
1936 s->s.h.keyframe = ssrc->s.h.keyframe;
1937 s->s.h.intraonly = ssrc->s.h.intraonly;
1938 s->ss_v = ssrc->ss_v;
1939 s->ss_h = ssrc->ss_h;
1940 s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
1941 s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
1942 s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
1943 s->bytesperpixel = ssrc->bytesperpixel;
1944 s->gf_fmt = ssrc->gf_fmt;
1945 s->w = ssrc->w;
1946 s->h = ssrc->h;
1947 s->s.h.bpp = ssrc->s.h.bpp;
1948 s->bpp_index = ssrc->bpp_index;
1949 s->pix_fmt = ssrc->pix_fmt;
1950 s->webm_alpha_warned = ssrc->webm_alpha_warned;
1951 memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
1952 memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
1953 memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
1954 sizeof(s->s.h.segmentation.feat));
1955
1956 return 0;
1957}
1958#endif
1959
1961 .p.name = "vp9",
1962 CODEC_LONG_NAME("Google VP9"),
1963 .p.type = AVMEDIA_TYPE_VIDEO,
1964 .p.id = AV_CODEC_ID_VP9,
1965 .priv_data_size = sizeof(VP9Context),
1970 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
1973 .flush = vp9_decode_flush,
1974 UPDATE_THREAD_CONTEXT(vp9_decode_update_thread_context),
1976 .bsfs = "vp9_superframe_split",
1977 .hw_configs = (const AVCodecHWConfigInternal *const []) {
1978#if CONFIG_VP9_DXVA2_HWACCEL
1979 HWACCEL_DXVA2(vp9),
1980#endif
1981#if CONFIG_VP9_D3D11VA_HWACCEL
1982 HWACCEL_D3D11VA(vp9),
1983#endif
1984#if CONFIG_VP9_D3D11VA2_HWACCEL
1985 HWACCEL_D3D11VA2(vp9),
1986#endif
1987#if CONFIG_VP9_D3D12VA_HWACCEL
1988 HWACCEL_D3D12VA(vp9),
1989#endif
1990#if CONFIG_VP9_NVDEC_HWACCEL
1991 HWACCEL_NVDEC(vp9),
1992#endif
1993#if CONFIG_VP9_NVDEC_CUARRAY_HWACCEL
1995#endif
1996#if CONFIG_VP9_VAAPI_HWACCEL
1997 HWACCEL_VAAPI(vp9),
1998#endif
1999#if CONFIG_VP9_VDPAU_HWACCEL
2000 HWACCEL_VDPAU(vp9),
2001#endif
2002#if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
2004#endif
2005#if CONFIG_VP9_VULKAN_HWACCEL
2006 HWACCEL_VULKAN(vp9),
2007#endif
2008 NULL
2009 },
2010};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_vp9_decoder
Definition vp9.c:1960
static void finish(void)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1600
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition avcodec.h:1601
static int BS_FUNC decode012(BSCTX *bc)
Return decoded truncated unary code for the values 0, 1, 2.
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF
Codec initializes slice-based threading with a main function.
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip_uintp2
Definition common.h:124
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define max(a, b)
static int16_t block[64]
Definition dct.c:125
void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
Do nothing if dst and src already refer to the same AVFrame; otherwise unreference dst and if src is ...
Definition decode.c:2006
void ff_progress_frame_await(const ProgressFrame *f, int n)
Wait for earlier decoding threads to finish reference frames.
Definition decode.c:2021
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
Definition decode.c:2016
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition decode.c:1248
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
Definition decode.c:2383
int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
Wrapper around ff_progress_frame_alloc() and ff_thread_get_buffer().
Definition decode.c:1976
void ff_progress_frame_unref(ProgressFrame *f)
Give up a reference to the underlying frame contained in a ProgressFrame and reset the ProgressFrame,...
Definition decode.c:1999
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
static AVPacket * pkt
static AVFrame * frame
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:615
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
static const uint8_t bits[8]
Definition fastaudio.c:100
static const char * hwaccel
Definition ffplay.c:406
int ret
Definition ffplay.c:391
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define fail
Definition test.h:479
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition codec.h:102
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
#define AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS
Decoding only.
Definition avcodec.h:399
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
@ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
Data found in BlockAdditional element of matroska container.
Definition packet.h:188
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition packet.h:664
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_LOSSLESS
A decoder can use this flag to mark frames which were originally encoded losslessly.
Definition frame.h:715
#define AV_FRAME_FLAG_DISCARD
A flag to mark the frames which need to be decoded, but shouldn't be output.
Definition frame.h:698
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:694
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:281
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define DC_PRED
Definition h264pred.h:40
#define FF_HW_HAS_CB(avctx, function)
#define FF_HW_SIMPLE_CALL(avctx, function)
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_NVDEC_CUARRAY(codec)
Definition hwconfig.h:70
#define HWACCEL_DXVA2(codec)
Definition hwconfig.h:64
#define HWACCEL_VDPAU(codec)
Definition hwconfig.h:74
#define HWACCEL_D3D12VA(codec)
Definition hwconfig.h:82
#define HWACCEL_VULKAN(codec)
Definition hwconfig.h:78
#define HWACCEL_NVDEC(codec)
Definition hwconfig.h:68
#define HWACCEL_VAAPI(codec)
Definition hwconfig.h:72
#define HWACCEL_D3D11VA(codec)
Definition hwconfig.h:80
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition hwconfig.h:76
#define HWACCEL_D3D11VA2(codec)
Definition hwconfig.h:66
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RB32(p)
#define AV_RB64(p)
#define HWACCEL_MAX
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition pthread.c:92
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Definition pthread.c:105
Multithreading API for decoders.
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition videodsp.c:39
av_cold void ff_vp9dsp_init(VP9DSPContext *dsp, int bpp, int bitexact)
Definition vp9dsp.c:88
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static enum AVPixelFormat pix_fmt_rgb[3]
Definition libdav1d.c:66
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
uint8_t w
Definition llvidencdsp.c:39
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition log.c:456
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
const char data[16]
Definition mxf.c:155
#define av_malloc(s)
Definition ops_static.c:52
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition os2threads.h:158
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:132
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition os2threads.h:198
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:555
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:554
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:553
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:568
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition pixfmt.h:305
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition pixfmt.h:440
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition pixfmt.h:134
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
@ AV_PIX_FMT_CUARRAY
hardware decoding through openharmony
Definition pixfmt.h:506
@ AV_PIX_FMT_D3D11VA_VLD
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition pixfmt.h:254
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition pixfmt.h:126
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_VDPAU
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition pixfmt.h:194
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:711
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:713
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:717
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:712
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition pixfmt.h:722
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:714
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition pixfmt.h:718
@ AVCOL_SPC_SMPTE240M
derived from 170M primaries and D65 white point, 170M is derived from BT470 System M's primaries
Definition pixfmt.h:719
@ AVCOL_SPC_RESERVED
reserved for future use by ITU-T and ISO/IEC just like 15-255 are
Definition pixfmt.h:715
const AVProfile ff_vp9_profiles[]
Definition profiles.c:169
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds)
int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2 *func2, main_func *mainfunc, void *arg, int *ret, int job_count)
AVRefStructPool * av_refstruct_pool_alloc(size_t size, unsigned flags)
Equivalent to av_refstruct_pool_alloc(size, flags, NULL, NULL, NULL, NULL, NULL)
Definition refstruct.c:353
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition refstruct.c:315
void * av_refstruct_ref(void *obj)
Create a new reference to an object managed via this API, i.e.
Definition refstruct.c:141
#define AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME
If this flag is set, the entries will be zeroed before being returned to the user (after the init or ...
Definition refstruct.h:221
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
#define FF_ARRAY_ELEMS(a)
@ memory_order_release
Definition stdatomic.h:32
@ memory_order_relaxed
Definition stdatomic.h:29
@ memory_order_acquire
Definition stdatomic.h:31
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:297
int atomic_int
Definition stdatomic.h:63
#define atomic_load_explicit(object, order)
Definition stdatomic.h:247
#define atomic_init(obj, value)
Definition stdatomic.h:119
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:685
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1429
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1608
int profile
profile
Definition avcodec.h:1646
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition avcodec.h:1789
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:672
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
Data structure for storing block-level encoding information.
Video encoding parameters for a given frame.
int32_t delta_qp[4][2]
Quantisation parameter offset from the base (per-frame) qp for a given plane (first index) and AC/DC ...
int32_t qp
Base quantisation parameter for the frame.
Coded bitstream unit structure.
Definition cbs.h:77
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition cbs.h:119
enum BlockLevel bl
Definition vp9dec.h:91
uint8_t mask[2][2][8][4]
Definition vp9dec.h:82
VP9RawFrameHeader header
Definition cbs_vp9.h:165
uint8_t left_segpred_ctx[8]
Definition vp9dec.h:225
VP9Block * b_base
Definition vp9dec.h:182
unsigned int block_size_idx_x
Definition vp9dec.h:242
uint8_t left_y_nnz_ctx[16]
Definition vp9dec.h:218
uint8_t * uveob_base[2]
Definition vp9dec.h:235
const VP9Context * s
Definition vp9dec.h:176
unsigned int nb_block_structure
Definition vp9dec.h:245
uint8_t left_skip_ctx[8]
Definition vp9dec.h:223
unsigned int block_size_idx_y
Definition vp9dec.h:243
uint8_t * eob_base
Definition vp9dec.h:235
struct VP9TileData::@362347210250227013314212060146323350261010304114 counts
int16_t * uvblock_base[2]
Definition vp9dec.h:234
int16_t * block_base
Definition vp9dec.h:234
VPXRangeCoder * c
Definition vp9dec.h:178
VPXRangeCoder * c_b
Definition vp9dec.h:177
unsigned partition[4][4][4]
Definition vp9dec.h:209
struct VP9TileData::@101265210003061101033147345251051344325013111222 * block_structure
uint8_t left_uv_nnz_ctx[2][16]
Definition vp9dec.h:221
uint8_t left_mode_ctx[16]
Definition vp9dec.h:219
VP9Block * b
Definition vp9dec.h:182
unsigned tile_col_start
Definition vp9dec.h:183
uint8_t left_partition_ctx[8]
Definition vp9dec.h:222
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
int size
static double limit(double x)
AVVideoEncParams * av_video_enc_params_create_side_data(AVFrame *frame, enum AVVideoEncParamsType type, unsigned int nb_blocks)
Allocates memory for AVEncodeInfoFrame plus an array of nb_blocks AVEncodeInfoBlock in the given AVFr...
static av_always_inline AVVideoBlockParams * av_video_enc_params_block(AVVideoEncParams *par, unsigned int idx)
Get the block at the specified idx.
@ AV_VIDEO_ENC_PARAMS_VP9
VP9 stores:
Core video DSP helper functions.
Range decoder functions common to VP8 and VP9.
static av_always_inline int vp89_rac_get(VPXRangeCoder *c)
Definition vp89_rac.h:36
static av_always_inline int vp89_rac_get_tree(VPXRangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition vp89_rac.h:54
static av_unused int vp89_rac_get_uint(VPXRangeCoder *c, int bits)
Definition vp89_rac.h:41
static void vp9_tile_data_free(VP9TileData *td)
Definition vp9.c:92
static int read_colorspace_details(AVCodecContext *avctx)
Definition vp9.c:472
static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
Definition vp9.c:108
static int update_size(AVCodecContext *avctx, int w, int h)
Definition vp9.c:171
static int update_prob(VPXRangeCoder *c, int p)
Definition vp9.c:414
static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
Definition vp9.c:1205
#define VP9_SYNCCODE
Definition vp9.c:51
static int decode_frame_header(AVCodecContext *avctx, const uint8_t *data, int size, int *ref)
Definition vp9.c:534
static av_cold void vp9_decode_flush(AVCodecContext *avctx)
Definition vp9.c:1872
static av_cold int vp9_decode_init(AVCodecContext *avctx)
Definition vp9.c:1893
static void free_buffers(VP9Context *s)
Definition vp9.c:1262
static int vp9_alloc_entries(AVCodecContext *avctx, int n)
Definition vp9.c:89
static void vp9_frame_replace(VP9Frame *dst, const VP9Frame *src)
Definition vp9.c:155
static int vp9_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition vp9.c:1608
static av_always_inline int inv_recenter_nonneg(int v, int m)
Definition vp9.c:404
static void vp9_warn_unsupported_webm_alpha(AVCodecContext *avctx, const AVPacket *pkt)
Definition vp9.c:1590
static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
Definition vp9.c:398
static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
Definition vp9.c:1254
static int decode_tiles(AVCodecContext *avctx, const uint8_t *data, int size)
Definition vp9.c:1299
static void vp9_frame_unref(VP9Frame *f)
Definition vp9.c:99
static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame)
Definition vp9.c:1538
static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
Definition vp9.c:1125
static av_cold int vp9_decode_free(AVCodecContext *avctx)
Definition vp9.c:1271
static int update_block_buffers(AVCodecContext *avctx)
Definition vp9.c:337
#define assign(var, type, n)
@ FILTER_SWITCHABLE
Definition vp9.h:70
@ TX_4X4
Definition vp9.h:28
@ TX_SWITCHABLE
Definition vp9.h:33
void ff_vp9_decode_block(VP9TileData *td, int row, int col, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl, enum BlockPartition bp)
Definition vp9block.c:1264
const ProbContext ff_vp9_default_probs
Definition vp9data.c:1435
const int16_t ff_vp9_ac_qlookup[3][256]
Definition vp9data.c:334
const uint8_t ff_vp9_default_kf_partition_probs[4][4][3]
Definition vp9data.c:41
const int16_t ff_vp9_dc_qlookup[3][256]
Definition vp9data.c:231
const uint8_t ff_vp9_default_coef_probs[4][2][2][6][6][3]
Definition vp9data.c:1540
const uint8_t ff_vp9_model_pareto8[256][8]
Definition vp9data.c:1176
const int8_t ff_vp9_partition_tree[3][2]
Definition vp9data.c:35
#define REF_INVALID_SCALE
Definition vp9dec.h:43
void ff_vp9_loopfilter_sb(struct AVCodecContext *avctx, VP9Filter *lflvl, int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
Definition vp9lpf.c:179
void ff_vp9_adapt_probs(VP9Context *s)
Definition vp9prob.c:44
#define REF_FRAME_MVPAIR
Definition vp9shared.h:173
#define REF_FRAME_SEGMAP
Definition vp9shared.h:174
#define BLANK_FRAME
Definition vp9shared.h:175
@ PRED_SWITCHABLE
Definition vp9shared.h:53
@ PRED_SINGLEREF
Definition vp9shared.h:51
@ PRED_COMPREF
Definition vp9shared.h:52
BlockLevel
Definition vp9shared.h:79
@ BL_64X64
Definition vp9shared.h:80
@ BL_8X8
Definition vp9shared.h:83
#define CUR_FRAME
Definition vp9shared.h:172
@ NEARESTMV
Definition vp9shared.h:44
BlockPartition
Definition vp9shared.h:36
@ PARTITION_SPLIT
Definition vp9shared.h:40
@ PARTITION_H
Definition vp9shared.h:38
@ PARTITION_NONE
Definition vp9shared.h:37
@ PARTITION_V
Definition vp9shared.h:39
int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size)
Definition vpx_rac.c:42
Common VP5-VP9 range decoder stuff.
static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
Definition vpx_rac.h:99
static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c)
returns 1 if the end of the stream has been reached, 0 otherwise.
Definition vpx_rac.h:51
static double c[64]