2 * Copyright (C) 2012 Ronald S. Bultje
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/attributes.h"
23 #include "libavutil/common.h"
24 #include "libavutil/cpu.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/videodsp.h"
30 typedef void emu_edge_core_func(uint8_t *buf, const uint8_t *src,
31 x86_reg linesize, x86_reg start_y,
32 x86_reg end_y, x86_reg block_h,
33 x86_reg start_x, x86_reg end_x,
35 extern emu_edge_core_func ff_emu_edge_core_mmx;
36 extern emu_edge_core_func ff_emu_edge_core_sse;
38 static av_always_inline void emulated_edge_mc(uint8_t *buf, const uint8_t *src,
40 int block_w, int block_h,
43 emu_edge_core_func *core_fn)
45 int start_y, start_x, end_y, end_x, src_y_add = 0;
48 src_y_add = h - 1 - src_y;
50 } else if (src_y <= -block_h) {
51 src_y_add = 1 - block_h - src_y;
57 } else if (src_x <= -block_w) {
58 src += 1 - block_w - src_x;
62 start_y = FFMAX(0, -src_y);
63 start_x = FFMAX(0, -src_x);
64 end_y = FFMIN(block_h, h-src_y);
65 end_x = FFMIN(block_w, w-src_x);
66 assert(start_x < end_x && block_w > 0);
67 assert(start_y < end_y && block_h > 0);
69 // fill in the to-be-copied part plus all above/below
70 src += (src_y_add + start_y) * linesize + start_x;
72 core_fn(buf, src, linesize, start_y, end_y,
73 block_h, start_x, end_x, block_w);
77 static av_noinline void emulated_edge_mc_mmx(uint8_t *buf, const uint8_t *src,
79 int block_w, int block_h,
80 int src_x, int src_y, int w, int h)
82 emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
83 w, h, &ff_emu_edge_core_mmx);
87 static av_noinline void emulated_edge_mc_sse(uint8_t *buf, const uint8_t *src,
89 int block_w, int block_h,
90 int src_x, int src_y, int w, int h)
92 emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
93 w, h, &ff_emu_edge_core_sse);
95 #endif /* HAVE_YASM */
97 void ff_prefetch_mmxext(uint8_t *buf, ptrdiff_t stride, int h);
98 void ff_prefetch_3dnow(uint8_t *buf, ptrdiff_t stride, int h);
100 av_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)
103 int mm_flags = av_get_cpu_flags();
106 if (bpc <= 8 && mm_flags & AV_CPU_FLAG_MMX) {
107 ctx->emulated_edge_mc = emulated_edge_mc_mmx;
109 if (mm_flags & AV_CPU_FLAG_3DNOW) {
110 ctx->prefetch = ff_prefetch_3dnow;
112 #endif /* ARCH_X86_32 */
113 if (mm_flags & AV_CPU_FLAG_MMXEXT) {
114 ctx->prefetch = ff_prefetch_mmxext;
116 if (bpc <= 8 && mm_flags & AV_CPU_FLAG_SSE) {
117 ctx->emulated_edge_mc = emulated_edge_mc_sse;
119 #endif /* HAVE_YASM */