Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 uint16_t, inv_zigzag_direct16)[64];
32 
33 #if HAVE_MMX_INLINE
34 #define COMPILE_TEMPLATE_MMXEXT 0
35 #define COMPILE_TEMPLATE_SSE2 0
36 #define COMPILE_TEMPLATE_SSSE3 0
37 #define RENAME(a) a ## _mmx
38 #define RENAME_FDCT(a) a ## _mmx
39 #include "mpegvideoenc_template.c"
40 #endif /* HAVE_MMX_INLINE */
41 
42 #if HAVE_MMXEXT_INLINE
43 #undef COMPILE_TEMPLATE_SSSE3
44 #undef COMPILE_TEMPLATE_SSE2
45 #undef COMPILE_TEMPLATE_MMXEXT
46 #define COMPILE_TEMPLATE_MMXEXT 1
47 #define COMPILE_TEMPLATE_SSE2 0
48 #define COMPILE_TEMPLATE_SSSE3 0
49 #undef RENAME
50 #undef RENAME_FDCT
51 #define RENAME(a) a ## _mmxext
52 #define RENAME_FDCT(a) a ## _mmxext
53 #include "mpegvideoenc_template.c"
54 #endif /* HAVE_MMXEXT_INLINE */
55 
56 #if HAVE_SSE2_INLINE
57 #undef COMPILE_TEMPLATE_MMXEXT
58 #undef COMPILE_TEMPLATE_SSE2
59 #undef COMPILE_TEMPLATE_SSSE3
60 #define COMPILE_TEMPLATE_MMXEXT 0
61 #define COMPILE_TEMPLATE_SSE2 1
62 #define COMPILE_TEMPLATE_SSSE3 0
63 #undef RENAME
64 #undef RENAME_FDCT
65 #define RENAME(a) a ## _sse2
66 #define RENAME_FDCT(a) a ## _sse2
67 #include "mpegvideoenc_template.c"
68 #endif /* HAVE_SSE2_INLINE */
69 
70 #if HAVE_SSSE3_INLINE
71 #undef COMPILE_TEMPLATE_MMXEXT
72 #undef COMPILE_TEMPLATE_SSE2
73 #undef COMPILE_TEMPLATE_SSSE3
74 #define COMPILE_TEMPLATE_MMXEXT 0
75 #define COMPILE_TEMPLATE_SSE2 1
76 #define COMPILE_TEMPLATE_SSSE3 1
77 #undef RENAME
78 #undef RENAME_FDCT
79 #define RENAME(a) a ## _ssse3
80 #define RENAME_FDCT(a) a ## _sse2
81 #include "mpegvideoenc_template.c"
82 #endif /* HAVE_SSSE3_INLINE */
83 
84 #if HAVE_INLINE_ASM
85 static void denoise_dct_mmx(MpegEncContext *s, int16_t *block){
86  const int intra= s->mb_intra;
87  int *sum= s->dct_error_sum[intra];
88  uint16_t *offset= s->dct_offset[intra];
89 
90  s->dct_count[intra]++;
91 
92  __asm__ volatile(
93  "pxor %%mm7, %%mm7 \n\t"
94  "1: \n\t"
95  "pxor %%mm0, %%mm0 \n\t"
96  "pxor %%mm1, %%mm1 \n\t"
97  "movq (%0), %%mm2 \n\t"
98  "movq 8(%0), %%mm3 \n\t"
99  "pcmpgtw %%mm2, %%mm0 \n\t"
100  "pcmpgtw %%mm3, %%mm1 \n\t"
101  "pxor %%mm0, %%mm2 \n\t"
102  "pxor %%mm1, %%mm3 \n\t"
103  "psubw %%mm0, %%mm2 \n\t"
104  "psubw %%mm1, %%mm3 \n\t"
105  "movq %%mm2, %%mm4 \n\t"
106  "movq %%mm3, %%mm5 \n\t"
107  "psubusw (%2), %%mm2 \n\t"
108  "psubusw 8(%2), %%mm3 \n\t"
109  "pxor %%mm0, %%mm2 \n\t"
110  "pxor %%mm1, %%mm3 \n\t"
111  "psubw %%mm0, %%mm2 \n\t"
112  "psubw %%mm1, %%mm3 \n\t"
113  "movq %%mm2, (%0) \n\t"
114  "movq %%mm3, 8(%0) \n\t"
115  "movq %%mm4, %%mm2 \n\t"
116  "movq %%mm5, %%mm3 \n\t"
117  "punpcklwd %%mm7, %%mm4 \n\t"
118  "punpckhwd %%mm7, %%mm2 \n\t"
119  "punpcklwd %%mm7, %%mm5 \n\t"
120  "punpckhwd %%mm7, %%mm3 \n\t"
121  "paddd (%1), %%mm4 \n\t"
122  "paddd 8(%1), %%mm2 \n\t"
123  "paddd 16(%1), %%mm5 \n\t"
124  "paddd 24(%1), %%mm3 \n\t"
125  "movq %%mm4, (%1) \n\t"
126  "movq %%mm2, 8(%1) \n\t"
127  "movq %%mm5, 16(%1) \n\t"
128  "movq %%mm3, 24(%1) \n\t"
129  "add $16, %0 \n\t"
130  "add $32, %1 \n\t"
131  "add $16, %2 \n\t"
132  "cmp %3, %0 \n\t"
133  " jb 1b \n\t"
134  : "+r" (block), "+r" (sum), "+r" (offset)
135  : "r"(block+64)
136  );
137 }
138 
139 static void denoise_dct_sse2(MpegEncContext *s, int16_t *block){
140  const int intra= s->mb_intra;
141  int *sum= s->dct_error_sum[intra];
142  uint16_t *offset= s->dct_offset[intra];
143 
144  s->dct_count[intra]++;
145 
146  __asm__ volatile(
147  "pxor %%xmm7, %%xmm7 \n\t"
148  "1: \n\t"
149  "pxor %%xmm0, %%xmm0 \n\t"
150  "pxor %%xmm1, %%xmm1 \n\t"
151  "movdqa (%0), %%xmm2 \n\t"
152  "movdqa 16(%0), %%xmm3 \n\t"
153  "pcmpgtw %%xmm2, %%xmm0 \n\t"
154  "pcmpgtw %%xmm3, %%xmm1 \n\t"
155  "pxor %%xmm0, %%xmm2 \n\t"
156  "pxor %%xmm1, %%xmm3 \n\t"
157  "psubw %%xmm0, %%xmm2 \n\t"
158  "psubw %%xmm1, %%xmm3 \n\t"
159  "movdqa %%xmm2, %%xmm4 \n\t"
160  "movdqa %%xmm3, %%xmm5 \n\t"
161  "psubusw (%2), %%xmm2 \n\t"
162  "psubusw 16(%2), %%xmm3 \n\t"
163  "pxor %%xmm0, %%xmm2 \n\t"
164  "pxor %%xmm1, %%xmm3 \n\t"
165  "psubw %%xmm0, %%xmm2 \n\t"
166  "psubw %%xmm1, %%xmm3 \n\t"
167  "movdqa %%xmm2, (%0) \n\t"
168  "movdqa %%xmm3, 16(%0) \n\t"
169  "movdqa %%xmm4, %%xmm6 \n\t"
170  "movdqa %%xmm5, %%xmm0 \n\t"
171  "punpcklwd %%xmm7, %%xmm4 \n\t"
172  "punpckhwd %%xmm7, %%xmm6 \n\t"
173  "punpcklwd %%xmm7, %%xmm5 \n\t"
174  "punpckhwd %%xmm7, %%xmm0 \n\t"
175  "paddd (%1), %%xmm4 \n\t"
176  "paddd 16(%1), %%xmm6 \n\t"
177  "paddd 32(%1), %%xmm5 \n\t"
178  "paddd 48(%1), %%xmm0 \n\t"
179  "movdqa %%xmm4, (%1) \n\t"
180  "movdqa %%xmm6, 16(%1) \n\t"
181  "movdqa %%xmm5, 32(%1) \n\t"
182  "movdqa %%xmm0, 48(%1) \n\t"
183  "add $32, %0 \n\t"
184  "add $64, %1 \n\t"
185  "add $32, %2 \n\t"
186  "cmp %3, %0 \n\t"
187  " jb 1b \n\t"
188  : "+r" (block), "+r" (sum), "+r" (offset)
189  : "r"(block+64)
190  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
191  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
192  );
193 }
194 #endif /* HAVE_INLINE_ASM */
195 
197 {
198  const int dct_algo = s->avctx->dct_algo;
199  int i;
200 
201  for (i = 0; i < 64; i++)
203 
204  if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
205 #if HAVE_MMX_INLINE
206  int cpu_flags = av_get_cpu_flags();
207  if (INLINE_MMX(cpu_flags)) {
208  s->dct_quantize = dct_quantize_mmx;
209  s->denoise_dct = denoise_dct_mmx;
210  }
211 #endif
212 #if HAVE_MMXEXT_INLINE
213  if (INLINE_MMXEXT(cpu_flags))
214  s->dct_quantize = dct_quantize_mmxext;
215 #endif
216 #if HAVE_SSE2_INLINE
217  if (INLINE_SSE2(cpu_flags)) {
218  s->dct_quantize = dct_quantize_sse2;
219  s->denoise_dct = denoise_dct_sse2;
220  }
221 #endif
222 #if HAVE_SSSE3_INLINE
223  if (INLINE_SSSE3(cpu_flags))
224  s->dct_quantize = dct_quantize_ssse3;
225 #endif
226  }
227 }
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:2458
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
#define INLINE_SSE2(flags)
Definition: cpu.h:66
mpegvideo header.
Macro definitions for various function/variable attributes.
static uint16_t inv_zigzag_direct16[64]
Definition: mpegvideoenc.c:31
#define av_cold
Definition: attributes.h:66
uint16_t(* dct_offset)[64]
Definition: mpegvideo.h:451
av_cold void ff_mpv_encode_init_x86(MpegEncContext *s)
Definition: mpegvideoenc.c:196
#define XMM_CLOBBERS_ONLY(...)
Definition: asm.h:97
#define FF_DCT_MMX
Definition: avcodec.h:2464
#define INLINE_MMX(flags)
Definition: cpu.h:63
#define INLINE_SSSE3(flags)
Definition: cpu.h:68
Libavcodec external API header.
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:47
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
Definition: mpegvideo.h:624
void(* denoise_dct)(struct MpegEncContext *s, int16_t *block)
Definition: mpegvideo.h:626
MpegEncContext.
Definition: mpegvideo.h:204
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
#define INLINE_MMXEXT(flags)
Definition: cpu.h:64
int(* dct_error_sum)[64]
Definition: mpegvideo.h:449
int dct_count[2]
Definition: mpegvideo.h:450
#define FF_DCT_AUTO
Definition: avcodec.h:2459
static int16_t block[64]
Definition: dct-test.c:88