--- xvid.c 2002/06/16 19:53:44 1.19 +++ xvid.c 2002/12/19 00:37:56 1.33.2.18 @@ -27,15 +27,17 @@ * 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.19 2002/06/16 19:53:44 edgomez Exp $ + * $Id: xvid.c,v 1.33.2.18 2002/12/19 00:37:56 Isibaar Exp $ * ****************************************************************************/ @@ -47,14 +49,84 @@ #include "dct/fdct.h" #include "image/colorspace.h" #include "image/interpolate8x8.h" +#include "image/reduced.h" #include "utils/mem_transfer.h" +#include "utils/mbfunctions.h" #include "quant/quant_h263.h" #include "quant/quant_mpeg4.h" +#include "motion/motion.h" #include "motion/sad.h" #include "utils/emms.h" #include "utils/timer.h" #include "bitstream/mbcoding.h" +#if defined(ARCH_X86) && defined(EXPERIMENTAL_SSE2_CODE) + +#ifdef WIN32 +#include +#else +#include +#include +#endif + + +#ifndef WIN32 + +static jmp_buf mark; + +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 +*/ + +int +sigill_check(void (*func)()) +{ +#ifdef WIN32 + _try { + func(); + } + _except(EXCEPTION_EXECUTE_HANDLER) { + + if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION) + return 1; + } + return 0; +#else + void * old_handler; + int jmpret; + + + old_handler = signal(SIGILL, sigill_handler); + if (old_handler == SIG_ERR) + { + return -1; + } + + jmpret = setjmp(mark); + if (jmpret == 0) + { + func(); + } + + signal(SIGILL, old_handler); + + return jmpret; +#endif +} +#endif + /***************************************************************************** * XviD Init Entry point * @@ -69,30 +141,45 @@ * ****************************************************************************/ -int -xvid_init(void *handle, - int opt, - void *param1, - void *param2) + +static +int xvid_init_init(XVID_INIT_PARAM * init_param) { int cpu_flags; - XVID_INIT_PARAM *init_param; - init_param = (XVID_INIT_PARAM *) param1; + /* 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) > 0) { + if ((init_param->cpu_flags & XVID_CPU_FORCE)) { + cpu_flags = init_param->cpu_flags; + } else { -#ifdef ARCH_X86 cpu_flags = check_cpu_features(); -#else - cpu_flags = 0; + +#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 + } + + if ((init_param->cpu_flags & XVID_CPU_CHKONLY)) + { init_param->cpu_flags = cpu_flags; + return XVID_ERR_OK; } + init_param->cpu_flags = cpu_flags; + + /* Initialize the function pointers */ idct_int32_init(); init_vlc_tables(); @@ -126,49 +213,112 @@ transfer_16to8add = transfer_16to8add_c; transfer8x8_copy = transfer8x8_copy_c; + /* Interlacing functions */ + MBFieldTest = MBFieldTest_c; + /* Image interpolation related functions */ interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_c; interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_c; interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_c; + interpolate16x16_lowpass_h = interpolate16x16_lowpass_h_c; + interpolate16x16_lowpass_v = interpolate16x16_lowpass_v_c; + interpolate16x16_lowpass_hv = interpolate16x16_lowpass_hv_c; + + interpolate8x8_lowpass_h = interpolate8x8_lowpass_h_c; + interpolate8x8_lowpass_v = interpolate8x8_lowpass_v_c; + interpolate8x8_lowpass_hv = interpolate8x8_lowpass_hv_c; + + interpolate8x8_6tap_lowpass_h = interpolate8x8_6tap_lowpass_h_c; + interpolate8x8_6tap_lowpass_v = interpolate8x8_6tap_lowpass_v_c; + + interpolate8x8_avg2 = interpolate8x8_avg2_c; + 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; + /* Initialize internal colorspace transformation tables */ colorspace_init(); /* All colorspace transformation functions User Format->YV12 */ - rgb555_to_yv12 = rgb555_to_yv12_c; - rgb565_to_yv12 = rgb565_to_yv12_c; - rgb24_to_yv12 = rgb24_to_yv12_c; - rgb32_to_yv12 = rgb32_to_yv12_c; - yuv_to_yv12 = yuv_to_yv12_c; - yuyv_to_yv12 = yuyv_to_yv12_c; - uyvy_to_yv12 = uyvy_to_yv12_c; + yv12_to_yv12 = yv12_to_yv12_c; + rgb555_to_yv12 = rgb555_to_yv12_c; + rgb565_to_yv12 = rgb565_to_yv12_c; + bgr_to_yv12 = bgr_to_yv12_c; + bgra_to_yv12 = bgra_to_yv12_c; + abgr_to_yv12 = abgr_to_yv12_c; + rgba_to_yv12 = rgba_to_yv12_c; + yuyv_to_yv12 = yuyv_to_yv12_c; + uyvy_to_yv12 = uyvy_to_yv12_c; + + rgb555i_to_yv12 = rgb555i_to_yv12_c; + rgb565i_to_yv12 = rgb565i_to_yv12_c; + bgri_to_yv12 = bgri_to_yv12_c; + bgrai_to_yv12 = bgrai_to_yv12_c; + abgri_to_yv12 = abgri_to_yv12_c; + rgbai_to_yv12 = rgbai_to_yv12_c; + yuyvi_to_yv12 = yuyvi_to_yv12_c; + uyvyi_to_yv12 = uyvyi_to_yv12_c; + /* All colorspace transformation functions YV12->User format */ - yv12_to_rgb555 = yv12_to_rgb555_c; - yv12_to_rgb565 = yv12_to_rgb565_c; - yv12_to_rgb24 = yv12_to_rgb24_c; - yv12_to_rgb32 = yv12_to_rgb32_c; - yv12_to_yuv = yv12_to_yuv_c; - yv12_to_yuyv = yv12_to_yuyv_c; - yv12_to_uyvy = yv12_to_uyvy_c; + yv12_to_rgb555 = yv12_to_rgb555_c; + yv12_to_rgb565 = yv12_to_rgb565_c; + yv12_to_bgr = yv12_to_bgr_c; + yv12_to_bgra = yv12_to_bgra_c; + yv12_to_abgr = yv12_to_abgr_c; + yv12_to_rgba = yv12_to_rgba_c; + yv12_to_yuyv = yv12_to_yuyv_c; + yv12_to_uyvy = yv12_to_uyvy_c; + + yv12_to_rgb555i = yv12_to_rgb555i_c; + yv12_to_rgb565i = yv12_to_rgb565i_c; + yv12_to_bgri = yv12_to_bgri_c; + yv12_to_bgrai = yv12_to_bgrai_c; + yv12_to_abgri = yv12_to_abgri_c; + yv12_to_rgbai = yv12_to_rgbai_c; + yv12_to_yuyvi = yv12_to_yuyvi_c; + yv12_to_uyvyi = yv12_to_uyvyi_c; /* Functions used in motion estimation algorithms */ calc_cbp = calc_cbp_c; sad16 = sad16_c; - sad16bi = sad16bi_c; sad8 = sad8_c; + sad16bi = sad16bi_c; + sad8bi = sad8bi_c; dev16 = dev16_c; + sad16v = sad16v_c; + +// Halfpel8_Refine = Halfpel8_Refine_c; #ifdef ARCH_X86 + + if ((cpu_flags & XVID_CPU_MMX) || (cpu_flags & XVID_CPU_MMXEXT) || + (cpu_flags & XVID_CPU_3DNOW) || (cpu_flags & XVID_CPU_3DNOWEXT) || + (cpu_flags & XVID_CPU_SSE) || (cpu_flags & XVID_CPU_SSE2)) + { + /* Restore FPU context : emms_c is a nop functions */ + emms = emms_mmx; + } + if ((cpu_flags & XVID_CPU_MMX) > 0) { /* Forward and Inverse Discrete Cosine Transformation functions */ fdct = fdct_mmx; idct = idct_mmx; - /* To restore FPU context after mmx use */ - emms = emms_mmx; - /* Quantization related functions */ quant_intra = quant_intra_mmx; dequant_intra = dequant_intra_mmx; @@ -184,35 +334,70 @@ transfer_8to16copy = transfer_8to16copy_mmx; transfer_16to8copy = transfer_16to8copy_mmx; transfer_8to16sub = transfer_8to16sub_mmx; + transfer_8to16sub2 = transfer_8to16sub2_mmx; transfer_16to8add = transfer_16to8add_mmx; transfer8x8_copy = transfer8x8_copy_mmx; + /* Interlacing Functions */ + MBFieldTest = MBFieldTest_mmx; + /* Image Interpolation related functions */ interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_mmx; interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_mmx; interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_mmx; - /* Image RGB->YV12 related functions */ - rgb24_to_yv12 = rgb24_to_yv12_mmx; - rgb32_to_yv12 = rgb32_to_yv12_mmx; - yuv_to_yv12 = yuv_to_yv12_mmx; + interpolate8x8_6tap_lowpass_h = interpolate8x8_6tap_lowpass_h_mmx; + interpolate8x8_6tap_lowpass_v = interpolate8x8_6tap_lowpass_v_mmx; + + interpolate8x8_avg2 = interpolate8x8_avg2_mmx; + interpolate8x8_avg4 = interpolate8x8_avg4_mmx; + + /* reduced resolution */ + copy_upsampled_8x8_16to8 = xvid_Copy_Upsampled_8x8_16To8_mmx; + add_upsampled_8x8_16to8 = xvid_Add_Upsampled_8x8_16To8_mmx; + hfilter_31 = xvid_HFilter_31_mmx; + filter_18x18_to_8x8 = xvid_Filter_18x18_To_8x8_mmx; + filter_diff_18x18_to_8x8 = xvid_Filter_Diff_18x18_To_8x8_mmx; + + /* image input xxx_to_yv12 related functions */ + yv12_to_yv12 = yv12_to_yv12_mmx; + bgr_to_yv12 = bgr_to_yv12_mmx; + bgra_to_yv12 = bgra_to_yv12_mmx; yuyv_to_yv12 = yuyv_to_yv12_mmx; uyvy_to_yv12 = uyvy_to_yv12_mmx; - /* Image YV12->RGB related functions */ - yv12_to_rgb24 = yv12_to_rgb24_mmx; - yv12_to_rgb32 = yv12_to_rgb32_mmx; + /* image output yv12_to_xxx related functions */ + yv12_to_bgr = yv12_to_bgr_mmx; + yv12_to_bgra = yv12_to_bgra_mmx; yv12_to_yuyv = yv12_to_yuyv_mmx; yv12_to_uyvy = yv12_to_uyvy_mmx; + + yv12_to_yuyvi = yv12_to_yuyvi_mmx; + yv12_to_uyvyi = yv12_to_uyvyi_mmx; /* Motion estimation related functions */ calc_cbp = calc_cbp_mmx; sad16 = sad16_mmx; sad8 = sad8_mmx; + sad16bi = sad16bi_mmx; + 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) { + + /* ME functions */ + sad16bi = sad16bi_3dn; + sad8bi = sad8bi_3dn; + yuyv_to_yv12 = yuyv_to_yv12_3dn; + uyvy_to_yv12 = uyvy_to_yv12_3dn; } + if ((cpu_flags & XVID_CPU_MMXEXT) > 0) { /* Inverse DCT */ @@ -223,17 +408,32 @@ interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_xmm; interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_xmm; + /* reduced resolution */ + copy_upsampled_8x8_16to8 = xvid_Copy_Upsampled_8x8_16To8_xmm; + add_upsampled_8x8_16to8 = xvid_Add_Upsampled_8x8_16To8_xmm; + + /* Quantization */ + quant4_intra = quant4_intra_xmm; + quant4_inter = quant4_inter_xmm; + + dequant_intra = dequant_intra_xmm; + dequant_inter = dequant_inter_xmm; + /* Buffer transfer */ transfer_8to16sub2 = transfer_8to16sub2_xmm; /* Colorspace transformation */ - yuv_to_yv12 = yuv_to_yv12_xmm; + yv12_to_yv12 = yv12_to_yv12_xmm; + yuyv_to_yv12 = yuyv_to_yv12_xmm; + uyvy_to_yv12 = uyvy_to_yv12_xmm; /* ME functions */ sad16 = sad16_xmm; sad8 = sad8_xmm; + sad16bi = sad16bi_xmm; + sad8bi = sad8bi_xmm; dev16 = dev16_xmm; - + sad16v = sad16v_xmm; } if ((cpu_flags & XVID_CPU_3DNOW) > 0) { @@ -244,9 +444,47 @@ interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dn; } + if ((cpu_flags & XVID_CPU_3DNOWEXT) > 0) { + + /* Inverse DCT */ + idct = idct_3dne; + + /* Buffer transfer */ + transfer_8to16copy = transfer_8to16copy_3dne; + transfer_16to8copy = transfer_16to8copy_3dne; + transfer_8to16sub = transfer_8to16sub_3dne; + transfer_8to16sub2 = transfer_8to16sub2_3dne; + transfer_16to8add = transfer_16to8add_3dne; + transfer8x8_copy = transfer8x8_copy_3dne; + + /* Quantization */ + dequant4_intra = dequant4_intra_3dne; + dequant4_inter = dequant4_inter_3dne; + quant_intra = quant_intra_3dne; + quant_inter = quant_inter_3dne; + dequant_intra = dequant_intra_3dne; + dequant_inter = dequant_inter_3dne; + + /* ME functions */ + calc_cbp = calc_cbp_3dne; + sad16 = sad16_3dne; + sad8 = sad8_3dne; + sad16bi = sad16bi_3dne; + sad8bi = sad8bi_3dne; + dev16 = dev16_3dne; + + /* Interpolation */ + interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_3dne; + interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_3dne; + interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dne; + } + + if ((cpu_flags & XVID_CPU_SSE2) > 0) { #ifdef EXPERIMENTAL_SSE2_CODE + calc_cbp = calc_cbp_sse2; + /* Quantization */ quant_intra = quant_intra_sse2; dequant_intra = dequant_intra_sse2; @@ -254,7 +492,6 @@ dequant_inter = dequant_inter_sse2; /* ME */ - calc_cbp = calc_cbp_sse2; sad16 = sad16_sse2; dev16 = dev16_sse2; @@ -278,6 +515,7 @@ sad16bi = sad16bi_ia64; sad8 = sad8_ia64; dev16 = dev16_ia64; +// Halfpel8_Refine = Halfpel8_Refine_ia64; quant_intra = quant_intra_ia64; dequant_intra = dequant_intra_ia64; quant_inter = quant_inter_ia64; @@ -306,15 +544,61 @@ #endif #endif - /* Inform the client the API version */ - init_param->api_version = API_VERSION; + return XVID_ERR_OK; +} - /* Inform the client the core build - unused because we're still alpha */ - init_param->core_build = 1000; + +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 width = convert->width; + const int height = convert->height; + const int width2 = convert->width/2; + const int height2 = convert->height/2; + IMAGE img; + + switch (convert->input.colorspace & ~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; + image_output(&img, width, height, width, + convert->output.y, convert->output.y_stride, + convert->output.colorspace, convert->interlacing); + break; + + default : + return XVID_ERR_FORMAT; + } + + + emms(); return XVID_ERR_OK; } + +int +xvid_init(void *handle, + int opt, + void *param1, + void *param2) +{ + switch(opt) + { + case XVID_INIT_INIT : + return xvid_init_init((XVID_INIT_PARAM*)param1); + + case XVID_INIT_CONVERT : + return xvid_init_convert((XVID_INIT_CONVERTINFO*)param1); + + default : + return XVID_ERR_FAIL; + } +} + /***************************************************************************** * XviD Native decoder entry point * @@ -333,7 +617,7 @@ { switch (opt) { case XVID_DEC_DECODE: - return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1); + return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1, (XVID_DEC_STATS*) param2); case XVID_DEC_CREATE: return decoder_create((XVID_DEC_PARAM *) param1); @@ -365,6 +649,11 @@ { 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);