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 |
37 |
* 22.12.2001 removed some compiler warnings |
* 22.12.2001 removed some compiler warnings |
38 |
* 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> |
39 |
* |
* |
40 |
|
* $Id: divx4.c,v 1.12 2002-05-22 12:40:25 chl Exp $ |
41 |
|
* |
42 |
*************************************************************************/ |
*************************************************************************/ |
43 |
|
|
44 |
|
#include <stdlib.h> |
45 |
#include <malloc.h> |
#include <string.h> |
46 |
#include <string.h> // memset |
#include <stdio.h> |
47 |
|
|
48 |
#include "xvid.h" |
#include "xvid.h" |
49 |
#include "divx4.h" |
#include "divx4.h" |
52 |
|
|
53 |
#define EMULATED_DIVX_VERSION 20011001 |
#define EMULATED_DIVX_VERSION 20011001 |
54 |
|
|
55 |
// decore |
/************************************************************************** |
56 |
|
* Divx Instance Structure |
57 |
|
* |
58 |
|
* This chain list datatype allows XviD do instanciate multiples divx4 |
59 |
|
* sessions. |
60 |
|
* |
61 |
|
* ToDo : The way this chain list is used does not guarantee reentrance |
62 |
|
* because they are not protected by any kind of mutex to allow |
63 |
|
* only one modifier. We should add a mutex for each element in |
64 |
|
* the chainlist. |
65 |
|
*************************************************************************/ |
66 |
|
|
67 |
|
|
68 |
typedef struct DINST |
typedef struct DINST |
75 |
|
|
76 |
} DINST; |
} DINST; |
77 |
|
|
78 |
|
typedef struct EINST |
79 |
|
{ |
80 |
|
struct EINST * next; |
81 |
|
|
82 |
static DINST * dhead = NULL; |
void * handle; |
83 |
|
int quality; |
84 |
|
|
85 |
|
} EINST; |
86 |
|
|
87 |
DINST * dinst_find(unsigned long key) |
/************************************************************************** |
88 |
{ |
* Global data (needed to emulate correctly exported symbols from divx4) |
89 |
DINST * dcur = dhead; |
*************************************************************************/ |
90 |
|
|
91 |
while (dcur) |
/* This is not used in this module but is required by some divx4 encoders*/ |
92 |
{ |
int quiet_encore = 1; |
|
if (dcur->key == key) |
|
|
{ |
|
|
return dcur; |
|
|
} |
|
|
dcur = dcur->next; |
|
|
} |
|
93 |
|
|
94 |
return NULL; |
/************************************************************************** |
95 |
} |
* Local data |
96 |
|
*************************************************************************/ |
97 |
|
|
98 |
|
/* The Divx4 instance chainlist */ |
99 |
|
static DINST * dhead = NULL; |
100 |
|
static EINST * ehead = NULL; |
101 |
|
|
102 |
DINST * dinst_add(unsigned long key) |
/* Divx4 quality to XviD encoder motion flag presets */ |
103 |
{ |
static int const divx4_motion_presets[7] = { |
104 |
DINST * dnext = dhead; |
0, |
105 |
|
|
106 |
dhead = malloc(sizeof(DINST)); |
PMV_EARLYSTOP16, |
|
if (dhead == NULL) |
|
|
{ |
|
|
dhead = dnext; |
|
|
return NULL; |
|
|
} |
|
107 |
|
|
108 |
dhead->key = key; |
PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16, |
|
dhead->next = dnext; |
|
109 |
|
|
110 |
return dhead; |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, |
|
} |
|
111 |
|
|
112 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
113 |
|
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
114 |
|
|
115 |
void dinst_remove(unsigned long key) |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | |
116 |
{ |
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8, |
|
DINST * dcur = dhead; |
|
117 |
|
|
118 |
if (dhead == NULL) |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | |
119 |
{ |
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
120 |
return; |
}; |
|
} |
|
121 |
|
|
|
if (dcur->key == key) |
|
|
{ |
|
|
dhead = dcur->next; |
|
|
free(dcur); |
|
|
return; |
|
|
} |
|
122 |
|
|
123 |
while (dcur->next) |
/* Divx4 quality to general encoder flag presets */ |
124 |
{ |
static int const divx4_general_presets[7] = { |
125 |
if (dcur->next->key == key) |
0, |
126 |
{ |
XVID_H263QUANT, |
127 |
DINST * tmp = dcur->next; |
XVID_H263QUANT, |
128 |
dcur->next = tmp->next; |
XVID_H263QUANT | XVID_HALFPEL, |
129 |
free(tmp); |
XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL, |
130 |
return; |
XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL, |
131 |
} |
XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL |
132 |
dcur = dcur->next; |
}; |
|
} |
|
|
} |
|
133 |
|
|
134 |
|
/************************************************************************** |
135 |
|
* Local Prototypes |
136 |
|
*************************************************************************/ |
137 |
|
|
138 |
int xvid_to_opendivx_dec_csp(int csp) |
/* Chain list helper functions */ |
139 |
{ |
static DINST * dinst_find(unsigned long key); |
140 |
switch(csp) |
static DINST * dinst_add(unsigned long key); |
141 |
{ |
static void dinst_remove(unsigned long key); |
142 |
case DEC_YV12 : |
|
143 |
return XVID_CSP_YV12; |
static EINST * einst_find(void *handle); |
144 |
case DEC_420 : |
static EINST * einst_add(void *handle); |
145 |
return XVID_CSP_I420; |
static void einst_remove(void *handle); |
146 |
case DEC_YUY2 : |
|
147 |
return XVID_CSP_YUY2; |
/* Converts divx4 colorspaces codes to xvid codes */ |
148 |
case DEC_UYVY : |
static int xvid_to_opendivx_dec_csp(int csp); |
149 |
return XVID_CSP_UYVY; |
static int xvid_to_opendivx_enc_csp(int csp); |
|
case DEC_RGB32 : |
|
|
return XVID_CSP_VFLIP | XVID_CSP_RGB32; |
|
|
case DEC_RGB24 : |
|
|
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
|
|
case DEC_RGB565 : |
|
|
return XVID_CSP_VFLIP | XVID_CSP_RGB565; |
|
|
case DEC_RGB555 : |
|
|
return XVID_CSP_VFLIP | XVID_CSP_RGB555; |
|
|
case DEC_RGB32_INV : |
|
|
return XVID_CSP_RGB32; |
|
|
case DEC_RGB24_INV : |
|
|
return XVID_CSP_RGB24; |
|
|
case DEC_RGB565_INV : |
|
|
return XVID_CSP_RGB565; |
|
|
case DEC_RGB555_INV : |
|
|
return XVID_CSP_RGB555; |
|
|
case DEC_USER : |
|
|
return XVID_CSP_USER; |
|
|
default : |
|
|
return -1; |
|
|
} |
|
|
} |
|
150 |
|
|
151 |
|
/************************************************************************** |
152 |
|
* decore part |
153 |
|
* |
154 |
|
* decore is the divx4 entry point used to decompress the mpeg4 bitstream |
155 |
|
* into a user defined image format. |
156 |
|
*************************************************************************/ |
157 |
|
|
158 |
int decore(unsigned long key, unsigned long opt, |
int |
159 |
void * param1, void * param2) |
decore(unsigned long key, unsigned long opt, void * param1, void * param2) |
160 |
{ |
{ |
161 |
|
|
162 |
int xerr; |
int xerr; |
163 |
|
|
164 |
switch (opt) |
switch (opt) { |
165 |
{ |
|
166 |
case DEC_OPT_MEMORY_REQS : |
case DEC_OPT_MEMORY_REQS : |
167 |
{ |
{ |
168 |
memset(param2, 0, sizeof(DEC_MEM_REQS)); |
memset(param2, 0, sizeof(DEC_MEM_REQS)); |
171 |
|
|
172 |
case DEC_OPT_INIT : |
case DEC_OPT_INIT : |
173 |
{ |
{ |
|
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
|
174 |
XVID_INIT_PARAM xinit; |
XVID_INIT_PARAM xinit; |
175 |
XVID_DEC_PARAM xparam; |
XVID_DEC_PARAM xparam; |
176 |
DINST * dcur = dinst_find(key); |
DINST * dcur; |
177 |
if (dcur == NULL) |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
178 |
|
|
179 |
|
/* Find the divx4 instance */ |
180 |
|
if ((dcur = dinst_find(key)) == NULL) |
181 |
{ |
{ |
182 |
dcur = dinst_add(key); |
dcur = dinst_add(key); |
183 |
} |
} |
184 |
|
|
185 |
|
/* |
186 |
|
* XviD initialization |
187 |
|
* XviD will detect the host cpu type and activate optimized |
188 |
|
* functions according to the host cpu features. |
189 |
|
*/ |
190 |
xinit.cpu_flags = 0; |
xinit.cpu_flags = 0; |
191 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_init(NULL, 0, &xinit, NULL); |
192 |
|
|
193 |
|
/* XviD decoder initialization for this instance */ |
194 |
xparam.width = dparam->x_dim; |
xparam.width = dparam->x_dim; |
195 |
xparam.height = dparam->y_dim; |
xparam.height = dparam->y_dim; |
196 |
dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format); |
dcur->xframe.colorspace = |
197 |
|
xvid_to_opendivx_dec_csp(dparam->output_format); |
198 |
|
|
199 |
xerr = decoder_create(&xparam); |
xerr = decoder_create(&xparam); |
200 |
|
|
201 |
|
/* Store the xvid handle into the divx4 instance chainlist */ |
202 |
dcur->handle = xparam.handle; |
dcur->handle = xparam.handle; |
203 |
|
|
204 |
break; |
break; |
206 |
|
|
207 |
case DEC_OPT_RELEASE : |
case DEC_OPT_RELEASE : |
208 |
{ |
{ |
209 |
DINST * dcur = dinst_find(key); |
DINST * dcur; |
210 |
if (dcur == NULL) |
|
211 |
|
/* Find the divx4 instance into the chain list */ |
212 |
|
if ((dcur = dinst_find(key)) == NULL) |
213 |
{ |
{ |
214 |
return DEC_EXIT; |
return DEC_EXIT; |
215 |
} |
} |
216 |
|
|
217 |
|
/* Destroy the XviD decoder attached to this divx4 instance */ |
218 |
xerr = decoder_destroy(dcur->handle); |
xerr = decoder_destroy(dcur->handle); |
219 |
|
|
220 |
|
/* Remove the divx4 instance from the chainlist */ |
221 |
dinst_remove(key); |
dinst_remove(key); |
222 |
|
|
223 |
break; |
break; |
225 |
|
|
226 |
case DEC_OPT_SETPP : |
case DEC_OPT_SETPP : |
227 |
{ |
{ |
228 |
// DEC_SET * dset = (DEC_SET *)param1; |
DINST * dcur; |
229 |
DINST * dcur = dinst_find(key); |
|
230 |
if (dcur == NULL) |
/* Find the divx4 instance into the chain list */ |
231 |
|
if ((dcur = dinst_find(key)) == NULL) |
232 |
{ |
{ |
233 |
return DEC_EXIT; |
return DEC_EXIT; |
234 |
} |
} |
235 |
|
|
236 |
// dcur->xframe.pp = dset->postproc_level; |
/* |
237 |
|
* We return DEC_OK but XviD has no postprocessing implemented |
238 |
|
* in core. |
239 |
|
*/ |
240 |
return DEC_OK; |
return DEC_OK; |
241 |
} |
} |
242 |
|
|
243 |
case DEC_OPT_SETOUT : |
case DEC_OPT_SETOUT : |
244 |
{ |
{ |
245 |
|
DINST * dcur; |
246 |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
DEC_PARAM * dparam = (DEC_PARAM *)param1; |
247 |
DINST * dcur = dinst_find(key); |
|
248 |
if (dcur == NULL) |
if ((dcur = dinst_find(key)) == NULL) |
249 |
{ |
{ |
250 |
return DEC_EXIT; |
return DEC_EXIT; |
251 |
} |
} |
252 |
|
|
253 |
dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format); |
/* Change the output colorspace */ |
254 |
|
dcur->xframe.colorspace = |
255 |
|
xvid_to_opendivx_dec_csp(dparam->output_format); |
256 |
|
|
257 |
return DEC_OK; |
return DEC_OK; |
258 |
} |
} |
259 |
|
|
260 |
case DEC_OPT_FRAME: |
case DEC_OPT_FRAME: |
261 |
{ |
{ |
262 |
int csp_tmp; |
int csp_tmp = 0; |
263 |
|
DINST * dcur; |
264 |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
DEC_FRAME * dframe = (DEC_FRAME *)param1; |
265 |
DINST * dcur = dinst_find(key); |
|
266 |
if (dcur == NULL) |
if ((dcur = dinst_find(key)) == NULL) |
267 |
{ |
{ |
268 |
return DEC_EXIT; |
return DEC_EXIT; |
269 |
} |
} |
270 |
|
|
271 |
|
/* Copy the divx4 fields to the XviD decoder structure */ |
272 |
dcur->xframe.bitstream = dframe->bitstream; |
dcur->xframe.bitstream = dframe->bitstream; |
273 |
dcur->xframe.length = dframe->length; |
dcur->xframe.length = dframe->length; |
274 |
dcur->xframe.image = dframe->bmp; |
dcur->xframe.image = dframe->bmp; |
275 |
dcur->xframe.stride = dframe->stride; |
dcur->xframe.stride = dframe->stride; |
276 |
|
|
277 |
|
/* Does the frame need to be skipped ? */ |
278 |
if (!dframe->render_flag) |
if (!dframe->render_flag) |
279 |
{ |
{ |
280 |
|
/* |
281 |
|
* Then we use the null colorspace to force XviD to |
282 |
|
* skip the frame. The original colorspace will be |
283 |
|
* restored after the decoder call |
284 |
|
*/ |
285 |
csp_tmp = dcur->xframe.colorspace; |
csp_tmp = dcur->xframe.colorspace; |
286 |
dcur->xframe.colorspace = XVID_CSP_NULL; |
dcur->xframe.colorspace = XVID_CSP_NULL; |
287 |
} |
} |
288 |
|
|
289 |
|
/* Decode the bitstream */ |
290 |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
xerr = decoder_decode(dcur->handle, &dcur->xframe); |
291 |
|
|
292 |
|
/* Restore the real colorspace for this instance */ |
293 |
if (!dframe->render_flag) |
if (!dframe->render_flag) |
294 |
{ |
{ |
295 |
dcur->xframe.colorspace = csp_tmp; |
dcur->xframe.colorspace = csp_tmp; |
298 |
break; |
break; |
299 |
} |
} |
300 |
|
|
|
|
|
301 |
case DEC_OPT_FRAME_311 : |
case DEC_OPT_FRAME_311 : |
302 |
|
/* XviD does not handle Divx ;-) 3.11 yet */ |
303 |
return DEC_EXIT; |
return DEC_EXIT; |
304 |
|
|
|
|
|
305 |
case DEC_OPT_VERSION: |
case DEC_OPT_VERSION: |
306 |
return EMULATED_DIVX_VERSION; |
return EMULATED_DIVX_VERSION; |
307 |
|
|
308 |
default : |
default : |
309 |
return DEC_EXIT; |
return DEC_EXIT; |
310 |
} |
} |
311 |
|
|
312 |
|
|
313 |
|
/* XviD error code -> Divx4 */ |
314 |
switch(xerr) |
switch(xerr) |
315 |
{ |
{ |
316 |
case XVID_ERR_OK : return DEC_OK; |
case XVID_ERR_OK : |
317 |
case XVID_ERR_MEMORY : return DEC_MEMORY; |
return DEC_OK; |
318 |
case XVID_ERR_FORMAT : return DEC_BAD_FORMAT; |
case XVID_ERR_MEMORY : |
319 |
default : // case XVID_ERR_FAIL : |
return DEC_MEMORY; |
320 |
|
case XVID_ERR_FORMAT : |
321 |
|
return DEC_BAD_FORMAT; |
322 |
|
default : |
323 |
return DEC_EXIT; |
return DEC_EXIT; |
324 |
} |
} |
325 |
} |
} |
326 |
|
|
327 |
|
/************************************************************************** |
328 |
|
* Encore Part |
329 |
|
* |
330 |
// encore |
* encore is the divx4 entry point used to compress a frame to a mpeg4 |
331 |
|
* bitstream. |
332 |
|
*************************************************************************/ |
333 |
|
|
334 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
335 |
|
|
336 |
int pmvfast_presets[7] = { |
int |
337 |
0, PMV_QUICKSTOP16, PMV_EARLYSTOP16, PMV_EARLYSTOP16 | PMV_EARLYSTOP8, |
encore(void * handle, int opt, void * param1, void * param2) |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | |
|
|
PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 |
|
|
}; |
|
|
|
|
|
int quality; |
|
|
|
|
|
int encore(void * handle, int opt, void * param1, void * param2) |
|
338 |
{ |
{ |
339 |
|
|
340 |
int xerr; |
int xerr; |
341 |
|
|
342 |
switch(opt) |
switch(opt) { |
|
{ |
|
343 |
case ENC_OPT_INIT : |
case ENC_OPT_INIT : |
344 |
{ |
{ |
345 |
|
EINST *ecur; |
346 |
ENC_PARAM * eparam = (ENC_PARAM *)param1; |
ENC_PARAM * eparam = (ENC_PARAM *)param1; |
347 |
XVID_INIT_PARAM xinit; |
XVID_INIT_PARAM xinit; |
348 |
XVID_ENC_PARAM xparam; |
XVID_ENC_PARAM xparam; |
349 |
|
|
350 |
|
/* Init XviD which will detect host cpu features */ |
351 |
xinit.cpu_flags = 0; |
xinit.cpu_flags = 0; |
352 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_init(NULL, 0, &xinit, NULL); |
353 |
|
|
354 |
|
/* Settings are copied to the XviD encoder structure */ |
355 |
xparam.width = eparam->x_dim; |
xparam.width = eparam->x_dim; |
356 |
xparam.height = eparam->y_dim; |
xparam.height = eparam->y_dim; |
357 |
if ((eparam->framerate - (int)eparam->framerate) == 0) |
if ((eparam->framerate - (int)eparam->framerate) == 0) |
364 |
xparam.fincr = FRAMERATE_INCR; |
xparam.fincr = FRAMERATE_INCR; |
365 |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate); |
366 |
} |
} |
367 |
xparam.bitrate = eparam->bitrate; |
xparam.rc_bitrate = eparam->bitrate; |
368 |
xparam.rc_buffersize = 10 * eparam->bitrate; |
xparam.rc_reaction_delay_factor = 16; |
369 |
|
xparam.rc_averaging_period = 100; |
370 |
|
xparam.rc_buffer = 100; |
371 |
xparam.min_quantizer = eparam->min_quantizer; |
xparam.min_quantizer = eparam->min_quantizer; |
372 |
xparam.max_quantizer = eparam->max_quantizer; |
xparam.max_quantizer = eparam->max_quantizer; |
373 |
xparam.max_key_interval = eparam->max_key_interval; |
xparam.max_key_interval = eparam->max_key_interval; |
|
quality = eparam->quality; |
|
374 |
|
|
375 |
|
/* Create the encoder session */ |
376 |
xerr = encoder_create(&xparam); |
xerr = encoder_create(&xparam); |
377 |
|
|
378 |
eparam->handle = xparam.handle; |
eparam->handle = xparam.handle; |
379 |
|
|
380 |
|
/* Create an encoder instance in the chainlist */ |
381 |
|
if ((ecur = einst_find(xparam.handle)) == NULL) |
382 |
|
{ |
383 |
|
ecur = einst_add(xparam.handle); |
384 |
|
|
385 |
|
if(ecur == NULL) { |
386 |
|
encoder_destroy((Encoder*)xparam.handle); |
387 |
|
return ENC_MEMORY; |
388 |
|
} |
389 |
|
|
390 |
|
} |
391 |
|
|
392 |
|
ecur->quality = eparam->quality; |
393 |
|
if(ecur->quality < 0) ecur->quality = 0; |
394 |
|
if(ecur->quality > 6) ecur->quality = 6; |
395 |
|
|
396 |
break; |
break; |
397 |
} |
} |
398 |
|
|
399 |
case ENC_OPT_RELEASE : |
case ENC_OPT_RELEASE : |
400 |
{ |
{ |
401 |
|
EINST *ecur; |
402 |
|
|
403 |
|
if ((ecur = einst_find(handle)) == NULL) |
404 |
|
{ |
405 |
|
return ENC_FAIL; |
406 |
|
} |
407 |
|
|
408 |
xerr = encoder_destroy((Encoder *) handle); |
xerr = encoder_destroy((Encoder *) handle); |
409 |
break; |
break; |
410 |
} |
} |
412 |
case ENC_OPT_ENCODE : |
case ENC_OPT_ENCODE : |
413 |
case ENC_OPT_ENCODE_VBR : |
case ENC_OPT_ENCODE_VBR : |
414 |
{ |
{ |
415 |
|
EINST *ecur; |
416 |
|
|
417 |
ENC_FRAME * eframe = (ENC_FRAME *)param1; |
ENC_FRAME * eframe = (ENC_FRAME *)param1; |
418 |
ENC_RESULT * eresult = (ENC_RESULT *)param2; |
ENC_RESULT * eresult = (ENC_RESULT *)param2; |
419 |
XVID_ENC_FRAME xframe; |
XVID_ENC_FRAME xframe; |
420 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
421 |
|
|
422 |
|
if ((ecur = einst_find(handle)) == NULL) |
423 |
|
{ |
424 |
|
return ENC_FAIL; |
425 |
|
} |
426 |
|
|
427 |
|
/* Copy the divx4 info into the xvid structure */ |
428 |
xframe.bitstream = eframe->bitstream; |
xframe.bitstream = eframe->bitstream; |
429 |
xframe.length = eframe->length; |
xframe.length = eframe->length; |
430 |
|
xframe.motion = divx4_motion_presets[ecur->quality]; |
431 |
xframe.general = XVID_HALFPEL | XVID_H263QUANT; |
xframe.general = divx4_general_presets[ecur->quality]; |
|
|
|
|
if(quality > 3) |
|
|
xframe.general |= XVID_INTER4V; |
|
|
|
|
|
xframe.motion = pmvfast_presets[quality]; |
|
432 |
|
|
433 |
xframe.image = eframe->image; |
xframe.image = eframe->image; |
434 |
switch (eframe->colorspace) |
xframe.colorspace = |
435 |
{ |
xvid_to_opendivx_enc_csp(eframe->colorspace); |
|
case ENC_CSP_RGB24 : |
|
|
xframe.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24; |
|
|
break; |
|
|
case ENC_CSP_YV12 : |
|
|
xframe.colorspace = XVID_CSP_YV12; |
|
|
break; |
|
|
case ENC_CSP_YUY2 : |
|
|
xframe.colorspace = XVID_CSP_YUY2; |
|
|
break; |
|
|
case ENC_CSP_UYVY : |
|
|
xframe.colorspace = XVID_CSP_UYVY; |
|
|
break; |
|
|
case ENC_CSP_I420 : |
|
|
xframe.colorspace = XVID_CSP_I420; |
|
|
break; |
|
|
} |
|
436 |
|
|
437 |
if (opt == ENC_OPT_ENCODE_VBR) |
if (opt == ENC_OPT_ENCODE_VBR) |
438 |
{ |
{ |
445 |
xframe.quant = 0; |
xframe.quant = 0; |
446 |
} |
} |
447 |
|
|
448 |
|
/* Encode the frame */ |
449 |
xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) ); |
xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) ); |
450 |
|
|
451 |
|
/* Copy back the xvid structure to the divx4 one */ |
452 |
if (eresult) |
if (eresult) |
453 |
{ |
{ |
454 |
eresult->is_key_frame = xframe.intra; |
eresult->is_key_frame = xframe.intra; |
467 |
return ENC_FAIL; |
return ENC_FAIL; |
468 |
} |
} |
469 |
|
|
470 |
|
/* XviD Error code -> Divx4 error code */ |
471 |
switch(xerr) |
switch(xerr) |
472 |
{ |
{ |
473 |
case XVID_ERR_OK : return ENC_OK; |
case XVID_ERR_OK : |
474 |
case XVID_ERR_MEMORY : return ENC_MEMORY; |
return ENC_OK; |
475 |
case XVID_ERR_FORMAT : return ENC_BAD_FORMAT; |
case XVID_ERR_MEMORY : |
476 |
default : // case XVID_ERR_FAIL : |
return ENC_MEMORY; |
477 |
|
case XVID_ERR_FORMAT : |
478 |
|
return ENC_BAD_FORMAT; |
479 |
|
default : |
480 |
return ENC_FAIL; |
return ENC_FAIL; |
481 |
} |
} |
482 |
} |
} |
483 |
|
|
484 |
|
/************************************************************************** |
485 |
|
* Local Functions |
486 |
|
*************************************************************************/ |
487 |
|
|
488 |
|
/*************************************** |
489 |
|
* DINST chainlist helper functions * |
490 |
|
***************************************/ |
491 |
|
|
492 |
|
/* Find an element in the chainlist according to its key value */ |
493 |
|
static DINST * dinst_find(unsigned long key) |
494 |
|
{ |
495 |
|
DINST * dcur = dhead; |
496 |
|
|
497 |
|
while (dcur) |
498 |
|
{ |
499 |
|
if (dcur->key == key) |
500 |
|
{ |
501 |
|
return dcur; |
502 |
|
} |
503 |
|
dcur = dcur->next; |
504 |
|
} |
505 |
|
|
506 |
|
return NULL; |
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
|
/* Add an element to the chainlist */ |
511 |
|
static DINST * dinst_add(unsigned long key) |
512 |
|
{ |
513 |
|
DINST * dnext = dhead; |
514 |
|
|
515 |
|
dhead = malloc(sizeof(DINST)); |
516 |
|
if (dhead == NULL) |
517 |
|
{ |
518 |
|
dhead = dnext; |
519 |
|
return NULL; |
520 |
|
} |
521 |
|
|
522 |
|
dhead->key = key; |
523 |
|
dhead->next = dnext; |
524 |
|
|
525 |
|
return dhead; |
526 |
|
} |
527 |
|
|
528 |
|
|
529 |
|
/* Remove an elmement from the chainlist */ |
530 |
|
static void dinst_remove(unsigned long key) |
531 |
|
{ |
532 |
|
DINST * dcur = dhead; |
533 |
|
|
534 |
|
if (dhead == NULL) |
535 |
|
{ |
536 |
|
return; |
537 |
|
} |
538 |
|
|
539 |
|
if (dcur->key == key) |
540 |
|
{ |
541 |
|
dhead = dcur->next; |
542 |
|
free(dcur); |
543 |
|
return; |
544 |
|
} |
545 |
|
|
546 |
|
while (dcur->next) |
547 |
|
{ |
548 |
|
if (dcur->next->key == key) |
549 |
|
{ |
550 |
|
DINST * tmp = dcur->next; |
551 |
|
dcur->next = tmp->next; |
552 |
|
free(tmp); |
553 |
|
return; |
554 |
|
} |
555 |
|
dcur = dcur->next; |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
|
/*************************************** |
561 |
|
* EINST chainlist helper functions * |
562 |
|
***************************************/ |
563 |
|
|
564 |
|
/* Find an element in the chainlist according to its handle */ |
565 |
|
static EINST * einst_find(void *handle) |
566 |
|
{ |
567 |
|
EINST * ecur = ehead; |
568 |
|
|
569 |
|
while (ecur) |
570 |
|
{ |
571 |
|
if (ecur->handle == handle) |
572 |
|
{ |
573 |
|
return ecur; |
574 |
|
} |
575 |
|
ecur = ecur->next; |
576 |
|
} |
577 |
|
|
578 |
|
return NULL; |
579 |
|
} |
580 |
|
|
581 |
|
|
582 |
|
/* Add an element to the chainlist */ |
583 |
|
static EINST * einst_add(void *handle) |
584 |
|
{ |
585 |
|
EINST * enext = ehead; |
586 |
|
|
587 |
|
ehead = malloc(sizeof(EINST)); |
588 |
|
if (ehead == NULL) |
589 |
|
{ |
590 |
|
ehead = enext; |
591 |
|
return NULL; |
592 |
|
} |
593 |
|
|
594 |
|
ehead->handle = handle; |
595 |
|
ehead->next = enext; |
596 |
|
|
597 |
|
return ehead; |
598 |
|
} |
599 |
|
|
600 |
|
|
601 |
|
/* Remove an elmement from the chainlist */ |
602 |
|
static void einst_remove(void *handle) |
603 |
|
{ |
604 |
|
EINST * ecur = ehead; |
605 |
|
|
606 |
|
if (ehead == NULL) |
607 |
|
{ |
608 |
|
return; |
609 |
|
} |
610 |
|
|
611 |
|
if (ecur->handle == handle) |
612 |
|
{ |
613 |
|
ehead = ecur->next; |
614 |
|
free(ecur); |
615 |
|
return; |
616 |
|
} |
617 |
|
|
618 |
|
while (ecur->next) |
619 |
|
{ |
620 |
|
if (ecur->next->handle == handle) |
621 |
|
{ |
622 |
|
EINST * tmp = ecur->next; |
623 |
|
ecur->next = tmp->next; |
624 |
|
free(tmp); |
625 |
|
return; |
626 |
|
} |
627 |
|
ecur = ecur->next; |
628 |
|
} |
629 |
|
} |
630 |
|
/*************************************** |
631 |
|
* Colorspace code converter * |
632 |
|
***************************************/ |
633 |
|
|
634 |
|
static int xvid_to_opendivx_dec_csp(int csp) |
635 |
|
{ |
636 |
|
|
637 |
|
switch(csp) |
638 |
|
{ |
639 |
|
case DEC_YV12 : |
640 |
|
return XVID_CSP_YV12; |
641 |
|
case DEC_420 : |
642 |
|
return XVID_CSP_I420; |
643 |
|
case DEC_YUY2 : |
644 |
|
return XVID_CSP_YUY2; |
645 |
|
case DEC_UYVY : |
646 |
|
return XVID_CSP_UYVY; |
647 |
|
case DEC_RGB32 : |
648 |
|
return XVID_CSP_VFLIP | XVID_CSP_RGB32; |
649 |
|
case DEC_RGB24 : |
650 |
|
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
651 |
|
case DEC_RGB565 : |
652 |
|
return XVID_CSP_VFLIP | XVID_CSP_RGB565; |
653 |
|
case DEC_RGB555 : |
654 |
|
return XVID_CSP_VFLIP | XVID_CSP_RGB555; |
655 |
|
case DEC_RGB32_INV : |
656 |
|
return XVID_CSP_RGB32; |
657 |
|
case DEC_RGB24_INV : |
658 |
|
return XVID_CSP_RGB24; |
659 |
|
case DEC_RGB565_INV : |
660 |
|
return XVID_CSP_RGB565; |
661 |
|
case DEC_RGB555_INV : |
662 |
|
return XVID_CSP_RGB555; |
663 |
|
case DEC_USER : |
664 |
|
return XVID_CSP_USER; |
665 |
|
default : |
666 |
|
return -1; |
667 |
|
} |
668 |
|
} |
669 |
|
|
670 |
|
static int xvid_to_opendivx_enc_csp(int csp) |
671 |
|
{ |
672 |
|
|
673 |
|
switch (csp) |
674 |
|
{ |
675 |
|
case ENC_CSP_RGB24 : |
676 |
|
return XVID_CSP_VFLIP | XVID_CSP_RGB24; |
677 |
|
case ENC_CSP_YV12 : |
678 |
|
return XVID_CSP_YV12; |
679 |
|
case ENC_CSP_YUY2 : |
680 |
|
return XVID_CSP_YUY2; |
681 |
|
case ENC_CSP_UYVY : |
682 |
|
return XVID_CSP_UYVY; |
683 |
|
case ENC_CSP_I420 : |
684 |
|
return XVID_CSP_I420; |
685 |
|
default : |
686 |
|
return -1; |
687 |
|
} |
688 |
|
} |