--- xvid.c 2003/01/03 16:25:14 1.33.2.21 +++ xvid.c 2003/06/09 17:07:10 1.46 @@ -3,15 +3,6 @@ * XVID MPEG-4 VIDEO CODEC * - Native API implementation - * - * This program is an implementation of a part of one or more MPEG-4 - * Video tools as specified in ISO/IEC 14496-2 standard. Those intending - * to use this software module in hardware or software products are - * advised that its use may infringe existing patents or copyrights, and - * any such use would be at such party's own risk. The original - * developer of this software module and his/her company, and subsequent - * editors and their companies, will have no liability for use of this - * software or modifications or derivatives thereof. - * * This program is free software ; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation ; either version 2 of the License, or @@ -26,18 +17,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - ****************************************************************************/ - -/***************************************************************************** - * - * History - * - * - 23.06.2002 added XVID_CPU_CHKONLY - * - 17.03.2002 Added interpolate8x8_halfpel_hv_xmm - * - 22.12.2001 API change: added xvid_init() - Isibaar - * - 16.12.2001 inital version; (c)2001 peter ross - * - * $Id: xvid.c,v 1.33.2.21 2003/01/03 16:25:14 suxen_drol Exp $ + * $Id: xvid.c,v 1.46 2003/06/09 17:07:10 Isibaar Exp $ * ****************************************************************************/ @@ -65,40 +45,38 @@ #include "utils/timer.h" #include "bitstream/mbcoding.h" -#if defined(ARCH_X86) && defined(EXPERIMENTAL_SSE2_CODE) +#if defined(ARCH_IS_IA32) -#ifdef WIN32 -#include +#if defined(_MSC_VER) +# include #else -#include -#include -#endif - +# include +# include -#ifndef WIN32 + static jmp_buf mark; -static jmp_buf mark; - -static void -sigill_handler(int signal) -{ - longjmp(mark, 1); -} + static void + sigill_handler(int signal) + { + longjmp(mark, 1); + } #endif /* -calls the funcptr, and returns whether SIGILL (illegal instruction) was signalled -return values: --1 : could not determine -0 : SIGILL was *not* signalled -1 : SIGILL was signalled -*/ + * Calls the funcptr, and returns whether SIGILL (illegal instruction) was + * signalled + * + * Return values: + * -1 : could not determine + * 0 : SIGILL was *not* signalled + * 1 : SIGILL was signalled + */ int sigill_check(void (*func)()) { -#ifdef WIN32 +#if defined(_MSC_VER) _try { func(); } @@ -132,6 +110,33 @@ } #endif + +/* detect cpu flags */ +static unsigned int +detect_cpu_flags() +{ + /* enable native assembly optimizations by default */ + unsigned int cpu_flags = XVID_CPU_ASM; + +#if defined(ARCH_IS_IA32) + cpu_flags |= check_cpu_features(); + if ((cpu_flags & XVID_CPU_SSE) && sigill_check(sse_os_trigger)) + cpu_flags &= ~XVID_CPU_SSE; + + if ((cpu_flags & XVID_CPU_SSE2) && sigill_check(sse2_os_trigger)) + cpu_flags &= ~XVID_CPU_SSE2; +#endif + +#if defined(ARCH_IS_PPC) +#if defined(ARCH_IS_PPC_ALTIVEC) + cpu_flags |= XVID_CPU_ALTIVEC; +#endif +#endif + + return cpu_flags; +} + + /***************************************************************************** * XviD Init Entry point * @@ -165,15 +170,7 @@ } else { - cpu_flags = check_cpu_features(); - -#if defined(ARCH_X86) && defined(EXPERIMENTAL_SSE2_CODE) - if ((cpu_flags & XVID_CPU_SSE) && sigill_check(sse_os_trigger)) - cpu_flags &= ~XVID_CPU_SSE; - - if ((cpu_flags & XVID_CPU_SSE2) && sigill_check(sse2_os_trigger)) - cpu_flags &= ~XVID_CPU_SSE2; -#endif + cpu_flags = detect_cpu_flags(); } if ((init_param->cpu_flags & XVID_CPU_CHKONLY)) @@ -214,6 +211,7 @@ transfer_8to16copy = transfer_8to16copy_c; transfer_16to8copy = transfer_16to8copy_c; transfer_8to16sub = transfer_8to16sub_c; + transfer_8to16subro = transfer_8to16subro_c; transfer_8to16sub2 = transfer_8to16sub2_c; transfer_16to8add = transfer_16to8add_c; transfer8x8_copy = transfer8x8_copy_c; @@ -241,16 +239,10 @@ interpolate8x8_avg4 = interpolate8x8_avg4_c; /* reduced resoltuion */ - copy_upsampled_8x8_16to8 = xvid_Copy_Upsampled_8x8_16To8_C; add_upsampled_8x8_16to8 = xvid_Add_Upsampled_8x8_16To8_C; -#ifdef ARCH_X86 - vfilter_31 = xvid_VFilter_31_x86; - hfilter_31 = xvid_HFilter_31_x86; -#else vfilter_31 = xvid_VFilter_31_C; hfilter_31 = xvid_HFilter_31_C; -#endif filter_18x18_to_8x8 = xvid_Filter_18x18_To_8x8_C; filter_diff_18x18_to_8x8 = xvid_Filter_Diff_18x18_To_8x8_C; @@ -306,9 +298,15 @@ dev16 = dev16_c; sad16v = sad16v_c; -// Halfpel8_Refine = Halfpel8_Refine_c; +/* Halfpel8_Refine = Halfpel8_Refine_c; */ + +#if defined(ARCH_IS_IA32) -#ifdef ARCH_X86 + if ((cpu_flags & XVID_CPU_ASM)) + { + vfilter_31 = xvid_VFilter_31_x86; + hfilter_31 = xvid_HFilter_31_x86; + } if ((cpu_flags & XVID_CPU_MMX) || (cpu_flags & XVID_CPU_MMXEXT) || (cpu_flags & XVID_CPU_3DNOW) || (cpu_flags & XVID_CPU_3DNOWEXT) || @@ -318,11 +316,11 @@ emms = emms_mmx; } - if ((cpu_flags & XVID_CPU_MMX) > 0) { + if ((cpu_flags & XVID_CPU_MMX)) { /* Forward and Inverse Discrete Cosine Transformation functions */ fdct = fdct_mmx; - idct = idct_mmx; + idct = simple_idct_mmx; /* use simple idct by default */ /* Quantization related functions */ quant_intra = quant_intra_mmx; @@ -339,6 +337,7 @@ transfer_8to16copy = transfer_8to16copy_mmx; transfer_16to8copy = transfer_16to8copy_mmx; transfer_8to16sub = transfer_8to16sub_mmx; + transfer_8to16subro = transfer_8to16subro_mmx; transfer_8to16sub2 = transfer_8to16sub2_mmx; transfer_16to8add = transfer_16to8add_mmx; transfer8x8_copy = transfer8x8_copy_mmx; @@ -388,11 +387,12 @@ sad8bi = sad8bi_mmx; dev16 = dev16_mmx; sad16v = sad16v_mmx; - } /* these 3dnow functions are faster than mmx, but slower than xmm. */ - if ((cpu_flags & XVID_CPU_3DNOW) > 0) { + if ((cpu_flags & XVID_CPU_3DNOW)) { + + emms = emms_3dn; /* ME functions */ sad16bi = sad16bi_3dn; @@ -403,10 +403,10 @@ } - if ((cpu_flags & XVID_CPU_MMXEXT) > 0) { + if ((cpu_flags & XVID_CPU_MMXEXT)) { /* Inverse DCT */ - idct = idct_xmm; + /* idct = idct_xmm; Don't use Walken idct anymore! */ /* Interpolation */ interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_xmm; @@ -441,7 +441,7 @@ sad16v = sad16v_xmm; } - if ((cpu_flags & XVID_CPU_3DNOW) > 0) { + if ((cpu_flags & XVID_CPU_3DNOW)) { /* Interpolation */ interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_3dn; @@ -449,15 +449,16 @@ interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dn; } - if ((cpu_flags & XVID_CPU_3DNOWEXT) > 0) { + if ((cpu_flags & XVID_CPU_3DNOWEXT)) { /* Inverse DCT */ - idct = idct_3dne; + /* idct = idct_3dne; Don't use Walken idct anymore */ /* Buffer transfer */ transfer_8to16copy = transfer_8to16copy_3dne; transfer_16to8copy = transfer_16to8copy_3dne; transfer_8to16sub = transfer_8to16sub_3dne; + transfer_8to16subro = transfer_8to16subro_3dne; transfer_8to16sub2 = transfer_8to16sub2_3dne; transfer_16to8add = transfer_16to8add_3dne; transfer8x8_copy = transfer8x8_copy_3dne; @@ -485,8 +486,7 @@ } - if ((cpu_flags & XVID_CPU_SSE2) > 0) { -#ifdef EXPERIMENTAL_SSE2_CODE + if ((cpu_flags & XVID_CPU_SSE2)) { calc_cbp = calc_cbp_sse2; @@ -496,23 +496,22 @@ quant_inter = quant_inter_sse2; dequant_inter = dequant_inter_sse2; - /* ME */ +#if defined(EXPERIMENTAL_SSE2_CODE) + /* ME; slower than xmm */ sad16 = sad16_sse2; dev16 = dev16_sse2; - - /* Forward and Inverse DCT */ - idct = idct_sse2; - fdct = fdct_sse2; #endif + /* Forward and Inverse DCT */ + /* idct = idct_sse2; + /* fdct = fdct_sse2; Both are none to be unprecise - better deactivate for now */ } - #endif -#ifdef ARCH_IA64 - if ((cpu_flags & XVID_CPU_IA64) > 0) { //use assembler routines? +#if defined(ARCH_IS_IA64) + if ((cpu_flags & XVID_CPU_ASM)) { /* use assembler routines? */ idct_ia64_init(); fdct = fdct_ia64; - idct = idct_ia64; //not yet working, crashes + idct = idct_ia64; /*not yet working, crashes */ interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_ia64; interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_ia64; interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_ia64; @@ -520,7 +519,7 @@ sad16bi = sad16bi_ia64; sad8 = sad8_ia64; dev16 = dev16_ia64; -// Halfpel8_Refine = Halfpel8_Refine_ia64; +/* Halfpel8_Refine = Halfpel8_Refine_ia64; */ quant_intra = quant_intra_ia64; dequant_intra = dequant_intra_ia64; quant_inter = quant_inter_ia64; @@ -531,22 +530,26 @@ transfer_8to16sub2 = transfer_8to16sub2_ia64; transfer_16to8add = transfer_16to8add_ia64; transfer8x8_copy = transfer8x8_copy_ia64; - DEBUG("Using IA-64 assembler routines.\n"); + DPRINTF(DPRINTF_DEBUG, "Using IA-64 assembler routines."); } #endif -#ifdef ARCH_PPC -#ifdef ARCH_PPC_ALTIVEC - calc_cbp = calc_cbp_altivec; - fdct = fdct_altivec; - idct = idct_altivec; - sadInit = sadInit_altivec; - sad16 = sad16_altivec; - sad8 = sad8_altivec; - dev16 = dev16_altivec; -#else - calc_cbp = calc_cbp_ppc; -#endif +#if defined(ARCH_IS_PPC) + if ((cpu_flags & XVID_CPU_ASM)) + { + calc_cbp = calc_cbp_ppc; + } + + if ((cpu_flags & XVID_CPU_ALTIVEC)) + { + calc_cbp = calc_cbp_altivec; + fdct = fdct_altivec; + idct = idct_altivec; + sadInit = sadInit_altivec; + sad16 = sad16_altivec; + sad8 = sad8_altivec; + dev16 = dev16_altivec; + } #endif return XVID_ERR_OK; @@ -557,7 +560,11 @@ static int xvid_init_convert(XVID_INIT_CONVERTINFO* convert) { - // const int flip1 = (convert->input.colorspace & XVID_CSP_VFLIP) ^ (convert->output.colorspace & XVID_CSP_VFLIP); +/* + const int flip1 = + (convert->input.colorspace & XVID_CSP_VFLIP) ^ + (convert->output.colorspace & XVID_CSP_VFLIP); +*/ const int width = convert->width; const int height = convert->height; const int width2 = convert->width/2; @@ -643,7 +650,7 @@ #define TEST_FDCT (TEST_FORWARD) #define TEST_IDCT (0) -int test_transform(void * funcA, void * funcB, const char * nameB, +static int test_transform(void * funcA, void * funcB, const char * nameB, int test, int flags) { int i; @@ -723,14 +730,14 @@ #define TEST_DEQUANT_INTRA (TEST_INTRA) #define TEST_DEQUANT_INTER (0) -int test_quant(void * funcA, void * funcB, const char * nameB, +static int test_quant(void * funcA, void * funcB, const char * nameB, int test, int flags) { int q,i; int64_t timeSTART; int64_t timeA = 0; int64_t timeB = 0; - int retA, retB; + int retA = 0, retB = 0; DECLARE_ALIGNED_MATRIX(arrayX, 1, 64, int16_t, CACHE_LINE); DECLARE_ALIGNED_MATRIX(arrayA, 1, 64, int16_t, CACHE_LINE); DECLARE_ALIGNED_MATRIX(arrayB, 1, 64, int16_t, CACHE_LINE); @@ -804,26 +811,37 @@ int xvid_init_test(int flags) { +#if defined(ARCH_IS_IA32) int cpu_flags; +#endif - srand(time(0)); + printf("XviD tests\n\n"); - printf("xvid_init_test\n"); +#if defined(ARCH_IS_IA32) + cpu_flags = detect_cpu_flags(); +#endif -#if defined(ARCH_X86) - cpu_flags = check_cpu_features(); idct_int32_init(); - emms_mmx(); + emms(); + + srand(time(0)); + /* fDCT test */ printf("--- fdct ---\n"); test_transform(fdct_int32, fdct_int32, "c", TEST_FDCT, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_transform(fdct_int32, fdct_mmx, "mmx", TEST_FDCT, flags); if (cpu_flags & XVID_CPU_SSE2) test_transform(fdct_int32, fdct_sse2, "sse2", TEST_FDCT, flags); +#endif + /* iDCT test */ printf("\n--- idct ---\n"); test_transform(idct_int32, idct_int32, "c", TEST_IDCT, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_transform(idct_int32, idct_mmx, "mmx", TEST_IDCT, flags); if (cpu_flags & XVID_CPU_MMXEXT) @@ -832,27 +850,39 @@ test_transform(idct_int32, idct_3dne, "3dne", TEST_IDCT, flags); if (cpu_flags & XVID_CPU_SSE2) test_transform(idct_int32, idct_sse2, "sse2", TEST_IDCT, flags); +#endif + /* Intra quantization test */ printf("\n--- quant intra ---\n"); test_quant(quant_intra_c, quant_intra_c, "c", TEST_QUANT_INTRA, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(quant_intra_c, quant_intra_mmx, "mmx", TEST_QUANT_INTRA, flags); if (cpu_flags & XVID_CPU_3DNOWEXT) test_quant(quant_intra_c, quant_intra_3dne, "3dne", TEST_QUANT_INTRA, flags); if (cpu_flags & XVID_CPU_SSE2) test_quant(quant_intra_c, quant_intra_sse2, "sse2", TEST_QUANT_INTRA, flags); +#endif + /* Inter quantization test */ printf("\n--- quant inter ---\n"); test_quant(quant_inter_c, quant_inter_c, "c", TEST_QUANT_INTER, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(quant_inter_c, quant_inter_mmx, "mmx", TEST_QUANT_INTER, flags); if (cpu_flags & XVID_CPU_3DNOWEXT) test_quant(quant_inter_c, quant_inter_3dne, "3dne", TEST_QUANT_INTER, flags); if (cpu_flags & XVID_CPU_SSE2) test_quant(quant_inter_c, quant_inter_sse2, "sse2", TEST_QUANT_INTER, flags); +#endif + /* Intra dequantization test */ printf("\n--- dequant intra ---\n"); test_quant(dequant_intra_c, dequant_intra_c, "c", TEST_DEQUANT_INTRA, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(dequant_intra_c, dequant_intra_mmx, "mmx", TEST_DEQUANT_INTRA, flags); if (cpu_flags & XVID_CPU_MMXEXT) @@ -861,9 +891,13 @@ test_quant(dequant_intra_c, dequant_intra_3dne, "3dne", TEST_DEQUANT_INTRA, flags); if (cpu_flags & XVID_CPU_SSE2) test_quant(dequant_intra_c, dequant_intra_sse2, "sse2", TEST_DEQUANT_INTRA, flags); +#endif + /* Inter dequantization test */ printf("\n--- dequant inter ---\n"); test_quant(dequant_inter_c, dequant_inter_c, "c", TEST_DEQUANT_INTER, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(dequant_inter_c, dequant_inter_mmx, "mmx", TEST_DEQUANT_INTER, flags); if (cpu_flags & XVID_CPU_MMXEXT) @@ -872,39 +906,54 @@ test_quant(dequant_inter_c, dequant_inter_3dne, "3dne", TEST_DEQUANT_INTER, flags); if (cpu_flags & XVID_CPU_SSE2) test_quant(dequant_inter_c, dequant_inter_sse2, "sse2", TEST_DEQUANT_INTER, flags); +#endif - printf("\n--- quant4_intra ---\n"); + /* Intra quantization test */ + printf("\n--- quant4 intra ---\n"); test_quant(quant4_intra_c, quant4_intra_c, "c", TEST_QUANT_INTRA, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(quant4_intra_c, quant4_intra_mmx, "mmx", TEST_QUANT_INTRA, flags); if (cpu_flags & XVID_CPU_MMXEXT) test_quant(quant4_intra_c, quant4_intra_xmm, "xmm", TEST_QUANT_INTRA, flags); +#endif - printf("\n--- quant4_inter ---\n"); + /* Inter quantization test */ + printf("\n--- quant4 inter ---\n"); test_quant(quant4_inter_c, quant4_inter_c, "c", TEST_QUANT_INTER, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(quant4_inter_c, quant4_inter_mmx, "mmx", TEST_QUANT_INTER, flags); if (cpu_flags & XVID_CPU_MMXEXT) test_quant(quant4_inter_c, quant4_inter_xmm, "xmm", TEST_QUANT_INTER, flags); +#endif - printf("\n--- dequant4_intra ---\n"); + /* Intra dequantization test */ + printf("\n--- dequant4 intra ---\n"); test_quant(dequant4_intra_c, dequant4_intra_c, "c", TEST_DEQUANT_INTRA, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(dequant4_intra_c, dequant4_intra_mmx, "mmx", TEST_DEQUANT_INTRA, flags); if (cpu_flags & XVID_CPU_3DNOWEXT) test_quant(dequant4_intra_c, dequant4_intra_3dne, "3dne", TEST_DEQUANT_INTRA, flags); +#endif - printf("\n--- dequant4_inter ---\n"); + /* Inter dequantization test */ + printf("\n--- dequant4 inter ---\n"); test_quant(dequant4_inter_c, dequant4_inter_c, "c", TEST_DEQUANT_INTER, flags); + +#if defined(ARCH_IS_IA32) if (cpu_flags & XVID_CPU_MMX) test_quant(dequant4_inter_c, dequant4_inter_mmx, "mmx", TEST_DEQUANT_INTER, flags); if (cpu_flags & XVID_CPU_3DNOWEXT) test_quant(dequant4_inter_c, dequant4_inter_3dne, "3dne", TEST_DEQUANT_INTER, flags); - - emms_mmx(); - #endif + emms(); + return XVID_ERR_OK; } @@ -924,8 +973,10 @@ return xvid_init_convert((XVID_INIT_CONVERTINFO*)param1); case XVID_INIT_TEST : - return xvid_init_test((int)param1); - + { + ptr_t flags = (ptr_t)param1; + return xvid_init_test((int)flags); + } default : return XVID_ERR_FAIL; } @@ -983,11 +1034,13 @@ case XVID_ENC_ENCODE: if (((Encoder *) handle)->mbParam.max_bframes >= 0) - return encoder_encode_bframes((Encoder *) handle, (XVID_ENC_FRAME *) param1, - (XVID_ENC_STATS *) param2); + return encoder_encode_bframes((Encoder *) handle, + (XVID_ENC_FRAME *) param1, + (XVID_ENC_STATS *) param2); else - return encoder_encode((Encoder *) handle, (XVID_ENC_FRAME *) param1, - (XVID_ENC_STATS *) param2); + return encoder_encode((Encoder *) handle, + (XVID_ENC_FRAME *) param1, + (XVID_ENC_STATS *) param2); case XVID_ENC_CREATE: return encoder_create((XVID_ENC_PARAM *) param1);