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