FFmpeg
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/mem_internal.h"
25 #include "libavutil/x86/asm.h"
26 #include "libavutil/x86/cpu.h"
27 #include "libavcodec/avcodec.h"
28 #include "libavcodec/dct.h"
30 
31 /* not permutated inverse zigzag_direct + 1 for MMX quantizer */
32 DECLARE_ALIGNED(16, static const uint16_t, inv_zigzag_direct16)[64] = {
33  1, 2, 6, 7, 15, 16, 28, 29,
34  3, 5, 8, 14, 17, 27, 30, 43,
35  4, 9, 13, 18, 26, 31, 42, 44,
36  10, 12, 19, 25, 32, 41, 45, 54,
37  11, 20, 24, 33, 40, 46, 53, 55,
38  21, 23, 34, 39, 47, 52, 56, 61,
39  22, 35, 38, 48, 51, 57, 60, 62,
40  36, 37, 49, 50, 58, 59, 63, 64,
41 };
42 
43 #if HAVE_6REGS
44 
45 #if HAVE_SSE2_INLINE
46 #define COMPILE_TEMPLATE_SSSE3 0
47 #define RENAME(a) a ## _sse2
48 #include "mpegvideoenc_template.c"
49 #endif /* HAVE_SSE2_INLINE */
50 
51 #if HAVE_SSSE3_INLINE
52 #undef COMPILE_TEMPLATE_SSSE3
53 #define COMPILE_TEMPLATE_SSSE3 1
54 #undef RENAME
55 #define RENAME(a) a ## _ssse3
56 #include "mpegvideoenc_template.c"
57 #endif /* HAVE_SSSE3_INLINE */
58 
59 #endif /* HAVE_6REGS */
60 
61 #if HAVE_INLINE_ASM
62 #if HAVE_SSE2_INLINE
63 static void denoise_dct_sse2(MPVEncContext *const s, int16_t block[])
64 {
65  const int intra = s->c.mb_intra;
66  int *sum= s->dct_error_sum[intra];
67  uint16_t *offset= s->dct_offset[intra];
68 
69  s->dct_count[intra]++;
70 
71  __asm__ volatile(
72  "pxor %%xmm7, %%xmm7 \n\t"
73  "1: \n\t"
74  "pxor %%xmm0, %%xmm0 \n\t"
75  "pxor %%xmm1, %%xmm1 \n\t"
76  "movdqa (%0), %%xmm2 \n\t"
77  "movdqa 16(%0), %%xmm3 \n\t"
78  "pcmpgtw %%xmm2, %%xmm0 \n\t"
79  "pcmpgtw %%xmm3, %%xmm1 \n\t"
80  "pxor %%xmm0, %%xmm2 \n\t"
81  "pxor %%xmm1, %%xmm3 \n\t"
82  "psubw %%xmm0, %%xmm2 \n\t"
83  "psubw %%xmm1, %%xmm3 \n\t"
84  "movdqa %%xmm2, %%xmm4 \n\t"
85  "movdqa %%xmm3, %%xmm5 \n\t"
86  "psubusw (%2), %%xmm2 \n\t"
87  "psubusw 16(%2), %%xmm3 \n\t"
88  "pxor %%xmm0, %%xmm2 \n\t"
89  "pxor %%xmm1, %%xmm3 \n\t"
90  "psubw %%xmm0, %%xmm2 \n\t"
91  "psubw %%xmm1, %%xmm3 \n\t"
92  "movdqa %%xmm2, (%0) \n\t"
93  "movdqa %%xmm3, 16(%0) \n\t"
94  "movdqa %%xmm4, %%xmm6 \n\t"
95  "movdqa %%xmm5, %%xmm0 \n\t"
96  "punpcklwd %%xmm7, %%xmm4 \n\t"
97  "punpckhwd %%xmm7, %%xmm6 \n\t"
98  "punpcklwd %%xmm7, %%xmm5 \n\t"
99  "punpckhwd %%xmm7, %%xmm0 \n\t"
100  "paddd (%1), %%xmm4 \n\t"
101  "paddd 16(%1), %%xmm6 \n\t"
102  "paddd 32(%1), %%xmm5 \n\t"
103  "paddd 48(%1), %%xmm0 \n\t"
104  "movdqa %%xmm4, (%1) \n\t"
105  "movdqa %%xmm6, 16(%1) \n\t"
106  "movdqa %%xmm5, 32(%1) \n\t"
107  "movdqa %%xmm0, 48(%1) \n\t"
108  "add $32, %0 \n\t"
109  "add $64, %1 \n\t"
110  "add $32, %2 \n\t"
111  "cmp %3, %0 \n\t"
112  " jb 1b \n\t"
113  : "+r" (block), "+r" (sum), "+r" (offset)
114  : "r"(block+64)
115  XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
116  "%xmm4", "%xmm5", "%xmm6", "%xmm7")
117  );
118 }
119 #endif /* HAVE_SSE2_INLINE */
120 #endif /* HAVE_INLINE_ASM */
121 
123 {
124  const int dct_algo = s->c.avctx->dct_algo;
125 
126  if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
127 #if HAVE_MMX_INLINE
128  int cpu_flags = av_get_cpu_flags();
129 #if HAVE_SSE2_INLINE
130  if (INLINE_SSE2(cpu_flags)) {
131 #if HAVE_6REGS
132  s->dct_quantize = dct_quantize_sse2;
133 #endif
134  s->denoise_dct = denoise_dct_sse2;
135  }
136 #endif
137 #if HAVE_6REGS && HAVE_SSSE3_INLINE
138  if (INLINE_SSSE3(cpu_flags))
139  s->dct_quantize = dct_quantize_ssse3;
140 #endif
141 #endif
142  }
143 }
MPVEncContext
Definition: mpegvideoenc.h:45
cpu.h
mem_internal.h
mpegvideoenc_template.c
mpegvideoenc.h
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
av_cold
#define av_cold
Definition: attributes.h:90
dct.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
INLINE_SSE2
#define INLINE_SSE2(flags)
Definition: cpu.h:90
FF_DCT_MMX
#define FF_DCT_MMX
Definition: avcodec.h:1516
ff_dct_encode_init_x86
av_cold void ff_dct_encode_init_x86(MPVEncContext *const s)
Definition: mpegvideoenc.c:122
asm.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
cpu.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
attributes.h
avcodec.h
__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
XMM_CLOBBERS_ONLY
#define XMM_CLOBBERS_ONLY(...)
Definition: asm.h:99
FF_DCT_AUTO
#define FF_DCT_AUTO
Definition: avcodec.h:1513
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
inv_zigzag_direct16
static const uint16_t inv_zigzag_direct16[64]
Definition: mpegvideoenc.c:32