FFmpeg
mpegvideoencdsp_init.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21 #include "libavutil/cpu.h"
22 #include "libavutil/x86/cpu.h"
23 #include "libavcodec/avcodec.h"
25 
26 int ff_pix_sum16_sse2(const uint8_t *pix, ptrdiff_t line_size);
27 int ff_pix_sum16_xop(const uint8_t *pix, ptrdiff_t line_size);
28 int ff_pix_norm1_sse2(const uint8_t *pix, ptrdiff_t line_size);
29 
30 #if HAVE_INLINE_ASM
31 
32 #define PHADDD(a, t) \
33  "movq " #a ", " #t " \n\t" \
34  "psrlq $32, " #a " \n\t" \
35  "paddd " #t ", " #a " \n\t"
36 
37 /*
38  * pmulhw: dst[0 - 15] = (src[0 - 15] * dst[0 - 15])[16 - 31]
39  * pmulhrw: dst[0 - 15] = (src[0 - 15] * dst[0 - 15] + 0x8000)[16 - 31]
40  * pmulhrsw: dst[0 - 15] = (src[0 - 15] * dst[0 - 15] + 0x4000)[15 - 30]
41  */
42 #define PMULHRW(x, y, s, o) \
43  "pmulhw " #s ", " #x " \n\t" \
44  "pmulhw " #s ", " #y " \n\t" \
45  "paddw " #o ", " #x " \n\t" \
46  "paddw " #o ", " #y " \n\t" \
47  "psraw $1, " #x " \n\t" \
48  "psraw $1, " #y " \n\t"
49 #define DEF(x) x ## _mmx
50 #define SET_RND MOVQ_WONE
51 #define SCALE_OFFSET 1
52 
54 
55 #undef DEF
56 #undef SET_RND
57 #undef SCALE_OFFSET
58 #undef PMULHRW
59 
60 #define DEF(x) x ## _3dnow
61 #define SET_RND(x)
62 #define SCALE_OFFSET 0
63 #define PMULHRW(x, y, s, o) \
64  "pmulhrw " #s ", " #x " \n\t" \
65  "pmulhrw " #s ", " #y " \n\t"
66 
68 
69 #undef DEF
70 #undef SET_RND
71 #undef SCALE_OFFSET
72 #undef PMULHRW
73 
74 #if HAVE_SSSE3_INLINE
75 #undef PHADDD
76 #define DEF(x) x ## _ssse3
77 #define SET_RND(x)
78 #define SCALE_OFFSET -1
79 
80 #define PHADDD(a, t) \
81  "pshufw $0x0E, " #a ", " #t " \n\t" \
82  /* faster than phaddd on core2 */ \
83  "paddd " #t ", " #a " \n\t"
84 
85 #define PMULHRW(x, y, s, o) \
86  "pmulhrsw " #s ", " #x " \n\t" \
87  "pmulhrsw " #s ", " #y " \n\t"
88 
90 
91 #undef DEF
92 #undef SET_RND
93 #undef SCALE_OFFSET
94 #undef PMULHRW
95 #undef PHADDD
96 #endif /* HAVE_SSSE3_INLINE */
97 
98 /* Draw the edges of width 'w' of an image of size width, height */
99 static void draw_edges_mmx(uint8_t *buf, ptrdiff_t wrap, int width, int height,
100  int w, int h, int sides)
101 {
102  uint8_t *ptr, *last_line;
103  int i;
104 
105  /* left and right */
106  ptr = buf;
107  if (w == 8) {
108  __asm__ volatile (
109  "1: \n\t"
110  "movd (%0), %%mm0 \n\t"
111  "punpcklbw %%mm0, %%mm0 \n\t"
112  "punpcklwd %%mm0, %%mm0 \n\t"
113  "punpckldq %%mm0, %%mm0 \n\t"
114  "movq %%mm0, -8(%0) \n\t"
115  "movq -8(%0, %2), %%mm1 \n\t"
116  "punpckhbw %%mm1, %%mm1 \n\t"
117  "punpckhwd %%mm1, %%mm1 \n\t"
118  "punpckhdq %%mm1, %%mm1 \n\t"
119  "movq %%mm1, (%0, %2) \n\t"
120  "add %1, %0 \n\t"
121  "cmp %3, %0 \n\t"
122  "jnz 1b \n\t"
123  : "+r" (ptr)
124  : "r" ((x86_reg) wrap), "r" ((x86_reg) width),
125  "r" (ptr + wrap * height));
126  } else if (w == 16) {
127  __asm__ volatile (
128  "1: \n\t"
129  "movd (%0), %%mm0 \n\t"
130  "punpcklbw %%mm0, %%mm0 \n\t"
131  "punpcklwd %%mm0, %%mm0 \n\t"
132  "punpckldq %%mm0, %%mm0 \n\t"
133  "movq %%mm0, -8(%0) \n\t"
134  "movq %%mm0, -16(%0) \n\t"
135  "movq -8(%0, %2), %%mm1 \n\t"
136  "punpckhbw %%mm1, %%mm1 \n\t"
137  "punpckhwd %%mm1, %%mm1 \n\t"
138  "punpckhdq %%mm1, %%mm1 \n\t"
139  "movq %%mm1, (%0, %2) \n\t"
140  "movq %%mm1, 8(%0, %2) \n\t"
141  "add %1, %0 \n\t"
142  "cmp %3, %0 \n\t"
143  "jnz 1b \n\t"
144  : "+r"(ptr)
145  : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
146  );
147  } else {
148  av_assert1(w == 4);
149  __asm__ volatile (
150  "1: \n\t"
151  "movd (%0), %%mm0 \n\t"
152  "punpcklbw %%mm0, %%mm0 \n\t"
153  "punpcklwd %%mm0, %%mm0 \n\t"
154  "movd %%mm0, -4(%0) \n\t"
155  "movd -4(%0, %2), %%mm1 \n\t"
156  "punpcklbw %%mm1, %%mm1 \n\t"
157  "punpckhwd %%mm1, %%mm1 \n\t"
158  "punpckhdq %%mm1, %%mm1 \n\t"
159  "movd %%mm1, (%0, %2) \n\t"
160  "add %1, %0 \n\t"
161  "cmp %3, %0 \n\t"
162  "jnz 1b \n\t"
163  : "+r" (ptr)
164  : "r" ((x86_reg) wrap), "r" ((x86_reg) width),
165  "r" (ptr + wrap * height));
166  }
167 
168  /* top and bottom + corners */
169  buf -= w;
170  last_line = buf + (height - 1) * wrap;
171  if (sides & EDGE_TOP)
172  for (i = 0; i < h; i++)
173  // top
174  memcpy(buf - (i + 1) * wrap, buf, width + w + w);
175  if (sides & EDGE_BOTTOM)
176  for (i = 0; i < h; i++)
177  // bottom
178  memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
179 }
180 
181 #endif /* HAVE_INLINE_ASM */
182 
184  AVCodecContext *avctx)
185 {
186  int cpu_flags = av_get_cpu_flags();
187 
188  if (EXTERNAL_SSE2(cpu_flags)) {
189  c->pix_sum = ff_pix_sum16_sse2;
190  c->pix_norm1 = ff_pix_norm1_sse2;
191  }
192 
193  if (EXTERNAL_XOP(cpu_flags)) {
194  c->pix_sum = ff_pix_sum16_xop;
195  }
196 
197 #if HAVE_INLINE_ASM
198 
199  if (INLINE_MMX(cpu_flags)) {
200  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
201  c->try_8x8basis = try_8x8basis_mmx;
202  }
203  c->add_8x8basis = add_8x8basis_mmx;
204 
205  if (avctx->bits_per_raw_sample <= 8) {
206  c->draw_edges = draw_edges_mmx;
207  }
208  }
209 
210  if (INLINE_AMD3DNOW(cpu_flags)) {
211  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
212  c->try_8x8basis = try_8x8basis_3dnow;
213  }
214  c->add_8x8basis = add_8x8basis_3dnow;
215  }
216 
217 #if HAVE_SSSE3_INLINE
218  if (INLINE_SSSE3(cpu_flags)) {
219  if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
220  c->try_8x8basis = try_8x8basis_ssse3;
221  }
222  c->add_8x8basis = add_8x8basis_ssse3;
223  }
224 #endif /* HAVE_SSSE3_INLINE */
225 
226 #endif /* HAVE_INLINE_ASM */
227 }
INLINE_MMX
#define INLINE_MMX(flags)
Definition: cpu.h:87
cpu.h
EDGE_BOTTOM
#define EDGE_BOTTOM
Definition: mpegvideoencdsp.h:30
mpegvideoenc_qns_template.c
x86_reg
int x86_reg
Definition: asm.h:72
ff_mpegvideoencdsp_init_x86
av_cold void ff_mpegvideoencdsp_init_x86(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
Definition: mpegvideoencdsp_init.c:183
w
uint8_t w
Definition: llviddspenc.c:38
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:56
wrap
#define wrap(func)
Definition: neontest.h:65
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:508
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
ff_pix_norm1_sse2
int ff_pix_norm1_sse2(const uint8_t *pix, ptrdiff_t line_size)
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1585
EXTERNAL_XOP
#define EXTERNAL_XOP(flags)
Definition: cpu.h:73
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
height
#define height
Definition: dsp.h:85
cpu.h
attributes.h
ff_pix_sum16_xop
int ff_pix_sum16_xop(const uint8_t *pix, ptrdiff_t line_size)
EXTERNAL_SSE2
#define EXTERNAL_SSE2(flags)
Definition: cpu.h:59
MpegvideoEncDSPContext
Definition: mpegvideoencdsp.h:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
avcodec.h
ff_pix_sum16_sse2
int ff_pix_sum16_sse2(const uint8_t *pix, ptrdiff_t line_size)
__asm__
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
INLINE_SSSE3
#define INLINE_SSSE3(flags)
Definition: cpu.h:96
AVCodecContext
main external API structure.
Definition: avcodec.h:451
EDGE_TOP
#define EDGE_TOP
Definition: mpegvideoencdsp.h:29
mpegvideoencdsp.h
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
h
h
Definition: vp9dsp_template.c:2070
width
#define width
Definition: dsp.h:85
INLINE_AMD3DNOW
#define INLINE_AMD3DNOW(flags)
Definition: cpu.h:85