[svn] / branches / dev-api-3 / xvidcore / src / xvid.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/src/xvid.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 540 - (view) (download)

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

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