* Also of inspiration:
* a page about fdct at http://www.geocities.com/ssavekar/dct.htm
* Skal's fdct at http://skal.planet-d.net/coding/dct.html
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * 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.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "common.h"
-#include "../dsputil.h"
+
+#include "libavutil/common.h"
+#include "libavcodec/dsputil.h"
#include "mmx.h"
#define ATTR_ALIGN(align) __attribute__ ((__aligned__ (align)))
#define RND_FRW_ROW (1 << (SHIFT_FRW_ROW-1))
//#define RND_FRW_COL (1 << (SHIFT_FRW_COL-1))
+#define X8(x) x,x,x,x,x,x,x,x
+
//concatenated table, for forward DCT transformation
-static const int16_t fdct_tg_all_16[] ATTR_ALIGN(8) = {
- 13036, 13036, 13036, 13036, // tg * (2<<16) + 0.5
- 27146, 27146, 27146, 27146, // tg * (2<<16) + 0.5
- -21746, -21746, -21746, -21746, // tg * (2<<16) + 0.5
+static const int16_t fdct_tg_all_16[24] ATTR_ALIGN(16) = {
+ X8(13036), // tg * (2<<16) + 0.5
+ X8(27146), // tg * (2<<16) + 0.5
+ X8(-21746) // tg * (2<<16) + 0.5
};
-static const int16_t ocos_4_16[4] ATTR_ALIGN(8) = {
- 23170, 23170, 23170, 23170, //cos * (2<<15) + 0.5
+static const int16_t ocos_4_16[8] ATTR_ALIGN(16) = {
+ X8(23170) //cos * (2<<15) + 0.5
};
-static const int64_t fdct_one_corr ATTR_ALIGN(8) = 0x0001000100010001LL;
+static const int16_t fdct_one_corr[8] ATTR_ALIGN(16) = { X8(1) };
static const int32_t fdct_r_row[2] ATTR_ALIGN(8) = {RND_FRW_ROW, RND_FRW_ROW };
-struct
+static struct
{
const int32_t fdct_r_row_sse2[4] ATTR_ALIGN(16);
} fdct_r_row_sse2 ATTR_ALIGN(16)=
29692, -12299, 26722, -31521,
};
-struct
+static struct
{
const int16_t tab_frw_01234567_sse2[256] ATTR_ALIGN(16);
} tab_frw_01234567_sse2 ATTR_ALIGN(16) =
TABLE_SSE2
}};
-
-static always_inline void fdct_col(const int16_t *in, int16_t *out, int offset)
-{
- movq_m2r(*(in + offset + 1 * 8), mm0);
- movq_m2r(*(in + offset + 6 * 8), mm1);
- movq_r2r(mm0, mm2);
- movq_m2r(*(in + offset + 2 * 8), mm3);
- paddsw_r2r(mm1, mm0);
- movq_m2r(*(in + offset + 5 * 8), mm4);
- psllw_i2r(SHIFT_FRW_COL, mm0);
- movq_m2r(*(in + offset + 0 * 8), mm5);
- paddsw_r2r(mm3, mm4);
- paddsw_m2r(*(in + offset + 7 * 8), mm5);
- psllw_i2r(SHIFT_FRW_COL, mm4);
- movq_r2r(mm0, mm6);
- psubsw_r2r(mm1, mm2);
- movq_m2r(*(fdct_tg_all_16 + 4), mm1);
- psubsw_r2r(mm4, mm0);
- movq_m2r(*(in + offset + 3 * 8), mm7);
- pmulhw_r2r(mm0, mm1);
- paddsw_m2r(*(in + offset + 4 * 8), mm7);
- psllw_i2r(SHIFT_FRW_COL, mm5);
- paddsw_r2r(mm4, mm6);
- psllw_i2r(SHIFT_FRW_COL, mm7);
- movq_r2r(mm5, mm4);
- psubsw_r2r(mm7, mm5);
- paddsw_r2r(mm5, mm1);
- paddsw_r2r(mm7, mm4);
- por_m2r(fdct_one_corr, mm1);
- psllw_i2r(SHIFT_FRW_COL + 1, mm2);
- pmulhw_m2r(*(fdct_tg_all_16 + 4), mm5);
- movq_r2r(mm4, mm7);
- psubsw_m2r(*(in + offset + 5 * 8), mm3);
- psubsw_r2r(mm6, mm4);
- movq_r2m(mm1, *(out + offset + 2 * 8));
- paddsw_r2r(mm6, mm7);
- movq_m2r(*(in + offset + 3 * 8), mm1);
- psllw_i2r(SHIFT_FRW_COL + 1, mm3);
- psubsw_m2r(*(in + offset + 4 * 8), mm1);
- movq_r2r(mm2, mm6);
- movq_r2m(mm4, *(out + offset + 4 * 8));
- paddsw_r2r(mm3, mm2);
- pmulhw_m2r(*ocos_4_16, mm2);
- psubsw_r2r(mm3, mm6);
- pmulhw_m2r(*ocos_4_16, mm6);
- psubsw_r2r(mm0, mm5);
- por_m2r(fdct_one_corr, mm5);
- psllw_i2r(SHIFT_FRW_COL, mm1);
- por_m2r(fdct_one_corr, mm2);
- movq_r2r(mm1, mm4);
- movq_m2r(*(in + offset + 0 * 8), mm3);
- paddsw_r2r(mm6, mm1);
- psubsw_m2r(*(in + offset + 7 * 8), mm3);
- psubsw_r2r(mm6, mm4);
- movq_m2r(*(fdct_tg_all_16 + 0), mm0);
- psllw_i2r(SHIFT_FRW_COL, mm3);
- movq_m2r(*(fdct_tg_all_16 + 8), mm6);
- pmulhw_r2r(mm1, mm0);
- movq_r2m(mm7, *(out + offset + 0 * 8));
- pmulhw_r2r(mm4, mm6);
- movq_r2m(mm5, *(out + offset + 6 * 8));
- movq_r2r(mm3, mm7);
- movq_m2r(*(fdct_tg_all_16 + 8), mm5);
- psubsw_r2r(mm2, mm7);
- paddsw_r2r(mm2, mm3);
- pmulhw_r2r(mm7, mm5);
- paddsw_r2r(mm3, mm0);
- paddsw_r2r(mm4, mm6);
- pmulhw_m2r(*(fdct_tg_all_16 + 0), mm3);
- por_m2r(fdct_one_corr, mm0);
- paddsw_r2r(mm7, mm5);
- psubsw_r2r(mm6, mm7);
- movq_r2m(mm0, *(out + offset + 1 * 8));
- paddsw_r2r(mm4, mm5);
- movq_r2m(mm7, *(out + offset + 3 * 8));
- psubsw_r2r(mm1, mm3);
- movq_r2m(mm5, *(out + offset + 5 * 8));
- movq_r2m(mm3, *(out + offset + 7 * 8));
+#define FDCT_COL(cpu, mm, mov)\
+static av_always_inline void fdct_col_##cpu(const int16_t *in, int16_t *out, int offset)\
+{\
+ mov##_m2r(*(in + offset + 1 * 8), mm##0);\
+ mov##_m2r(*(in + offset + 6 * 8), mm##1);\
+ mov##_r2r(mm##0, mm##2);\
+ mov##_m2r(*(in + offset + 2 * 8), mm##3);\
+ paddsw_r2r(mm##1, mm##0);\
+ mov##_m2r(*(in + offset + 5 * 8), mm##4);\
+ psllw_i2r(SHIFT_FRW_COL, mm##0);\
+ mov##_m2r(*(in + offset + 0 * 8), mm##5);\
+ paddsw_r2r(mm##3, mm##4);\
+ paddsw_m2r(*(in + offset + 7 * 8), mm##5);\
+ psllw_i2r(SHIFT_FRW_COL, mm##4);\
+ mov##_r2r(mm##0, mm##6);\
+ psubsw_r2r(mm##1, mm##2);\
+ mov##_m2r(*(fdct_tg_all_16 + 8), mm##1);\
+ psubsw_r2r(mm##4, mm##0);\
+ mov##_m2r(*(in + offset + 3 * 8), mm##7);\
+ pmulhw_r2r(mm##0, mm##1);\
+ paddsw_m2r(*(in + offset + 4 * 8), mm##7);\
+ psllw_i2r(SHIFT_FRW_COL, mm##5);\
+ paddsw_r2r(mm##4, mm##6);\
+ psllw_i2r(SHIFT_FRW_COL, mm##7);\
+ mov##_r2r(mm##5, mm##4);\
+ psubsw_r2r(mm##7, mm##5);\
+ paddsw_r2r(mm##5, mm##1);\
+ paddsw_r2r(mm##7, mm##4);\
+ por_m2r(*fdct_one_corr, mm##1);\
+ psllw_i2r(SHIFT_FRW_COL + 1, mm##2);\
+ pmulhw_m2r(*(fdct_tg_all_16 + 8), mm##5);\
+ mov##_r2r(mm##4, mm##7);\
+ psubsw_m2r(*(in + offset + 5 * 8), mm##3);\
+ psubsw_r2r(mm##6, mm##4);\
+ mov##_r2m(mm##1, *(out + offset + 2 * 8));\
+ paddsw_r2r(mm##6, mm##7);\
+ mov##_m2r(*(in + offset + 3 * 8), mm##1);\
+ psllw_i2r(SHIFT_FRW_COL + 1, mm##3);\
+ psubsw_m2r(*(in + offset + 4 * 8), mm##1);\
+ mov##_r2r(mm##2, mm##6);\
+ mov##_r2m(mm##4, *(out + offset + 4 * 8));\
+ paddsw_r2r(mm##3, mm##2);\
+ pmulhw_m2r(*ocos_4_16, mm##2);\
+ psubsw_r2r(mm##3, mm##6);\
+ pmulhw_m2r(*ocos_4_16, mm##6);\
+ psubsw_r2r(mm##0, mm##5);\
+ por_m2r(*fdct_one_corr, mm##5);\
+ psllw_i2r(SHIFT_FRW_COL, mm##1);\
+ por_m2r(*fdct_one_corr, mm##2);\
+ mov##_r2r(mm##1, mm##4);\
+ mov##_m2r(*(in + offset + 0 * 8), mm##3);\
+ paddsw_r2r(mm##6, mm##1);\
+ psubsw_m2r(*(in + offset + 7 * 8), mm##3);\
+ psubsw_r2r(mm##6, mm##4);\
+ mov##_m2r(*(fdct_tg_all_16 + 0), mm##0);\
+ psllw_i2r(SHIFT_FRW_COL, mm##3);\
+ mov##_m2r(*(fdct_tg_all_16 + 16), mm##6);\
+ pmulhw_r2r(mm##1, mm##0);\
+ mov##_r2m(mm##7, *(out + offset + 0 * 8));\
+ pmulhw_r2r(mm##4, mm##6);\
+ mov##_r2m(mm##5, *(out + offset + 6 * 8));\
+ mov##_r2r(mm##3, mm##7);\
+ mov##_m2r(*(fdct_tg_all_16 + 16), mm##5);\
+ psubsw_r2r(mm##2, mm##7);\
+ paddsw_r2r(mm##2, mm##3);\
+ pmulhw_r2r(mm##7, mm##5);\
+ paddsw_r2r(mm##3, mm##0);\
+ paddsw_r2r(mm##4, mm##6);\
+ pmulhw_m2r(*(fdct_tg_all_16 + 0), mm##3);\
+ por_m2r(*fdct_one_corr, mm##0);\
+ paddsw_r2r(mm##7, mm##5);\
+ psubsw_r2r(mm##6, mm##7);\
+ mov##_r2m(mm##0, *(out + offset + 1 * 8));\
+ paddsw_r2r(mm##4, mm##5);\
+ mov##_r2m(mm##7, *(out + offset + 3 * 8));\
+ psubsw_r2r(mm##1, mm##3);\
+ mov##_r2m(mm##5, *(out + offset + 5 * 8));\
+ mov##_r2m(mm##3, *(out + offset + 7 * 8));\
}
+FDCT_COL(mmx, mm, movq)
+FDCT_COL(sse2, xmm, movdqa)
-static always_inline void fdct_row_sse2(const int16_t *in, int16_t *out)
+static av_always_inline void fdct_row_sse2(const int16_t *in, int16_t *out)
{
asm volatile(
- ".macro FDCT_ROW_SSE2_H1 i t \n\t"
- "movq \\i(%0), %%xmm2 \n\t"
- "movq \\i+8(%0), %%xmm0 \n\t"
- "movdqa \\t+32(%1), %%xmm3 \n\t"
- "movdqa \\t+48(%1), %%xmm7 \n\t"
- "movdqa \\t(%1), %%xmm4 \n\t"
- "movdqa \\t+16(%1), %%xmm5 \n\t"
- ".endm \n\t"
- ".macro FDCT_ROW_SSE2_H2 i t \n\t"
- "movq \\i(%0), %%xmm2 \n\t"
- "movq \\i+8(%0), %%xmm0 \n\t"
- "movdqa \\t+32(%1), %%xmm3 \n\t"
- "movdqa \\t+48(%1), %%xmm7 \n\t"
- ".endm \n\t"
- ".macro FDCT_ROW_SSE2 i \n\t"
- "movq %%xmm2, %%xmm1 \n\t"
- "pshuflw $27, %%xmm0, %%xmm0 \n\t"
- "paddsw %%xmm0, %%xmm1 \n\t"
- "psubsw %%xmm0, %%xmm2 \n\t"
- "punpckldq %%xmm2, %%xmm1 \n\t"
- "pshufd $78, %%xmm1, %%xmm2 \n\t"
- "pmaddwd %%xmm2, %%xmm3 \n\t"
- "pmaddwd %%xmm1, %%xmm7 \n\t"
- "pmaddwd %%xmm5, %%xmm2 \n\t"
- "pmaddwd %%xmm4, %%xmm1 \n\t"
- "paddd %%xmm7, %%xmm3 \n\t"
- "paddd %%xmm2, %%xmm1 \n\t"
- "paddd %%xmm6, %%xmm3 \n\t"
- "paddd %%xmm6, %%xmm1 \n\t"
- "psrad %3, %%xmm3 \n\t"
- "psrad %3, %%xmm1 \n\t"
- "packssdw %%xmm3, %%xmm1 \n\t"
- "movdqa %%xmm1, \\i(%4) \n\t"
- ".endm \n\t"
+#define FDCT_ROW_SSE2_H1(i,t) \
+ "movq " #i "(%0), %%xmm2 \n\t" \
+ "movq " #i "+8(%0), %%xmm0 \n\t" \
+ "movdqa " #t "+32(%1), %%xmm3 \n\t" \
+ "movdqa " #t "+48(%1), %%xmm7 \n\t" \
+ "movdqa " #t "(%1), %%xmm4 \n\t" \
+ "movdqa " #t "+16(%1), %%xmm5 \n\t"
+
+#define FDCT_ROW_SSE2_H2(i,t) \
+ "movq " #i "(%0), %%xmm2 \n\t" \
+ "movq " #i "+8(%0), %%xmm0 \n\t" \
+ "movdqa " #t "+32(%1), %%xmm3 \n\t" \
+ "movdqa " #t "+48(%1), %%xmm7 \n\t"
+
+#define FDCT_ROW_SSE2(i) \
+ "movq %%xmm2, %%xmm1 \n\t" \
+ "pshuflw $27, %%xmm0, %%xmm0 \n\t" \
+ "paddsw %%xmm0, %%xmm1 \n\t" \
+ "psubsw %%xmm0, %%xmm2 \n\t" \
+ "punpckldq %%xmm2, %%xmm1 \n\t" \
+ "pshufd $78, %%xmm1, %%xmm2 \n\t" \
+ "pmaddwd %%xmm2, %%xmm3 \n\t" \
+ "pmaddwd %%xmm1, %%xmm7 \n\t" \
+ "pmaddwd %%xmm5, %%xmm2 \n\t" \
+ "pmaddwd %%xmm4, %%xmm1 \n\t" \
+ "paddd %%xmm7, %%xmm3 \n\t" \
+ "paddd %%xmm2, %%xmm1 \n\t" \
+ "paddd %%xmm6, %%xmm3 \n\t" \
+ "paddd %%xmm6, %%xmm1 \n\t" \
+ "psrad %3, %%xmm3 \n\t" \
+ "psrad %3, %%xmm1 \n\t" \
+ "packssdw %%xmm3, %%xmm1 \n\t" \
+ "movdqa %%xmm1, " #i "(%4) \n\t"
+
"movdqa (%2), %%xmm6 \n\t"
- "FDCT_ROW_SSE2_H1 0 0 \n\t"
- "FDCT_ROW_SSE2 0 \n\t"
- "FDCT_ROW_SSE2_H2 64 0 \n\t"
- "FDCT_ROW_SSE2 64 \n\t"
-
- "FDCT_ROW_SSE2_H1 16 64 \n\t"
- "FDCT_ROW_SSE2 16 \n\t"
- "FDCT_ROW_SSE2_H2 112 64 \n\t"
- "FDCT_ROW_SSE2 112 \n\t"
-
- "FDCT_ROW_SSE2_H1 32 128 \n\t"
- "FDCT_ROW_SSE2 32 \n\t"
- "FDCT_ROW_SSE2_H2 96 128 \n\t"
- "FDCT_ROW_SSE2 96 \n\t"
-
- "FDCT_ROW_SSE2_H1 48 192 \n\t"
- "FDCT_ROW_SSE2 48 \n\t"
- "FDCT_ROW_SSE2_H2 80 192 \n\t"
- "FDCT_ROW_SSE2 80 \n\t"
+ FDCT_ROW_SSE2_H1(0,0)
+ FDCT_ROW_SSE2(0)
+ FDCT_ROW_SSE2_H2(64,0)
+ FDCT_ROW_SSE2(64)
+
+ FDCT_ROW_SSE2_H1(16,64)
+ FDCT_ROW_SSE2(16)
+ FDCT_ROW_SSE2_H2(112,64)
+ FDCT_ROW_SSE2(112)
+
+ FDCT_ROW_SSE2_H1(32,128)
+ FDCT_ROW_SSE2(32)
+ FDCT_ROW_SSE2_H2(96,128)
+ FDCT_ROW_SSE2(96)
+
+ FDCT_ROW_SSE2_H1(48,192)
+ FDCT_ROW_SSE2(48)
+ FDCT_ROW_SSE2_H2(80,192)
+ FDCT_ROW_SSE2(80)
:
: "r" (in), "r" (tab_frw_01234567_sse2.tab_frw_01234567_sse2), "r" (fdct_r_row_sse2.fdct_r_row_sse2), "i" (SHIFT_FRW_ROW), "r" (out)
);
}
-static always_inline void fdct_row_mmx2(const int16_t *in, int16_t *out, const int16_t *table)
+static av_always_inline void fdct_row_mmx2(const int16_t *in, int16_t *out, const int16_t *table)
{
pshufw_m2r(*(in + 4), mm5, 0x1B);
movq_m2r(*(in + 0), mm0);
movq_r2m(mm7, *(out + 4));
}
-static always_inline void fdct_row_mmx(const int16_t *in, int16_t *out, const int16_t *table)
+static av_always_inline void fdct_row_mmx(const int16_t *in, int16_t *out, const int16_t *table)
{
-//FIXME reorder (i dont have a old mmx only cpu here to benchmark ...)
+//FIXME reorder (I do not have an old MMX-only CPU here to benchmark ...)
movd_m2r(*(in + 6), mm1);
punpcklwd_m2r(*(in + 4), mm1);
movq_r2r(mm1, mm2);
void ff_fdct_mmx(int16_t *block)
{
int64_t align_tmp[16] ATTR_ALIGN(8);
- int16_t * const block_tmp= (int16_t*)align_tmp;
- int16_t *block1, *out;
- const int16_t *table;
+ int16_t * block1= (int16_t*)align_tmp;
+ const int16_t *table= tab_frw_01234567;
int i;
- block1 = block_tmp;
- fdct_col(block, block1, 0);
- fdct_col(block, block1, 4);
+ fdct_col_mmx(block, block1, 0);
+ fdct_col_mmx(block, block1, 4);
- block1 = block_tmp;
- table = tab_frw_01234567;
- out = block;
for(i=8;i>0;i--) {
- fdct_row_mmx(block1, out, table);
+ fdct_row_mmx(block1, block, table);
block1 += 8;
table += 32;
- out += 8;
+ block += 8;
}
}
void ff_fdct_mmx2(int16_t *block)
{
int64_t align_tmp[16] ATTR_ALIGN(8);
- int16_t * const block_tmp= (int16_t*)align_tmp;
- int16_t *block1, *out;
- const int16_t *table;
+ int16_t *block1= (int16_t*)align_tmp;
+ const int16_t *table= tab_frw_01234567;
int i;
- block1 = block_tmp;
- fdct_col(block, block1, 0);
- fdct_col(block, block1, 4);
+ fdct_col_mmx(block, block1, 0);
+ fdct_col_mmx(block, block1, 4);
- block1 = block_tmp;
- table = tab_frw_01234567;
- out = block;
for(i=8;i>0;i--) {
- fdct_row_mmx2(block1, out, table);
+ fdct_row_mmx2(block1, block, table);
block1 += 8;
table += 32;
- out += 8;
+ block += 8;
}
}
void ff_fdct_sse2(int16_t *block)
{
- int64_t align_tmp[16] ATTR_ALIGN(8);
- int16_t * const block_tmp= (int16_t*)align_tmp;
- int16_t *block1;
-
- block1 = block_tmp;
- fdct_col(block, block1, 0);
- fdct_col(block, block1, 4);
+ int64_t align_tmp[16] ATTR_ALIGN(16);
+ int16_t * const block1= (int16_t*)align_tmp;
+ fdct_col_sse2(block, block1, 0);
fdct_row_sse2(block1, block);
}