1 |
/************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* OpenDivx API wrapper |
* - OpenDivx API wrapper - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* 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 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
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 |
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 |
|
|
|
PMV_QUICKSTOP16, |
|
|
|
|
109 |
PMV_EARLYSTOP16, |
PMV_EARLYSTOP16, |
110 |
|
|
111 |
|
PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16, |
112 |
|
|
113 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
114 |
|
|
115 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | |
116 |
PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, |
PMV_HALFPELREFINE8, |
117 |
|
|
118 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | |
119 |
PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, |
PMV_HALFPELREFINE8, |
120 |
|
|
121 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | |
122 |
PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | |
PMV_HALFPELREFINE8 |
|
PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 |
|
123 |
}; |
}; |
124 |
|
|
125 |
|
|
159 |
*************************************************************************/ |
*************************************************************************/ |
160 |
|
|
161 |
int |
int |
162 |
decore(unsigned long key, unsigned long opt, void * param1, void * param2) |
decore(unsigned long key, |
163 |
|
unsigned long opt, |
164 |
|
void *param1, |
165 |
|
void *param2) |
166 |
{ |
{ |
167 |
|
|
168 |
int xerr; |
int xerr; |
183 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
184 |
|
|
185 |
/* Find the divx4 instance */ |
/* Find the divx4 instance */ |
186 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
187 |
dcur = dinst_add(key); |
dcur = dinst_add(key); |
188 |
} |
} |
189 |
|
|
214 |
DINST * dcur; |
DINST * dcur; |
215 |
|
|
216 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
217 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
218 |
return DEC_EXIT; |
return DEC_EXIT; |
219 |
} |
} |
220 |
|
|
232 |
DINST * dcur; |
DINST * dcur; |
233 |
|
|
234 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
235 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
236 |
return DEC_EXIT; |
return DEC_EXIT; |
237 |
} |
} |
238 |
|
|
248 |
DINST * dcur; |
DINST * dcur; |
249 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
250 |
|
|
251 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
252 |
return DEC_EXIT; |
return DEC_EXIT; |
253 |
} |
} |
254 |
|
|
265 |
DINST * dcur; |
DINST * dcur; |
266 |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
267 |
|
|
268 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
269 |
return DEC_EXIT; |
return DEC_EXIT; |
270 |
} |
} |
271 |
|
|
276 |
dcur->xframe.stride = dframe->stride; |
dcur->xframe.stride = dframe->stride; |
277 |
|
|
278 |
/* Does the frame need to be skipped ? */ |
/* Does the frame need to be skipped ? */ |
279 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
280 |
/* |
/* |
281 |
* Then we use the null colorspace to force XviD to |
* Then we use the null colorspace to force XviD to |
282 |
* skip the frame. The original colorspace will be |
* skip the frame. The original colorspace will be |
290 |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
291 |
|
|
292 |
/* Restore the real colorspace for this instance */ |
/* Restore the real colorspace for this instance */ |
293 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
294 |
dcur->xframe.colorspace = csp_tmp; |
dcur->xframe.colorspace = csp_tmp; |
295 |
} |
} |
296 |
|
|
310 |
|
|
311 |
|
|
312 |
/* XviD error code -> Divx4 */ |
/* XviD error code -> Divx4 */ |
313 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
314 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
315 |
return DEC_OK; |
return DEC_OK; |
316 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
332 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
333 |
|
|
334 |
int |
int |
335 |
encore(void * handle, int opt, void * param1, void * param2) |
encore(void *handle, |
336 |
|
int opt, |
337 |
|
void *param1, |
338 |
|
void *param2) |
339 |
{ |
{ |
340 |
|
|
341 |
int xerr; |
int xerr; |
355 |
/* Settings are copied to the XviD encoder structure */ |
/* Settings are copied to the XviD encoder structure */ |
356 |
xparam.width = eparam->x_dim; |
xparam.width = eparam->x_dim; |
357 |
xparam.height = eparam->y_dim; |
xparam.height = eparam->y_dim; |
358 |
if ((eparam->framerate - (int)eparam->framerate) == 0) |
if ((eparam->framerate - (int) eparam->framerate) == 0) { |
|
{ |
|
359 |
xparam.fincr = 1; |
xparam.fincr = 1; |
360 |
xparam.fbase = (int)eparam->framerate; |
xparam.fbase = (int)eparam->framerate; |
361 |
} |
} else { |
|
else |
|
|
{ |
|
362 |
xparam.fincr = FRAMERATE_INCR; |
xparam.fincr = FRAMERATE_INCR; |
363 |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
364 |
} |
} |
370 |
xparam.max_quantizer = eparam->max_quantizer; |
xparam.max_quantizer = eparam->max_quantizer; |
371 |
xparam.max_key_interval = eparam->max_key_interval; |
xparam.max_key_interval = eparam->max_key_interval; |
372 |
|
|
373 |
|
#ifdef BFRAMES |
374 |
|
xparam.global = 0; |
375 |
|
xparam.max_bframes = -1; /* use "original" IP-frame encoder */ |
376 |
|
xparam.bquant_ratio = 200; |
377 |
|
xparam.frame_drop_ratio = 0; /* dont drop frames */ |
378 |
|
#endif |
379 |
|
|
380 |
/* Create the encoder session */ |
/* Create the encoder session */ |
381 |
xerr = encoder_create(&xparam); |
xerr = encoder_create(&xparam); |
382 |
|
|
383 |
eparam->handle = xparam.handle; |
eparam->handle = xparam.handle; |
384 |
|
|
385 |
/* Create an encoder instance in the chainlist */ |
/* Create an encoder instance in the chainlist */ |
386 |
if ((ecur = einst_find(xparam.handle)) == NULL) |
if ((ecur = einst_find(xparam.handle)) == NULL) { |
|
{ |
|
387 |
ecur = einst_add(xparam.handle); |
ecur = einst_add(xparam.handle); |
388 |
|
|
389 |
if(ecur == NULL) { |
if(ecur == NULL) { |
394 |
} |
} |
395 |
|
|
396 |
ecur->quality = eparam->quality; |
ecur->quality = eparam->quality; |
397 |
if(ecur->quality < 0) ecur->quality = 0; |
if (ecur->quality < 0) |
398 |
if(ecur->quality > 6) ecur->quality = 6; |
ecur->quality = 0; |
399 |
|
if (ecur->quality > 6) |
400 |
|
ecur->quality = 6; |
401 |
|
|
402 |
break; |
break; |
403 |
} |
} |
406 |
{ |
{ |
407 |
EINST *ecur; |
EINST *ecur; |
408 |
|
|
409 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
410 |
return ENC_FAIL; |
return ENC_FAIL; |
411 |
} |
} |
412 |
|
|
413 |
|
einst_remove(handle); |
414 |
xerr = encoder_destroy((Encoder *) handle); |
xerr = encoder_destroy((Encoder *) handle); |
415 |
|
|
416 |
break; |
break; |
417 |
} |
} |
418 |
|
|
426 |
XVID_ENC_FRAME xframe; |
XVID_ENC_FRAME xframe; |
427 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
428 |
|
|
429 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
430 |
return ENC_FAIL; |
return ENC_FAIL; |
431 |
} |
} |
432 |
|
|
437 |
xframe.general = divx4_general_presets[ecur->quality]; |
xframe.general = divx4_general_presets[ecur->quality]; |
438 |
|
|
439 |
xframe.image = eframe->image; |
xframe.image = eframe->image; |
440 |
xframe.colorspace = |
xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
441 |
|
|
442 |
if (opt == ENC_OPT_ENCODE_VBR) |
if (opt == ENC_OPT_ENCODE_VBR) { |
|
{ |
|
443 |
xframe.intra = eframe->intra; |
xframe.intra = eframe->intra; |
444 |
xframe.quant = eframe->quant; |
xframe.quant = eframe->quant; |
445 |
} |
} else { |
|
else |
|
|
{ |
|
446 |
xframe.intra = -1; |
xframe.intra = -1; |
447 |
xframe.quant = 0; |
xframe.quant = 0; |
448 |
} |
} |
449 |
|
|
450 |
/* Encode the frame */ |
/* Encode the frame */ |
451 |
xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) ); |
xerr = |
452 |
|
encoder_encode((Encoder *) handle, &xframe, |
453 |
|
(eresult ? &xstats : NULL)); |
454 |
|
|
455 |
/* Copy back the xvid structure to the divx4 one */ |
/* Copy back the xvid structure to the divx4 one */ |
456 |
if (eresult) |
if (eresult) { |
|
{ |
|
457 |
eresult->is_key_frame = xframe.intra; |
eresult->is_key_frame = xframe.intra; |
458 |
eresult->quantizer = xstats.quant; |
eresult->quantizer = xstats.quant; |
459 |
eresult->total_bits = xframe.length * 8; |
eresult->total_bits = xframe.length * 8; |
460 |
eresult->motion_bits = xstats.hlength * 8; |
eresult->motion_bits = xstats.hlength * 8; |
461 |
eresult->texture_bits = eresult->total_bits - eresult->motion_bits; |
eresult->texture_bits = |
462 |
|
eresult->total_bits - eresult->motion_bits; |
463 |
} |
} |
464 |
|
|
465 |
eframe->length = xframe.length; |
eframe->length = xframe.length; |
472 |
} |
} |
473 |
|
|
474 |
/* XviD Error code -> Divx4 error code */ |
/* XviD Error code -> Divx4 error code */ |
475 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
476 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
477 |
return ENC_OK; |
return ENC_OK; |
478 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
493 |
***************************************/ |
***************************************/ |
494 |
|
|
495 |
/* Find an element in the chainlist according to its key value */ |
/* Find an element in the chainlist according to its key value */ |
496 |
static DINST * dinst_find(unsigned long key) |
static DINST * |
497 |
|
dinst_find(unsigned long key) |
498 |
{ |
{ |
499 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
500 |
|
|
501 |
while (dcur) |
while (dcur) { |
502 |
{ |
if (dcur->key == key) { |
|
if (dcur->key == key) |
|
|
{ |
|
503 |
return dcur; |
return dcur; |
504 |
} |
} |
505 |
dcur = dcur->next; |
dcur = dcur->next; |
510 |
|
|
511 |
|
|
512 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
513 |
static DINST * dinst_add(unsigned long key) |
static DINST * |
514 |
|
dinst_add(unsigned long key) |
515 |
{ |
{ |
516 |
DINST * dnext = dhead; |
DINST * dnext = dhead; |
517 |
|
|
518 |
dhead = malloc(sizeof(DINST)); |
dhead = malloc(sizeof(DINST)); |
519 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
520 |
dhead = dnext; |
dhead = dnext; |
521 |
return NULL; |
return NULL; |
522 |
} |
} |
529 |
|
|
530 |
|
|
531 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
532 |
static void dinst_remove(unsigned long key) |
static void |
533 |
|
dinst_remove(unsigned long key) |
534 |
{ |
{ |
535 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
536 |
|
|
537 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
538 |
return; |
return; |
539 |
} |
} |
540 |
|
|
541 |
if (dcur->key == key) |
if (dcur->key == key) { |
|
{ |
|
542 |
dhead = dcur->next; |
dhead = dcur->next; |
543 |
free(dcur); |
free(dcur); |
544 |
return; |
return; |
545 |
} |
} |
546 |
|
|
547 |
while (dcur->next) |
while (dcur->next) { |
548 |
{ |
if (dcur->next->key == key) { |
|
if (dcur->next->key == key) |
|
|
{ |
|
549 |
DINST * tmp = dcur->next; |
DINST * tmp = dcur->next; |
550 |
|
|
551 |
dcur->next = tmp->next; |
dcur->next = tmp->next; |
552 |
free(tmp); |
free(tmp); |
553 |
return; |
return; |
562 |
***************************************/ |
***************************************/ |
563 |
|
|
564 |
/* Find an element in the chainlist according to its handle */ |
/* Find an element in the chainlist according to its handle */ |
565 |
static EINST * einst_find(void *handle) |
static EINST * |
566 |
|
einst_find(void *handle) |
567 |
{ |
{ |
568 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
569 |
|
|
570 |
while (ecur) |
while (ecur) { |
571 |
{ |
if (ecur->handle == handle) { |
|
if (ecur->handle == handle) |
|
|
{ |
|
572 |
return ecur; |
return ecur; |
573 |
} |
} |
574 |
ecur = ecur->next; |
ecur = ecur->next; |
579 |
|
|
580 |
|
|
581 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
582 |
static EINST * einst_add(void *handle) |
static EINST * |
583 |
|
einst_add(void *handle) |
584 |
{ |
{ |
585 |
EINST * enext = ehead; |
EINST * enext = ehead; |
586 |
|
|
587 |
ehead = malloc(sizeof(EINST)); |
ehead = malloc(sizeof(EINST)); |
588 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
589 |
ehead = enext; |
ehead = enext; |
590 |
return NULL; |
return NULL; |
591 |
} |
} |
598 |
|
|
599 |
|
|
600 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
601 |
static void einst_remove(void *handle) |
static void |
602 |
|
einst_remove(void *handle) |
603 |
{ |
{ |
604 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
605 |
|
|
606 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
607 |
return; |
return; |
608 |
} |
} |
609 |
|
|
610 |
if (ecur->handle == handle) |
if (ecur->handle == handle) { |
|
{ |
|
611 |
ehead = ecur->next; |
ehead = ecur->next; |
612 |
free(ecur); |
free(ecur); |
613 |
return; |
return; |
614 |
} |
} |
615 |
|
|
616 |
while (ecur->next) |
while (ecur->next) { |
617 |
{ |
if (ecur->next->handle == handle) { |
|
if (ecur->next->handle == handle) |
|
|
{ |
|
618 |
EINST * tmp = ecur->next; |
EINST * tmp = ecur->next; |
619 |
|
|
620 |
ecur->next = tmp->next; |
ecur->next = tmp->next; |
621 |
free(tmp); |
free(tmp); |
622 |
return; |
return; |
624 |
ecur = ecur->next; |
ecur = ecur->next; |
625 |
} |
} |
626 |
} |
} |
627 |
|
|
628 |
/*************************************** |
/*************************************** |
629 |
* Colorspace code converter * |
* Colorspace code converter * |
630 |
***************************************/ |
***************************************/ |
631 |
|
|
632 |
static int xvid_to_opendivx_dec_csp(int csp) |
static int |
633 |
|
xvid_to_opendivx_dec_csp(int csp) |
634 |
{ |
{ |
635 |
|
|
636 |
switch(csp) |
switch (csp) { |
|
{ |
|
637 |
case DEC_YV12 : |
case DEC_YV12 : |
638 |
return XVID_CSP_YV12; |
return XVID_CSP_YV12; |
639 |
case DEC_420 : |
case DEC_420 : |
665 |
} |
} |
666 |
} |
} |
667 |
|
|
668 |
static int xvid_to_opendivx_enc_csp(int csp) |
static int |
669 |
|
xvid_to_opendivx_enc_csp(int csp) |
670 |
{ |
{ |
671 |
|
|
672 |
switch (csp) |
switch (csp) { |
|
{ |
|
673 |
case ENC_CSP_RGB24 : |
case ENC_CSP_RGB24 : |
674 |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
675 |
case ENC_CSP_YV12 : |
case ENC_CSP_YV12 : |