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