--- xvid.c 2003/02/16 05:11:39 1.42 +++ xvid.c 2003/07/14 12:40:16 1.45.2.10 @@ -3,14 +3,7 @@ * 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. + * Copyright(C) 2001-2003 Peter Ross * * 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 @@ -26,18 +19,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.42 2003/02/16 05:11:39 suxen_drol Exp $ + * $Id: xvid.c,v 1.45.2.10 2003/07/14 12:40:16 syskin Exp $ * ****************************************************************************/ @@ -65,6 +47,10 @@ #include "utils/timer.h" #include "bitstream/mbcoding.h" +#if defined(_DEBUG) +unsigned int xvid_debug = 0; /* xvid debug mask */ +#endif + #if defined(ARCH_IS_IA32) #if defined(_MSC_VER) @@ -84,12 +70,14 @@ /* -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)()) @@ -171,34 +159,14 @@ static -int xvid_init_init(XVID_INIT_PARAM * init_param) +int xvid_gbl_init(xvid_gbl_init_t * init) { - int cpu_flags; - - /* Inform the client the API version */ - init_param->api_version = API_VERSION; - - /* Inform the client the core build - unused because we're still alpha */ - init_param->core_build = 1000; - - /* Do we have to force CPU features ? */ - if ((init_param->cpu_flags & XVID_CPU_FORCE)) { + unsigned int cpu_flags; - cpu_flags = init_param->cpu_flags; - - } else { - - cpu_flags = detect_cpu_flags(); - } - - if ((init_param->cpu_flags & XVID_CPU_CHKONLY)) - { - init_param->cpu_flags = cpu_flags; - return XVID_ERR_OK; - } - - init_param->cpu_flags = cpu_flags; + if (XVID_MAJOR(init->version) != 1) /* v1.x.x */ + return XVID_ERR_VERSION; + cpu_flags = (init->cpu_flags & XVID_CPU_FORCE) ? init->cpu_flags : detect_cpu_flags(); /* Initialize the function pointers */ idct_int32_init(); @@ -316,12 +284,11 @@ dev16 = dev16_c; sad16v = sad16v_c; -// Halfpel8_Refine = Halfpel8_Refine_c; +/* Halfpel8_Refine = Halfpel8_Refine_c; */ #if defined(ARCH_IS_IA32) - if ((cpu_flags & XVID_CPU_ASM)) - { + if ((cpu_flags & XVID_CPU_ASM)) { vfilter_31 = xvid_VFilter_31_x86; hfilter_31 = xvid_HFilter_31_x86; } @@ -436,7 +403,7 @@ add_upsampled_8x8_16to8 = xvid_Add_Upsampled_8x8_16To8_xmm; /* Quantization */ - quant4_intra = quant4_intra_xmm; +// quant4_intra = quant4_intra_xmm; quant4_inter = quant4_inter_xmm; dequant_intra = dequant_intra_xmm; @@ -484,7 +451,7 @@ /* Quantization */ dequant4_intra = dequant4_intra_3dne; dequant4_inter = dequant4_inter_3dne; - quant_intra = quant_intra_3dne; +// quant_intra = quant_intra_3dne; quant_inter = quant_inter_3dne; dequant_intra = dequant_intra_3dne; dequant_inter = dequant_inter_3dne; @@ -520,16 +487,18 @@ dev16 = dev16_sse2; #endif /* Forward and Inverse DCT */ +#if 0 /* Both function are known to be unprecise, better keep them deactivated */ idct = idct_sse2; fdct = fdct_sse2; +#endif } #endif #if defined(ARCH_IS_IA64) - if ((cpu_flags & XVID_CPU_ASM)) { //use assembler routines? + 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; @@ -537,7 +506,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; @@ -548,7 +517,6 @@ transfer_8to16sub2 = transfer_8to16sub2_ia64; transfer_16to8add = transfer_16to8add_ia64; transfer8x8_copy = transfer8x8_copy_ia64; - DEBUG("Using IA-64 assembler routines.\n"); } #endif @@ -570,30 +538,63 @@ } #endif - return XVID_ERR_OK; +#if defined(_DEBUG) + xvid_debug = init->debug; +#endif + + return 0; } +static int +xvid_gbl_info(xvid_gbl_info_t * info) +{ + if (XVID_MAJOR(info->version) != 1) /* v1.x.x */ + return XVID_ERR_VERSION; + + info->actual_version = XVID_VERSION; + info->build = "dev-api-4"; + info->cpu_flags = detect_cpu_flags(); + +#if defined(_SMP) && defined(WIN32) + info->num_threads = pthread_num_processors_np();; +#else + info->num_threads = 0; +#endif + + return 0; +} + static int -xvid_init_convert(XVID_INIT_CONVERTINFO* convert) +xvid_gbl_convert(xvid_gbl_convert_t* convert) { - // 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; - const int height2 = convert->height/2; + int width; + int height; + int width2; + int height2; IMAGE img; - switch (convert->input.colorspace & ~XVID_CSP_VFLIP) + if (XVID_MAJOR(convert->version) != 1) /* v1.x.x */ + return XVID_ERR_VERSION; + +#if 0 + const int flip1 = (convert->input.colorspace & XVID_CSP_VFLIP) ^ (convert->output.colorspace & XVID_CSP_VFLIP); +#endif + width = convert->width; + height = convert->height; + width2 = convert->width/2; + height2 = convert->height/2; + + switch (convert->input.csp & ~XVID_CSP_VFLIP) { case XVID_CSP_YV12 : - img.y = convert->input.y; - img.v = (uint8_t*)convert->input.y + width*height; - img.u = (uint8_t*)convert->input.y + width*height + width2*height2; + img.y = convert->input.plane[0]; + img.v = (uint8_t*)convert->input.plane[0] + convert->input.stride[0]*height; + img.u = (uint8_t*)convert->input.plane[0] + convert->input.stride[0]*height + (convert->input.stride[0]/2)*height2; image_output(&img, width, height, width, - convert->output.y, convert->output.y_stride, - convert->output.colorspace, convert->interlacing); + (uint8_t**)convert->output.plane, convert->output.stride, + convert->output.csp, convert->interlacing); break; default : @@ -602,7 +603,7 @@ emms(); - return XVID_ERR_OK; + return 0; } @@ -651,7 +652,7 @@ { int i, diff = 0; for (i = 0; i < size; i++) - diff += ABS(blockA[i]-blockB[i]); + diff += abs(blockA[i]-blockB[i]); return diff; } @@ -751,7 +752,7 @@ 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); @@ -825,26 +826,37 @@ int xvid_init_test(int flags) { +#if defined(ARCH_IS_IA32) int cpu_flags; +#endif - srand(time(0)); - - printf("xvid_init_test\n"); + printf("XviD tests\n\n"); -#if defined(ARCH_IS_IA32) +#if defined(ARCH_IS_IA32) cpu_flags = detect_cpu_flags(); +#endif + 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) @@ -853,27 +865,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) @@ -882,9 +906,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) @@ -893,60 +921,91 @@ 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 - return XVID_ERR_OK; + emms(); + + return 0; } +/***************************************************************************** + * XviD Global Entry point + * + * Well this function initialize all internal function pointers according + * to the CPU features forced by the library client or autodetected (depending + * on the XVID_CPU_FORCE flag). It also initializes vlc coding tables and all + * image colorspace transformation tables. + * + ****************************************************************************/ + + int -xvid_init(void *handle, +xvid_global(void *handle, int opt, void *param1, void *param2) { switch(opt) { - case XVID_INIT_INIT : - return xvid_init_init((XVID_INIT_PARAM*)param1); + case XVID_GBL_INIT : + return xvid_gbl_init((xvid_gbl_init_t*)param1); - case XVID_INIT_CONVERT : - return xvid_init_convert((XVID_INIT_CONVERTINFO*)param1); + case XVID_GBL_INFO : + return xvid_gbl_info((xvid_gbl_info_t*)param1); - case XVID_INIT_TEST : - return xvid_init_test((int)param1); + case XVID_GBL_CONVERT : + return xvid_gbl_convert((xvid_gbl_convert_t*)param1); + case XVID_GBL_TEST : + { + ptr_t flags = (ptr_t)param1; + return xvid_init_test((int)flags); + } default : return XVID_ERR_FAIL; } @@ -969,15 +1028,15 @@ void *param2) { switch (opt) { - case XVID_DEC_DECODE: - return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1, (XVID_DEC_STATS*) param2); - case XVID_DEC_CREATE: - return decoder_create((XVID_DEC_PARAM *) param1); + return decoder_create((xvid_dec_create_t *) param1); case XVID_DEC_DESTROY: return decoder_destroy((DECODER *) handle); + case XVID_DEC_DECODE: + return decoder_decode((DECODER *) handle, (xvid_dec_frame_t *) param1, (xvid_dec_stats_t*) param2); + default: return XVID_ERR_FAIL; } @@ -1003,18 +1062,15 @@ switch (opt) { 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); - else - return encoder_encode((Encoder *) handle, (XVID_ENC_FRAME *) param1, - (XVID_ENC_STATS *) param2); + return enc_encode((Encoder *) handle, + (xvid_enc_frame_t *) param1, + (xvid_enc_stats_t *) param2); case XVID_ENC_CREATE: - return encoder_create((XVID_ENC_PARAM *) param1); + return enc_create((xvid_enc_create_t *) param1); case XVID_ENC_DESTROY: - return encoder_destroy((Encoder *) handle); + return enc_destroy((Encoder *) handle); default: return XVID_ERR_FAIL;