32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
35 |
|
* 24.02.2002 #def BFRAMES compatibility |
36 |
* 26.02.2001 fixed dec_csp bugs |
* 26.02.2001 fixed dec_csp bugs |
37 |
* 26.12.2001 xvid_init() support |
* 26.12.2001 xvid_init() support |
38 |
* 22.12.2001 removed some compiler warnings |
* 22.12.2001 removed some compiler warnings |
39 |
* 16.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
* 16.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
40 |
* |
* |
41 |
* $Id: divx4.c,v 1.13 2002-06-09 23:05:35 edgomez Exp $ |
* $Id: divx4.c,v 1.16.2.1 2002-10-05 21:34:35 Isibaar Exp $ |
42 |
* |
* |
43 |
*************************************************************************/ |
*************************************************************************/ |
44 |
|
|
74 |
void * handle; |
void * handle; |
75 |
XVID_DEC_FRAME xframe; |
XVID_DEC_FRAME xframe; |
76 |
|
|
77 |
} DINST; |
} |
78 |
|
DINST; |
79 |
|
|
80 |
typedef struct EINST |
typedef struct EINST |
81 |
{ |
{ |
84 |
void * handle; |
void * handle; |
85 |
int quality; |
int quality; |
86 |
|
|
87 |
} EINST; |
} |
88 |
|
EINST; |
89 |
|
|
90 |
/************************************************************************** |
/************************************************************************** |
91 |
* Global data (needed to emulate correctly exported symbols from divx4) |
* Global data (needed to emulate correctly exported symbols from divx4) |
106 |
static int const divx4_motion_presets[7] = { |
static int const divx4_motion_presets[7] = { |
107 |
0, |
0, |
108 |
|
|
109 |
PMV_EARLYSTOP16, |
0, |
110 |
|
|
111 |
PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16, |
PMV_ADVANCEDDIAMOND16, |
112 |
|
|
113 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
PMV_HALFPELREFINE16, |
114 |
|
|
115 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
|
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
|
116 |
|
|
117 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
|
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
|
118 |
|
|
119 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | |
PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_HALFPELREFINE8 |
|
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
|
120 |
}; |
}; |
121 |
|
|
122 |
|
|
156 |
*************************************************************************/ |
*************************************************************************/ |
157 |
|
|
158 |
int |
int |
159 |
decore(unsigned long key, unsigned long opt, void * param1, void * param2) |
decore(unsigned long key, |
160 |
|
unsigned long opt, |
161 |
|
void *param1, |
162 |
|
void *param2) |
163 |
{ |
{ |
164 |
|
|
165 |
int xerr; |
int xerr; |
180 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
181 |
|
|
182 |
/* Find the divx4 instance */ |
/* Find the divx4 instance */ |
183 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
184 |
dcur = dinst_add(key); |
dcur = dinst_add(key); |
185 |
} |
} |
186 |
|
|
211 |
DINST * dcur; |
DINST * dcur; |
212 |
|
|
213 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
214 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
215 |
return DEC_EXIT; |
return DEC_EXIT; |
216 |
} |
} |
217 |
|
|
229 |
DINST * dcur; |
DINST * dcur; |
230 |
|
|
231 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
232 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
233 |
return DEC_EXIT; |
return DEC_EXIT; |
234 |
} |
} |
235 |
|
|
245 |
DINST * dcur; |
DINST * dcur; |
246 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
247 |
|
|
248 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
249 |
return DEC_EXIT; |
return DEC_EXIT; |
250 |
} |
} |
251 |
|
|
262 |
DINST * dcur; |
DINST * dcur; |
263 |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
264 |
|
|
265 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
266 |
return DEC_EXIT; |
return DEC_EXIT; |
267 |
} |
} |
268 |
|
|
273 |
dcur->xframe.stride = dframe->stride; |
dcur->xframe.stride = dframe->stride; |
274 |
|
|
275 |
/* Does the frame need to be skipped ? */ |
/* Does the frame need to be skipped ? */ |
276 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
277 |
/* |
/* |
278 |
* Then we use the null colorspace to force XviD to |
* Then we use the null colorspace to force XviD to |
279 |
* skip the frame. The original colorspace will be |
* skip the frame. The original colorspace will be |
287 |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
288 |
|
|
289 |
/* Restore the real colorspace for this instance */ |
/* Restore the real colorspace for this instance */ |
290 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
291 |
dcur->xframe.colorspace = csp_tmp; |
dcur->xframe.colorspace = csp_tmp; |
292 |
} |
} |
293 |
|
|
307 |
|
|
308 |
|
|
309 |
/* XviD error code -> Divx4 */ |
/* XviD error code -> Divx4 */ |
310 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
311 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
312 |
return DEC_OK; |
return DEC_OK; |
313 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
329 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
330 |
|
|
331 |
int |
int |
332 |
encore(void * handle, int opt, void * param1, void * param2) |
encore(void *handle, |
333 |
|
int opt, |
334 |
|
void *param1, |
335 |
|
void *param2) |
336 |
{ |
{ |
337 |
|
|
338 |
int xerr; |
int xerr; |
352 |
/* Settings are copied to the XviD encoder structure */ |
/* Settings are copied to the XviD encoder structure */ |
353 |
xparam.width = eparam->x_dim; |
xparam.width = eparam->x_dim; |
354 |
xparam.height = eparam->y_dim; |
xparam.height = eparam->y_dim; |
355 |
if ((eparam->framerate - (int)eparam->framerate) == 0) |
if ((eparam->framerate - (int) eparam->framerate) == 0) { |
|
{ |
|
356 |
xparam.fincr = 1; |
xparam.fincr = 1; |
357 |
xparam.fbase = (int)eparam->framerate; |
xparam.fbase = (int)eparam->framerate; |
358 |
} |
} else { |
|
else |
|
|
{ |
|
359 |
xparam.fincr = FRAMERATE_INCR; |
xparam.fincr = FRAMERATE_INCR; |
360 |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
361 |
} |
} |
367 |
xparam.max_quantizer = eparam->max_quantizer; |
xparam.max_quantizer = eparam->max_quantizer; |
368 |
xparam.max_key_interval = eparam->max_key_interval; |
xparam.max_key_interval = eparam->max_key_interval; |
369 |
|
|
370 |
|
#ifdef BFRAMES |
371 |
|
xparam.global = 0; |
372 |
|
xparam.max_bframes = -1; /* use "original" IP-frame encoder */ |
373 |
|
xparam.bquant_ratio = 200; |
374 |
|
xparam.frame_drop_ratio = 0; /* dont drop frames */ |
375 |
|
#endif |
376 |
|
|
377 |
/* Create the encoder session */ |
/* Create the encoder session */ |
378 |
xerr = encoder_create(&xparam); |
xerr = encoder_create(&xparam); |
379 |
|
|
380 |
eparam->handle = xparam.handle; |
eparam->handle = xparam.handle; |
381 |
|
|
382 |
/* Create an encoder instance in the chainlist */ |
/* Create an encoder instance in the chainlist */ |
383 |
if ((ecur = einst_find(xparam.handle)) == NULL) |
if ((ecur = einst_find(xparam.handle)) == NULL) { |
|
{ |
|
384 |
ecur = einst_add(xparam.handle); |
ecur = einst_add(xparam.handle); |
385 |
|
|
386 |
if(ecur == NULL) { |
if(ecur == NULL) { |
391 |
} |
} |
392 |
|
|
393 |
ecur->quality = eparam->quality; |
ecur->quality = eparam->quality; |
394 |
if(ecur->quality < 0) ecur->quality = 0; |
if (ecur->quality < 0) |
395 |
if(ecur->quality > 6) ecur->quality = 6; |
ecur->quality = 0; |
396 |
|
if (ecur->quality > 6) |
397 |
|
ecur->quality = 6; |
398 |
|
|
399 |
break; |
break; |
400 |
} |
} |
403 |
{ |
{ |
404 |
EINST *ecur; |
EINST *ecur; |
405 |
|
|
406 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
407 |
return ENC_FAIL; |
return ENC_FAIL; |
408 |
} |
} |
409 |
|
|
423 |
XVID_ENC_FRAME xframe; |
XVID_ENC_FRAME xframe; |
424 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
425 |
|
|
426 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
427 |
return ENC_FAIL; |
return ENC_FAIL; |
428 |
} |
} |
429 |
|
|
434 |
xframe.general = divx4_general_presets[ecur->quality]; |
xframe.general = divx4_general_presets[ecur->quality]; |
435 |
|
|
436 |
xframe.image = eframe->image; |
xframe.image = eframe->image; |
437 |
xframe.colorspace = |
xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
438 |
|
|
439 |
if (opt == ENC_OPT_ENCODE_VBR) |
if (opt == ENC_OPT_ENCODE_VBR) { |
|
{ |
|
440 |
xframe.intra = eframe->intra; |
xframe.intra = eframe->intra; |
441 |
xframe.quant = eframe->quant; |
xframe.quant = eframe->quant; |
442 |
} |
} else { |
|
else |
|
|
{ |
|
443 |
xframe.intra = -1; |
xframe.intra = -1; |
444 |
xframe.quant = 0; |
xframe.quant = 0; |
445 |
} |
} |
446 |
|
|
447 |
/* Encode the frame */ |
/* Encode the frame */ |
448 |
xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) ); |
xerr = |
449 |
|
encoder_encode((Encoder *) handle, &xframe, |
450 |
|
(eresult ? &xstats : NULL)); |
451 |
|
|
452 |
/* Copy back the xvid structure to the divx4 one */ |
/* Copy back the xvid structure to the divx4 one */ |
453 |
if (eresult) |
if (eresult) { |
|
{ |
|
454 |
eresult->is_key_frame = xframe.intra; |
eresult->is_key_frame = xframe.intra; |
455 |
eresult->quantizer = xstats.quant; |
eresult->quantizer = xstats.quant; |
456 |
eresult->total_bits = xframe.length * 8; |
eresult->total_bits = xframe.length * 8; |
457 |
eresult->motion_bits = xstats.hlength * 8; |
eresult->motion_bits = xstats.hlength * 8; |
458 |
eresult->texture_bits = eresult->total_bits - eresult->motion_bits; |
eresult->texture_bits = |
459 |
|
eresult->total_bits - eresult->motion_bits; |
460 |
} |
} |
461 |
|
|
462 |
eframe->length = xframe.length; |
eframe->length = xframe.length; |
469 |
} |
} |
470 |
|
|
471 |
/* XviD Error code -> Divx4 error code */ |
/* XviD Error code -> Divx4 error code */ |
472 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
473 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
474 |
return ENC_OK; |
return ENC_OK; |
475 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
490 |
***************************************/ |
***************************************/ |
491 |
|
|
492 |
/* Find an element in the chainlist according to its key value */ |
/* Find an element in the chainlist according to its key value */ |
493 |
static DINST * dinst_find(unsigned long key) |
static DINST * |
494 |
|
dinst_find(unsigned long key) |
495 |
{ |
{ |
496 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
497 |
|
|
498 |
while (dcur) |
while (dcur) { |
499 |
{ |
if (dcur->key == key) { |
|
if (dcur->key == key) |
|
|
{ |
|
500 |
return dcur; |
return dcur; |
501 |
} |
} |
502 |
dcur = dcur->next; |
dcur = dcur->next; |
507 |
|
|
508 |
|
|
509 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
510 |
static DINST * dinst_add(unsigned long key) |
static DINST * |
511 |
|
dinst_add(unsigned long key) |
512 |
{ |
{ |
513 |
DINST * dnext = dhead; |
DINST * dnext = dhead; |
514 |
|
|
515 |
dhead = malloc(sizeof(DINST)); |
dhead = malloc(sizeof(DINST)); |
516 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
517 |
dhead = dnext; |
dhead = dnext; |
518 |
return NULL; |
return NULL; |
519 |
} |
} |
526 |
|
|
527 |
|
|
528 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
529 |
static void dinst_remove(unsigned long key) |
static void |
530 |
|
dinst_remove(unsigned long key) |
531 |
{ |
{ |
532 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
533 |
|
|
534 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
535 |
return; |
return; |
536 |
} |
} |
537 |
|
|
538 |
if (dcur->key == key) |
if (dcur->key == key) { |
|
{ |
|
539 |
dhead = dcur->next; |
dhead = dcur->next; |
540 |
free(dcur); |
free(dcur); |
541 |
return; |
return; |
542 |
} |
} |
543 |
|
|
544 |
while (dcur->next) |
while (dcur->next) { |
545 |
{ |
if (dcur->next->key == key) { |
|
if (dcur->next->key == key) |
|
|
{ |
|
546 |
DINST * tmp = dcur->next; |
DINST * tmp = dcur->next; |
547 |
|
|
548 |
dcur->next = tmp->next; |
dcur->next = tmp->next; |
549 |
free(tmp); |
free(tmp); |
550 |
return; |
return; |
559 |
***************************************/ |
***************************************/ |
560 |
|
|
561 |
/* Find an element in the chainlist according to its handle */ |
/* Find an element in the chainlist according to its handle */ |
562 |
static EINST * einst_find(void *handle) |
static EINST * |
563 |
|
einst_find(void *handle) |
564 |
{ |
{ |
565 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
566 |
|
|
567 |
while (ecur) |
while (ecur) { |
568 |
{ |
if (ecur->handle == handle) { |
|
if (ecur->handle == handle) |
|
|
{ |
|
569 |
return ecur; |
return ecur; |
570 |
} |
} |
571 |
ecur = ecur->next; |
ecur = ecur->next; |
576 |
|
|
577 |
|
|
578 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
579 |
static EINST * einst_add(void *handle) |
static EINST * |
580 |
|
einst_add(void *handle) |
581 |
{ |
{ |
582 |
EINST * enext = ehead; |
EINST * enext = ehead; |
583 |
|
|
584 |
ehead = malloc(sizeof(EINST)); |
ehead = malloc(sizeof(EINST)); |
585 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
586 |
ehead = enext; |
ehead = enext; |
587 |
return NULL; |
return NULL; |
588 |
} |
} |
595 |
|
|
596 |
|
|
597 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
598 |
static void einst_remove(void *handle) |
static void |
599 |
|
einst_remove(void *handle) |
600 |
{ |
{ |
601 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
602 |
|
|
603 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
604 |
return; |
return; |
605 |
} |
} |
606 |
|
|
607 |
if (ecur->handle == handle) |
if (ecur->handle == handle) { |
|
{ |
|
608 |
ehead = ecur->next; |
ehead = ecur->next; |
609 |
free(ecur); |
free(ecur); |
610 |
return; |
return; |
611 |
} |
} |
612 |
|
|
613 |
while (ecur->next) |
while (ecur->next) { |
614 |
{ |
if (ecur->next->handle == handle) { |
|
if (ecur->next->handle == handle) |
|
|
{ |
|
615 |
EINST * tmp = ecur->next; |
EINST * tmp = ecur->next; |
616 |
|
|
617 |
ecur->next = tmp->next; |
ecur->next = tmp->next; |
618 |
free(tmp); |
free(tmp); |
619 |
return; |
return; |
621 |
ecur = ecur->next; |
ecur = ecur->next; |
622 |
} |
} |
623 |
} |
} |
624 |
|
|
625 |
/*************************************** |
/*************************************** |
626 |
* Colorspace code converter * |
* Colorspace code converter * |
627 |
***************************************/ |
***************************************/ |
628 |
|
|
629 |
static int xvid_to_opendivx_dec_csp(int csp) |
static int |
630 |
|
xvid_to_opendivx_dec_csp(int csp) |
631 |
{ |
{ |
632 |
|
|
633 |
switch(csp) |
switch (csp) { |
|
{ |
|
634 |
case DEC_YV12 : |
case DEC_YV12 : |
635 |
return XVID_CSP_YV12; |
return XVID_CSP_YV12; |
636 |
case DEC_420 : |
case DEC_420 : |
662 |
} |
} |
663 |
} |
} |
664 |
|
|
665 |
static int xvid_to_opendivx_enc_csp(int csp) |
static int |
666 |
|
xvid_to_opendivx_enc_csp(int csp) |
667 |
{ |
{ |
668 |
|
|
669 |
switch (csp) |
switch (csp) { |
|
{ |
|
670 |
case ENC_CSP_RGB24 : |
case ENC_CSP_RGB24 : |
671 |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
672 |
case ENC_CSP_YV12 : |
case ENC_CSP_YV12 : |