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