2012-08-13 14:11:38 +03:00
|
|
|
/*
|
|
|
|
* The simplest mpeg encoder (well, it was the simplest!)
|
|
|
|
* Copyright (c) 2000,2001 Fabrice Bellard
|
|
|
|
*
|
2012-08-24 15:22:38 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-08-13 14:11:38 +03:00
|
|
|
*
|
2012-08-24 15:22:38 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-08-13 14:11:38 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-08-24 15:22:38 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-08-13 14:11:38 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-08-24 15:22:38 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-08-13 14:11:38 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "libavutil/cpu.h"
|
|
|
|
#include "libavutil/x86/asm.h"
|
2012-08-29 20:01:05 +03:00
|
|
|
#include "libavutil/x86/cpu.h"
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "libavcodec/avcodec.h"
|
2013-02-07 23:58:48 +03:00
|
|
|
#include "libavcodec/dct.h"
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "libavcodec/mpegvideo.h"
|
|
|
|
|
2013-12-01 07:05:23 +03:00
|
|
|
/* not permutated inverse zigzag_direct + 1 for MMX quantizer */
|
2016-05-22 21:20:31 +02:00
|
|
|
DECLARE_ALIGNED(16, static const uint16_t, inv_zigzag_direct16)[64] = {
|
2017-05-19 11:17:58 +02:00
|
|
|
1, 2, 6, 7, 15, 16, 28, 29,
|
|
|
|
3, 5, 8, 14, 17, 27, 30, 43,
|
|
|
|
4, 9, 13, 18, 26, 31, 42, 44,
|
|
|
|
10, 12, 19, 25, 32, 41, 45, 54,
|
|
|
|
11, 20, 24, 33, 40, 46, 53, 55,
|
|
|
|
21, 23, 34, 39, 47, 52, 56, 61,
|
|
|
|
22, 35, 38, 48, 51, 57, 60, 62,
|
|
|
|
36, 37, 49, 50, 58, 59, 63, 64,
|
2016-05-22 21:20:31 +02:00
|
|
|
};
|
2012-08-13 14:11:38 +03:00
|
|
|
|
2014-04-19 10:56:01 +03:00
|
|
|
#if HAVE_6REGS
|
|
|
|
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_MMX_INLINE
|
2012-08-28 18:37:54 +03:00
|
|
|
#define COMPILE_TEMPLATE_MMXEXT 0
|
|
|
|
#define COMPILE_TEMPLATE_SSE2 0
|
|
|
|
#define COMPILE_TEMPLATE_SSSE3 0
|
2013-12-30 19:06:45 +03:00
|
|
|
#define RENAME(a) a ## _mmx
|
|
|
|
#define RENAME_FDCT(a) a ## _mmx
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "mpegvideoenc_template.c"
|
2012-08-28 15:53:33 +03:00
|
|
|
#endif /* HAVE_MMX_INLINE */
|
2012-08-13 14:11:38 +03:00
|
|
|
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_MMXEXT_INLINE
|
2012-08-28 18:37:54 +03:00
|
|
|
#undef COMPILE_TEMPLATE_SSSE3
|
|
|
|
#undef COMPILE_TEMPLATE_SSE2
|
|
|
|
#undef COMPILE_TEMPLATE_MMXEXT
|
|
|
|
#define COMPILE_TEMPLATE_MMXEXT 1
|
|
|
|
#define COMPILE_TEMPLATE_SSE2 0
|
|
|
|
#define COMPILE_TEMPLATE_SSSE3 0
|
2012-08-13 14:11:38 +03:00
|
|
|
#undef RENAME
|
2013-12-30 19:06:45 +03:00
|
|
|
#undef RENAME_FDCT
|
|
|
|
#define RENAME(a) a ## _mmxext
|
|
|
|
#define RENAME_FDCT(a) a ## _mmxext
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "mpegvideoenc_template.c"
|
2012-08-28 15:53:33 +03:00
|
|
|
#endif /* HAVE_MMXEXT_INLINE */
|
2012-08-13 14:11:38 +03:00
|
|
|
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_SSE2_INLINE
|
2012-08-28 18:37:54 +03:00
|
|
|
#undef COMPILE_TEMPLATE_MMXEXT
|
|
|
|
#undef COMPILE_TEMPLATE_SSE2
|
|
|
|
#undef COMPILE_TEMPLATE_SSSE3
|
|
|
|
#define COMPILE_TEMPLATE_MMXEXT 0
|
|
|
|
#define COMPILE_TEMPLATE_SSE2 1
|
|
|
|
#define COMPILE_TEMPLATE_SSSE3 0
|
2012-08-13 14:11:38 +03:00
|
|
|
#undef RENAME
|
2013-12-30 19:06:45 +03:00
|
|
|
#undef RENAME_FDCT
|
|
|
|
#define RENAME(a) a ## _sse2
|
|
|
|
#define RENAME_FDCT(a) a ## _sse2
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "mpegvideoenc_template.c"
|
2012-08-28 15:53:33 +03:00
|
|
|
#endif /* HAVE_SSE2_INLINE */
|
2012-08-13 14:11:38 +03:00
|
|
|
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_SSSE3_INLINE
|
2012-08-28 18:37:54 +03:00
|
|
|
#undef COMPILE_TEMPLATE_MMXEXT
|
|
|
|
#undef COMPILE_TEMPLATE_SSE2
|
|
|
|
#undef COMPILE_TEMPLATE_SSSE3
|
|
|
|
#define COMPILE_TEMPLATE_MMXEXT 0
|
|
|
|
#define COMPILE_TEMPLATE_SSE2 1
|
|
|
|
#define COMPILE_TEMPLATE_SSSE3 1
|
2012-08-13 14:11:38 +03:00
|
|
|
#undef RENAME
|
2013-12-30 19:06:45 +03:00
|
|
|
#undef RENAME_FDCT
|
|
|
|
#define RENAME(a) a ## _ssse3
|
|
|
|
#define RENAME_FDCT(a) a ## _sse2
|
2012-08-13 14:11:38 +03:00
|
|
|
#include "mpegvideoenc_template.c"
|
2012-08-28 15:53:33 +03:00
|
|
|
#endif /* HAVE_SSSE3_INLINE */
|
2012-08-13 14:11:38 +03:00
|
|
|
|
2014-04-19 10:56:01 +03:00
|
|
|
#endif /* HAVE_6REGS */
|
|
|
|
|
2013-12-20 15:00:22 +03:00
|
|
|
#if HAVE_INLINE_ASM
|
2015-09-19 17:00:50 +02:00
|
|
|
#if HAVE_MMX_INLINE
|
2013-12-20 15:00:22 +03:00
|
|
|
static void denoise_dct_mmx(MpegEncContext *s, int16_t *block){
|
|
|
|
const int intra= s->mb_intra;
|
|
|
|
int *sum= s->dct_error_sum[intra];
|
|
|
|
uint16_t *offset= s->dct_offset[intra];
|
|
|
|
|
|
|
|
s->dct_count[intra]++;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
"pxor %%mm7, %%mm7 \n\t"
|
|
|
|
"1: \n\t"
|
|
|
|
"pxor %%mm0, %%mm0 \n\t"
|
|
|
|
"pxor %%mm1, %%mm1 \n\t"
|
|
|
|
"movq (%0), %%mm2 \n\t"
|
|
|
|
"movq 8(%0), %%mm3 \n\t"
|
|
|
|
"pcmpgtw %%mm2, %%mm0 \n\t"
|
|
|
|
"pcmpgtw %%mm3, %%mm1 \n\t"
|
|
|
|
"pxor %%mm0, %%mm2 \n\t"
|
|
|
|
"pxor %%mm1, %%mm3 \n\t"
|
|
|
|
"psubw %%mm0, %%mm2 \n\t"
|
|
|
|
"psubw %%mm1, %%mm3 \n\t"
|
|
|
|
"movq %%mm2, %%mm4 \n\t"
|
|
|
|
"movq %%mm3, %%mm5 \n\t"
|
|
|
|
"psubusw (%2), %%mm2 \n\t"
|
|
|
|
"psubusw 8(%2), %%mm3 \n\t"
|
|
|
|
"pxor %%mm0, %%mm2 \n\t"
|
|
|
|
"pxor %%mm1, %%mm3 \n\t"
|
|
|
|
"psubw %%mm0, %%mm2 \n\t"
|
|
|
|
"psubw %%mm1, %%mm3 \n\t"
|
|
|
|
"movq %%mm2, (%0) \n\t"
|
|
|
|
"movq %%mm3, 8(%0) \n\t"
|
|
|
|
"movq %%mm4, %%mm2 \n\t"
|
|
|
|
"movq %%mm5, %%mm3 \n\t"
|
|
|
|
"punpcklwd %%mm7, %%mm4 \n\t"
|
|
|
|
"punpckhwd %%mm7, %%mm2 \n\t"
|
|
|
|
"punpcklwd %%mm7, %%mm5 \n\t"
|
|
|
|
"punpckhwd %%mm7, %%mm3 \n\t"
|
|
|
|
"paddd (%1), %%mm4 \n\t"
|
|
|
|
"paddd 8(%1), %%mm2 \n\t"
|
|
|
|
"paddd 16(%1), %%mm5 \n\t"
|
|
|
|
"paddd 24(%1), %%mm3 \n\t"
|
|
|
|
"movq %%mm4, (%1) \n\t"
|
|
|
|
"movq %%mm2, 8(%1) \n\t"
|
|
|
|
"movq %%mm5, 16(%1) \n\t"
|
|
|
|
"movq %%mm3, 24(%1) \n\t"
|
|
|
|
"add $16, %0 \n\t"
|
|
|
|
"add $32, %1 \n\t"
|
|
|
|
"add $16, %2 \n\t"
|
|
|
|
"cmp %3, %0 \n\t"
|
|
|
|
" jb 1b \n\t"
|
|
|
|
: "+r" (block), "+r" (sum), "+r" (offset)
|
|
|
|
: "r"(block+64)
|
|
|
|
);
|
|
|
|
}
|
2015-09-19 17:00:50 +02:00
|
|
|
#endif /* HAVE_MMX_INLINE */
|
2013-12-20 15:00:22 +03:00
|
|
|
|
2015-09-19 17:00:50 +02:00
|
|
|
#if HAVE_SSE2_INLINE
|
2013-12-20 15:00:22 +03:00
|
|
|
static void denoise_dct_sse2(MpegEncContext *s, int16_t *block){
|
|
|
|
const int intra= s->mb_intra;
|
|
|
|
int *sum= s->dct_error_sum[intra];
|
|
|
|
uint16_t *offset= s->dct_offset[intra];
|
|
|
|
|
|
|
|
s->dct_count[intra]++;
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
"pxor %%xmm7, %%xmm7 \n\t"
|
|
|
|
"1: \n\t"
|
|
|
|
"pxor %%xmm0, %%xmm0 \n\t"
|
|
|
|
"pxor %%xmm1, %%xmm1 \n\t"
|
|
|
|
"movdqa (%0), %%xmm2 \n\t"
|
|
|
|
"movdqa 16(%0), %%xmm3 \n\t"
|
|
|
|
"pcmpgtw %%xmm2, %%xmm0 \n\t"
|
|
|
|
"pcmpgtw %%xmm3, %%xmm1 \n\t"
|
|
|
|
"pxor %%xmm0, %%xmm2 \n\t"
|
|
|
|
"pxor %%xmm1, %%xmm3 \n\t"
|
|
|
|
"psubw %%xmm0, %%xmm2 \n\t"
|
|
|
|
"psubw %%xmm1, %%xmm3 \n\t"
|
|
|
|
"movdqa %%xmm2, %%xmm4 \n\t"
|
|
|
|
"movdqa %%xmm3, %%xmm5 \n\t"
|
|
|
|
"psubusw (%2), %%xmm2 \n\t"
|
|
|
|
"psubusw 16(%2), %%xmm3 \n\t"
|
|
|
|
"pxor %%xmm0, %%xmm2 \n\t"
|
|
|
|
"pxor %%xmm1, %%xmm3 \n\t"
|
|
|
|
"psubw %%xmm0, %%xmm2 \n\t"
|
|
|
|
"psubw %%xmm1, %%xmm3 \n\t"
|
|
|
|
"movdqa %%xmm2, (%0) \n\t"
|
|
|
|
"movdqa %%xmm3, 16(%0) \n\t"
|
|
|
|
"movdqa %%xmm4, %%xmm6 \n\t"
|
|
|
|
"movdqa %%xmm5, %%xmm0 \n\t"
|
|
|
|
"punpcklwd %%xmm7, %%xmm4 \n\t"
|
|
|
|
"punpckhwd %%xmm7, %%xmm6 \n\t"
|
|
|
|
"punpcklwd %%xmm7, %%xmm5 \n\t"
|
|
|
|
"punpckhwd %%xmm7, %%xmm0 \n\t"
|
|
|
|
"paddd (%1), %%xmm4 \n\t"
|
|
|
|
"paddd 16(%1), %%xmm6 \n\t"
|
|
|
|
"paddd 32(%1), %%xmm5 \n\t"
|
|
|
|
"paddd 48(%1), %%xmm0 \n\t"
|
|
|
|
"movdqa %%xmm4, (%1) \n\t"
|
|
|
|
"movdqa %%xmm6, 16(%1) \n\t"
|
|
|
|
"movdqa %%xmm5, 32(%1) \n\t"
|
|
|
|
"movdqa %%xmm0, 48(%1) \n\t"
|
|
|
|
"add $32, %0 \n\t"
|
|
|
|
"add $64, %1 \n\t"
|
|
|
|
"add $32, %2 \n\t"
|
|
|
|
"cmp %3, %0 \n\t"
|
|
|
|
" jb 1b \n\t"
|
|
|
|
: "+r" (block), "+r" (sum), "+r" (offset)
|
|
|
|
: "r"(block+64)
|
|
|
|
XMM_CLOBBERS_ONLY("%xmm0", "%xmm1", "%xmm2", "%xmm3",
|
|
|
|
"%xmm4", "%xmm5", "%xmm6", "%xmm7")
|
|
|
|
);
|
|
|
|
}
|
2015-09-19 17:00:50 +02:00
|
|
|
#endif /* HAVE_SSE2_INLINE */
|
2013-12-20 15:00:22 +03:00
|
|
|
#endif /* HAVE_INLINE_ASM */
|
|
|
|
|
2013-02-06 15:27:24 +03:00
|
|
|
av_cold void ff_dct_encode_init_x86(MpegEncContext *s)
|
2012-08-13 14:11:38 +03:00
|
|
|
{
|
|
|
|
const int dct_algo = s->avctx->dct_algo;
|
|
|
|
|
|
|
|
if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_MMX_INLINE
|
2013-07-17 21:19:24 +03:00
|
|
|
int cpu_flags = av_get_cpu_flags();
|
2013-12-20 15:00:22 +03:00
|
|
|
if (INLINE_MMX(cpu_flags)) {
|
2014-04-19 10:56:01 +03:00
|
|
|
#if HAVE_6REGS
|
2013-12-30 19:06:45 +03:00
|
|
|
s->dct_quantize = dct_quantize_mmx;
|
2014-04-19 10:56:01 +03:00
|
|
|
#endif
|
2013-12-20 15:00:22 +03:00
|
|
|
s->denoise_dct = denoise_dct_mmx;
|
|
|
|
}
|
2012-08-29 12:14:17 +03:00
|
|
|
#endif
|
2014-04-19 10:56:01 +03:00
|
|
|
#if HAVE_6REGS && HAVE_MMXEXT_INLINE
|
2013-07-17 21:19:24 +03:00
|
|
|
if (INLINE_MMXEXT(cpu_flags))
|
2013-12-30 19:06:45 +03:00
|
|
|
s->dct_quantize = dct_quantize_mmxext;
|
2012-08-29 12:14:17 +03:00
|
|
|
#endif
|
2012-08-28 15:53:33 +03:00
|
|
|
#if HAVE_SSE2_INLINE
|
2013-12-20 15:00:22 +03:00
|
|
|
if (INLINE_SSE2(cpu_flags)) {
|
2014-04-19 10:56:01 +03:00
|
|
|
#if HAVE_6REGS
|
2013-12-30 19:06:45 +03:00
|
|
|
s->dct_quantize = dct_quantize_sse2;
|
2014-04-19 10:56:01 +03:00
|
|
|
#endif
|
2013-12-20 15:00:22 +03:00
|
|
|
s->denoise_dct = denoise_dct_sse2;
|
|
|
|
}
|
2012-08-29 12:14:17 +03:00
|
|
|
#endif
|
2014-04-19 10:56:01 +03:00
|
|
|
#if HAVE_6REGS && HAVE_SSSE3_INLINE
|
2013-07-17 21:19:24 +03:00
|
|
|
if (INLINE_SSSE3(cpu_flags))
|
2013-12-30 19:06:45 +03:00
|
|
|
s->dct_quantize = dct_quantize_ssse3;
|
2012-08-13 14:11:38 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|