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