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