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

Annotation of /xvidcore/src/divx4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (view) (download)

1 : Isibaar 1.1 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 1.9 * OpenDivx API wrapper
5 : Isibaar 1.1 *
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 1.10 * $Id$
41 :     *
42 : Isibaar 1.1 *************************************************************************/
43 :    
44 : knhor 1.7 #include <stdlib.h>
45 : edgomez 1.9 #include <string.h>
46 : Isibaar 1.1
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 1.9 /**************************************************************************
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 1.1
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 1.9 /**************************************************************************
78 :     * Global data (needed to emulate correctly exported symbols from divx4)
79 :     *************************************************************************/
80 :    
81 :     /* This is not used in this module but is required by some divx4 encoders*/
82 :     int quiet_encore = 1;
83 :    
84 :     /**************************************************************************
85 :     * Local data
86 :     *************************************************************************/
87 :    
88 :     /* The Divx4 instance chainlist */
89 :     static DINST * dhead = NULL;
90 :    
91 :     /* Divx4 quality to XviD encoder motion flag presets */
92 :     static int const divx4_motion_presets[7] = {
93 :     0,
94 :    
95 :     PMV_QUICKSTOP16,
96 :    
97 :     PMV_EARLYSTOP16,
98 :    
99 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
100 :    
101 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
102 :     PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
103 :    
104 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
105 :     PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
106 :    
107 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
108 :     PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
109 :     PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8
110 :     };
111 :    
112 :    
113 :     /* 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 :    
124 :     /*
125 :     * Current divx4 encoder quality
126 :     * ToDo : this data should not be shared between encoder instances
127 :     */
128 :     static int quality;
129 :    
130 :     /**************************************************************************
131 :     * Local Prototypes
132 :     *************************************************************************/
133 :    
134 :     /* 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 :    
139 :     /* 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 :    
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 :    
150 :     int
151 :     decore(unsigned long key, unsigned long opt, void * param1, void * param2)
152 :     {
153 :    
154 :     int xerr;
155 :    
156 :     switch (opt) {
157 :    
158 :     case DEC_OPT_MEMORY_REQS :
159 :     {
160 :     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 :     {
174 :     dcur = dinst_add(key);
175 :     }
176 :    
177 :     /*
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 :    
185 :     /* 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 :    
191 :     xerr = decoder_create(&xparam);
192 :    
193 :     /* Store the xvid handle into the divx4 instance chainlist */
194 :     dcur->handle = xparam.handle;
195 :    
196 :     break;
197 :     }
198 :    
199 :     case DEC_OPT_RELEASE :
200 :     {
201 :     DINST * dcur;
202 :    
203 :     /* Find the divx4 instance into the chain list */
204 :     if ((dcur = dinst_find(key)) == NULL)
205 :     {
206 :     return DEC_EXIT;
207 :     }
208 :    
209 :     /* Destroy the XviD decoder attached to this divx4 instance */
210 :     xerr = decoder_destroy(dcur->handle);
211 :    
212 :     /* Remove the divx4 instance from the chainlist */
213 :     dinst_remove(key);
214 :    
215 :     break;
216 :     }
217 :    
218 :     case DEC_OPT_SETPP :
219 :     {
220 :     DINST * dcur;
221 :    
222 :     /* Find the divx4 instance into the chain list */
223 :     if ((dcur = dinst_find(key)) == NULL)
224 :     {
225 :     return DEC_EXIT;
226 :     }
227 :    
228 :     /*
229 :     * We return DEC_OK but XviD has no postprocessing implemented
230 :     * in core.
231 :     */
232 :     return DEC_OK;
233 :     }
234 :    
235 :     case DEC_OPT_SETOUT :
236 :     {
237 :     DINST * dcur;
238 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
239 :    
240 :     if ((dcur = dinst_find(key)) == NULL)
241 :     {
242 :     return DEC_EXIT;
243 :     }
244 :    
245 :     /* Change the output colorspace */
246 :     dcur->xframe.colorspace =
247 :     xvid_to_opendivx_dec_csp(dparam->output_format);
248 :    
249 :     return DEC_OK;
250 :     }
251 : Isibaar 1.1
252 : edgomez 1.9 case DEC_OPT_FRAME:
253 :     {
254 :     int csp_tmp = 0;
255 :     DINST * dcur;
256 :     DEC_FRAME * dframe = (DEC_FRAME *)param1;
257 :    
258 :     if ((dcur = dinst_find(key)) == NULL)
259 :     {
260 :     return DEC_EXIT;
261 :     }
262 :    
263 :     /* 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 :    
269 :     /* 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 :    
281 :     /* Decode the bitstream */
282 :     xerr = decoder_decode(dcur->handle, &dcur->xframe);
283 :    
284 :     /* Restore the real colorspace for this instance */
285 :     if (!dframe->render_flag)
286 :     {
287 :     dcur->xframe.colorspace = csp_tmp;
288 :     }
289 :    
290 :     break;
291 :     }
292 :    
293 :     case DEC_OPT_FRAME_311 :
294 :     /* XviD does not handle Divx ;-) 3.11 yet */
295 :     return DEC_EXIT;
296 :    
297 :     case DEC_OPT_VERSION:
298 :     return EMULATED_DIVX_VERSION;
299 :    
300 :     default :
301 :     return DEC_EXIT;
302 :     }
303 :    
304 :    
305 :     /* XviD error code -> Divx4 */
306 :     switch(xerr)
307 :     {
308 :     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 :     return DEC_EXIT;
316 :     }
317 :     }
318 :    
319 :     /**************************************************************************
320 :     * Encore Part
321 :     *
322 :     * encore is the divx4 entry point used to compress a frame to a mpeg4
323 :     * bitstream.
324 :     *************************************************************************/
325 :    
326 :     #define FRAMERATE_INCR 1001
327 :    
328 :     int
329 :     encore(void * handle, int opt, void * param1, void * param2)
330 :     {
331 :    
332 :     int xerr;
333 :    
334 :     switch(opt) {
335 :     case ENC_OPT_INIT :
336 :     {
337 :     ENC_PARAM * eparam = (ENC_PARAM *)param1;
338 :     XVID_INIT_PARAM xinit;
339 :     XVID_ENC_PARAM xparam;
340 :    
341 :     /* Init XviD which will detect host cpu features */
342 :     xinit.cpu_flags = 0;
343 :     xvid_init(NULL, 0, &xinit, NULL);
344 :    
345 :     /* 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 :    
367 :     /* Create the encoder session */
368 :     xerr = encoder_create(&xparam);
369 :    
370 :     eparam->handle = xparam.handle;
371 :    
372 :     break;
373 :     }
374 :    
375 :     case ENC_OPT_RELEASE :
376 :     {
377 :     xerr = encoder_destroy((Encoder *) handle);
378 :     break;
379 :     }
380 :    
381 :     case ENC_OPT_ENCODE :
382 :     case ENC_OPT_ENCODE_VBR :
383 :     {
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 :    
389 :     /* Copy the divx4 info into the xvid structure */
390 :     xframe.bitstream = eframe->bitstream;
391 :     xframe.length = eframe->length;
392 :    
393 :     xframe.motion = divx4_motion_presets[quality];
394 :     xframe.general = divx4_general_presets[quality];
395 :    
396 :     xframe.image = eframe->image;
397 :     xframe.colorspace =
398 :     xvid_to_opendivx_enc_csp(eframe->colorspace);
399 :    
400 :     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 :    
411 :     /* Encode the frame */
412 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
413 :    
414 :     /* 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 :    
424 :     eframe->length = xframe.length;
425 :    
426 :     break;
427 :     }
428 :    
429 :     default:
430 :     return ENC_FAIL;
431 :     }
432 :    
433 :     /* XviD Error code -> Divx4 error code */
434 :     switch(xerr)
435 :     {
436 :     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 :     return ENC_FAIL;
444 :     }
445 :     }
446 :    
447 :     /**************************************************************************
448 :     * Local Functions
449 :     *************************************************************************/
450 : Isibaar 1.1
451 : edgomez 1.9 /***************************************
452 :     * DINST chainlist helper functions *
453 :     ***************************************/
454 : Isibaar 1.1
455 : edgomez 1.9 /* Find an element in the chainlist according to its key value */
456 :     static DINST * dinst_find(unsigned long key)
457 : Isibaar 1.1 {
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 : edgomez 1.9 /* Add an element to the chainlist */
474 :     static DINST * dinst_add(unsigned long key)
475 : Isibaar 1.1 {
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 : edgomez 1.9 /* Remove an elmement from the chainlist */
493 :     static void dinst_remove(unsigned long key)
494 : Isibaar 1.1 {
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 : edgomez 1.9 /***************************************
523 :     * Colorspace code converter *
524 :     ***************************************/
525 : Isibaar 1.1
526 : edgomez 1.9 static int xvid_to_opendivx_dec_csp(int csp)
527 : Isibaar 1.1 {
528 : edgomez 1.9
529 : Isibaar 1.1 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 : edgomez 1.9 static int xvid_to_opendivx_enc_csp(int csp)
563 : Isibaar 1.1 {
564 :    
565 : edgomez 1.9 switch (csp)
566 : Isibaar 1.1 {
567 : edgomez 1.9 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 : Isibaar 1.1 default :
578 : edgomez 1.9 return -1;
579 : Isibaar 1.1 }
580 :     }

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