]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/videodsp_init.c
17a90115cf9e1b2114f3d9a365248d10c489772f
[ffmpeg] / libavcodec / x86 / videodsp_init.c
1 /*
2  * Copyright (C) 2002-2012 Michael Niedermayer
3  * Copyright (C) 2012 Ronald S. Bultje
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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.
11  *
12  * FFmpeg 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "config.h"
23 #include "libavutil/attributes.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/cpu.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/x86/asm.h"
29 #include "libavutil/x86/cpu.h"
30 #include "libavcodec/videodsp.h"
31
32 #if HAVE_YASM
33 typedef void emu_edge_core_func(uint8_t *buf, const uint8_t *src,
34                                 x86_reg linesize, x86_reg start_y,
35                                 x86_reg end_y, x86_reg block_h,
36                                 x86_reg start_x, x86_reg end_x,
37                                 x86_reg block_w);
38 extern emu_edge_core_func ff_emu_edge_core_mmx;
39 extern emu_edge_core_func ff_emu_edge_core_sse;
40
41 static av_always_inline void emulated_edge_mc(uint8_t *buf, const uint8_t *src,
42                                               ptrdiff_t linesize,
43                                               int block_w, int block_h,
44                                               int src_x, int src_y,
45                                               int w, int h,
46                                               emu_edge_core_func *core_fn)
47 {
48     int start_y, start_x, end_y, end_x, src_y_add = 0;
49
50     if(!w || !h)
51         return;
52
53     if (src_y >= h) {
54         src -= src_y*linesize;
55         src_y_add = h - 1;
56         src_y     = h - 1;
57     } else if (src_y <= -block_h) {
58         src -= src_y*linesize;
59         src_y_add = 1 - block_h;
60         src_y     = 1 - block_h;
61     }
62     if (src_x >= w) {
63         src   += w - 1 - src_x;
64         src_x  = w - 1;
65     } else if (src_x <= -block_w) {
66         src   += 1 - block_w - src_x;
67         src_x  = 1 - block_w;
68     }
69
70     start_y = FFMAX(0, -src_y);
71     start_x = FFMAX(0, -src_x);
72     end_y   = FFMIN(block_h, h-src_y);
73     end_x   = FFMIN(block_w, w-src_x);
74     av_assert2(start_x < end_x && block_w > 0);
75     av_assert2(start_y < end_y && block_h > 0);
76
77     // fill in the to-be-copied part plus all above/below
78     src += (src_y_add + start_y) * linesize + start_x;
79     buf += start_x;
80     core_fn(buf, src, linesize, start_y, end_y,
81             block_h, start_x, end_x, block_w);
82 }
83
84 #if ARCH_X86_32
85 static av_noinline void emulated_edge_mc_mmx(uint8_t *buf, const uint8_t *src,
86                                              ptrdiff_t linesize,
87                                              int block_w, int block_h,
88                                              int src_x, int src_y, int w, int h)
89 {
90     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
91                      w, h, &ff_emu_edge_core_mmx);
92 }
93 #endif
94
95 static av_noinline void emulated_edge_mc_sse(uint8_t *buf, const uint8_t *src,
96                                              ptrdiff_t linesize,
97                                              int block_w, int block_h,
98                                              int src_x, int src_y, int w, int h)
99 {
100     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
101                      w, h, &ff_emu_edge_core_sse);
102 }
103 #endif /* HAVE_YASM */
104
105 void ff_prefetch_mmxext(uint8_t *buf, ptrdiff_t stride, int h);
106 void ff_prefetch_3dnow(uint8_t *buf, ptrdiff_t stride, int h);
107
108 av_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)
109 {
110 #if HAVE_YASM
111     int cpu_flags = av_get_cpu_flags();
112
113 #if ARCH_X86_32
114     if (EXTERNAL_MMX(cpu_flags) && bpc <= 8) {
115         ctx->emulated_edge_mc = emulated_edge_mc_mmx;
116     }
117     if (EXTERNAL_AMD3DNOW(cpu_flags)) {
118         ctx->prefetch = ff_prefetch_3dnow;
119     }
120 #endif /* ARCH_X86_32 */
121     if (EXTERNAL_MMXEXT(cpu_flags)) {
122         ctx->prefetch = ff_prefetch_mmxext;
123     }
124     if (EXTERNAL_SSE(cpu_flags) && bpc <= 8) {
125         ctx->emulated_edge_mc = emulated_edge_mc_sse;
126     }
127 #endif /* HAVE_YASM */
128 }