2 * Copyright (C) 2010 David Conrad
3 * Copyright (C) 2010 Ronald S. Bultje
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * VP8 compatible video decoder
27 #include "libavutil/common.h"
32 // TODO: Maybe add dequant
33 static void vp8_luma_dc_wht_c(int16_t block[4][4][16], int16_t dc[16])
35 int i, t0, t1, t2, t3;
37 for (i = 0; i < 4; i++) {
38 t0 = dc[0 * 4 + i] + dc[3 * 4 + i];
39 t1 = dc[1 * 4 + i] + dc[2 * 4 + i];
40 t2 = dc[1 * 4 + i] - dc[2 * 4 + i];
41 t3 = dc[0 * 4 + i] - dc[3 * 4 + i];
43 dc[0 * 4 + i] = t0 + t1;
44 dc[1 * 4 + i] = t3 + t2;
45 dc[2 * 4 + i] = t0 - t1;
46 dc[3 * 4 + i] = t3 - t2;
49 for (i = 0; i < 4; i++) {
50 t0 = dc[i * 4 + 0] + dc[i * 4 + 3] + 3; // rounding
51 t1 = dc[i * 4 + 1] + dc[i * 4 + 2];
52 t2 = dc[i * 4 + 1] - dc[i * 4 + 2];
53 t3 = dc[i * 4 + 0] - dc[i * 4 + 3] + 3; // rounding
59 block[i][0][0] = (t0 + t1) >> 3;
60 block[i][1][0] = (t3 + t2) >> 3;
61 block[i][2][0] = (t0 - t1) >> 3;
62 block[i][3][0] = (t3 - t2) >> 3;
66 static void vp8_luma_dc_wht_dc_c(int16_t block[4][4][16], int16_t dc[16])
68 int i, val = (dc[0] + 3) >> 3;
71 for (i = 0; i < 4; i++) {
79 #define MUL_20091(a) ((((a) * 20091) >> 16) + (a))
80 #define MUL_35468(a) (((a) * 35468) >> 16)
82 static void vp8_idct_add_c(uint8_t *dst, int16_t block[16], ptrdiff_t stride)
84 int i, t0, t1, t2, t3;
87 for (i = 0; i < 4; i++) {
88 t0 = block[0 * 4 + i] + block[2 * 4 + i];
89 t1 = block[0 * 4 + i] - block[2 * 4 + i];
90 t2 = MUL_35468(block[1 * 4 + i]) - MUL_20091(block[3 * 4 + i]);
91 t3 = MUL_20091(block[1 * 4 + i]) + MUL_35468(block[3 * 4 + i]);
97 tmp[i * 4 + 0] = t0 + t3;
98 tmp[i * 4 + 1] = t1 + t2;
99 tmp[i * 4 + 2] = t1 - t2;
100 tmp[i * 4 + 3] = t0 - t3;
103 for (i = 0; i < 4; i++) {
104 t0 = tmp[0 * 4 + i] + tmp[2 * 4 + i];
105 t1 = tmp[0 * 4 + i] - tmp[2 * 4 + i];
106 t2 = MUL_35468(tmp[1 * 4 + i]) - MUL_20091(tmp[3 * 4 + i]);
107 t3 = MUL_20091(tmp[1 * 4 + i]) + MUL_35468(tmp[3 * 4 + i]);
109 dst[0] = av_clip_uint8(dst[0] + ((t0 + t3 + 4) >> 3));
110 dst[1] = av_clip_uint8(dst[1] + ((t1 + t2 + 4) >> 3));
111 dst[2] = av_clip_uint8(dst[2] + ((t1 - t2 + 4) >> 3));
112 dst[3] = av_clip_uint8(dst[3] + ((t0 - t3 + 4) >> 3));
117 static void vp8_idct_dc_add_c(uint8_t *dst, int16_t block[16], ptrdiff_t stride)
119 int i, dc = (block[0] + 4) >> 3;
122 for (i = 0; i < 4; i++) {
123 dst[0] = av_clip_uint8(dst[0] + dc);
124 dst[1] = av_clip_uint8(dst[1] + dc);
125 dst[2] = av_clip_uint8(dst[2] + dc);
126 dst[3] = av_clip_uint8(dst[3] + dc);
131 static void vp8_idct_dc_add4uv_c(uint8_t *dst, int16_t block[4][16],
134 vp8_idct_dc_add_c(dst + stride * 0 + 0, block[0], stride);
135 vp8_idct_dc_add_c(dst + stride * 0 + 4, block[1], stride);
136 vp8_idct_dc_add_c(dst + stride * 4 + 0, block[2], stride);
137 vp8_idct_dc_add_c(dst + stride * 4 + 4, block[3], stride);
140 static void vp8_idct_dc_add4y_c(uint8_t *dst, int16_t block[4][16],
143 vp8_idct_dc_add_c(dst + 0, block[0], stride);
144 vp8_idct_dc_add_c(dst + 4, block[1], stride);
145 vp8_idct_dc_add_c(dst + 8, block[2], stride);
146 vp8_idct_dc_add_c(dst + 12, block[3], stride);
149 // because I like only having two parameters to pass functions...
150 #define LOAD_PIXELS \
151 int av_unused p3 = p[-4 * stride]; \
152 int av_unused p2 = p[-3 * stride]; \
153 int av_unused p1 = p[-2 * stride]; \
154 int av_unused p0 = p[-1 * stride]; \
155 int av_unused q0 = p[ 0 * stride]; \
156 int av_unused q1 = p[ 1 * stride]; \
157 int av_unused q2 = p[ 2 * stride]; \
158 int av_unused q3 = p[ 3 * stride];
160 #define clip_int8(n) (cm[n + 0x80] - 0x80)
162 static av_always_inline void filter_common(uint8_t *p, ptrdiff_t stride,
167 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
172 a += clip_int8(p1 - q1);
176 // We deviate from the spec here with c(a+3) >> 3
177 // since that's what libvpx does.
178 f1 = FFMIN(a + 4, 127) >> 3;
179 f2 = FFMIN(a + 3, 127) >> 3;
181 // Despite what the spec says, we do need to clamp here to
182 // be bitexact with libvpx.
183 p[-1 * stride] = cm[p0 + f2];
184 p[ 0 * stride] = cm[q0 - f1];
186 // only used for _inner on blocks without high edge variance
189 p[-2 * stride] = cm[p1 + a];
190 p[ 1 * stride] = cm[q1 - a];
194 static av_always_inline int simple_limit(uint8_t *p, ptrdiff_t stride, int flim)
197 return 2 * FFABS(p0 - q0) + (FFABS(p1 - q1) >> 1) <= flim;
201 * E - limit at the macroblock edge
202 * I - limit for interior difference
204 static av_always_inline int normal_limit(uint8_t *p, ptrdiff_t stride,
208 return simple_limit(p, stride, E) &&
209 FFABS(p3 - p2) <= I &&
210 FFABS(p2 - p1) <= I &&
211 FFABS(p1 - p0) <= I &&
212 FFABS(q3 - q2) <= I &&
213 FFABS(q2 - q1) <= I &&
217 // high edge variance
218 static av_always_inline int hev(uint8_t *p, ptrdiff_t stride, int thresh)
221 return FFABS(p1 - p0) > thresh || FFABS(q1 - q0) > thresh;
224 static av_always_inline void filter_mbedge(uint8_t *p, ptrdiff_t stride)
227 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
231 w = clip_int8(p1 - q1);
232 w = clip_int8(w + 3 * (q0 - p0));
234 a0 = (27 * w + 63) >> 7;
235 a1 = (18 * w + 63) >> 7;
236 a2 = (9 * w + 63) >> 7;
238 p[-3 * stride] = cm[p2 + a2];
239 p[-2 * stride] = cm[p1 + a1];
240 p[-1 * stride] = cm[p0 + a0];
241 p[ 0 * stride] = cm[q0 - a0];
242 p[ 1 * stride] = cm[q1 - a1];
243 p[ 2 * stride] = cm[q2 - a2];
246 #define LOOP_FILTER(dir, size, stridea, strideb, maybe_inline) \
247 static maybe_inline \
248 void vp8_ ## dir ## _loop_filter ## size ## _c(uint8_t *dst, \
250 int flim_E, int flim_I, \
254 for (i = 0; i < size; i++) \
255 if (normal_limit(dst + i * stridea, strideb, flim_E, flim_I)) { \
256 if (hev(dst + i * stridea, strideb, hev_thresh)) \
257 filter_common(dst + i * stridea, strideb, 1); \
259 filter_mbedge(dst + i * stridea, strideb); \
263 static maybe_inline \
264 void vp8_ ## dir ## _loop_filter ## size ## _inner_c(uint8_t *dst, \
266 int flim_E, int flim_I, \
270 for (i = 0; i < size; i++) \
271 if (normal_limit(dst + i * stridea, strideb, flim_E, flim_I)) { \
272 int hv = hev(dst + i * stridea, strideb, hev_thresh); \
274 filter_common(dst + i * stridea, strideb, 1); \
276 filter_common(dst + i * stridea, strideb, 0); \
280 LOOP_FILTER(v, 16, 1, stride, )
281 LOOP_FILTER(h, 16, stride, 1, )
283 #define UV_LOOP_FILTER(dir, stridea, strideb) \
284 LOOP_FILTER(dir, 8, stridea, strideb, av_always_inline) \
285 static void vp8_ ## dir ## _loop_filter8uv_c(uint8_t *dstU, uint8_t *dstV, \
286 ptrdiff_t stride, int fE, \
287 int fI, int hev_thresh) \
289 vp8_ ## dir ## _loop_filter8_c(dstU, stride, fE, fI, hev_thresh); \
290 vp8_ ## dir ## _loop_filter8_c(dstV, stride, fE, fI, hev_thresh); \
293 static void vp8_ ## dir ## _loop_filter8uv_inner_c(uint8_t *dstU, \
295 ptrdiff_t stride, int fE, \
296 int fI, int hev_thresh) \
298 vp8_ ## dir ## _loop_filter8_inner_c(dstU, stride, fE, fI, hev_thresh); \
299 vp8_ ## dir ## _loop_filter8_inner_c(dstV, stride, fE, fI, hev_thresh); \
302 UV_LOOP_FILTER(v, 1, stride)
303 UV_LOOP_FILTER(h, stride, 1)
305 static void vp8_v_loop_filter_simple_c(uint8_t *dst, ptrdiff_t stride, int flim)
309 for (i = 0; i < 16; i++)
310 if (simple_limit(dst + i, stride, flim))
311 filter_common(dst + i, stride, 1);
314 static void vp8_h_loop_filter_simple_c(uint8_t *dst, ptrdiff_t stride, int flim)
318 for (i = 0; i < 16; i++)
319 if (simple_limit(dst + i * stride, 1, flim))
320 filter_common(dst + i * stride, 1, 1);
323 static const uint8_t subpel_filters[7][6] = {
324 { 0, 6, 123, 12, 1, 0 },
325 { 2, 11, 108, 36, 8, 1 },
326 { 0, 9, 93, 50, 6, 0 },
327 { 3, 16, 77, 77, 16, 3 },
328 { 0, 6, 50, 93, 9, 0 },
329 { 1, 8, 36, 108, 11, 2 },
330 { 0, 1, 12, 123, 6, 0 },
333 #define PUT_PIXELS(WIDTH) \
334 static void put_vp8_pixels ## WIDTH ## _c(uint8_t *dst, ptrdiff_t dststride, \
335 uint8_t *src, ptrdiff_t srcstride, \
336 int h, int x, int y) \
339 for (i = 0; i < h; i++, dst += dststride, src += srcstride) \
340 memcpy(dst, src, WIDTH); \
347 #define FILTER_6TAP(src, F, stride) \
348 cm[(F[2] * src[x + 0 * stride] - F[1] * src[x - 1 * stride] + \
349 F[0] * src[x - 2 * stride] + F[3] * src[x + 1 * stride] - \
350 F[4] * src[x + 2 * stride] + F[5] * src[x + 3 * stride] + 64) >> 7]
352 #define FILTER_4TAP(src, F, stride) \
353 cm[(F[2] * src[x + 0 * stride] - F[1] * src[x - 1 * stride] + \
354 F[3] * src[x + 1 * stride] - F[4] * src[x + 2 * stride] + 64) >> 7]
356 #define VP8_EPEL_H(SIZE, TAPS) \
357 static void put_vp8_epel ## SIZE ## _h ## TAPS ## _c(uint8_t *dst, \
358 ptrdiff_t dststride, \
360 ptrdiff_t srcstride, \
361 int h, int mx, int my) \
363 const uint8_t *filter = subpel_filters[mx - 1]; \
364 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; \
366 for (y = 0; y < h; y++) { \
367 for (x = 0; x < SIZE; x++) \
368 dst[x] = FILTER_ ## TAPS ## TAP(src, filter, 1); \
374 #define VP8_EPEL_V(SIZE, TAPS) \
375 static void put_vp8_epel ## SIZE ## _v ## TAPS ## _c(uint8_t *dst, \
376 ptrdiff_t dststride, \
378 ptrdiff_t srcstride, \
379 int h, int mx, int my) \
381 const uint8_t *filter = subpel_filters[my - 1]; \
382 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; \
384 for (y = 0; y < h; y++) { \
385 for (x = 0; x < SIZE; x++) \
386 dst[x] = FILTER_ ## TAPS ## TAP(src, filter, srcstride); \
392 #define VP8_EPEL_HV(SIZE, HTAPS, VTAPS) \
394 put_vp8_epel ## SIZE ## _h ## HTAPS ## v ## VTAPS ## _c(uint8_t *dst, \
395 ptrdiff_t dststride, \
397 ptrdiff_t srcstride, \
401 const uint8_t *filter = subpel_filters[mx - 1]; \
402 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; \
404 uint8_t tmp_array[(2 * SIZE + VTAPS - 1) * SIZE]; \
405 uint8_t *tmp = tmp_array; \
406 src -= (2 - (VTAPS == 4)) * srcstride; \
408 for (y = 0; y < h + VTAPS - 1; y++) { \
409 for (x = 0; x < SIZE; x++) \
410 tmp[x] = FILTER_ ## HTAPS ## TAP(src, filter, 1); \
414 tmp = tmp_array + (2 - (VTAPS == 4)) * SIZE; \
415 filter = subpel_filters[my - 1]; \
417 for (y = 0; y < h; y++) { \
418 for (x = 0; x < SIZE; x++) \
419 dst[x] = FILTER_ ## VTAPS ## TAP(tmp, filter, SIZE); \
438 VP8_EPEL_HV(16, 4, 4)
441 VP8_EPEL_HV(16, 4, 6)
444 VP8_EPEL_HV(16, 6, 4)
447 VP8_EPEL_HV(16, 6, 6)
451 #define VP8_BILINEAR(SIZE) \
452 static void put_vp8_bilinear ## SIZE ## _h_c(uint8_t *dst, ptrdiff_t dstride, \
453 uint8_t *src, ptrdiff_t sstride, \
454 int h, int mx, int my) \
456 int a = 8 - mx, b = mx; \
458 for (y = 0; y < h; y++) { \
459 for (x = 0; x < SIZE; x++) \
460 dst[x] = (a * src[x] + b * src[x + 1] + 4) >> 3; \
466 static void put_vp8_bilinear ## SIZE ## _v_c(uint8_t *dst, ptrdiff_t dstride, \
467 uint8_t *src, ptrdiff_t sstride, \
468 int h, int mx, int my) \
470 int c = 8 - my, d = my; \
472 for (y = 0; y < h; y++) { \
473 for (x = 0; x < SIZE; x++) \
474 dst[x] = (c * src[x] + d * src[x + sstride] + 4) >> 3; \
480 static void put_vp8_bilinear ## SIZE ## _hv_c(uint8_t *dst, \
484 int h, int mx, int my) \
486 int a = 8 - mx, b = mx; \
487 int c = 8 - my, d = my; \
489 uint8_t tmp_array[(2 * SIZE + 1) * SIZE]; \
490 uint8_t *tmp = tmp_array; \
491 for (y = 0; y < h + 1; y++) { \
492 for (x = 0; x < SIZE; x++) \
493 tmp[x] = (a * src[x] + b * src[x + 1] + 4) >> 3; \
498 for (y = 0; y < h; y++) { \
499 for (x = 0; x < SIZE; x++) \
500 dst[x] = (c * tmp[x] + d * tmp[x + SIZE] + 4) >> 3; \
510 #define VP8_MC_FUNC(IDX, SIZE) \
511 dsp->put_vp8_epel_pixels_tab[IDX][0][0] = put_vp8_pixels ## SIZE ## _c; \
512 dsp->put_vp8_epel_pixels_tab[IDX][0][1] = put_vp8_epel ## SIZE ## _h4_c; \
513 dsp->put_vp8_epel_pixels_tab[IDX][0][2] = put_vp8_epel ## SIZE ## _h6_c; \
514 dsp->put_vp8_epel_pixels_tab[IDX][1][0] = put_vp8_epel ## SIZE ## _v4_c; \
515 dsp->put_vp8_epel_pixels_tab[IDX][1][1] = put_vp8_epel ## SIZE ## _h4v4_c; \
516 dsp->put_vp8_epel_pixels_tab[IDX][1][2] = put_vp8_epel ## SIZE ## _h6v4_c; \
517 dsp->put_vp8_epel_pixels_tab[IDX][2][0] = put_vp8_epel ## SIZE ## _v6_c; \
518 dsp->put_vp8_epel_pixels_tab[IDX][2][1] = put_vp8_epel ## SIZE ## _h4v6_c; \
519 dsp->put_vp8_epel_pixels_tab[IDX][2][2] = put_vp8_epel ## SIZE ## _h6v6_c
521 #define VP8_BILINEAR_MC_FUNC(IDX, SIZE) \
522 dsp->put_vp8_bilinear_pixels_tab[IDX][0][0] = put_vp8_pixels ## SIZE ## _c; \
523 dsp->put_vp8_bilinear_pixels_tab[IDX][0][1] = put_vp8_bilinear ## SIZE ## _h_c; \
524 dsp->put_vp8_bilinear_pixels_tab[IDX][0][2] = put_vp8_bilinear ## SIZE ## _h_c; \
525 dsp->put_vp8_bilinear_pixels_tab[IDX][1][0] = put_vp8_bilinear ## SIZE ## _v_c; \
526 dsp->put_vp8_bilinear_pixels_tab[IDX][1][1] = put_vp8_bilinear ## SIZE ## _hv_c; \
527 dsp->put_vp8_bilinear_pixels_tab[IDX][1][2] = put_vp8_bilinear ## SIZE ## _hv_c; \
528 dsp->put_vp8_bilinear_pixels_tab[IDX][2][0] = put_vp8_bilinear ## SIZE ## _v_c; \
529 dsp->put_vp8_bilinear_pixels_tab[IDX][2][1] = put_vp8_bilinear ## SIZE ## _hv_c; \
530 dsp->put_vp8_bilinear_pixels_tab[IDX][2][2] = put_vp8_bilinear ## SIZE ## _hv_c
532 av_cold void ff_vp8dsp_init(VP8DSPContext *dsp)
534 dsp->vp8_luma_dc_wht = vp8_luma_dc_wht_c;
535 dsp->vp8_luma_dc_wht_dc = vp8_luma_dc_wht_dc_c;
536 dsp->vp8_idct_add = vp8_idct_add_c;
537 dsp->vp8_idct_dc_add = vp8_idct_dc_add_c;
538 dsp->vp8_idct_dc_add4y = vp8_idct_dc_add4y_c;
539 dsp->vp8_idct_dc_add4uv = vp8_idct_dc_add4uv_c;
541 dsp->vp8_v_loop_filter16y = vp8_v_loop_filter16_c;
542 dsp->vp8_h_loop_filter16y = vp8_h_loop_filter16_c;
543 dsp->vp8_v_loop_filter8uv = vp8_v_loop_filter8uv_c;
544 dsp->vp8_h_loop_filter8uv = vp8_h_loop_filter8uv_c;
546 dsp->vp8_v_loop_filter16y_inner = vp8_v_loop_filter16_inner_c;
547 dsp->vp8_h_loop_filter16y_inner = vp8_h_loop_filter16_inner_c;
548 dsp->vp8_v_loop_filter8uv_inner = vp8_v_loop_filter8uv_inner_c;
549 dsp->vp8_h_loop_filter8uv_inner = vp8_h_loop_filter8uv_inner_c;
551 dsp->vp8_v_loop_filter_simple = vp8_v_loop_filter_simple_c;
552 dsp->vp8_h_loop_filter_simple = vp8_h_loop_filter_simple_c;
558 VP8_BILINEAR_MC_FUNC(0, 16);
559 VP8_BILINEAR_MC_FUNC(1, 8);
560 VP8_BILINEAR_MC_FUNC(2, 4);
563 ff_vp8dsp_init_arm(dsp);
565 ff_vp8dsp_init_ppc(dsp);
567 ff_vp8dsp_init_x86(dsp);