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