3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - OpenDivx API wrapper - |
* - OpenDivx API wrapper - |
5 |
* |
* |
6 |
|
* Copyright(C) 2001-2002 Peter Ross <pross@cs.rmit.edu.au> |
7 |
|
* |
8 |
* 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 |
9 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* to use this software module in hardware or software products are |
* to use this software module in hardware or software products are |
30 |
* |
* |
31 |
*************************************************************************/ |
*************************************************************************/ |
32 |
|
|
|
/************************************************************************** |
|
|
* |
|
|
* History: |
|
|
* |
|
|
* 26.02.2001 fixed dec_csp bugs |
|
|
* 26.12.2001 xvid_init() support |
|
|
* 22.12.2001 removed some compiler warnings |
|
|
* 16.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
|
|
* |
|
|
* $Id: divx4.c,v 1.13 2002-06-09 23:05:35 edgomez Exp $ |
|
|
* |
|
|
*************************************************************************/ |
|
|
|
|
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
#include <string.h> |
#include <string.h> |
35 |
#include <stdio.h> |
#include <stdio.h> |
62 |
void * handle; |
void * handle; |
63 |
XVID_DEC_FRAME xframe; |
XVID_DEC_FRAME xframe; |
64 |
|
|
65 |
} DINST; |
} |
66 |
|
DINST; |
67 |
|
|
68 |
typedef struct EINST |
typedef struct EINST |
69 |
{ |
{ |
72 |
void * handle; |
void * handle; |
73 |
int quality; |
int quality; |
74 |
|
|
75 |
} EINST; |
} |
76 |
|
EINST; |
77 |
|
|
78 |
/************************************************************************** |
/************************************************************************** |
79 |
* Global data (needed to emulate correctly exported symbols from divx4) |
* Global data (needed to emulate correctly exported symbols from divx4) |
100 |
|
|
101 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
102 |
|
|
103 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | |
104 |
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE8, |
105 |
|
|
106 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | |
107 |
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE8, |
108 |
|
|
109 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | |
110 |
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_HALFPELREFINE8 |
111 |
}; |
}; |
112 |
|
|
113 |
|
|
147 |
*************************************************************************/ |
*************************************************************************/ |
148 |
|
|
149 |
int |
int |
150 |
decore(unsigned long key, unsigned long opt, void * param1, void * param2) |
decore(unsigned long key, |
151 |
|
unsigned long opt, |
152 |
|
void *param1, |
153 |
|
void *param2) |
154 |
{ |
{ |
155 |
|
|
156 |
int xerr; |
int xerr; |
171 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
172 |
|
|
173 |
/* Find the divx4 instance */ |
/* Find the divx4 instance */ |
174 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
175 |
dcur = dinst_add(key); |
dcur = dinst_add(key); |
176 |
} |
} |
177 |
|
|
202 |
DINST * dcur; |
DINST * dcur; |
203 |
|
|
204 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
205 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
206 |
return DEC_EXIT; |
return DEC_EXIT; |
207 |
} |
} |
208 |
|
|
220 |
DINST * dcur; |
DINST * dcur; |
221 |
|
|
222 |
/* Find the divx4 instance into the chain list */ |
/* Find the divx4 instance into the chain list */ |
223 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
224 |
return DEC_EXIT; |
return DEC_EXIT; |
225 |
} |
} |
226 |
|
|
236 |
DINST * dcur; |
DINST * dcur; |
237 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
238 |
|
|
239 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
240 |
return DEC_EXIT; |
return DEC_EXIT; |
241 |
} |
} |
242 |
|
|
253 |
DINST * dcur; |
DINST * dcur; |
254 |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
255 |
|
|
256 |
if ((dcur = dinst_find(key)) == NULL) |
if ((dcur = dinst_find(key)) == NULL) { |
|
{ |
|
257 |
return DEC_EXIT; |
return DEC_EXIT; |
258 |
} |
} |
259 |
|
|
264 |
dcur->xframe.stride = dframe->stride; |
dcur->xframe.stride = dframe->stride; |
265 |
|
|
266 |
/* Does the frame need to be skipped ? */ |
/* Does the frame need to be skipped ? */ |
267 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
268 |
/* |
/* |
269 |
* Then we use the null colorspace to force XviD to |
* Then we use the null colorspace to force XviD to |
270 |
* skip the frame. The original colorspace will be |
* skip the frame. The original colorspace will be |
278 |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
279 |
|
|
280 |
/* Restore the real colorspace for this instance */ |
/* Restore the real colorspace for this instance */ |
281 |
if (!dframe->render_flag) |
if (!dframe->render_flag) { |
|
{ |
|
282 |
dcur->xframe.colorspace = csp_tmp; |
dcur->xframe.colorspace = csp_tmp; |
283 |
} |
} |
284 |
|
|
298 |
|
|
299 |
|
|
300 |
/* XviD error code -> Divx4 */ |
/* XviD error code -> Divx4 */ |
301 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
302 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
303 |
return DEC_OK; |
return DEC_OK; |
304 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
320 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
321 |
|
|
322 |
int |
int |
323 |
encore(void * handle, int opt, void * param1, void * param2) |
encore(void *handle, |
324 |
|
int opt, |
325 |
|
void *param1, |
326 |
|
void *param2) |
327 |
{ |
{ |
328 |
|
|
329 |
int xerr; |
int xerr; |
343 |
/* Settings are copied to the XviD encoder structure */ |
/* Settings are copied to the XviD encoder structure */ |
344 |
xparam.width = eparam->x_dim; |
xparam.width = eparam->x_dim; |
345 |
xparam.height = eparam->y_dim; |
xparam.height = eparam->y_dim; |
346 |
if ((eparam->framerate - (int)eparam->framerate) == 0) |
if ((eparam->framerate - (int) eparam->framerate) == 0) { |
|
{ |
|
347 |
xparam.fincr = 1; |
xparam.fincr = 1; |
348 |
xparam.fbase = (int)eparam->framerate; |
xparam.fbase = (int)eparam->framerate; |
349 |
} |
} else { |
|
else |
|
|
{ |
|
350 |
xparam.fincr = FRAMERATE_INCR; |
xparam.fincr = FRAMERATE_INCR; |
351 |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
352 |
} |
} |
364 |
eparam->handle = xparam.handle; |
eparam->handle = xparam.handle; |
365 |
|
|
366 |
/* Create an encoder instance in the chainlist */ |
/* Create an encoder instance in the chainlist */ |
367 |
if ((ecur = einst_find(xparam.handle)) == NULL) |
if ((ecur = einst_find(xparam.handle)) == NULL) { |
|
{ |
|
368 |
ecur = einst_add(xparam.handle); |
ecur = einst_add(xparam.handle); |
369 |
|
|
370 |
if(ecur == NULL) { |
if(ecur == NULL) { |
375 |
} |
} |
376 |
|
|
377 |
ecur->quality = eparam->quality; |
ecur->quality = eparam->quality; |
378 |
if(ecur->quality < 0) ecur->quality = 0; |
if (ecur->quality < 0) |
379 |
if(ecur->quality > 6) ecur->quality = 6; |
ecur->quality = 0; |
380 |
|
if (ecur->quality > 6) |
381 |
|
ecur->quality = 6; |
382 |
|
|
383 |
break; |
break; |
384 |
} |
} |
387 |
{ |
{ |
388 |
EINST *ecur; |
EINST *ecur; |
389 |
|
|
390 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
391 |
return ENC_FAIL; |
return ENC_FAIL; |
392 |
} |
} |
393 |
|
|
407 |
XVID_ENC_FRAME xframe; |
XVID_ENC_FRAME xframe; |
408 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
409 |
|
|
410 |
if ((ecur = einst_find(handle)) == NULL) |
if ((ecur = einst_find(handle)) == NULL) { |
|
{ |
|
411 |
return ENC_FAIL; |
return ENC_FAIL; |
412 |
} |
} |
413 |
|
|
418 |
xframe.general = divx4_general_presets[ecur->quality]; |
xframe.general = divx4_general_presets[ecur->quality]; |
419 |
|
|
420 |
xframe.image = eframe->image; |
xframe.image = eframe->image; |
421 |
xframe.colorspace = |
xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
422 |
|
|
423 |
if (opt == ENC_OPT_ENCODE_VBR) |
if (opt == ENC_OPT_ENCODE_VBR) { |
|
{ |
|
424 |
xframe.intra = eframe->intra; |
xframe.intra = eframe->intra; |
425 |
xframe.quant = eframe->quant; |
xframe.quant = eframe->quant; |
426 |
} |
} else { |
|
else |
|
|
{ |
|
427 |
xframe.intra = -1; |
xframe.intra = -1; |
428 |
xframe.quant = 0; |
xframe.quant = 0; |
429 |
} |
} |
430 |
|
|
431 |
/* Encode the frame */ |
/* Encode the frame */ |
432 |
xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) ); |
xerr = |
433 |
|
encoder_encode((Encoder *) handle, &xframe, |
434 |
|
(eresult ? &xstats : NULL)); |
435 |
|
|
436 |
/* Copy back the xvid structure to the divx4 one */ |
/* Copy back the xvid structure to the divx4 one */ |
437 |
if (eresult) |
if (eresult) { |
|
{ |
|
438 |
eresult->is_key_frame = xframe.intra; |
eresult->is_key_frame = xframe.intra; |
439 |
eresult->quantizer = xstats.quant; |
eresult->quantizer = xstats.quant; |
440 |
eresult->total_bits = xframe.length * 8; |
eresult->total_bits = xframe.length * 8; |
441 |
eresult->motion_bits = xstats.hlength * 8; |
eresult->motion_bits = xstats.hlength * 8; |
442 |
eresult->texture_bits = eresult->total_bits - eresult->motion_bits; |
eresult->texture_bits = |
443 |
|
eresult->total_bits - eresult->motion_bits; |
444 |
} |
} |
445 |
|
|
446 |
eframe->length = xframe.length; |
eframe->length = xframe.length; |
453 |
} |
} |
454 |
|
|
455 |
/* XviD Error code -> Divx4 error code */ |
/* XviD Error code -> Divx4 error code */ |
456 |
switch(xerr) |
switch (xerr) { |
|
{ |
|
457 |
case XVID_ERR_OK : |
case XVID_ERR_OK : |
458 |
return ENC_OK; |
return ENC_OK; |
459 |
case XVID_ERR_MEMORY : |
case XVID_ERR_MEMORY : |
474 |
***************************************/ |
***************************************/ |
475 |
|
|
476 |
/* Find an element in the chainlist according to its key value */ |
/* Find an element in the chainlist according to its key value */ |
477 |
static DINST * dinst_find(unsigned long key) |
static DINST * |
478 |
|
dinst_find(unsigned long key) |
479 |
{ |
{ |
480 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
481 |
|
|
482 |
while (dcur) |
while (dcur) { |
483 |
{ |
if (dcur->key == key) { |
|
if (dcur->key == key) |
|
|
{ |
|
484 |
return dcur; |
return dcur; |
485 |
} |
} |
486 |
dcur = dcur->next; |
dcur = dcur->next; |
491 |
|
|
492 |
|
|
493 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
494 |
static DINST * dinst_add(unsigned long key) |
static DINST * |
495 |
|
dinst_add(unsigned long key) |
496 |
{ |
{ |
497 |
DINST * dnext = dhead; |
DINST * dnext = dhead; |
498 |
|
|
499 |
dhead = malloc(sizeof(DINST)); |
dhead = malloc(sizeof(DINST)); |
500 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
501 |
dhead = dnext; |
dhead = dnext; |
502 |
return NULL; |
return NULL; |
503 |
} |
} |
510 |
|
|
511 |
|
|
512 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
513 |
static void dinst_remove(unsigned long key) |
static void |
514 |
|
dinst_remove(unsigned long key) |
515 |
{ |
{ |
516 |
DINST * dcur = dhead; |
DINST * dcur = dhead; |
517 |
|
|
518 |
if (dhead == NULL) |
if (dhead == NULL) { |
|
{ |
|
519 |
return; |
return; |
520 |
} |
} |
521 |
|
|
522 |
if (dcur->key == key) |
if (dcur->key == key) { |
|
{ |
|
523 |
dhead = dcur->next; |
dhead = dcur->next; |
524 |
free(dcur); |
free(dcur); |
525 |
return; |
return; |
526 |
} |
} |
527 |
|
|
528 |
while (dcur->next) |
while (dcur->next) { |
529 |
{ |
if (dcur->next->key == key) { |
|
if (dcur->next->key == key) |
|
|
{ |
|
530 |
DINST * tmp = dcur->next; |
DINST * tmp = dcur->next; |
531 |
|
|
532 |
dcur->next = tmp->next; |
dcur->next = tmp->next; |
533 |
free(tmp); |
free(tmp); |
534 |
return; |
return; |
543 |
***************************************/ |
***************************************/ |
544 |
|
|
545 |
/* Find an element in the chainlist according to its handle */ |
/* Find an element in the chainlist according to its handle */ |
546 |
static EINST * einst_find(void *handle) |
static EINST * |
547 |
|
einst_find(void *handle) |
548 |
{ |
{ |
549 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
550 |
|
|
551 |
while (ecur) |
while (ecur) { |
552 |
{ |
if (ecur->handle == handle) { |
|
if (ecur->handle == handle) |
|
|
{ |
|
553 |
return ecur; |
return ecur; |
554 |
} |
} |
555 |
ecur = ecur->next; |
ecur = ecur->next; |
560 |
|
|
561 |
|
|
562 |
/* Add an element to the chainlist */ |
/* Add an element to the chainlist */ |
563 |
static EINST * einst_add(void *handle) |
static EINST * |
564 |
|
einst_add(void *handle) |
565 |
{ |
{ |
566 |
EINST * enext = ehead; |
EINST * enext = ehead; |
567 |
|
|
568 |
ehead = malloc(sizeof(EINST)); |
ehead = malloc(sizeof(EINST)); |
569 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
570 |
ehead = enext; |
ehead = enext; |
571 |
return NULL; |
return NULL; |
572 |
} |
} |
579 |
|
|
580 |
|
|
581 |
/* Remove an elmement from the chainlist */ |
/* Remove an elmement from the chainlist */ |
582 |
static void einst_remove(void *handle) |
static void |
583 |
|
einst_remove(void *handle) |
584 |
{ |
{ |
585 |
EINST * ecur = ehead; |
EINST * ecur = ehead; |
586 |
|
|
587 |
if (ehead == NULL) |
if (ehead == NULL) { |
|
{ |
|
588 |
return; |
return; |
589 |
} |
} |
590 |
|
|
591 |
if (ecur->handle == handle) |
if (ecur->handle == handle) { |
|
{ |
|
592 |
ehead = ecur->next; |
ehead = ecur->next; |
593 |
free(ecur); |
free(ecur); |
594 |
return; |
return; |
595 |
} |
} |
596 |
|
|
597 |
while (ecur->next) |
while (ecur->next) { |
598 |
{ |
if (ecur->next->handle == handle) { |
|
if (ecur->next->handle == handle) |
|
|
{ |
|
599 |
EINST * tmp = ecur->next; |
EINST * tmp = ecur->next; |
600 |
|
|
601 |
ecur->next = tmp->next; |
ecur->next = tmp->next; |
602 |
free(tmp); |
free(tmp); |
603 |
return; |
return; |
605 |
ecur = ecur->next; |
ecur = ecur->next; |
606 |
} |
} |
607 |
} |
} |
608 |
|
|
609 |
/*************************************** |
/*************************************** |
610 |
* Colorspace code converter * |
* Colorspace code converter * |
611 |
***************************************/ |
***************************************/ |
612 |
|
|
613 |
static int xvid_to_opendivx_dec_csp(int csp) |
static int |
614 |
|
xvid_to_opendivx_dec_csp(int csp) |
615 |
{ |
{ |
616 |
|
|
617 |
switch(csp) |
switch (csp) { |
|
{ |
|
618 |
case DEC_YV12 : |
case DEC_YV12 : |
619 |
return XVID_CSP_YV12; |
return XVID_CSP_YV12; |
620 |
case DEC_420 : |
case DEC_420 : |
646 |
} |
} |
647 |
} |
} |
648 |
|
|
649 |
static int xvid_to_opendivx_enc_csp(int csp) |
static int |
650 |
|
xvid_to_opendivx_enc_csp(int csp) |
651 |
{ |
{ |
652 |
|
|
653 |
switch (csp) |
switch (csp) { |
|
{ |
|
654 |
case ENC_CSP_RGB24 : |
case ENC_CSP_RGB24 : |
655 |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
656 |
case ENC_CSP_YV12 : |
case ENC_CSP_YV12 : |