]> git.sesse.net Git - ffmpeg/blob - libavcodec/x86/videodsp_init.c
x86: dsputil: cosmetics: Remove two pointless variable indirections
[ffmpeg] / libavcodec / x86 / videodsp_init.c
1 /*
2  * Copyright (C) 2012 Ronald S. Bultje
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
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"
28
29 #if HAVE_YASM
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,
34                                 x86_reg block_w);
35 extern emu_edge_core_func ff_emu_edge_core_mmx;
36 extern emu_edge_core_func ff_emu_edge_core_sse;
37
38 static av_always_inline void emulated_edge_mc(uint8_t *buf, const uint8_t *src,
39                                               ptrdiff_t linesize,
40                                               int block_w, int block_h,
41                                               int src_x, int src_y,
42                                               int w, int h,
43                                               emu_edge_core_func *core_fn)
44 {
45     int start_y, start_x, end_y, end_x, src_y_add = 0;
46
47     if (src_y >= h) {
48         src_y_add = h - 1 - src_y;
49         src_y     = h - 1;
50     } else if (src_y <= -block_h) {
51         src_y_add = 1 - block_h - src_y;
52         src_y     = 1 - block_h;
53     }
54     if (src_x >= w) {
55         src   += w - 1 - src_x;
56         src_x  = w - 1;
57     } else if (src_x <= -block_w) {
58         src   += 1 - block_w - src_x;
59         src_x  = 1 - block_w;
60     }
61
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);
68
69     // fill in the to-be-copied part plus all above/below
70     src += (src_y_add + start_y) * linesize + start_x;
71     buf += start_x;
72     core_fn(buf, src, linesize, start_y, end_y,
73             block_h, start_x, end_x, block_w);
74 }
75
76 #if ARCH_X86_32
77 static av_noinline void emulated_edge_mc_mmx(uint8_t *buf, const uint8_t *src,
78                                              ptrdiff_t linesize,
79                                              int block_w, int block_h,
80                                              int src_x, int src_y, int w, int h)
81 {
82     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
83                      w, h, &ff_emu_edge_core_mmx);
84 }
85 #endif
86
87 static av_noinline void emulated_edge_mc_sse(uint8_t *buf, const uint8_t *src,
88                                              ptrdiff_t linesize,
89                                              int block_w, int block_h,
90                                              int src_x, int src_y, int w, int h)
91 {
92     emulated_edge_mc(buf, src, linesize, block_w, block_h, src_x, src_y,
93                      w, h, &ff_emu_edge_core_sse);
94 }
95 #endif /* HAVE_YASM */
96
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);
99
100 av_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)
101 {
102 #if HAVE_YASM
103     int mm_flags = av_get_cpu_flags();
104
105 #if ARCH_X86_32
106     if (bpc <= 8 && mm_flags & AV_CPU_FLAG_MMX) {
107         ctx->emulated_edge_mc = emulated_edge_mc_mmx;
108     }
109     if (mm_flags & AV_CPU_FLAG_3DNOW) {
110         ctx->prefetch = ff_prefetch_3dnow;
111     }
112 #endif /* ARCH_X86_32 */
113     if (mm_flags & AV_CPU_FLAG_MMXEXT) {
114         ctx->prefetch = ff_prefetch_mmxext;
115     }
116     if (bpc <= 8 && mm_flags & AV_CPU_FLAG_SSE) {
117         ctx->emulated_edge_mc = emulated_edge_mc_sse;
118     }
119 #endif /* HAVE_YASM */
120 }