2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/attributes.h"
23 #include "libavutil/cpu.h"
24 #include "libavutil/x86/asm.h"
25 #include "libavutil/x86/cpu.h"
26 #include "libavcodec/avcodec.h"
27 #include "libavcodec/dct.h"
28 #include "libavcodec/mpegvideo.h"
29 #include "dsputil_x86.h"
31 /* not permutated inverse zigzag_direct + 1 for MMX quantizer */
32 DECLARE_ALIGNED(16, static uint16_t, inv_zigzag_direct16)[64];
35 #define COMPILE_TEMPLATE_MMXEXT 0
36 #define COMPILE_TEMPLATE_SSE2 0
37 #define COMPILE_TEMPLATE_SSSE3 0
38 #define RENAME(a) a ## _MMX
39 #define RENAMEl(a) a ## _mmx
40 #include "mpegvideoenc_template.c"
41 #endif /* HAVE_MMX_INLINE */
43 #if HAVE_MMXEXT_INLINE
44 #undef COMPILE_TEMPLATE_SSSE3
45 #undef COMPILE_TEMPLATE_SSE2
46 #undef COMPILE_TEMPLATE_MMXEXT
47 #define COMPILE_TEMPLATE_MMXEXT 1
48 #define COMPILE_TEMPLATE_SSE2 0
49 #define COMPILE_TEMPLATE_SSSE3 0
52 #define RENAME(a) a ## _MMXEXT
53 #define RENAMEl(a) a ## _mmxext
54 #include "mpegvideoenc_template.c"
55 #endif /* HAVE_MMXEXT_INLINE */
58 #undef COMPILE_TEMPLATE_MMXEXT
59 #undef COMPILE_TEMPLATE_SSE2
60 #undef COMPILE_TEMPLATE_SSSE3
61 #define COMPILE_TEMPLATE_MMXEXT 0
62 #define COMPILE_TEMPLATE_SSE2 1
63 #define COMPILE_TEMPLATE_SSSE3 0
66 #define RENAME(a) a ## _SSE2
67 #define RENAMEl(a) a ## _sse2
68 #include "mpegvideoenc_template.c"
69 #endif /* HAVE_SSE2_INLINE */
72 #undef COMPILE_TEMPLATE_MMXEXT
73 #undef COMPILE_TEMPLATE_SSE2
74 #undef COMPILE_TEMPLATE_SSSE3
75 #define COMPILE_TEMPLATE_MMXEXT 0
76 #define COMPILE_TEMPLATE_SSE2 1
77 #define COMPILE_TEMPLATE_SSSE3 1
80 #define RENAME(a) a ## _SSSE3
81 #define RENAMEl(a) a ## _sse2
82 #include "mpegvideoenc_template.c"
83 #endif /* HAVE_SSSE3_INLINE */
86 static void denoise_dct_mmx(MpegEncContext *s, int16_t *block){
87 const int intra= s->mb_intra;
88 int *sum= s->dct_error_sum[intra];
89 uint16_t *offset= s->dct_offset[intra];
91 s->dct_count[intra]++;
94 "pxor %%mm7, %%mm7 \n\t"
96 "pxor %%mm0, %%mm0 \n\t"
97 "pxor %%mm1, %%mm1 \n\t"
98 "movq (%0), %%mm2 \n\t"
99 "movq 8(%0), %%mm3 \n\t"
100 "pcmpgtw %%mm2, %%mm0 \n\t"
101 "pcmpgtw %%mm3, %%mm1 \n\t"
102 "pxor %%mm0, %%mm2 \n\t"
103 "pxor %%mm1, %%mm3 \n\t"
104 "psubw %%mm0, %%mm2 \n\t"
105 "psubw %%mm1, %%mm3 \n\t"
106 "movq %%mm2, %%mm4 \n\t"
107 "movq %%mm3, %%mm5 \n\t"
108 "psubusw (%2), %%mm2 \n\t"
109 "psubusw 8(%2), %%mm3 \n\t"
110 "pxor %%mm0, %%mm2 \n\t"
111 "pxor %%mm1, %%mm3 \n\t"
112 "psubw %%mm0, %%mm2 \n\t"
113 "psubw %%mm1, %%mm3 \n\t"
114 "movq %%mm2, (%0) \n\t"
115 "movq %%mm3, 8(%0) \n\t"
116 "movq %%mm4, %%mm2 \n\t"
117 "movq %%mm5, %%mm3 \n\t"
118 "punpcklwd %%mm7, %%mm4 \n\t"
119 "punpckhwd %%mm7, %%mm2 \n\t"
120 "punpcklwd %%mm7, %%mm5 \n\t"
121 "punpckhwd %%mm7, %%mm3 \n\t"
122 "paddd (%1), %%mm4 \n\t"
123 "paddd 8(%1), %%mm2 \n\t"
124 "paddd 16(%1), %%mm5 \n\t"
125 "paddd 24(%1), %%mm3 \n\t"
126 "movq %%mm4, (%1) \n\t"
127 "movq %%mm2, 8(%1) \n\t"
128 "movq %%mm5, 16(%1) \n\t"
129 "movq %%mm3, 24(%1) \n\t"
135 : "+r" (block), "+r" (sum), "+r" (offset)
140 static void denoise_dct_sse2(MpegEncContext *s, int16_t *block){
141 const int intra= s->mb_intra;
142 int *sum= s->dct_error_sum[intra];
143 uint16_t *offset= s->dct_offset[intra];
145 s->dct_count[intra]++;
148 "pxor %%xmm7, %%xmm7 \n\t"
150 "pxor %%xmm0, %%xmm0 \n\t"
151 "pxor %%xmm1, %%xmm1 \n\t"
152 "movdqa (%0), %%xmm2 \n\t"
153 "movdqa 16(%0), %%xmm3 \n\t"
154 "pcmpgtw %%xmm2, %%xmm0 \n\t"
155 "pcmpgtw %%xmm3, %%xmm1 \n\t"
156 "pxor %%xmm0, %%xmm2 \n\t"
157 "pxor %%xmm1, %%xmm3 \n\t"
158 "psubw %%xmm0, %%xmm2 \n\t"
159 "psubw %%xmm1, %%xmm3 \n\t"
160 "movdqa %%xmm2, %%xmm4 \n\t"
161 "movdqa %%xmm3, %%xmm5 \n\t"
162 "psubusw (%2), %%xmm2 \n\t"
163 "psubusw 16(%2), %%xmm3 \n\t"
164 "pxor %%xmm0, %%xmm2 \n\t"
165 "pxor %%xmm1, %%xmm3 \n\t"
166 "psubw %%xmm0, %%xmm2 \n\t"
167 "psubw %%xmm1, %%xmm3 \n\t"
168 "movdqa %%xmm2, (%0) \n\t"
169 "movdqa %%xmm3, 16(%0) \n\t"
170 "movdqa %%xmm4, %%xmm6 \n\t"
171 "movdqa %%xmm5, %%xmm0 \n\t"
172 "punpcklwd %%xmm7, %%xmm4 \n\t"
173 "punpckhwd %%xmm7, %%xmm6 \n\t"
174 "punpcklwd %%xmm7, %%xmm5 \n\t"
175 "punpckhwd %%xmm7, %%xmm0 \n\t"
176 "paddd (%1), %%xmm4 \n\t"
177 "paddd 16(%1), %%xmm6 \n\t"
178 "paddd 32(%1), %%xmm5 \n\t"
179 "paddd 48(%1), %%xmm0 \n\t"
180 "movdqa %%xmm4, (%1) \n\t"
181 "movdqa %%xmm6, 16(%1) \n\t"
182 "movdqa %%xmm5, 32(%1) \n\t"
183 "movdqa %%xmm0, 48(%1) \n\t"
189 : "+r" (block), "+r" (sum), "+r" (offset)
191 XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
192 "%xmm4", "%xmm5", "%xmm6", "%xmm7")
195 #endif /* HAVE_INLINE_ASM */
197 av_cold void ff_dct_encode_init_x86(MpegEncContext *s)
199 const int dct_algo = s->avctx->dct_algo;
202 for (i = 0; i < 64; i++)
203 inv_zigzag_direct16[ff_zigzag_direct[i]] = i + 1;
205 if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
207 int cpu_flags = av_get_cpu_flags();
208 if (INLINE_MMX(cpu_flags)) {
209 s->dct_quantize = dct_quantize_MMX;
210 s->denoise_dct = denoise_dct_mmx;
213 #if HAVE_MMXEXT_INLINE
214 if (INLINE_MMXEXT(cpu_flags))
215 s->dct_quantize = dct_quantize_MMXEXT;
218 if (INLINE_SSE2(cpu_flags)) {
219 s->dct_quantize = dct_quantize_SSE2;
220 s->denoise_dct = denoise_dct_sse2;
223 #if HAVE_SSSE3_INLINE
224 if (INLINE_SSSE3(cpu_flags))
225 s->dct_quantize = dct_quantize_SSSE3;