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