FFmpeg  4.0
mpegvideoenc.c
Go to the documentation of this file.
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard
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 "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 
30 /* not permutated inverse zigzag_direct + 1 for MMX quantizer */
31 DECLARE_ALIGNED(16, static const uint16_t, inv_zigzag_direct16)[64] = {
32  1, 2, 6, 7, 15, 16, 28, 29,
33  3, 5, 8, 14, 17, 27, 30, 43,
34  4, 9, 13, 18, 26, 31, 42, 44,
35  10, 12, 19, 25, 32, 41, 45, 54,
36  11, 20, 24, 33, 40, 46, 53, 55,
37  21, 23, 34, 39, 47, 52, 56, 61,
38  22, 35, 38, 48, 51, 57, 60, 62,
39  36, 37, 49, 50, 58, 59, 63, 64,
40 };
41 
42 #if HAVE_6REGS
43 
44 #if HAVE_MMX_INLINE
45 #define COMPILE_TEMPLATE_MMXEXT 0
46 #define COMPILE_TEMPLATE_SSE2 0
47 #define COMPILE_TEMPLATE_SSSE3 0
48 #define RENAME(a) a ## _mmx
49 #define RENAME_FDCT(a) a ## _mmx
50 #include "mpegvideoenc_template.c"
51 #endif /* HAVE_MMX_INLINE */
52 
53 #if HAVE_MMXEXT_INLINE
54 #undef COMPILE_TEMPLATE_SSSE3
55 #undef COMPILE_TEMPLATE_SSE2
56 #undef COMPILE_TEMPLATE_MMXEXT
57 #define COMPILE_TEMPLATE_MMXEXT 1
58 #define COMPILE_TEMPLATE_SSE2 0
59 #define COMPILE_TEMPLATE_SSSE3 0
60 #undef RENAME
61 #undef RENAME_FDCT
62 #define RENAME(a) a ## _mmxext
63 #define RENAME_FDCT(a) a ## _mmxext
64 #include "mpegvideoenc_template.c"
65 #endif /* HAVE_MMXEXT_INLINE */
66 
67 #if HAVE_SSE2_INLINE
68 #undef COMPILE_TEMPLATE_MMXEXT
69 #undef COMPILE_TEMPLATE_SSE2
70 #undef COMPILE_TEMPLATE_SSSE3
71 #define COMPILE_TEMPLATE_MMXEXT 0
72 #define COMPILE_TEMPLATE_SSE2 1
73 #define COMPILE_TEMPLATE_SSSE3 0
74 #undef RENAME
75 #undef RENAME_FDCT
76 #define RENAME(a) a ## _sse2
77 #define RENAME_FDCT(a) a ## _sse2
78 #include "mpegvideoenc_template.c"
79 #endif /* HAVE_SSE2_INLINE */
80 
81 #if HAVE_SSSE3_INLINE
82 #undef COMPILE_TEMPLATE_MMXEXT
83 #undef COMPILE_TEMPLATE_SSE2
84 #undef COMPILE_TEMPLATE_SSSE3
85 #define COMPILE_TEMPLATE_MMXEXT 0
86 #define COMPILE_TEMPLATE_SSE2 1
87 #define COMPILE_TEMPLATE_SSSE3 1
88 #undef RENAME
89 #undef RENAME_FDCT
90 #define RENAME(a) a ## _ssse3
91 #define RENAME_FDCT(a) a ## _sse2
92 #include "mpegvideoenc_template.c"
93 #endif /* HAVE_SSSE3_INLINE */
94 
95 #endif /* HAVE_6REGS */
96 
97 #if HAVE_INLINE_ASM
98 #if HAVE_MMX_INLINE
99 static void denoise_dct_mmx(MpegEncContext *s, int16_t *block){
100  const int intra= s->mb_intra;
101  int *sum= s->dct_error_sum[intra];
102  uint16_t *offset= s->dct_offset[intra];
103 
104  s->dct_count[intra]++;
105 
106  __asm__ volatile(
107  "pxor %%mm7, %%mm7 \n\t"
108  "1: \n\t"
109  "pxor %%mm0, %%mm0 \n\t"
110  "pxor %%mm1, %%mm1 \n\t"
111  "movq (%0), %%mm2 \n\t"
112  "movq 8(%0), %%mm3 \n\t"
113  "pcmpgtw %%mm2, %%mm0 \n\t"
114  "pcmpgtw %%mm3, %%mm1 \n\t"
115  "pxor %%mm0, %%mm2 \n\t"
116  "pxor %%mm1, %%mm3 \n\t"
117  "psubw %%mm0, %%mm2 \n\t"
118  "psubw %%mm1, %%mm3 \n\t"
119  "movq %%mm2, %%mm4 \n\t"
120  "movq %%mm3, %%mm5 \n\t"
121  "psubusw (%2), %%mm2 \n\t"
122  "psubusw 8(%2), %%mm3 \n\t"
123  "pxor %%mm0, %%mm2 \n\t"
124  "pxor %%mm1, %%mm3 \n\t"
125  "psubw %%mm0, %%mm2 \n\t"
126  "psubw %%mm1, %%mm3 \n\t"
127  "movq %%mm2, (%0) \n\t"
128  "movq %%mm3, 8(%0) \n\t"
129  "movq %%mm4, %%mm2 \n\t"
130  "movq %%mm5, %%mm3 \n\t"
131  "punpcklwd %%mm7, %%mm4 \n\t"
132  "punpckhwd %%mm7, %%mm2 \n\t"
133  "punpcklwd %%mm7, %%mm5 \n\t"
134  "punpckhwd %%mm7, %%mm3 \n\t"
135  "paddd (%1), %%mm4 \n\t"
136  "paddd 8(%1), %%mm2 \n\t"
137  "paddd 16(%1), %%mm5 \n\t"
138  "paddd 24(%1), %%mm3 \n\t"
139  "movq %%mm4, (%1) \n\t"
140  "movq %%mm2, 8(%1) \n\t"
141  "movq %%mm5, 16(%1) \n\t"
142  "movq %%mm3, 24(%1) \n\t"
143  "add $16, %0 \n\t"
144  "add $32, %1 \n\t"
145  "add $16, %2 \n\t"
146  "cmp %3, %0 \n\t"
147  " jb 1b \n\t"
148  : "+r" (block), "+r" (sum), "+r" (offset)
149  : "r"(block+64)
150  );
151 }
152 #endif /* HAVE_MMX_INLINE */
153 
154 #if HAVE_SSE2_INLINE
155 static void denoise_dct_sse2(MpegEncContext *s, int16_t *block){
156  const int intra= s->mb_intra;
157  int *sum= s->dct_error_sum[intra];
158  uint16_t *offset= s->dct_offset[intra];
159 
160  s->dct_count[intra]++;
161 
162  __asm__ volatile(
163  "pxor %%xmm7, %%xmm7 \n\t"
164  "1: \n\t"
165  "pxor %%xmm0, %%xmm0 \n\t"
166  "pxor %%xmm1, %%xmm1 \n\t"
167  "movdqa (%0), %%xmm2 \n\t"
168  "movdqa 16(%0), %%xmm3 \n\t"
169  "pcmpgtw %%xmm2, %%xmm0 \n\t"
170  "pcmpgtw %%xmm3, %%xmm1 \n\t"
171  "pxor %%xmm0, %%xmm2 \n\t"
172  "pxor %%xmm1, %%xmm3 \n\t"
173  "psubw %%xmm0, %%xmm2 \n\t"
174  "psubw %%xmm1, %%xmm3 \n\t"
175  "movdqa %%xmm2, %%xmm4 \n\t"
176  "movdqa %%xmm3, %%xmm5 \n\t"
177  "psubusw (%2), %%xmm2 \n\t"
178  "psubusw 16(%2), %%xmm3 \n\t"
179  "pxor %%xmm0, %%xmm2 \n\t"
180  "pxor %%xmm1, %%xmm3 \n\t"
181  "psubw %%xmm0, %%xmm2 \n\t"
182  "psubw %%xmm1, %%xmm3 \n\t"
183  "movdqa %%xmm2, (%0) \n\t"
184  "movdqa %%xmm3, 16(%0) \n\t"
185  "movdqa %%xmm4, %%xmm6 \n\t"
186  "movdqa %%xmm5, %%xmm0 \n\t"
187  "punpcklwd %%xmm7, %%xmm4 \n\t"
188  "punpckhwd %%xmm7, %%xmm6 \n\t"
189  "punpcklwd %%xmm7, %%xmm5 \n\t"
190  "punpckhwd %%xmm7, %%xmm0 \n\t"
191  "paddd (%1), %%xmm4 \n\t"
192  "paddd 16(%1), %%xmm6 \n\t"
193  "paddd 32(%1), %%xmm5 \n\t"
194  "paddd 48(%1), %%xmm0 \n\t"
195  "movdqa %%xmm4, (%1) \n\t"
196  "movdqa %%xmm6, 16(%1) \n\t"
197  "movdqa %%xmm5, 32(%1) \n\t"
198  "movdqa %%xmm0, 48(%1) \n\t"
199  "add $32, %0 \n\t"
200  "add $64, %1 \n\t"
201  "add $32, %2 \n\t"
202  "cmp %3, %0 \n\t"
203  " jb 1b \n\t"
204  : "+r" (block), "+r" (sum), "+r" (offset)
205  : "r"(block+64)
206  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
207  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
208  );
209 }
210 #endif /* HAVE_SSE2_INLINE */
211 #endif /* HAVE_INLINE_ASM */
212 
214 {
215  const int dct_algo = s->avctx->dct_algo;
216 
217  if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
218 #if HAVE_MMX_INLINE
219  int cpu_flags = av_get_cpu_flags();
220  if (INLINE_MMX(cpu_flags)) {
221 #if HAVE_6REGS
222  s->dct_quantize = dct_quantize_mmx;
223 #endif
224  s->denoise_dct = denoise_dct_mmx;
225  }
226 #endif
227 #if HAVE_6REGS && HAVE_MMXEXT_INLINE
228  if (INLINE_MMXEXT(cpu_flags))
229  s->dct_quantize = dct_quantize_mmxext;
230 #endif
231 #if HAVE_SSE2_INLINE
232  if (INLINE_SSE2(cpu_flags)) {
233 #if HAVE_6REGS
234  s->dct_quantize = dct_quantize_sse2;
235 #endif
236  s->denoise_dct = denoise_dct_sse2;
237  }
238 #endif
239 #if HAVE_6REGS && HAVE_SSSE3_INLINE
240  if (INLINE_SSSE3(cpu_flags))
241  s->dct_quantize = dct_quantize_ssse3;
242 #endif
243  }
244 }
const char * s
Definition: avisynth_c.h:768
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2700
#define INLINE_SSE2(flags)
Definition: cpu.h:89
mpegvideo header.
static atomic_int cpu_flags
Definition: cpu.c:50
Macro definitions for various function/variable attributes.
static int16_t block[64]
Definition: dct.c:115
#define av_cold
Definition: attributes.h:82
uint16_t(* dct_offset)[64]
Definition: mpegvideo.h:334
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
#define FF_DCT_MMX
Definition: avcodec.h:2704
#define INLINE_MMX(flags)
Definition: cpu.h:86
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define INLINE_SSSE3(flags)
Definition: cpu.h:95
static const uint16_t inv_zigzag_direct16[64]
Definition: mpegvideoenc.c:31
av_cold void ff_dct_encode_init_x86(MpegEncContext *s)
Definition: mpegvideoenc.c:213
Libavcodec external API header.
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:93
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:534
void(* denoise_dct)(struct MpegEncContext *s, int16_t *block)
Definition: mpegvideo.h:536
MpegEncContext.
Definition: mpegvideo.h:81
#define XMM_CLOBBERS_ONLY(...)
Definition: asm.h:99
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
#define INLINE_MMXEXT(flags)
Definition: cpu.h:87
int(* dct_error_sum)[64]
Definition: mpegvideo.h:332
int dct_count[2]
Definition: mpegvideo.h:333
#define FF_DCT_AUTO
Definition: avcodec.h:2701