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

Annotation of /xvidcore/src/xvid.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (view) (download)

1 : edgomez 1.16 /*****************************************************************************
2 : edgomez 1.17 *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Native API implementation -
5 :     *
6 : suxen_drol 1.37 * Copyright(C) 2001-2002 Peter Ross <pross@xvid.org>
7 : edgomez 1.34 *
8 : edgomez 1.39 * This file is part of XviD, a free MPEG-4 video encoder/decoder
9 :     *
10 :     * 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 1.17 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 1.39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : edgomez 1.17 * 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 1.39 * along with this program; if not, write to the Free Software
22 : edgomez 1.17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 1.39 * Under section 8 of the GNU General Public License, the copyright
25 :     * holders of XVID explicitly forbid distribution in the following
26 :     * countries:
27 :     *
28 :     * - 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 :     * $Id$
54 : edgomez 1.36 *
55 : edgomez 1.17 ****************************************************************************/
56 : chenm001 1.29
57 : Isibaar 1.1 #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 1.30 #include "motion/motion.h"
69 : Isibaar 1.1 #include "motion/sad.h"
70 :     #include "utils/emms.h"
71 :     #include "utils/timer.h"
72 : Isibaar 1.9 #include "bitstream/mbcoding.h"
73 : Isibaar 1.1
74 : suxen_drol 1.31 #if defined(ARCH_X86) && defined(EXPERIMENTAL_SSE2_CODE)
75 :    
76 :     #ifdef WIN32
77 :     #include <windows.h>
78 :     #else
79 :     #include <signal.h>
80 :     #include <setjmp.h>
81 :     #endif
82 :    
83 :    
84 :     #ifndef WIN32
85 :    
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 1.34 * 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 1.31
104 :     int
105 :     sigill_check(void (*func)())
106 :     {
107 :     #ifdef WIN32
108 :     _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 1.16 /*****************************************************************************
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 1.15 int
156 :     xvid_init(void *handle,
157 :     int opt,
158 :     void *param1,
159 :     void *param2)
160 : Isibaar 1.1 {
161 :     int cpu_flags;
162 :     XVID_INIT_PARAM *init_param;
163 :    
164 :     init_param = (XVID_INIT_PARAM *) param1;
165 :    
166 : suxen_drol 1.21 /* 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 1.31 /* Do we have to force CPU features ? */
173 :     if ((init_param->cpu_flags & XVID_CPU_FORCE)) {
174 : suxen_drol 1.21
175 : Isibaar 1.1 cpu_flags = init_param->cpu_flags;
176 : suxen_drol 1.31
177 : edgomez 1.16 } else {
178 : Isibaar 1.1
179 : chenm001 1.29 cpu_flags = check_cpu_features();
180 : suxen_drol 1.31
181 :     #if defined(ARCH_X86) && defined(EXPERIMENTAL_SSE2_CODE)
182 :     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 1.1 init_param->cpu_flags = cpu_flags;
193 : suxen_drol 1.31 return XVID_ERR_OK;
194 : Isibaar 1.1 }
195 : suxen_drol 1.31
196 :     init_param->cpu_flags = cpu_flags;
197 :    
198 : Isibaar 1.1
199 : edgomez 1.16 /* Initialize the function pointers */
200 : Isibaar 1.1 idct_int32_init();
201 : Isibaar 1.9 init_vlc_tables();
202 :    
203 : edgomez 1.16 /* Fixed Point Forward/Inverse DCT transformations */
204 : Isibaar 1.1 fdct = fdct_int32;
205 :     idct = idct_int32;
206 :    
207 : edgomez 1.16 /* Only needed on PPC Altivec archs */
208 : canard 1.10 sadInit = 0;
209 : edgomez 1.15
210 : edgomez 1.16 /* Restore FPU context : emms_c is a nop functions */
211 : Isibaar 1.1 emms = emms_c;
212 :    
213 : edgomez 1.16 /* Quantization functions */
214 :     quant_intra = quant_intra_c;
215 : Isibaar 1.1 dequant_intra = dequant_intra_c;
216 : edgomez 1.16 quant_inter = quant_inter_c;
217 : Isibaar 1.1 dequant_inter = dequant_inter_c;
218 :    
219 : edgomez 1.16 quant4_intra = quant4_intra_c;
220 : Isibaar 1.1 dequant4_intra = dequant4_intra_c;
221 : edgomez 1.16 quant4_inter = quant4_inter_c;
222 : Isibaar 1.1 dequant4_inter = dequant4_inter_c;
223 :    
224 : edgomez 1.16 /* Block transfer related functions */
225 : Isibaar 1.1 transfer_8to16copy = transfer_8to16copy_c;
226 :     transfer_16to8copy = transfer_16to8copy_c;
227 : edgomez 1.16 transfer_8to16sub = transfer_8to16sub_c;
228 : suxen_drol 1.11 transfer_8to16sub2 = transfer_8to16sub2_c;
229 : edgomez 1.16 transfer_16to8add = transfer_16to8add_c;
230 :     transfer8x8_copy = transfer8x8_copy_c;
231 : Isibaar 1.1
232 : edgomez 1.16 /* Image interpolation related functions */
233 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_c;
234 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_c;
235 : Isibaar 1.1 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_c;
236 :    
237 : edgomez 1.16 /* Initialize internal colorspace transformation tables */
238 : Isibaar 1.1 colorspace_init();
239 :    
240 : edgomez 1.16 /* All colorspace transformation functions User Format->YV12 */
241 : Isibaar 1.1 rgb555_to_yv12 = rgb555_to_yv12_c;
242 :     rgb565_to_yv12 = rgb565_to_yv12_c;
243 : edgomez 1.16 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 1.1
249 : edgomez 1.16 /* All colorspace transformation functions YV12->User format */
250 : Isibaar 1.1 yv12_to_rgb555 = yv12_to_rgb555_c;
251 :     yv12_to_rgb565 = yv12_to_rgb565_c;
252 : edgomez 1.16 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 1.1
258 : edgomez 1.16 /* Functions used in motion estimation algorithms */
259 : Isibaar 1.1 calc_cbp = calc_cbp_c;
260 : edgomez 1.16 sad16 = sad16_c;
261 : suxen_drol 1.33 sad8 = sad8_c;
262 : edgomez 1.16 sad16bi = sad16bi_c;
263 : suxen_drol 1.33 sad8bi = sad8bi_c;
264 : edgomez 1.16 dev16 = dev16_c;
265 : suxen_drol 1.33
266 : ia64p 1.30 Halfpel8_Refine = Halfpel8_Refine_c;
267 : Isibaar 1.1
268 :     #ifdef ARCH_X86
269 : edgomez 1.15 if ((cpu_flags & XVID_CPU_MMX) > 0) {
270 : edgomez 1.16
271 :     /* Forward and Inverse Discrete Cosine Transformation functions */
272 : Isibaar 1.1 fdct = fdct_mmx;
273 :     idct = idct_mmx;
274 :    
275 : edgomez 1.16 /* To restore FPU context after mmx use */
276 : Isibaar 1.1 emms = emms_mmx;
277 :    
278 : edgomez 1.16 /* Quantization related functions */
279 :     quant_intra = quant_intra_mmx;
280 : Isibaar 1.1 dequant_intra = dequant_intra_mmx;
281 : edgomez 1.16 quant_inter = quant_inter_mmx;
282 : Isibaar 1.1 dequant_inter = dequant_inter_mmx;
283 :    
284 : edgomez 1.16 quant4_intra = quant4_intra_mmx;
285 : Isibaar 1.1 dequant4_intra = dequant4_intra_mmx;
286 : edgomez 1.16 quant4_inter = quant4_inter_mmx;
287 : Isibaar 1.1 dequant4_inter = dequant4_inter_mmx;
288 :    
289 : edgomez 1.16 /* Block related functions */
290 : Isibaar 1.1 transfer_8to16copy = transfer_8to16copy_mmx;
291 :     transfer_16to8copy = transfer_16to8copy_mmx;
292 : edgomez 1.16 transfer_8to16sub = transfer_8to16sub_mmx;
293 : edgomez 1.22 transfer_8to16sub2 = transfer_8to16sub2_mmx;
294 : edgomez 1.16 transfer_16to8add = transfer_16to8add_mmx;
295 :     transfer8x8_copy = transfer8x8_copy_mmx;
296 : edgomez 1.22
297 : edgomez 1.16
298 :     /* Image Interpolation related functions */
299 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_mmx;
300 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_mmx;
301 : Isibaar 1.1 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_mmx;
302 :    
303 : edgomez 1.16 /* Image RGB->YV12 related functions */
304 : Isibaar 1.1 rgb24_to_yv12 = rgb24_to_yv12_mmx;
305 :     rgb32_to_yv12 = rgb32_to_yv12_mmx;
306 : edgomez 1.16 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 1.1
310 : edgomez 1.16 /* Image YV12->RGB related functions */
311 : Isibaar 1.1 yv12_to_rgb24 = yv12_to_rgb24_mmx;
312 :     yv12_to_rgb32 = yv12_to_rgb32_mmx;
313 : edgomez 1.16 yv12_to_yuyv = yv12_to_yuyv_mmx;
314 :     yv12_to_uyvy = yv12_to_uyvy_mmx;
315 : Isibaar 1.1
316 : edgomez 1.16 /* Motion estimation related functions */
317 : Isibaar 1.1 calc_cbp = calc_cbp_mmx;
318 : edgomez 1.16 sad16 = sad16_mmx;
319 :     sad8 = sad8_mmx;
320 : suxen_drol 1.33 sad16bi = sad16bi_mmx;
321 :     sad8bi = sad8bi_mmx;
322 : edgomez 1.16 dev16 = dev16_mmx;
323 : Isibaar 1.1
324 :     }
325 :    
326 : suxen_drol 1.33 /* 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 1.15 if ((cpu_flags & XVID_CPU_MMXEXT) > 0) {
336 : edgomez 1.16
337 :     /* Inverse DCT */
338 : Isibaar 1.1 idct = idct_xmm;
339 : edgomez 1.16
340 :     /* Interpolation */
341 :     interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_xmm;
342 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_xmm;
343 : h 1.3 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_xmm;
344 : Isibaar 1.25
345 : chenm001 1.29 /* Quantization */
346 :     dequant_intra = dequant_intra_xmm;
347 :     dequant_inter = dequant_inter_xmm;
348 :    
349 : edgomez 1.19 /* Buffer transfer */
350 :     transfer_8to16sub2 = transfer_8to16sub2_xmm;
351 : edgomez 1.16
352 :     /* Colorspace transformation */
353 : Isibaar 1.1 yuv_to_yv12 = yuv_to_yv12_xmm;
354 :    
355 : edgomez 1.16 /* ME functions */
356 : Isibaar 1.1 sad16 = sad16_xmm;
357 : suxen_drol 1.33 sad8 = sad8_xmm;
358 : chenm001 1.29 sad16bi = sad16bi_xmm;
359 : suxen_drol 1.33 sad8bi = sad8bi_xmm;
360 : Isibaar 1.1 dev16 = dev16_xmm;
361 :    
362 :     }
363 :    
364 : edgomez 1.15 if ((cpu_flags & XVID_CPU_3DNOW) > 0) {
365 : edgomez 1.16
366 :     /* Interpolation */
367 : Isibaar 1.1 interpolate8x8_halfpel_h = interpolate8x8_halfpel_h_3dn;
368 :     interpolate8x8_halfpel_v = interpolate8x8_halfpel_v_3dn;
369 : h 1.4 interpolate8x8_halfpel_hv = interpolate8x8_halfpel_hv_3dn;
370 : Isibaar 1.1 }
371 :    
372 : edgomez 1.15 if ((cpu_flags & XVID_CPU_SSE2) > 0) {
373 : Isibaar 1.14 #ifdef EXPERIMENTAL_SSE2_CODE
374 : edgomez 1.16
375 : chenm001 1.29 calc_cbp = calc_cbp_sse2;
376 :    
377 : edgomez 1.16 /* Quantization */
378 :     quant_intra = quant_intra_sse2;
379 : Isibaar 1.14 dequant_intra = dequant_intra_sse2;
380 : edgomez 1.16 quant_inter = quant_inter_sse2;
381 : Isibaar 1.14 dequant_inter = dequant_inter_sse2;
382 : h 1.13
383 : edgomez 1.16 /* ME */
384 :     sad16 = sad16_sse2;
385 :     dev16 = dev16_sse2;
386 :    
387 :     /* Forward and Inverse DCT */
388 :     idct = idct_sse2;
389 : Isibaar 1.14 fdct = fdct_sse2;
390 :     #endif
391 : h 1.12 }
392 : edgomez 1.16
393 : Isibaar 1.1 #endif
394 : Isibaar 1.18
395 :     #ifdef ARCH_IA64
396 :     if ((cpu_flags & XVID_CPU_IA64) > 0) { //use assembler routines?
397 :     idct_ia64_init();
398 :     fdct = fdct_ia64;
399 : ia64p 1.38 idct = idct_ia64;
400 : Isibaar 1.18 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 1.30 Halfpel8_Refine = Halfpel8_Refine_ia64;
408 : Isibaar 1.18 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 1.38 // DEBUG("Using IA-64 assembler routines.\n");
419 : Isibaar 1.18 }
420 :     #endif
421 : edgomez 1.16
422 : canard 1.5 #ifdef ARCH_PPC
423 : canard 1.6 #ifdef ARCH_PPC_ALTIVEC
424 :     calc_cbp = calc_cbp_altivec;
425 : canard 1.7 fdct = fdct_altivec;
426 :     idct = idct_altivec;
427 : canard 1.10 sadInit = sadInit_altivec;
428 : canard 1.8 sad16 = sad16_altivec;
429 :     sad8 = sad8_altivec;
430 :     dev16 = dev16_altivec;
431 : canard 1.6 #else
432 : canard 1.5 calc_cbp = calc_cbp_ppc;
433 : canard 1.6 #endif
434 : canard 1.5 #endif
435 : Isibaar 1.1
436 :     return XVID_ERR_OK;
437 :     }
438 :    
439 : edgomez 1.16 /*****************************************************************************
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 1.15 int
450 :     xvid_decore(void *handle,
451 :     int opt,
452 :     void *param1,
453 :     void *param2)
454 : Isibaar 1.1 {
455 : edgomez 1.15 switch (opt) {
456 :     case XVID_DEC_DECODE:
457 :     return decoder_decode((DECODER *) handle, (XVID_DEC_FRAME *) param1);
458 :    
459 :     case XVID_DEC_CREATE:
460 : chenm001 1.29 return decoder_create((XVID_DEC_PARAM *) param1);
461 : edgomez 1.15
462 :     case XVID_DEC_DESTROY:
463 :     return decoder_destroy((DECODER *) handle);
464 : Isibaar 1.1
465 :     default:
466 : edgomez 1.15 return XVID_ERR_FAIL;
467 :     }
468 : Isibaar 1.1 }
469 :    
470 : edgomez 1.16
471 :     /*****************************************************************************
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 : Isibaar 1.1
481 : edgomez 1.15 int
482 :     xvid_encore(void *handle,
483 :     int opt,
484 :     void *param1,
485 :     void *param2)
486 : Isibaar 1.1 {
487 : edgomez 1.15 switch (opt) {
488 :     case XVID_ENC_ENCODE:
489 :     return encoder_encode((Encoder *) handle, (XVID_ENC_FRAME *) param1,
490 :     (XVID_ENC_STATS *) param2);
491 :    
492 :     case XVID_ENC_CREATE:
493 :     return encoder_create((XVID_ENC_PARAM *) param1);
494 :    
495 :     case XVID_ENC_DESTROY:
496 :     return encoder_destroy((Encoder *) handle);
497 : Isibaar 1.1
498 :     default:
499 : edgomez 1.15 return XVID_ERR_FAIL;
500 :     }
501 : Isibaar 1.1 }

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