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