FFmpeg
vp8.c
Go to the documentation of this file.
1 /*
2  * VP7/VP8 compatible video decoder
3  *
4  * Copyright (C) 2010 David Conrad
5  * Copyright (C) 2010 Ronald S. Bultje
6  * Copyright (C) 2010 Fiona Glaser
7  * Copyright (C) 2012 Daniel Kang
8  * Copyright (C) 2014 Peter Ross
9  *
10  * This file is part of FFmpeg.
11  *
12  * FFmpeg is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * FFmpeg is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with FFmpeg; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 #include "config_components.h"
28 
29 #include "libavutil/mem_internal.h"
30 
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "hwconfig.h"
35 #include "mathops.h"
36 #include "thread.h"
37 #include "threadframe.h"
38 #include "vp8.h"
39 #include "vp89_rac.h"
40 #include "vp8data.h"
41 #include "vpx_rac.h"
42 
43 #if ARCH_ARM
44 # include "arm/vp8.h"
45 #endif
46 
47 // fixme: add 1 bit to all the calls to this?
49 {
50  int v;
51 
52  if (!vp89_rac_get(c))
53  return 0;
54 
55  v = vp89_rac_get_uint(c, bits);
56 
57  if (vp89_rac_get(c))
58  v = -v;
59 
60  return v;
61 }
62 
64 {
65  int v = vp89_rac_get_uint(c, 7) << 1;
66  return v + !v;
67 }
68 
69 // DCTextra
70 static int vp8_rac_get_coeff(VPXRangeCoder *c, const uint8_t *prob)
71 {
72  int v = 0;
73 
74  do {
75  v = (v<<1) + vpx_rac_get_prob(c, *prob++);
76  } while (*prob);
77 
78  return v;
79 }
80 
81 static void free_buffers(VP8Context *s)
82 {
83  int i;
84  if (s->thread_data)
85  for (i = 0; i < MAX_THREADS; i++) {
86 #if HAVE_THREADS
87  pthread_cond_destroy(&s->thread_data[i].cond);
88  pthread_mutex_destroy(&s->thread_data[i].lock);
89 #endif
90  av_freep(&s->thread_data[i].filter_strength);
91  }
92  av_freep(&s->thread_data);
93  av_freep(&s->macroblocks_base);
94  av_freep(&s->intra4x4_pred_mode_top);
95  av_freep(&s->top_nnz);
96  av_freep(&s->top_border);
97 
98  s->macroblocks = NULL;
99 }
100 
102 {
103  int ret;
104  if ((ret = ff_thread_get_ext_buffer(s->avctx, &f->tf,
105  ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
106  return ret;
107  if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height)))
108  goto fail;
109  if (s->avctx->hwaccel) {
110  const AVHWAccel *hwaccel = s->avctx->hwaccel;
111  if (hwaccel->frame_priv_data_size) {
112  f->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
113  if (!f->hwaccel_priv_buf)
114  goto fail;
115  f->hwaccel_picture_private = f->hwaccel_priv_buf->data;
116  }
117  }
118  return 0;
119 
120 fail:
121  av_buffer_unref(&f->seg_map);
122  ff_thread_release_ext_buffer(s->avctx, &f->tf);
123  return AVERROR(ENOMEM);
124 }
125 
127 {
128  av_buffer_unref(&f->seg_map);
129  av_buffer_unref(&f->hwaccel_priv_buf);
130  f->hwaccel_picture_private = NULL;
131  ff_thread_release_ext_buffer(s->avctx, &f->tf);
132 }
133 
134 #if CONFIG_VP8_DECODER
135 static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, const VP8Frame *src)
136 {
137  int ret;
138 
139  vp8_release_frame(s, dst);
140 
141  if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
142  return ret;
143  if (src->seg_map &&
144  !(dst->seg_map = av_buffer_ref(src->seg_map))) {
145  vp8_release_frame(s, dst);
146  return AVERROR(ENOMEM);
147  }
148  if (src->hwaccel_picture_private) {
149  dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
150  if (!dst->hwaccel_priv_buf)
151  return AVERROR(ENOMEM);
153  }
154 
155  return 0;
156 }
157 #endif /* CONFIG_VP8_DECODER */
158 
159 static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
160 {
161  VP8Context *s = avctx->priv_data;
162  int i;
163 
164  for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
165  vp8_release_frame(s, &s->frames[i]);
166  memset(s->framep, 0, sizeof(s->framep));
167 
168  if (free_mem)
169  free_buffers(s);
170 }
171 
172 static void vp8_decode_flush(AVCodecContext *avctx)
173 {
174  vp8_decode_flush_impl(avctx, 0);
175 }
176 
178 {
179  VP8Frame *frame = NULL;
180  int i;
181 
182  // find a free buffer
183  for (i = 0; i < 5; i++)
184  if (&s->frames[i] != s->framep[VP8_FRAME_CURRENT] &&
185  &s->frames[i] != s->framep[VP8_FRAME_PREVIOUS] &&
186  &s->frames[i] != s->framep[VP8_FRAME_GOLDEN] &&
187  &s->frames[i] != s->framep[VP8_FRAME_ALTREF]) {
188  frame = &s->frames[i];
189  break;
190  }
191  if (i == 5) {
192  av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
193  abort();
194  }
195  if (frame->tf.f->buf[0])
197 
198  return frame;
199 }
200 
202 {
203  enum AVPixelFormat pix_fmts[] = {
204 #if CONFIG_VP8_VAAPI_HWACCEL
206 #endif
207 #if CONFIG_VP8_NVDEC_HWACCEL
209 #endif
212  };
213 
214  return ff_get_format(s->avctx, pix_fmts);
215 }
216 
217 static av_always_inline
218 int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
219 {
220  AVCodecContext *avctx = s->avctx;
221  int i, ret, dim_reset = 0;
222 
223  if (width != s->avctx->width || ((width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height) && s->macroblocks_base ||
224  height != s->avctx->height) {
225  vp8_decode_flush_impl(s->avctx, 1);
226 
227  ret = ff_set_dimensions(s->avctx, width, height);
228  if (ret < 0)
229  return ret;
230 
231  dim_reset = (s->macroblocks_base != NULL);
232  }
233 
234  if ((s->pix_fmt == AV_PIX_FMT_NONE || dim_reset) &&
235  !s->actually_webp && !is_vp7) {
236  s->pix_fmt = get_pixel_format(s);
237  if (s->pix_fmt < 0)
238  return AVERROR(EINVAL);
239  avctx->pix_fmt = s->pix_fmt;
240  }
241 
242  s->mb_width = (s->avctx->coded_width + 15) / 16;
243  s->mb_height = (s->avctx->coded_height + 15) / 16;
244 
245  s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
246  avctx->thread_count > 1;
247  if (!s->mb_layout) { // Frame threading and one thread
248  s->macroblocks_base = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
249  sizeof(*s->macroblocks));
250  s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
251  } else // Sliced threading
252  s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
253  sizeof(*s->macroblocks));
254  s->top_nnz = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
255  s->top_border = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
256  s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
257 
258  if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||
259  !s->thread_data || (!s->intra4x4_pred_mode_top && !s->mb_layout)) {
260  free_buffers(s);
261  return AVERROR(ENOMEM);
262  }
263 
264  for (i = 0; i < MAX_THREADS; i++) {
265  s->thread_data[i].filter_strength =
266  av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
267  if (!s->thread_data[i].filter_strength) {
268  free_buffers(s);
269  return AVERROR(ENOMEM);
270  }
271 #if HAVE_THREADS
272  pthread_mutex_init(&s->thread_data[i].lock, NULL);
273  pthread_cond_init(&s->thread_data[i].cond, NULL);
274 #endif
275  }
276 
277  s->macroblocks = s->macroblocks_base + 1;
278 
279  return 0;
280 }
281 
283 {
285 }
286 
288 {
290 }
291 
292 
294 {
295  VPXRangeCoder *c = &s->c;
296  int i;
297 
298  s->segmentation.update_map = vp89_rac_get(c);
299  s->segmentation.update_feature_data = vp89_rac_get(c);
300 
301  if (s->segmentation.update_feature_data) {
302  s->segmentation.absolute_vals = vp89_rac_get(c);
303 
304  for (i = 0; i < 4; i++)
305  s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
306 
307  for (i = 0; i < 4; i++)
308  s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
309  }
310  if (s->segmentation.update_map)
311  for (i = 0; i < 3; i++)
312  s->prob->segmentid[i] = vp89_rac_get(c) ? vp89_rac_get_uint(c, 8) : 255;
313 }
314 
316 {
317  VPXRangeCoder *c = &s->c;
318  int i;
319 
320  for (i = 0; i < 4; i++) {
321  if (vp89_rac_get(c)) {
322  s->lf_delta.ref[i] = vp89_rac_get_uint(c, 6);
323 
324  if (vp89_rac_get(c))
325  s->lf_delta.ref[i] = -s->lf_delta.ref[i];
326  }
327  }
328 
329  for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
330  if (vp89_rac_get(c)) {
331  s->lf_delta.mode[i] = vp89_rac_get_uint(c, 6);
332 
333  if (vp89_rac_get(c))
334  s->lf_delta.mode[i] = -s->lf_delta.mode[i];
335  }
336  }
337 }
338 
339 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
340 {
341  const uint8_t *sizes = buf;
342  int i;
343  int ret;
344 
345  s->num_coeff_partitions = 1 << vp89_rac_get_uint(&s->c, 2);
346 
347  buf += 3 * (s->num_coeff_partitions - 1);
348  buf_size -= 3 * (s->num_coeff_partitions - 1);
349  if (buf_size < 0)
350  return -1;
351 
352  for (i = 0; i < s->num_coeff_partitions - 1; i++) {
353  int size = AV_RL24(sizes + 3 * i);
354  if (buf_size - size < 0)
355  return -1;
356  s->coeff_partition_size[i] = size;
357 
358  ret = ff_vpx_init_range_decoder(&s->coeff_partition[i], buf, size);
359  if (ret < 0)
360  return ret;
361  buf += size;
362  buf_size -= size;
363  }
364 
365  s->coeff_partition_size[i] = buf_size;
366  ff_vpx_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
367 
368  return 0;
369 }
370 
372 {
373  VPXRangeCoder *c = &s->c;
374 
375  int yac_qi = vp89_rac_get_uint(c, 7);
376  int ydc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
377  int y2dc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
378  int y2ac_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
379  int uvdc_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
380  int uvac_qi = vp89_rac_get(c) ? vp89_rac_get_uint(c, 7) : yac_qi;
381 
382  s->qmat[0].luma_qmul[0] = vp7_ydc_qlookup[ydc_qi];
383  s->qmat[0].luma_qmul[1] = vp7_yac_qlookup[yac_qi];
384  s->qmat[0].luma_dc_qmul[0] = vp7_y2dc_qlookup[y2dc_qi];
385  s->qmat[0].luma_dc_qmul[1] = vp7_y2ac_qlookup[y2ac_qi];
386  s->qmat[0].chroma_qmul[0] = FFMIN(vp7_ydc_qlookup[uvdc_qi], 132);
387  s->qmat[0].chroma_qmul[1] = vp7_yac_qlookup[uvac_qi];
388 }
389 
391 {
392  VPXRangeCoder *c = &s->c;
393  int i, base_qi;
394 
395  s->quant.yac_qi = vp89_rac_get_uint(c, 7);
396  s->quant.ydc_delta = vp8_rac_get_sint(c, 4);
397  s->quant.y2dc_delta = vp8_rac_get_sint(c, 4);
398  s->quant.y2ac_delta = vp8_rac_get_sint(c, 4);
399  s->quant.uvdc_delta = vp8_rac_get_sint(c, 4);
400  s->quant.uvac_delta = vp8_rac_get_sint(c, 4);
401 
402  for (i = 0; i < 4; i++) {
403  if (s->segmentation.enabled) {
404  base_qi = s->segmentation.base_quant[i];
405  if (!s->segmentation.absolute_vals)
406  base_qi += s->quant.yac_qi;
407  } else
408  base_qi = s->quant.yac_qi;
409 
410  s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.ydc_delta, 7)];
411  s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi, 7)];
412  s->qmat[i].luma_dc_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.y2dc_delta, 7)] * 2;
413  /* 101581>>16 is equivalent to 155/100 */
414  s->qmat[i].luma_dc_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + s->quant.y2ac_delta, 7)] * 101581 >> 16;
415  s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + s->quant.uvdc_delta, 7)];
416  s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + s->quant.uvac_delta, 7)];
417 
418  s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
419  s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
420  }
421 }
422 
423 /**
424  * Determine which buffers golden and altref should be updated with after this frame.
425  * The spec isn't clear here, so I'm going by my understanding of what libvpx does
426  *
427  * Intra frames update all 3 references
428  * Inter frames update VP8_FRAME_PREVIOUS if the update_last flag is set
429  * If the update (golden|altref) flag is set, it's updated with the current frame
430  * if update_last is set, and VP8_FRAME_PREVIOUS otherwise.
431  * If the flag is not set, the number read means:
432  * 0: no update
433  * 1: VP8_FRAME_PREVIOUS
434  * 2: update golden with altref, or update altref with golden
435  */
437 {
438  VPXRangeCoder *c = &s->c;
439 
440  if (update)
441  return VP8_FRAME_CURRENT;
442 
443  switch (vp89_rac_get_uint(c, 2)) {
444  case 1:
445  return VP8_FRAME_PREVIOUS;
446  case 2:
448  }
449  return VP8_FRAME_NONE;
450 }
451 
453 {
454  int i, j;
455  for (i = 0; i < 4; i++)
456  for (j = 0; j < 16; j++)
457  memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
458  sizeof(s->prob->token[i][j]));
459 }
460 
462 {
463  VPXRangeCoder *c = &s->c;
464  int i, j, k, l, m;
465 
466  for (i = 0; i < 4; i++)
467  for (j = 0; j < 8; j++)
468  for (k = 0; k < 3; k++)
469  for (l = 0; l < NUM_DCT_TOKENS-1; l++)
471  int prob = vp89_rac_get_uint(c, 8);
472  for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
473  s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
474  }
475 }
476 
477 #define VP7_MVC_SIZE 17
478 #define VP8_MVC_SIZE 19
479 
481  int mvc_size)
482 {
483  VPXRangeCoder *c = &s->c;
484  int i, j;
485 
486  if (vp89_rac_get(c))
487  for (i = 0; i < 4; i++)
488  s->prob->pred16x16[i] = vp89_rac_get_uint(c, 8);
489  if (vp89_rac_get(c))
490  for (i = 0; i < 3; i++)
491  s->prob->pred8x8c[i] = vp89_rac_get_uint(c, 8);
492 
493  // 17.2 MV probability update
494  for (i = 0; i < 2; i++)
495  for (j = 0; j < mvc_size; j++)
497  s->prob->mvc[i][j] = vp8_rac_get_nn(c);
498 }
499 
500 static void update_refs(VP8Context *s)
501 {
502  VPXRangeCoder *c = &s->c;
503 
504  int update_golden = vp89_rac_get(c);
505  int update_altref = vp89_rac_get(c);
506 
507  s->update_golden = ref_to_update(s, update_golden, VP8_FRAME_GOLDEN);
508  s->update_altref = ref_to_update(s, update_altref, VP8_FRAME_ALTREF);
509 }
510 
511 static void copy_chroma(AVFrame *dst, const AVFrame *src, int width, int height)
512 {
513  int i, j;
514 
515  for (j = 1; j < 3; j++) {
516  for (i = 0; i < height / 2; i++)
517  memcpy(dst->data[j] + i * dst->linesize[j],
518  src->data[j] + i * src->linesize[j], width / 2);
519  }
520 }
521 
522 static void fade(uint8_t *dst, ptrdiff_t dst_linesize,
523  const uint8_t *src, ptrdiff_t src_linesize,
524  int width, int height,
525  int alpha, int beta)
526 {
527  int i, j;
528  for (j = 0; j < height; j++) {
529  const uint8_t *src2 = src + j * src_linesize;
530  uint8_t *dst2 = dst + j * dst_linesize;
531  for (i = 0; i < width; i++) {
532  uint8_t y = src2[i];
533  dst2[i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
534  }
535  }
536 }
537 
538 static int vp7_fade_frame(VP8Context *s, int alpha, int beta)
539 {
540  int ret;
541 
542  if (!s->keyframe && (alpha || beta)) {
543  int width = s->mb_width * 16;
544  int height = s->mb_height * 16;
545  const AVFrame *src;
546  AVFrame *dst;
547 
548  if (!s->framep[VP8_FRAME_PREVIOUS] ||
549  !s->framep[VP8_FRAME_GOLDEN]) {
550  av_log(s->avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
551  return AVERROR_INVALIDDATA;
552  }
553 
554  src =
555  dst = s->framep[VP8_FRAME_PREVIOUS]->tf.f;
556 
557  /* preserve the golden frame, write a new previous frame */
558  if (s->framep[VP8_FRAME_GOLDEN] == s->framep[VP8_FRAME_PREVIOUS]) {
560  if ((ret = vp8_alloc_frame(s, s->framep[VP8_FRAME_PREVIOUS], 1)) < 0)
561  return ret;
562 
563  dst = s->framep[VP8_FRAME_PREVIOUS]->tf.f;
564 
565  copy_chroma(dst, src, width, height);
566  }
567 
568  fade(dst->data[0], dst->linesize[0],
569  src->data[0], src->linesize[0],
570  width, height, alpha, beta);
571  }
572 
573  return 0;
574 }
575 
576 static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
577 {
578  VPXRangeCoder *c = &s->c;
579  int part1_size, hscale, vscale, i, j, ret;
580  int width = s->avctx->width;
581  int height = s->avctx->height;
582  int alpha = 0;
583  int beta = 0;
584  int fade_present = 1;
585 
586  if (buf_size < 4) {
587  return AVERROR_INVALIDDATA;
588  }
589 
590  s->profile = (buf[0] >> 1) & 7;
591  if (s->profile > 1) {
592  avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
593  return AVERROR_INVALIDDATA;
594  }
595 
596  s->keyframe = !(buf[0] & 1);
597  s->invisible = 0;
598  part1_size = AV_RL24(buf) >> 4;
599 
600  if (buf_size < 4 - s->profile + part1_size) {
601  av_log(s->avctx, AV_LOG_ERROR, "Buffer size %d is too small, needed : %d\n", buf_size, 4 - s->profile + part1_size);
602  return AVERROR_INVALIDDATA;
603  }
604 
605  buf += 4 - s->profile;
606  buf_size -= 4 - s->profile;
607 
608  memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
609 
610  ret = ff_vpx_init_range_decoder(c, buf, part1_size);
611  if (ret < 0)
612  return ret;
613  buf += part1_size;
614  buf_size -= part1_size;
615 
616  /* A. Dimension information (keyframes only) */
617  if (s->keyframe) {
618  width = vp89_rac_get_uint(c, 12);
619  height = vp89_rac_get_uint(c, 12);
620  hscale = vp89_rac_get_uint(c, 2);
621  vscale = vp89_rac_get_uint(c, 2);
622  if (hscale || vscale)
623  avpriv_request_sample(s->avctx, "Upscaling");
624 
625  s->update_golden = s->update_altref = VP8_FRAME_CURRENT;
627  memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
628  sizeof(s->prob->pred16x16));
629  memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
630  sizeof(s->prob->pred8x8c));
631  for (i = 0; i < 2; i++)
632  memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
633  sizeof(vp7_mv_default_prob[i]));
634  memset(&s->segmentation, 0, sizeof(s->segmentation));
635  memset(&s->lf_delta, 0, sizeof(s->lf_delta));
636  memcpy(s->prob[0].scan, ff_zigzag_scan, sizeof(s->prob[0].scan));
637  }
638 
639  if (s->keyframe || s->profile > 0)
640  memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
641 
642  /* B. Decoding information for all four macroblock-level features */
643  for (i = 0; i < 4; i++) {
644  s->feature_enabled[i] = vp89_rac_get(c);
645  if (s->feature_enabled[i]) {
646  s->feature_present_prob[i] = vp89_rac_get_uint(c, 8);
647 
648  for (j = 0; j < 3; j++)
649  s->feature_index_prob[i][j] =
650  vp89_rac_get(c) ? vp89_rac_get_uint(c, 8) : 255;
651 
652  if (vp7_feature_value_size[s->profile][i])
653  for (j = 0; j < 4; j++)
654  s->feature_value[i][j] =
656  }
657  }
658 
659  s->segmentation.enabled = 0;
660  s->segmentation.update_map = 0;
661  s->lf_delta.enabled = 0;
662 
663  s->num_coeff_partitions = 1;
664  ret = ff_vpx_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
665  if (ret < 0)
666  return ret;
667 
668  if (!s->macroblocks_base || /* first frame */
669  width != s->avctx->width || height != s->avctx->height ||
670  (width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
671  if ((ret = vp7_update_dimensions(s, width, height)) < 0)
672  return ret;
673  }
674 
675  /* C. Dequantization indices */
676  vp7_get_quants(s);
677 
678  /* D. Golden frame update flag (a Flag) for interframes only */
679  if (!s->keyframe) {
680  s->update_golden = vp89_rac_get(c) ? VP8_FRAME_CURRENT : VP8_FRAME_NONE;
681  s->sign_bias[VP8_FRAME_GOLDEN] = 0;
682  }
683 
684  s->update_last = 1;
685  s->update_probabilities = 1;
686 
687  if (s->profile > 0) {
688  s->update_probabilities = vp89_rac_get(c);
689  if (!s->update_probabilities)
690  s->prob[1] = s->prob[0];
691 
692  if (!s->keyframe)
693  fade_present = vp89_rac_get(c);
694  }
695 
696  if (vpx_rac_is_end(c))
697  return AVERROR_INVALIDDATA;
698  /* E. Fading information for previous frame */
699  if (fade_present && vp89_rac_get(c)) {
700  alpha = (int8_t) vp89_rac_get_uint(c, 8);
701  beta = (int8_t) vp89_rac_get_uint(c, 8);
702  }
703 
704  /* F. Loop filter type */
705  if (!s->profile)
706  s->filter.simple = vp89_rac_get(c);
707 
708  /* G. DCT coefficient ordering specification */
709  if (vp89_rac_get(c))
710  for (i = 1; i < 16; i++)
711  s->prob[0].scan[i] = ff_zigzag_scan[vp89_rac_get_uint(c, 4)];
712 
713  /* H. Loop filter levels */
714  if (s->profile > 0)
715  s->filter.simple = vp89_rac_get(c);
716  s->filter.level = vp89_rac_get_uint(c, 6);
717  s->filter.sharpness = vp89_rac_get_uint(c, 3);
718 
719  /* I. DCT coefficient probability update; 13.3 Token Probability Updates */
721 
722  s->mbskip_enabled = 0;
723 
724  /* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
725  if (!s->keyframe) {
726  s->prob->intra = vp89_rac_get_uint(c, 8);
727  s->prob->last = vp89_rac_get_uint(c, 8);
729  }
730 
731  if (vpx_rac_is_end(c))
732  return AVERROR_INVALIDDATA;
733 
734  if ((ret = vp7_fade_frame(s, alpha, beta)) < 0)
735  return ret;
736 
737  return 0;
738 }
739 
740 static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
741 {
742  VPXRangeCoder *c = &s->c;
743  int header_size, hscale, vscale, ret;
744  int width = s->avctx->width;
745  int height = s->avctx->height;
746 
747  if (buf_size < 3) {
748  av_log(s->avctx, AV_LOG_ERROR, "Insufficent data (%d) for header\n", buf_size);
749  return AVERROR_INVALIDDATA;
750  }
751 
752  s->keyframe = !(buf[0] & 1);
753  s->profile = (buf[0]>>1) & 7;
754  s->invisible = !(buf[0] & 0x10);
755  header_size = AV_RL24(buf) >> 5;
756  buf += 3;
757  buf_size -= 3;
758 
759  s->header_partition_size = header_size;
760 
761  if (s->profile > 3)
762  av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
763 
764  if (!s->profile)
765  memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
766  sizeof(s->put_pixels_tab));
767  else // profile 1-3 use bilinear, 4+ aren't defined so whatever
768  memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
769  sizeof(s->put_pixels_tab));
770 
771  if (header_size > buf_size - 7 * s->keyframe) {
772  av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
773  return AVERROR_INVALIDDATA;
774  }
775 
776  if (s->keyframe) {
777  if (AV_RL24(buf) != 0x2a019d) {
778  av_log(s->avctx, AV_LOG_ERROR,
779  "Invalid start code 0x%x\n", AV_RL24(buf));
780  return AVERROR_INVALIDDATA;
781  }
782  width = AV_RL16(buf + 3) & 0x3fff;
783  height = AV_RL16(buf + 5) & 0x3fff;
784  hscale = buf[4] >> 6;
785  vscale = buf[6] >> 6;
786  buf += 7;
787  buf_size -= 7;
788 
789  if (hscale || vscale)
790  avpriv_request_sample(s->avctx, "Upscaling");
791 
792  s->update_golden = s->update_altref = VP8_FRAME_CURRENT;
794  memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
795  sizeof(s->prob->pred16x16));
796  memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
797  sizeof(s->prob->pred8x8c));
798  memcpy(s->prob->mvc, vp8_mv_default_prob,
799  sizeof(s->prob->mvc));
800  memset(&s->segmentation, 0, sizeof(s->segmentation));
801  memset(&s->lf_delta, 0, sizeof(s->lf_delta));
802  }
803 
804  ret = ff_vpx_init_range_decoder(c, buf, header_size);
805  if (ret < 0)
806  return ret;
807  buf += header_size;
808  buf_size -= header_size;
809 
810  if (s->keyframe) {
811  s->colorspace = vp89_rac_get(c);
812  if (s->colorspace)
813  av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
814  s->fullrange = vp89_rac_get(c);
815  }
816 
817  if ((s->segmentation.enabled = vp89_rac_get(c)))
819  else
820  s->segmentation.update_map = 0; // FIXME: move this to some init function?
821 
822  s->filter.simple = vp89_rac_get(c);
823  s->filter.level = vp89_rac_get_uint(c, 6);
824  s->filter.sharpness = vp89_rac_get_uint(c, 3);
825 
826  if ((s->lf_delta.enabled = vp89_rac_get(c))) {
827  s->lf_delta.update = vp89_rac_get(c);
828  if (s->lf_delta.update)
830  }
831 
832  if (setup_partitions(s, buf, buf_size)) {
833  av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
834  return AVERROR_INVALIDDATA;
835  }
836 
837  if (!s->macroblocks_base || /* first frame */
838  width != s->avctx->width || height != s->avctx->height ||
839  (width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height)
840  if ((ret = vp8_update_dimensions(s, width, height)) < 0)
841  return ret;
842 
843  vp8_get_quants(s);
844 
845  if (!s->keyframe) {
846  update_refs(s);
847  s->sign_bias[VP8_FRAME_GOLDEN] = vp89_rac_get(c);
848  s->sign_bias[VP8_FRAME_ALTREF] = vp89_rac_get(c);
849  }
850 
851  // if we aren't saving this frame's probabilities for future frames,
852  // make a copy of the current probabilities
853  if (!(s->update_probabilities = vp89_rac_get(c)))
854  s->prob[1] = s->prob[0];
855 
856  s->update_last = s->keyframe || vp89_rac_get(c);
857 
859 
860  if ((s->mbskip_enabled = vp89_rac_get(c)))
861  s->prob->mbskip = vp89_rac_get_uint(c, 8);
862 
863  if (!s->keyframe) {
864  s->prob->intra = vp89_rac_get_uint(c, 8);
865  s->prob->last = vp89_rac_get_uint(c, 8);
866  s->prob->golden = vp89_rac_get_uint(c, 8);
868  }
869 
870  // Record the entropy coder state here so that hwaccels can use it.
871  s->c.code_word = vpx_rac_renorm(&s->c);
872  s->coder_state_at_header_end.input = s->c.buffer - (-s->c.bits / 8);
873  s->coder_state_at_header_end.range = s->c.high;
874  s->coder_state_at_header_end.value = s->c.code_word >> 16;
875  s->coder_state_at_header_end.bit_count = -s->c.bits % 8;
876 
877  return 0;
878 }
879 
880 static av_always_inline
881 void clamp_mv(const VP8mvbounds *s, VP8mv *dst, const VP8mv *src)
882 {
883  dst->x = av_clip(src->x, av_clip(s->mv_min.x, INT16_MIN, INT16_MAX),
884  av_clip(s->mv_max.x, INT16_MIN, INT16_MAX));
885  dst->y = av_clip(src->y, av_clip(s->mv_min.y, INT16_MIN, INT16_MAX),
886  av_clip(s->mv_max.y, INT16_MIN, INT16_MAX));
887 }
888 
889 /**
890  * Motion vector coding, 17.1.
891  */
892 static av_always_inline int read_mv_component(VPXRangeCoder *c, const uint8_t *p, int vp7)
893 {
894  int bit, x = 0;
895 
896  if (vpx_rac_get_prob_branchy(c, p[0])) {
897  int i;
898 
899  for (i = 0; i < 3; i++)
900  x += vpx_rac_get_prob(c, p[9 + i]) << i;
901  for (i = (vp7 ? 7 : 9); i > 3; i--)
902  x += vpx_rac_get_prob(c, p[9 + i]) << i;
903  if (!(x & (vp7 ? 0xF0 : 0xFFF0)) || vpx_rac_get_prob(c, p[12]))
904  x += 8;
905  } else {
906  // small_mvtree
907  const uint8_t *ps = p + 2;
908  bit = vpx_rac_get_prob(c, *ps);
909  ps += 1 + 3 * bit;
910  x += 4 * bit;
911  bit = vpx_rac_get_prob(c, *ps);
912  ps += 1 + bit;
913  x += 2 * bit;
914  x += vpx_rac_get_prob(c, *ps);
915  }
916 
917  return (x && vpx_rac_get_prob(c, p[1])) ? -x : x;
918 }
919 
920 static int vp7_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
921 {
922  return read_mv_component(c, p, 1);
923 }
924 
925 static int vp8_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
926 {
927  return read_mv_component(c, p, 0);
928 }
929 
930 static av_always_inline
931 const uint8_t *get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
932 {
933  if (is_vp7)
934  return vp7_submv_prob;
935 
936  if (left == top)
937  return vp8_submv_prob[4 - !!left];
938  if (!top)
939  return vp8_submv_prob[2];
940  return vp8_submv_prob[1 - !!left];
941 }
942 
943 /**
944  * Split motion vector prediction, 16.4.
945  * @returns the number of motion vectors parsed (2, 4 or 16)
946  */
947 static av_always_inline
949  int layout, int is_vp7)
950 {
951  int part_idx;
952  int n, num;
953  const VP8Macroblock *top_mb;
954  const VP8Macroblock *left_mb = &mb[-1];
955  const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning];
956  const uint8_t *mbsplits_top, *mbsplits_cur, *firstidx;
957  const VP8mv *top_mv;
958  const VP8mv *left_mv = left_mb->bmv;
959  const VP8mv *cur_mv = mb->bmv;
960 
961  if (!layout) // layout is inlined, s->mb_layout is not
962  top_mb = &mb[2];
963  else
964  top_mb = &mb[-s->mb_width - 1];
965  mbsplits_top = vp8_mbsplits[top_mb->partitioning];
966  top_mv = top_mb->bmv;
967 
971  else
972  part_idx = VP8_SPLITMVMODE_8x8;
973  } else {
974  part_idx = VP8_SPLITMVMODE_4x4;
975  }
976 
977  num = vp8_mbsplit_count[part_idx];
978  mbsplits_cur = vp8_mbsplits[part_idx],
979  firstidx = vp8_mbfirstidx[part_idx];
980  mb->partitioning = part_idx;
981 
982  for (n = 0; n < num; n++) {
983  int k = firstidx[n];
984  uint32_t left, above;
985  const uint8_t *submv_prob;
986 
987  if (!(k & 3))
988  left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
989  else
990  left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
991  if (k <= 3)
992  above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
993  else
994  above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
995 
996  submv_prob = get_submv_prob(left, above, is_vp7);
997 
998  if (vpx_rac_get_prob_branchy(c, submv_prob[0])) {
999  if (vpx_rac_get_prob_branchy(c, submv_prob[1])) {
1000  if (vpx_rac_get_prob_branchy(c, submv_prob[2])) {
1001  mb->bmv[n].y = mb->mv.y +
1002  read_mv_component(c, s->prob->mvc[0], is_vp7);
1003  mb->bmv[n].x = mb->mv.x +
1004  read_mv_component(c, s->prob->mvc[1], is_vp7);
1005  } else {
1006  AV_ZERO32(&mb->bmv[n]);
1007  }
1008  } else {
1009  AV_WN32A(&mb->bmv[n], above);
1010  }
1011  } else {
1012  AV_WN32A(&mb->bmv[n], left);
1013  }
1014  }
1015 
1016  return num;
1017 }
1018 
1019 /**
1020  * The vp7 reference decoder uses a padding macroblock column (added to right
1021  * edge of the frame) to guard against illegal macroblock offsets. The
1022  * algorithm has bugs that permit offsets to straddle the padding column.
1023  * This function replicates those bugs.
1024  *
1025  * @param[out] edge_x macroblock x address
1026  * @param[out] edge_y macroblock y address
1027  *
1028  * @return macroblock offset legal (boolean)
1029  */
1030 static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
1031  int xoffset, int yoffset, int boundary,
1032  int *edge_x, int *edge_y)
1033 {
1034  int vwidth = mb_width + 1;
1035  int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
1036  if (new < boundary || new % vwidth == vwidth - 1)
1037  return 0;
1038  *edge_y = new / vwidth;
1039  *edge_x = new % vwidth;
1040  return 1;
1041 }
1042 
1043 static const VP8mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
1044 {
1045  return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
1046 }
1047 
1048 static av_always_inline
1050  int mb_x, int mb_y, int layout)
1051 {
1052  enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
1053  enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
1054  int idx = CNT_ZERO;
1055  VP8mv near_mv[3];
1056  uint8_t cnt[3] = { 0 };
1057  VPXRangeCoder *c = &s->c;
1058  int i;
1059 
1060  AV_ZERO32(&near_mv[0]);
1061  AV_ZERO32(&near_mv[1]);
1062  AV_ZERO32(&near_mv[2]);
1063 
1064  for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
1065  const VP7MVPred * pred = &vp7_mv_pred[i];
1066  int edge_x, edge_y;
1067 
1068  if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
1069  pred->yoffset, !s->profile, &edge_x, &edge_y)) {
1070  const VP8Macroblock *edge = (s->mb_layout == 1)
1071  ? s->macroblocks_base + 1 + edge_x +
1072  (s->mb_width + 1) * (edge_y + 1)
1073  : s->macroblocks + edge_x +
1074  (s->mb_height - edge_y - 1) * 2;
1075  uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
1076  if (mv) {
1077  if (AV_RN32A(&near_mv[CNT_NEAREST])) {
1078  if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
1079  idx = CNT_NEAREST;
1080  } else if (AV_RN32A(&near_mv[CNT_NEAR])) {
1081  if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
1082  continue;
1083  idx = CNT_NEAR;
1084  } else {
1085  AV_WN32A(&near_mv[CNT_NEAR], mv);
1086  idx = CNT_NEAR;
1087  }
1088  } else {
1089  AV_WN32A(&near_mv[CNT_NEAREST], mv);
1090  idx = CNT_NEAREST;
1091  }
1092  } else {
1093  idx = CNT_ZERO;
1094  }
1095  } else {
1096  idx = CNT_ZERO;
1097  }
1098  cnt[idx] += vp7_mv_pred[i].score;
1099  }
1100 
1101  mb->partitioning = VP8_SPLITMVMODE_NONE;
1102 
1103  if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
1104  mb->mode = VP8_MVMODE_MV;
1105 
1106  if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
1107 
1108  if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
1109 
1110  if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
1111  AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
1112  else
1113  AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR] ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
1114 
1115  if (vpx_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
1116  mb->mode = VP8_MVMODE_SPLIT;
1117  mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
1118  } else {
1119  mb->mv.y += vp7_read_mv_component(c, s->prob->mvc[0]);
1120  mb->mv.x += vp7_read_mv_component(c, s->prob->mvc[1]);
1121  mb->bmv[0] = mb->mv;
1122  }
1123  } else {
1124  mb->mv = near_mv[CNT_NEAR];
1125  mb->bmv[0] = mb->mv;
1126  }
1127  } else {
1128  mb->mv = near_mv[CNT_NEAREST];
1129  mb->bmv[0] = mb->mv;
1130  }
1131  } else {
1132  mb->mode = VP8_MVMODE_ZERO;
1133  AV_ZERO32(&mb->mv);
1134  mb->bmv[0] = mb->mv;
1135  }
1136 }
1137 
1138 static av_always_inline
1140  int mb_x, int mb_y, int layout)
1141 {
1142  VP8Macroblock *mb_edge[3] = { 0 /* top */,
1143  mb - 1 /* left */,
1144  0 /* top-left */ };
1145  enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
1146  enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
1147  int idx = CNT_ZERO;
1148  int cur_sign_bias = s->sign_bias[mb->ref_frame];
1149  const int8_t *sign_bias = s->sign_bias;
1150  VP8mv near_mv[4];
1151  uint8_t cnt[4] = { 0 };
1152  VPXRangeCoder *c = &s->c;
1153 
1154  if (!layout) { // layout is inlined (s->mb_layout is not)
1155  mb_edge[0] = mb + 2;
1156  mb_edge[2] = mb + 1;
1157  } else {
1158  mb_edge[0] = mb - s->mb_width - 1;
1159  mb_edge[2] = mb - s->mb_width - 2;
1160  }
1161 
1162  AV_ZERO32(&near_mv[0]);
1163  AV_ZERO32(&near_mv[1]);
1164  AV_ZERO32(&near_mv[2]);
1165 
1166  /* Process MB on top, left and top-left */
1167 #define MV_EDGE_CHECK(n) \
1168  { \
1169  const VP8Macroblock *edge = mb_edge[n]; \
1170  int edge_ref = edge->ref_frame; \
1171  if (edge_ref != VP8_FRAME_CURRENT) { \
1172  uint32_t mv = AV_RN32A(&edge->mv); \
1173  if (mv) { \
1174  if (cur_sign_bias != sign_bias[edge_ref]) { \
1175  /* SWAR negate of the values in mv. */ \
1176  mv = ~mv; \
1177  mv = ((mv & 0x7fff7fff) + \
1178  0x00010001) ^ (mv & 0x80008000); \
1179  } \
1180  if (!n || mv != AV_RN32A(&near_mv[idx])) \
1181  AV_WN32A(&near_mv[++idx], mv); \
1182  cnt[idx] += 1 + (n != 2); \
1183  } else \
1184  cnt[CNT_ZERO] += 1 + (n != 2); \
1185  } \
1186  }
1187 
1188  MV_EDGE_CHECK(0)
1189  MV_EDGE_CHECK(1)
1190  MV_EDGE_CHECK(2)
1191 
1192  mb->partitioning = VP8_SPLITMVMODE_NONE;
1193  if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
1194  mb->mode = VP8_MVMODE_MV;
1195 
1196  /* If we have three distinct MVs, merge first and last if they're the same */
1197  if (cnt[CNT_SPLITMV] &&
1198  AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
1199  cnt[CNT_NEAREST] += 1;
1200 
1201  /* Swap near and nearest if necessary */
1202  if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
1203  FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
1204  FFSWAP(VP8mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
1205  }
1206 
1207  if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
1208  if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
1209  /* Choose the best mv out of 0,0 and the nearest mv */
1210  clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
1211  cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
1212  (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
1213  (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
1214 
1215  if (vpx_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
1216  mb->mode = VP8_MVMODE_SPLIT;
1217  mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP8) - 1];
1218  } else {
1219  mb->mv.y += vp8_read_mv_component(c, s->prob->mvc[0]);
1220  mb->mv.x += vp8_read_mv_component(c, s->prob->mvc[1]);
1221  mb->bmv[0] = mb->mv;
1222  }
1223  } else {
1224  clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_NEAR]);
1225  mb->bmv[0] = mb->mv;
1226  }
1227  } else {
1228  clamp_mv(mv_bounds, &mb->mv, &near_mv[CNT_NEAREST]);
1229  mb->bmv[0] = mb->mv;
1230  }
1231  } else {
1232  mb->mode = VP8_MVMODE_ZERO;
1233  AV_ZERO32(&mb->mv);
1234  mb->bmv[0] = mb->mv;
1235  }
1236 }
1237 
1238 static av_always_inline
1240  int mb_x, int keyframe, int layout)
1241 {
1242  uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1243 
1244  if (layout) {
1245  VP8Macroblock *mb_top = mb - s->mb_width - 1;
1246  memcpy(mb->intra4x4_pred_mode_top, mb_top->intra4x4_pred_mode_top, 4);
1247  }
1248  if (keyframe) {
1249  int x, y;
1250  uint8_t *top;
1251  uint8_t *const left = s->intra4x4_pred_mode_left;
1252  if (layout)
1253  top = mb->intra4x4_pred_mode_top;
1254  else
1255  top = s->intra4x4_pred_mode_top + 4 * mb_x;
1256  for (y = 0; y < 4; y++) {
1257  for (x = 0; x < 4; x++) {
1258  const uint8_t *ctx;
1259  ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
1260  *intra4x4 = vp89_rac_get_tree(c, vp8_pred4x4_tree, ctx);
1261  left[y] = top[x] = *intra4x4;
1262  intra4x4++;
1263  }
1264  }
1265  } else {
1266  int i;
1267  for (i = 0; i < 16; i++)
1268  intra4x4[i] = vp89_rac_get_tree(c, vp8_pred4x4_tree,
1270  }
1271 }
1272 
1273 static av_always_inline
1274 void decode_mb_mode(VP8Context *s, const VP8mvbounds *mv_bounds,
1275  VP8Macroblock *mb, int mb_x, int mb_y,
1276  uint8_t *segment, const uint8_t *ref, int layout, int is_vp7)
1277 {
1278  VPXRangeCoder *c = &s->c;
1279  static const char * const vp7_feature_name[] = { "q-index",
1280  "lf-delta",
1281  "partial-golden-update",
1282  "blit-pitch" };
1283  if (is_vp7) {
1284  int i;
1285  *segment = 0;
1286  for (i = 0; i < 4; i++) {
1287  if (s->feature_enabled[i]) {
1288  if (vpx_rac_get_prob_branchy(c, s->feature_present_prob[i])) {
1290  s->feature_index_prob[i]);
1291  av_log(s->avctx, AV_LOG_WARNING,
1292  "Feature %s present in macroblock (value 0x%x)\n",
1293  vp7_feature_name[i], s->feature_value[i][index]);
1294  }
1295  }
1296  }
1297  } else if (s->segmentation.update_map) {
1298  int bit = vpx_rac_get_prob(c, s->prob->segmentid[0]);
1299  *segment = vpx_rac_get_prob(c, s->prob->segmentid[1+bit]) + 2*bit;
1300  } else if (s->segmentation.enabled)
1301  *segment = ref ? *ref : *segment;
1302  mb->segment = *segment;
1303 
1304  mb->skip = s->mbskip_enabled ? vpx_rac_get_prob(c, s->prob->mbskip) : 0;
1305 
1306  if (s->keyframe) {
1309 
1310  if (mb->mode == MODE_I4x4) {
1311  decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
1312  } else {
1313  const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
1314  : vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
1315  if (s->mb_layout)
1316  AV_WN32A(mb->intra4x4_pred_mode_top, modes);
1317  else
1318  AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
1319  AV_WN32A(s->intra4x4_pred_mode_left, modes);
1320  }
1321 
1322  mb->chroma_pred_mode = vp89_rac_get_tree(c, vp8_pred8x8c_tree,
1324  mb->ref_frame = VP8_FRAME_CURRENT;
1325  } else if (vpx_rac_get_prob_branchy(c, s->prob->intra)) {
1326  // inter MB, 16.2
1327  if (vpx_rac_get_prob_branchy(c, s->prob->last))
1328  mb->ref_frame =
1329  (!is_vp7 && vpx_rac_get_prob(c, s->prob->golden)) ? VP8_FRAME_ALTREF
1330  : VP8_FRAME_GOLDEN;
1331  else
1332  mb->ref_frame = VP8_FRAME_PREVIOUS;
1333  s->ref_count[mb->ref_frame - 1]++;
1334 
1335  // motion vectors, 16.3
1336  if (is_vp7)
1337  vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
1338  else
1339  vp8_decode_mvs(s, mv_bounds, mb, mb_x, mb_y, layout);
1340  } else {
1341  // intra MB, 16.1
1343  s->prob->pred16x16);
1344 
1345  if (mb->mode == MODE_I4x4)
1346  decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
1347 
1348  mb->chroma_pred_mode = vp89_rac_get_tree(c, vp8_pred8x8c_tree,
1349  s->prob->pred8x8c);
1350  mb->ref_frame = VP8_FRAME_CURRENT;
1351  mb->partitioning = VP8_SPLITMVMODE_NONE;
1352  AV_ZERO32(&mb->bmv[0]);
1353  }
1354 }
1355 
1356 /**
1357  * @param r arithmetic bitstream reader context
1358  * @param block destination for block coefficients
1359  * @param probs probabilities to use when reading trees from the bitstream
1360  * @param i initial coeff index, 0 unless a separate DC block is coded
1361  * @param qmul array holding the dc/ac dequant factor at position 0/1
1362  *
1363  * @return 0 if no coeffs were decoded
1364  * otherwise, the index of the last coeff decoded plus one
1365  */
1366 static av_always_inline
1368  uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1369  int i, const uint8_t *token_prob, const int16_t qmul[2],
1370  const uint8_t scan[16], int vp7)
1371 {
1372  VPXRangeCoder c = *r;
1373  goto skip_eob;
1374  do {
1375  int coeff;
1376 restart:
1377  if (!vpx_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
1378  break;
1379 
1380 skip_eob:
1381  if (!vpx_rac_get_prob_branchy(&c, token_prob[1])) { // DCT_0
1382  if (++i == 16)
1383  break; // invalid input; blocks should end with EOB
1384  token_prob = probs[i][0];
1385  if (vp7)
1386  goto restart;
1387  goto skip_eob;
1388  }
1389 
1390  if (!vpx_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
1391  coeff = 1;
1392  token_prob = probs[i + 1][1];
1393  } else {
1394  if (!vpx_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
1395  coeff = vpx_rac_get_prob_branchy(&c, token_prob[4]);
1396  if (coeff)
1397  coeff += vpx_rac_get_prob(&c, token_prob[5]);
1398  coeff += 2;
1399  } else {
1400  // DCT_CAT*
1401  if (!vpx_rac_get_prob_branchy(&c, token_prob[6])) {
1402  if (!vpx_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
1404  } else { // DCT_CAT2
1405  coeff = 7;
1406  coeff += vpx_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
1408  }
1409  } else { // DCT_CAT3 and up
1410  int a = vpx_rac_get_prob(&c, token_prob[8]);
1411  int b = vpx_rac_get_prob(&c, token_prob[9 + a]);
1412  int cat = (a << 1) + b;
1413  coeff = 3 + (8 << cat);
1415  }
1416  }
1417  token_prob = probs[i + 1][2];
1418  }
1419  block[scan[i]] = (vp89_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
1420  } while (++i < 16);
1421 
1422  *r = c;
1423  return i;
1424 }
1425 
1426 static av_always_inline
1427 int inter_predict_dc(int16_t block[16], int16_t pred[2])
1428 {
1429  int16_t dc = block[0];
1430  int ret = 0;
1431 
1432  if (pred[1] > 3) {
1433  dc += pred[0];
1434  ret = 1;
1435  }
1436 
1437  if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
1438  block[0] = pred[0] = dc;
1439  pred[1] = 0;
1440  } else {
1441  if (pred[0] == dc)
1442  pred[1]++;
1443  block[0] = pred[0] = dc;
1444  }
1445 
1446  return ret;
1447 }
1448 
1450  int16_t block[16],
1451  uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1452  int i, const uint8_t *token_prob,
1453  const int16_t qmul[2],
1454  const uint8_t scan[16])
1455 {
1456  return decode_block_coeffs_internal(r, block, probs, i,
1457  token_prob, qmul, scan, IS_VP7);
1458 }
1459 
1460 #ifndef vp8_decode_block_coeffs_internal
1462  int16_t block[16],
1463  uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1464  int i, const uint8_t *token_prob,
1465  const int16_t qmul[2])
1466 {
1467  return decode_block_coeffs_internal(r, block, probs, i,
1468  token_prob, qmul, ff_zigzag_scan, IS_VP8);
1469 }
1470 #endif
1471 
1472 /**
1473  * @param c arithmetic bitstream reader context
1474  * @param block destination for block coefficients
1475  * @param probs probabilities to use when reading trees from the bitstream
1476  * @param i initial coeff index, 0 unless a separate DC block is coded
1477  * @param zero_nhood the initial prediction context for number of surrounding
1478  * all-zero blocks (only left/top, so 0-2)
1479  * @param qmul array holding the dc/ac dequant factor at position 0/1
1480  * @param scan scan pattern (VP7 only)
1481  *
1482  * @return 0 if no coeffs were decoded
1483  * otherwise, the index of the last coeff decoded plus one
1484  */
1485 static av_always_inline
1487  uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1488  int i, int zero_nhood, const int16_t qmul[2],
1489  const uint8_t scan[16], int vp7)
1490 {
1491  const uint8_t *token_prob = probs[i][zero_nhood];
1492  if (!vpx_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
1493  return 0;
1494  return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
1495  token_prob, qmul, scan)
1497  token_prob, qmul);
1498 }
1499 
1500 static av_always_inline
1502  VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
1503  int is_vp7)
1504 {
1505  int i, x, y, luma_start = 0, luma_ctx = 3;
1506  int nnz_pred, nnz, nnz_total = 0;
1507  int segment = mb->segment;
1508  int block_dc = 0;
1509 
1510  if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
1511  nnz_pred = t_nnz[8] + l_nnz[8];
1512 
1513  // decode DC values and do hadamard
1514  nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
1515  nnz_pred, s->qmat[segment].luma_dc_qmul,
1516  ff_zigzag_scan, is_vp7);
1517  l_nnz[8] = t_nnz[8] = !!nnz;
1518 
1519  if (is_vp7 && mb->mode > MODE_I4x4) {
1520  nnz |= inter_predict_dc(td->block_dc,
1521  s->inter_dc_pred[mb->ref_frame - 1]);
1522  }
1523 
1524  if (nnz) {
1525  nnz_total += nnz;
1526  block_dc = 1;
1527  if (nnz == 1)
1528  s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
1529  else
1530  s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
1531  }
1532  luma_start = 1;
1533  luma_ctx = 0;
1534  }
1535 
1536  // luma blocks
1537  for (y = 0; y < 4; y++)
1538  for (x = 0; x < 4; x++) {
1539  nnz_pred = l_nnz[y] + t_nnz[x];
1540  nnz = decode_block_coeffs(c, td->block[y][x],
1541  s->prob->token[luma_ctx],
1542  luma_start, nnz_pred,
1543  s->qmat[segment].luma_qmul,
1544  s->prob[0].scan, is_vp7);
1545  /* nnz+block_dc may be one more than the actual last index,
1546  * but we don't care */
1547  td->non_zero_count_cache[y][x] = nnz + block_dc;
1548  t_nnz[x] = l_nnz[y] = !!nnz;
1549  nnz_total += nnz;
1550  }
1551 
1552  // chroma blocks
1553  // TODO: what to do about dimensions? 2nd dim for luma is x,
1554  // but for chroma it's (y<<1)|x
1555  for (i = 4; i < 6; i++)
1556  for (y = 0; y < 2; y++)
1557  for (x = 0; x < 2; x++) {
1558  nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];
1559  nnz = decode_block_coeffs(c, td->block[i][(y << 1) + x],
1560  s->prob->token[2], 0, nnz_pred,
1561  s->qmat[segment].chroma_qmul,
1562  s->prob[0].scan, is_vp7);
1563  td->non_zero_count_cache[i][(y << 1) + x] = nnz;
1564  t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;
1565  nnz_total += nnz;
1566  }
1567 
1568  // if there were no coded coeffs despite the macroblock not being marked skip,
1569  // we MUST not do the inner loop filter and should not do IDCT
1570  // Since skip isn't used for bitstream prediction, just manually set it.
1571  if (!nnz_total)
1572  mb->skip = 1;
1573 }
1574 
1575 static av_always_inline
1576 void backup_mb_border(uint8_t *top_border, const uint8_t *src_y,
1577  const uint8_t *src_cb, const uint8_t *src_cr,
1578  ptrdiff_t linesize, ptrdiff_t uvlinesize, int simple)
1579 {
1580  AV_COPY128(top_border, src_y + 15 * linesize);
1581  if (!simple) {
1582  AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
1583  AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
1584  }
1585 }
1586 
1587 static av_always_inline
1588 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
1589  uint8_t *src_cr, ptrdiff_t linesize, ptrdiff_t uvlinesize, int mb_x,
1590  int mb_y, int mb_width, int simple, int xchg)
1591 {
1592  uint8_t *top_border_m1 = top_border - 32; // for TL prediction
1593  src_y -= linesize;
1594  src_cb -= uvlinesize;
1595  src_cr -= uvlinesize;
1596 
1597 #define XCHG(a, b, xchg) \
1598  do { \
1599  if (xchg) \
1600  AV_SWAP64(b, a); \
1601  else \
1602  AV_COPY64(b, a); \
1603  } while (0)
1604 
1605  XCHG(top_border_m1 + 8, src_y - 8, xchg);
1606  XCHG(top_border, src_y, xchg);
1607  XCHG(top_border + 8, src_y + 8, 1);
1608  if (mb_x < mb_width - 1)
1609  XCHG(top_border + 32, src_y + 16, 1);
1610 
1611  // only copy chroma for normal loop filter
1612  // or to initialize the top row to 127
1613  if (!simple || !mb_y) {
1614  XCHG(top_border_m1 + 16, src_cb - 8, xchg);
1615  XCHG(top_border_m1 + 24, src_cr - 8, xchg);
1616  XCHG(top_border + 16, src_cb, 1);
1617  XCHG(top_border + 24, src_cr, 1);
1618  }
1619 }
1620 
1621 static av_always_inline
1622 int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
1623 {
1624  if (!mb_x)
1625  return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
1626  else
1627  return mb_y ? mode : LEFT_DC_PRED8x8;
1628 }
1629 
1630 static av_always_inline
1631 int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
1632 {
1633  if (!mb_x)
1634  return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
1635  else
1636  return mb_y ? mode : HOR_PRED8x8;
1637 }
1638 
1639 static av_always_inline
1640 int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
1641 {
1642  switch (mode) {
1643  case DC_PRED8x8:
1644  return check_dc_pred8x8_mode(mode, mb_x, mb_y);
1645  case VERT_PRED8x8:
1646  return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
1647  case HOR_PRED8x8:
1648  return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
1649  case PLANE_PRED8x8: /* TM */
1650  return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
1651  }
1652  return mode;
1653 }
1654 
1655 static av_always_inline
1656 int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
1657 {
1658  if (!mb_x) {
1659  return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
1660  } else {
1661  return mb_y ? mode : HOR_VP8_PRED;
1662  }
1663 }
1664 
1665 static av_always_inline
1666 int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
1667  int *copy_buf, int vp7)
1668 {
1669  switch (mode) {
1670  case VERT_PRED:
1671  if (!mb_x && mb_y) {
1672  *copy_buf = 1;
1673  return mode;
1674  }
1675  /* fall-through */
1676  case DIAG_DOWN_LEFT_PRED:
1677  case VERT_LEFT_PRED:
1678  return !mb_y ? (vp7 ? DC_128_PRED : DC_127_PRED) : mode;
1679  case HOR_PRED:
1680  if (!mb_y) {
1681  *copy_buf = 1;
1682  return mode;
1683  }
1684  /* fall-through */
1685  case HOR_UP_PRED:
1686  return !mb_x ? (vp7 ? DC_128_PRED : DC_129_PRED) : mode;
1687  case TM_VP8_PRED:
1688  return check_tm_pred4x4_mode(mode, mb_x, mb_y, vp7);
1689  case DC_PRED: /* 4x4 DC doesn't use the same "H.264-style" exceptions
1690  * as 16x16/8x8 DC */
1691  case DIAG_DOWN_RIGHT_PRED:
1692  case VERT_RIGHT_PRED:
1693  case HOR_DOWN_PRED:
1694  if (!mb_y || !mb_x)
1695  *copy_buf = 1;
1696  return mode;
1697  }
1698  return mode;
1699 }
1700 
1701 static av_always_inline
1702 void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
1703  VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
1704 {
1705  int x, y, mode, nnz;
1706  uint32_t tr;
1707 
1708  /* for the first row, we need to run xchg_mb_border to init the top edge
1709  * to 127 otherwise, skip it if we aren't going to deblock */
1710  if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1711  xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1712  s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1713  s->filter.simple, 1);
1714 
1715  if (mb->mode < MODE_I4x4) {
1716  mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
1717  s->hpc.pred16x16[mode](dst[0], s->linesize);
1718  } else {
1719  uint8_t *ptr = dst[0];
1720  const uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1721  const uint8_t lo = is_vp7 ? 128 : 127;
1722  const uint8_t hi = is_vp7 ? 128 : 129;
1723  const uint8_t tr_top[4] = { lo, lo, lo, lo };
1724 
1725  // all blocks on the right edge of the macroblock use bottom edge
1726  // the top macroblock for their topright edge
1727  const uint8_t *tr_right = ptr - s->linesize + 16;
1728 
1729  // if we're on the right edge of the frame, said edge is extended
1730  // from the top macroblock
1731  if (mb_y && mb_x == s->mb_width - 1) {
1732  tr = tr_right[-1] * 0x01010101u;
1733  tr_right = (uint8_t *) &tr;
1734  }
1735 
1736  if (mb->skip)
1737  AV_ZERO128(td->non_zero_count_cache);
1738 
1739  for (y = 0; y < 4; y++) {
1740  const uint8_t *topright = ptr + 4 - s->linesize;
1741  for (x = 0; x < 4; x++) {
1742  int copy = 0;
1743  ptrdiff_t linesize = s->linesize;
1744  uint8_t *dst = ptr + 4 * x;
1745  LOCAL_ALIGNED(4, uint8_t, copy_dst, [5 * 8]);
1746 
1747  if ((y == 0 || x == 3) && mb_y == 0) {
1748  topright = tr_top;
1749  } else if (x == 3)
1750  topright = tr_right;
1751 
1752  mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
1753  mb_y + y, &copy, is_vp7);
1754  if (copy) {
1755  dst = copy_dst + 12;
1756  linesize = 8;
1757  if (!(mb_y + y)) {
1758  copy_dst[3] = lo;
1759  AV_WN32A(copy_dst + 4, lo * 0x01010101U);
1760  } else {
1761  AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
1762  if (!(mb_x + x)) {
1763  copy_dst[3] = hi;
1764  } else {
1765  copy_dst[3] = ptr[4 * x - s->linesize - 1];
1766  }
1767  }
1768  if (!(mb_x + x)) {
1769  copy_dst[11] =
1770  copy_dst[19] =
1771  copy_dst[27] =
1772  copy_dst[35] = hi;
1773  } else {
1774  copy_dst[11] = ptr[4 * x - 1];
1775  copy_dst[19] = ptr[4 * x + s->linesize - 1];
1776  copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
1777  copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
1778  }
1779  }
1780  s->hpc.pred4x4[mode](dst, topright, linesize);
1781  if (copy) {
1782  AV_COPY32(ptr + 4 * x, copy_dst + 12);
1783  AV_COPY32(ptr + 4 * x + s->linesize, copy_dst + 20);
1784  AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
1785  AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
1786  }
1787 
1788  nnz = td->non_zero_count_cache[y][x];
1789  if (nnz) {
1790  if (nnz == 1)
1791  s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
1792  td->block[y][x], s->linesize);
1793  else
1794  s->vp8dsp.vp8_idct_add(ptr + 4 * x,
1795  td->block[y][x], s->linesize);
1796  }
1797  topright += 4;
1798  }
1799 
1800  ptr += 4 * s->linesize;
1801  intra4x4 += 4;
1802  }
1803  }
1804 
1805  mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
1806  mb_x, mb_y, is_vp7);
1807  s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
1808  s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
1809 
1810  if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1811  xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1812  s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1813  s->filter.simple, 0);
1814 }
1815 
1816 static const uint8_t subpel_idx[3][8] = {
1817  { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
1818  // also function pointer index
1819  { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
1820  { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
1821 };
1822 
1823 /**
1824  * luma MC function
1825  *
1826  * @param s VP8 decoding context
1827  * @param dst target buffer for block data at block position
1828  * @param ref reference picture buffer at origin (0, 0)
1829  * @param mv motion vector (relative to block position) to get pixel data from
1830  * @param x_off horizontal position of block from origin (0, 0)
1831  * @param y_off vertical position of block from origin (0, 0)
1832  * @param block_w width of block (16, 8 or 4)
1833  * @param block_h height of block (always same as block_w)
1834  * @param width width of src/dst plane data
1835  * @param height height of src/dst plane data
1836  * @param linesize size of a single line of plane data, including padding
1837  * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1838  */
1839 static av_always_inline
1840 void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
1841  const ThreadFrame *ref, const VP8mv *mv,
1842  int x_off, int y_off, int block_w, int block_h,
1843  int width, int height, ptrdiff_t linesize,
1844  vp8_mc_func mc_func[3][3])
1845 {
1846  const uint8_t *src = ref->f->data[0];
1847 
1848  if (AV_RN32A(mv)) {
1849  ptrdiff_t src_linesize = linesize;
1850 
1851  int mx = (mv->x * 2) & 7, mx_idx = subpel_idx[0][mx];
1852  int my = (mv->y * 2) & 7, my_idx = subpel_idx[0][my];
1853 
1854  x_off += mv->x >> 2;
1855  y_off += mv->y >> 2;
1856 
1857  // edge emulation
1858  ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
1859  src += y_off * linesize + x_off;
1860  if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1861  y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1862  s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1863  src - my_idx * linesize - mx_idx,
1864  EDGE_EMU_LINESIZE, linesize,
1865  block_w + subpel_idx[1][mx],
1866  block_h + subpel_idx[1][my],
1867  x_off - mx_idx, y_off - my_idx,
1868  width, height);
1869  src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1870  src_linesize = EDGE_EMU_LINESIZE;
1871  }
1872  mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
1873  } else {
1874  ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
1875  mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
1876  linesize, block_h, 0, 0);
1877  }
1878 }
1879 
1880 /**
1881  * chroma MC function
1882  *
1883  * @param s VP8 decoding context
1884  * @param dst1 target buffer for block data at block position (U plane)
1885  * @param dst2 target buffer for block data at block position (V plane)
1886  * @param ref reference picture buffer at origin (0, 0)
1887  * @param mv motion vector (relative to block position) to get pixel data from
1888  * @param x_off horizontal position of block from origin (0, 0)
1889  * @param y_off vertical position of block from origin (0, 0)
1890  * @param block_w width of block (16, 8 or 4)
1891  * @param block_h height of block (always same as block_w)
1892  * @param width width of src/dst plane data
1893  * @param height height of src/dst plane data
1894  * @param linesize size of a single line of plane data, including padding
1895  * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1896  */
1897 static av_always_inline
1898 void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
1899  uint8_t *dst2, const ThreadFrame *ref, const VP8mv *mv,
1900  int x_off, int y_off, int block_w, int block_h,
1901  int width, int height, ptrdiff_t linesize,
1902  vp8_mc_func mc_func[3][3])
1903 {
1904  const uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
1905 
1906  if (AV_RN32A(mv)) {
1907  int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
1908  int my = mv->y & 7, my_idx = subpel_idx[0][my];
1909 
1910  x_off += mv->x >> 3;
1911  y_off += mv->y >> 3;
1912 
1913  // edge emulation
1914  src1 += y_off * linesize + x_off;
1915  src2 += y_off * linesize + x_off;
1916  ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
1917  if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1918  y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1919  s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1920  src1 - my_idx * linesize - mx_idx,
1921  EDGE_EMU_LINESIZE, linesize,
1922  block_w + subpel_idx[1][mx],
1923  block_h + subpel_idx[1][my],
1924  x_off - mx_idx, y_off - my_idx, width, height);
1925  src1 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1926  mc_func[my_idx][mx_idx](dst1, linesize, src1, EDGE_EMU_LINESIZE, block_h, mx, my);
1927 
1928  s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1929  src2 - my_idx * linesize - mx_idx,
1930  EDGE_EMU_LINESIZE, linesize,
1931  block_w + subpel_idx[1][mx],
1932  block_h + subpel_idx[1][my],
1933  x_off - mx_idx, y_off - my_idx, width, height);
1934  src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1935  mc_func[my_idx][mx_idx](dst2, linesize, src2, EDGE_EMU_LINESIZE, block_h, mx, my);
1936  } else {
1937  mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
1938  mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
1939  }
1940  } else {
1941  ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
1942  mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1943  mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1944  }
1945 }
1946 
1947 static av_always_inline
1948 void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
1949  const ThreadFrame *ref_frame, int x_off, int y_off,
1950  int bx_off, int by_off, int block_w, int block_h,
1951  int width, int height, const VP8mv *mv)
1952 {
1953  VP8mv uvmv = *mv;
1954 
1955  /* Y */
1956  vp8_mc_luma(s, td, dst[0] + by_off * s->linesize + bx_off,
1957  ref_frame, mv, x_off + bx_off, y_off + by_off,
1958  block_w, block_h, width, height, s->linesize,
1959  s->put_pixels_tab[block_w == 8]);
1960 
1961  /* U/V */
1962  if (s->profile == 3) {
1963  /* this block only applies VP8; it is safe to check
1964  * only the profile, as VP7 profile <= 1 */
1965  uvmv.x &= ~7;
1966  uvmv.y &= ~7;
1967  }
1968  x_off >>= 1;
1969  y_off >>= 1;
1970  bx_off >>= 1;
1971  by_off >>= 1;
1972  width >>= 1;
1973  height >>= 1;
1974  block_w >>= 1;
1975  block_h >>= 1;
1976  vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
1977  dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
1978  &uvmv, x_off + bx_off, y_off + by_off,
1979  block_w, block_h, width, height, s->uvlinesize,
1980  s->put_pixels_tab[1 + (block_w == 4)]);
1981 }
1982 
1983 /* Fetch pixels for estimated mv 4 macroblocks ahead.
1984  * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1985 static av_always_inline
1987  int mb_x, int mb_y, int mb_xy, int ref)
1988 {
1989  /* Don't prefetch refs that haven't been used very often this frame. */
1990  if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
1991  int x_off = mb_x << 4, y_off = mb_y << 4;
1992  int mx = (mb->mv.x >> 2) + x_off + 8;
1993  int my = (mb->mv.y >> 2) + y_off;
1994  uint8_t **src = s->framep[ref]->tf.f->data;
1995  int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
1996  /* For threading, a ff_thread_await_progress here might be useful, but
1997  * it actually slows down the decoder. Since a bad prefetch doesn't
1998  * generate bad decoder output, we don't run it here. */
1999  s->vdsp.prefetch(src[0] + off, s->linesize, 4);
2000  off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
2001  s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
2002  }
2003 }
2004 
2005 /**
2006  * Apply motion vectors to prediction buffer, chapter 18.
2007  */
2008 static av_always_inline
2009 void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
2010  VP8Macroblock *mb, int mb_x, int mb_y)
2011 {
2012  int x_off = mb_x << 4, y_off = mb_y << 4;
2013  int width = 16 * s->mb_width, height = 16 * s->mb_height;
2014  const ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
2015  const VP8mv *bmv = mb->bmv;
2016 
2017  switch (mb->partitioning) {
2018  case VP8_SPLITMVMODE_NONE:
2019  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2020  0, 0, 16, 16, width, height, &mb->mv);
2021  break;
2022  case VP8_SPLITMVMODE_4x4: {
2023  int x, y;
2024  VP8mv uvmv;
2025 
2026  /* Y */
2027  for (y = 0; y < 4; y++) {
2028  for (x = 0; x < 4; x++) {
2029  vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
2030  ref, &bmv[4 * y + x],
2031  4 * x + x_off, 4 * y + y_off, 4, 4,
2032  width, height, s->linesize,
2033  s->put_pixels_tab[2]);
2034  }
2035  }
2036 
2037  /* U/V */
2038  x_off >>= 1;
2039  y_off >>= 1;
2040  width >>= 1;
2041  height >>= 1;
2042  for (y = 0; y < 2; y++) {
2043  for (x = 0; x < 2; x++) {
2044  uvmv.x = mb->bmv[2 * y * 4 + 2 * x ].x +
2045  mb->bmv[2 * y * 4 + 2 * x + 1].x +
2046  mb->bmv[(2 * y + 1) * 4 + 2 * x ].x +
2047  mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
2048  uvmv.y = mb->bmv[2 * y * 4 + 2 * x ].y +
2049  mb->bmv[2 * y * 4 + 2 * x + 1].y +
2050  mb->bmv[(2 * y + 1) * 4 + 2 * x ].y +
2051  mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
2052  uvmv.x = (uvmv.x + 2 + FF_SIGNBIT(uvmv.x)) >> 2;
2053  uvmv.y = (uvmv.y + 2 + FF_SIGNBIT(uvmv.y)) >> 2;
2054  if (s->profile == 3) {
2055  uvmv.x &= ~7;
2056  uvmv.y &= ~7;
2057  }
2058  vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
2059  dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
2060  &uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
2061  width, height, s->uvlinesize,
2062  s->put_pixels_tab[2]);
2063  }
2064  }
2065  break;
2066  }
2067  case VP8_SPLITMVMODE_16x8:
2068  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2069  0, 0, 16, 8, width, height, &bmv[0]);
2070  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2071  0, 8, 16, 8, width, height, &bmv[1]);
2072  break;
2073  case VP8_SPLITMVMODE_8x16:
2074  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2075  0, 0, 8, 16, width, height, &bmv[0]);
2076  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2077  8, 0, 8, 16, width, height, &bmv[1]);
2078  break;
2079  case VP8_SPLITMVMODE_8x8:
2080  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2081  0, 0, 8, 8, width, height, &bmv[0]);
2082  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2083  8, 0, 8, 8, width, height, &bmv[1]);
2084  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2085  0, 8, 8, 8, width, height, &bmv[2]);
2086  vp8_mc_part(s, td, dst, ref, x_off, y_off,
2087  8, 8, 8, 8, width, height, &bmv[3]);
2088  break;
2089  }
2090 }
2091 
2092 static av_always_inline
2093 void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3],
2094  const VP8Macroblock *mb)
2095 {
2096  int x, y, ch;
2097 
2098  if (mb->mode != MODE_I4x4) {
2099  uint8_t *y_dst = dst[0];
2100  for (y = 0; y < 4; y++) {
2101  uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
2102  if (nnz4) {
2103  if (nnz4 & ~0x01010101) {
2104  for (x = 0; x < 4; x++) {
2105  if ((uint8_t) nnz4 == 1)
2106  s->vp8dsp.vp8_idct_dc_add(y_dst + 4 * x,
2107  td->block[y][x],
2108  s->linesize);
2109  else if ((uint8_t) nnz4 > 1)
2110  s->vp8dsp.vp8_idct_add(y_dst + 4 * x,
2111  td->block[y][x],
2112  s->linesize);
2113  nnz4 >>= 8;
2114  if (!nnz4)
2115  break;
2116  }
2117  } else {
2118  s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
2119  }
2120  }
2121  y_dst += 4 * s->linesize;
2122  }
2123  }
2124 
2125  for (ch = 0; ch < 2; ch++) {
2126  uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
2127  if (nnz4) {
2128  uint8_t *ch_dst = dst[1 + ch];
2129  if (nnz4 & ~0x01010101) {
2130  for (y = 0; y < 2; y++) {
2131  for (x = 0; x < 2; x++) {
2132  if ((uint8_t) nnz4 == 1)
2133  s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
2134  td->block[4 + ch][(y << 1) + x],
2135  s->uvlinesize);
2136  else if ((uint8_t) nnz4 > 1)
2137  s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
2138  td->block[4 + ch][(y << 1) + x],
2139  s->uvlinesize);
2140  nnz4 >>= 8;
2141  if (!nnz4)
2142  goto chroma_idct_end;
2143  }
2144  ch_dst += 4 * s->uvlinesize;
2145  }
2146  } else {
2147  s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
2148  }
2149  }
2150 chroma_idct_end:
2151  ;
2152  }
2153 }
2154 
2155 static av_always_inline
2157  VP8FilterStrength *f, int is_vp7)
2158 {
2159  int interior_limit, filter_level;
2160 
2161  if (s->segmentation.enabled) {
2162  filter_level = s->segmentation.filter_level[mb->segment];
2163  if (!s->segmentation.absolute_vals)
2164  filter_level += s->filter.level;
2165  } else
2166  filter_level = s->filter.level;
2167 
2168  if (s->lf_delta.enabled) {
2169  filter_level += s->lf_delta.ref[mb->ref_frame];
2170  filter_level += s->lf_delta.mode[mb->mode];
2171  }
2172 
2173  filter_level = av_clip_uintp2(filter_level, 6);
2174 
2175  interior_limit = filter_level;
2176  if (s->filter.sharpness) {
2177  interior_limit >>= (s->filter.sharpness + 3) >> 2;
2178  interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
2179  }
2180  interior_limit = FFMAX(interior_limit, 1);
2181 
2182  f->filter_level = filter_level;
2183  f->inner_limit = interior_limit;
2184  f->inner_filter = is_vp7 || !mb->skip || mb->mode == MODE_I4x4 ||
2185  mb->mode == VP8_MVMODE_SPLIT;
2186 }
2187 
2188 static av_always_inline
2189 void filter_mb(const VP8Context *s, uint8_t *const dst[3], const VP8FilterStrength *f,
2190  int mb_x, int mb_y, int is_vp7)
2191 {
2192  int mbedge_lim, bedge_lim_y, bedge_lim_uv, hev_thresh;
2193  int filter_level = f->filter_level;
2194  int inner_limit = f->inner_limit;
2195  int inner_filter = f->inner_filter;
2196  ptrdiff_t linesize = s->linesize;
2197  ptrdiff_t uvlinesize = s->uvlinesize;
2198  static const uint8_t hev_thresh_lut[2][64] = {
2199  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
2200  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2201  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2202  3, 3, 3, 3 },
2203  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
2204  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2205  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2206  2, 2, 2, 2 }
2207  };
2208 
2209  if (!filter_level)
2210  return;
2211 
2212  if (is_vp7) {
2213  bedge_lim_y = filter_level;
2214  bedge_lim_uv = filter_level * 2;
2215  mbedge_lim = filter_level + 2;
2216  } else {
2217  bedge_lim_y =
2218  bedge_lim_uv = filter_level * 2 + inner_limit;
2219  mbedge_lim = bedge_lim_y + 4;
2220  }
2221 
2222  hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
2223 
2224  if (mb_x) {
2225  s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
2226  mbedge_lim, inner_limit, hev_thresh);
2227  s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
2228  mbedge_lim, inner_limit, hev_thresh);
2229  }
2230 
2231 #define H_LOOP_FILTER_16Y_INNER(cond) \
2232  if (cond && inner_filter) { \
2233  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 4, linesize, \
2234  bedge_lim_y, inner_limit, \
2235  hev_thresh); \
2236  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 8, linesize, \
2237  bedge_lim_y, inner_limit, \
2238  hev_thresh); \
2239  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize, \
2240  bedge_lim_y, inner_limit, \
2241  hev_thresh); \
2242  s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4, \
2243  uvlinesize, bedge_lim_uv, \
2244  inner_limit, hev_thresh); \
2245  }
2246 
2247  H_LOOP_FILTER_16Y_INNER(!is_vp7)
2248 
2249  if (mb_y) {
2250  s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
2251  mbedge_lim, inner_limit, hev_thresh);
2252  s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
2253  mbedge_lim, inner_limit, hev_thresh);
2254  }
2255 
2256  if (inner_filter) {
2257  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 4 * linesize,
2258  linesize, bedge_lim_y,
2259  inner_limit, hev_thresh);
2260  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 8 * linesize,
2261  linesize, bedge_lim_y,
2262  inner_limit, hev_thresh);
2263  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
2264  linesize, bedge_lim_y,
2265  inner_limit, hev_thresh);
2266  s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
2267  dst[2] + 4 * uvlinesize,
2268  uvlinesize, bedge_lim_uv,
2269  inner_limit, hev_thresh);
2270  }
2271 
2272  H_LOOP_FILTER_16Y_INNER(is_vp7)
2273 }
2274 
2275 static av_always_inline
2276 void filter_mb_simple(const VP8Context *s, uint8_t *dst, const VP8FilterStrength *f,
2277  int mb_x, int mb_y)
2278 {
2279  int mbedge_lim, bedge_lim;
2280  int filter_level = f->filter_level;
2281  int inner_limit = f->inner_limit;
2282  int inner_filter = f->inner_filter;
2283  ptrdiff_t linesize = s->linesize;
2284 
2285  if (!filter_level)
2286  return;
2287 
2288  bedge_lim = 2 * filter_level + inner_limit;
2289  mbedge_lim = bedge_lim + 4;
2290 
2291  if (mb_x)
2292  s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
2293  if (inner_filter) {
2294  s->vp8dsp.vp8_h_loop_filter_simple(dst + 4, linesize, bedge_lim);
2295  s->vp8dsp.vp8_h_loop_filter_simple(dst + 8, linesize, bedge_lim);
2296  s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
2297  }
2298 
2299  if (mb_y)
2300  s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
2301  if (inner_filter) {
2302  s->vp8dsp.vp8_v_loop_filter_simple(dst + 4 * linesize, linesize, bedge_lim);
2303  s->vp8dsp.vp8_v_loop_filter_simple(dst + 8 * linesize, linesize, bedge_lim);
2304  s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
2305  }
2306 }
2307 
2308 #define MARGIN (16 << 2)
2309 static av_always_inline
2311  const VP8Frame *prev_frame, int is_vp7)
2312 {
2313  VP8Context *s = avctx->priv_data;
2314  int mb_x, mb_y;
2315 
2316  s->mv_bounds.mv_min.y = -MARGIN;
2317  s->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
2318  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
2319  VP8Macroblock *mb = s->macroblocks_base +
2320  ((s->mb_width + 1) * (mb_y + 1) + 1);
2321  int mb_xy = mb_y * s->mb_width;
2322 
2323  AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
2324 
2325  s->mv_bounds.mv_min.x = -MARGIN;
2326  s->mv_bounds.mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
2327 
2328  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
2329  if (vpx_rac_is_end(&s->c)) {
2330  return AVERROR_INVALIDDATA;
2331  }
2332  if (mb_y == 0)
2333  AV_WN32A((mb - s->mb_width - 1)->intra4x4_pred_mode_top,
2334  DC_PRED * 0x01010101);
2335  decode_mb_mode(s, &s->mv_bounds, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
2336  prev_frame && prev_frame->seg_map ?
2337  prev_frame->seg_map->data + mb_xy : NULL, 1, is_vp7);
2338  s->mv_bounds.mv_min.x -= 64;
2339  s->mv_bounds.mv_max.x -= 64;
2340  }
2341  s->mv_bounds.mv_min.y -= 64;
2342  s->mv_bounds.mv_max.y -= 64;
2343  }
2344  return 0;
2345 }
2346 
2347 static int vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2348  const VP8Frame *prev_frame)
2349 {
2350  return vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
2351 }
2352 
2353 static int vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2354  const VP8Frame *prev_frame)
2355 {
2356  return vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
2357 }
2358 
2359 #if HAVE_THREADS
2360 #define check_thread_pos(td, otd, mb_x_check, mb_y_check) \
2361  do { \
2362  int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \
2363  if (atomic_load(&otd->thread_mb_pos) < tmp) { \
2364  pthread_mutex_lock(&otd->lock); \
2365  atomic_store(&td->wait_mb_pos, tmp); \
2366  do { \
2367  if (atomic_load(&otd->thread_mb_pos) >= tmp) \
2368  break; \
2369  pthread_cond_wait(&otd->cond, &otd->lock); \
2370  } while (1); \
2371  atomic_store(&td->wait_mb_pos, INT_MAX); \
2372  pthread_mutex_unlock(&otd->lock); \
2373  } \
2374  } while (0)
2375 
2376 #define update_pos(td, mb_y, mb_x) \
2377  do { \
2378  int pos = (mb_y << 16) | (mb_x & 0xFFFF); \
2379  int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
2380  (num_jobs > 1); \
2381  int is_null = !next_td || !prev_td; \
2382  int pos_check = (is_null) ? 1 : \
2383  (next_td != td && pos >= atomic_load(&next_td->wait_mb_pos)) || \
2384  (prev_td != td && pos >= atomic_load(&prev_td->wait_mb_pos)); \
2385  atomic_store(&td->thread_mb_pos, pos); \
2386  if (sliced_threading && pos_check) { \
2387  pthread_mutex_lock(&td->lock); \
2388  pthread_cond_broadcast(&td->cond); \
2389  pthread_mutex_unlock(&td->lock); \
2390  } \
2391  } while (0)
2392 #else
2393 #define check_thread_pos(td, otd, mb_x_check, mb_y_check) while(0)
2394 #define update_pos(td, mb_y, mb_x) while(0)
2395 #endif
2396 
2398  int jobnr, int threadnr, int is_vp7)
2399 {
2400  VP8Context *s = avctx->priv_data;
2401  VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
2402  int mb_y = atomic_load(&td->thread_mb_pos) >> 16;
2403  int mb_x, mb_xy = mb_y * s->mb_width;
2404  int num_jobs = s->num_jobs;
2405  const VP8Frame *prev_frame = s->prev_frame;
2406  VP8Frame *curframe = s->curframe;
2407  VPXRangeCoder *coeff_c = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
2408 
2409  VP8Macroblock *mb;
2410  uint8_t *dst[3] = {
2411  curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
2412  curframe->tf.f->data[1] + 8 * mb_y * s->uvlinesize,
2413  curframe->tf.f->data[2] + 8 * mb_y * s->uvlinesize
2414  };
2415 
2416  if (vpx_rac_is_end(&s->c))
2417  return AVERROR_INVALIDDATA;
2418 
2419  if (mb_y == 0)
2420  prev_td = td;
2421  else
2422  prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2423  if (mb_y == s->mb_height - 1)
2424  next_td = td;
2425  else
2426  next_td = &s->thread_data[(jobnr + 1) % num_jobs];
2427  if (s->mb_layout == 1)
2428  mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
2429  else {
2430  // Make sure the previous frame has read its segmentation map,
2431  // if we re-use the same map.
2432  if (prev_frame && s->segmentation.enabled &&
2433  !s->segmentation.update_map)
2434  ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
2435  mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
2436  memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
2437  AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
2438  }
2439 
2440  if (!is_vp7 || mb_y == 0)
2441  memset(td->left_nnz, 0, sizeof(td->left_nnz));
2442 
2443  td->mv_bounds.mv_min.x = -MARGIN;
2444  td->mv_bounds.mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
2445 
2446  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
2447  if (vpx_rac_is_end(&s->c))
2448  return AVERROR_INVALIDDATA;
2449  // Wait for previous thread to read mb_x+2, and reach mb_y-1.
2450  if (prev_td != td) {
2451  if (threadnr != 0) {
2452  check_thread_pos(td, prev_td,
2453  mb_x + (is_vp7 ? 2 : 1),
2454  mb_y - (is_vp7 ? 2 : 1));
2455  } else {
2456  check_thread_pos(td, prev_td,
2457  mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
2458  mb_y - (is_vp7 ? 2 : 1));
2459  }
2460  }
2461 
2462  s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
2463  s->linesize, 4);
2464  s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
2465  dst[2] - dst[1], 2);
2466 
2467  if (!s->mb_layout)
2468  decode_mb_mode(s, &td->mv_bounds, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
2469  prev_frame && prev_frame->seg_map ?
2470  prev_frame->seg_map->data + mb_xy : NULL, 0, is_vp7);
2471 
2472  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_PREVIOUS);
2473 
2474  if (!mb->skip) {
2475  if (vpx_rac_is_end(coeff_c))
2476  return AVERROR_INVALIDDATA;
2477  decode_mb_coeffs(s, td, coeff_c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
2478  }
2479 
2480  if (mb->mode <= MODE_I4x4)
2481  intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
2482  else
2483  inter_predict(s, td, dst, mb, mb_x, mb_y);
2484 
2485  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_GOLDEN);
2486 
2487  if (!mb->skip) {
2488  idct_mb(s, td, dst, mb);
2489  } else {
2490  AV_ZERO64(td->left_nnz);
2491  AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
2492 
2493  /* Reset DC block predictors if they would exist
2494  * if the mb had coefficients */
2495  if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
2496  td->left_nnz[8] = 0;
2497  s->top_nnz[mb_x][8] = 0;
2498  }
2499  }
2500 
2501  if (s->deblock_filter)
2502  filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
2503 
2504  if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
2505  if (s->filter.simple)
2506  backup_mb_border(s->top_border[mb_x + 1], dst[0],
2507  NULL, NULL, s->linesize, 0, 1);
2508  else
2509  backup_mb_border(s->top_border[mb_x + 1], dst[0],
2510  dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2511  }
2512 
2513  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP8_FRAME_ALTREF);
2514 
2515  dst[0] += 16;
2516  dst[1] += 8;
2517  dst[2] += 8;
2518  td->mv_bounds.mv_min.x -= 64;
2519  td->mv_bounds.mv_max.x -= 64;
2520 
2521  if (mb_x == s->mb_width + 1) {
2522  update_pos(td, mb_y, s->mb_width + 3);
2523  } else {
2524  update_pos(td, mb_y, mb_x);
2525  }
2526  }
2527  return 0;
2528 }
2529 
2530 static int vp7_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
2531  int jobnr, int threadnr)
2532 {
2533  return decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 1);
2534 }
2535 
2536 static int vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
2537  int jobnr, int threadnr)
2538 {
2539  return decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 0);
2540 }
2541 
2542 static av_always_inline void filter_mb_row(AVCodecContext *avctx, void *tdata,
2543  int jobnr, int threadnr, int is_vp7)
2544 {
2545  VP8Context *s = avctx->priv_data;
2546  VP8ThreadData *td = &s->thread_data[threadnr];
2547  int mb_x, mb_y = atomic_load(&td->thread_mb_pos) >> 16, num_jobs = s->num_jobs;
2548  AVFrame *curframe = s->curframe->tf.f;
2549  VP8Macroblock *mb;
2550  VP8ThreadData *prev_td, *next_td;
2551  uint8_t *dst[3] = {
2552  curframe->data[0] + 16 * mb_y * s->linesize,
2553  curframe->data[1] + 8 * mb_y * s->uvlinesize,
2554  curframe->data[2] + 8 * mb_y * s->uvlinesize
2555  };
2556 
2557  if (s->mb_layout == 1)
2558  mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
2559  else
2560  mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
2561 
2562  if (mb_y == 0)
2563  prev_td = td;
2564  else
2565  prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2566  if (mb_y == s->mb_height - 1)
2567  next_td = td;
2568  else
2569  next_td = &s->thread_data[(jobnr + 1) % num_jobs];
2570 
2571  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
2572  const VP8FilterStrength *f = &td->filter_strength[mb_x];
2573  if (prev_td != td)
2574  check_thread_pos(td, prev_td,
2575  (mb_x + 1) + (s->mb_width + 3), mb_y - 1);
2576  if (next_td != td)
2577  if (next_td != &s->thread_data[0])
2578  check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
2579 
2580  if (num_jobs == 1) {
2581  if (s->filter.simple)
2582  backup_mb_border(s->top_border[mb_x + 1], dst[0],
2583  NULL, NULL, s->linesize, 0, 1);
2584  else
2585  backup_mb_border(s->top_border[mb_x + 1], dst[0],
2586  dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2587  }
2588 
2589  if (s->filter.simple)
2590  filter_mb_simple(s, dst[0], f, mb_x, mb_y);
2591  else
2592  filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
2593  dst[0] += 16;
2594  dst[1] += 8;
2595  dst[2] += 8;
2596 
2597  update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
2598  }
2599 }
2600 
2601 static void vp7_filter_mb_row(AVCodecContext *avctx, void *tdata,
2602  int jobnr, int threadnr)
2603 {
2604  filter_mb_row(avctx, tdata, jobnr, threadnr, 1);
2605 }
2606 
2607 static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
2608  int jobnr, int threadnr)
2609 {
2610  filter_mb_row(avctx, tdata, jobnr, threadnr, 0);
2611 }
2612 
2613 static av_always_inline
2614 int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
2615  int threadnr, int is_vp7)
2616 {
2617  const VP8Context *s = avctx->priv_data;
2618  VP8ThreadData *td = &s->thread_data[jobnr];
2619  VP8ThreadData *next_td = NULL, *prev_td = NULL;
2620  VP8Frame *curframe = s->curframe;
2621  int mb_y, num_jobs = s->num_jobs;
2622  int ret;
2623 
2624  td->thread_nr = threadnr;
2625  td->mv_bounds.mv_min.y = -MARGIN - 64 * threadnr;
2626  td->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN - 64 * threadnr;
2627  for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
2628  atomic_store(&td->thread_mb_pos, mb_y << 16);
2629  ret = s->decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
2630  if (ret < 0) {
2631  update_pos(td, s->mb_height, INT_MAX & 0xFFFF);
2632  return ret;
2633  }
2634  if (s->deblock_filter)
2635  s->filter_mb_row(avctx, tdata, jobnr, threadnr);
2636  update_pos(td, mb_y, INT_MAX & 0xFFFF);
2637 
2638  td->mv_bounds.mv_min.y -= 64 * num_jobs;
2639  td->mv_bounds.mv_max.y -= 64 * num_jobs;
2640 
2641  if (avctx->active_thread_type == FF_THREAD_FRAME)
2642  ff_thread_report_progress(&curframe->tf, mb_y, 0);
2643  }
2644 
2645  return 0;
2646 }
2647 
2648 static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2649  int jobnr, int threadnr)
2650 {
2651  return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
2652 }
2653 
2654 static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2655  int jobnr, int threadnr)
2656 {
2657  return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
2658 }
2659 
2660 static av_always_inline
2661 int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame,
2662  const AVPacket *avpkt, int is_vp7)
2663 {
2664  VP8Context *s = avctx->priv_data;
2665  int ret, i, referenced, num_jobs;
2666  enum AVDiscard skip_thresh;
2667  VP8Frame *av_uninit(curframe), *prev_frame;
2668 
2669  if (is_vp7)
2670  ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
2671  else
2672  ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
2673 
2674  if (ret < 0)
2675  goto err;
2676 
2677  if (s->actually_webp) {
2678  // avctx->pix_fmt already set in caller.
2679  } else if (!is_vp7 && s->pix_fmt == AV_PIX_FMT_NONE) {
2680  s->pix_fmt = get_pixel_format(s);
2681  if (s->pix_fmt < 0) {
2682  ret = AVERROR(EINVAL);
2683  goto err;
2684  }
2685  avctx->pix_fmt = s->pix_fmt;
2686  }
2687 
2688  prev_frame = s->framep[VP8_FRAME_CURRENT];
2689 
2690  referenced = s->update_last || s->update_golden == VP8_FRAME_CURRENT ||
2691  s->update_altref == VP8_FRAME_CURRENT;
2692 
2693  skip_thresh = !referenced ? AVDISCARD_NONREF
2694  : !s->keyframe ? AVDISCARD_NONKEY
2695  : AVDISCARD_ALL;
2696 
2697  if (avctx->skip_frame >= skip_thresh) {
2698  s->invisible = 1;
2699  memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
2700  goto skip_decode;
2701  }
2702  s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
2703 
2704  // release no longer referenced frames
2705  for (i = 0; i < 5; i++)
2706  if (s->frames[i].tf.f->buf[0] &&
2707  &s->frames[i] != prev_frame &&
2708  &s->frames[i] != s->framep[VP8_FRAME_PREVIOUS] &&
2709  &s->frames[i] != s->framep[VP8_FRAME_GOLDEN] &&
2710  &s->frames[i] != s->framep[VP8_FRAME_ALTREF])
2711  vp8_release_frame(s, &s->frames[i]);
2712 
2713  curframe = s->framep[VP8_FRAME_CURRENT] = vp8_find_free_buffer(s);
2714 
2715  if (!s->colorspace)
2716  avctx->colorspace = AVCOL_SPC_BT470BG;
2717  if (s->fullrange)
2718  avctx->color_range = AVCOL_RANGE_JPEG;
2719  else
2720  avctx->color_range = AVCOL_RANGE_MPEG;
2721 
2722  /* Given that arithmetic probabilities are updated every frame, it's quite
2723  * likely that the values we have on a random interframe are complete
2724  * junk if we didn't start decode on a keyframe. So just don't display
2725  * anything rather than junk. */
2726  if (!s->keyframe && (!s->framep[VP8_FRAME_PREVIOUS] ||
2727  !s->framep[VP8_FRAME_GOLDEN] ||
2728  !s->framep[VP8_FRAME_ALTREF])) {
2729  av_log(avctx, AV_LOG_WARNING,
2730  "Discarding interframe without a prior keyframe!\n");
2732  goto err;
2733  }
2734 
2735  curframe->tf.f->key_frame = s->keyframe;
2736  curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2738  if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
2739  goto err;
2740 
2741  // check if golden and altref are swapped
2742  if (s->update_altref != VP8_FRAME_NONE)
2743  s->next_framep[VP8_FRAME_ALTREF] = s->framep[s->update_altref];
2744  else
2745  s->next_framep[VP8_FRAME_ALTREF] = s->framep[VP8_FRAME_ALTREF];
2746 
2747  if (s->update_golden != VP8_FRAME_NONE)
2748  s->next_framep[VP8_FRAME_GOLDEN] = s->framep[s->update_golden];
2749  else
2750  s->next_framep[VP8_FRAME_GOLDEN] = s->framep[VP8_FRAME_GOLDEN];
2751 
2752  if (s->update_last)
2753  s->next_framep[VP8_FRAME_PREVIOUS] = curframe;
2754  else
2755  s->next_framep[VP8_FRAME_PREVIOUS] = s->framep[VP8_FRAME_PREVIOUS];
2756 
2757  s->next_framep[VP8_FRAME_CURRENT] = curframe;
2758 
2759  if (ffcodec(avctx->codec)->update_thread_context)
2760  ff_thread_finish_setup(avctx);
2761 
2762  if (avctx->hwaccel) {
2763  ret = avctx->hwaccel->start_frame(avctx, avpkt->data, avpkt->size);
2764  if (ret < 0)
2765  goto err;
2766 
2767  ret = avctx->hwaccel->decode_slice(avctx, avpkt->data, avpkt->size);
2768  if (ret < 0)
2769  goto err;
2770 
2771  ret = avctx->hwaccel->end_frame(avctx);
2772  if (ret < 0)
2773  goto err;
2774 
2775  } else {
2776  s->linesize = curframe->tf.f->linesize[0];
2777  s->uvlinesize = curframe->tf.f->linesize[1];
2778 
2779  memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
2780  /* Zero macroblock structures for top/top-left prediction
2781  * from outside the frame. */
2782  if (!s->mb_layout)
2783  memset(s->macroblocks + s->mb_height * 2 - 1, 0,
2784  (s->mb_width + 1) * sizeof(*s->macroblocks));
2785  if (!s->mb_layout && s->keyframe)
2786  memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
2787 
2788  memset(s->ref_count, 0, sizeof(s->ref_count));
2789 
2790  if (s->mb_layout == 1) {
2791  // Make sure the previous frame has read its segmentation map,
2792  // if we re-use the same map.
2793  if (prev_frame && s->segmentation.enabled &&
2794  !s->segmentation.update_map)
2795  ff_thread_await_progress(&prev_frame->tf, 1, 0);
2796  if (is_vp7)
2797  ret = vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
2798  else
2799  ret = vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
2800  if (ret < 0)
2801  goto err;
2802  }
2803 
2804  if (avctx->active_thread_type == FF_THREAD_FRAME)
2805  num_jobs = 1;
2806  else
2807  num_jobs = FFMIN(s->num_coeff_partitions, avctx->thread_count);
2808  s->num_jobs = num_jobs;
2809  s->curframe = curframe;
2810  s->prev_frame = prev_frame;
2811  s->mv_bounds.mv_min.y = -MARGIN;
2812  s->mv_bounds.mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
2813  for (i = 0; i < MAX_THREADS; i++) {
2814  VP8ThreadData *td = &s->thread_data[i];
2815  atomic_init(&td->thread_mb_pos, 0);
2816  atomic_init(&td->wait_mb_pos, INT_MAX);
2817  }
2818  if (is_vp7)
2819  avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL,
2820  num_jobs);
2821  else
2822  avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL,
2823  num_jobs);
2824  }
2825 
2826  ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
2827  memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
2828 
2829 skip_decode:
2830  // if future frames don't use the updated probabilities,
2831  // reset them to the values we saved
2832  if (!s->update_probabilities)
2833  s->prob[0] = s->prob[1];
2834 
2835  if (!s->invisible) {
2836  if ((ret = av_frame_ref(rframe, curframe->tf.f)) < 0)
2837  return ret;
2838  *got_frame = 1;
2839  }
2840 
2841  return avpkt->size;
2842 err:
2843  memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
2844  return ret;
2845 }
2846 
2848  int *got_frame, AVPacket *avpkt)
2849 {
2850  return vp78_decode_frame(avctx, frame, got_frame, avpkt, IS_VP8);
2851 }
2852 
2853 #if CONFIG_VP7_DECODER
2854 static int vp7_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2855  int *got_frame, AVPacket *avpkt)
2856 {
2857  return vp78_decode_frame(avctx, frame, got_frame, avpkt, IS_VP7);
2858 }
2859 #endif /* CONFIG_VP7_DECODER */
2860 
2862 {
2863  VP8Context *s = avctx->priv_data;
2864  int i;
2865 
2866  vp8_decode_flush_impl(avctx, 1);
2867  for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
2868  av_frame_free(&s->frames[i].tf.f);
2869 
2870  return 0;
2871 }
2872 
2874 {
2875  int i;
2876  for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
2877  s->frames[i].tf.f = av_frame_alloc();
2878  if (!s->frames[i].tf.f)
2879  return AVERROR(ENOMEM);
2880  }
2881  return 0;
2882 }
2883 
2884 static av_always_inline
2885 int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
2886 {
2887  VP8Context *s = avctx->priv_data;
2888  int ret;
2889 
2890  s->avctx = avctx;
2891  s->vp7 = avctx->codec->id == AV_CODEC_ID_VP7;
2892  s->pix_fmt = AV_PIX_FMT_NONE;
2893  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2894 
2895  ff_videodsp_init(&s->vdsp, 8);
2896 
2897  ff_vp78dsp_init(&s->vp8dsp);
2898  if (CONFIG_VP7_DECODER && is_vp7) {
2899  ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP7, 8, 1);
2900  ff_vp7dsp_init(&s->vp8dsp);
2901  s->decode_mb_row_no_filter = vp7_decode_mb_row_no_filter;
2902  s->filter_mb_row = vp7_filter_mb_row;
2903  } else if (CONFIG_VP8_DECODER && !is_vp7) {
2904  ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
2905  ff_vp8dsp_init(&s->vp8dsp);
2906  s->decode_mb_row_no_filter = vp8_decode_mb_row_no_filter;
2907  s->filter_mb_row = vp8_filter_mb_row;
2908  }
2909 
2910  /* does not change for VP8 */
2911  memcpy(s->prob[0].scan, ff_zigzag_scan, sizeof(s->prob[0].scan));
2912 
2913  if ((ret = vp8_init_frames(s)) < 0) {
2914  ff_vp8_decode_free(avctx);
2915  return ret;
2916  }
2917 
2918  return 0;
2919 }
2920 
2921 #if CONFIG_VP7_DECODER
2922 static int vp7_decode_init(AVCodecContext *avctx)
2923 {
2924  return vp78_decode_init(avctx, IS_VP7);
2925 }
2926 #endif /* CONFIG_VP7_DECODER */
2927 
2929 {
2930  return vp78_decode_init(avctx, IS_VP8);
2931 }
2932 
2933 #if CONFIG_VP8_DECODER
2934 #if HAVE_THREADS
2935 #define REBASE(pic) ((pic) ? (pic) - &s_src->frames[0] + &s->frames[0] : NULL)
2936 
2937 static int vp8_decode_update_thread_context(AVCodecContext *dst,
2938  const AVCodecContext *src)
2939 {
2940  VP8Context *s = dst->priv_data, *s_src = src->priv_data;
2941  int i;
2942 
2943  if (s->macroblocks_base &&
2944  (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
2945  free_buffers(s);
2946  s->mb_width = s_src->mb_width;
2947  s->mb_height = s_src->mb_height;
2948  }
2949 
2950  s->pix_fmt = s_src->pix_fmt;
2951  s->prob[0] = s_src->prob[!s_src->update_probabilities];
2952  s->segmentation = s_src->segmentation;
2953  s->lf_delta = s_src->lf_delta;
2954  memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
2955 
2956  for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
2957  if (s_src->frames[i].tf.f->buf[0]) {
2958  int ret = vp8_ref_frame(s, &s->frames[i], &s_src->frames[i]);
2959  if (ret < 0)
2960  return ret;
2961  }
2962  }
2963 
2964  s->framep[0] = REBASE(s_src->next_framep[0]);
2965  s->framep[1] = REBASE(s_src->next_framep[1]);
2966  s->framep[2] = REBASE(s_src->next_framep[2]);
2967  s->framep[3] = REBASE(s_src->next_framep[3]);
2968 
2969  return 0;
2970 }
2971 #endif /* HAVE_THREADS */
2972 #endif /* CONFIG_VP8_DECODER */
2973 
2974 #if CONFIG_VP7_DECODER
2975 const FFCodec ff_vp7_decoder = {
2976  .p.name = "vp7",
2977  CODEC_LONG_NAME("On2 VP7"),
2978  .p.type = AVMEDIA_TYPE_VIDEO,
2979  .p.id = AV_CODEC_ID_VP7,
2980  .priv_data_size = sizeof(VP8Context),
2981  .init = vp7_decode_init,
2982  .close = ff_vp8_decode_free,
2983  FF_CODEC_DECODE_CB(vp7_decode_frame),
2984  .p.capabilities = AV_CODEC_CAP_DR1,
2985  .flush = vp8_decode_flush,
2986 };
2987 #endif /* CONFIG_VP7_DECODER */
2988 
2989 #if CONFIG_VP8_DECODER
2990 const FFCodec ff_vp8_decoder = {
2991  .p.name = "vp8",
2992  CODEC_LONG_NAME("On2 VP8"),
2993  .p.type = AVMEDIA_TYPE_VIDEO,
2994  .p.id = AV_CODEC_ID_VP8,
2995  .priv_data_size = sizeof(VP8Context),
2997  .close = ff_vp8_decode_free,
2999  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
3001  .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS,
3002  .flush = vp8_decode_flush,
3003  UPDATE_THREAD_CONTEXT(vp8_decode_update_thread_context),
3004  .hw_configs = (const AVCodecHWConfigInternal *const []) {
3005 #if CONFIG_VP8_VAAPI_HWACCEL
3006  HWACCEL_VAAPI(vp8),
3007 #endif
3008 #if CONFIG_VP8_NVDEC_HWACCEL
3009  HWACCEL_NVDEC(vp8),
3010 #endif
3011  NULL
3012  },
3013 };
3014 #endif /* CONFIG_VP7_DECODER */
vp8_mode_contexts
static const int vp8_mode_contexts[6][4]
Definition: vp8data.h:118
hwconfig.h
vp8_dct_cat1_prob
static const uint8_t vp8_dct_cat1_prob[]
Definition: vp8data.h:336
FFCodec::update_thread_context
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec_internal.h:149
decode_mb_mode
static av_always_inline void decode_mb_mode(VP8Context *s, const VP8mvbounds *mv_bounds, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment, const uint8_t *ref, int layout, int is_vp7)
Definition: vp8.c:1274
VP7_MV_PRED_COUNT
#define VP7_MV_PRED_COUNT
Definition: vp8data.h:68
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1369
ff_vp8_decode_free
av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
Definition: vp8.c:2861
vp7_pred4x4_mode
static const uint8_t vp7_pred4x4_mode[]
Definition: vp8data.h:33
HOR_PRED8x8
#define HOR_PRED8x8
Definition: h264pred.h:69
decode_block_coeffs_internal
static av_always_inline int decode_block_coeffs_internal(VPXRangeCoder *r, int16_t block[16], uint8_t probs[16][3][NUM_DCT_TOKENS - 1], int i, const uint8_t *token_prob, const int16_t qmul[2], const uint8_t scan[16], int vp7)
Definition: vp8.c:1367
vp7_mv_pred
static const VP7MVPred vp7_mv_pred[VP7_MV_PRED_COUNT]
Definition: vp8data.h:69
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:253
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
vp8_decode_block_coeffs_internal
static int vp8_decode_block_coeffs_internal(VPXRangeCoder *r, int16_t block[16], uint8_t probs[16][3][NUM_DCT_TOKENS - 1], int i, const uint8_t *token_prob, const int16_t qmul[2])
Definition: vp8.c:1461
vp7_read_mv_component
static int vp7_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
Definition: vp8.c:920
vp7_calculate_mb_offset
static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width, int xoffset, int yoffset, int boundary, int *edge_x, int *edge_y)
The vp7 reference decoder uses a padding macroblock column (added to right edge of the frame) to guar...
Definition: vp8.c:1030
av_clip
#define av_clip
Definition: common.h:95
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
backup_mb_border
static av_always_inline void backup_mb_border(uint8_t *top_border, const uint8_t *src_y, const uint8_t *src_cb, const uint8_t *src_cr, ptrdiff_t linesize, ptrdiff_t uvlinesize, int simple)
Definition: vp8.c:1576
VP8Macroblock::partitioning
uint8_t partitioning
Definition: vp8.h:102
VP8_FRAME_CURRENT
@ VP8_FRAME_CURRENT
Definition: vp8.h:45
r
const char * r
Definition: vf_curves.c:126
vp7_filter_mb_row
static void vp7_filter_mb_row(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2601
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
DC_PRED8x8
#define DC_PRED8x8
Definition: h264pred.h:68
IS_VP7
#define IS_VP7
Definition: vp8dsp.h:99
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:975
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1178
vp78_decode_init
static av_always_inline int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
Definition: vp8.c:2885
mem_internal.h
DC_128_PRED
@ DC_128_PRED
Definition: vp9.h:58
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1147
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
check_tm_pred8x8_mode
static av_always_inline int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
Definition: vp8.c:1631
vp8_submv_prob
static const uint8_t vp8_submv_prob[5][3]
Definition: vp8data.h:153
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:119
pthread_mutex_init
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
vp7_ydc_qlookup
static const uint16_t vp7_ydc_qlookup[]
Definition: vp8data.h:772
src1
const pixel * src1
Definition: h264pred_template.c:421
HOR_VP8_PRED
#define HOR_VP8_PRED
unaveraged version of HOR_PRED, see
Definition: h264pred.h:63
mv
static const int8_t mv[256][2]
Definition: 4xm.c:80
vp7_decode_mvs
static av_always_inline void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
Definition: vp8.c:1049
vp7_mv_default_prob
static const uint8_t vp7_mv_default_prob[2][17]
Definition: vp8data.h:738
check_intra_pred4x4_mode_emuedge
static av_always_inline int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf, int vp7)
Definition: vp8.c:1666
ff_vp8_dct_cat_prob
const uint8_t *const ff_vp8_dct_cat_prob[]
Definition: vp8data.h:356
VP8Frame::seg_map
AVBufferRef * seg_map
Definition: vp8.h:155
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:116
check_tm_pred4x4_mode
static av_always_inline int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
Definition: vp8.c:1656
vp7_y2dc_qlookup
static const uint16_t vp7_y2dc_qlookup[]
Definition: vp8data.h:797
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
TM_VP8_PRED
@ TM_VP8_PRED
Definition: vp9.h:55
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:661
vp8_mc_chroma
static av_always_inline void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1, uint8_t *dst2, const ThreadFrame *ref, const VP8mv *mv, int x_off, int y_off, int block_w, int block_h, int width, int height, ptrdiff_t linesize, vp8_mc_func mc_func[3][3])
chroma MC function
Definition: vp8.c:1898
AVPacket::data
uint8_t * data
Definition: packet.h:374
inter_predict_dc
static av_always_inline int inter_predict_dc(int16_t block[16], int16_t pred[2])
Definition: vp8.c:1427
DC_PRED
@ DC_PRED
Definition: vp9.h:48
b
#define b
Definition: input.c:41
VP7_MVC_SIZE
#define VP7_MVC_SIZE
Definition: vp8.c:477
VERT_LEFT_PRED
@ VERT_LEFT_PRED
Definition: vp9.h:53
vp8_get_quants
static void vp8_get_quants(VP8Context *s)
Definition: vp8.c:390
FFCodec
Definition: codec_internal.h:119
AV_WN32A
#define AV_WN32A(p, v)
Definition: intreadwrite.h:538
cat
#define cat(a, bpp, b)
Definition: vp9dsp_init.h:32
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
VP8mvbounds
Definition: vp8.h:116
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
vp89_rac.h
inter_predict
static av_always_inline void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
Apply motion vectors to prediction buffer, chapter 18.
Definition: vp8.c:2009
VP8_SPLITMVMODE_4x4
@ VP8_SPLITMVMODE_4x4
4x4 blocks of 4x4px each
Definition: vp8.h:81
ff_set_dimensions
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:91
VP8_FRAME_ALTREF
@ VP8_FRAME_ALTREF
Definition: vp8.h:48
VERT_VP8_PRED
#define VERT_VP8_PRED
for VP8, VERT_PRED is the average of
Definition: h264pred.h:60
VPXRangeCoder
Definition: vpx_rac.h:35
thread.h
ff_thread_await_progress
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before ff_thread_await_progress() has been called on them. reget_buffer() and buffer age optimizations no longer work. *The contents of buffers must not be written to after ff_thread_report_progress() has been called on them. This includes draw_edges(). Porting codecs to frame threading
ThreadFrame::f
AVFrame * f
Definition: threadframe.h:28
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
VP8_MVC_SIZE
#define VP8_MVC_SIZE
Definition: vp8.c:478
vp8_decode_mb_row_no_filter
static int vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2536
vp8_rac_get_sint
static int vp8_rac_get_sint(VPXRangeCoder *c, int bits)
Definition: vp8.c:48
vp8_pred8x8c_tree
static const int8_t vp8_pred8x8c_tree[3][2]
Definition: vp8data.h:180
XCHG
#define XCHG(a, b, xchg)
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
update_pos
#define update_pos(td, mb_y, mb_x)
Definition: vp8.c:2394
vp8.h
get_bmv_ptr
static const VP8mv * get_bmv_ptr(const VP8Macroblock *mb, int subblock)
Definition: vp8.c:1043
update_dimensions
static av_always_inline int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
Definition: vp8.c:218
AVCOL_SPC_BT470BG
@ 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:593
VP8_SPLITMVMODE_8x8
@ VP8_SPLITMVMODE_8x8
2x2 blocks of 8x8px each
Definition: vp8.h:80
AVHWAccel
Definition: avcodec.h:2070
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:407
DC_127_PRED
@ DC_127_PRED
Definition: vp9.h:59
vp8_mv_update_prob
static const uint8_t vp8_mv_update_prob[2][19]
Definition: vp8data.h:727
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1698
fail
#define fail()
Definition: checkasm.h:134
VERT_PRED
@ VERT_PRED
Definition: vp9.h:46
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1466
ff_vp8_decoder
const FFCodec ff_vp8_decoder
VP8mv::y
int16_t y
Definition: vp8.h:87
DIAG_DOWN_RIGHT_PRED
@ DIAG_DOWN_RIGHT_PRED
Definition: vp9.h:50
MAX_THREADS
#define MAX_THREADS
Definition: frame_thread_encoder.c:34
ff_videodsp_init
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:39
update
static av_always_inline void update(SilenceDetectContext *s, AVFrame *insamples, int is_silence, int current_sample, int64_t nb_samples_notify, AVRational time_base)
Definition: af_silencedetect.c:78
VP8Macroblock::bmv
VP8mv bmv[16]
Definition: vp8.h:108
idct_mb
static av_always_inline void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3], const VP8Macroblock *mb)
Definition: vp8.c:2093
check_intra_pred8x8_mode_emuedge
static av_always_inline int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
Definition: vp8.c:1640
filter_level_for_mb
static av_always_inline void filter_level_for_mb(const VP8Context *s, const VP8Macroblock *mb, VP8FilterStrength *f, int is_vp7)
Definition: vp8.c:2156
read_mv_component
static av_always_inline int read_mv_component(VPXRangeCoder *c, const uint8_t *p, int vp7)
Motion vector coding, 17.1.
Definition: vp8.c:892
vp8_filter_mb_row
static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2607
vp7_get_quants
static void vp7_get_quants(VP8Context *s)
Definition: vp8.c:371
VP8_SPLITMVMODE_16x8
@ VP8_SPLITMVMODE_16x8
2 16x8 blocks (vertical)
Definition: vp8.h:78
vp8_init_frames
static av_cold int vp8_init_frames(VP8Context *s)
Definition: vp8.c:2873
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:104
vp8_mc_part
static av_always_inline void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3], const ThreadFrame *ref_frame, int x_off, int y_off, int bx_off, int by_off, int block_w, int block_h, int width, int height, const VP8mv *mv)
Definition: vp8.c:1948
vp8_mc_luma
static av_always_inline void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst, const ThreadFrame *ref, const VP8mv *mv, int x_off, int y_off, int block_w, int block_h, int width, int height, ptrdiff_t linesize, vp8_mc_func mc_func[3][3])
luma MC function
Definition: vp8.c:1840
ff_vp7dsp_init
void ff_vp7dsp_init(VP8DSPContext *c)
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_vp8dsp_init
void ff_vp8dsp_init(VP8DSPContext *c)
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
HOR_PRED
@ HOR_PRED
Definition: vp9.h:47
av_cold
#define av_cold
Definition: attributes.h:90
vp8_dct_cat2_prob
static const uint8_t vp8_dct_cat2_prob[]
Definition: vp8data.h:339
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:624
LOCAL_ALIGNED
#define LOCAL_ALIGNED(a, t, v,...)
Definition: mem_internal.h:114
width
#define width
vp8_pred4x4_mode
static const uint8_t vp8_pred4x4_mode[]
Definition: vp8data.h:40
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
s
#define s(width, name)
Definition: cbs_vp9.c:256
filter_mb_simple
static av_always_inline void filter_mb_simple(const VP8Context *s, uint8_t *dst, const VP8FilterStrength *f, int mb_x, int mb_y)
Definition: vp8.c:2276
vpx_rac_renorm
static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c)
Definition: vpx_rac.h:58
AV_ZERO64
#define AV_ZERO64(d)
Definition: intreadwrite.h:633
vp8_pred8x8c_prob_inter
static const uint8_t vp8_pred8x8c_prob_inter[3]
Definition: vp8data.h:189
DC_129_PRED8x8
#define DC_129_PRED8x8
Definition: h264pred.h:86
AV_ZERO32
#define AV_ZERO32(d)
Definition: intreadwrite.h:629
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:376
VP8Frame::tf
ThreadFrame tf
Definition: vp8.h:154
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:363
vp8_pred16x16_tree_intra
static const int8_t vp8_pred16x16_tree_intra[4][2]
Definition: vp8data.h:47
bits
uint8_t bits
Definition: vp3data.h:128
parse_segment_info
static void parse_segment_info(VP8Context *s)
Definition: vp8.c:293
vp8_pred4x4_prob_inter
static const uint8_t vp8_pred4x4_prob_inter[9]
Definition: vp8data.h:192
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
vp8_mbsplits
static const uint8_t vp8_mbsplits[5][16]
Definition: vp8data.h:127
ctx
AVFormatContext * ctx
Definition: movenc.c:48
vp78_decode_frame
static av_always_inline int vp78_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, const AVPacket *avpkt, int is_vp7)
Definition: vp8.c:2661
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
vp7_mode_contexts
static const int vp7_mode_contexts[31][4]
Definition: vp8data.h:84
vp78_decode_mv_mb_modes
static av_always_inline int vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe, const VP8Frame *prev_frame, int is_vp7)
Definition: vp8.c:2310
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
atomic_load
#define atomic_load(object)
Definition: stdatomic.h:93
VP8_SPLITMVMODE_8x16
@ VP8_SPLITMVMODE_8x16
2 8x16 blocks (horizontal)
Definition: vp8.h:79
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:885
vp8_mv_default_prob
static const uint8_t vp8_mv_default_prob[2][19]
Definition: vp8data.h:749
vp8_coeff_band_indexes
static const int8_t vp8_coeff_band_indexes[8][10]
Definition: vp8data.h:325
TOP_DC_PRED8x8
#define TOP_DC_PRED8x8
Definition: h264pred.h:75
if
if(ret)
Definition: filter_design.txt:179
vp8_pred16x16_prob_inter
static const uint8_t vp8_pred16x16_prob_inter[4]
Definition: vp8data.h:164
vp8_decode_flush_impl
static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
Definition: vp8.c:159
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:113
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:76
threadframe.h
vp8_rac_get_nn
static int vp8_rac_get_nn(VPXRangeCoder *c)
Definition: vp8.c:63
clamp_mv
static av_always_inline void clamp_mv(const VP8mvbounds *s, VP8mv *dst, const VP8mv *src)
Definition: vp8.c:881
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:57
AV_COPY128
#define AV_COPY128(d, s)
Definition: intreadwrite.h:609
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:982
av_buffer_unref
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:139
vp7_decode_mb_row_sliced
static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2648
AV_COPY64
#define AV_COPY64(d, s)
Definition: intreadwrite.h:605
vp8_update_dimensions
static int vp8_update_dimensions(VP8Context *s, int width, int height)
Definition: vp8.c:287
VP8FilterStrength
Definition: vp8.h:90
NUM_DCT_TOKENS
@ NUM_DCT_TOKENS
Definition: vp8.h:65
AVHWAccel::end_frame
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2170
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
VP8Frame::hwaccel_priv_buf
AVBufferRef * hwaccel_priv_buf
Definition: vp8.h:157
vp89_rac_get_uint
static av_unused int vp89_rac_get_uint(VPXRangeCoder *c, int bits)
Definition: vp89_rac.h:41
check_thread_pos
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)
Definition: vp8.c:2393
VP7MVPred
Definition: vp8data.h:61
mathops.h
vp8_mc_func
void(* vp8_mc_func)(uint8_t *dst, ptrdiff_t dstStride, const uint8_t *src, ptrdiff_t srcStride, int h, int x, int y)
Definition: vp8dsp.h:33
vp7_yac_qlookup
static const uint16_t vp7_yac_qlookup[]
Definition: vp8data.h:784
vp8_token_default_probs
static const uint8_t vp8_token_default_probs[4][8][3][NUM_DCT_TOKENS - 1]
Definition: vp8data.h:363
VERT_PRED8x8
#define VERT_PRED8x8
Definition: h264pred.h:70
vp8_token_update_probs
static const uint8_t vp8_token_update_probs[4][8][3][NUM_DCT_TOKENS - 1]
Definition: vp8data.h:534
vp8_mbsplit_count
static const uint8_t vp8_mbsplit_count[4]
Definition: vp8data.h:142
UPDATE_THREAD_CONTEXT
#define UPDATE_THREAD_CONTEXT(func)
Definition: codec_internal.h:273
vp8_decode_mvs
static av_always_inline void vp8_decode_mvs(VP8Context *s, const VP8mvbounds *mv_bounds, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
Definition: vp8.c:1139
AV_ZERO128
#define AV_ZERO128(d)
Definition: intreadwrite.h:637
VP8FrameType
VP8FrameType
Definition: vp8.h:43
decode_mb_row_no_filter
static av_always_inline int decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr, int is_vp7)
Definition: vp8.c:2397
VP8mv
Definition: vp8.h:85
vp7_feature_value_size
static const uint8_t vp7_feature_value_size[2][4]
Definition: vp8data.h:760
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
VP8Frame
Definition: vp8.h:153
vp8.h
ff_vp8_decode_init
av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
Definition: vp8.c:2928
VP8_FRAME_GOLDEN
@ VP8_FRAME_GOLDEN
Definition: vp8.h:47
FF_SIGNBIT
#define FF_SIGNBIT(x)
Definition: mathops.h:130
vp8_mbfirstidx
static const uint8_t vp8_mbfirstidx[4][16]
Definition: vp8data.h:135
DC_127_PRED8x8
#define DC_127_PRED8x8
Definition: h264pred.h:85
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:75
f
f
Definition: af_crystalizer.c:122
xchg_mb_border
static av_always_inline void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, ptrdiff_t linesize, ptrdiff_t uvlinesize, int mb_x, int mb_y, int mb_width, int simple, int xchg)
Definition: vp8.c:1588
ff_zigzag_scan
const uint8_t ff_zigzag_scan[16+1]
Definition: mathtables.c:109
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
vp8_pred4x4_tree
static const int8_t vp8_pred4x4_tree[9][2]
Definition: vp8data.h:168
MV_EDGE_CHECK
#define MV_EDGE_CHECK(n)
AVPacket::size
int size
Definition: packet.h:375
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:187
av_frame_ref
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:353
codec_internal.h
vp8_coeff_band
static const uint8_t vp8_coeff_band[16]
Definition: vp8data.h:319
subpel_idx
static const uint8_t subpel_idx[3][8]
Definition: vp8.c:1816
vp7_update_dimensions
static int vp7_update_dimensions(VP8Context *s, int width, int height)
Definition: vp8.c:282
EDGE_EMU_LINESIZE
#define EDGE_EMU_LINESIZE
Definition: vp8.h:147
size
int size
Definition: twinvq_data.h:10344
VERT_RIGHT_PRED
@ VERT_RIGHT_PRED
Definition: vp9.h:51
vp8_release_frame
static void vp8_release_frame(VP8Context *s, VP8Frame *f)
Definition: vp8.c:126
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
free_buffers
static void free_buffers(VP8Context *s)
Definition: vp8.c:81
DC_128_PRED8x8
#define DC_128_PRED8x8
Definition: h264pred.h:76
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:317
decode_block_coeffs
static av_always_inline int decode_block_coeffs(VPXRangeCoder *c, int16_t block[16], uint8_t probs[16][3][NUM_DCT_TOKENS - 1], int i, int zero_nhood, const int16_t qmul[2], const uint8_t scan[16], int vp7)
Definition: vp8.c:1486
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1478
vp8_pred8x8c_prob_intra
static const uint8_t vp8_pred8x8c_prob_intra[3]
Definition: vp8data.h:186
vp8_decode_mv_mb_modes
static int vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame, const VP8Frame *prev_frame)
Definition: vp8.c:2353
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
AVCodecHWConfigInternal
Definition: hwconfig.h:29
vp8_pred4x4_prob_intra
static const uint8_t vp8_pred4x4_prob_intra[10][10][9]
Definition: vp8data.h:196
VP8ThreadData
Definition: vp8.h:121
height
#define height
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AV_CODEC_CAP_SLICE_THREADS
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:117
vp8_decode_frame_header
static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
Definition: vp8.c:740
vp8_mbsplit_prob
static const uint8_t vp8_mbsplit_prob[3]
Definition: vp8data.h:145
setup_partitions
static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
Definition: vp8.c:339
vp8_pred16x16_tree_inter
static const int8_t vp8_pred16x16_tree_inter[4][2]
Definition: vp8data.h:54
vp7_feature_index_tree
static const int8_t vp7_feature_index_tree[4][2]
Definition: vp8data.h:765
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1684
HWACCEL_NVDEC
#define HWACCEL_NVDEC(codec)
Definition: hwconfig.h:71
PLANE_PRED8x8
#define PLANE_PRED8x8
Definition: h264pred.h:71
vpx_rac_is_end
static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c)
returns 1 if the end of the stream has been reached, 0 otherwise.
Definition: vpx_rac.h:51
AV_PIX_FMT_VAAPI
@ AV_PIX_FMT_VAAPI
Hardware acceleration through VA-API, data[3] contains a VASurfaceID.
Definition: pixfmt.h:119
mb
#define mb
Definition: vf_colormatrix.c:101
pthread_cond_destroy
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
MODE_I4x4
#define MODE_I4x4
Definition: vp8.h:69
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1477
H_LOOP_FILTER_16Y_INNER
#define H_LOOP_FILTER_16Y_INNER(cond)
vp78_decode_mb_row_sliced
static av_always_inline int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr, int is_vp7)
Definition: vp8.c:2614
AVCodec::id
enum AVCodecID id
Definition: codec.h:218
pthread_mutex_destroy
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
AV_CODEC_ID_VP7
@ AV_CODEC_ID_VP7
Definition: codec_id.h:233
ref_to_update
static VP8FrameType ref_to_update(VP8Context *s, int update, VP8FrameType ref)
Determine which buffers golden and altref should be updated with after this frame.
Definition: vp8.c:436
vp8_read_mv_component
static int vp8_read_mv_component(VPXRangeCoder *c, const uint8_t *p)
Definition: vp8.c:925
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
DC_129_PRED
@ DC_129_PRED
Definition: vp9.h:60
AVHWAccel::decode_slice
int(* decode_slice)(AVCodecContext *avctx, const uint8_t *buf, uint32_t buf_size)
Callback for each slice.
Definition: avcodec.h:2159
modes
static const SiprModeParam modes[MODE_COUNT]
Definition: sipr.c:70
vpx_rac.h
src2
const pixel * src2
Definition: h264pred_template.c:422
vp7_fade_frame
static int vp7_fade_frame(VP8Context *s, int alpha, int beta)
Definition: vp8.c:538
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
vpx_rac_get_prob_branchy
static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
Definition: vpx_rac.h:99
intra_predict
static av_always_inline void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *const dst[3], VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
Definition: vp8.c:1702
AV_COPY32
#define AV_COPY32(d, s)
Definition: intreadwrite.h:601
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
VP8mv::x
int16_t x
Definition: vp8.h:86
vp78_reset_probability_tables
static void vp78_reset_probability_tables(VP8Context *s)
Definition: vp8.c:452
vp7_decode_frame_header
static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
Definition: vp8.c:576
VP8_FRAME_NONE
@ VP8_FRAME_NONE
Definition: vp8.h:44
profile
int profile
Definition: mxfenc.c:2006
fade
static void fade(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, int width, int height, int alpha, int beta)
Definition: vp8.c:522
decode_intra4x4_modes
static av_always_inline void decode_intra4x4_modes(VP8Context *s, VPXRangeCoder *c, VP8Macroblock *mb, int mb_x, int keyframe, int layout)
Definition: vp8.c:1239
VP8Macroblock
Definition: vp8.h:96
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:608
ff_thread_get_ext_buffer
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1080
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:644
vp8_decode_mb_row_sliced
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2654
ff_vp7_decoder
const FFCodec ff_vp7_decoder
VP8_SPLITMVMODE_NONE
@ VP8_SPLITMVMODE_NONE
(only used in prediction) no split MVs
Definition: vp8.h:82
LEFT_DC_PRED8x8
#define LEFT_DC_PRED8x8
Definition: h264pred.h:74
prefetch_motion
static av_always_inline void prefetch_motion(const VP8Context *s, const VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
Definition: vp8.c:1986
avcodec.h
AV_RN32A
#define AV_RN32A(p)
Definition: intreadwrite.h:526
vp89_rac_get_tree
static av_always_inline int vp89_rac_get_tree(VPXRangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp89_rac.h:54
decode_mb_coeffs
static av_always_inline void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VPXRangeCoder *c, VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9], int is_vp7)
Definition: vp8.c:1501
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:93
ret
ret
Definition: filter_design.txt:187
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
check_dc_pred8x8_mode
static av_always_inline int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
Definition: vp8.c:1622
pred
static const float pred[4]
Definition: siprdata.h:259
vp7_decode_mb_row_no_filter
static int vp7_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:2530
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
vp8_pred16x16_prob_intra
static const uint8_t vp8_pred16x16_prob_intra[4]
Definition: vp8data.h:161
vp8_ac_qlookup
static const uint16_t vp8_ac_qlookup[VP8_MAX_QUANT+1]
Definition: vp8data.h:716
prob
#define prob(name, subs,...)
Definition: cbs_vp9.c:372
ff_vpx_init_range_decoder
int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size)
Definition: vpx_rac.c:42
ff_thread_finish_setup
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call ff_thread_finish_setup() afterwards. If some code can 't be moved
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
vp89_rac_get
static av_always_inline int vp89_rac_get(VPXRangeCoder *c)
Definition: vp89_rac.h:36
AVCodecContext
main external API structure.
Definition: avcodec.h:398
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1485
VP8Macroblock::intra4x4_pred_mode_top
uint8_t intra4x4_pred_mode_top[4]
Definition: vp8.h:106
decode_splitmvs
static av_always_inline int decode_splitmvs(const VP8Context *s, VPXRangeCoder *c, VP8Macroblock *mb, int layout, int is_vp7)
Split motion vector prediction, 16.4.
Definition: vp8.c:948
ThreadFrame
Definition: threadframe.h:27
ff_h264_pred_init
av_cold void ff_h264_pred_init(H264PredContext *h, int codec_id, const int bit_depth, int chroma_format_idc)
Set the intra prediction function pointers.
Definition: h264pred.c:437
HOR_UP_PRED
@ HOR_UP_PRED
Definition: vp9.h:54
vp8data.h
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVHWAccel::frame_priv_data_size
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:2179
VP8Macroblock::mode
uint8_t mode
Definition: vp8.h:100
IS_VP8
#define IS_VP8
Definition: vp8dsp.h:100
VP8_MVMODE_SPLIT
@ VP8_MVMODE_SPLIT
Definition: vp8.h:74
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
HOR_DOWN_PRED
@ HOR_DOWN_PRED
Definition: vp9.h:52
vp7_decode_block_coeffs_internal
static int vp7_decode_block_coeffs_internal(VPXRangeCoder *r, int16_t block[16], uint8_t probs[16][3][NUM_DCT_TOKENS - 1], int i, const uint8_t *token_prob, const int16_t qmul[2], const uint8_t scan[16])
Definition: vp8.c:1449
filter_mb
static av_always_inline void filter_mb(const VP8Context *s, uint8_t *const dst[3], const VP8FilterStrength *f, int mb_x, int mb_y, int is_vp7)
Definition: vp8.c:2189
segment
Definition: hls.c:75
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
AVHWAccel::start_frame
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:2132
vp78_update_probability_tables
static void vp78_update_probability_tables(VP8Context *s)
Definition: vp8.c:461
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
vp78_update_pred16x16_pred8x8_mvc_probabilities
static void vp78_update_pred16x16_pred8x8_mvc_probabilities(VP8Context *s, int mvc_size)
Definition: vp8.c:480
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
update_refs
static void update_refs(VP8Context *s)
Definition: vp8.c:500
vp7_decode_mv_mb_modes
static int vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame, const VP8Frame *prev_frame)
Definition: vp8.c:2347
update_lf_deltas
static void update_lf_deltas(VP8Context *s)
Definition: vp8.c:315
filter_mb_row
static av_always_inline void filter_mb_row(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr, int is_vp7)
Definition: vp8.c:2542
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
get_pixel_format
static enum AVPixelFormat get_pixel_format(VP8Context *s)
Definition: vp8.c:201
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_vp8_decode_frame
int ff_vp8_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: vp8.c:2847
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
VP7MVPred::score
uint8_t score
Definition: vp8data.h:65
VP8Context
Definition: vp8.h:162
HWACCEL_VAAPI
#define HWACCEL_VAAPI(codec)
Definition: hwconfig.h:73
DIAG_DOWN_LEFT_PRED
@ DIAG_DOWN_LEFT_PRED
Definition: vp9.h:49
vp8_find_free_buffer
static VP8Frame * vp8_find_free_buffer(VP8Context *s)
Definition: vp8.c:177
int32_t
int32_t
Definition: audioconvert.c:56
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:370
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:78
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_WN64
#define AV_WN64(p, v)
Definition: intreadwrite.h:380
VP8_MVMODE_ZERO
@ VP8_MVMODE_ZERO
Definition: vp8.h:72
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
pthread_cond_init
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:133
vp7_y2ac_qlookup
static const uint16_t vp7_y2ac_qlookup[]
Definition: vp8data.h:810
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
vp7_submv_prob
static const uint8_t vp7_submv_prob[3]
Definition: vp8data.h:149
AVDiscard
AVDiscard
Definition: defs.h:67
AVDISCARD_NONREF
@ AVDISCARD_NONREF
discard all non reference
Definition: defs.h:72
vp8_rac_get_coeff
static int vp8_rac_get_coeff(VPXRangeCoder *c, const uint8_t *prob)
Definition: vp8.c:70
vp8_dc_qlookup
static const uint8_t vp8_dc_qlookup[VP8_MAX_QUANT+1]
Definition: vp8data.h:705
copy_chroma
static void copy_chroma(AVFrame *dst, const AVFrame *src, int width, int height)
Definition: vp8.c:511
VP8_FRAME_PREVIOUS
@ VP8_FRAME_PREVIOUS
Definition: vp8.h:46
vpx_rac_get_prob
#define vpx_rac_get_prob
Definition: vpx_rac.h:82
vp8_decode_flush
static void vp8_decode_flush(AVCodecContext *avctx)
Definition: vp8.c:172
AVCodecContext::execute2
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:1536
VP8_MVMODE_MV
@ VP8_MVMODE_MV
Definition: vp8.h:73
MARGIN
#define MARGIN
Definition: vp8.c:2308
vp8_alloc_frame
static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
Definition: vp8.c:101
get_submv_prob
static const av_always_inline uint8_t * get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
Definition: vp8.c:931
ff_vp78dsp_init
av_cold void ff_vp78dsp_init(VP8DSPContext *dsp)
Definition: vp8dsp.c:668
VP8Frame::hwaccel_picture_private
void * hwaccel_picture_private
Definition: vp8.h:158