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