vp8.c
Go to the documentation of this file.
1 /*
2  * VP8 compatible video decoder
3  *
4  * Copyright (C) 2010 David Conrad
5  * Copyright (C) 2010 Ronald S. Bultje
6  * Copyright (C) 2010 Jason Garrett-Glaser
7  * Copyright (C) 2012 Daniel Kang
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/imgutils.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "vp8.h"
30 #include "vp8data.h"
31 #include "rectangle.h"
32 #include "thread.h"
33 
34 #if ARCH_ARM
35 # include "arm/vp8.h"
36 #endif
37 
38 static void free_buffers(VP8Context *s)
39 {
40  int i;
41  if (s->thread_data)
42  for (i = 0; i < MAX_THREADS; i++) {
43 #if HAVE_THREADS
44  pthread_cond_destroy(&s->thread_data[i].cond);
46 #endif
49  }
50  av_freep(&s->thread_data);
53  av_freep(&s->top_nnz);
54  av_freep(&s->top_border);
55 
56  s->macroblocks = NULL;
57 }
58 
60 {
61  int ret;
62  if ((ret = ff_thread_get_buffer(s->avctx, f)) < 0)
63  return ret;
64  if (s->num_maps_to_be_freed && !s->maps_are_invalid) {
66  } else if (!(f->ref_index[0] = av_mallocz(s->mb_width * s->mb_height))) {
68  return AVERROR(ENOMEM);
69  }
70  return 0;
71 }
72 
73 static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
74 {
75  if (f->ref_index[0]) {
76  if (prefer_delayed_free) {
77  /* Upon a size change, we want to free the maps but other threads may still
78  * be using them, so queue them. Upon a seek, all threads are inactive so
79  * we want to cache one to prevent re-allocation in the next decoding
80  * iteration, but the rest we can free directly. */
81  int max_queued_maps = can_direct_free ? 1 : FF_ARRAY_ELEMS(s->segmentation_maps);
82  if (s->num_maps_to_be_freed < max_queued_maps) {
84  } else if (can_direct_free) /* vp8_decode_flush(), but our queue is full */ {
85  av_free(f->ref_index[0]);
86  } /* else: MEMLEAK (should never happen, but better that than crash) */
87  f->ref_index[0] = NULL;
88  } else /* vp8_decode_free() */ {
89  av_free(f->ref_index[0]);
90  }
91  }
93 }
94 
96  int prefer_delayed_free, int can_direct_free, int free_mem)
97 {
98  VP8Context *s = avctx->priv_data;
99  int i;
100 
101  if (!avctx->internal->is_copy) {
102  for (i = 0; i < 5; i++)
103  if (s->frames[i].data[0])
104  vp8_release_frame(s, &s->frames[i], prefer_delayed_free, can_direct_free);
105  }
106  memset(s->framep, 0, sizeof(s->framep));
107 
108  if (free_mem) {
109  free_buffers(s);
110  s->maps_are_invalid = 1;
111  }
112 }
113 
114 static void vp8_decode_flush(AVCodecContext *avctx)
115 {
116  vp8_decode_flush_impl(avctx, 1, 1, 0);
117 }
118 
119 static int update_dimensions(VP8Context *s, int width, int height)
120 {
121  AVCodecContext *avctx = s->avctx;
122  int i;
123 
124  if (width != s->avctx->width ||
125  height != s->avctx->height) {
126  if (av_image_check_size(width, height, 0, s->avctx))
127  return AVERROR_INVALIDDATA;
128 
129  vp8_decode_flush_impl(s->avctx, 1, 0, 1);
130 
131  avcodec_set_dimensions(s->avctx, width, height);
132  }
133 
134  s->mb_width = (s->avctx->coded_width +15) / 16;
135  s->mb_height = (s->avctx->coded_height+15) / 16;
136 
138  if (!s->mb_layout) { // Frame threading and one thread
139  s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
141  }
142  else // Sliced threading
143  s->macroblocks_base = av_mallocz((s->mb_width+2)*(s->mb_height+2)*sizeof(*s->macroblocks));
144  s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
145  s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
147 
148  for (i = 0; i < MAX_THREADS; i++) {
150 #if HAVE_THREADS
151  pthread_mutex_init(&s->thread_data[i].lock, NULL);
152  pthread_cond_init(&s->thread_data[i].cond, NULL);
153 #endif
154  }
155 
156  if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||
157  (!s->intra4x4_pred_mode_top && !s->mb_layout))
158  return AVERROR(ENOMEM);
159 
160  s->macroblocks = s->macroblocks_base + 1;
161 
162  return 0;
163 }
164 
166 {
167  VP56RangeCoder *c = &s->c;
168  int i;
169 
171 
172  if (vp8_rac_get(c)) { // update segment feature data
174 
175  for (i = 0; i < 4; i++)
177 
178  for (i = 0; i < 4; i++)
180  }
181  if (s->segmentation.update_map)
182  for (i = 0; i < 3; i++)
183  s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
184 }
185 
187 {
188  VP56RangeCoder *c = &s->c;
189  int i;
190 
191  for (i = 0; i < 4; i++) {
192  if (vp8_rac_get(c)) {
193  s->lf_delta.ref[i] = vp8_rac_get_uint(c, 6);
194 
195  if (vp8_rac_get(c))
196  s->lf_delta.ref[i] = -s->lf_delta.ref[i];
197  }
198  }
199 
200  for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
201  if (vp8_rac_get(c)) {
202  s->lf_delta.mode[i] = vp8_rac_get_uint(c, 6);
203 
204  if (vp8_rac_get(c))
205  s->lf_delta.mode[i] = -s->lf_delta.mode[i];
206  }
207  }
208 }
209 
210 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
211 {
212  const uint8_t *sizes = buf;
213  int i;
214 
215  s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
216 
217  buf += 3*(s->num_coeff_partitions-1);
218  buf_size -= 3*(s->num_coeff_partitions-1);
219  if (buf_size < 0)
220  return -1;
221 
222  for (i = 0; i < s->num_coeff_partitions-1; i++) {
223  int size = AV_RL24(sizes + 3*i);
224  if (buf_size - size < 0)
225  return -1;
226 
227  ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
228  buf += size;
229  buf_size -= size;
230  }
231  ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
232 
233  return 0;
234 }
235 
236 static void get_quants(VP8Context *s)
237 {
238  VP56RangeCoder *c = &s->c;
239  int i, base_qi;
240 
241  int yac_qi = vp8_rac_get_uint(c, 7);
242  int ydc_delta = vp8_rac_get_sint(c, 4);
243  int y2dc_delta = vp8_rac_get_sint(c, 4);
244  int y2ac_delta = vp8_rac_get_sint(c, 4);
245  int uvdc_delta = vp8_rac_get_sint(c, 4);
246  int uvac_delta = vp8_rac_get_sint(c, 4);
247 
248  for (i = 0; i < 4; i++) {
249  if (s->segmentation.enabled) {
250  base_qi = s->segmentation.base_quant[i];
251  if (!s->segmentation.absolute_vals)
252  base_qi += yac_qi;
253  } else
254  base_qi = yac_qi;
255 
256  s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta , 7)];
257  s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi , 7)];
258  s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)];
259  /* 101581>>16 is equivalent to 155/100 */
260  s->qmat[i].luma_dc_qmul[1] = (101581 * vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)]) >> 16;
261  s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
262  s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
263 
264  s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
265  s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
266  }
267 }
268 
282 static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
283 {
284  VP56RangeCoder *c = &s->c;
285 
286  if (update)
287  return VP56_FRAME_CURRENT;
288 
289  switch (vp8_rac_get_uint(c, 2)) {
290  case 1:
291  return VP56_FRAME_PREVIOUS;
292  case 2:
294  }
295  return VP56_FRAME_NONE;
296 }
297 
298 static void update_refs(VP8Context *s)
299 {
300  VP56RangeCoder *c = &s->c;
301 
302  int update_golden = vp8_rac_get(c);
303  int update_altref = vp8_rac_get(c);
304 
305  s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
306  s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
307 }
308 
309 static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
310 {
311  VP56RangeCoder *c = &s->c;
312  int header_size, hscale, vscale, i, j, k, l, m, ret;
313  int width = s->avctx->width;
314  int height = s->avctx->height;
315 
316  s->keyframe = !(buf[0] & 1);
317  s->profile = (buf[0]>>1) & 7;
318  s->invisible = !(buf[0] & 0x10);
319  header_size = AV_RL24(buf) >> 5;
320  buf += 3;
321  buf_size -= 3;
322 
323  if (s->profile > 3)
324  av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
325 
326  if (!s->profile)
327  memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
328  else // profile 1-3 use bilinear, 4+ aren't defined so whatever
330 
331  if (header_size > buf_size - 7*s->keyframe) {
332  av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
333  return AVERROR_INVALIDDATA;
334  }
335 
336  if (s->keyframe) {
337  if (AV_RL24(buf) != 0x2a019d) {
338  av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
339  return AVERROR_INVALIDDATA;
340  }
341  width = AV_RL16(buf+3) & 0x3fff;
342  height = AV_RL16(buf+5) & 0x3fff;
343  hscale = buf[4] >> 6;
344  vscale = buf[6] >> 6;
345  buf += 7;
346  buf_size -= 7;
347 
348  if (hscale || vscale)
349  av_log_missing_feature(s->avctx, "Upscaling", 1);
350 
352  for (i = 0; i < 4; i++)
353  for (j = 0; j < 16; j++)
354  memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
355  sizeof(s->prob->token[i][j]));
356  memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
357  memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
358  memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
359  memset(&s->segmentation, 0, sizeof(s->segmentation));
360  memset(&s->lf_delta, 0, sizeof(s->lf_delta));
361  }
362 
363  ff_vp56_init_range_decoder(c, buf, header_size);
364  buf += header_size;
365  buf_size -= header_size;
366 
367  if (s->keyframe) {
368  if (vp8_rac_get(c))
369  av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
370  vp8_rac_get(c); // whether we can skip clamping in dsp functions
371  }
372 
373  if ((s->segmentation.enabled = vp8_rac_get(c)))
375  else
376  s->segmentation.update_map = 0; // FIXME: move this to some init function?
377 
378  s->filter.simple = vp8_rac_get(c);
379  s->filter.level = vp8_rac_get_uint(c, 6);
380  s->filter.sharpness = vp8_rac_get_uint(c, 3);
381 
382  if ((s->lf_delta.enabled = vp8_rac_get(c)))
383  if (vp8_rac_get(c))
384  update_lf_deltas(s);
385 
386  if (setup_partitions(s, buf, buf_size)) {
387  av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
388  return AVERROR_INVALIDDATA;
389  }
390 
391  if (!s->macroblocks_base || /* first frame */
392  width != s->avctx->width || height != s->avctx->height) {
393  if ((ret = update_dimensions(s, width, height)) < 0)
394  return ret;
395  }
396 
397  get_quants(s);
398 
399  if (!s->keyframe) {
400  update_refs(s);
402  s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
403  }
404 
405  // if we aren't saving this frame's probabilities for future frames,
406  // make a copy of the current probabilities
407  if (!(s->update_probabilities = vp8_rac_get(c)))
408  s->prob[1] = s->prob[0];
409 
410  s->update_last = s->keyframe || vp8_rac_get(c);
411 
412  for (i = 0; i < 4; i++)
413  for (j = 0; j < 8; j++)
414  for (k = 0; k < 3; k++)
415  for (l = 0; l < NUM_DCT_TOKENS-1; l++)
417  int prob = vp8_rac_get_uint(c, 8);
418  for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
419  s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
420  }
421 
422  if ((s->mbskip_enabled = vp8_rac_get(c)))
423  s->prob->mbskip = vp8_rac_get_uint(c, 8);
424 
425  if (!s->keyframe) {
426  s->prob->intra = vp8_rac_get_uint(c, 8);
427  s->prob->last = vp8_rac_get_uint(c, 8);
428  s->prob->golden = vp8_rac_get_uint(c, 8);
429 
430  if (vp8_rac_get(c))
431  for (i = 0; i < 4; i++)
432  s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
433  if (vp8_rac_get(c))
434  for (i = 0; i < 3; i++)
435  s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
436 
437  // 17.2 MV probability update
438  for (i = 0; i < 2; i++)
439  for (j = 0; j < 19; j++)
441  s->prob->mvc[i][j] = vp8_rac_get_nn(c);
442  }
443 
444  return 0;
445 }
446 
447 static av_always_inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
448 {
449  dst->x = av_clip(src->x, s->mv_min.x, s->mv_max.x);
450  dst->y = av_clip(src->y, s->mv_min.y, s->mv_max.y);
451 }
452 
456 static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
457 {
458  int bit, x = 0;
459 
460  if (vp56_rac_get_prob_branchy(c, p[0])) {
461  int i;
462 
463  for (i = 0; i < 3; i++)
464  x += vp56_rac_get_prob(c, p[9 + i]) << i;
465  for (i = 9; i > 3; i--)
466  x += vp56_rac_get_prob(c, p[9 + i]) << i;
467  if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
468  x += 8;
469  } else {
470  // small_mvtree
471  const uint8_t *ps = p+2;
472  bit = vp56_rac_get_prob(c, *ps);
473  ps += 1 + 3*bit;
474  x += 4*bit;
475  bit = vp56_rac_get_prob(c, *ps);
476  ps += 1 + bit;
477  x += 2*bit;
478  x += vp56_rac_get_prob(c, *ps);
479  }
480 
481  return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
482 }
483 
484 static av_always_inline
485 const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
486 {
487  if (left == top)
488  return vp8_submv_prob[4-!!left];
489  if (!top)
490  return vp8_submv_prob[2];
491  return vp8_submv_prob[1-!!left];
492 }
493 
498 static av_always_inline
500 {
501  int part_idx;
502  int n, num;
503  VP8Macroblock *top_mb;
504  VP8Macroblock *left_mb = &mb[-1];
505  const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
506  *mbsplits_top,
507  *mbsplits_cur, *firstidx;
508  VP56mv *top_mv;
509  VP56mv *left_mv = left_mb->bmv;
510  VP56mv *cur_mv = mb->bmv;
511 
512  if (!layout) // layout is inlined, s->mb_layout is not
513  top_mb = &mb[2];
514  else
515  top_mb = &mb[-s->mb_width-1];
516  mbsplits_top = vp8_mbsplits[top_mb->partitioning];
517  top_mv = top_mb->bmv;
518 
522  } else {
523  part_idx = VP8_SPLITMVMODE_8x8;
524  }
525  } else {
526  part_idx = VP8_SPLITMVMODE_4x4;
527  }
528 
529  num = vp8_mbsplit_count[part_idx];
530  mbsplits_cur = vp8_mbsplits[part_idx],
531  firstidx = vp8_mbfirstidx[part_idx];
532  mb->partitioning = part_idx;
533 
534  for (n = 0; n < num; n++) {
535  int k = firstidx[n];
536  uint32_t left, above;
537  const uint8_t *submv_prob;
538 
539  if (!(k & 3))
540  left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
541  else
542  left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
543  if (k <= 3)
544  above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
545  else
546  above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
547 
548  submv_prob = get_submv_prob(left, above);
549 
550  if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
551  if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
552  if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
553  mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
554  mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
555  } else {
556  AV_ZERO32(&mb->bmv[n]);
557  }
558  } else {
559  AV_WN32A(&mb->bmv[n], above);
560  }
561  } else {
562  AV_WN32A(&mb->bmv[n], left);
563  }
564  }
565 
566  return num;
567 }
568 
569 static av_always_inline
570 void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
571 {
572  VP8Macroblock *mb_edge[3] = { 0 /* top */,
573  mb - 1 /* left */,
574  0 /* top-left */ };
575  enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
576  enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
577  int idx = CNT_ZERO;
578  int cur_sign_bias = s->sign_bias[mb->ref_frame];
579  int8_t *sign_bias = s->sign_bias;
580  VP56mv near_mv[4];
581  uint8_t cnt[4] = { 0 };
582  VP56RangeCoder *c = &s->c;
583 
584  if (!layout) { // layout is inlined (s->mb_layout is not)
585  mb_edge[0] = mb + 2;
586  mb_edge[2] = mb + 1;
587  }
588  else {
589  mb_edge[0] = mb - s->mb_width-1;
590  mb_edge[2] = mb - s->mb_width-2;
591  }
592 
593  AV_ZERO32(&near_mv[0]);
594  AV_ZERO32(&near_mv[1]);
595  AV_ZERO32(&near_mv[2]);
596 
597  /* Process MB on top, left and top-left */
598  #define MV_EDGE_CHECK(n)\
599  {\
600  VP8Macroblock *edge = mb_edge[n];\
601  int edge_ref = edge->ref_frame;\
602  if (edge_ref != VP56_FRAME_CURRENT) {\
603  uint32_t mv = AV_RN32A(&edge->mv);\
604  if (mv) {\
605  if (cur_sign_bias != sign_bias[edge_ref]) {\
606  /* SWAR negate of the values in mv. */\
607  mv = ~mv;\
608  mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
609  }\
610  if (!n || mv != AV_RN32A(&near_mv[idx]))\
611  AV_WN32A(&near_mv[++idx], mv);\
612  cnt[idx] += 1 + (n != 2);\
613  } else\
614  cnt[CNT_ZERO] += 1 + (n != 2);\
615  }\
616  }
617 
618  MV_EDGE_CHECK(0)
619  MV_EDGE_CHECK(1)
620  MV_EDGE_CHECK(2)
621 
623  if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
624  mb->mode = VP8_MVMODE_MV;
625 
626  /* If we have three distinct MVs, merge first and last if they're the same */
627  if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
628  cnt[CNT_NEAREST] += 1;
629 
630  /* Swap near and nearest if necessary */
631  if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
632  FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
633  FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
634  }
635 
636  if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
637  if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
638 
639  /* Choose the best mv out of 0,0 and the nearest mv */
640  clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
641  cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
642  (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
643  (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
644 
645  if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
646  mb->mode = VP8_MVMODE_SPLIT;
647  mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout) - 1];
648  } else {
649  mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
650  mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
651  mb->bmv[0] = mb->mv;
652  }
653  } else {
654  clamp_mv(s, &mb->mv, &near_mv[CNT_NEAR]);
655  mb->bmv[0] = mb->mv;
656  }
657  } else {
658  clamp_mv(s, &mb->mv, &near_mv[CNT_NEAREST]);
659  mb->bmv[0] = mb->mv;
660  }
661  } else {
662  mb->mode = VP8_MVMODE_ZERO;
663  AV_ZERO32(&mb->mv);
664  mb->bmv[0] = mb->mv;
665  }
666 }
667 
668 static av_always_inline
670  int mb_x, int keyframe, int layout)
671 {
672  uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
673 
674  if (layout == 1) {
675  VP8Macroblock *mb_top = mb - s->mb_width - 1;
676  memcpy(mb->intra4x4_pred_mode_top, mb_top->intra4x4_pred_mode_top, 4);
677  }
678  if (keyframe) {
679  int x, y;
680  uint8_t* top;
681  uint8_t* const left = s->intra4x4_pred_mode_left;
682  if (layout == 1)
683  top = mb->intra4x4_pred_mode_top;
684  else
685  top = s->intra4x4_pred_mode_top + 4 * mb_x;
686  for (y = 0; y < 4; y++) {
687  for (x = 0; x < 4; x++) {
688  const uint8_t *ctx;
689  ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
690  *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
691  left[y] = top[x] = *intra4x4;
692  intra4x4++;
693  }
694  }
695  } else {
696  int i;
697  for (i = 0; i < 16; i++)
699  }
700 }
701 
702 static av_always_inline
703 void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
704  uint8_t *segment, uint8_t *ref, int layout)
705 {
706  VP56RangeCoder *c = &s->c;
707 
708  if (s->segmentation.update_map)
710  else if (s->segmentation.enabled)
711  *segment = ref ? *ref : *segment;
712  mb->segment = *segment;
713 
714  mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
715 
716  if (s->keyframe) {
718 
719  if (mb->mode == MODE_I4x4) {
720  decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
721  } else {
722  const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
723  if (s->mb_layout == 1)
724  AV_WN32A(mb->intra4x4_pred_mode_top, modes);
725  else
726  AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
727  AV_WN32A( s->intra4x4_pred_mode_left, modes);
728  }
729 
732  } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
733  // inter MB, 16.2
734  if (vp56_rac_get_prob_branchy(c, s->prob->last))
735  mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
736  VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
737  else
739  s->ref_count[mb->ref_frame-1]++;
740 
741  // motion vectors, 16.3
742  decode_mvs(s, mb, mb_x, mb_y, layout);
743  } else {
744  // intra MB, 16.1
746 
747  if (mb->mode == MODE_I4x4)
748  decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
749 
753  AV_ZERO32(&mb->bmv[0]);
754  }
755 }
756 
757 #ifndef decode_block_coeffs_internal
758 
768  uint8_t probs[16][3][NUM_DCT_TOKENS-1],
769  int i, uint8_t *token_prob, int16_t qmul[2])
770 {
771  VP56RangeCoder c = *r;
772  goto skip_eob;
773  do {
774  int coeff;
775  if (!vp56_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
776  break;
777 
778 skip_eob:
779  if (!vp56_rac_get_prob_branchy(&c, token_prob[1])) { // DCT_0
780  if (++i == 16)
781  break; // invalid input; blocks should end with EOB
782  token_prob = probs[i][0];
783  goto skip_eob;
784  }
785 
786  if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
787  coeff = 1;
788  token_prob = probs[i+1][1];
789  } else {
790  if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
791  coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);
792  if (coeff)
793  coeff += vp56_rac_get_prob(&c, token_prob[5]);
794  coeff += 2;
795  } else {
796  // DCT_CAT*
797  if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
798  if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
799  coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
800  } else { // DCT_CAT2
801  coeff = 7;
802  coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
803  coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
804  }
805  } else { // DCT_CAT3 and up
806  int a = vp56_rac_get_prob(&c, token_prob[8]);
807  int b = vp56_rac_get_prob(&c, token_prob[9+a]);
808  int cat = (a<<1) + b;
809  coeff = 3 + (8<<cat);
810  coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
811  }
812  }
813  token_prob = probs[i+1][2];
814  }
815  block[zigzag_scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
816  } while (++i < 16);
817 
818  *r = c;
819  return i;
820 }
821 #endif
822 
834 static av_always_inline
836  uint8_t probs[16][3][NUM_DCT_TOKENS-1],
837  int i, int zero_nhood, int16_t qmul[2])
838 {
839  uint8_t *token_prob = probs[i][zero_nhood];
840  if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
841  return 0;
842  return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
843 }
844 
845 static av_always_inline
847  uint8_t t_nnz[9], uint8_t l_nnz[9])
848 {
849  int i, x, y, luma_start = 0, luma_ctx = 3;
850  int nnz_pred, nnz, nnz_total = 0;
851  int segment = mb->segment;
852  int block_dc = 0;
853 
854  if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
855  nnz_pred = t_nnz[8] + l_nnz[8];
856 
857  // decode DC values and do hadamard
858  nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0, nnz_pred,
859  s->qmat[segment].luma_dc_qmul);
860  l_nnz[8] = t_nnz[8] = !!nnz;
861  if (nnz) {
862  nnz_total += nnz;
863  block_dc = 1;
864  if (nnz == 1)
866  else
867  s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
868  }
869  luma_start = 1;
870  luma_ctx = 0;
871  }
872 
873  // luma blocks
874  for (y = 0; y < 4; y++)
875  for (x = 0; x < 4; x++) {
876  nnz_pred = l_nnz[y] + t_nnz[x];
877  nnz = decode_block_coeffs(c, td->block[y][x], s->prob->token[luma_ctx], luma_start,
878  nnz_pred, s->qmat[segment].luma_qmul);
879  // nnz+block_dc may be one more than the actual last index, but we don't care
880  td->non_zero_count_cache[y][x] = nnz + block_dc;
881  t_nnz[x] = l_nnz[y] = !!nnz;
882  nnz_total += nnz;
883  }
884 
885  // chroma blocks
886  // TODO: what to do about dimensions? 2nd dim for luma is x,
887  // but for chroma it's (y<<1)|x
888  for (i = 4; i < 6; i++)
889  for (y = 0; y < 2; y++)
890  for (x = 0; x < 2; x++) {
891  nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
892  nnz = decode_block_coeffs(c, td->block[i][(y<<1)+x], s->prob->token[2], 0,
893  nnz_pred, s->qmat[segment].chroma_qmul);
894  td->non_zero_count_cache[i][(y<<1)+x] = nnz;
895  t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
896  nnz_total += nnz;
897  }
898 
899  // if there were no coded coeffs despite the macroblock not being marked skip,
900  // we MUST not do the inner loop filter and should not do IDCT
901  // Since skip isn't used for bitstream prediction, just manually set it.
902  if (!nnz_total)
903  mb->skip = 1;
904 }
905 
906 static av_always_inline
907 void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
908  int linesize, int uvlinesize, int simple)
909 {
910  AV_COPY128(top_border, src_y + 15*linesize);
911  if (!simple) {
912  AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
913  AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
914  }
915 }
916 
917 static av_always_inline
918 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
919  int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
920  int simple, int xchg)
921 {
922  uint8_t *top_border_m1 = top_border-32; // for TL prediction
923  src_y -= linesize;
924  src_cb -= uvlinesize;
925  src_cr -= uvlinesize;
926 
927 #define XCHG(a,b,xchg) do { \
928  if (xchg) AV_SWAP64(b,a); \
929  else AV_COPY64(b,a); \
930  } while (0)
931 
932  XCHG(top_border_m1+8, src_y-8, xchg);
933  XCHG(top_border, src_y, xchg);
934  XCHG(top_border+8, src_y+8, 1);
935  if (mb_x < mb_width-1)
936  XCHG(top_border+32, src_y+16, 1);
937 
938  // only copy chroma for normal loop filter
939  // or to initialize the top row to 127
940  if (!simple || !mb_y) {
941  XCHG(top_border_m1+16, src_cb-8, xchg);
942  XCHG(top_border_m1+24, src_cr-8, xchg);
943  XCHG(top_border+16, src_cb, 1);
944  XCHG(top_border+24, src_cr, 1);
945  }
946 }
947 
948 static av_always_inline
949 int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
950 {
951  if (!mb_x) {
952  return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
953  } else {
954  return mb_y ? mode : LEFT_DC_PRED8x8;
955  }
956 }
957 
958 static av_always_inline
959 int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
960 {
961  if (!mb_x) {
962  return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
963  } else {
964  return mb_y ? mode : HOR_PRED8x8;
965  }
966 }
967 
968 static av_always_inline
969 int check_intra_pred8x8_mode(int mode, int mb_x, int mb_y)
970 {
971  if (mode == DC_PRED8x8) {
972  return check_dc_pred8x8_mode(mode, mb_x, mb_y);
973  } else {
974  return mode;
975  }
976 }
977 
978 static av_always_inline
979 int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
980 {
981  switch (mode) {
982  case DC_PRED8x8:
983  return check_dc_pred8x8_mode(mode, mb_x, mb_y);
984  case VERT_PRED8x8:
985  return !mb_y ? DC_127_PRED8x8 : mode;
986  case HOR_PRED8x8:
987  return !mb_x ? DC_129_PRED8x8 : mode;
988  case PLANE_PRED8x8 /*TM*/:
989  return check_tm_pred8x8_mode(mode, mb_x, mb_y);
990  }
991  return mode;
992 }
993 
994 static av_always_inline
995 int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
996 {
997  if (!mb_x) {
998  return mb_y ? VERT_VP8_PRED : DC_129_PRED;
999  } else {
1000  return mb_y ? mode : HOR_VP8_PRED;
1001  }
1002 }
1003 
1004 static av_always_inline
1005 int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
1006 {
1007  switch (mode) {
1008  case VERT_PRED:
1009  if (!mb_x && mb_y) {
1010  *copy_buf = 1;
1011  return mode;
1012  }
1013  /* fall-through */
1014  case DIAG_DOWN_LEFT_PRED:
1015  case VERT_LEFT_PRED:
1016  return !mb_y ? DC_127_PRED : mode;
1017  case HOR_PRED:
1018  if (!mb_y) {
1019  *copy_buf = 1;
1020  return mode;
1021  }
1022  /* fall-through */
1023  case HOR_UP_PRED:
1024  return !mb_x ? DC_129_PRED : mode;
1025  case TM_VP8_PRED:
1026  return check_tm_pred4x4_mode(mode, mb_x, mb_y);
1027  case DC_PRED: // 4x4 DC doesn't use the same "H.264-style" exceptions as 16x16/8x8 DC
1028  case DIAG_DOWN_RIGHT_PRED:
1029  case VERT_RIGHT_PRED:
1030  case HOR_DOWN_PRED:
1031  if (!mb_y || !mb_x)
1032  *copy_buf = 1;
1033  return mode;
1034  }
1035  return mode;
1036 }
1037 
1038 static av_always_inline
1040  VP8Macroblock *mb, int mb_x, int mb_y)
1041 {
1042  AVCodecContext *avctx = s->avctx;
1043  int x, y, mode, nnz;
1044  uint32_t tr;
1045 
1046  // for the first row, we need to run xchg_mb_border to init the top edge to 127
1047  // otherwise, skip it if we aren't going to deblock
1048  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1049  xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
1050  s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1051  s->filter.simple, 1);
1052 
1053  if (mb->mode < MODE_I4x4) {
1054  if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // tested
1055  mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
1056  } else {
1057  mode = check_intra_pred8x8_mode(mb->mode, mb_x, mb_y);
1058  }
1059  s->hpc.pred16x16[mode](dst[0], s->linesize);
1060  } else {
1061  uint8_t *ptr = dst[0];
1062  uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1063  uint8_t tr_top[4] = { 127, 127, 127, 127 };
1064 
1065  // all blocks on the right edge of the macroblock use bottom edge
1066  // the top macroblock for their topright edge
1067  uint8_t *tr_right = ptr - s->linesize + 16;
1068 
1069  // if we're on the right edge of the frame, said edge is extended
1070  // from the top macroblock
1071  if (!(!mb_y && avctx->flags & CODEC_FLAG_EMU_EDGE) &&
1072  mb_x == s->mb_width-1) {
1073  tr = tr_right[-1]*0x01010101u;
1074  tr_right = (uint8_t *)&tr;
1075  }
1076 
1077  if (mb->skip)
1079 
1080  for (y = 0; y < 4; y++) {
1081  uint8_t *topright = ptr + 4 - s->linesize;
1082  for (x = 0; x < 4; x++) {
1083  int copy = 0, linesize = s->linesize;
1084  uint8_t *dst = ptr+4*x;
1085  DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
1086 
1087  if ((y == 0 || x == 3) && mb_y == 0 && avctx->flags & CODEC_FLAG_EMU_EDGE) {
1088  topright = tr_top;
1089  } else if (x == 3)
1090  topright = tr_right;
1091 
1092  if (avctx->flags & CODEC_FLAG_EMU_EDGE) { // mb_x+x or mb_y+y is a hack but works
1093  mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, &copy);
1094  if (copy) {
1095  dst = copy_dst + 12;
1096  linesize = 8;
1097  if (!(mb_y + y)) {
1098  copy_dst[3] = 127U;
1099  AV_WN32A(copy_dst+4, 127U * 0x01010101U);
1100  } else {
1101  AV_COPY32(copy_dst+4, ptr+4*x-s->linesize);
1102  if (!(mb_x + x)) {
1103  copy_dst[3] = 129U;
1104  } else {
1105  copy_dst[3] = ptr[4*x-s->linesize-1];
1106  }
1107  }
1108  if (!(mb_x + x)) {
1109  copy_dst[11] =
1110  copy_dst[19] =
1111  copy_dst[27] =
1112  copy_dst[35] = 129U;
1113  } else {
1114  copy_dst[11] = ptr[4*x -1];
1115  copy_dst[19] = ptr[4*x+s->linesize -1];
1116  copy_dst[27] = ptr[4*x+s->linesize*2-1];
1117  copy_dst[35] = ptr[4*x+s->linesize*3-1];
1118  }
1119  }
1120  } else {
1121  mode = intra4x4[x];
1122  }
1123  s->hpc.pred4x4[mode](dst, topright, linesize);
1124  if (copy) {
1125  AV_COPY32(ptr+4*x , copy_dst+12);
1126  AV_COPY32(ptr+4*x+s->linesize , copy_dst+20);
1127  AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
1128  AV_COPY32(ptr+4*x+s->linesize*3, copy_dst+36);
1129  }
1130 
1131  nnz = td->non_zero_count_cache[y][x];
1132  if (nnz) {
1133  if (nnz == 1)
1134  s->vp8dsp.vp8_idct_dc_add(ptr+4*x, td->block[y][x], s->linesize);
1135  else
1136  s->vp8dsp.vp8_idct_add(ptr+4*x, td->block[y][x], s->linesize);
1137  }
1138  topright += 4;
1139  }
1140 
1141  ptr += 4*s->linesize;
1142  intra4x4 += 4;
1143  }
1144  }
1145 
1146  if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
1147  mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode, mb_x, mb_y);
1148  } else {
1149  mode = check_intra_pred8x8_mode(mb->chroma_pred_mode, mb_x, mb_y);
1150  }
1151  s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
1152  s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
1153 
1154  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1155  xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
1156  s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1157  s->filter.simple, 0);
1158 }
1159 
1160 static const uint8_t subpel_idx[3][8] = {
1161  { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
1162  // also function pointer index
1163  { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
1164  { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
1165 };
1166 
1183 static av_always_inline
1185  AVFrame *ref, const VP56mv *mv,
1186  int x_off, int y_off, int block_w, int block_h,
1187  int width, int height, int linesize,
1188  vp8_mc_func mc_func[3][3])
1189 {
1190  uint8_t *src = ref->data[0];
1191 
1192  if (AV_RN32A(mv)) {
1193 
1194  int mx = (mv->x << 1)&7, mx_idx = subpel_idx[0][mx];
1195  int my = (mv->y << 1)&7, my_idx = subpel_idx[0][my];
1196 
1197  x_off += mv->x >> 2;
1198  y_off += mv->y >> 2;
1199 
1200  // edge emulation
1201  ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
1202  src += y_off * linesize + x_off;
1203  if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1204  y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1205  s->vdsp.emulated_edge_mc(td->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize,
1206  block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1207  x_off - mx_idx, y_off - my_idx, width, height);
1208  src = td->edge_emu_buffer + mx_idx + linesize * my_idx;
1209  }
1210  mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
1211  } else {
1212  ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
1213  mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
1214  }
1215 }
1216 
1234 static av_always_inline
1236  AVFrame *ref, const VP56mv *mv, int x_off, int y_off,
1237  int block_w, int block_h, int width, int height, int linesize,
1238  vp8_mc_func mc_func[3][3])
1239 {
1240  uint8_t *src1 = ref->data[1], *src2 = ref->data[2];
1241 
1242  if (AV_RN32A(mv)) {
1243  int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
1244  int my = mv->y&7, my_idx = subpel_idx[0][my];
1245 
1246  x_off += mv->x >> 3;
1247  y_off += mv->y >> 3;
1248 
1249  // edge emulation
1250  src1 += y_off * linesize + x_off;
1251  src2 += y_off * linesize + x_off;
1252  ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
1253  if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
1254  y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
1255  s->vdsp.emulated_edge_mc(td->edge_emu_buffer, src1 - my_idx * linesize - mx_idx, linesize,
1256  block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1257  x_off - mx_idx, y_off - my_idx, width, height);
1258  src1 = td->edge_emu_buffer + mx_idx + linesize * my_idx;
1259  mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
1260 
1261  s->vdsp.emulated_edge_mc(td->edge_emu_buffer, src2 - my_idx * linesize - mx_idx, linesize,
1262  block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1263  x_off - mx_idx, y_off - my_idx, width, height);
1264  src2 = td->edge_emu_buffer + mx_idx + linesize * my_idx;
1265  mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
1266  } else {
1267  mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
1268  mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
1269  }
1270  } else {
1271  ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
1272  mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1273  mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
1274  }
1275 }
1276 
1277 static av_always_inline
1279  AVFrame *ref_frame, int x_off, int y_off,
1280  int bx_off, int by_off,
1281  int block_w, int block_h,
1282  int width, int height, VP56mv *mv)
1283 {
1284  VP56mv uvmv = *mv;
1285 
1286  /* Y */
1287  vp8_mc_luma(s, td, dst[0] + by_off * s->linesize + bx_off,
1288  ref_frame, mv, x_off + bx_off, y_off + by_off,
1289  block_w, block_h, width, height, s->linesize,
1290  s->put_pixels_tab[block_w == 8]);
1291 
1292  /* U/V */
1293  if (s->profile == 3) {
1294  uvmv.x &= ~7;
1295  uvmv.y &= ~7;
1296  }
1297  x_off >>= 1; y_off >>= 1;
1298  bx_off >>= 1; by_off >>= 1;
1299  width >>= 1; height >>= 1;
1300  block_w >>= 1; block_h >>= 1;
1301  vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
1302  dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
1303  &uvmv, x_off + bx_off, y_off + by_off,
1304  block_w, block_h, width, height, s->uvlinesize,
1305  s->put_pixels_tab[1 + (block_w == 4)]);
1306 }
1307 
1308 /* Fetch pixels for estimated mv 4 macroblocks ahead.
1309  * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1310 static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
1311 {
1312  /* Don't prefetch refs that haven't been used very often this frame. */
1313  if (s->ref_count[ref-1] > (mb_xy >> 5)) {
1314  int x_off = mb_x << 4, y_off = mb_y << 4;
1315  int mx = (mb->mv.x>>2) + x_off + 8;
1316  int my = (mb->mv.y>>2) + y_off;
1317  uint8_t **src= s->framep[ref]->data;
1318  int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
1319  /* For threading, a ff_thread_await_progress here might be useful, but
1320  * it actually slows down the decoder. Since a bad prefetch doesn't
1321  * generate bad decoder output, we don't run it here. */
1322  s->vdsp.prefetch(src[0]+off, s->linesize, 4);
1323  off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1324  s->vdsp.prefetch(src[1]+off, src[2]-src[1], 2);
1325  }
1326 }
1327 
1331 static av_always_inline
1333  VP8Macroblock *mb, int mb_x, int mb_y)
1334 {
1335  int x_off = mb_x << 4, y_off = mb_y << 4;
1336  int width = 16*s->mb_width, height = 16*s->mb_height;
1337  AVFrame *ref = s->framep[mb->ref_frame];
1338  VP56mv *bmv = mb->bmv;
1339 
1340  switch (mb->partitioning) {
1341  case VP8_SPLITMVMODE_NONE:
1342  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1343  0, 0, 16, 16, width, height, &mb->mv);
1344  break;
1345  case VP8_SPLITMVMODE_4x4: {
1346  int x, y;
1347  VP56mv uvmv;
1348 
1349  /* Y */
1350  for (y = 0; y < 4; y++) {
1351  for (x = 0; x < 4; x++) {
1352  vp8_mc_luma(s, td, dst[0] + 4*y*s->linesize + x*4,
1353  ref, &bmv[4*y + x],
1354  4*x + x_off, 4*y + y_off, 4, 4,
1355  width, height, s->linesize,
1356  s->put_pixels_tab[2]);
1357  }
1358  }
1359 
1360  /* U/V */
1361  x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
1362  for (y = 0; y < 2; y++) {
1363  for (x = 0; x < 2; x++) {
1364  uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
1365  mb->bmv[ 2*y * 4 + 2*x+1].x +
1366  mb->bmv[(2*y+1) * 4 + 2*x ].x +
1367  mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1368  uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
1369  mb->bmv[ 2*y * 4 + 2*x+1].y +
1370  mb->bmv[(2*y+1) * 4 + 2*x ].y +
1371  mb->bmv[(2*y+1) * 4 + 2*x+1].y;
1372  uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1373  uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
1374  if (s->profile == 3) {
1375  uvmv.x &= ~7;
1376  uvmv.y &= ~7;
1377  }
1378  vp8_mc_chroma(s, td, dst[1] + 4*y*s->uvlinesize + x*4,
1379  dst[2] + 4*y*s->uvlinesize + x*4, ref, &uvmv,
1380  4*x + x_off, 4*y + y_off, 4, 4,
1381  width, height, s->uvlinesize,
1382  s->put_pixels_tab[2]);
1383  }
1384  }
1385  break;
1386  }
1387  case VP8_SPLITMVMODE_16x8:
1388  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1389  0, 0, 16, 8, width, height, &bmv[0]);
1390  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1391  0, 8, 16, 8, width, height, &bmv[1]);
1392  break;
1393  case VP8_SPLITMVMODE_8x16:
1394  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1395  0, 0, 8, 16, width, height, &bmv[0]);
1396  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1397  8, 0, 8, 16, width, height, &bmv[1]);
1398  break;
1399  case VP8_SPLITMVMODE_8x8:
1400  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1401  0, 0, 8, 8, width, height, &bmv[0]);
1402  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1403  8, 0, 8, 8, width, height, &bmv[1]);
1404  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1405  0, 8, 8, 8, width, height, &bmv[2]);
1406  vp8_mc_part(s, td, dst, ref, x_off, y_off,
1407  8, 8, 8, 8, width, height, &bmv[3]);
1408  break;
1409  }
1410 }
1411 
1413  uint8_t *dst[3], VP8Macroblock *mb)
1414 {
1415  int x, y, ch;
1416 
1417  if (mb->mode != MODE_I4x4) {
1418  uint8_t *y_dst = dst[0];
1419  for (y = 0; y < 4; y++) {
1420  uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
1421  if (nnz4) {
1422  if (nnz4&~0x01010101) {
1423  for (x = 0; x < 4; x++) {
1424  if ((uint8_t)nnz4 == 1)
1425  s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, td->block[y][x], s->linesize);
1426  else if((uint8_t)nnz4 > 1)
1427  s->vp8dsp.vp8_idct_add(y_dst+4*x, td->block[y][x], s->linesize);
1428  nnz4 >>= 8;
1429  if (!nnz4)
1430  break;
1431  }
1432  } else {
1433  s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
1434  }
1435  }
1436  y_dst += 4*s->linesize;
1437  }
1438  }
1439 
1440  for (ch = 0; ch < 2; ch++) {
1441  uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4+ch]);
1442  if (nnz4) {
1443  uint8_t *ch_dst = dst[1+ch];
1444  if (nnz4&~0x01010101) {
1445  for (y = 0; y < 2; y++) {
1446  for (x = 0; x < 2; x++) {
1447  if ((uint8_t)nnz4 == 1)
1448  s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
1449  else if((uint8_t)nnz4 > 1)
1450  s->vp8dsp.vp8_idct_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
1451  nnz4 >>= 8;
1452  if (!nnz4)
1453  goto chroma_idct_end;
1454  }
1455  ch_dst += 4*s->uvlinesize;
1456  }
1457  } else {
1458  s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4+ch], s->uvlinesize);
1459  }
1460  }
1461 chroma_idct_end: ;
1462  }
1463 }
1464 
1466 {
1467  int interior_limit, filter_level;
1468 
1469  if (s->segmentation.enabled) {
1470  filter_level = s->segmentation.filter_level[mb->segment];
1471  if (!s->segmentation.absolute_vals)
1472  filter_level += s->filter.level;
1473  } else
1474  filter_level = s->filter.level;
1475 
1476  if (s->lf_delta.enabled) {
1477  filter_level += s->lf_delta.ref[mb->ref_frame];
1478  filter_level += s->lf_delta.mode[mb->mode];
1479  }
1480 
1481  filter_level = av_clip_uintp2(filter_level, 6);
1482 
1483  interior_limit = filter_level;
1484  if (s->filter.sharpness) {
1485  interior_limit >>= (s->filter.sharpness + 3) >> 2;
1486  interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
1487  }
1488  interior_limit = FFMAX(interior_limit, 1);
1489 
1490  f->filter_level = filter_level;
1491  f->inner_limit = interior_limit;
1492  f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
1493 }
1494 
1495 static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
1496 {
1497  int mbedge_lim, bedge_lim, hev_thresh;
1498  int filter_level = f->filter_level;
1499  int inner_limit = f->inner_limit;
1500  int inner_filter = f->inner_filter;
1501  int linesize = s->linesize;
1502  int uvlinesize = s->uvlinesize;
1503  static const uint8_t hev_thresh_lut[2][64] = {
1504  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1505  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1506  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1507  3, 3, 3, 3 },
1508  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1509  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1510  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1511  2, 2, 2, 2 }
1512  };
1513 
1514  if (!filter_level)
1515  return;
1516 
1517  bedge_lim = 2*filter_level + inner_limit;
1518  mbedge_lim = bedge_lim + 4;
1519 
1520  hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
1521 
1522  if (mb_x) {
1523  s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
1524  mbedge_lim, inner_limit, hev_thresh);
1525  s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
1526  mbedge_lim, inner_limit, hev_thresh);
1527  }
1528 
1529  if (inner_filter) {
1530  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
1531  inner_limit, hev_thresh);
1532  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
1533  inner_limit, hev_thresh);
1534  s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
1535  inner_limit, hev_thresh);
1536  s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
1537  uvlinesize, bedge_lim,
1538  inner_limit, hev_thresh);
1539  }
1540 
1541  if (mb_y) {
1542  s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
1543  mbedge_lim, inner_limit, hev_thresh);
1544  s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
1545  mbedge_lim, inner_limit, hev_thresh);
1546  }
1547 
1548  if (inner_filter) {
1549  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
1550  linesize, bedge_lim,
1551  inner_limit, hev_thresh);
1552  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
1553  linesize, bedge_lim,
1554  inner_limit, hev_thresh);
1555  s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
1556  linesize, bedge_lim,
1557  inner_limit, hev_thresh);
1558  s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
1559  dst[2] + 4 * uvlinesize,
1560  uvlinesize, bedge_lim,
1561  inner_limit, hev_thresh);
1562  }
1563 }
1564 
1565 static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
1566 {
1567  int mbedge_lim, bedge_lim;
1568  int filter_level = f->filter_level;
1569  int inner_limit = f->inner_limit;
1570  int inner_filter = f->inner_filter;
1571  int linesize = s->linesize;
1572 
1573  if (!filter_level)
1574  return;
1575 
1576  bedge_lim = 2*filter_level + inner_limit;
1577  mbedge_lim = bedge_lim + 4;
1578 
1579  if (mb_x)
1580  s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
1581  if (inner_filter) {
1582  s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
1583  s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
1584  s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
1585  }
1586 
1587  if (mb_y)
1588  s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
1589  if (inner_filter) {
1590  s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
1591  s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
1592  s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
1593  }
1594 }
1595 
1596 static void release_queued_segmaps(VP8Context *s, int is_close)
1597 {
1598  int leave_behind = is_close ? 0 : !s->maps_are_invalid;
1599  while (s->num_maps_to_be_freed > leave_behind)
1601  s->maps_are_invalid = 0;
1602 }
1603 
1604 #define MARGIN (16 << 2)
1605 static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, AVFrame *curframe,
1606  AVFrame *prev_frame)
1607 {
1608  VP8Context *s = avctx->priv_data;
1609  int mb_x, mb_y;
1610 
1611  s->mv_min.y = -MARGIN;
1612  s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
1613  for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
1614  VP8Macroblock *mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
1615  int mb_xy = mb_y*s->mb_width;
1616 
1617  AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
1618 
1619  s->mv_min.x = -MARGIN;
1620  s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
1621  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
1622  if (mb_y == 0)
1623  AV_WN32A((mb-s->mb_width-1)->intra4x4_pred_mode_top, DC_PRED*0x01010101);
1624  decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
1625  prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL, 1);
1626  s->mv_min.x -= 64;
1627  s->mv_max.x -= 64;
1628  }
1629  s->mv_min.y -= 64;
1630  s->mv_max.y -= 64;
1631  }
1632 }
1633 
1634 #if HAVE_THREADS
1635 #define check_thread_pos(td, otd, mb_x_check, mb_y_check)\
1636  do {\
1637  int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF);\
1638  if (otd->thread_mb_pos < tmp) {\
1639  pthread_mutex_lock(&otd->lock);\
1640  td->wait_mb_pos = tmp;\
1641  do {\
1642  if (otd->thread_mb_pos >= tmp)\
1643  break;\
1644  pthread_cond_wait(&otd->cond, &otd->lock);\
1645  } while (1);\
1646  td->wait_mb_pos = INT_MAX;\
1647  pthread_mutex_unlock(&otd->lock);\
1648  }\
1649  } while(0);
1650 
1651 #define update_pos(td, mb_y, mb_x)\
1652  do {\
1653  int pos = (mb_y << 16) | (mb_x & 0xFFFF);\
1654  int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && (num_jobs > 1);\
1655  int is_null = (next_td == NULL) || (prev_td == NULL);\
1656  int pos_check = (is_null) ? 1 :\
1657  (next_td != td && pos >= next_td->wait_mb_pos) ||\
1658  (prev_td != td && pos >= prev_td->wait_mb_pos);\
1659  td->thread_mb_pos = pos;\
1660  if (sliced_threading && pos_check) {\
1661  pthread_mutex_lock(&td->lock);\
1662  pthread_cond_broadcast(&td->cond);\
1663  pthread_mutex_unlock(&td->lock);\
1664  }\
1665  } while(0);
1666 #else
1667 #define check_thread_pos(td, otd, mb_x_check, mb_y_check)
1668 #define update_pos(td, mb_y, mb_x)
1669 #endif
1670 
1671 static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
1672  int jobnr, int threadnr)
1673 {
1674  VP8Context *s = avctx->priv_data;
1675  VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
1676  int mb_y = td->thread_mb_pos>>16;
1677  int i, y, mb_x, mb_xy = mb_y*s->mb_width;
1678  int num_jobs = s->num_jobs;
1679  AVFrame *curframe = s->curframe, *prev_frame = s->prev_frame;
1680  VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
1681  VP8Macroblock *mb;
1682  uint8_t *dst[3] = {
1683  curframe->data[0] + 16*mb_y*s->linesize,
1684  curframe->data[1] + 8*mb_y*s->uvlinesize,
1685  curframe->data[2] + 8*mb_y*s->uvlinesize
1686  };
1687  if (mb_y == 0) prev_td = td;
1688  else prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1689  if (mb_y == s->mb_height-1) next_td = td;
1690  else next_td = &s->thread_data[(jobnr + 1)%num_jobs];
1691  if (s->mb_layout == 1)
1692  mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
1693  else {
1694  mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
1695  memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
1696  AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
1697  }
1698 
1699  memset(td->left_nnz, 0, sizeof(td->left_nnz));
1700  // left edge of 129 for intra prediction
1701  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1702  for (i = 0; i < 3; i++)
1703  for (y = 0; y < 16>>!!i; y++)
1704  dst[i][y*curframe->linesize[i]-1] = 129;
1705  if (mb_y == 1) {
1706  s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
1707  }
1708  }
1709 
1710  s->mv_min.x = -MARGIN;
1711  s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
1712 
1713  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
1714  // Wait for previous thread to read mb_x+2, and reach mb_y-1.
1715  if (prev_td != td) {
1716  if (threadnr != 0) {
1717  check_thread_pos(td, prev_td, mb_x+1, mb_y-1);
1718  } else {
1719  check_thread_pos(td, prev_td, (s->mb_width+3) + (mb_x+1), mb_y-1);
1720  }
1721  }
1722 
1723  s->vdsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1724  s->vdsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
1725 
1726  if (!s->mb_layout)
1727  decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
1728  prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL, 0);
1729 
1730  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
1731 
1732  if (!mb->skip)
1733  decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz);
1734 
1735  if (mb->mode <= MODE_I4x4)
1736  intra_predict(s, td, dst, mb, mb_x, mb_y);
1737  else
1738  inter_predict(s, td, dst, mb, mb_x, mb_y);
1739 
1740  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
1741 
1742  if (!mb->skip) {
1743  idct_mb(s, td, dst, mb);
1744  } else {
1745  AV_ZERO64(td->left_nnz);
1746  AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
1747 
1748  // Reset DC block predictors if they would exist if the mb had coefficients
1749  if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1750  td->left_nnz[8] = 0;
1751  s->top_nnz[mb_x][8] = 0;
1752  }
1753  }
1754 
1755  if (s->deblock_filter)
1756  filter_level_for_mb(s, mb, &td->filter_strength[mb_x]);
1757 
1758  if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs-1) {
1759  if (s->filter.simple)
1760  backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
1761  else
1762  backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1763  }
1764 
1765  prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
1766 
1767  dst[0] += 16;
1768  dst[1] += 8;
1769  dst[2] += 8;
1770  s->mv_min.x -= 64;
1771  s->mv_max.x -= 64;
1772 
1773  if (mb_x == s->mb_width+1) {
1774  update_pos(td, mb_y, s->mb_width+3);
1775  } else {
1776  update_pos(td, mb_y, mb_x);
1777  }
1778  }
1779 }
1780 
1781 static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
1782  int jobnr, int threadnr)
1783 {
1784  VP8Context *s = avctx->priv_data;
1785  VP8ThreadData *td = &s->thread_data[threadnr];
1786  int mb_x, mb_y = td->thread_mb_pos>>16, num_jobs = s->num_jobs;
1787  AVFrame *curframe = s->curframe;
1788  VP8Macroblock *mb;
1789  VP8ThreadData *prev_td, *next_td;
1790  uint8_t *dst[3] = {
1791  curframe->data[0] + 16*mb_y*s->linesize,
1792  curframe->data[1] + 8*mb_y*s->uvlinesize,
1793  curframe->data[2] + 8*mb_y*s->uvlinesize
1794  };
1795 
1796  if (s->mb_layout == 1)
1797  mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
1798  else
1799  mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
1800 
1801  if (mb_y == 0) prev_td = td;
1802  else prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1803  if (mb_y == s->mb_height-1) next_td = td;
1804  else next_td = &s->thread_data[(jobnr + 1)%num_jobs];
1805 
1806  for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
1807  VP8FilterStrength *f = &td->filter_strength[mb_x];
1808  if (prev_td != td) {
1809  check_thread_pos(td, prev_td, (mb_x+1) + (s->mb_width+3), mb_y-1);
1810  }
1811  if (next_td != td)
1812  if (next_td != &s->thread_data[0]) {
1813  check_thread_pos(td, next_td, mb_x+1, mb_y+1);
1814  }
1815 
1816  if (num_jobs == 1) {
1817  if (s->filter.simple)
1818  backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
1819  else
1820  backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1821  }
1822 
1823  if (s->filter.simple)
1824  filter_mb_simple(s, dst[0], f, mb_x, mb_y);
1825  else
1826  filter_mb(s, dst, f, mb_x, mb_y);
1827  dst[0] += 16;
1828  dst[1] += 8;
1829  dst[2] += 8;
1830 
1831  update_pos(td, mb_y, (s->mb_width+3) + mb_x);
1832  }
1833 }
1834 
1835 static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
1836  int jobnr, int threadnr)
1837 {
1838  VP8Context *s = avctx->priv_data;
1839  VP8ThreadData *td = &s->thread_data[jobnr];
1840  VP8ThreadData *next_td = NULL, *prev_td = NULL;
1841  AVFrame *curframe = s->curframe;
1842  int mb_y, num_jobs = s->num_jobs;
1843  td->thread_nr = threadnr;
1844  for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
1845  if (mb_y >= s->mb_height) break;
1846  td->thread_mb_pos = mb_y<<16;
1847  vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
1848  if (s->deblock_filter)
1849  vp8_filter_mb_row(avctx, tdata, jobnr, threadnr);
1850  update_pos(td, mb_y, INT_MAX & 0xFFFF);
1851 
1852  s->mv_min.y -= 64;
1853  s->mv_max.y -= 64;
1854 
1855  if (avctx->active_thread_type == FF_THREAD_FRAME)
1856  ff_thread_report_progress(curframe, mb_y, 0);
1857  }
1858 
1859  return 0;
1860 }
1861 
1862 static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1863  AVPacket *avpkt)
1864 {
1865  VP8Context *s = avctx->priv_data;
1866  int ret, i, referenced, num_jobs;
1867  enum AVDiscard skip_thresh;
1868  AVFrame *av_uninit(curframe), *prev_frame;
1869 
1870  release_queued_segmaps(s, 0);
1871 
1872  if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1873  goto err;
1874 
1875  prev_frame = s->framep[VP56_FRAME_CURRENT];
1876 
1877  referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1879 
1880  skip_thresh = !referenced ? AVDISCARD_NONREF :
1882 
1883  if (avctx->skip_frame >= skip_thresh) {
1884  s->invisible = 1;
1885  memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
1886  goto skip_decode;
1887  }
1888  s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
1889 
1890  // release no longer referenced frames
1891  for (i = 0; i < 5; i++)
1892  if (s->frames[i].data[0] &&
1893  &s->frames[i] != prev_frame &&
1894  &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1895  &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1896  &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1897  vp8_release_frame(s, &s->frames[i], 1, 0);
1898 
1899  // find a free buffer
1900  for (i = 0; i < 5; i++)
1901  if (&s->frames[i] != prev_frame &&
1902  &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1903  &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1904  &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1905  curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1906  break;
1907  }
1908  if (i == 5) {
1909  av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
1910  abort();
1911  }
1912  if (curframe->data[0])
1913  vp8_release_frame(s, curframe, 1, 0);
1914 
1915  // Given that arithmetic probabilities are updated every frame, it's quite likely
1916  // that the values we have on a random interframe are complete junk if we didn't
1917  // start decode on a keyframe. So just don't display anything rather than junk.
1918  if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1919  !s->framep[VP56_FRAME_GOLDEN] ||
1920  !s->framep[VP56_FRAME_GOLDEN2])) {
1921  av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
1922  ret = AVERROR_INVALIDDATA;
1923  goto err;
1924  }
1925 
1926  curframe->key_frame = s->keyframe;
1927  curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1928  curframe->reference = referenced ? 3 : 0;
1929  if ((ret = vp8_alloc_frame(s, curframe))) {
1930  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1931  goto err;
1932  }
1933 
1934  // check if golden and altref are swapped
1935  if (s->update_altref != VP56_FRAME_NONE) {
1937  } else {
1939  }
1940  if (s->update_golden != VP56_FRAME_NONE) {
1942  } else {
1944  }
1945  if (s->update_last) {
1946  s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
1947  } else {
1949  }
1950  s->next_framep[VP56_FRAME_CURRENT] = curframe;
1951 
1952  ff_thread_finish_setup(avctx);
1953 
1954  s->linesize = curframe->linesize[0];
1955  s->uvlinesize = curframe->linesize[1];
1956 
1957  if (!s->thread_data[0].edge_emu_buffer)
1958  for (i = 0; i < MAX_THREADS; i++)
1960 
1961  memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1962  /* Zero macroblock structures for top/top-left prediction from outside the frame. */
1963  if (!s->mb_layout)
1964  memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
1965  if (!s->mb_layout && s->keyframe)
1966  memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
1967 
1968  // top edge of 127 for intra prediction
1969  if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
1970  s->top_border[0][15] = s->top_border[0][23] = 127;
1971  s->top_border[0][31] = 127;
1972  memset(s->top_border[1], 127, s->mb_width*sizeof(*s->top_border));
1973  }
1974  memset(s->ref_count, 0, sizeof(s->ref_count));
1975 
1976 
1977  // Make sure the previous frame has read its segmentation map,
1978  // if we re-use the same map.
1979  if (prev_frame && s->segmentation.enabled && !s->segmentation.update_map)
1980  ff_thread_await_progress(prev_frame, 1, 0);
1981 
1982  if (s->mb_layout == 1)
1983  vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
1984 
1985  if (avctx->active_thread_type == FF_THREAD_FRAME)
1986  num_jobs = 1;
1987  else
1988  num_jobs = FFMIN(s->num_coeff_partitions, avctx->thread_count);
1989  s->num_jobs = num_jobs;
1990  s->curframe = curframe;
1991  s->prev_frame = prev_frame;
1992  s->mv_min.y = -MARGIN;
1993  s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
1994  for (i = 0; i < MAX_THREADS; i++) {
1995  s->thread_data[i].thread_mb_pos = 0;
1996  s->thread_data[i].wait_mb_pos = INT_MAX;
1997  }
1998  avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL, num_jobs);
1999 
2000  ff_thread_report_progress(curframe, INT_MAX, 0);
2001  memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
2002 
2003 skip_decode:
2004  // if future frames don't use the updated probabilities,
2005  // reset them to the values we saved
2006  if (!s->update_probabilities)
2007  s->prob[0] = s->prob[1];
2008 
2009  if (!s->invisible) {
2010  *(AVFrame*)data = *curframe;
2011  *got_frame = 1;
2012  }
2013 
2014  return avpkt->size;
2015 err:
2016  memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
2017  return ret;
2018 }
2019 
2021 {
2022  VP8Context *s = avctx->priv_data;
2023 
2024  s->avctx = avctx;
2025  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2026 
2027  ff_videodsp_init(&s->vdsp, 8);
2029  ff_vp8dsp_init(&s->vp8dsp);
2030 
2031  return 0;
2032 }
2033 
2035 {
2036  vp8_decode_flush_impl(avctx, 0, 1, 1);
2037  release_queued_segmaps(avctx->priv_data, 1);
2038  return 0;
2039 }
2040 
2042 {
2043  VP8Context *s = avctx->priv_data;
2044 
2045  s->avctx = avctx;
2046 
2047  return 0;
2048 }
2049 
2050 #define REBASE(pic) \
2051  pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
2052 
2054 {
2055  VP8Context *s = dst->priv_data, *s_src = src->priv_data;
2056 
2057  if (s->macroblocks_base &&
2058  (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
2059  free_buffers(s);
2060  s->maps_are_invalid = 1;
2061  s->mb_width = s_src->mb_width;
2062  s->mb_height = s_src->mb_height;
2063  }
2064 
2065  s->prob[0] = s_src->prob[!s_src->update_probabilities];
2066  s->segmentation = s_src->segmentation;
2067  s->lf_delta = s_src->lf_delta;
2068  memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
2069 
2070  memcpy(&s->frames, &s_src->frames, sizeof(s->frames));
2071  s->framep[0] = REBASE(s_src->next_framep[0]);
2072  s->framep[1] = REBASE(s_src->next_framep[1]);
2073  s->framep[2] = REBASE(s_src->next_framep[2]);
2074  s->framep[3] = REBASE(s_src->next_framep[3]);
2075 
2076  return 0;
2077 }
2078 
2080  .name = "vp8",
2081  .type = AVMEDIA_TYPE_VIDEO,
2082  .id = AV_CODEC_ID_VP8,
2083  .priv_data_size = sizeof(VP8Context),
2084  .init = vp8_decode_init,
2089  .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
2092 };
static void get_quants(VP8Context *s)
Definition: vp8.c:236
static void vp8_decode_flush_impl(AVCodecContext *avctx, int prefer_delayed_free, int can_direct_free, int free_mem)
Definition: vp8.c:95
uint8_t golden
Definition: vp8.h:225
uint8_t inner_limit
Definition: vp8.h:77
#define VERT_PRED8x8
Definition: h264pred.h:70
VP8Macroblock * macroblocks
Definition: vp8.h:168
static const uint8_t vp8_dc_qlookup[VP8_MAX_QUANT+1]
Definition: vp8data.h:645
static const uint8_t vp8_submv_prob[5][3]
Definition: vp8data.h:88
static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
Definition: vp8.c:309
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
discard all frames except keyframes
Definition: avcodec.h:535
void(* prefetch)(uint8_t *buf, ptrdiff_t stride, int h)
Prefetch memory into cache (if supported by hardware).
Definition: videodsp.h:61
static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
Definition: vp8.c:73
#define DC_128_PRED8x8
Definition: h264pred.h:76
#define VERT_LEFT_PRED
Definition: h264pred.h:45
(only used in prediction) no split MVs
Definition: vp8.h:72
int size
static void update_lf_deltas(VP8Context *s)
Definition: vp8.c:186
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
VP56mv mv_min
Definition: vp8.h:143
int8_t sign_bias[4]
one state [0, 1] per ref frame type
Definition: vp8.h:146
int coded_width
Bitstream width / height, may be different from width/height.
Definition: avcodec.h:1515
misc image utilities
static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t(*tree)[2], const uint8_t *probs)
Definition: vp56.h:361
static int vp8_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: vp8.c:2053
static av_always_inline void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1, uint8_t *dst2, AVFrame *ref, const VP56mv *mv, int x_off, int y_off, int block_w, int block_h, int width, int height, int linesize, vp8_mc_func mc_func[3][3])
chroma MC function
Definition: vp8.c:1235
uint8_t * intra4x4_pred_mode_top
Definition: vp8.h:170
uint8_t mbskip_enabled
Definition: vp8.h:141
static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
Determine which buffers golden and altref should be updated with after this frame.
Definition: vp8.c:282
void(* vp8_v_loop_filter16y)(uint8_t *dst, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:47
static void release_queued_segmaps(VP8Context *s, int is_close)
Definition: vp8.c:1596
uint8_t token[4][16][3][NUM_DCT_TOKENS-1]
Definition: vp8.h:228
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
static av_always_inline int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
Definition: vp8.c:1005
int linesize
Definition: vp8.h:136
int size
Definition: avcodec.h:916
static void vp8_decode_flush(AVCodecContext *avctx)
Definition: vp8.c:114
AVFrame * next_framep[4]
Definition: vp8.h:130
#define MARGIN
Definition: vp8.c:1604
vp8_mc_func put_vp8_bilinear_pixels_tab[3][3][3]
Definition: vp8dsp.h:80
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
VP56mv bmv[16]
Definition: vp8.h:93
#define AV_RL16
Definition: intreadwrite.h:42
uint8_t inner_filter
Definition: vp8.h:78
static const int8_t vp8_pred8x8c_tree[3][2]
Definition: vp8data.h:112
uint8_t segmentid[3]
Definition: vp8.h:221
discard all
Definition: avcodec.h:536
static av_always_inline int check_intra_pred8x8_mode(int mode, int mb_x, int mb_y)
Definition: vp8.c:969
#define HOR_PRED8x8
Definition: h264pred.h:69
AVCodec.
Definition: avcodec.h:2960
uint8_t sharpness
Definition: vp8.h:165
#define AV_WN32A(p, v)
Definition: intreadwrite.h:458
2 16x8 blocks (vertical)
Definition: vp8.h:68
#define AV_COPY32(d, s)
Definition: intreadwrite.h:506
int update_probabilities
If this flag is not set, all the probability updates are discarded after this frame is decoded...
Definition: vp8.h:242
static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f)
Definition: vp8.c:1465
static const uint8_t zigzag_scan[16]
Definition: h264data.h:55
vp8_mc_func put_vp8_epel_pixels_tab[3][3][3]
first dimension: width>>3, height is assumed equal to width second dimension: 0 if no vertical interp...
Definition: vp8dsp.h:79
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static const uint8_t vp8_pred8x8c_prob_inter[3]
Definition: vp8data.h:120
uint8_t(* top_nnz)[9]
Definition: vp8.h:210
int num_jobs
Definition: vp8.h:265
static const uint8_t vp8_mbsplits[5][16]
Definition: vp8data.h:66
enum AVDiscard skip_frame
Definition: avcodec.h:2907
#define MAX_THREADS
Definition: mpegvideo.h:62
#define AV_RN32A(p)
Definition: intreadwrite.h:446
uint8_t pred16x16[4]
Definition: vp8.h:226
static const int8_t vp8_pred16x16_tree_intra[4][2]
Definition: vp8data.h:41
uint8_t update_map
Definition: vp8.h:157
#define PLANE_PRED8x8
Definition: h264pred.h:71
uint16_t mb_height
Definition: vp8.h:135
int16_t y
Definition: vp56.h:41
static av_always_inline void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
Definition: vp8.c:570
void(* emulated_edge_mc)(uint8_t *buf, const uint8_t *src, ptrdiff_t linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:50
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
struct VP8Context::@62 filter
int update_golden
VP56_FRAME_NONE if not updated, or which frame to copy if so.
Definition: vp8.h:235
uint8_t intra4x4_pred_mode_top[4]
Definition: vp8.h:91
static av_always_inline void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width, int simple, int xchg)
Definition: vp8.c:918
struct VP8DecoderContext VP8Context
uint8_t
static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:129
#define DC_PRED8x8
Definition: h264pred.h:68
#define TM_VP8_PRED
"True Motion", used instead of plane
Definition: h264pred.h:59
uint8_t ref_frame
Definition: vp8.h:86
AVFrame * curframe
Definition: vp8.h:131
#define b
Definition: input.c:52
#define TOP_DC_PRED8x8
Definition: h264pred.h:75
av_cold void ff_vp8dsp_init(VP8DSPContext *dsp)
Definition: vp8dsp.c:494
struct VP8Context::@61 segmentation
Base parameters for segmentation, i.e.
#define DIAG_DOWN_LEFT_PRED
Definition: h264pred.h:41
uint8_t mvc[2][19]
Definition: vp8.h:229
void(* vp8_h_loop_filter8uv_inner)(uint8_t *dstU, uint8_t *dstV, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:64
VP56Frame
Definition: vp56data.h:31
VP56mv mv
Definition: vp8.h:92
int8_t base_quant[4]
Definition: vp8.h:158
static const uint8_t vp8_mv_update_prob[2][19]
Definition: vp8data.h:669
void(* pred8x8[4+3+4])(uint8_t *src, ptrdiff_t stride)
Definition: h264pred.h:97
int update_last
update VP56_FRAME_PREVIOUS with the current one
Definition: vp8.h:234
static int update_dimensions(VP8Context *s, int width, int height)
Definition: vp8.c:119
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static av_always_inline int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16], uint8_t probs[16][3][NUM_DCT_TOKENS-1], int i, int zero_nhood, int16_t qmul[2])
Definition: vp8.c:835
AVFrame * prev_frame
Definition: vp8.h:132
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static void parse_segment_info(VP8Context *s)
Definition: vp8.c:165
AVCodec ff_vp8_decoder
Definition: vp8.c:2079
int num_coeff_partitions
All coefficients are contained in separate arith coding contexts.
Definition: vp8.h:248
static const uint8_t vp8_token_default_probs[4][8][3][NUM_DCT_TOKENS-1]
Definition: vp8data.h:293
vp8_mc_func put_pixels_tab[3][3][3]
Definition: vp8.h:253
void(* pred4x4[9+3+3])(uint8_t *src, const uint8_t *topright, ptrdiff_t stride)
Definition: h264pred.h:93
#define AV_COPY64(d, s)
Definition: intreadwrite.h:510
uint8_t intra4x4_pred_mode_mb[16]
Definition: vp8.h:90
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
uint8_t intra4x4_pred_mode_left[4]
Definition: vp8.h:171
#define VERT_VP8_PRED
for VP8, VERT_PRED is the average of
Definition: h264pred.h:60
#define r
Definition: input.c:51
DCTELEM block[6][4][16]
Definition: vp8.h:97
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
Definition: pthread.c:702
static const uint8_t vp8_mbsplit_count[4]
Definition: vp8data.h:85
void ff_h264_pred_init(H264PredContext *h, int codec_id, const int bit_depth, const int chroma_format_idc)
Set the intra prediction function pointers.
Definition: h264pred.c:399
static const int8_t vp8_coeff_band_indexes[8][10]
Definition: vp8data.h:265
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
H264PredContext hpc
Definition: vp8.h:252
static const uint8_t vp8_pred4x4_mode[]
Definition: vp8data.h:33
void(* vp8_idct_dc_add)(uint8_t *dst, DCTELEM block[16], ptrdiff_t stride)
Definition: vp8dsp.h:40
static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
Definition: vp8.c:1310
void(* vp8_luma_dc_wht)(DCTELEM block[4][4][16], DCTELEM dc[16])
Definition: vp8dsp.h:37
uint8_t absolute_vals
Definition: vp8.h:156
uint16_t mb_width
Definition: vp8.h:134
static const uint8_t vp8_dct_cat2_prob[]
Definition: vp8data.h:278
Multithreading support functions.
static const uint8_t vp8_mv_default_prob[2][19]
Definition: vp8data.h:680
static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:1671
uint8_t last
Definition: vp8.h:224
static const int sizes[][2]
Definition: img2dec.c:46
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:63
void(* vp8_h_loop_filter8uv)(uint8_t *dstU, uint8_t *dstV, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:53
uint8_t mode
Definition: vp8.h:85
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:97
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:1835
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2752
VP8 compatible video decoder.
void(* vp8_v_loop_filter8uv)(uint8_t *dstU, uint8_t *dstV, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:51
static const uint8_t vp8_mbfirstidx[4][16]
Definition: vp8data.h:79
AVCodecContext * avctx
Definition: vp8.h:128
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:36
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
VideoDSPContext vdsp
Definition: vp8.h:250
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
VP8Macroblock * macroblocks_base
Definition: vp8.h:232
static av_always_inline void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], AVFrame *ref_frame, int x_off, int y_off, int bx_off, int by_off, int block_w, int block_h, int width, int height, VP56mv *mv)
Definition: vp8.c:1278
static const uint8_t vp8_pred4x4_prob_inter[9]
Definition: vp8data.h:122
static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr)
Definition: vp8.c:1781
static av_always_inline void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c, VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9])
Definition: vp8.c:846
static int decode_block_coeffs_internal(VP56RangeCoder *r, DCTELEM block[16], uint8_t probs[16][3][NUM_DCT_TOKENS-1], int i, uint8_t *token_prob, int16_t qmul[2])
Definition: vp8.c:767
uint8_t keyframe
Definition: vp8.h:139
static av_always_inline void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst, AVFrame *ref, const VP56mv *mv, int x_off, int y_off, int block_w, int block_h, int width, int height, int linesize, vp8_mc_func mc_func[3][3])
luma MC function
Definition: vp8.c:1184
int off
Definition: dsputil_bfin.c:28
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:128
int16_t luma_qmul[2]
Definition: vp8.h:180
static const uint8_t vp8_pred16x16_prob_inter[4]
Definition: vp8data.h:97
int maps_are_invalid
Definition: vp8.h:264
Definition: hls.c:58
struct VP8Context::@65 prob[2]
These are all of the updatable probabilities for binary decisions.
#define HOR_UP_PRED
Definition: h264pred.h:46
useful rectangle filling function
#define REBASE(pic)
Definition: vp8.c:2050
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:102
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
Motion vector coding, 17.1.
Definition: vp8.c:456
static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
Definition: vp8.c:59
DCTELEM block_dc[16]
Definition: vp8.h:98
static DCTELEM block[64]
Definition: dct-test.c:169
4x4 blocks of 4x4px each
Definition: vp8.h:71
uint8_t deblock_filter
Definition: vp8.h:140
static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
Definition: vp56.h:300
int width
picture width / height.
Definition: avcodec.h:1508
uint8_t mbskip
Definition: vp8.h:222
int8_t ref[4]
filter strength adjustment for macroblocks that reference: [0] - intra / VP56_FRAME_CURRENT [1] - VP5...
Definition: vp8.h:206
static void free_buffers(VP8Context *s)
Definition: vp8.c:38
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)
Definition: vp8.c:1667
static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, AVFrame *curframe, AVFrame *prev_frame)
Definition: vp8.c:1605
struct VP8Context::@63 qmat[4]
Macroblocks can have one of 4 different quants in a frame when segmentation is enabled.
static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
Definition: vp56.h:288
void(* vp8_mc_func)(uint8_t *dst, ptrdiff_t dstStride, uint8_t *src, ptrdiff_t srcStride, int h, int x, int y)
Definition: vp8dsp.h:32
int16_t luma_dc_qmul[2]
luma dc-only block quant
Definition: vp8.h:181
int16_t chroma_qmul[2]
Definition: vp8.h:182
static const uint8_t vp8_pred4x4_prob_intra[10][10][9]
Definition: vp8data.h:127
#define AV_RL32
Definition: intreadwrite.h:146
uint8_t(* top_border)[16+8+8]
Definition: vp8.h:209
uint8_t * edge_emu_buffer
Definition: vp8.h:121
#define vp56_rac_get_prob
Definition: vp56.h:218
static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
Definition: vp8.c:1495
#define VERT_PRED
Prediction types.
Definition: h264pred.h:38
#define LEFT_DC_PRED8x8
Definition: h264pred.h:74
#define DIAG_DOWN_RIGHT_PRED
Definition: h264pred.h:42
uint8_t segment
Definition: vp8.h:89
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2733
AVFrame frames[5]
Definition: vp8.h:254
static const int8_t mv[256][2]
Definition: 4xm.c:73
static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
Definition: vp56.h:235
void(* vp8_v_loop_filter8uv_inner)(uint8_t *dstU, uint8_t *dstV, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:61
NULL
Definition: eval.c:52
void(* vp8_h_loop_filter_simple)(uint8_t *dst, ptrdiff_t stride, int flim)
Definition: vp8dsp.h:69
static int width
Definition: utils.c:156
static av_always_inline void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
Apply motion vectors to prediction buffer, chapter 18.
Definition: vp8.c:1332
uint8_t simple
Definition: vp8.h:163
external API header
static const uint8_t vp8_pred8x8c_prob_intra[3]
Definition: vp8data.h:119
void ff_thread_await_progress(AVFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread.c:684
uint8_t level
Definition: vp8.h:164
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
static void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:153
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
#define HOR_PRED
Definition: h264pred.h:39
int mb_layout
This describes the macroblock memory layout.
Definition: vp8.h:271
uint8_t left_nnz[9]
For coeff decode, we need to know whether the above block had non-zero coefficients.
Definition: vp8.h:113
#define HOR_DOWN_PRED
Definition: h264pred.h:44
static const uint8_t vp8_mbsplit_prob[3]
Definition: vp8data.h:86
VP56RangeCoder c
header context, includes mb modes and motion vectors
Definition: vp8.h:212
void(* pred16x16[4+3+2])(uint8_t *src, ptrdiff_t stride)
Definition: h264pred.h:98
VP56RangeCoder coeff_partition[8]
Definition: vp8.h:249
static const int8_t vp8_pred16x16_tree_inter[4][2]
Definition: vp8data.h:49
static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
Definition: vp8.c:210
int coded_height
Definition: avcodec.h:1515
#define INT_BIT
Definition: internal.h:57
VP8FilterStrength * filter_strength
Definition: vp8.h:122
void av_log_missing_feature(void *avc, const char *feature, int want_sample)
Log a generic warning message about a missing feature.
Definition: utils.c:2007
static av_always_inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
Definition: vp8.c:447
static av_always_inline int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
Definition: vp8.c:949
int num_maps_to_be_freed
Definition: vp8.h:263
#define MV_EDGE_CHECK(n)
static const int8_t vp8_pred4x4_tree[9][2]
Definition: vp8data.h:99
uint8_t enabled
whether each mb can have a different strength based on mode/ref
Definition: vp8.h:155
static av_always_inline void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb)
Definition: vp8.c:1412
void(* vp8_luma_dc_wht_dc)(DCTELEM block[4][4][16], DCTELEM dc[16])
Definition: vp8dsp.h:38
int8_t * ref_index[2]
motion reference frame index the order in which these are stored can depend on the codec...
Definition: avcodec.h:1195
static const uint8_t subpel_idx[3][8]
Definition: vp8.c:1160
void(* vp8_idct_dc_add4y)(uint8_t *dst, DCTELEM block[4][16], ptrdiff_t stride)
Definition: vp8dsp.h:41
int uvlinesize
Definition: vp8.h:137
static void update_refs(VP8Context *s)
Definition: vp8.c:298
static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
Definition: vp56.h:368
static const uint8_t vp8_coeff_band[16]
Definition: vp8data.h:258
VP56mv mv_max
Definition: vp8.h:144
uint8_t * segmentation_maps[5]
A list of segmentation_map buffers that are to be free()'ed in the next decoding iteration.
Definition: vp8.h:262
static const uint16_t vp8_ac_qlookup[VP8_MAX_QUANT+1]
Definition: vp8data.h:657
static av_cold int vp8_decode_init(AVCodecContext *avctx)
Definition: vp8.c:2020
static const uint8_t vp8_pred16x16_prob_intra[4]
Definition: vp8data.h:96
short DCTELEM
Definition: dsputil.h:39
static const int8_t vp8_segmentid_tree[][2]
Definition: vp8data.h:251
static av_always_inline void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb, int mb_x, int keyframe, int layout)
Definition: vp8.c:669
#define DC_127_PRED8x8
Definition: h264pred.h:85
void(* vp8_h_loop_filter16y_inner)(uint8_t *dst, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:59
void(* vp8_idct_add)(uint8_t *dst, DCTELEM block[16], ptrdiff_t stride)
Definition: vp8dsp.h:39
void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: vp56rac.c:40
Definition: vp56.h:39
#define AV_RL24
Definition: intreadwrite.h:78
int update_altref
Definition: vp8.h:236
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int8_t mode[VP8_MVMODE_SPLIT+1]
filter strength adjustment for the following macroblock modes: [0-3] - i16x16 (always zero) [4] - i4x...
Definition: vp8.h:197
2 8x16 blocks (horizontal)
Definition: vp8.h:69
static av_always_inline void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple)
Definition: vp8.c:907
Definition: vf_drawbox.c:36
#define AV_ZERO128(d)
Definition: intreadwrite.h:542
uint8_t pred8x8c[3]
Definition: vp8.h:227
int height
Definition: gxfenc.c:72
discard all non reference
Definition: avcodec.h:533
#define VERT_RIGHT_PRED
Definition: h264pred.h:43
uint8_t partitioning
Definition: vp8.h:87
#define AV_ZERO64(d)
Definition: intreadwrite.h:538
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
void(* vp8_v_loop_filter_simple)(uint8_t *dst, ptrdiff_t stride, int flim)
Definition: vp8dsp.h:68
int16_t x
Definition: vp56.h:40
common internal api header.
#define AV_COPY128(d, s)
Definition: intreadwrite.h:514
int wait_mb_pos
Definition: vp8.h:120
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1772
uint8_t chroma_pred_mode
Definition: vp8.h:88
static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
Definition: vp8.c:2041
static av_always_inline int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
Definition: vp8.c:995
#define DC_129_PRED8x8
Definition: h264pred.h:86
enum AVDiscard skip_loop_filter
Definition: avcodec.h:2893
static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
Definition: vp56.h:272
int invisible
Definition: vp8.h:233
static const SiprModeParam modes[MODE_COUNT]
Definition: sipr.c:69
static av_always_inline int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
Definition: vp8.c:979
#define DC_127_PRED
Definition: h264pred.h:65
int ref_count[3]
Definition: vp8.h:147
void * priv_data
Definition: avcodec.h:1382
AVFrame * framep[4]
Definition: vp8.h:129
void(* vp8_h_loop_filter16y)(uint8_t *dst, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:49
#define MODE_I4x4
Definition: vp8.h:59
#define XCHG(a, b, xchg)
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:2793
#define update_pos(td, mb_y, mb_x)
Definition: vp8.c:1668
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1390
static av_always_inline void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb, int mb_x, int mb_y)
Definition: vp8.c:1039
#define HOR_VP8_PRED
unaveraged version of HOR_PRED, see
Definition: h264pred.h:63
VP8DSPContext vp8dsp
Definition: vp8.h:251
int thread_nr
Definition: vp8.h:114
#define AV_ZERO32(d)
Definition: intreadwrite.h:534
void(* vp8_idct_dc_add4uv)(uint8_t *dst, DCTELEM block[4][16], ptrdiff_t stride)
Definition: vp8dsp.h:43
static av_always_inline int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb, int layout)
Split motion vector prediction, 16.4.
Definition: vp8.c:499
uint64_t layout
static av_always_inline int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
Definition: vp8.c:959
AVDiscard
Definition: avcodec.h:528
static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
Definition: vp56.h:322
void(* vp8_v_loop_filter16y_inner)(uint8_t *dst, ptrdiff_t stride, int flim_E, int flim_I, int hev_thresh)
Definition: vp8dsp.h:57
static av_always_inline void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment, uint8_t *ref, int layout)
Definition: vp8.c:703
static const uint8_t vp8_token_update_probs[4][8][3][NUM_DCT_TOKENS-1]
Definition: vp8data.h:465
int8_t filter_level[4]
base loop filter level
Definition: vp8.h:159
static const int vp8_mode_contexts[6][4]
Definition: vp8data.h:57
#define DC_PRED
Definition: h264pred.h:40
static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vp8.c:1862
static const uint8_t vp8_dct_cat1_prob[]
Definition: vp8data.h:277
void ff_thread_report_progress(AVFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread.c:666
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread.c:921
uint8_t intra
Definition: vp8.h:223
static av_always_inline const uint8_t * get_submv_prob(uint32_t left, uint32_t top)
Definition: vp8.c:485
uint8_t non_zero_count_cache[6][4]
This is the index plus one of the last non-zero coeff for each of the blocks in the current macrobloc...
Definition: vp8.h:106
uint8_t skip
Definition: vp8.h:82
This structure stores compressed data.
Definition: avcodec.h:898
uint8_t profile
Definition: vp8.h:142
const uint8_t *const ff_vp8_dct_cat_prob[]
Definition: vp8data.h:285
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
VP8ThreadData * thread_data
Definition: vp8.h:127
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread.c:979
#define DC_129_PRED
Definition: h264pred.h:66
Predicted.
Definition: avutil.h:246
int thread_mb_pos
Definition: vp8.h:119
2x2 blocks of 8x8px each
Definition: vp8.h:70
static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
Definition: vp8.c:1565
struct VP8Context::@64 lf_delta
static av_cold int vp8_decode_free(AVCodecContext *avctx)
Definition: vp8.c:2034
#define AV_WN64(p, v)
Definition: intreadwrite.h:342
uint8_t filter_level
Definition: vp8.h:76
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)