1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (C) 2002-2012 Michael Niedermayer
3cabdff1aSopenharmony_ci * Copyright (C) 2012 Ronald S. Bultje
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * This file is part of FFmpeg.
6cabdff1aSopenharmony_ci *
7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
11cabdff1aSopenharmony_ci *
12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15cabdff1aSopenharmony_ci * Lesser General Public License for more details.
16cabdff1aSopenharmony_ci *
17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20cabdff1aSopenharmony_ci */
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include "config.h"
23cabdff1aSopenharmony_ci#include "libavutil/attributes.h"
24cabdff1aSopenharmony_ci#include "libavutil/avassert.h"
25cabdff1aSopenharmony_ci#include "libavutil/common.h"
26cabdff1aSopenharmony_ci#include "libavutil/cpu.h"
27cabdff1aSopenharmony_ci#include "libavutil/x86/asm.h"
28cabdff1aSopenharmony_ci#include "libavutil/x86/cpu.h"
29cabdff1aSopenharmony_ci#include "libavcodec/videodsp.h"
30cabdff1aSopenharmony_ci
31cabdff1aSopenharmony_ci#if HAVE_X86ASM
32cabdff1aSopenharmony_citypedef void emu_edge_vfix_func(uint8_t *dst, x86_reg dst_stride,
33cabdff1aSopenharmony_ci                                const uint8_t *src, x86_reg src_stride,
34cabdff1aSopenharmony_ci                                x86_reg start_y, x86_reg end_y, x86_reg bh);
35cabdff1aSopenharmony_citypedef void emu_edge_vvar_func(uint8_t *dst, x86_reg dst_stride,
36cabdff1aSopenharmony_ci                                const uint8_t *src, x86_reg src_stride,
37cabdff1aSopenharmony_ci                                x86_reg start_y, x86_reg end_y, x86_reg bh,
38cabdff1aSopenharmony_ci                                x86_reg w);
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix1_mmx;
41cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix2_mmx;
42cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix3_mmx;
43cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix4_mmx;
44cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix5_mmx;
45cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix6_mmx;
46cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix7_mmx;
47cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix8_mmx;
48cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix9_mmx;
49cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix10_mmx;
50cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix11_mmx;
51cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix12_mmx;
52cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix13_mmx;
53cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix14_mmx;
54cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix15_mmx;
55cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix16_sse;
56cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix17_sse;
57cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix18_sse;
58cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix19_sse;
59cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix20_sse;
60cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix21_sse;
61cabdff1aSopenharmony_ciextern emu_edge_vfix_func ff_emu_edge_vfix22_sse;
62cabdff1aSopenharmony_cistatic emu_edge_vfix_func * const vfixtbl_sse[22] = {
63cabdff1aSopenharmony_ci    ff_emu_edge_vfix1_mmx,  ff_emu_edge_vfix2_mmx,  ff_emu_edge_vfix3_mmx,
64cabdff1aSopenharmony_ci    ff_emu_edge_vfix4_mmx,  ff_emu_edge_vfix5_mmx,  ff_emu_edge_vfix6_mmx,
65cabdff1aSopenharmony_ci    ff_emu_edge_vfix7_mmx,  ff_emu_edge_vfix8_mmx,  ff_emu_edge_vfix9_mmx,
66cabdff1aSopenharmony_ci    ff_emu_edge_vfix10_mmx, ff_emu_edge_vfix11_mmx, ff_emu_edge_vfix12_mmx,
67cabdff1aSopenharmony_ci    ff_emu_edge_vfix13_mmx, ff_emu_edge_vfix14_mmx, ff_emu_edge_vfix15_mmx,
68cabdff1aSopenharmony_ci    ff_emu_edge_vfix16_sse, ff_emu_edge_vfix17_sse, ff_emu_edge_vfix18_sse,
69cabdff1aSopenharmony_ci    ff_emu_edge_vfix19_sse, ff_emu_edge_vfix20_sse, ff_emu_edge_vfix21_sse,
70cabdff1aSopenharmony_ci    ff_emu_edge_vfix22_sse
71cabdff1aSopenharmony_ci};
72cabdff1aSopenharmony_ciextern emu_edge_vvar_func ff_emu_edge_vvar_sse;
73cabdff1aSopenharmony_ci
74cabdff1aSopenharmony_citypedef void emu_edge_hfix_func(uint8_t *dst, x86_reg dst_stride,
75cabdff1aSopenharmony_ci                                x86_reg start_x, x86_reg bh);
76cabdff1aSopenharmony_citypedef void emu_edge_hvar_func(uint8_t *dst, x86_reg dst_stride,
77cabdff1aSopenharmony_ci                                x86_reg start_x, x86_reg n_words, x86_reg bh);
78cabdff1aSopenharmony_ci
79cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix2_mmx;
80cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix4_mmx;
81cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix6_mmx;
82cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix8_mmx;
83cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix10_mmx;
84cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix12_mmx;
85cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix14_mmx;
86cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix16_sse2;
87cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix18_sse2;
88cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix20_sse2;
89cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix22_sse2;
90cabdff1aSopenharmony_cistatic emu_edge_hfix_func * const hfixtbl_sse2[11] = {
91cabdff1aSopenharmony_ci    ff_emu_edge_hfix2_mmx,  ff_emu_edge_hfix4_mmx,  ff_emu_edge_hfix6_mmx,
92cabdff1aSopenharmony_ci    ff_emu_edge_hfix8_mmx,  ff_emu_edge_hfix10_mmx, ff_emu_edge_hfix12_mmx,
93cabdff1aSopenharmony_ci    ff_emu_edge_hfix14_mmx, ff_emu_edge_hfix16_sse2, ff_emu_edge_hfix18_sse2,
94cabdff1aSopenharmony_ci    ff_emu_edge_hfix20_sse2, ff_emu_edge_hfix22_sse2
95cabdff1aSopenharmony_ci};
96cabdff1aSopenharmony_ciextern emu_edge_hvar_func ff_emu_edge_hvar_sse2;
97cabdff1aSopenharmony_ci#if HAVE_AVX2_EXTERNAL
98cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix8_avx2;
99cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix10_avx2;
100cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix12_avx2;
101cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix14_avx2;
102cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix16_avx2;
103cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix18_avx2;
104cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix20_avx2;
105cabdff1aSopenharmony_ciextern emu_edge_hfix_func ff_emu_edge_hfix22_avx2;
106cabdff1aSopenharmony_cistatic emu_edge_hfix_func * const hfixtbl_avx2[11] = {
107cabdff1aSopenharmony_ci    ff_emu_edge_hfix2_mmx,  ff_emu_edge_hfix4_mmx,  ff_emu_edge_hfix6_mmx,
108cabdff1aSopenharmony_ci    ff_emu_edge_hfix8_avx2,  ff_emu_edge_hfix10_avx2, ff_emu_edge_hfix12_avx2,
109cabdff1aSopenharmony_ci    ff_emu_edge_hfix14_avx2, ff_emu_edge_hfix16_avx2, ff_emu_edge_hfix18_avx2,
110cabdff1aSopenharmony_ci    ff_emu_edge_hfix20_avx2, ff_emu_edge_hfix22_avx2
111cabdff1aSopenharmony_ci};
112cabdff1aSopenharmony_ciextern emu_edge_hvar_func ff_emu_edge_hvar_avx2;
113cabdff1aSopenharmony_ci#endif
114cabdff1aSopenharmony_ci
115cabdff1aSopenharmony_cistatic av_always_inline void emulated_edge_mc(uint8_t *dst, const uint8_t *src,
116cabdff1aSopenharmony_ci                                              ptrdiff_t dst_stride,
117cabdff1aSopenharmony_ci                                              ptrdiff_t src_stride,
118cabdff1aSopenharmony_ci                                              x86_reg block_w, x86_reg block_h,
119cabdff1aSopenharmony_ci                                              x86_reg src_x, x86_reg src_y,
120cabdff1aSopenharmony_ci                                              x86_reg w, x86_reg h,
121cabdff1aSopenharmony_ci                                              emu_edge_vfix_func * const *vfix_tbl,
122cabdff1aSopenharmony_ci                                              emu_edge_vvar_func *v_extend_var,
123cabdff1aSopenharmony_ci                                              emu_edge_hfix_func * const *hfix_tbl,
124cabdff1aSopenharmony_ci                                              emu_edge_hvar_func *h_extend_var)
125cabdff1aSopenharmony_ci{
126cabdff1aSopenharmony_ci    x86_reg start_y, start_x, end_y, end_x, src_y_add = 0, p;
127cabdff1aSopenharmony_ci
128cabdff1aSopenharmony_ci    if (!w || !h)
129cabdff1aSopenharmony_ci        return;
130cabdff1aSopenharmony_ci
131cabdff1aSopenharmony_ci    av_assert2(block_w <= FFABS(dst_stride));
132cabdff1aSopenharmony_ci
133cabdff1aSopenharmony_ci    if (src_y >= h) {
134cabdff1aSopenharmony_ci        src -= src_y*src_stride;
135cabdff1aSopenharmony_ci        src_y_add = h - 1;
136cabdff1aSopenharmony_ci        src_y     = h - 1;
137cabdff1aSopenharmony_ci    } else if (src_y <= -block_h) {
138cabdff1aSopenharmony_ci        src -= src_y*src_stride;
139cabdff1aSopenharmony_ci        src_y_add = 1 - block_h;
140cabdff1aSopenharmony_ci        src_y     = 1 - block_h;
141cabdff1aSopenharmony_ci    }
142cabdff1aSopenharmony_ci    if (src_x >= w) {
143cabdff1aSopenharmony_ci        src   += w - 1 - src_x;
144cabdff1aSopenharmony_ci        src_x  = w - 1;
145cabdff1aSopenharmony_ci    } else if (src_x <= -block_w) {
146cabdff1aSopenharmony_ci        src   += 1 - block_w - src_x;
147cabdff1aSopenharmony_ci        src_x  = 1 - block_w;
148cabdff1aSopenharmony_ci    }
149cabdff1aSopenharmony_ci
150cabdff1aSopenharmony_ci    start_y = FFMAX(0, -src_y);
151cabdff1aSopenharmony_ci    start_x = FFMAX(0, -src_x);
152cabdff1aSopenharmony_ci    end_y   = FFMIN(block_h, h-src_y);
153cabdff1aSopenharmony_ci    end_x   = FFMIN(block_w, w-src_x);
154cabdff1aSopenharmony_ci    av_assert2(start_x < end_x && block_w > 0);
155cabdff1aSopenharmony_ci    av_assert2(start_y < end_y && block_h > 0);
156cabdff1aSopenharmony_ci
157cabdff1aSopenharmony_ci    // fill in the to-be-copied part plus all above/below
158cabdff1aSopenharmony_ci    src += (src_y_add + start_y) * src_stride + start_x;
159cabdff1aSopenharmony_ci    w = end_x - start_x;
160cabdff1aSopenharmony_ci    if (w <= 22) {
161cabdff1aSopenharmony_ci        vfix_tbl[w - 1](dst + start_x, dst_stride, src, src_stride,
162cabdff1aSopenharmony_ci                        start_y, end_y, block_h);
163cabdff1aSopenharmony_ci    } else {
164cabdff1aSopenharmony_ci        v_extend_var(dst + start_x, dst_stride, src, src_stride,
165cabdff1aSopenharmony_ci                     start_y, end_y, block_h, w);
166cabdff1aSopenharmony_ci    }
167cabdff1aSopenharmony_ci
168cabdff1aSopenharmony_ci    // fill left
169cabdff1aSopenharmony_ci    if (start_x) {
170cabdff1aSopenharmony_ci        if (start_x <= 22) {
171cabdff1aSopenharmony_ci            hfix_tbl[(start_x - 1) >> 1](dst, dst_stride, start_x, block_h);
172cabdff1aSopenharmony_ci        } else {
173cabdff1aSopenharmony_ci            h_extend_var(dst, dst_stride,
174cabdff1aSopenharmony_ci                         start_x, (start_x + 1) >> 1, block_h);
175cabdff1aSopenharmony_ci        }
176cabdff1aSopenharmony_ci    }
177cabdff1aSopenharmony_ci
178cabdff1aSopenharmony_ci    // fill right
179cabdff1aSopenharmony_ci    p = block_w - end_x;
180cabdff1aSopenharmony_ci    if (p) {
181cabdff1aSopenharmony_ci        if (p <= 22) {
182cabdff1aSopenharmony_ci            hfix_tbl[(p - 1) >> 1](dst + end_x - (p & 1), dst_stride,
183cabdff1aSopenharmony_ci                                   -!(p & 1), block_h);
184cabdff1aSopenharmony_ci        } else {
185cabdff1aSopenharmony_ci            h_extend_var(dst + end_x - (p & 1), dst_stride,
186cabdff1aSopenharmony_ci                         -!(p & 1), (p + 1) >> 1, block_h);
187cabdff1aSopenharmony_ci        }
188cabdff1aSopenharmony_ci    }
189cabdff1aSopenharmony_ci}
190cabdff1aSopenharmony_ci
191cabdff1aSopenharmony_cistatic av_noinline void emulated_edge_mc_sse2(uint8_t *buf, const uint8_t *src,
192cabdff1aSopenharmony_ci                                              ptrdiff_t buf_stride,
193cabdff1aSopenharmony_ci                                              ptrdiff_t src_stride,
194cabdff1aSopenharmony_ci                                              int block_w, int block_h,
195cabdff1aSopenharmony_ci                                              int src_x, int src_y, int w,
196cabdff1aSopenharmony_ci                                              int h)
197cabdff1aSopenharmony_ci{
198cabdff1aSopenharmony_ci    emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
199cabdff1aSopenharmony_ci                     src_x, src_y, w, h, vfixtbl_sse, &ff_emu_edge_vvar_sse,
200cabdff1aSopenharmony_ci                     hfixtbl_sse2, &ff_emu_edge_hvar_sse2);
201cabdff1aSopenharmony_ci}
202cabdff1aSopenharmony_ci
203cabdff1aSopenharmony_ci#if HAVE_AVX2_EXTERNAL
204cabdff1aSopenharmony_cistatic av_noinline void emulated_edge_mc_avx2(uint8_t *buf, const uint8_t *src,
205cabdff1aSopenharmony_ci                                              ptrdiff_t buf_stride,
206cabdff1aSopenharmony_ci                                              ptrdiff_t src_stride,
207cabdff1aSopenharmony_ci                                              int block_w, int block_h,
208cabdff1aSopenharmony_ci                                              int src_x, int src_y, int w,
209cabdff1aSopenharmony_ci                                              int h)
210cabdff1aSopenharmony_ci{
211cabdff1aSopenharmony_ci    emulated_edge_mc(buf, src, buf_stride, src_stride, block_w, block_h,
212cabdff1aSopenharmony_ci                     src_x, src_y, w, h, vfixtbl_sse, &ff_emu_edge_vvar_sse,
213cabdff1aSopenharmony_ci                     hfixtbl_avx2, &ff_emu_edge_hvar_avx2);
214cabdff1aSopenharmony_ci}
215cabdff1aSopenharmony_ci#endif /* HAVE_AVX2_EXTERNAL */
216cabdff1aSopenharmony_ci#endif /* HAVE_X86ASM */
217cabdff1aSopenharmony_ci
218cabdff1aSopenharmony_civoid ff_prefetch_mmxext(uint8_t *buf, ptrdiff_t stride, int h);
219cabdff1aSopenharmony_ci
220cabdff1aSopenharmony_ciav_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)
221cabdff1aSopenharmony_ci{
222cabdff1aSopenharmony_ci#if HAVE_X86ASM
223cabdff1aSopenharmony_ci    int cpu_flags = av_get_cpu_flags();
224cabdff1aSopenharmony_ci
225cabdff1aSopenharmony_ci    if (EXTERNAL_MMXEXT(cpu_flags)) {
226cabdff1aSopenharmony_ci        ctx->prefetch = ff_prefetch_mmxext;
227cabdff1aSopenharmony_ci    }
228cabdff1aSopenharmony_ci    if (EXTERNAL_SSE2(cpu_flags) && bpc <= 8) {
229cabdff1aSopenharmony_ci        ctx->emulated_edge_mc = emulated_edge_mc_sse2;
230cabdff1aSopenharmony_ci    }
231cabdff1aSopenharmony_ci#if HAVE_AVX2_EXTERNAL
232cabdff1aSopenharmony_ci    if (EXTERNAL_AVX2(cpu_flags) && bpc <= 8) {
233cabdff1aSopenharmony_ci        ctx->emulated_edge_mc = emulated_edge_mc_avx2;
234cabdff1aSopenharmony_ci    }
235cabdff1aSopenharmony_ci#endif
236cabdff1aSopenharmony_ci#endif /* HAVE_X86ASM */
237cabdff1aSopenharmony_ci}
238