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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 144 - (view) (download)

1 : Isibaar 3 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 143 * OpenDivx API wrapper
5 : Isibaar 3 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 :     /**************************************************************************
32 :     *
33 :     * History:
34 :     *
35 :     * 26.02.2001 fixed dec_csp bugs
36 :     * 26.12.2001 xvid_init() support
37 :     * 22.12.2001 removed some compiler warnings
38 :     * 16.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
39 :     *
40 : edgomez 144 * $Id: divx4.c,v 1.10 2002-04-28 20:03:14 edgomez Exp $
41 :     *
42 : Isibaar 3 *************************************************************************/
43 :    
44 : knhor 129 #include <stdlib.h>
45 : edgomez 143 #include <string.h>
46 : Isibaar 3
47 :     #include "xvid.h"
48 :     #include "divx4.h"
49 :     #include "decoder.h"
50 :     #include "encoder.h"
51 :    
52 :     #define EMULATED_DIVX_VERSION 20011001
53 :    
54 : edgomez 143 /**************************************************************************
55 :     * Divx Instance Structure
56 :     *
57 :     * This chain list datatype allows XviD do instanciate multiples divx4
58 :     * sessions.
59 :     *
60 :     * ToDo : The way this chain list is used does not guarantee reentrance
61 :     * because they are not protected by any kind of mutex to allow
62 :     * only one modifier. We should add a mutex for each element in
63 :     * the chainlist.
64 :     *************************************************************************/
65 : Isibaar 3
66 :    
67 :     typedef struct DINST
68 :     {
69 :     unsigned long key;
70 :     struct DINST * next;
71 :    
72 :     void * handle;
73 :     XVID_DEC_FRAME xframe;
74 :    
75 :     } DINST;
76 :    
77 : edgomez 143 /**************************************************************************
78 :     * Global data (needed to emulate correctly exported symbols from divx4)
79 :     *************************************************************************/
80 : Isibaar 3
81 : edgomez 143 /* This is not used in this module but is required by some divx4 encoders*/
82 :     int quiet_encore = 1;
83 : Isibaar 3
84 : edgomez 143 /**************************************************************************
85 :     * Local data
86 :     *************************************************************************/
87 : Isibaar 3
88 : edgomez 143 /* The Divx4 instance chainlist */
89 :     static DINST * dhead = NULL;
90 : Isibaar 3
91 : edgomez 143 /* Divx4 quality to XviD encoder motion flag presets */
92 :     static int const divx4_motion_presets[7] = {
93 :     0,
94 : Isibaar 3
95 : edgomez 143 PMV_QUICKSTOP16,
96 : Isibaar 3
97 : edgomez 143 PMV_EARLYSTOP16,
98 : Isibaar 3
99 : edgomez 143 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
100 : Isibaar 3
101 : edgomez 143 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
102 :     PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
103 : Isibaar 3
104 : edgomez 143 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
105 :     PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
106 : Isibaar 3
107 : edgomez 143 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
108 :     PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
109 :     PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8
110 :     };
111 : Isibaar 3
112 :    
113 : edgomez 143 /* Divx4 quality to general encoder flag presets */
114 :     static int const divx4_general_presets[7] = {
115 :     0,
116 :     XVID_H263QUANT,
117 :     XVID_H263QUANT,
118 :     XVID_H263QUANT | XVID_HALFPEL,
119 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
120 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
121 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL
122 :     };
123 : Isibaar 3
124 : edgomez 143 /*
125 :     * Current divx4 encoder quality
126 :     * ToDo : this data should not be shared between encoder instances
127 :     */
128 :     static int quality;
129 : Isibaar 3
130 : edgomez 143 /**************************************************************************
131 :     * Local Prototypes
132 :     *************************************************************************/
133 : Isibaar 3
134 : edgomez 143 /* Chain list helper functions */
135 :     static DINST * dinst_find(unsigned long key);
136 :     static DINST * dinst_add(unsigned long key);
137 :     static void dinst_remove(unsigned long key);
138 : Isibaar 3
139 : edgomez 143 /* Converts divx4 colorspaces codes to xvid codes */
140 :     static int xvid_to_opendivx_dec_csp(int csp);
141 :     static int xvid_to_opendivx_enc_csp(int csp);
142 : Isibaar 3
143 : edgomez 143 /**************************************************************************
144 :     * decore part
145 :     *
146 :     * decore is the divx4 entry point used to decompress the mpeg4 bitstream
147 :     * into a user defined image format.
148 :     *************************************************************************/
149 : Isibaar 3
150 : edgomez 143 int
151 :     decore(unsigned long key, unsigned long opt, void * param1, void * param2)
152 : Isibaar 3 {
153 : edgomez 143
154 : Isibaar 3 int xerr;
155 :    
156 : edgomez 143 switch (opt) {
157 :    
158 :     case DEC_OPT_MEMORY_REQS :
159 : Isibaar 3 {
160 : edgomez 143 memset(param2, 0, sizeof(DEC_MEM_REQS));
161 :     return DEC_OK;
162 :     }
163 :    
164 :     case DEC_OPT_INIT :
165 :     {
166 :     XVID_INIT_PARAM xinit;
167 :     XVID_DEC_PARAM xparam;
168 :     DINST * dcur;
169 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
170 :    
171 :     /* Find the divx4 instance */
172 :     if ((dcur = dinst_find(key)) == NULL)
173 : Isibaar 3 {
174 : edgomez 143 dcur = dinst_add(key);
175 : Isibaar 3 }
176 :    
177 : edgomez 143 /*
178 :     * XviD initialization
179 :     * XviD will detect the host cpu type and activate optimized
180 :     * functions according to the host cpu features.
181 :     */
182 :     xinit.cpu_flags = 0;
183 :     xvid_init(NULL, 0, &xinit, NULL);
184 : Isibaar 3
185 : edgomez 143 /* XviD decoder initialization for this instance */
186 :     xparam.width = dparam->x_dim;
187 :     xparam.height = dparam->y_dim;
188 :     dcur->xframe.colorspace =
189 :     xvid_to_opendivx_dec_csp(dparam->output_format);
190 : Isibaar 3
191 : edgomez 143 xerr = decoder_create(&xparam);
192 : Isibaar 3
193 : edgomez 143 /* Store the xvid handle into the divx4 instance chainlist */
194 :     dcur->handle = xparam.handle;
195 : Isibaar 3
196 : edgomez 143 break;
197 :     }
198 : Isibaar 3
199 :     case DEC_OPT_RELEASE :
200 : edgomez 143 {
201 :     DINST * dcur;
202 :    
203 :     /* Find the divx4 instance into the chain list */
204 :     if ((dcur = dinst_find(key)) == NULL)
205 : Isibaar 3 {
206 : edgomez 143 return DEC_EXIT;
207 :     }
208 : Isibaar 3
209 : edgomez 143 /* Destroy the XviD decoder attached to this divx4 instance */
210 :     xerr = decoder_destroy(dcur->handle);
211 : Isibaar 3
212 : edgomez 143 /* Remove the divx4 instance from the chainlist */
213 :     dinst_remove(key);
214 : Isibaar 3
215 : edgomez 143 break;
216 :     }
217 : Isibaar 3
218 :     case DEC_OPT_SETPP :
219 : edgomez 143 {
220 :     DINST * dcur;
221 :    
222 :     /* Find the divx4 instance into the chain list */
223 :     if ((dcur = dinst_find(key)) == NULL)
224 : Isibaar 3 {
225 : edgomez 143 return DEC_EXIT;
226 :     }
227 : Isibaar 3
228 : edgomez 143 /*
229 :     * We return DEC_OK but XviD has no postprocessing implemented
230 :     * in core.
231 :     */
232 :     return DEC_OK;
233 :     }
234 : Isibaar 3
235 : edgomez 143 case DEC_OPT_SETOUT :
236 :     {
237 :     DINST * dcur;
238 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
239 : Isibaar 3
240 : edgomez 143 if ((dcur = dinst_find(key)) == NULL)
241 : Isibaar 3 {
242 : edgomez 143 return DEC_EXIT;
243 :     }
244 : Isibaar 3
245 : edgomez 143 /* Change the output colorspace */
246 :     dcur->xframe.colorspace =
247 :     xvid_to_opendivx_dec_csp(dparam->output_format);
248 : Isibaar 3
249 : edgomez 143 return DEC_OK;
250 :     }
251 : Isibaar 3
252 :     case DEC_OPT_FRAME:
253 : edgomez 143 {
254 :     int csp_tmp = 0;
255 :     DINST * dcur;
256 :     DEC_FRAME * dframe = (DEC_FRAME *)param1;
257 :    
258 :     if ((dcur = dinst_find(key)) == NULL)
259 : Isibaar 3 {
260 : edgomez 143 return DEC_EXIT;
261 :     }
262 : edgomez 19
263 : edgomez 143 /* Copy the divx4 fields to the XviD decoder structure */
264 :     dcur->xframe.bitstream = dframe->bitstream;
265 :     dcur->xframe.length = dframe->length;
266 :     dcur->xframe.image = dframe->bmp;
267 :     dcur->xframe.stride = dframe->stride;
268 : Isibaar 3
269 : edgomez 143 /* Does the frame need to be skipped ? */
270 :     if (!dframe->render_flag)
271 :     {
272 :     /*
273 :     * Then we use the null colorspace to force XviD to
274 :     * skip the frame. The original colorspace will be
275 :     * restored after the decoder call
276 :     */
277 :     csp_tmp = dcur->xframe.colorspace;
278 :     dcur->xframe.colorspace = XVID_CSP_NULL;
279 :     }
280 : Isibaar 3
281 : edgomez 143 /* Decode the bitstream */
282 :     xerr = decoder_decode(dcur->handle, &dcur->xframe);
283 : Isibaar 3
284 : edgomez 143 /* Restore the real colorspace for this instance */
285 :     if (!dframe->render_flag)
286 :     {
287 :     dcur->xframe.colorspace = csp_tmp;
288 : Isibaar 3 }
289 :    
290 : edgomez 143 break;
291 :     }
292 : Isibaar 3
293 :     case DEC_OPT_FRAME_311 :
294 : edgomez 143 /* XviD does not handle Divx ;-) 3.11 yet */
295 : Isibaar 3 return DEC_EXIT;
296 :    
297 :     case DEC_OPT_VERSION:
298 :     return EMULATED_DIVX_VERSION;
299 : edgomez 143
300 : Isibaar 3 default :
301 :     return DEC_EXIT;
302 :     }
303 :    
304 :    
305 : edgomez 143 /* XviD error code -> Divx4 */
306 : Isibaar 3 switch(xerr)
307 :     {
308 : edgomez 143 case XVID_ERR_OK :
309 :     return DEC_OK;
310 :     case XVID_ERR_MEMORY :
311 :     return DEC_MEMORY;
312 :     case XVID_ERR_FORMAT :
313 :     return DEC_BAD_FORMAT;
314 :     default :
315 : Isibaar 3 return DEC_EXIT;
316 :     }
317 :     }
318 :    
319 : edgomez 143 /**************************************************************************
320 :     * Encore Part
321 :     *
322 :     * encore is the divx4 entry point used to compress a frame to a mpeg4
323 :     * bitstream.
324 :     *************************************************************************/
325 : Isibaar 3
326 :     #define FRAMERATE_INCR 1001
327 :    
328 : edgomez 143 int
329 :     encore(void * handle, int opt, void * param1, void * param2)
330 :     {
331 : Isibaar 9
332 : Isibaar 3 int xerr;
333 :    
334 : edgomez 143 switch(opt) {
335 :     case ENC_OPT_INIT :
336 : Isibaar 3 {
337 : edgomez 143 ENC_PARAM * eparam = (ENC_PARAM *)param1;
338 :     XVID_INIT_PARAM xinit;
339 :     XVID_ENC_PARAM xparam;
340 : Isibaar 3
341 : edgomez 143 /* Init XviD which will detect host cpu features */
342 :     xinit.cpu_flags = 0;
343 :     xvid_init(NULL, 0, &xinit, NULL);
344 : Isibaar 3
345 : edgomez 143 /* Settings are copied to the XviD encoder structure */
346 :     xparam.width = eparam->x_dim;
347 :     xparam.height = eparam->y_dim;
348 :     if ((eparam->framerate - (int)eparam->framerate) == 0)
349 :     {
350 :     xparam.fincr = 1;
351 :     xparam.fbase = (int)eparam->framerate;
352 :     }
353 :     else
354 :     {
355 :     xparam.fincr = FRAMERATE_INCR;
356 :     xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
357 :     }
358 :     xparam.rc_bitrate = eparam->bitrate;
359 :     xparam.rc_reaction_delay_factor = 16;
360 :     xparam.rc_averaging_period = 100;
361 :     xparam.rc_buffer = 100;
362 :     xparam.min_quantizer = eparam->min_quantizer;
363 :     xparam.max_quantizer = eparam->max_quantizer;
364 :     xparam.max_key_interval = eparam->max_key_interval;
365 :     quality = eparam->quality;
366 : Isibaar 3
367 : edgomez 143 /* Create the encoder session */
368 :     xerr = encoder_create(&xparam);
369 : Isibaar 3
370 : edgomez 143 eparam->handle = xparam.handle;
371 : Isibaar 3
372 : edgomez 143 break;
373 :     }
374 : Isibaar 3
375 :     case ENC_OPT_RELEASE :
376 : edgomez 143 {
377 :     xerr = encoder_destroy((Encoder *) handle);
378 :     break;
379 :     }
380 : Isibaar 3
381 :     case ENC_OPT_ENCODE :
382 :     case ENC_OPT_ENCODE_VBR :
383 : edgomez 143 {
384 :     ENC_FRAME * eframe = (ENC_FRAME *)param1;
385 :     ENC_RESULT * eresult = (ENC_RESULT *)param2;
386 :     XVID_ENC_FRAME xframe;
387 :     XVID_ENC_STATS xstats;
388 : Isibaar 3
389 : edgomez 143 /* Copy the divx4 info into the xvid structure */
390 :     xframe.bitstream = eframe->bitstream;
391 :     xframe.length = eframe->length;
392 : Isibaar 3
393 : edgomez 143 xframe.motion = divx4_motion_presets[quality];
394 :     xframe.general = divx4_general_presets[quality];
395 : Isibaar 9
396 : edgomez 143 xframe.image = eframe->image;
397 :     xframe.colorspace =
398 :     xvid_to_opendivx_enc_csp(eframe->colorspace);
399 : Isibaar 3
400 : edgomez 143 if (opt == ENC_OPT_ENCODE_VBR)
401 :     {
402 :     xframe.intra = eframe->intra;
403 :     xframe.quant = eframe->quant;
404 :     }
405 :     else
406 :     {
407 :     xframe.intra = -1;
408 :     xframe.quant = 0;
409 :     }
410 : Isibaar 3
411 : edgomez 143 /* Encode the frame */
412 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
413 : Isibaar 3
414 : edgomez 143 /* Copy back the xvid structure to the divx4 one */
415 :     if (eresult)
416 :     {
417 :     eresult->is_key_frame = xframe.intra;
418 :     eresult->quantizer = xstats.quant;
419 :     eresult->total_bits = xframe.length * 8;
420 :     eresult->motion_bits = xstats.hlength * 8;
421 :     eresult->texture_bits = eresult->total_bits - eresult->motion_bits;
422 :     }
423 : Isibaar 3
424 : edgomez 143 eframe->length = xframe.length;
425 : Isibaar 3
426 : edgomez 143 break;
427 :     }
428 : Isibaar 3
429 :     default:
430 :     return ENC_FAIL;
431 :     }
432 :    
433 : edgomez 143 /* XviD Error code -> Divx4 error code */
434 : Isibaar 3 switch(xerr)
435 :     {
436 : edgomez 143 case XVID_ERR_OK :
437 :     return ENC_OK;
438 :     case XVID_ERR_MEMORY :
439 :     return ENC_MEMORY;
440 :     case XVID_ERR_FORMAT :
441 :     return ENC_BAD_FORMAT;
442 :     default :
443 : Isibaar 3 return ENC_FAIL;
444 :     }
445 :     }
446 :    
447 : edgomez 143 /**************************************************************************
448 :     * Local Functions
449 :     *************************************************************************/
450 :    
451 :     /***************************************
452 :     * DINST chainlist helper functions *
453 :     ***************************************/
454 :    
455 :     /* Find an element in the chainlist according to its key value */
456 :     static DINST * dinst_find(unsigned long key)
457 :     {
458 :     DINST * dcur = dhead;
459 :    
460 :     while (dcur)
461 :     {
462 :     if (dcur->key == key)
463 :     {
464 :     return dcur;
465 :     }
466 :     dcur = dcur->next;
467 :     }
468 :    
469 :     return NULL;
470 :     }
471 :    
472 :    
473 :     /* Add an element to the chainlist */
474 :     static DINST * dinst_add(unsigned long key)
475 :     {
476 :     DINST * dnext = dhead;
477 :    
478 :     dhead = malloc(sizeof(DINST));
479 :     if (dhead == NULL)
480 :     {
481 :     dhead = dnext;
482 :     return NULL;
483 :     }
484 :    
485 :     dhead->key = key;
486 :     dhead->next = dnext;
487 :    
488 :     return dhead;
489 :     }
490 :    
491 :    
492 :     /* Remove an elmement from the chainlist */
493 :     static void dinst_remove(unsigned long key)
494 :     {
495 :     DINST * dcur = dhead;
496 :    
497 :     if (dhead == NULL)
498 :     {
499 :     return;
500 :     }
501 :    
502 :     if (dcur->key == key)
503 :     {
504 :     dhead = dcur->next;
505 :     free(dcur);
506 :     return;
507 :     }
508 :    
509 :     while (dcur->next)
510 :     {
511 :     if (dcur->next->key == key)
512 :     {
513 :     DINST * tmp = dcur->next;
514 :     dcur->next = tmp->next;
515 :     free(tmp);
516 :     return;
517 :     }
518 :     dcur = dcur->next;
519 :     }
520 :     }
521 :    
522 :     /***************************************
523 :     * Colorspace code converter *
524 :     ***************************************/
525 :    
526 :     static int xvid_to_opendivx_dec_csp(int csp)
527 :     {
528 :    
529 :     switch(csp)
530 :     {
531 :     case DEC_YV12 :
532 :     return XVID_CSP_YV12;
533 :     case DEC_420 :
534 :     return XVID_CSP_I420;
535 :     case DEC_YUY2 :
536 :     return XVID_CSP_YUY2;
537 :     case DEC_UYVY :
538 :     return XVID_CSP_UYVY;
539 :     case DEC_RGB32 :
540 :     return XVID_CSP_VFLIP | XVID_CSP_RGB32;
541 :     case DEC_RGB24 :
542 :     return XVID_CSP_VFLIP | XVID_CSP_RGB24;
543 :     case DEC_RGB565 :
544 :     return XVID_CSP_VFLIP | XVID_CSP_RGB565;
545 :     case DEC_RGB555 :
546 :     return XVID_CSP_VFLIP | XVID_CSP_RGB555;
547 :     case DEC_RGB32_INV :
548 :     return XVID_CSP_RGB32;
549 :     case DEC_RGB24_INV :
550 :     return XVID_CSP_RGB24;
551 :     case DEC_RGB565_INV :
552 :     return XVID_CSP_RGB565;
553 :     case DEC_RGB555_INV :
554 :     return XVID_CSP_RGB555;
555 :     case DEC_USER :
556 :     return XVID_CSP_USER;
557 :     default :
558 :     return -1;
559 :     }
560 :     }
561 :    
562 :     static int xvid_to_opendivx_enc_csp(int csp)
563 :     {
564 :    
565 :     switch (csp)
566 :     {
567 :     case ENC_CSP_RGB24 :
568 :     return XVID_CSP_VFLIP | XVID_CSP_RGB24;
569 :     case ENC_CSP_YV12 :
570 :     return XVID_CSP_YV12;
571 :     case ENC_CSP_YUY2 :
572 :     return XVID_CSP_YUY2;
573 :     case ENC_CSP_UYVY :
574 :     return XVID_CSP_UYVY;
575 :     case ENC_CSP_I420 :
576 :     return XVID_CSP_I420;
577 :     default :
578 :     return -1;
579 :     }
580 :     }

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