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.45 2003-02-21 00:00:57 edgomez Exp $ |
* $Id: xvid.c,v 1.45.2.7 2003-06-09 19:42:08 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
47 |
#include "utils/timer.h" |
#include "utils/timer.h" |
48 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
49 |
|
|
50 |
|
#if defined(_DEBUG) |
51 |
|
unsigned int xvid_debug = 0; /* xvid debug mask */ |
52 |
|
#endif |
53 |
|
|
54 |
#if defined(ARCH_IS_IA32) |
#if defined(ARCH_IS_IA32) |
55 |
|
|
56 |
#if defined(_MSC_VER) |
#if defined(_MSC_VER) |
159 |
|
|
160 |
|
|
161 |
static |
static |
162 |
int xvid_init_init(XVID_INIT_PARAM * init_param) |
int xvid_gbl_init(xvid_gbl_init_t * init) |
163 |
{ |
{ |
164 |
int cpu_flags; |
unsigned 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)) { |
|
|
|
|
|
cpu_flags = init_param->cpu_flags; |
|
165 |
|
|
166 |
} else { |
if (XVID_MAJOR(init->version) != 1) /* v1.x.x */ |
167 |
|
return XVID_ERR_VERSION; |
|
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; |
|
168 |
|
|
169 |
|
cpu_flags = (init->cpu_flags & XVID_CPU_FORCE) ? init->cpu_flags : detect_cpu_flags(); |
170 |
|
|
171 |
/* Initialize the function pointers */ |
/* Initialize the function pointers */ |
172 |
idct_int32_init(); |
idct_int32_init(); |
288 |
|
|
289 |
#if defined(ARCH_IS_IA32) |
#if defined(ARCH_IS_IA32) |
290 |
|
|
291 |
if ((cpu_flags & XVID_CPU_ASM)) |
if ((cpu_flags & XVID_CPU_ASM)) { |
|
{ |
|
292 |
vfilter_31 = xvid_VFilter_31_x86; |
vfilter_31 = xvid_VFilter_31_x86; |
293 |
hfilter_31 = xvid_HFilter_31_x86; |
hfilter_31 = xvid_HFilter_31_x86; |
294 |
} |
} |
305 |
|
|
306 |
/* Forward and Inverse Discrete Cosine Transformation functions */ |
/* Forward and Inverse Discrete Cosine Transformation functions */ |
307 |
fdct = fdct_mmx; |
fdct = fdct_mmx; |
308 |
idct = idct_mmx; |
idct = simple_idct_mmx; |
309 |
|
|
310 |
/* Quantization related functions */ |
/* Quantization related functions */ |
311 |
quant_intra = quant_intra_mmx; |
quant_intra = quant_intra_mmx; |
391 |
if ((cpu_flags & XVID_CPU_MMXEXT)) { |
if ((cpu_flags & XVID_CPU_MMXEXT)) { |
392 |
|
|
393 |
/* Inverse DCT */ |
/* Inverse DCT */ |
394 |
|
#if 0 /* We don't use Walken idct anymore! */ |
395 |
idct = idct_xmm; |
idct = idct_xmm; |
396 |
|
#endif |
397 |
|
|
398 |
/* Interpolation */ |
/* Interpolation */ |
399 |
interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_xmm; |
interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_xmm; |
439 |
if ((cpu_flags & XVID_CPU_3DNOWEXT)) { |
if ((cpu_flags & XVID_CPU_3DNOWEXT)) { |
440 |
|
|
441 |
/* Inverse DCT */ |
/* Inverse DCT */ |
442 |
|
#if 0 /* We don't use Walken idct anymore! */ |
443 |
idct = idct_3dne; |
idct = idct_3dne; |
444 |
|
#endif |
445 |
|
|
446 |
/* Buffer transfer */ |
/* Buffer transfer */ |
447 |
transfer_8to16copy = transfer_8to16copy_3dne; |
transfer_8to16copy = transfer_8to16copy_3dne; |
491 |
dev16 = dev16_sse2; |
dev16 = dev16_sse2; |
492 |
#endif |
#endif |
493 |
/* Forward and Inverse DCT */ |
/* Forward and Inverse DCT */ |
494 |
|
#if 0 /* Both function are known to be unprecise, better keep them deactivated */ |
495 |
idct = idct_sse2; |
idct = idct_sse2; |
496 |
fdct = fdct_sse2; |
fdct = fdct_sse2; |
497 |
|
#endif |
498 |
} |
} |
499 |
#endif |
#endif |
500 |
|
|
543 |
} |
} |
544 |
#endif |
#endif |
545 |
|
|
546 |
return XVID_ERR_OK; |
#if defined(_DEBUG) |
547 |
|
xvid_debug = init->debug; |
548 |
|
#endif |
549 |
|
|
550 |
|
return 0; |
551 |
} |
} |
552 |
|
|
553 |
|
|
554 |
|
static int |
555 |
|
xvid_gbl_info(xvid_gbl_info_t * info) |
556 |
|
{ |
557 |
|
if (XVID_MAJOR(info->version) != 1) /* v1.x.x */ |
558 |
|
return XVID_ERR_VERSION; |
559 |
|
|
560 |
|
info->actual_version = XVID_VERSION; |
561 |
|
info->build = "dev-api-4"; |
562 |
|
info->cpu_flags = detect_cpu_flags(); |
563 |
|
|
564 |
|
#if defined(_SMP) && defined(WIN32) |
565 |
|
info->num_threads = pthread_num_processors_np();; |
566 |
|
#else |
567 |
|
info->num_threads = 0; |
568 |
|
#endif |
569 |
|
|
570 |
|
return 0; |
571 |
|
} |
572 |
|
|
573 |
|
|
574 |
static int |
static int |
575 |
xvid_init_convert(XVID_INIT_CONVERTINFO* convert) |
xvid_gbl_convert(xvid_gbl_convert_t* convert) |
576 |
{ |
{ |
577 |
/* |
int width; |
578 |
const int flip1 = |
int height; |
579 |
(convert->input.colorspace & XVID_CSP_VFLIP) ^ |
int width2; |
580 |
(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; |
|
581 |
IMAGE img; |
IMAGE img; |
582 |
|
|
583 |
switch (convert->input.colorspace & ~XVID_CSP_VFLIP) |
if (XVID_MAJOR(convert->version) != 1) /* v1.x.x */ |
584 |
|
return XVID_ERR_VERSION; |
585 |
|
|
586 |
|
#if 0 |
587 |
|
const int flip1 = (convert->input.colorspace & XVID_CSP_VFLIP) ^ (convert->output.colorspace & XVID_CSP_VFLIP); |
588 |
|
#endif |
589 |
|
width = convert->width; |
590 |
|
height = convert->height; |
591 |
|
width2 = convert->width/2; |
592 |
|
height2 = convert->height/2; |
593 |
|
|
594 |
|
switch (convert->input.csp & ~XVID_CSP_VFLIP) |
595 |
{ |
{ |
596 |
case XVID_CSP_YV12 : |
case XVID_CSP_YV12 : |
597 |
img.y = convert->input.y; |
img.y = convert->input.plane[0]; |
598 |
img.v = (uint8_t*)convert->input.y + width*height; |
img.v = (uint8_t*)convert->input.plane[0] + convert->input.stride[0]*height; |
599 |
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; |
600 |
image_output(&img, width, height, width, |
image_output(&img, width, height, width, |
601 |
convert->output.y, convert->output.y_stride, |
(uint8_t**)convert->output.plane, convert->output.stride, |
602 |
convert->output.colorspace, convert->interlacing); |
convert->output.csp, convert->interlacing); |
603 |
break; |
break; |
604 |
|
|
605 |
default : |
default : |
608 |
|
|
609 |
|
|
610 |
emms(); |
emms(); |
611 |
return XVID_ERR_OK; |
return 0; |
612 |
} |
} |
613 |
|
|
614 |
|
|
657 |
{ |
{ |
658 |
int i, diff = 0; |
int i, diff = 0; |
659 |
for (i = 0; i < size; i++) |
for (i = 0; i < size; i++) |
660 |
diff += ABS(blockA[i]-blockB[i]); |
diff += abs(blockA[i]-blockB[i]); |
661 |
return diff; |
return diff; |
662 |
} |
} |
663 |
|
|
974 |
|
|
975 |
emms(); |
emms(); |
976 |
|
|
977 |
return XVID_ERR_OK; |
return 0; |
978 |
} |
} |
979 |
|
|
980 |
|
|
981 |
|
/***************************************************************************** |
982 |
|
* XviD Global Entry point |
983 |
|
* |
984 |
|
* Well this function initialize all internal function pointers according |
985 |
|
* to the CPU features forced by the library client or autodetected (depending |
986 |
|
* on the XVID_CPU_FORCE flag). It also initializes vlc coding tables and all |
987 |
|
* image colorspace transformation tables. |
988 |
|
* |
989 |
|
****************************************************************************/ |
990 |
|
|
991 |
|
|
992 |
int |
int |
993 |
xvid_init(void *handle, |
xvid_global(void *handle, |
994 |
int opt, |
int opt, |
995 |
void *param1, |
void *param1, |
996 |
void *param2) |
void *param2) |
997 |
{ |
{ |
998 |
switch(opt) |
switch(opt) |
999 |
{ |
{ |
1000 |
case XVID_INIT_INIT : |
case XVID_GBL_INIT : |
1001 |
return xvid_init_init((XVID_INIT_PARAM*)param1); |
return xvid_gbl_init((xvid_gbl_init_t*)param1); |
1002 |
|
|
1003 |
|
case XVID_GBL_INFO : |
1004 |
|
return xvid_gbl_info((xvid_gbl_info_t*)param1); |
1005 |
|
|
1006 |
case XVID_INIT_CONVERT : |
case XVID_GBL_CONVERT : |
1007 |
return xvid_init_convert((XVID_INIT_CONVERTINFO*)param1); |
return xvid_gbl_convert((xvid_gbl_convert_t*)param1); |
1008 |
|
|
1009 |
case XVID_INIT_TEST : |
case XVID_GBL_TEST : |
1010 |
{ |
{ |
1011 |
ptr_t flags = (ptr_t)param1; |
ptr_t flags = (ptr_t)param1; |
1012 |
return xvid_init_test((int)flags); |
return xvid_init_test((int)flags); |
1033 |
void *param2) |
void *param2) |
1034 |
{ |
{ |
1035 |
switch (opt) { |
switch (opt) { |
|
case XVID_DEC_DECODE: |
|
|
return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1, (XVID_DEC_STATS*) param2); |
|
|
|
|
1036 |
case XVID_DEC_CREATE: |
case XVID_DEC_CREATE: |
1037 |
return decoder_create((XVID_DEC_PARAM *) param1); |
return decoder_create((xvid_dec_create_t *) param1); |
1038 |
|
|
1039 |
case XVID_DEC_DESTROY: |
case XVID_DEC_DESTROY: |
1040 |
return decoder_destroy((DECODER *) handle); |
return decoder_destroy((DECODER *) handle); |
1041 |
|
|
1042 |
|
case XVID_DEC_DECODE: |
1043 |
|
return decoder_decode((DECODER *) handle, (xvid_dec_frame_t *) param1, (xvid_dec_stats_t*) param2); |
1044 |
|
|
1045 |
default: |
default: |
1046 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
1047 |
} |
} |
1067 |
switch (opt) { |
switch (opt) { |
1068 |
case XVID_ENC_ENCODE: |
case XVID_ENC_ENCODE: |
1069 |
|
|
1070 |
if (((Encoder *) handle)->mbParam.max_bframes >= 0) |
return enc_encode((Encoder *) handle, |
1071 |
return encoder_encode_bframes((Encoder *) handle, |
(xvid_enc_frame_t *) param1, |
1072 |
(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); |
|
1073 |
|
|
1074 |
case XVID_ENC_CREATE: |
case XVID_ENC_CREATE: |
1075 |
return encoder_create((XVID_ENC_PARAM *) param1); |
return enc_create((xvid_enc_create_t *) param1); |
1076 |
|
|
1077 |
case XVID_ENC_DESTROY: |
case XVID_ENC_DESTROY: |
1078 |
return encoder_destroy((Encoder *) handle); |
return enc_destroy((Encoder *) handle); |
1079 |
|
|
1080 |
default: |
default: |
1081 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |