3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Native API implementation - |
* - Native API implementation - |
5 |
* |
* |
6 |
|
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 |
|
* |
8 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation ; either version 2 of the License, or |
* the Free Software Foundation ; either version 2 of the License, or |
19 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: xvid.c,v 1.44 2003-02-20 22:46:18 edgomez Exp $ |
* $Id: xvid.c,v 1.45.2.15 2003-10-01 23:23:01 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
46 |
#include "utils/emms.h" |
#include "utils/emms.h" |
47 |
#include "utils/timer.h" |
#include "utils/timer.h" |
48 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
49 |
|
#include "image/qpel.h" |
50 |
|
|
51 |
|
#if defined(_DEBUG) |
52 |
|
unsigned int xvid_debug = 0; /* xvid debug mask */ |
53 |
|
#endif |
54 |
|
|
55 |
#if defined(ARCH_IS_IA32) |
#if defined(ARCH_IS_IA32) |
56 |
|
|
160 |
|
|
161 |
|
|
162 |
static |
static |
163 |
int xvid_init_init(XVID_INIT_PARAM * init_param) |
int xvid_gbl_init(xvid_gbl_init_t * init) |
164 |
{ |
{ |
165 |
int cpu_flags; |
unsigned int cpu_flags; |
|
|
|
|
/* Inform the client the API version */ |
|
|
init_param->api_version = API_VERSION; |
|
166 |
|
|
167 |
/* Inform the client the core build - unused because we're still alpha */ |
if (XVID_VERSION_MAJOR(init->version) != 1) /* v1.x.x */ |
168 |
init_param->core_build = 1000; |
return XVID_ERR_VERSION; |
|
|
|
|
/* Do we have to force CPU features ? */ |
|
|
if ((init_param->cpu_flags & XVID_CPU_FORCE)) { |
|
|
|
|
|
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; |
|
169 |
|
|
170 |
|
cpu_flags = (init->cpu_flags & XVID_CPU_FORCE) ? init->cpu_flags : detect_cpu_flags(); |
171 |
|
|
172 |
/* Initialize the function pointers */ |
/* Initialize the function pointers */ |
173 |
idct_int32_init(); |
idct_int32_init(); |
183 |
/* Restore FPU context : emms_c is a nop functions */ |
/* Restore FPU context : emms_c is a nop functions */ |
184 |
emms = emms_c; |
emms = emms_c; |
185 |
|
|
186 |
|
/* Qpel stuff */ |
187 |
|
xvid_QP_Funcs = &xvid_QP_Funcs_C; |
188 |
|
xvid_QP_Add_Funcs = &xvid_QP_Add_Funcs_C; |
189 |
|
xvid_Init_QP(); |
190 |
|
|
191 |
/* Quantization functions */ |
/* Quantization functions */ |
192 |
quant_intra = quant_intra_c; |
quant_intra = quant_intra_c; |
193 |
dequant_intra = dequant_intra_c; |
dequant_intra = dequant_intra_c; |
294 |
|
|
295 |
#if defined(ARCH_IS_IA32) |
#if defined(ARCH_IS_IA32) |
296 |
|
|
297 |
if ((cpu_flags & XVID_CPU_ASM)) |
if ((cpu_flags & XVID_CPU_ASM)) { |
|
{ |
|
298 |
vfilter_31 = xvid_VFilter_31_x86; |
vfilter_31 = xvid_VFilter_31_x86; |
299 |
hfilter_31 = xvid_HFilter_31_x86; |
hfilter_31 = xvid_HFilter_31_x86; |
300 |
} |
} |
313 |
fdct = fdct_mmx; |
fdct = fdct_mmx; |
314 |
idct = idct_mmx; |
idct = idct_mmx; |
315 |
|
|
316 |
|
/* Qpel stuff */ |
317 |
|
xvid_QP_Funcs = &xvid_QP_Funcs_mmx; |
318 |
|
xvid_QP_Add_Funcs = &xvid_QP_Add_Funcs_mmx; |
319 |
|
|
320 |
/* Quantization related functions */ |
/* Quantization related functions */ |
321 |
quant_intra = quant_intra_mmx; |
quant_intra = quant_intra_mmx; |
322 |
dequant_intra = dequant_intra_mmx; |
dequant_intra = dequant_intra_mmx; |
480 |
interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dne; |
interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dne; |
481 |
} |
} |
482 |
|
|
483 |
|
#if defined(EXPERIMENTAL_SSE2_CODE) /* mark the whole SSE2 stuff as experimental. At least on |
484 |
|
my P4, it crashes... */ |
485 |
if ((cpu_flags & XVID_CPU_SSE2)) { |
if ((cpu_flags & XVID_CPU_SSE2)) { |
486 |
|
|
487 |
calc_cbp = calc_cbp_sse2; |
calc_cbp = calc_cbp_sse2; |
492 |
quant_inter = quant_inter_sse2; |
quant_inter = quant_inter_sse2; |
493 |
dequant_inter = dequant_inter_sse2; |
dequant_inter = dequant_inter_sse2; |
494 |
|
|
|
#if defined(EXPERIMENTAL_SSE2_CODE) |
|
495 |
/* ME; slower than xmm */ |
/* ME; slower than xmm */ |
496 |
sad16 = sad16_sse2; |
sad16 = sad16_sse2; |
497 |
dev16 = dev16_sse2; |
dev16 = dev16_sse2; |
|
#endif |
|
498 |
/* Forward and Inverse DCT */ |
/* Forward and Inverse DCT */ |
499 |
|
#if 0 /* Both function are known to be unprecise, better keep them deactivated */ |
500 |
idct = idct_sse2; |
idct = idct_sse2; |
501 |
fdct = fdct_sse2; |
fdct = fdct_sse2; |
502 |
|
#endif |
503 |
} |
} |
504 |
#endif |
#endif |
505 |
|
#endif |
506 |
|
|
507 |
#if defined(ARCH_IS_IA64) |
#if defined(ARCH_IS_IA64) |
508 |
if ((cpu_flags & XVID_CPU_ASM)) { /* use assembler routines? */ |
if ((cpu_flags & XVID_CPU_ASM)) { /* use assembler routines? */ |
527 |
transfer_8to16sub2 = transfer_8to16sub2_ia64; |
transfer_8to16sub2 = transfer_8to16sub2_ia64; |
528 |
transfer_16to8add = transfer_16to8add_ia64; |
transfer_16to8add = transfer_16to8add_ia64; |
529 |
transfer8x8_copy = transfer8x8_copy_ia64; |
transfer8x8_copy = transfer8x8_copy_ia64; |
|
DPRINTF(DPRINTF_DEBUG, "Using IA-64 assembler routines."); |
|
530 |
} |
} |
531 |
#endif |
#endif |
532 |
|
|
548 |
} |
} |
549 |
#endif |
#endif |
550 |
|
|
551 |
return XVID_ERR_OK; |
#if defined(_DEBUG) |
552 |
|
xvid_debug = init->debug; |
553 |
|
#endif |
554 |
|
|
555 |
|
return 0; |
556 |
} |
} |
557 |
|
|
558 |
|
|
559 |
|
static int |
560 |
|
xvid_gbl_info(xvid_gbl_info_t * info) |
561 |
|
{ |
562 |
|
if (XVID_VERSION_MAJOR(info->version) != 1) /* v1.x.x */ |
563 |
|
return XVID_ERR_VERSION; |
564 |
|
|
565 |
|
info->actual_version = XVID_VERSION; |
566 |
|
info->build = "dev-api-4"; |
567 |
|
info->cpu_flags = detect_cpu_flags(); |
568 |
|
|
569 |
|
#if defined(_SMP) && defined(WIN32) |
570 |
|
info->num_threads = pthread_num_processors_np();; |
571 |
|
#else |
572 |
|
info->num_threads = 0; |
573 |
|
#endif |
574 |
|
|
575 |
|
return 0; |
576 |
|
} |
577 |
|
|
578 |
|
|
579 |
static int |
static int |
580 |
xvid_init_convert(XVID_INIT_CONVERTINFO* convert) |
xvid_gbl_convert(xvid_gbl_convert_t* convert) |
581 |
{ |
{ |
582 |
/* |
int width; |
583 |
const int flip1 = |
int height; |
584 |
(convert->input.colorspace & XVID_CSP_VFLIP) ^ |
int width2; |
585 |
(convert->output.colorspace & XVID_CSP_VFLIP); |
int height2; |
|
*/ |
|
|
const int width = convert->width; |
|
|
const int height = convert->height; |
|
|
const int width2 = convert->width/2; |
|
|
const int height2 = convert->height/2; |
|
586 |
IMAGE img; |
IMAGE img; |
587 |
|
|
588 |
switch (convert->input.colorspace & ~XVID_CSP_VFLIP) |
if (XVID_VERSION_MAJOR(convert->version) != 1) /* v1.x.x */ |
589 |
|
return XVID_ERR_VERSION; |
590 |
|
|
591 |
|
#if 0 |
592 |
|
const int flip1 = (convert->input.colorspace & XVID_CSP_VFLIP) ^ (convert->output.colorspace & XVID_CSP_VFLIP); |
593 |
|
#endif |
594 |
|
width = convert->width; |
595 |
|
height = convert->height; |
596 |
|
width2 = convert->width/2; |
597 |
|
height2 = convert->height/2; |
598 |
|
|
599 |
|
switch (convert->input.csp & ~XVID_CSP_VFLIP) |
600 |
{ |
{ |
601 |
case XVID_CSP_YV12 : |
case XVID_CSP_YV12 : |
602 |
img.y = convert->input.y; |
img.y = convert->input.plane[0]; |
603 |
img.v = (uint8_t*)convert->input.y + width*height; |
img.v = (uint8_t*)convert->input.plane[0] + convert->input.stride[0]*height; |
604 |
img.u = (uint8_t*)convert->input.y + width*height + width2*height2; |
img.u = (uint8_t*)convert->input.plane[0] + convert->input.stride[0]*height + (convert->input.stride[0]/2)*height2; |
605 |
image_output(&img, width, height, width, |
image_output(&img, width, height, width, |
606 |
convert->output.y, convert->output.y_stride, |
(uint8_t**)convert->output.plane, convert->output.stride, |
607 |
convert->output.colorspace, convert->interlacing); |
convert->output.csp, convert->interlacing); |
608 |
break; |
break; |
609 |
|
|
610 |
default : |
default : |
613 |
|
|
614 |
|
|
615 |
emms(); |
emms(); |
616 |
return XVID_ERR_OK; |
return 0; |
617 |
} |
} |
618 |
|
|
619 |
|
|
662 |
{ |
{ |
663 |
int i, diff = 0; |
int i, diff = 0; |
664 |
for (i = 0; i < size; i++) |
for (i = 0; i < size; i++) |
665 |
diff += ABS(blockA[i]-blockB[i]); |
diff += abs(blockA[i]-blockB[i]); |
666 |
return diff; |
return diff; |
667 |
} |
} |
668 |
|
|
836 |
|
|
837 |
int xvid_init_test(int flags) |
int xvid_init_test(int flags) |
838 |
{ |
{ |
839 |
int cpu_flags = 0; |
#if defined(ARCH_IS_IA32) |
840 |
|
int cpu_flags; |
841 |
srand(time(0)); |
#endif |
842 |
|
|
843 |
printf("XviD tests\n\n"); |
printf("XviD tests\n\n"); |
844 |
|
|
849 |
idct_int32_init(); |
idct_int32_init(); |
850 |
emms(); |
emms(); |
851 |
|
|
852 |
|
srand(time(0)); |
853 |
|
|
854 |
/* fDCT test */ |
/* fDCT test */ |
855 |
printf("--- fdct ---\n"); |
printf("--- fdct ---\n"); |
856 |
test_transform(fdct_int32, fdct_int32, "c", TEST_FDCT, flags); |
test_transform(fdct_int32, fdct_int32, "c", TEST_FDCT, flags); |
979 |
|
|
980 |
emms(); |
emms(); |
981 |
|
|
982 |
return XVID_ERR_OK; |
return 0; |
983 |
} |
} |
984 |
|
|
985 |
|
|
986 |
|
/***************************************************************************** |
987 |
|
* XviD Global Entry point |
988 |
|
* |
989 |
|
* Well this function initialize all internal function pointers according |
990 |
|
* to the CPU features forced by the library client or autodetected (depending |
991 |
|
* on the XVID_CPU_FORCE flag). It also initializes vlc coding tables and all |
992 |
|
* image colorspace transformation tables. |
993 |
|
* |
994 |
|
****************************************************************************/ |
995 |
|
|
996 |
|
|
997 |
int |
int |
998 |
xvid_init(void *handle, |
xvid_global(void *handle, |
999 |
int opt, |
int opt, |
1000 |
void *param1, |
void *param1, |
1001 |
void *param2) |
void *param2) |
1002 |
{ |
{ |
1003 |
switch(opt) |
switch(opt) |
1004 |
{ |
{ |
1005 |
case XVID_INIT_INIT : |
case XVID_GBL_INIT : |
1006 |
return xvid_init_init((XVID_INIT_PARAM*)param1); |
return xvid_gbl_init((xvid_gbl_init_t*)param1); |
1007 |
|
|
1008 |
case XVID_INIT_CONVERT : |
case XVID_GBL_INFO : |
1009 |
return xvid_init_convert((XVID_INIT_CONVERTINFO*)param1); |
return xvid_gbl_info((xvid_gbl_info_t*)param1); |
1010 |
|
|
1011 |
case XVID_INIT_TEST : |
case XVID_GBL_CONVERT : |
1012 |
|
return xvid_gbl_convert((xvid_gbl_convert_t*)param1); |
1013 |
|
|
1014 |
|
case XVID_GBL_TEST : |
1015 |
{ |
{ |
1016 |
ptr_t flags = (ptr_t)param1; |
ptr_t flags = (ptr_t)param1; |
1017 |
return xvid_init_test((int)flags); |
return xvid_init_test((int)flags); |
1038 |
void *param2) |
void *param2) |
1039 |
{ |
{ |
1040 |
switch (opt) { |
switch (opt) { |
|
case XVID_DEC_DECODE: |
|
|
return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1, (XVID_DEC_STATS*) param2); |
|
|
|
|
1041 |
case XVID_DEC_CREATE: |
case XVID_DEC_CREATE: |
1042 |
return decoder_create((XVID_DEC_PARAM *) param1); |
return decoder_create((xvid_dec_create_t *) param1); |
1043 |
|
|
1044 |
case XVID_DEC_DESTROY: |
case XVID_DEC_DESTROY: |
1045 |
return decoder_destroy((DECODER *) handle); |
return decoder_destroy((DECODER *) handle); |
1046 |
|
|
1047 |
|
case XVID_DEC_DECODE: |
1048 |
|
return decoder_decode((DECODER *) handle, (xvid_dec_frame_t *) param1, (xvid_dec_stats_t*) param2); |
1049 |
|
|
1050 |
default: |
default: |
1051 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
1052 |
} |
} |
1072 |
switch (opt) { |
switch (opt) { |
1073 |
case XVID_ENC_ENCODE: |
case XVID_ENC_ENCODE: |
1074 |
|
|
1075 |
if (((Encoder *) handle)->mbParam.max_bframes >= 0) |
return enc_encode((Encoder *) handle, |
1076 |
return encoder_encode_bframes((Encoder *) handle, |
(xvid_enc_frame_t *) param1, |
1077 |
(XVID_ENC_FRAME *) param1, |
(xvid_enc_stats_t *) param2); |
|
(XVID_ENC_STATS *) param2); |
|
|
else |
|
|
return encoder_encode((Encoder *) handle, |
|
|
(XVID_ENC_FRAME *) param1, |
|
|
(XVID_ENC_STATS *) param2); |
|
1078 |
|
|
1079 |
case XVID_ENC_CREATE: |
case XVID_ENC_CREATE: |
1080 |
return encoder_create((XVID_ENC_PARAM *) param1); |
return enc_create((xvid_enc_create_t *) param1); |
1081 |
|
|
1082 |
case XVID_ENC_DESTROY: |
case XVID_ENC_DESTROY: |
1083 |
return encoder_destroy((Encoder *) handle); |
return enc_destroy((Encoder *) handle); |
1084 |
|
|
1085 |
default: |
default: |
1086 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |