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