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