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