FFmpeg
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 "avcodec.h"
25 #include "get_bits.h"
26 #include "hwaccel.h"
27 #include "internal.h"
28 #include "profiles.h"
29 #include "thread.h"
30 #include "videodsp.h"
31 #include "vp56.h"
32 #include "vp9.h"
33 #include "vp9data.h"
34 #include "vp9dec.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/pixdesc.h"
37 
38 #define VP9_SYNCCODE 0x498342
39 
40 #if HAVE_THREADS
41 static void vp9_free_entries(AVCodecContext *avctx) {
42  VP9Context *s = avctx->priv_data;
43 
44  if (avctx->active_thread_type & FF_THREAD_SLICE) {
45  pthread_mutex_destroy(&s->progress_mutex);
46  pthread_cond_destroy(&s->progress_cond);
47  av_freep(&s->entries);
48  }
49 }
50 
51 static int vp9_alloc_entries(AVCodecContext *avctx, int n) {
52  VP9Context *s = avctx->priv_data;
53  int i;
54 
55  if (avctx->active_thread_type & FF_THREAD_SLICE) {
56  if (s->entries)
57  av_freep(&s->entries);
58 
59  s->entries = av_malloc_array(n, sizeof(atomic_int));
60 
61  if (!s->entries) {
62  av_freep(&s->entries);
63  return AVERROR(ENOMEM);
64  }
65 
66  for (i = 0; i < n; i++)
67  atomic_init(&s->entries[i], 0);
68 
69  pthread_mutex_init(&s->progress_mutex, NULL);
70  pthread_cond_init(&s->progress_cond, NULL);
71  }
72  return 0;
73 }
74 
75 static void vp9_report_tile_progress(VP9Context *s, int field, int n) {
76  pthread_mutex_lock(&s->progress_mutex);
77  atomic_fetch_add_explicit(&s->entries[field], n, memory_order_release);
78  pthread_cond_signal(&s->progress_cond);
79  pthread_mutex_unlock(&s->progress_mutex);
80 }
81 
82 static void vp9_await_tile_progress(VP9Context *s, int field, int n) {
83  if (atomic_load_explicit(&s->entries[field], memory_order_acquire) >= n)
84  return;
85 
86  pthread_mutex_lock(&s->progress_mutex);
87  while (atomic_load_explicit(&s->entries[field], memory_order_relaxed) != n)
88  pthread_cond_wait(&s->progress_cond, &s->progress_mutex);
89  pthread_mutex_unlock(&s->progress_mutex);
90 }
91 #else
92 static void vp9_free_entries(AVCodecContext *avctx) {}
93 static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; }
94 #endif
95 
97 {
98  ff_thread_release_buffer(avctx, &f->tf);
101  f->segmentation_map = NULL;
103 }
104 
106 {
107  VP9Context *s = avctx->priv_data;
108  int ret, sz;
109 
111  if (ret < 0)
112  return ret;
113 
114  sz = 64 * s->sb_cols * s->sb_rows;
115  f->extradata = av_buffer_allocz(sz * (1 + sizeof(VP9mvrefPair)));
116  if (!f->extradata) {
117  goto fail;
118  }
119 
121  f->mv = (VP9mvrefPair *) (f->extradata->data + sz);
122 
123  if (avctx->hwaccel) {
124  const AVHWAccel *hwaccel = avctx->hwaccel;
126  if (hwaccel->frame_priv_data_size) {
128  if (!f->hwaccel_priv_buf)
129  goto fail;
131  }
132  }
133 
134  return 0;
135 
136 fail:
137  vp9_frame_unref(avctx, f);
138  return AVERROR(ENOMEM);
139 }
140 
142 {
143  int ret;
144 
145  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
146  if (ret < 0)
147  return ret;
148 
149  dst->extradata = av_buffer_ref(src->extradata);
150  if (!dst->extradata)
151  goto fail;
152 
154  dst->mv = src->mv;
155  dst->uses_2pass = src->uses_2pass;
156 
157  if (src->hwaccel_picture_private) {
159  if (!dst->hwaccel_priv_buf)
160  goto fail;
162  }
163 
164  return 0;
165 
166 fail:
167  vp9_frame_unref(avctx, dst);
168  return AVERROR(ENOMEM);
169 }
170 
171 static 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_NVDEC_HWACCEL + \
176  CONFIG_VP9_VAAPI_HWACCEL + \
177  CONFIG_VP9_VDPAU_HWACCEL)
178  enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmtp = pix_fmts;
179  VP9Context *s = avctx->priv_data;
180  uint8_t *p;
181  int bytesperpixel = s->bytesperpixel, ret, cols, rows;
182  int lflvl_len, i;
183 
184  av_assert0(w > 0 && h > 0);
185 
186  if (!(s->pix_fmt == s->gf_fmt && w == s->w && h == s->h)) {
187  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
188  return ret;
189 
190  switch (s->pix_fmt) {
191  case AV_PIX_FMT_YUV420P:
192 #if CONFIG_VP9_VDPAU_HWACCEL
193  *fmtp++ = AV_PIX_FMT_VDPAU;
194 #endif
196 #if CONFIG_VP9_DXVA2_HWACCEL
197  *fmtp++ = AV_PIX_FMT_DXVA2_VLD;
198 #endif
199 #if CONFIG_VP9_D3D11VA_HWACCEL
200  *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
201  *fmtp++ = AV_PIX_FMT_D3D11;
202 #endif
203 #if CONFIG_VP9_NVDEC_HWACCEL
204  *fmtp++ = AV_PIX_FMT_CUDA;
205 #endif
206 #if CONFIG_VP9_VAAPI_HWACCEL
207  *fmtp++ = AV_PIX_FMT_VAAPI;
208 #endif
209  break;
211 #if CONFIG_VP9_NVDEC_HWACCEL
212  *fmtp++ = AV_PIX_FMT_CUDA;
213 #endif
214 #if CONFIG_VP9_VAAPI_HWACCEL
215  *fmtp++ = AV_PIX_FMT_VAAPI;
216 #endif
217  break;
218  }
219 
220  *fmtp++ = s->pix_fmt;
221  *fmtp = AV_PIX_FMT_NONE;
222 
223  ret = ff_thread_get_format(avctx, pix_fmts);
224  if (ret < 0)
225  return ret;
226 
227  avctx->pix_fmt = ret;
228  s->gf_fmt = s->pix_fmt;
229  s->w = w;
230  s->h = h;
231  }
232 
233  cols = (w + 7) >> 3;
234  rows = (h + 7) >> 3;
235 
236  if (s->intra_pred_data[0] && cols == s->cols && rows == s->rows && s->pix_fmt == s->last_fmt)
237  return 0;
238 
239  s->last_fmt = s->pix_fmt;
240  s->sb_cols = (w + 63) >> 6;
241  s->sb_rows = (h + 63) >> 6;
242  s->cols = (w + 7) >> 3;
243  s->rows = (h + 7) >> 3;
244  lflvl_len = avctx->active_thread_type == FF_THREAD_SLICE ? s->sb_rows : 1;
245 
246 #define assign(var, type, n) var = (type) p; p += s->sb_cols * (n) * sizeof(*var)
247  av_freep(&s->intra_pred_data[0]);
248  // FIXME we slightly over-allocate here for subsampled chroma, but a little
249  // bit of padding shouldn't affect performance...
250  p = av_malloc(s->sb_cols * (128 + 192 * bytesperpixel +
251  lflvl_len * sizeof(*s->lflvl) + 16 * sizeof(*s->above_mv_ctx)));
252  if (!p)
253  return AVERROR(ENOMEM);
254  assign(s->intra_pred_data[0], uint8_t *, 64 * bytesperpixel);
255  assign(s->intra_pred_data[1], uint8_t *, 64 * bytesperpixel);
256  assign(s->intra_pred_data[2], uint8_t *, 64 * bytesperpixel);
257  assign(s->above_y_nnz_ctx, uint8_t *, 16);
258  assign(s->above_mode_ctx, uint8_t *, 16);
259  assign(s->above_mv_ctx, VP56mv(*)[2], 16);
260  assign(s->above_uv_nnz_ctx[0], uint8_t *, 16);
261  assign(s->above_uv_nnz_ctx[1], uint8_t *, 16);
263  assign(s->above_skip_ctx, uint8_t *, 8);
264  assign(s->above_txfm_ctx, uint8_t *, 8);
265  assign(s->above_segpred_ctx, uint8_t *, 8);
266  assign(s->above_intra_ctx, uint8_t *, 8);
267  assign(s->above_comp_ctx, uint8_t *, 8);
268  assign(s->above_ref_ctx, uint8_t *, 8);
269  assign(s->above_filter_ctx, uint8_t *, 8);
270  assign(s->lflvl, VP9Filter *, lflvl_len);
271 #undef assign
272 
273  if (s->td) {
274  for (i = 0; i < s->active_tile_cols; i++) {
275  av_freep(&s->td[i].b_base);
276  av_freep(&s->td[i].block_base);
277  }
278  }
279 
280  if (s->s.h.bpp != s->last_bpp) {
281  ff_vp9dsp_init(&s->dsp, s->s.h.bpp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
282  ff_videodsp_init(&s->vdsp, s->s.h.bpp);
283  s->last_bpp = s->s.h.bpp;
284  }
285 
286  return 0;
287 }
288 
290 {
291  int i;
292  VP9Context *s = avctx->priv_data;
293  int chroma_blocks, chroma_eobs, bytesperpixel = s->bytesperpixel;
294  VP9TileData *td = &s->td[0];
295 
297  return 0;
298 
299  av_free(td->b_base);
300  av_free(td->block_base);
301  chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v);
302  chroma_eobs = 16 * 16 >> (s->ss_h + s->ss_v);
303  if (s->s.frames[CUR_FRAME].uses_2pass) {
304  int sbs = s->sb_cols * s->sb_rows;
305 
306  td->b_base = av_malloc_array(s->cols * s->rows, sizeof(VP9Block));
307  td->block_base = av_mallocz(((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
308  16 * 16 + 2 * chroma_eobs) * sbs);
309  if (!td->b_base || !td->block_base)
310  return AVERROR(ENOMEM);
311  td->uvblock_base[0] = td->block_base + sbs * 64 * 64 * bytesperpixel;
312  td->uvblock_base[1] = td->uvblock_base[0] + sbs * chroma_blocks * bytesperpixel;
313  td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel);
314  td->uveob_base[0] = td->eob_base + 16 * 16 * sbs;
315  td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs;
316  } else {
317  for (i = 1; i < s->active_tile_cols; i++) {
318  if (s->td[i].b_base && s->td[i].block_base) {
319  av_free(s->td[i].b_base);
320  av_free(s->td[i].block_base);
321  }
322  }
323  for (i = 0; i < s->active_tile_cols; i++) {
324  s->td[i].b_base = av_malloc(sizeof(VP9Block));
325  s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) +
326  16 * 16 + 2 * chroma_eobs);
327  if (!s->td[i].b_base || !s->td[i].block_base)
328  return AVERROR(ENOMEM);
329  s->td[i].uvblock_base[0] = s->td[i].block_base + 64 * 64 * bytesperpixel;
330  s->td[i].uvblock_base[1] = s->td[i].uvblock_base[0] + chroma_blocks * bytesperpixel;
331  s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel);
332  s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16;
333  s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs;
334  }
335  }
337 
338  return 0;
339 }
340 
341 // The sign bit is at the end, not the start, of a bit sequence
343 {
344  int v = get_bits(gb, n);
345  return get_bits1(gb) ? -v : v;
346 }
347 
348 static av_always_inline int inv_recenter_nonneg(int v, int m)
349 {
350  if (v > 2 * m)
351  return v;
352  if (v & 1)
353  return m - ((v + 1) >> 1);
354  return m + (v >> 1);
355 }
356 
357 // differential forward probability updates
358 static int update_prob(VP56RangeCoder *c, int p)
359 {
360  static const uint8_t inv_map_table[255] = {
361  7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
362  189, 202, 215, 228, 241, 254, 1, 2, 3, 4, 5, 6, 8, 9,
363  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24,
364  25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39,
365  40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54,
366  55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
367  70, 71, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
368  86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 99, 100,
369  101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115,
370  116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130,
371  131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
372  146, 147, 148, 149, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
373  161, 162, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
374  177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 190, 191,
375  192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 203, 204, 205, 206,
376  207, 208, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 220, 221,
377  222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236,
378  237, 238, 239, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251,
379  252, 253, 253,
380  };
381  int d;
382 
383  /* This code is trying to do a differential probability update. For a
384  * current probability A in the range [1, 255], the difference to a new
385  * probability of any value can be expressed differentially as 1-A, 255-A
386  * where some part of this (absolute range) exists both in positive as
387  * well as the negative part, whereas another part only exists in one
388  * half. We're trying to code this shared part differentially, i.e.
389  * times two where the value of the lowest bit specifies the sign, and
390  * the single part is then coded on top of this. This absolute difference
391  * then again has a value of [0, 254], but a bigger value in this range
392  * indicates that we're further away from the original value A, so we
393  * can code this as a VLC code, since higher values are increasingly
394  * unlikely. The first 20 values in inv_map_table[] allow 'cheap, rough'
395  * updates vs. the 'fine, exact' updates further down the range, which
396  * adds one extra dimension to this differential update model. */
397 
398  if (!vp8_rac_get(c)) {
399  d = vp8_rac_get_uint(c, 4) + 0;
400  } else if (!vp8_rac_get(c)) {
401  d = vp8_rac_get_uint(c, 4) + 16;
402  } else if (!vp8_rac_get(c)) {
403  d = vp8_rac_get_uint(c, 5) + 32;
404  } else {
405  d = vp8_rac_get_uint(c, 7);
406  if (d >= 65)
407  d = (d << 1) - 65 + vp8_rac_get(c);
408  d += 64;
409  av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
410  }
411 
412  return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
413  255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
414 }
415 
417 {
418  static const enum AVColorSpace colorspaces[8] = {
421  };
422  VP9Context *s = avctx->priv_data;
423  int bits = avctx->profile <= 1 ? 0 : 1 + get_bits1(&s->gb); // 0:8, 1:10, 2:12
424 
425  s->bpp_index = bits;
426  s->s.h.bpp = 8 + bits * 2;
427  s->bytesperpixel = (7 + s->s.h.bpp) >> 3;
428  avctx->colorspace = colorspaces[get_bits(&s->gb, 3)];
429  if (avctx->colorspace == AVCOL_SPC_RGB) { // RGB = profile 1
430  static const enum AVPixelFormat pix_fmt_rgb[3] = {
432  };
433  s->ss_h = s->ss_v = 0;
434  avctx->color_range = AVCOL_RANGE_JPEG;
435  s->pix_fmt = pix_fmt_rgb[bits];
436  if (avctx->profile & 1) {
437  if (get_bits1(&s->gb)) {
438  av_log(avctx, AV_LOG_ERROR, "Reserved bit set in RGB\n");
439  return AVERROR_INVALIDDATA;
440  }
441  } else {
442  av_log(avctx, AV_LOG_ERROR, "RGB not supported in profile %d\n",
443  avctx->profile);
444  return AVERROR_INVALIDDATA;
445  }
446  } else {
447  static const enum AVPixelFormat pix_fmt_for_ss[3][2 /* v */][2 /* h */] = {
454  };
456  if (avctx->profile & 1) {
457  s->ss_h = get_bits1(&s->gb);
458  s->ss_v = get_bits1(&s->gb);
459  s->pix_fmt = pix_fmt_for_ss[bits][s->ss_v][s->ss_h];
460  if (s->pix_fmt == AV_PIX_FMT_YUV420P) {
461  av_log(avctx, AV_LOG_ERROR, "YUV 4:2:0 not supported in profile %d\n",
462  avctx->profile);
463  return AVERROR_INVALIDDATA;
464  } else if (get_bits1(&s->gb)) {
465  av_log(avctx, AV_LOG_ERROR, "Profile %d color details reserved bit set\n",
466  avctx->profile);
467  return AVERROR_INVALIDDATA;
468  }
469  } else {
470  s->ss_h = s->ss_v = 1;
471  s->pix_fmt = pix_fmt_for_ss[bits][1][1];
472  }
473  }
474 
475  return 0;
476 }
477 
479  const uint8_t *data, int size, int *ref)
480 {
481  VP9Context *s = avctx->priv_data;
482  int c, i, j, k, l, m, n, w, h, max, size2, ret, sharp;
483  int last_invisible;
484  const uint8_t *data2;
485 
486  /* general header */
487  if ((ret = init_get_bits8(&s->gb, data, size)) < 0) {
488  av_log(avctx, AV_LOG_ERROR, "Failed to initialize bitstream reader\n");
489  return ret;
490  }
491  if (get_bits(&s->gb, 2) != 0x2) { // frame marker
492  av_log(avctx, AV_LOG_ERROR, "Invalid frame marker\n");
493  return AVERROR_INVALIDDATA;
494  }
495  avctx->profile = get_bits1(&s->gb);
496  avctx->profile |= get_bits1(&s->gb) << 1;
497  if (avctx->profile == 3) avctx->profile += get_bits1(&s->gb);
498  if (avctx->profile > 3) {
499  av_log(avctx, AV_LOG_ERROR, "Profile %d is not yet supported\n", avctx->profile);
500  return AVERROR_INVALIDDATA;
501  }
502  s->s.h.profile = avctx->profile;
503  if (get_bits1(&s->gb)) {
504  *ref = get_bits(&s->gb, 3);
505  return 0;
506  }
507 
508  s->last_keyframe = s->s.h.keyframe;
509  s->s.h.keyframe = !get_bits1(&s->gb);
510 
511  last_invisible = s->s.h.invisible;
512  s->s.h.invisible = !get_bits1(&s->gb);
513  s->s.h.errorres = get_bits1(&s->gb);
514  s->s.h.use_last_frame_mvs = !s->s.h.errorres && !last_invisible;
515 
516  if (s->s.h.keyframe) {
517  if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
518  av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
519  return AVERROR_INVALIDDATA;
520  }
521  if ((ret = read_colorspace_details(avctx)) < 0)
522  return ret;
523  // for profile 1, here follows the subsampling bits
524  s->s.h.refreshrefmask = 0xff;
525  w = get_bits(&s->gb, 16) + 1;
526  h = get_bits(&s->gb, 16) + 1;
527  if (get_bits1(&s->gb)) // display size
528  skip_bits(&s->gb, 32);
529  } else {
530  s->s.h.intraonly = s->s.h.invisible ? get_bits1(&s->gb) : 0;
531  s->s.h.resetctx = s->s.h.errorres ? 0 : get_bits(&s->gb, 2);
532  if (s->s.h.intraonly) {
533  if (get_bits_long(&s->gb, 24) != VP9_SYNCCODE) { // synccode
534  av_log(avctx, AV_LOG_ERROR, "Invalid sync code\n");
535  return AVERROR_INVALIDDATA;
536  }
537  if (avctx->profile >= 1) {
538  if ((ret = read_colorspace_details(avctx)) < 0)
539  return ret;
540  } else {
541  s->ss_h = s->ss_v = 1;
542  s->s.h.bpp = 8;
543  s->bpp_index = 0;
544  s->bytesperpixel = 1;
545  s->pix_fmt = AV_PIX_FMT_YUV420P;
546  avctx->colorspace = AVCOL_SPC_BT470BG;
547  avctx->color_range = AVCOL_RANGE_MPEG;
548  }
549  s->s.h.refreshrefmask = get_bits(&s->gb, 8);
550  w = get_bits(&s->gb, 16) + 1;
551  h = get_bits(&s->gb, 16) + 1;
552  if (get_bits1(&s->gb)) // display size
553  skip_bits(&s->gb, 32);
554  } else {
555  s->s.h.refreshrefmask = get_bits(&s->gb, 8);
556  s->s.h.refidx[0] = get_bits(&s->gb, 3);
557  s->s.h.signbias[0] = get_bits1(&s->gb) && !s->s.h.errorres;
558  s->s.h.refidx[1] = get_bits(&s->gb, 3);
559  s->s.h.signbias[1] = get_bits1(&s->gb) && !s->s.h.errorres;
560  s->s.h.refidx[2] = get_bits(&s->gb, 3);
561  s->s.h.signbias[2] = get_bits1(&s->gb) && !s->s.h.errorres;
562  if (!s->s.refs[s->s.h.refidx[0]].f->buf[0] ||
563  !s->s.refs[s->s.h.refidx[1]].f->buf[0] ||
564  !s->s.refs[s->s.h.refidx[2]].f->buf[0]) {
565  av_log(avctx, AV_LOG_ERROR, "Not all references are available\n");
566  return AVERROR_INVALIDDATA;
567  }
568  if (get_bits1(&s->gb)) {
569  w = s->s.refs[s->s.h.refidx[0]].f->width;
570  h = s->s.refs[s->s.h.refidx[0]].f->height;
571  } else if (get_bits1(&s->gb)) {
572  w = s->s.refs[s->s.h.refidx[1]].f->width;
573  h = s->s.refs[s->s.h.refidx[1]].f->height;
574  } else if (get_bits1(&s->gb)) {
575  w = s->s.refs[s->s.h.refidx[2]].f->width;
576  h = s->s.refs[s->s.h.refidx[2]].f->height;
577  } else {
578  w = get_bits(&s->gb, 16) + 1;
579  h = get_bits(&s->gb, 16) + 1;
580  }
581  // Note that in this code, "CUR_FRAME" is actually before we
582  // have formally allocated a frame, and thus actually represents
583  // the _last_ frame
584  s->s.h.use_last_frame_mvs &= s->s.frames[CUR_FRAME].tf.f->width == w &&
585  s->s.frames[CUR_FRAME].tf.f->height == h;
586  if (get_bits1(&s->gb)) // display size
587  skip_bits(&s->gb, 32);
588  s->s.h.highprecisionmvs = get_bits1(&s->gb);
590  get_bits(&s->gb, 2);
591  s->s.h.allowcompinter = s->s.h.signbias[0] != s->s.h.signbias[1] ||
592  s->s.h.signbias[0] != s->s.h.signbias[2];
593  if (s->s.h.allowcompinter) {
594  if (s->s.h.signbias[0] == s->s.h.signbias[1]) {
595  s->s.h.fixcompref = 2;
596  s->s.h.varcompref[0] = 0;
597  s->s.h.varcompref[1] = 1;
598  } else if (s->s.h.signbias[0] == s->s.h.signbias[2]) {
599  s->s.h.fixcompref = 1;
600  s->s.h.varcompref[0] = 0;
601  s->s.h.varcompref[1] = 2;
602  } else {
603  s->s.h.fixcompref = 0;
604  s->s.h.varcompref[0] = 1;
605  s->s.h.varcompref[1] = 2;
606  }
607  }
608  }
609  }
610  s->s.h.refreshctx = s->s.h.errorres ? 0 : get_bits1(&s->gb);
611  s->s.h.parallelmode = s->s.h.errorres ? 1 : get_bits1(&s->gb);
612  s->s.h.framectxid = c = get_bits(&s->gb, 2);
613  if (s->s.h.keyframe || s->s.h.intraonly)
614  s->s.h.framectxid = 0; // BUG: libvpx ignores this field in keyframes
615 
616  /* loopfilter header data */
617  if (s->s.h.keyframe || s->s.h.errorres || s->s.h.intraonly) {
618  // reset loopfilter defaults
619  s->s.h.lf_delta.ref[0] = 1;
620  s->s.h.lf_delta.ref[1] = 0;
621  s->s.h.lf_delta.ref[2] = -1;
622  s->s.h.lf_delta.ref[3] = -1;
623  s->s.h.lf_delta.mode[0] = 0;
624  s->s.h.lf_delta.mode[1] = 0;
625  memset(s->s.h.segmentation.feat, 0, sizeof(s->s.h.segmentation.feat));
626  }
627  s->s.h.filter.level = get_bits(&s->gb, 6);
628  sharp = get_bits(&s->gb, 3);
629  // if sharpness changed, reinit lim/mblim LUTs. if it didn't change, keep
630  // the old cache values since they are still valid
631  if (s->s.h.filter.sharpness != sharp) {
632  for (i = 1; i <= 63; i++) {
633  int limit = i;
634 
635  if (sharp > 0) {
636  limit >>= (sharp + 3) >> 2;
637  limit = FFMIN(limit, 9 - sharp);
638  }
639  limit = FFMAX(limit, 1);
640 
641  s->filter_lut.lim_lut[i] = limit;
642  s->filter_lut.mblim_lut[i] = 2 * (i + 2) + limit;
643  }
644  }
645  s->s.h.filter.sharpness = sharp;
646  if ((s->s.h.lf_delta.enabled = get_bits1(&s->gb))) {
647  if ((s->s.h.lf_delta.updated = get_bits1(&s->gb))) {
648  for (i = 0; i < 4; i++)
649  if (get_bits1(&s->gb))
650  s->s.h.lf_delta.ref[i] = get_sbits_inv(&s->gb, 6);
651  for (i = 0; i < 2; i++)
652  if (get_bits1(&s->gb))
653  s->s.h.lf_delta.mode[i] = get_sbits_inv(&s->gb, 6);
654  }
655  }
656 
657  /* quantization header data */
658  s->s.h.yac_qi = get_bits(&s->gb, 8);
659  s->s.h.ydc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
660  s->s.h.uvdc_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
661  s->s.h.uvac_qdelta = get_bits1(&s->gb) ? get_sbits_inv(&s->gb, 4) : 0;
662  s->s.h.lossless = s->s.h.yac_qi == 0 && s->s.h.ydc_qdelta == 0 &&
663  s->s.h.uvdc_qdelta == 0 && s->s.h.uvac_qdelta == 0;
664  if (s->s.h.lossless)
666 
667  /* segmentation header info */
668  if ((s->s.h.segmentation.enabled = get_bits1(&s->gb))) {
669  if ((s->s.h.segmentation.update_map = get_bits1(&s->gb))) {
670  for (i = 0; i < 7; i++)
671  s->s.h.segmentation.prob[i] = get_bits1(&s->gb) ?
672  get_bits(&s->gb, 8) : 255;
673  if ((s->s.h.segmentation.temporal = get_bits1(&s->gb)))
674  for (i = 0; i < 3; i++)
675  s->s.h.segmentation.pred_prob[i] = get_bits1(&s->gb) ?
676  get_bits(&s->gb, 8) : 255;
677  }
678 
679  if (get_bits1(&s->gb)) {
681  for (i = 0; i < 8; i++) {
682  if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
683  s->s.h.segmentation.feat[i].q_val = get_sbits_inv(&s->gb, 8);
684  if ((s->s.h.segmentation.feat[i].lf_enabled = get_bits1(&s->gb)))
685  s->s.h.segmentation.feat[i].lf_val = get_sbits_inv(&s->gb, 6);
686  if ((s->s.h.segmentation.feat[i].ref_enabled = get_bits1(&s->gb)))
687  s->s.h.segmentation.feat[i].ref_val = get_bits(&s->gb, 2);
688  s->s.h.segmentation.feat[i].skip_enabled = get_bits1(&s->gb);
689  }
690  }
691  }
692 
693  // set qmul[] based on Y/UV, AC/DC and segmentation Q idx deltas
694  for (i = 0; i < (s->s.h.segmentation.enabled ? 8 : 1); i++) {
695  int qyac, qydc, quvac, quvdc, lflvl, sh;
696 
697  if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].q_enabled) {
698  if (s->s.h.segmentation.absolute_vals)
699  qyac = av_clip_uintp2(s->s.h.segmentation.feat[i].q_val, 8);
700  else
701  qyac = av_clip_uintp2(s->s.h.yac_qi + s->s.h.segmentation.feat[i].q_val, 8);
702  } else {
703  qyac = s->s.h.yac_qi;
704  }
705  qydc = av_clip_uintp2(qyac + s->s.h.ydc_qdelta, 8);
706  quvdc = av_clip_uintp2(qyac + s->s.h.uvdc_qdelta, 8);
707  quvac = av_clip_uintp2(qyac + s->s.h.uvac_qdelta, 8);
708  qyac = av_clip_uintp2(qyac, 8);
709 
710  s->s.h.segmentation.feat[i].qmul[0][0] = ff_vp9_dc_qlookup[s->bpp_index][qydc];
711  s->s.h.segmentation.feat[i].qmul[0][1] = ff_vp9_ac_qlookup[s->bpp_index][qyac];
712  s->s.h.segmentation.feat[i].qmul[1][0] = ff_vp9_dc_qlookup[s->bpp_index][quvdc];
713  s->s.h.segmentation.feat[i].qmul[1][1] = ff_vp9_ac_qlookup[s->bpp_index][quvac];
714 
715  sh = s->s.h.filter.level >= 32;
716  if (s->s.h.segmentation.enabled && s->s.h.segmentation.feat[i].lf_enabled) {
717  if (s->s.h.segmentation.absolute_vals)
718  lflvl = av_clip_uintp2(s->s.h.segmentation.feat[i].lf_val, 6);
719  else
720  lflvl = av_clip_uintp2(s->s.h.filter.level + s->s.h.segmentation.feat[i].lf_val, 6);
721  } else {
722  lflvl = s->s.h.filter.level;
723  }
724  if (s->s.h.lf_delta.enabled) {
725  s->s.h.segmentation.feat[i].lflvl[0][0] =
726  s->s.h.segmentation.feat[i].lflvl[0][1] =
727  av_clip_uintp2(lflvl + (s->s.h.lf_delta.ref[0] * (1 << sh)), 6);
728  for (j = 1; j < 4; j++) {
729  s->s.h.segmentation.feat[i].lflvl[j][0] =
730  av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
731  s->s.h.lf_delta.mode[0]) * (1 << sh)), 6);
732  s->s.h.segmentation.feat[i].lflvl[j][1] =
733  av_clip_uintp2(lflvl + ((s->s.h.lf_delta.ref[j] +
734  s->s.h.lf_delta.mode[1]) * (1 << sh)), 6);
735  }
736  } else {
737  memset(s->s.h.segmentation.feat[i].lflvl, lflvl,
738  sizeof(s->s.h.segmentation.feat[i].lflvl));
739  }
740  }
741 
742  /* tiling info */
743  if ((ret = update_size(avctx, w, h)) < 0) {
744  av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder for %dx%d @ %d\n",
745  w, h, s->pix_fmt);
746  return ret;
747  }
748  for (s->s.h.tiling.log2_tile_cols = 0;
749  s->sb_cols > (64 << s->s.h.tiling.log2_tile_cols);
750  s->s.h.tiling.log2_tile_cols++) ;
751  for (max = 0; (s->sb_cols >> max) >= 4; max++) ;
752  max = FFMAX(0, max - 1);
753  while (max > s->s.h.tiling.log2_tile_cols) {
754  if (get_bits1(&s->gb))
755  s->s.h.tiling.log2_tile_cols++;
756  else
757  break;
758  }
759  s->s.h.tiling.log2_tile_rows = decode012(&s->gb);
760  s->s.h.tiling.tile_rows = 1 << s->s.h.tiling.log2_tile_rows;
761  if (s->s.h.tiling.tile_cols != (1 << s->s.h.tiling.log2_tile_cols)) {
762  int n_range_coders;
763  VP56RangeCoder *rc;
764 
765  if (s->td) {
766  for (i = 0; i < s->active_tile_cols; i++) {
767  av_free(s->td[i].b_base);
768  av_free(s->td[i].block_base);
769  }
770  av_free(s->td);
771  }
772 
773  s->s.h.tiling.tile_cols = 1 << s->s.h.tiling.log2_tile_cols;
774  vp9_free_entries(avctx);
776  s->s.h.tiling.tile_cols : 1;
777  vp9_alloc_entries(avctx, s->sb_rows);
778  if (avctx->active_thread_type == FF_THREAD_SLICE) {
779  n_range_coders = 4; // max_tile_rows
780  } else {
781  n_range_coders = s->s.h.tiling.tile_cols;
782  }
784  n_range_coders * sizeof(VP56RangeCoder));
785  if (!s->td)
786  return AVERROR(ENOMEM);
787  rc = (VP56RangeCoder *) &s->td[s->active_tile_cols];
788  for (i = 0; i < s->active_tile_cols; i++) {
789  s->td[i].s = s;
790  s->td[i].c_b = rc;
791  rc += n_range_coders;
792  }
793  }
794 
795  /* check reference frames */
796  if (!s->s.h.keyframe && !s->s.h.intraonly) {
797  for (i = 0; i < 3; i++) {
798  AVFrame *ref = s->s.refs[s->s.h.refidx[i]].f;
799  int refw = ref->width, refh = ref->height;
800 
801  if (ref->format != avctx->pix_fmt) {
802  av_log(avctx, AV_LOG_ERROR,
803  "Ref pixfmt (%s) did not match current frame (%s)",
805  av_get_pix_fmt_name(avctx->pix_fmt));
806  return AVERROR_INVALIDDATA;
807  } else if (refw == w && refh == h) {
808  s->mvscale[i][0] = s->mvscale[i][1] = 0;
809  } else {
810  if (w * 2 < refw || h * 2 < refh || w > 16 * refw || h > 16 * refh) {
811  av_log(avctx, AV_LOG_ERROR,
812  "Invalid ref frame dimensions %dx%d for frame size %dx%d\n",
813  refw, refh, w, h);
814  return AVERROR_INVALIDDATA;
815  }
816  s->mvscale[i][0] = (refw << 14) / w;
817  s->mvscale[i][1] = (refh << 14) / h;
818  s->mvstep[i][0] = 16 * s->mvscale[i][0] >> 14;
819  s->mvstep[i][1] = 16 * s->mvscale[i][1] >> 14;
820  }
821  }
822  }
823 
824  if (s->s.h.keyframe || s->s.h.errorres || (s->s.h.intraonly && s->s.h.resetctx == 3)) {
825  s->prob_ctx[0].p = s->prob_ctx[1].p = s->prob_ctx[2].p =
828  sizeof(ff_vp9_default_coef_probs));
830  sizeof(ff_vp9_default_coef_probs));
832  sizeof(ff_vp9_default_coef_probs));
834  sizeof(ff_vp9_default_coef_probs));
835  } else if (s->s.h.intraonly && s->s.h.resetctx == 2) {
838  sizeof(ff_vp9_default_coef_probs));
839  }
840 
841  // next 16 bits is size of the rest of the header (arith-coded)
842  s->s.h.compressed_header_size = size2 = get_bits(&s->gb, 16);
843  s->s.h.uncompressed_header_size = (get_bits_count(&s->gb) + 7) / 8;
844 
845  data2 = align_get_bits(&s->gb);
846  if (size2 > size - (data2 - data)) {
847  av_log(avctx, AV_LOG_ERROR, "Invalid compressed header size\n");
848  return AVERROR_INVALIDDATA;
849  }
850  ret = ff_vp56_init_range_decoder(&s->c, data2, size2);
851  if (ret < 0)
852  return ret;
853 
854  if (vp56_rac_get_prob_branchy(&s->c, 128)) { // marker bit
855  av_log(avctx, AV_LOG_ERROR, "Marker bit was set\n");
856  return AVERROR_INVALIDDATA;
857  }
858 
859  for (i = 0; i < s->active_tile_cols; i++) {
860  if (s->s.h.keyframe || s->s.h.intraonly) {
861  memset(s->td[i].counts.coef, 0, sizeof(s->td[0].counts.coef));
862  memset(s->td[i].counts.eob, 0, sizeof(s->td[0].counts.eob));
863  } else {
864  memset(&s->td[i].counts, 0, sizeof(s->td[0].counts));
865  }
866  }
867 
868  /* FIXME is it faster to not copy here, but do it down in the fw updates
869  * as explicit copies if the fw update is missing (and skip the copy upon
870  * fw update)? */
871  s->prob.p = s->prob_ctx[c].p;
872 
873  // txfm updates
874  if (s->s.h.lossless) {
875  s->s.h.txfmmode = TX_4X4;
876  } else {
877  s->s.h.txfmmode = vp8_rac_get_uint(&s->c, 2);
878  if (s->s.h.txfmmode == 3)
879  s->s.h.txfmmode += vp8_rac_get(&s->c);
880 
881  if (s->s.h.txfmmode == TX_SWITCHABLE) {
882  for (i = 0; i < 2; i++)
883  if (vp56_rac_get_prob_branchy(&s->c, 252))
884  s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
885  for (i = 0; i < 2; i++)
886  for (j = 0; j < 2; j++)
887  if (vp56_rac_get_prob_branchy(&s->c, 252))
888  s->prob.p.tx16p[i][j] =
889  update_prob(&s->c, s->prob.p.tx16p[i][j]);
890  for (i = 0; i < 2; i++)
891  for (j = 0; j < 3; j++)
892  if (vp56_rac_get_prob_branchy(&s->c, 252))
893  s->prob.p.tx32p[i][j] =
894  update_prob(&s->c, s->prob.p.tx32p[i][j]);
895  }
896  }
897 
898  // coef updates
899  for (i = 0; i < 4; i++) {
900  uint8_t (*ref)[2][6][6][3] = s->prob_ctx[c].coef[i];
901  if (vp8_rac_get(&s->c)) {
902  for (j = 0; j < 2; j++)
903  for (k = 0; k < 2; k++)
904  for (l = 0; l < 6; l++)
905  for (m = 0; m < 6; m++) {
906  uint8_t *p = s->prob.coef[i][j][k][l][m];
907  uint8_t *r = ref[j][k][l][m];
908  if (m >= 3 && l == 0) // dc only has 3 pt
909  break;
910  for (n = 0; n < 3; n++) {
911  if (vp56_rac_get_prob_branchy(&s->c, 252))
912  p[n] = update_prob(&s->c, r[n]);
913  else
914  p[n] = r[n];
915  }
916  memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
917  }
918  } else {
919  for (j = 0; j < 2; j++)
920  for (k = 0; k < 2; k++)
921  for (l = 0; l < 6; l++)
922  for (m = 0; m < 6; m++) {
923  uint8_t *p = s->prob.coef[i][j][k][l][m];
924  uint8_t *r = ref[j][k][l][m];
925  if (m > 3 && l == 0) // dc only has 3 pt
926  break;
927  memcpy(p, r, 3);
928  memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
929  }
930  }
931  if (s->s.h.txfmmode == i)
932  break;
933  }
934 
935  // mode updates
936  for (i = 0; i < 3; i++)
937  if (vp56_rac_get_prob_branchy(&s->c, 252))
938  s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
939  if (!s->s.h.keyframe && !s->s.h.intraonly) {
940  for (i = 0; i < 7; i++)
941  for (j = 0; j < 3; j++)
942  if (vp56_rac_get_prob_branchy(&s->c, 252))
943  s->prob.p.mv_mode[i][j] =
944  update_prob(&s->c, s->prob.p.mv_mode[i][j]);
945 
946  if (s->s.h.filtermode == FILTER_SWITCHABLE)
947  for (i = 0; i < 4; i++)
948  for (j = 0; j < 2; j++)
949  if (vp56_rac_get_prob_branchy(&s->c, 252))
950  s->prob.p.filter[i][j] =
951  update_prob(&s->c, s->prob.p.filter[i][j]);
952 
953  for (i = 0; i < 4; i++)
954  if (vp56_rac_get_prob_branchy(&s->c, 252))
955  s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
956 
957  if (s->s.h.allowcompinter) {
958  s->s.h.comppredmode = vp8_rac_get(&s->c);
959  if (s->s.h.comppredmode)
960  s->s.h.comppredmode += vp8_rac_get(&s->c);
961  if (s->s.h.comppredmode == PRED_SWITCHABLE)
962  for (i = 0; i < 5; i++)
963  if (vp56_rac_get_prob_branchy(&s->c, 252))
964  s->prob.p.comp[i] =
965  update_prob(&s->c, s->prob.p.comp[i]);
966  } else {
968  }
969 
970  if (s->s.h.comppredmode != PRED_COMPREF) {
971  for (i = 0; i < 5; i++) {
972  if (vp56_rac_get_prob_branchy(&s->c, 252))
973  s->prob.p.single_ref[i][0] =
974  update_prob(&s->c, s->prob.p.single_ref[i][0]);
975  if (vp56_rac_get_prob_branchy(&s->c, 252))
976  s->prob.p.single_ref[i][1] =
977  update_prob(&s->c, s->prob.p.single_ref[i][1]);
978  }
979  }
980 
981  if (s->s.h.comppredmode != PRED_SINGLEREF) {
982  for (i = 0; i < 5; i++)
983  if (vp56_rac_get_prob_branchy(&s->c, 252))
984  s->prob.p.comp_ref[i] =
985  update_prob(&s->c, s->prob.p.comp_ref[i]);
986  }
987 
988  for (i = 0; i < 4; i++)
989  for (j = 0; j < 9; j++)
990  if (vp56_rac_get_prob_branchy(&s->c, 252))
991  s->prob.p.y_mode[i][j] =
992  update_prob(&s->c, s->prob.p.y_mode[i][j]);
993 
994  for (i = 0; i < 4; i++)
995  for (j = 0; j < 4; j++)
996  for (k = 0; k < 3; k++)
997  if (vp56_rac_get_prob_branchy(&s->c, 252))
998  s->prob.p.partition[3 - i][j][k] =
999  update_prob(&s->c,
1000  s->prob.p.partition[3 - i][j][k]);
1001 
1002  // mv fields don't use the update_prob subexp model for some reason
1003  for (i = 0; i < 3; i++)
1004  if (vp56_rac_get_prob_branchy(&s->c, 252))
1005  s->prob.p.mv_joint[i] = (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1006 
1007  for (i = 0; i < 2; i++) {
1008  if (vp56_rac_get_prob_branchy(&s->c, 252))
1009  s->prob.p.mv_comp[i].sign =
1010  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1011 
1012  for (j = 0; j < 10; j++)
1013  if (vp56_rac_get_prob_branchy(&s->c, 252))
1014  s->prob.p.mv_comp[i].classes[j] =
1015  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1016 
1017  if (vp56_rac_get_prob_branchy(&s->c, 252))
1018  s->prob.p.mv_comp[i].class0 =
1019  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1020 
1021  for (j = 0; j < 10; j++)
1022  if (vp56_rac_get_prob_branchy(&s->c, 252))
1023  s->prob.p.mv_comp[i].bits[j] =
1024  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1025  }
1026 
1027  for (i = 0; i < 2; i++) {
1028  for (j = 0; j < 2; j++)
1029  for (k = 0; k < 3; k++)
1030  if (vp56_rac_get_prob_branchy(&s->c, 252))
1031  s->prob.p.mv_comp[i].class0_fp[j][k] =
1032  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1033 
1034  for (j = 0; j < 3; j++)
1035  if (vp56_rac_get_prob_branchy(&s->c, 252))
1036  s->prob.p.mv_comp[i].fp[j] =
1037  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1038  }
1039 
1040  if (s->s.h.highprecisionmvs) {
1041  for (i = 0; i < 2; i++) {
1042  if (vp56_rac_get_prob_branchy(&s->c, 252))
1043  s->prob.p.mv_comp[i].class0_hp =
1044  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1045 
1046  if (vp56_rac_get_prob_branchy(&s->c, 252))
1047  s->prob.p.mv_comp[i].hp =
1048  (vp8_rac_get_uint(&s->c, 7) << 1) | 1;
1049  }
1050  }
1051  }
1052 
1053  return (data2 - data) + size2;
1054 }
1055 
1056 static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1057  ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1058 {
1059  const VP9Context *s = td->s;
1060  int c = ((s->above_partition_ctx[col] >> (3 - bl)) & 1) |
1061  (((td->left_partition_ctx[row & 0x7] >> (3 - bl)) & 1) << 1);
1062  const uint8_t *p = s->s.h.keyframe || s->s.h.intraonly ? ff_vp9_default_kf_partition_probs[bl][c] :
1063  s->prob.p.partition[bl][c];
1064  enum BlockPartition bp;
1065  ptrdiff_t hbs = 4 >> bl;
1066  AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1067  ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1068  int bytesperpixel = s->bytesperpixel;
1069 
1070  if (bl == BL_8X8) {
1072  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1073  } else if (col + hbs < s->cols) { // FIXME why not <=?
1074  if (row + hbs < s->rows) { // FIXME why not <=?
1076  switch (bp) {
1077  case PARTITION_NONE:
1078  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1079  break;
1080  case PARTITION_H:
1081  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1082  yoff += hbs * 8 * y_stride;
1083  uvoff += hbs * 8 * uv_stride >> s->ss_v;
1084  ff_vp9_decode_block(td, row + hbs, col, lflvl, yoff, uvoff, bl, bp);
1085  break;
1086  case PARTITION_V:
1087  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1088  yoff += hbs * 8 * bytesperpixel;
1089  uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1090  ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, bl, bp);
1091  break;
1092  case PARTITION_SPLIT:
1093  decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1094  decode_sb(td, row, col + hbs, lflvl,
1095  yoff + 8 * hbs * bytesperpixel,
1096  uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1097  yoff += hbs * 8 * y_stride;
1098  uvoff += hbs * 8 * uv_stride >> s->ss_v;
1099  decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1100  decode_sb(td, row + hbs, col + hbs, lflvl,
1101  yoff + 8 * hbs * bytesperpixel,
1102  uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1103  break;
1104  default:
1105  av_assert0(0);
1106  }
1107  } else if (vp56_rac_get_prob_branchy(td->c, p[1])) {
1108  bp = PARTITION_SPLIT;
1109  decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1110  decode_sb(td, row, col + hbs, lflvl,
1111  yoff + 8 * hbs * bytesperpixel,
1112  uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1113  } else {
1114  bp = PARTITION_H;
1115  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1116  }
1117  } else if (row + hbs < s->rows) { // FIXME why not <=?
1118  if (vp56_rac_get_prob_branchy(td->c, p[2])) {
1119  bp = PARTITION_SPLIT;
1120  decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1121  yoff += hbs * 8 * y_stride;
1122  uvoff += hbs * 8 * uv_stride >> s->ss_v;
1123  decode_sb(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1124  } else {
1125  bp = PARTITION_V;
1126  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, bl, bp);
1127  }
1128  } else {
1129  bp = PARTITION_SPLIT;
1130  decode_sb(td, row, col, lflvl, yoff, uvoff, bl + 1);
1131  }
1132  td->counts.partition[bl][c][bp]++;
1133 }
1134 
1135 static void decode_sb_mem(VP9TileData *td, int row, int col, VP9Filter *lflvl,
1136  ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
1137 {
1138  const VP9Context *s = td->s;
1139  VP9Block *b = td->b;
1140  ptrdiff_t hbs = 4 >> bl;
1141  AVFrame *f = s->s.frames[CUR_FRAME].tf.f;
1142  ptrdiff_t y_stride = f->linesize[0], uv_stride = f->linesize[1];
1143  int bytesperpixel = s->bytesperpixel;
1144 
1145  if (bl == BL_8X8) {
1146  av_assert2(b->bl == BL_8X8);
1147  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1148  } else if (td->b->bl == bl) {
1149  ff_vp9_decode_block(td, row, col, lflvl, yoff, uvoff, b->bl, b->bp);
1150  if (b->bp == PARTITION_H && row + hbs < s->rows) {
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, b->bl, b->bp);
1154  } else if (b->bp == PARTITION_V && col + hbs < s->cols) {
1155  yoff += hbs * 8 * bytesperpixel;
1156  uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1157  ff_vp9_decode_block(td, row, col + hbs, lflvl, yoff, uvoff, b->bl, b->bp);
1158  }
1159  } else {
1160  decode_sb_mem(td, row, col, lflvl, yoff, uvoff, bl + 1);
1161  if (col + hbs < s->cols) { // FIXME why not <=?
1162  if (row + hbs < s->rows) {
1163  decode_sb_mem(td, row, col + hbs, lflvl, yoff + 8 * hbs * bytesperpixel,
1164  uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1165  yoff += hbs * 8 * y_stride;
1166  uvoff += hbs * 8 * uv_stride >> s->ss_v;
1167  decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1168  decode_sb_mem(td, row + hbs, col + hbs, lflvl,
1169  yoff + 8 * hbs * bytesperpixel,
1170  uvoff + (8 * hbs * bytesperpixel >> s->ss_h), bl + 1);
1171  } else {
1172  yoff += hbs * 8 * bytesperpixel;
1173  uvoff += hbs * 8 * bytesperpixel >> s->ss_h;
1174  decode_sb_mem(td, row, col + hbs, lflvl, yoff, uvoff, bl + 1);
1175  }
1176  } else if (row + hbs < s->rows) {
1177  yoff += hbs * 8 * y_stride;
1178  uvoff += hbs * 8 * uv_stride >> s->ss_v;
1179  decode_sb_mem(td, row + hbs, col, lflvl, yoff, uvoff, bl + 1);
1180  }
1181  }
1182 }
1183 
1184 static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
1185 {
1186  int sb_start = ( idx * n) >> log2_n;
1187  int sb_end = ((idx + 1) * n) >> log2_n;
1188  *start = FFMIN(sb_start, n) << 3;
1189  *end = FFMIN(sb_end, n) << 3;
1190 }
1191 
1192 static void free_buffers(VP9Context *s)
1193 {
1194  int i;
1195 
1196  av_freep(&s->intra_pred_data[0]);
1197  for (i = 0; i < s->active_tile_cols; i++) {
1198  av_freep(&s->td[i].b_base);
1199  av_freep(&s->td[i].block_base);
1200  }
1201 }
1202 
1204 {
1205  VP9Context *s = avctx->priv_data;
1206  int i;
1207 
1208  for (i = 0; i < 3; i++) {
1209  if (s->s.frames[i].tf.f->buf[0])
1210  vp9_frame_unref(avctx, &s->s.frames[i]);
1211  av_frame_free(&s->s.frames[i].tf.f);
1212  }
1213  for (i = 0; i < 8; i++) {
1214  if (s->s.refs[i].f->buf[0])
1215  ff_thread_release_buffer(avctx, &s->s.refs[i]);
1216  av_frame_free(&s->s.refs[i].f);
1217  if (s->next_refs[i].f->buf[0])
1218  ff_thread_release_buffer(avctx, &s->next_refs[i]);
1219  av_frame_free(&s->next_refs[i].f);
1220  }
1221 
1222  free_buffers(s);
1223  vp9_free_entries(avctx);
1224  av_freep(&s->td);
1225  return 0;
1226 }
1227 
1228 static int decode_tiles(AVCodecContext *avctx,
1229  const uint8_t *data, int size)
1230 {
1231  VP9Context *s = avctx->priv_data;
1232  VP9TileData *td = &s->td[0];
1233  int row, col, tile_row, tile_col, ret;
1234  int bytesperpixel;
1235  int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1236  AVFrame *f;
1237  ptrdiff_t yoff, uvoff, ls_y, ls_uv;
1238 
1239  f = s->s.frames[CUR_FRAME].tf.f;
1240  ls_y = f->linesize[0];
1241  ls_uv =f->linesize[1];
1242  bytesperpixel = s->bytesperpixel;
1243 
1244  yoff = uvoff = 0;
1245  for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1246  set_tile_offset(&tile_row_start, &tile_row_end,
1247  tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1248 
1249  for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1250  int64_t tile_size;
1251 
1252  if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1253  tile_row == s->s.h.tiling.tile_rows - 1) {
1254  tile_size = size;
1255  } else {
1256  tile_size = AV_RB32(data);
1257  data += 4;
1258  size -= 4;
1259  }
1260  if (tile_size > size) {
1261  ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1262  return AVERROR_INVALIDDATA;
1263  }
1264  ret = ff_vp56_init_range_decoder(&td->c_b[tile_col], data, tile_size);
1265  if (ret < 0)
1266  return ret;
1267  if (vp56_rac_get_prob_branchy(&td->c_b[tile_col], 128)) { // marker bit
1268  ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1269  return AVERROR_INVALIDDATA;
1270  }
1271  data += tile_size;
1272  size -= tile_size;
1273  }
1274 
1275  for (row = tile_row_start; row < tile_row_end;
1276  row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1277  VP9Filter *lflvl_ptr = s->lflvl;
1278  ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1279 
1280  for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1281  set_tile_offset(&tile_col_start, &tile_col_end,
1282  tile_col, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1283  td->tile_col_start = tile_col_start;
1284  if (s->pass != 2) {
1285  memset(td->left_partition_ctx, 0, 8);
1286  memset(td->left_skip_ctx, 0, 8);
1287  if (s->s.h.keyframe || s->s.h.intraonly) {
1288  memset(td->left_mode_ctx, DC_PRED, 16);
1289  } else {
1290  memset(td->left_mode_ctx, NEARESTMV, 8);
1291  }
1292  memset(td->left_y_nnz_ctx, 0, 16);
1293  memset(td->left_uv_nnz_ctx, 0, 32);
1294  memset(td->left_segpred_ctx, 0, 8);
1295 
1296  td->c = &td->c_b[tile_col];
1297  }
1298 
1299  for (col = tile_col_start;
1300  col < tile_col_end;
1301  col += 8, yoff2 += 64 * bytesperpixel,
1302  uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1303  // FIXME integrate with lf code (i.e. zero after each
1304  // use, similar to invtxfm coefficients, or similar)
1305  if (s->pass != 1) {
1306  memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1307  }
1308 
1309  if (s->pass == 2) {
1310  decode_sb_mem(td, row, col, lflvl_ptr,
1311  yoff2, uvoff2, BL_64X64);
1312  } else {
1313  if (vpX_rac_is_end(td->c)) {
1314  return AVERROR_INVALIDDATA;
1315  }
1316  decode_sb(td, row, col, lflvl_ptr,
1317  yoff2, uvoff2, BL_64X64);
1318  }
1319  }
1320  }
1321 
1322  if (s->pass == 1)
1323  continue;
1324 
1325  // backup pre-loopfilter reconstruction data for intra
1326  // prediction of next row of sb64s
1327  if (row + 8 < s->rows) {
1328  memcpy(s->intra_pred_data[0],
1329  f->data[0] + yoff + 63 * ls_y,
1330  8 * s->cols * bytesperpixel);
1331  memcpy(s->intra_pred_data[1],
1332  f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1333  8 * s->cols * bytesperpixel >> s->ss_h);
1334  memcpy(s->intra_pred_data[2],
1335  f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1336  8 * s->cols * bytesperpixel >> s->ss_h);
1337  }
1338 
1339  // loopfilter one row
1340  if (s->s.h.filter.level) {
1341  yoff2 = yoff;
1342  uvoff2 = uvoff;
1343  lflvl_ptr = s->lflvl;
1344  for (col = 0; col < s->cols;
1345  col += 8, yoff2 += 64 * bytesperpixel,
1346  uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1347  ff_vp9_loopfilter_sb(avctx, lflvl_ptr, row, col,
1348  yoff2, uvoff2);
1349  }
1350  }
1351 
1352  // FIXME maybe we can make this more finegrained by running the
1353  // loopfilter per-block instead of after each sbrow
1354  // In fact that would also make intra pred left preparation easier?
1355  ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, row >> 3, 0);
1356  }
1357  }
1358  return 0;
1359 }
1360 
1361 #if HAVE_THREADS
1362 static av_always_inline
1363 int decode_tiles_mt(AVCodecContext *avctx, void *tdata, int jobnr,
1364  int threadnr)
1365 {
1366  VP9Context *s = avctx->priv_data;
1367  VP9TileData *td = &s->td[jobnr];
1368  ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1369  int bytesperpixel = s->bytesperpixel, row, col, tile_row;
1370  unsigned tile_cols_len;
1371  int tile_row_start, tile_row_end, tile_col_start, tile_col_end;
1372  VP9Filter *lflvl_ptr_base;
1373  AVFrame *f;
1374 
1375  f = s->s.frames[CUR_FRAME].tf.f;
1376  ls_y = f->linesize[0];
1377  ls_uv =f->linesize[1];
1378 
1379  set_tile_offset(&tile_col_start, &tile_col_end,
1380  jobnr, s->s.h.tiling.log2_tile_cols, s->sb_cols);
1381  td->tile_col_start = tile_col_start;
1382  uvoff = (64 * bytesperpixel >> s->ss_h)*(tile_col_start >> 3);
1383  yoff = (64 * bytesperpixel)*(tile_col_start >> 3);
1384  lflvl_ptr_base = s->lflvl+(tile_col_start >> 3);
1385 
1386  for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1387  set_tile_offset(&tile_row_start, &tile_row_end,
1388  tile_row, s->s.h.tiling.log2_tile_rows, s->sb_rows);
1389 
1390  td->c = &td->c_b[tile_row];
1391  for (row = tile_row_start; row < tile_row_end;
1392  row += 8, yoff += ls_y * 64, uvoff += ls_uv * 64 >> s->ss_v) {
1393  ptrdiff_t yoff2 = yoff, uvoff2 = uvoff;
1394  VP9Filter *lflvl_ptr = lflvl_ptr_base+s->sb_cols*(row >> 3);
1395 
1396  memset(td->left_partition_ctx, 0, 8);
1397  memset(td->left_skip_ctx, 0, 8);
1398  if (s->s.h.keyframe || s->s.h.intraonly) {
1399  memset(td->left_mode_ctx, DC_PRED, 16);
1400  } else {
1401  memset(td->left_mode_ctx, NEARESTMV, 8);
1402  }
1403  memset(td->left_y_nnz_ctx, 0, 16);
1404  memset(td->left_uv_nnz_ctx, 0, 32);
1405  memset(td->left_segpred_ctx, 0, 8);
1406 
1407  for (col = tile_col_start;
1408  col < tile_col_end;
1409  col += 8, yoff2 += 64 * bytesperpixel,
1410  uvoff2 += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1411  // FIXME integrate with lf code (i.e. zero after each
1412  // use, similar to invtxfm coefficients, or similar)
1413  memset(lflvl_ptr->mask, 0, sizeof(lflvl_ptr->mask));
1414  decode_sb(td, row, col, lflvl_ptr,
1415  yoff2, uvoff2, BL_64X64);
1416  }
1417 
1418  // backup pre-loopfilter reconstruction data for intra
1419  // prediction of next row of sb64s
1420  tile_cols_len = tile_col_end - tile_col_start;
1421  if (row + 8 < s->rows) {
1422  memcpy(s->intra_pred_data[0] + (tile_col_start * 8 * bytesperpixel),
1423  f->data[0] + yoff + 63 * ls_y,
1424  8 * tile_cols_len * bytesperpixel);
1425  memcpy(s->intra_pred_data[1] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1426  f->data[1] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1427  8 * tile_cols_len * bytesperpixel >> s->ss_h);
1428  memcpy(s->intra_pred_data[2] + (tile_col_start * 8 * bytesperpixel >> s->ss_h),
1429  f->data[2] + uvoff + ((64 >> s->ss_v) - 1) * ls_uv,
1430  8 * tile_cols_len * bytesperpixel >> s->ss_h);
1431  }
1432 
1433  vp9_report_tile_progress(s, row >> 3, 1);
1434  }
1435  }
1436  return 0;
1437 }
1438 
1439 static av_always_inline
1440 int loopfilter_proc(AVCodecContext *avctx)
1441 {
1442  VP9Context *s = avctx->priv_data;
1443  ptrdiff_t uvoff, yoff, ls_y, ls_uv;
1444  VP9Filter *lflvl_ptr;
1445  int bytesperpixel = s->bytesperpixel, col, i;
1446  AVFrame *f;
1447 
1448  f = s->s.frames[CUR_FRAME].tf.f;
1449  ls_y = f->linesize[0];
1450  ls_uv =f->linesize[1];
1451 
1452  for (i = 0; i < s->sb_rows; i++) {
1453  vp9_await_tile_progress(s, i, s->s.h.tiling.tile_cols);
1454 
1455  if (s->s.h.filter.level) {
1456  yoff = (ls_y * 64)*i;
1457  uvoff = (ls_uv * 64 >> s->ss_v)*i;
1458  lflvl_ptr = s->lflvl+s->sb_cols*i;
1459  for (col = 0; col < s->cols;
1460  col += 8, yoff += 64 * bytesperpixel,
1461  uvoff += 64 * bytesperpixel >> s->ss_h, lflvl_ptr++) {
1462  ff_vp9_loopfilter_sb(avctx, lflvl_ptr, i << 3, col,
1463  yoff, uvoff);
1464  }
1465  }
1466  }
1467  return 0;
1468 }
1469 #endif
1470 
1471 static int vp9_decode_frame(AVCodecContext *avctx, void *frame,
1472  int *got_frame, AVPacket *pkt)
1473 {
1474  const uint8_t *data = pkt->data;
1475  int size = pkt->size;
1476  VP9Context *s = avctx->priv_data;
1477  int ret, i, j, ref;
1478  int retain_segmap_ref = s->s.frames[REF_FRAME_SEGMAP].segmentation_map &&
1480  AVFrame *f;
1481 
1482  if ((ret = decode_frame_header(avctx, data, size, &ref)) < 0) {
1483  return ret;
1484  } else if (ret == 0) {
1485  if (!s->s.refs[ref].f->buf[0]) {
1486  av_log(avctx, AV_LOG_ERROR, "Requested reference %d not available\n", ref);
1487  return AVERROR_INVALIDDATA;
1488  }
1489  if ((ret = av_frame_ref(frame, s->s.refs[ref].f)) < 0)
1490  return ret;
1491  ((AVFrame *)frame)->pts = pkt->pts;
1492 #if FF_API_PKT_PTS
1494  ((AVFrame *)frame)->pkt_pts = pkt->pts;
1496 #endif
1497  ((AVFrame *)frame)->pkt_dts = pkt->dts;
1498  for (i = 0; i < 8; i++) {
1499  if (s->next_refs[i].f->buf[0])
1500  ff_thread_release_buffer(avctx, &s->next_refs[i]);
1501  if (s->s.refs[i].f->buf[0] &&
1502  (ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i])) < 0)
1503  return ret;
1504  }
1505  *got_frame = 1;
1506  return pkt->size;
1507  }
1508  data += ret;
1509  size -= ret;
1510 
1511  if (!retain_segmap_ref || s->s.h.keyframe || s->s.h.intraonly) {
1512  if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0])
1514  if (!s->s.h.keyframe && !s->s.h.intraonly && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
1515  (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_SEGMAP], &s->s.frames[CUR_FRAME])) < 0)
1516  return ret;
1517  }
1518  if (s->s.frames[REF_FRAME_MVPAIR].tf.f->buf[0])
1520  if (!s->s.h.intraonly && !s->s.h.keyframe && !s->s.h.errorres && s->s.frames[CUR_FRAME].tf.f->buf[0] &&
1521  (ret = vp9_frame_ref(avctx, &s->s.frames[REF_FRAME_MVPAIR], &s->s.frames[CUR_FRAME])) < 0)
1522  return ret;
1523  if (s->s.frames[CUR_FRAME].tf.f->buf[0])
1524  vp9_frame_unref(avctx, &s->s.frames[CUR_FRAME]);
1525  if ((ret = vp9_frame_alloc(avctx, &s->s.frames[CUR_FRAME])) < 0)
1526  return ret;
1527  f = s->s.frames[CUR_FRAME].tf.f;
1528  f->key_frame = s->s.h.keyframe;
1530 
1531  if (s->s.frames[REF_FRAME_SEGMAP].tf.f->buf[0] &&
1535  }
1536 
1537  // ref frame setup
1538  for (i = 0; i < 8; i++) {
1539  if (s->next_refs[i].f->buf[0])
1540  ff_thread_release_buffer(avctx, &s->next_refs[i]);
1541  if (s->s.h.refreshrefmask & (1 << i)) {
1542  ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.frames[CUR_FRAME].tf);
1543  } else if (s->s.refs[i].f->buf[0]) {
1544  ret = ff_thread_ref_frame(&s->next_refs[i], &s->s.refs[i]);
1545  }
1546  if (ret < 0)
1547  return ret;
1548  }
1549 
1550  if (avctx->hwaccel) {
1551  ret = avctx->hwaccel->start_frame(avctx, NULL, 0);
1552  if (ret < 0)
1553  return ret;
1554  ret = avctx->hwaccel->decode_slice(avctx, pkt->data, pkt->size);
1555  if (ret < 0)
1556  return ret;
1557  ret = avctx->hwaccel->end_frame(avctx);
1558  if (ret < 0)
1559  return ret;
1560  goto finish;
1561  }
1562 
1563  // main tile decode loop
1564  memset(s->above_partition_ctx, 0, s->cols);
1565  memset(s->above_skip_ctx, 0, s->cols);
1566  if (s->s.h.keyframe || s->s.h.intraonly) {
1567  memset(s->above_mode_ctx, DC_PRED, s->cols * 2);
1568  } else {
1569  memset(s->above_mode_ctx, NEARESTMV, s->cols);
1570  }
1571  memset(s->above_y_nnz_ctx, 0, s->sb_cols * 16);
1572  memset(s->above_uv_nnz_ctx[0], 0, s->sb_cols * 16 >> s->ss_h);
1573  memset(s->above_uv_nnz_ctx[1], 0, s->sb_cols * 16 >> s->ss_h);
1574  memset(s->above_segpred_ctx, 0, s->cols);
1575  s->pass = s->s.frames[CUR_FRAME].uses_2pass =
1577  if ((ret = update_block_buffers(avctx)) < 0) {
1578  av_log(avctx, AV_LOG_ERROR,
1579  "Failed to allocate block buffers\n");
1580  return ret;
1581  }
1582  if (s->s.h.refreshctx && s->s.h.parallelmode) {
1583  int j, k, l, m;
1584 
1585  for (i = 0; i < 4; i++) {
1586  for (j = 0; j < 2; j++)
1587  for (k = 0; k < 2; k++)
1588  for (l = 0; l < 6; l++)
1589  for (m = 0; m < 6; m++)
1590  memcpy(s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m],
1591  s->prob.coef[i][j][k][l][m], 3);
1592  if (s->s.h.txfmmode == i)
1593  break;
1594  }
1595  s->prob_ctx[s->s.h.framectxid].p = s->prob.p;
1596  ff_thread_finish_setup(avctx);
1597  } else if (!s->s.h.refreshctx) {
1598  ff_thread_finish_setup(avctx);
1599  }
1600 
1601 #if HAVE_THREADS
1602  if (avctx->active_thread_type & FF_THREAD_SLICE) {
1603  for (i = 0; i < s->sb_rows; i++)
1604  atomic_store(&s->entries[i], 0);
1605  }
1606 #endif
1607 
1608  do {
1609  for (i = 0; i < s->active_tile_cols; i++) {
1610  s->td[i].b = s->td[i].b_base;
1611  s->td[i].block = s->td[i].block_base;
1612  s->td[i].uvblock[0] = s->td[i].uvblock_base[0];
1613  s->td[i].uvblock[1] = s->td[i].uvblock_base[1];
1614  s->td[i].eob = s->td[i].eob_base;
1615  s->td[i].uveob[0] = s->td[i].uveob_base[0];
1616  s->td[i].uveob[1] = s->td[i].uveob_base[1];
1617  }
1618 
1619 #if HAVE_THREADS
1620  if (avctx->active_thread_type == FF_THREAD_SLICE) {
1621  int tile_row, tile_col;
1622 
1623  av_assert1(!s->pass);
1624 
1625  for (tile_row = 0; tile_row < s->s.h.tiling.tile_rows; tile_row++) {
1626  for (tile_col = 0; tile_col < s->s.h.tiling.tile_cols; tile_col++) {
1627  int64_t tile_size;
1628 
1629  if (tile_col == s->s.h.tiling.tile_cols - 1 &&
1630  tile_row == s->s.h.tiling.tile_rows - 1) {
1631  tile_size = size;
1632  } else {
1633  tile_size = AV_RB32(data);
1634  data += 4;
1635  size -= 4;
1636  }
1637  if (tile_size > size)
1638  return AVERROR_INVALIDDATA;
1639  ret = ff_vp56_init_range_decoder(&s->td[tile_col].c_b[tile_row], data, tile_size);
1640  if (ret < 0)
1641  return ret;
1642  if (vp56_rac_get_prob_branchy(&s->td[tile_col].c_b[tile_row], 128)) // marker bit
1643  return AVERROR_INVALIDDATA;
1644  data += tile_size;
1645  size -= tile_size;
1646  }
1647  }
1648 
1649  ff_slice_thread_execute_with_mainfunc(avctx, decode_tiles_mt, loopfilter_proc, s->td, NULL, s->s.h.tiling.tile_cols);
1650  } else
1651 #endif
1652  {
1653  ret = decode_tiles(avctx, data, size);
1654  if (ret < 0) {
1655  ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1656  return ret;
1657  }
1658  }
1659 
1660  // Sum all counts fields into td[0].counts for tile threading
1661  if (avctx->active_thread_type == FF_THREAD_SLICE)
1662  for (i = 1; i < s->s.h.tiling.tile_cols; i++)
1663  for (j = 0; j < sizeof(s->td[i].counts) / sizeof(unsigned); j++)
1664  ((unsigned *)&s->td[0].counts)[j] += ((unsigned *)&s->td[i].counts)[j];
1665 
1666  if (s->pass < 2 && s->s.h.refreshctx && !s->s.h.parallelmode) {
1667  ff_vp9_adapt_probs(s);
1668  ff_thread_finish_setup(avctx);
1669  }
1670  } while (s->pass++ == 1);
1671  ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0);
1672 
1673 finish:
1674  // ref frame setup
1675  for (i = 0; i < 8; i++) {
1676  if (s->s.refs[i].f->buf[0])
1677  ff_thread_release_buffer(avctx, &s->s.refs[i]);
1678  if (s->next_refs[i].f->buf[0] &&
1679  (ret = ff_thread_ref_frame(&s->s.refs[i], &s->next_refs[i])) < 0)
1680  return ret;
1681  }
1682 
1683  if (!s->s.h.invisible) {
1684  if ((ret = av_frame_ref(frame, s->s.frames[CUR_FRAME].tf.f)) < 0)
1685  return ret;
1686  *got_frame = 1;
1687  }
1688 
1689  return pkt->size;
1690 }
1691 
1693 {
1694  VP9Context *s = avctx->priv_data;
1695  int i;
1696 
1697  for (i = 0; i < 3; i++)
1698  vp9_frame_unref(avctx, &s->s.frames[i]);
1699  for (i = 0; i < 8; i++)
1700  ff_thread_release_buffer(avctx, &s->s.refs[i]);
1701 }
1702 
1703 static int init_frames(AVCodecContext *avctx)
1704 {
1705  VP9Context *s = avctx->priv_data;
1706  int i;
1707 
1708  for (i = 0; i < 3; i++) {
1709  s->s.frames[i].tf.f = av_frame_alloc();
1710  if (!s->s.frames[i].tf.f) {
1711  vp9_decode_free(avctx);
1712  av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
1713  return AVERROR(ENOMEM);
1714  }
1715  }
1716  for (i = 0; i < 8; i++) {
1717  s->s.refs[i].f = av_frame_alloc();
1718  s->next_refs[i].f = av_frame_alloc();
1719  if (!s->s.refs[i].f || !s->next_refs[i].f) {
1720  vp9_decode_free(avctx);
1721  av_log(avctx, AV_LOG_ERROR, "Failed to allocate frame buffer %d\n", i);
1722  return AVERROR(ENOMEM);
1723  }
1724  }
1725 
1726  return 0;
1727 }
1728 
1730 {
1731  VP9Context *s = avctx->priv_data;
1732 
1733  avctx->internal->allocate_progress = 1;
1734  s->last_bpp = 0;
1735  s->s.h.filter.sharpness = -1;
1736 
1737  return init_frames(avctx);
1738 }
1739 
1740 #if HAVE_THREADS
1741 static av_cold int vp9_decode_init_thread_copy(AVCodecContext *avctx)
1742 {
1743  return init_frames(avctx);
1744 }
1745 
1746 static int vp9_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1747 {
1748  int i, ret;
1749  VP9Context *s = dst->priv_data, *ssrc = src->priv_data;
1750 
1751  for (i = 0; i < 3; i++) {
1752  if (s->s.frames[i].tf.f->buf[0])
1753  vp9_frame_unref(dst, &s->s.frames[i]);
1754  if (ssrc->s.frames[i].tf.f->buf[0]) {
1755  if ((ret = vp9_frame_ref(dst, &s->s.frames[i], &ssrc->s.frames[i])) < 0)
1756  return ret;
1757  }
1758  }
1759  for (i = 0; i < 8; i++) {
1760  if (s->s.refs[i].f->buf[0])
1761  ff_thread_release_buffer(dst, &s->s.refs[i]);
1762  if (ssrc->next_refs[i].f->buf[0]) {
1763  if ((ret = ff_thread_ref_frame(&s->s.refs[i], &ssrc->next_refs[i])) < 0)
1764  return ret;
1765  }
1766  }
1767 
1768  s->s.h.invisible = ssrc->s.h.invisible;
1769  s->s.h.keyframe = ssrc->s.h.keyframe;
1770  s->s.h.intraonly = ssrc->s.h.intraonly;
1771  s->ss_v = ssrc->ss_v;
1772  s->ss_h = ssrc->ss_h;
1773  s->s.h.segmentation.enabled = ssrc->s.h.segmentation.enabled;
1774  s->s.h.segmentation.update_map = ssrc->s.h.segmentation.update_map;
1775  s->s.h.segmentation.absolute_vals = ssrc->s.h.segmentation.absolute_vals;
1776  s->bytesperpixel = ssrc->bytesperpixel;
1777  s->gf_fmt = ssrc->gf_fmt;
1778  s->w = ssrc->w;
1779  s->h = ssrc->h;
1780  s->s.h.bpp = ssrc->s.h.bpp;
1781  s->bpp_index = ssrc->bpp_index;
1782  s->pix_fmt = ssrc->pix_fmt;
1783  memcpy(&s->prob_ctx, &ssrc->prob_ctx, sizeof(s->prob_ctx));
1784  memcpy(&s->s.h.lf_delta, &ssrc->s.h.lf_delta, sizeof(s->s.h.lf_delta));
1785  memcpy(&s->s.h.segmentation.feat, &ssrc->s.h.segmentation.feat,
1786  sizeof(s->s.h.segmentation.feat));
1787 
1788  return 0;
1789 }
1790 #endif
1791 
1793  .name = "vp9",
1794  .long_name = NULL_IF_CONFIG_SMALL("Google VP9"),
1795  .type = AVMEDIA_TYPE_VIDEO,
1796  .id = AV_CODEC_ID_VP9,
1797  .priv_data_size = sizeof(VP9Context),
1798  .init = vp9_decode_init,
1799  .close = vp9_decode_free,
1802  .caps_internal = FF_CODEC_CAP_SLICE_THREAD_HAS_MF,
1804  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp9_decode_init_thread_copy),
1805  .update_thread_context = ONLY_IF_THREADS_ENABLED(vp9_decode_update_thread_context),
1807  .bsfs = "vp9_superframe_split",
1808  .hw_configs = (const AVCodecHWConfigInternal*[]) {
1809 #if CONFIG_VP9_DXVA2_HWACCEL
1810  HWACCEL_DXVA2(vp9),
1811 #endif
1812 #if CONFIG_VP9_D3D11VA_HWACCEL
1813  HWACCEL_D3D11VA(vp9),
1814 #endif
1815 #if CONFIG_VP9_D3D11VA2_HWACCEL
1816  HWACCEL_D3D11VA2(vp9),
1817 #endif
1818 #if CONFIG_VP9_NVDEC_HWACCEL
1819  HWACCEL_NVDEC(vp9),
1820 #endif
1821 #if CONFIG_VP9_VAAPI_HWACCEL
1822  HWACCEL_VAAPI(vp9),
1823 #endif
1824 #if CONFIG_VP9_VDPAU_HWACCEL
1825  HWACCEL_VDPAU(vp9),
1826 #endif
1827  NULL
1828  },
1829 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:499
ThreadFrame tf
Definition: vp9shared.h:60
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
#define NULL
Definition: coverity.c:32
uint8_t left_uv_nnz_ctx[2][16]
Definition: vp9dec.h:205
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t * segmentation_map
Definition: vp9shared.h:62
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:389
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:108
uint8_t parallelmode
Definition: vp9shared.h:108
const uint8_t ff_vp9_default_kf_partition_probs[4][4][3]
Definition: vp9data.c:41
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
static void vp9_decode_flush(AVCodecContext *avctx)
Definition: vp9.c:1692
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
static void set_tile_offset(int *start, int *end, int idx, int log2_n, int n)
Definition: vp9.c:1184
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
#define HWACCEL_D3D11VA2(codec)
Definition: hwaccel.h:69
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:166
#define atomic_store(object, desired)
Definition: stdatomic.h:85
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
uint8_t allowcompinter
Definition: vp9shared.h:106
static void flush(AVCodecContext *avctx)
uint8_t mblim_lut[64]
Definition: vp9dec.h:121
VP5 and VP6 compatible video decoder (common features)
uint8_t update_map
Definition: vp9shared.h:133
static av_always_inline int get_sbits_inv(GetBitContext *gb, int n)
Definition: vp9.c:342
#define HWACCEL_NVDEC(codec)
Definition: hwaccel.h:71
uint8_t * above_skip_ctx
Definition: vp9dec.h:138
uint8_t mvstep[3][2]
Definition: vp9dec.h:154
VP9Context * s
Definition: vp9dec.h:160
struct VP9TileData::@197 counts
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
AVFrame * f
Definition: thread.h:35
static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp56.h:394
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:486
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have add an init_thread_copy() which re-allocates them for other threads.Add AV_CODEC_CAP_FRAME_THREADS to the codec capabilities.There will be very little speed gain at this point but it should work.If there are inter-frame dependencies
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:503
VP9BitstreamHeader h
Definition: vp9shared.h:160
VideoDSPContext vdsp
Definition: vp9dec.h:96
static av_cold int vp9_decode_init(AVCodecContext *avctx)
Definition: vp9.c:1729
ProbContext p
Definition: vp9dec.h:124
uint8_t last_keyframe
Definition: vp9dec.h:109
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2203
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
uint8_t ss_v
Definition: vp9dec.h:107
int size
Definition: avcodec.h:1481
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:403
uint8_t prob[7]
Definition: vp9shared.h:134
uint8_t tx32p[2][3]
Definition: vp9dec.h:55
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:504
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1778
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:391
uint8_t framectxid
Definition: vp9shared.h:109
const int16_t ff_vp9_dc_qlookup[3][256]
Definition: vp9data.c:231
unsigned coef[4][2][2][6][6][3]
Definition: vp9dec.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int16_t * block_base
Definition: vp9dec.h:218
VP9Filter * lflvl
Definition: vp9dec.h:149
static AVPacket pkt
#define HWACCEL_D3D11VA(codec)
Definition: hwaccel.h:79
uint8_t left_segpred_ctx[8]
Definition: vp9dec.h:209
intptr_t atomic_int
Definition: stdatomic.h:55
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2732
unsigned cols
Definition: vp9dec.h:116
#define src
Definition: vp8dsp.c:254
int profile
profile
Definition: avcodec.h:2901
AVCodec.
Definition: avcodec.h:3492
uint8_t comp_ref[5]
Definition: vp9dec.h:54
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:498
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:140
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
struct VP9BitstreamHeader::@202 segmentation
const int16_t ff_vp9_ac_qlookup[3][256]
Definition: vp9data.c:334
uint8_t left_mode_ctx[16]
Definition: vp9dec.h:203
functionally identical to above
Definition: pixfmt.h:505
unsigned log2_tile_rows
Definition: vp9shared.h:151
uint8_t * intra_pred_data[3]
Definition: vp9dec.h:148
int uncompressed_header_size
Definition: vp9shared.h:155
enum FilterMode filtermode
Definition: vp9shared.h:105
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t coef[4][2][2][6][6][3]
Definition: vp9dec.h:125
unsigned partition[4][4][4]
Definition: vp9dec.h:193
#define VP9_SYNCCODE
Definition: vp9.c:38
uint8_t
const int8_t ff_vp9_partition_tree[3][2]
Definition: vp9data.c:35
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static void vp9_free_entries(AVCodecContext *avctx)
Definition: vp9.c:92
uint8_t absolute_vals
Definition: vp9shared.h:132
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
uint8_t varcompref[2]
Definition: vp9shared.h:114
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:497
void ff_vp9_adapt_probs(VP9Context *s)
Definition: vp9prob.c:46
#define f(width, name)
Definition: cbs_vp9.c:255
static void free_buffers(VP9Context *s)
Definition: vp9.c:1192
VP9Frame frames[3]
Definition: vp9shared.h:166
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint8_t * eob_base
Definition: vp9dec.h:219
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
Multithreading support functions.
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:443
#define FF_CODEC_PROPERTY_LOSSLESS
Definition: avcodec.h:3231
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
VP9Block * b_base
Definition: vp9dec.h:166
uint8_t * uveob[2]
Definition: vp9dec.h:219
struct VP9BitstreamHeader::@201 lf_delta
static int update_size(AVCodecContext *avctx, int w, int h)
Definition: vp9.c:171
AVBufferRef * hwaccel_priv_buf
Definition: vp9shared.h:66
static void finish(void)
Definition: movenc.c:345
uint8_t * data
Definition: avcodec.h:1480
static enum AVPixelFormat pix_fmt_rgb[3]
Definition: libdav1d.c:52
#define HWACCEL_DXVA2(codec)
Definition: hwaccel.h:67
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
AVBufferRef * extradata
Definition: vp9shared.h:61
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1821
uint8_t left_partition_ctx[8]
Definition: vp9dec.h:206
VP56RangeCoder * c_b
Definition: vp9dec.h:161
uint8_t skip[3]
Definition: vp9dec.h:58
bitstream reader API header.
static av_always_inline int vpX_rac_is_end(VP56RangeCoder *c)
vp5689 returns 1 if the end of the stream has been reached, 0 otherwise.
Definition: vp56.h:237
uint8_t * above_uv_nnz_ctx[2]
Definition: vp9dec.h:137
VP9DSPContext dsp
Definition: vp9dec.h:95
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:148
uint8_t lim_lut[64]
Definition: vp9dec.h:120
#define max(a, b)
Definition: cuda_runtime.h:33
ptrdiff_t size
Definition: opengl_enc.c:100
enum CompPredMode comppredmode
Definition: vp9shared.h:149
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:392
unsigned log2_tile_cols
Definition: vp9shared.h:151
uint16_t mvscale[3][2]
Definition: vp9dec.h:153
uint8_t refidx[3]
Definition: vp9shared.h:111
uint8_t * above_txfm_ctx
Definition: vp9dec.h:139
av_cold void ff_vp9dsp_init(VP9DSPContext *dsp, int bpp, int bitexact)
Definition: vp9dsp.c:84
int h
Definition: vp9dec.h:114
#define av_log(a,...)
uint8_t bytesperpixel
Definition: vp9dec.h:108
void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl, int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff)
Definition: vp9lpf.c:178
uint8_t mask[2][2][8][4]
Definition: vp9dec.h:76
static av_cold int vp9_decode_free(AVCodecContext *avctx)
Definition: vp9.c:1203
Definition: vp9.h:28
uint8_t partition[4][4][3]
Definition: vp9dec.h:70
uint8_t hp
Definition: vp9dec.h:68
unsigned tile_col_start
Definition: vp9dec.h:167
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int width
Definition: frame.h:353
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define td
Definition: regdef.h:70
uint8_t sign
Definition: vp9dec.h:61
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:1135
static int decode_frame_header(AVCodecContext *avctx, const uint8_t *data, int size, int *ref)
Definition: vp9.c:478
unsigned tile_cols
Definition: vp9shared.h:152
GetBitContext gb
Definition: vp9dec.h:97
uint8_t fp[3]
Definition: vp9dec.h:66
uint8_t signbias[3]
Definition: vp9shared.h:112
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2846
uint8_t refreshctx
Definition: vp9shared.h:107
const char * r
Definition: vf_curves.c:114
uint8_t bpp_index
Definition: vp9dec.h:108
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1648
void * hwaccel_picture_private
Definition: vp9shared.h:67
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3499
uint8_t intra[4]
Definition: vp9dec.h:51
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:390
VP56RangeCoder * c
Definition: vp9dec.h:162
uint8_t bits
Definition: vp3data.h:202
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:122
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1040
uint8_t * above_filter_ctx
Definition: vp9dec.h:144
struct VP9Context::@196 prob
#define REF_FRAME_MVPAIR
Definition: vp9shared.h:164
const uint8_t ff_vp9_model_pareto8[256][8]
Definition: vp9data.c:1176
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
uint8_t comp[5]
Definition: vp9dec.h:52
int ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: vp56rac.c:40
uint8_t left_y_nnz_ctx[16]
Definition: vp9dec.h:202
#define b
Definition: input.c:41
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
int uses_2pass
Definition: vp9shared.h:64
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:911
VP9TileData * td
Definition: vp9dec.h:93
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2838
#define FFMIN(a, b)
Definition: common.h:96
struct VP9Context::@195 prob_ctx[4]
enum TxfmMode txfmmode
Definition: vp9shared.h:148
uint8_t keyframe
Definition: vp9shared.h:98
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards.If some code can't be moved
unsigned tile_rows
Definition: vp9shared.h:152
uint8_t w
Definition: llviddspenc.c:38
struct VP9BitstreamHeader::@202::@204 feat[MAX_SEGMENT]
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:508
uint8_t tx16p[2][2]
Definition: vp9dec.h:56
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
uint8_t class0_hp
Definition: vp9dec.h:67
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:100
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2839
VP9SharedContext s
Definition: vp9dec.h:92
uint8_t * above_partition_ctx
Definition: vp9dec.h:133
int n
Definition: avisynth_c.h:760
uint8_t mv_mode[7][3]
Definition: vp9dec.h:50
uint8_t fixcompref
Definition: vp9shared.h:113
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
int16_t * block
Definition: vp9dec.h:218
HW acceleration through CUDA.
Definition: pixfmt.h:235
uint8_t * above_segpred_ctx
Definition: vp9dec.h:140
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
unsigned rows
Definition: vp9dec.h:116
unsigned sb_cols
Definition: vp9dec.h:116
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:1044
#define atomic_fetch_add_explicit(object, operand, order)
Definition: stdatomic.h:149
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:368
static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
Definition: vp56.h:285
static int init_frames(AVCodecContext *avctx)
Definition: vp9.c:1703
VP56mv(* above_mv_ctx)[2]
Definition: vp9dec.h:145
Libavcodec external API header.
BlockLevel
Definition: vp9shared.h:70
uint8_t filter[4][2]
Definition: vp9dec.h:49
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3761
uint8_t class0_fp[2][3]
Definition: vp9dec.h:65
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have add an so the codec calls ff_thread_report set AVCodecInternal allocate_progress The frames must then be freed with ff_thread_release_buffer().Otherwise leave it at zero and decode directly into the user-supplied frames.Call ff_thread_report_progress() after some part of the current picture has decoded.A good place to put this is where draw_horiz_band() is called-add this if it isn't called anywhere
uint8_t * uveob_base[2]
Definition: vp9dec.h:219
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
int pass
Definition: vp9dec.h:99
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1568
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define REF_FRAME_SEGMAP
Definition: vp9shared.h:165
int8_t mode[2]
Definition: vp9shared.h:122
#define CUR_FRAME
Definition: vp9shared.h:163
static int update_block_buffers(AVCodecContext *avctx)
Definition: vp9.c:289
static int vp9_frame_ref(AVCodecContext *avctx, VP9Frame *dst, VP9Frame *src)
Definition: vp9.c:141
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
uint8_t * above_y_nnz_ctx
Definition: vp9dec.h:136
int16_t * uvblock_base[2]
Definition: vp9dec.h:218
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2196
uint8_t tx8p[2]
Definition: vp9dec.h:57
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:197
uint8_t ss_h
Definition: vp9dec.h:107
uint8_t y_mode[4][9]
Definition: vp9dec.h:47
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:393
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
struct ProbContext::@193 mv_comp[2]
int16_t * uvblock[2]
Definition: vp9dec.h:218
uint8_t last_bpp
Definition: vp9dec.h:108
static int read_colorspace_details(AVCodecContext *avctx)
Definition: vp9.c:416
uint8_t * above_intra_ctx
Definition: vp9dec.h:141
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:151
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
static int vp9_alloc_entries(AVCodecContext *avctx, int n)
Definition: vp9.c:93
struct VP9BitstreamHeader::@203 tiling
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer. ...
Definition: pixfmt.h:137
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
enum BlockPartition bp
Definition: vp9dec.h:86
static int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
Definition: vp56.h:338
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF
Codec initializes slice-based threading with a main function.
Definition: internal.h:70
#define HWACCEL_VAAPI(codec)
Definition: hwaccel.h:73
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:404
uint8_t * above_mode_ctx
Definition: vp9dec.h:134
uint8_t single_ref[5][2]
Definition: vp9dec.h:53
Definition: vp56.h:66
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:388
static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f)
Definition: vp9.c:96
static int vp9_decode_frame(AVCodecContext *avctx, void *frame, int *got_frame, AVPacket *pkt)
Definition: vp9.c:1471
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:3750
uint8_t bits[10]
Definition: vp9dec.h:64
ThreadFrame next_refs[8]
Definition: vp9dec.h:117
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:394
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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:1263
Definition: vp9.h:48
unsigned eob[4][2][2][6][6][2]
Definition: vp9dec.h:195
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:313
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:522
int(* start_frame)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Called at the beginning of each frame or field picture.
Definition: avcodec.h:3722
static int decode_tiles(AVCodecContext *avctx, const uint8_t *data, int size)
Definition: vp9.c:1228
static av_always_inline int inv_recenter_nonneg(int v, int m)
Definition: vp9.c:348
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
#define assign(var, type, n)
int w
Definition: vp9dec.h:114
int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2 *func2, main_func *mainfunc, void *arg, int *ret, int job_count)
enum AVPixelFormat pix_fmt last_fmt gf_fmt
Definition: vp9dec.h:115
AVCodec ff_vp9_decoder
Definition: vp9.c:1792
unsigned sb_rows
Definition: vp9dec.h:116
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:129
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:3230
static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
Definition: vp56.h:322
Core video DSP helper functions.
uint8_t mv_joint[3]
Definition: vp9dec.h:59
enum BlockLevel bl
Definition: vp9dec.h:85
void * priv_data
Definition: avcodec.h:1595
#define HWACCEL_MAX
uint8_t class0
Definition: vp9dec.h:63
#define av_free(p)
static int vp9_frame_alloc(AVCodecContext *avctx, VP9Frame *f)
Definition: vp9.c:105
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3770
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1603
static int decode012(GetBitContext *gb)
Definition: get_bits.h:831
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
VP9mvrefPair * mv
Definition: vp9shared.h:63
struct VP9BitstreamHeader::@200 filter
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
uint8_t invisible
Definition: vp9shared.h:99
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1479
uint8_t use_last_frame_mvs
Definition: vp9shared.h:110
int height
Definition: frame.h:353
ThreadFrame refs[8]
Definition: vp9shared.h:162
uint8_t pred_prob[3]
Definition: vp9shared.h:135
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
#define HWACCEL_VDPAU(codec)
Definition: hwaccel.h:75
void INT64 start
Definition: avisynth_c.h:766
VP9Block * b
Definition: vp9dec.h:166
#define av_always_inline
Definition: attributes.h:39
HW decoding through Direct3D11 via old API, Picture.data[3] contains a ID3D11VideoDecoderOutputView p...
Definition: pixfmt.h:229
static void decode_sb(VP9TileData *td, int row, int col, VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff, enum BlockLevel bl)
Definition: vp9.c:1056
static int update_prob(VP56RangeCoder *c, int p)
Definition: vp9.c:358
#define av_malloc_array(a, b)
const ProbContext ff_vp9_default_probs
Definition: vp9data.c:1435
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:2438
const AVProfile ff_vp9_profiles[]
Definition: profiles.c:134
uint8_t * above_ref_ctx
Definition: vp9dec.h:143
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
BlockPartition
Definition: vp9shared.h:34
struct VP9Context::@194 filter_lut
uint8_t classes[10]
Definition: vp9dec.h:62
const uint8_t ff_vp9_default_coef_probs[4][2][2][6][6][3]
Definition: vp9data.c:1540
uint8_t highprecisionmvs
Definition: vp9shared.h:104
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1457
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1179
uint8_t * above_comp_ctx
Definition: vp9dec.h:142
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:984
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
for(j=16;j >0;--j)
int block_alloc_using_2pass
Definition: vp9dec.h:152
Predicted.
Definition: avutil.h:275
int compressed_header_size
Definition: vp9shared.h:156
uint8_t refreshrefmask
Definition: vp9shared.h:103
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
uint8_t left_skip_ctx[8]
Definition: vp9dec.h:207
int active_tile_cols
Definition: vp9dec.h:99
VP56RangeCoder c
Definition: vp9dec.h:98