[cvs] / xvidcore / src / divx4.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/divx4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (view) (download)

1 : Isibaar 1.1 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * opendivx api wrapper
5 :     *
6 :     * 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
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the Free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the Free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 :     *************************************************************************/
30 :    
31 :     /**************************************************************************
32 :     *
33 :     * History:
34 :     *
35 :     * 26.02.2001 fixed dec_csp bugs
36 :     * 26.12.2001 xvid_init() support
37 :     * 22.12.2001 removed some compiler warnings
38 :     * 16.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
39 :     *
40 :     *************************************************************************/
41 :    
42 :    
43 :     #include <malloc.h>
44 :     #include <string.h> // memset
45 :    
46 :     #include "xvid.h"
47 :     #include "divx4.h"
48 :     #include "decoder.h"
49 :     #include "encoder.h"
50 :    
51 :     #define EMULATED_DIVX_VERSION 20011001
52 :    
53 :     // decore
54 :    
55 :    
56 :     typedef struct DINST
57 :     {
58 :     unsigned long key;
59 :     struct DINST * next;
60 :    
61 :     void * handle;
62 :     XVID_DEC_FRAME xframe;
63 :    
64 :     } DINST;
65 :    
66 :    
67 :     static DINST * dhead = NULL;
68 :    
69 :    
70 :     DINST * dinst_find(unsigned long key)
71 :     {
72 :     DINST * dcur = dhead;
73 :    
74 :     while (dcur)
75 :     {
76 :     if (dcur->key == key)
77 :     {
78 :     return dcur;
79 :     }
80 :     dcur = dcur->next;
81 :     }
82 :    
83 :     return NULL;
84 :     }
85 :    
86 :    
87 :     DINST * dinst_add(unsigned long key)
88 :     {
89 :     DINST * dnext = dhead;
90 :    
91 :     dhead = malloc(sizeof(DINST));
92 :     if (dhead == NULL)
93 :     {
94 :     dhead = dnext;
95 :     return NULL;
96 :     }
97 :    
98 :     dhead->key = key;
99 :     dhead->next = dnext;
100 :    
101 :     return dhead;
102 :     }
103 :    
104 :    
105 :     void dinst_remove(unsigned long key)
106 :     {
107 :     DINST * dcur = dhead;
108 :    
109 :     if (dhead == NULL)
110 :     {
111 :     return;
112 :     }
113 :    
114 :     if (dcur->key == key)
115 :     {
116 :     dhead = dcur->next;
117 :     free(dcur);
118 :     return;
119 :     }
120 :    
121 :     while (dcur->next)
122 :     {
123 :     if (dcur->next->key == key)
124 :     {
125 :     DINST * tmp = dcur->next;
126 :     dcur->next = tmp->next;
127 :     free(tmp);
128 :     return;
129 :     }
130 :     dcur = dcur->next;
131 :     }
132 :     }
133 :    
134 :    
135 :     int xvid_to_opendivx_dec_csp(int csp)
136 :     {
137 :     switch(csp)
138 :     {
139 :     case DEC_YV12 :
140 :     return XVID_CSP_YV12;
141 :     case DEC_420 :
142 :     return XVID_CSP_I420;
143 :     case DEC_YUY2 :
144 :     return XVID_CSP_YUY2;
145 :     case DEC_UYVY :
146 :     return XVID_CSP_UYVY;
147 :     case DEC_RGB32 :
148 :     return XVID_CSP_VFLIP | XVID_CSP_RGB32;
149 :     case DEC_RGB24 :
150 :     return XVID_CSP_VFLIP | XVID_CSP_RGB24;
151 :     case DEC_RGB565 :
152 :     return XVID_CSP_VFLIP | XVID_CSP_RGB565;
153 :     case DEC_RGB555 :
154 :     return XVID_CSP_VFLIP | XVID_CSP_RGB555;
155 :     case DEC_RGB32_INV :
156 :     return XVID_CSP_RGB32;
157 :     case DEC_RGB24_INV :
158 :     return XVID_CSP_RGB24;
159 :     case DEC_RGB565_INV :
160 :     return XVID_CSP_RGB565;
161 :     case DEC_RGB555_INV :
162 :     return XVID_CSP_RGB555;
163 :     case DEC_USER :
164 :     return XVID_CSP_USER;
165 :     default :
166 :     return -1;
167 :     }
168 :     }
169 :    
170 :    
171 :     int decore(unsigned long key, unsigned long opt,
172 :     void * param1, void * param2)
173 :     {
174 :     int xerr;
175 :    
176 :     switch (opt)
177 :     {
178 :     case DEC_OPT_MEMORY_REQS :
179 :     {
180 :     memset(param2, 0, sizeof(DEC_MEM_REQS));
181 :     return DEC_OK;
182 :     }
183 :    
184 :     case DEC_OPT_INIT :
185 :     {
186 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
187 :     XVID_INIT_PARAM xinit;
188 :     XVID_DEC_PARAM xparam;
189 :     DINST * dcur = dinst_find(key);
190 :     if (dcur == NULL)
191 :     {
192 :     dcur = dinst_add(key);
193 :     }
194 :    
195 :     xinit.cpu_flags = 0;
196 :     xvid_init(NULL, 0, &xinit, NULL);
197 :    
198 :     xparam.width = dparam->x_dim;
199 :     xparam.height = dparam->y_dim;
200 :     dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format);
201 :    
202 :     xerr = decoder_create(&xparam);
203 :    
204 :     dcur->handle = xparam.handle;
205 :    
206 :     break;
207 :     }
208 :    
209 :     case DEC_OPT_RELEASE :
210 :     {
211 :     DINST * dcur = dinst_find(key);
212 :     if (dcur == NULL)
213 :     {
214 :     return DEC_EXIT;
215 :     }
216 :    
217 :     xerr = decoder_destroy(dcur->handle);
218 :    
219 :     dinst_remove(key);
220 :    
221 :     break;
222 :     }
223 :    
224 :     case DEC_OPT_SETPP :
225 :     {
226 :     // DEC_SET * dset = (DEC_SET *)param1;
227 :     DINST * dcur = dinst_find(key);
228 :     if (dcur == NULL)
229 :     {
230 :     return DEC_EXIT;
231 :     }
232 :    
233 :     // dcur->xframe.pp = dset->postproc_level;
234 :    
235 :     return DEC_OK;
236 :     }
237 :    
238 :     case DEC_OPT_SETOUT :
239 :     {
240 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
241 :     DINST * dcur = dinst_find(key);
242 :     if (dcur == NULL)
243 :     {
244 :     return DEC_EXIT;
245 :     }
246 :    
247 :     dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format);
248 :    
249 :     return DEC_OK;
250 :     }
251 :    
252 :     case DEC_OPT_FRAME:
253 :     {
254 :     int csp_tmp;
255 :     DEC_FRAME * dframe = (DEC_FRAME *)param1;
256 :     DINST * dcur = dinst_find(key);
257 :     if (dcur == NULL)
258 :     {
259 :     return DEC_EXIT;
260 :     }
261 :    
262 :     dcur->xframe.bitstream = dframe->bitstream;
263 :     dcur->xframe.length = dframe->length;
264 :     dcur->xframe.image = dframe->bmp;
265 :     dcur->xframe.stride = dframe->stride;
266 :    
267 :     if (!dframe->render_flag)
268 :     {
269 :     csp_tmp = dcur->xframe.colorspace;
270 :     dcur->xframe.colorspace = XVID_CSP_NULL;
271 :     }
272 :    
273 :     xerr = decoder_decode(dcur->handle, &dcur->xframe);
274 :    
275 :     if (!dframe->render_flag)
276 :     {
277 :     dcur->xframe.colorspace = csp_tmp;
278 :     }
279 :    
280 :     break;
281 :     }
282 :    
283 :    
284 :     case DEC_OPT_FRAME_311 :
285 :     return DEC_EXIT;
286 :    
287 :    
288 :     case DEC_OPT_VERSION:
289 :     return EMULATED_DIVX_VERSION;
290 :     default :
291 :     return DEC_EXIT;
292 :     }
293 :    
294 :    
295 :     switch(xerr)
296 :     {
297 :     case XVID_ERR_OK : return DEC_OK;
298 :     case XVID_ERR_MEMORY : return DEC_MEMORY;
299 :     case XVID_ERR_FORMAT : return DEC_BAD_FORMAT;
300 :     default : // case XVID_ERR_FAIL :
301 :     return DEC_EXIT;
302 :     }
303 :     }
304 :    
305 :    
306 :    
307 :    
308 :     // encore
309 :    
310 :     #define FRAMERATE_INCR 1001
311 :    
312 :     int encore(void * handle, int opt, void * param1, void * param2)
313 :     {
314 :     int xerr;
315 :    
316 :     switch(opt)
317 :     {
318 :     case ENC_OPT_INIT :
319 :     {
320 :     ENC_PARAM * eparam = (ENC_PARAM *)param1;
321 :     XVID_INIT_PARAM xinit;
322 :     XVID_ENC_PARAM xparam;
323 :    
324 :     xinit.cpu_flags = 0;
325 :     xvid_init(NULL, 0, &xinit, NULL);
326 :    
327 :     xparam.width = eparam->x_dim;
328 :     xparam.height = eparam->y_dim;
329 :     if ((eparam->framerate - (int)eparam->framerate) == 0)
330 :     {
331 :     xparam.fincr = 1;
332 :     xparam.fbase = (int)eparam->framerate;
333 :     }
334 :     else
335 :     {
336 :     xparam.fincr = FRAMERATE_INCR;
337 :     xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
338 :     }
339 :     xparam.bitrate = eparam->bitrate;
340 :     xparam.rc_buffersize = eparam->bitrate;
341 :     xparam.min_quantizer = eparam->min_quantizer;
342 :     xparam.max_quantizer = eparam->max_quantizer;
343 :     xparam.max_key_interval = eparam->max_key_interval;
344 :    
345 :     xerr = encoder_create(&xparam);
346 :    
347 :     eparam->handle = xparam.handle;
348 :    
349 :     break;
350 :     }
351 :    
352 :     case ENC_OPT_RELEASE :
353 :     {
354 :     xerr = encoder_destroy((Encoder *) handle);
355 :     break;
356 :     }
357 :    
358 :     case ENC_OPT_ENCODE :
359 :     case ENC_OPT_ENCODE_VBR :
360 :     {
361 :     ENC_FRAME * eframe = (ENC_FRAME *)param1;
362 :     ENC_RESULT * eresult = (ENC_RESULT *)param2;
363 :     XVID_ENC_FRAME xframe;
364 :     XVID_ENC_STATS xstats;
365 :    
366 :     xframe.bitstream = eframe->bitstream;
367 :     xframe.length = eframe->length;
368 :    
369 :     xframe.image = eframe->image;
370 :     switch (eframe->colorspace)
371 :     {
372 :     case ENC_CSP_RGB24 :
373 :     xframe.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24;
374 :     break;
375 :     case ENC_CSP_YV12 :
376 :     xframe.colorspace = XVID_CSP_YV12;
377 :     break;
378 :     case ENC_CSP_YUY2 :
379 :     xframe.colorspace = XVID_CSP_YUY2;
380 :     break;
381 :     case ENC_CSP_UYVY :
382 :     xframe.colorspace = XVID_CSP_UYVY;
383 :     break;
384 :     case ENC_CSP_I420 :
385 :     xframe.colorspace = XVID_CSP_I420;
386 :     break;
387 :     }
388 :    
389 :     if (opt == ENC_OPT_ENCODE_VBR)
390 :     {
391 :     xframe.intra = eframe->intra;
392 :     xframe.quant = eframe->quant;
393 :     }
394 :     else
395 :     {
396 :     xframe.intra = -1;
397 :     xframe.quant = 0;
398 :     }
399 :    
400 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
401 :    
402 :     if (eresult)
403 :     {
404 :     eresult->is_key_frame = xframe.intra;
405 :     eresult->quantizer = xstats.quant;
406 :     eresult->total_bits = xframe.length * 8;
407 :     eresult->motion_bits = xstats.hlength * 8;
408 :     eresult->texture_bits = eresult->total_bits - eresult->motion_bits;
409 :     }
410 :    
411 :     eframe->length = xframe.length;
412 :    
413 :     break;
414 :     }
415 :    
416 :     default:
417 :     return ENC_FAIL;
418 :     }
419 :    
420 :     switch(xerr)
421 :     {
422 :     case XVID_ERR_OK : return ENC_OK;
423 :     case XVID_ERR_MEMORY : return ENC_MEMORY;
424 :     case XVID_ERR_FORMAT : return ENC_BAD_FORMAT;
425 :     default : // case XVID_ERR_FAIL :
426 :     return ENC_FAIL;
427 :     }
428 :     }
429 :    

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4