[svn] / trunk / xvidcore / src / xvid.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/xvid.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 824 - (view) (download)

1 : edgomez 200 /*****************************************************************************
2 : edgomez 201 *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Native API implementation -
5 :     *
6 : suxen_drol 499 * Copyright(C) 2001-2002 Peter Ross <pross@xvid.org>
7 : edgomez 408 *
8 : edgomez 648 * This file is part of XviD, a free MPEG-4 video encoder/decoder
9 : edgomez 201 *
10 : edgomez 648 * XviD is free software; you can redistribute it and/or modify it
11 :     * under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation; either version 2 of the License, or
13 : edgomez 201 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 648 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : edgomez 201 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 : edgomez 648 * along with this program; if not, write to the Free Software
22 : edgomez 201 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 648 * Under section 8 of the GNU General Public License, the copyright
25 :     * holders of XVID explicitly forbid distribution in the following
26 :     * countries:
27 : edgomez 410 *
28 : edgomez 648 * - Japan
29 :     * - United States of America
30 :     *
31 :     * Linking XviD statically or dynamically with other modules is making a
32 :     * combined work based on XviD. Thus, the terms and conditions of the
33 :     * GNU General Public License cover the whole combination.
34 :     *
35 :     * As a special exception, the copyright holders of XviD give you
36 :     * permission to link XviD with independent modules that communicate with
37 :     * XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38 :     * license terms of these independent modules, and to copy and distribute
39 :     * the resulting combined work under terms of your choice, provided that
40 :     * every copy of the combined work is accompanied by a complete copy of
41 :     * the source code of XviD (the version of XviD used to produce the
42 :     * combined work), being distributed under the terms of the GNU General
43 :     * Public License plus this exception. An independent module is a module
44 :     * which is not derived from or based on XviD.
45 :     *
46 :     * Note that people who make modified versions of XviD are not obligated
47 :     * to grant this special exception for their modified versions; it is
48 :     * their choice whether to do so. The GNU General Public License gives
49 :     * permission to release a modified version without this exception; this
50 :     * exception also makes it possible to release a modified version which
51 :     * carries forward this exception.
52 :     *
53 : edgomez 824 * $Id: xvid.c,v 1.40 2003-02-09 19:32:52 edgomez Exp $
54 : edgomez 648 *
55 : edgomez 201 ****************************************************************************/
56 : chenm001 274
57 : Isibaar 3 #include "xvid.h"
58 :     #include "decoder.h"
59 :     #include "encoder.h"
60 :     #include "bitstream/cbp.h"
61 :     #include "dct/idct.h"
62 :     #include "dct/fdct.h"
63 :     #include "image/colorspace.h"
64 :     #include "image/interpolate8x8.h"
65 :     #include "utils/mem_transfer.h"
66 :     #include "quant/quant_h263.h"
67 :     #include "quant/quant_mpeg4.h"
68 : ia64p 299 #include "motion/motion.h"
69 : Isibaar 3 #include "motion/sad.h"
70 :     #include "utils/emms.h"
71 :     #include "utils/timer.h"
72 : Isibaar 100 #include "bitstream/mbcoding.h"
73 : Isibaar 3
74 : edgomez 824 #if defined(ARCH_IS_IA32) && defined(EXPERIMENTAL_SSE2_CODE)
75 : suxen_drol 311
76 : edgomez 824 #ifdef _MSC_VER
77 : suxen_drol 311 #include <windows.h>
78 :     #else
79 :     #include <signal.h>
80 :     #include <setjmp.h>
81 :     #endif
82 :    
83 :    
84 : edgomez 824 #ifndef _MSC_VER
85 : suxen_drol 311
86 :     static jmp_buf mark;
87 :    
88 :     static void
89 :     sigill_handler(int signal)
90 :     {
91 :     longjmp(mark, 1);
92 :     }
93 :     #endif
94 :    
95 :    
96 :     /*
97 : edgomez 408 * Calls the funcptr, and returns whether SIGILL (illegal instruction) was signalled
98 :     * Return values:
99 :     * -1 : could not determine
100 :     * 0 : SIGILL was *not* signalled
101 :     * 1 : SIGILL was signalled
102 :     */
103 : suxen_drol 311
104 :     int
105 :     sigill_check(void (*func)())
106 :     {
107 : edgomez 824 #ifdef _MSC_VER
108 : suxen_drol 311 _try {
109 :     func();
110 :     }
111 :     _except(EXCEPTION_EXECUTE_HANDLER) {
112 :    
113 :     if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
114 :     return 1;
115 :     }
116 :     return 0;
117 :     #else
118 :     void * old_handler;
119 :     int jmpret;
120 :    
121 :    
122 :     old_handler = signal(SIGILL, sigill_handler);
123 :     if (old_handler == SIG_ERR)
124 :     {
125 :     return -1;
126 :     }
127 :    
128 :     jmpret = setjmp(mark);
129 :     if (jmpret == 0)
130 :     {
131 :     func();
132 :     }
133 :    
134 :     signal(SIGILL, old_handler);
135 :    
136 :     return jmpret;
137 :     #endif
138 :     }
139 :     #endif
140 :    
141 : edgomez 200 /*****************************************************************************
142 :     * XviD Init Entry point
143 :     *
144 :     * Well this function initialize all internal function pointers according
145 :     * to the CPU features forced by the library client or autodetected (depending
146 :     * on the XVID_CPU_FORCE flag). It also initializes vlc coding tables and all
147 :     * image colorspace transformation tables.
148 :     *
149 :     * Returned value : XVID_ERR_OK
150 :     * + API_VERSION in the input XVID_INIT_PARAM structure
151 :     * + core build " " " " "
152 :     *
153 :     ****************************************************************************/
154 :    
155 : edgomez 195 int
156 :     xvid_init(void *handle,
157 :     int opt,
158 :     void *param1,
159 :     void *param2)
160 : Isibaar 3 {
161 :     int cpu_flags;
162 :     XVID_INIT_PARAM *init_param;
163 :    
164 :     init_param = (XVID_INIT_PARAM *) param1;
165 :    
166 : suxen_drol 234 /* Inform the client the API version */
167 :     init_param->api_version = API_VERSION;
168 :    
169 :     /* Inform the client the core build - unused because we're still alpha */
170 :     init_param->core_build = 1000;
171 :    
172 : suxen_drol 311 /* Do we have to force CPU features ? */
173 :     if ((init_param->cpu_flags & XVID_CPU_FORCE)) {
174 : suxen_drol 234
175 : Isibaar 3 cpu_flags = init_param->cpu_flags;
176 : suxen_drol 311
177 : edgomez 200 } else {
178 : Isibaar 3
179 : chenm001 274 cpu_flags = check_cpu_features();
180 : suxen_drol 311
181 : edgomez 824 #if defined(ARCH_IS_IA32) && defined(EXPERIMENTAL_SSE2_CODE)
182 : suxen_drol 311 if ((cpu_flags & XVID_CPU_SSE) && sigill_check(sse_os_trigger))
183 :     cpu_flags &= ~XVID_CPU_SSE;
184 :    
185 :     if ((cpu_flags & XVID_CPU_SSE2) && sigill_check(sse2_os_trigger))
186 :     cpu_flags &= ~XVID_CPU_SSE2;
187 :     #endif
188 :     }
189 :    
190 :     if ((init_param->cpu_flags & XVID_CPU_CHKONLY))
191 :     {
192 : Isibaar 3 init_param->cpu_flags = cpu_flags;
193 : suxen_drol 311 return XVID_ERR_OK;
194 : Isibaar 3 }
195 :    
196 : suxen_drol 311 init_param->cpu_flags = cpu_flags;
197 :    
198 :    
199 : edgomez 200 /* Initialize the function pointers */
200 : Isibaar 3 idct_int32_init();
201 : Isibaar 100 init_vlc_tables();
202 :    
203 : edgomez 200 /* Fixed Point Forward/Inverse DCT transformations */
204 : Isibaar 3 fdct = fdct_int32;
205 :     idct = idct_int32;
206 :    
207 : edgomez 200 /* Only needed on PPC Altivec archs */
208 : canard 115 sadInit = 0;
209 : edgomez 195
210 : edgomez 200 /* Restore FPU context : emms_c is a nop functions */
211 : Isibaar 3 emms = emms_c;
212 :    
213 : edgomez 200 /* Quantization functions */
214 :     quant_intra = quant_intra_c;
215 : Isibaar 3 dequant_intra = dequant_intra_c;
216 : edgomez 200 quant_inter = quant_inter_c;
217 : Isibaar 3 dequant_inter = dequant_inter_c;
218 :    
219 : edgomez 200 quant4_intra = quant4_intra_c;
220 : Isibaar 3 dequant4_intra = dequant4_intra_c;
221 : edgomez 200 quant4_inter = quant4_inter_c;
222 : Isibaar 3 dequant4_inter = dequant4_inter_c;
223 :    
224 : edgomez 200 /* Block transfer related functions */
225 : Isibaar 3 transfer_8to16copy = transfer_8to16copy_c;
226 :     transfer_16to8copy = transfer_16to8copy_c;
227 : edgomez 200 transfer_8to16sub = transfer_8to16sub_c;
228 : suxen_drol 118 transfer_8to16sub2 = transfer_8to16sub2_c;
229 : edgomez 200 transfer_16to8add = transfer_16to8add_c;
230 :     transfer8x8_copy = transfer8x8_copy_c;
231 : Isibaar 3
232 : edgomez 200 /* Image interpolation related functions */
233 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_c;
234 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_c;
235 : Isibaar 3 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_c;
236 :    
237 : edgomez 200 /* Initialize internal colorspace transformation tables */
238 : Isibaar 3 colorspace_init();
239 :    
240 : edgomez 200 /* All colorspace transformation functions User Format->YV12 */
241 : Isibaar 3 rgb555_to_yv12 = rgb555_to_yv12_c;
242 :     rgb565_to_yv12 = rgb565_to_yv12_c;
243 : edgomez 200 rgb24_to_yv12 = rgb24_to_yv12_c;
244 :     rgb32_to_yv12 = rgb32_to_yv12_c;
245 :     yuv_to_yv12 = yuv_to_yv12_c;
246 :     yuyv_to_yv12 = yuyv_to_yv12_c;
247 :     uyvy_to_yv12 = uyvy_to_yv12_c;
248 : Isibaar 3
249 : edgomez 200 /* All colorspace transformation functions YV12->User format */
250 : Isibaar 3 yv12_to_rgb555 = yv12_to_rgb555_c;
251 :     yv12_to_rgb565 = yv12_to_rgb565_c;
252 : edgomez 200 yv12_to_rgb24 = yv12_to_rgb24_c;
253 :     yv12_to_rgb32 = yv12_to_rgb32_c;
254 :     yv12_to_yuv = yv12_to_yuv_c;
255 :     yv12_to_yuyv = yv12_to_yuyv_c;
256 :     yv12_to_uyvy = yv12_to_uyvy_c;
257 : Isibaar 3
258 : edgomez 200 /* Functions used in motion estimation algorithms */
259 : Isibaar 3 calc_cbp = calc_cbp_c;
260 : edgomez 200 sad16 = sad16_c;
261 : suxen_drol 329 sad8 = sad8_c;
262 : edgomez 200 sad16bi = sad16bi_c;
263 : suxen_drol 329 sad8bi = sad8bi_c;
264 : edgomez 200 dev16 = dev16_c;
265 : suxen_drol 329
266 : ia64p 299 Halfpel8_Refine = Halfpel8_Refine_c;
267 : Isibaar 3
268 : edgomez 824 #ifdef ARCH_IS_IA32
269 : edgomez 195 if ((cpu_flags & XVID_CPU_MMX) > 0) {
270 : edgomez 200
271 :     /* Forward and Inverse Discrete Cosine Transformation functions */
272 : Isibaar 3 fdct = fdct_mmx;
273 :     idct = idct_mmx;
274 :    
275 : edgomez 200 /* To restore FPU context after mmx use */
276 : Isibaar 3 emms = emms_mmx;
277 :    
278 : edgomez 200 /* Quantization related functions */
279 :     quant_intra = quant_intra_mmx;
280 : Isibaar 3 dequant_intra = dequant_intra_mmx;
281 : edgomez 200 quant_inter = quant_inter_mmx;
282 : Isibaar 3 dequant_inter = dequant_inter_mmx;
283 :    
284 : edgomez 200 quant4_intra = quant4_intra_mmx;
285 : Isibaar 3 dequant4_intra = dequant4_intra_mmx;
286 : edgomez 200 quant4_inter = quant4_inter_mmx;
287 : Isibaar 3 dequant4_inter = dequant4_inter_mmx;
288 :    
289 : edgomez 200 /* Block related functions */
290 : Isibaar 3 transfer_8to16copy = transfer_8to16copy_mmx;
291 :     transfer_16to8copy = transfer_16to8copy_mmx;
292 : edgomez 200 transfer_8to16sub = transfer_8to16sub_mmx;
293 : edgomez 236 transfer_8to16sub2 = transfer_8to16sub2_mmx;
294 : edgomez 200 transfer_16to8add = transfer_16to8add_mmx;
295 :     transfer8x8_copy = transfer8x8_copy_mmx;
296 : Isibaar 3
297 : edgomez 236
298 : edgomez 200 /* Image Interpolation related functions */
299 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_mmx;
300 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_mmx;
301 : Isibaar 3 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_mmx;
302 :    
303 : edgomez 200 /* Image RGB->YV12 related functions */
304 : Isibaar 3 rgb24_to_yv12 = rgb24_to_yv12_mmx;
305 :     rgb32_to_yv12 = rgb32_to_yv12_mmx;
306 : edgomez 200 yuv_to_yv12 = yuv_to_yv12_mmx;
307 :     yuyv_to_yv12 = yuyv_to_yv12_mmx;
308 :     uyvy_to_yv12 = uyvy_to_yv12_mmx;
309 : Isibaar 3
310 : edgomez 200 /* Image YV12->RGB related functions */
311 : Isibaar 3 yv12_to_rgb24 = yv12_to_rgb24_mmx;
312 :     yv12_to_rgb32 = yv12_to_rgb32_mmx;
313 : edgomez 200 yv12_to_yuyv = yv12_to_yuyv_mmx;
314 :     yv12_to_uyvy = yv12_to_uyvy_mmx;
315 : Isibaar 3
316 : edgomez 200 /* Motion estimation related functions */
317 : Isibaar 3 calc_cbp = calc_cbp_mmx;
318 : edgomez 200 sad16 = sad16_mmx;
319 :     sad8 = sad8_mmx;
320 : suxen_drol 329 sad16bi = sad16bi_mmx;
321 :     sad8bi = sad8bi_mmx;
322 : edgomez 200 dev16 = dev16_mmx;
323 : Isibaar 3
324 :     }
325 :    
326 : suxen_drol 329 /* these 3dnow functions are faster than mmx, but slower than xmm. */
327 :     if ((cpu_flags & XVID_CPU_3DNOW) > 0) {
328 :    
329 :     /* ME functions */
330 :     sad16bi = sad16bi_3dn;
331 :     sad8bi = sad8bi_3dn;
332 :     }
333 :    
334 :    
335 : edgomez 195 if ((cpu_flags & XVID_CPU_MMXEXT) > 0) {
336 : edgomez 200
337 :     /* Inverse DCT */
338 : Isibaar 3 idct = idct_xmm;
339 : edgomez 200
340 :     /* Interpolation */
341 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_xmm;
342 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_xmm;
343 : h 38 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_xmm;
344 : edgomez 200
345 : chenm001 274 /* Quantization */
346 :     dequant_intra = dequant_intra_xmm;
347 :     dequant_inter = dequant_inter_xmm;
348 :    
349 : edgomez 218 /* Buffer transfer */
350 :     transfer_8to16sub2 = transfer_8to16sub2_xmm;
351 :    
352 : edgomez 200 /* Colorspace transformation */
353 : Isibaar 3 yuv_to_yv12 = yuv_to_yv12_xmm;
354 :    
355 : edgomez 200 /* ME functions */
356 : Isibaar 3 sad16 = sad16_xmm;
357 : suxen_drol 329 sad8 = sad8_xmm;
358 : chenm001 274 sad16bi = sad16bi_xmm;
359 : suxen_drol 329 sad8bi = sad8bi_xmm;
360 : Isibaar 3 dev16 = dev16_xmm;
361 :    
362 :     }
363 :    
364 : edgomez 195 if ((cpu_flags & XVID_CPU_3DNOW) > 0) {
365 : edgomez 200
366 :     /* Interpolation */
367 : Isibaar 3 interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_3dn;
368 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_3dn;
369 : h 40 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dn;
370 : Isibaar 3 }
371 :    
372 : edgomez 195 if ((cpu_flags & XVID_CPU_SSE2) > 0) {
373 : Isibaar 154 #ifdef EXPERIMENTAL_SSE2_CODE
374 : edgomez 200
375 : chenm001 274 calc_cbp = calc_cbp_sse2;
376 :    
377 : edgomez 200 /* Quantization */
378 :     quant_intra = quant_intra_sse2;
379 : Isibaar 154 dequant_intra = dequant_intra_sse2;
380 : edgomez 200 quant_inter = quant_inter_sse2;
381 : Isibaar 154 dequant_inter = dequant_inter_sse2;
382 : h 135
383 : edgomez 200 /* ME */
384 :     sad16 = sad16_sse2;
385 :     dev16 = dev16_sse2;
386 :    
387 :     /* Forward and Inverse DCT */
388 :     idct = idct_sse2;
389 : Isibaar 154 fdct = fdct_sse2;
390 :     #endif
391 : h 126 }
392 : edgomez 200
393 : Isibaar 3 #endif
394 : edgomez 200
395 : edgomez 824 #ifdef ARCH_IS_IA64
396 : Isibaar 209 if ((cpu_flags & XVID_CPU_IA64) > 0) { //use assembler routines?
397 :     idct_ia64_init();
398 :     fdct = fdct_ia64;
399 : ia64p 585 idct = idct_ia64;
400 : Isibaar 209 interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_ia64;
401 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_ia64;
402 :     interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_ia64;
403 :     sad16 = sad16_ia64;
404 :     sad16bi = sad16bi_ia64;
405 :     sad8 = sad8_ia64;
406 :     dev16 = dev16_ia64;
407 : ia64p 299 Halfpel8_Refine = Halfpel8_Refine_ia64;
408 : Isibaar 209 quant_intra = quant_intra_ia64;
409 :     dequant_intra = dequant_intra_ia64;
410 :     quant_inter = quant_inter_ia64;
411 :     dequant_inter = dequant_inter_ia64;
412 :     transfer_8to16copy = transfer_8to16copy_ia64;
413 :     transfer_16to8copy = transfer_16to8copy_ia64;
414 :     transfer_8to16sub = transfer_8to16sub_ia64;
415 :     transfer_8to16sub2 = transfer_8to16sub2_ia64;
416 :     transfer_16to8add = transfer_16to8add_ia64;
417 :     transfer8x8_copy = transfer8x8_copy_ia64;
418 : ia64p 585 // DEBUG("Using IA-64 assembler routines.\n");
419 : Isibaar 209 }
420 :     #endif
421 :    
422 : edgomez 824 #ifdef ARCH_IS_PPC
423 :     #ifdef ARCH_IS_PPC_ALTIVEC
424 : canard 71 calc_cbp = calc_cbp_altivec;
425 : canard 76 fdct = fdct_altivec;
426 :     idct = idct_altivec;
427 : canard 115 sadInit = sadInit_altivec;
428 : canard 89 sad16 = sad16_altivec;
429 :     sad8 = sad8_altivec;
430 :     dev16 = dev16_altivec;
431 : canard 71 #else
432 : canard 52 calc_cbp = calc_cbp_ppc;
433 :     #endif
434 : canard 71 #endif
435 : edgomez 195
436 : Isibaar 3 return XVID_ERR_OK;
437 :     }
438 :    
439 : edgomez 200 /*****************************************************************************
440 :     * XviD Native decoder entry point
441 :     *
442 :     * This function is just a wrapper to all the option cases.
443 :     *
444 :     * Returned values : XVID_ERR_FAIL when opt is invalid
445 :     * else returns the wrapped function result
446 :     *
447 :     ****************************************************************************/
448 :    
449 : edgomez 195 int
450 :     xvid_decore(void *handle,
451 :     int opt,
452 :     void *param1,
453 :     void *param2)
454 : Isibaar 3 {
455 : edgomez 195 switch (opt) {
456 :     case XVID_DEC_DECODE:
457 :     return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1);
458 : Isibaar 3
459 : edgomez 195 case XVID_DEC_CREATE:
460 : chenm001 274 return decoder_create((XVID_DEC_PARAM *) param1);
461 : Isibaar 3
462 : edgomez 195 case XVID_DEC_DESTROY:
463 :     return decoder_destroy((DECODER *) handle);
464 :    
465 : Isibaar 3 default:
466 : edgomez 195 return XVID_ERR_FAIL;
467 :     }
468 : Isibaar 3 }
469 :    
470 :    
471 : edgomez 200 /*****************************************************************************
472 :     * XviD Native encoder entry point
473 :     *
474 :     * This function is just a wrapper to all the option cases.
475 :     *
476 :     * Returned values : XVID_ERR_FAIL when opt is invalid
477 :     * else returns the wrapped function result
478 :     *
479 :     ****************************************************************************/
480 :    
481 : edgomez 195 int
482 :     xvid_encore(void *handle,
483 :     int opt,
484 :     void *param1,
485 :     void *param2)
486 : Isibaar 3 {
487 : edgomez 195 switch (opt) {
488 :     case XVID_ENC_ENCODE:
489 :     return encoder_encode((Encoder *) handle, (XVID_ENC_FRAME *) param1,
490 :     (XVID_ENC_STATS *) param2);
491 : Isibaar 3
492 : edgomez 195 case XVID_ENC_CREATE:
493 :     return encoder_create((XVID_ENC_PARAM *) param1);
494 : Isibaar 3
495 : edgomez 195 case XVID_ENC_DESTROY:
496 :     return encoder_destroy((Encoder *) handle);
497 :    
498 : Isibaar 3 default:
499 : edgomez 195 return XVID_ERR_FAIL;
500 :     }
501 : Isibaar 3 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4