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

Annotation of /xvidcore/src/divx4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (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 : knhor 1.7 #ifndef FREEBSD
43 : Isibaar 1.1 #include <malloc.h>
44 : knhor 1.7 #else
45 :     #include <stdlib.h>
46 :     #endif
47 : Isibaar 1.1 #include <string.h> // memset
48 :    
49 :     #include "xvid.h"
50 :     #include "divx4.h"
51 :     #include "decoder.h"
52 :     #include "encoder.h"
53 :    
54 :     #define EMULATED_DIVX_VERSION 20011001
55 :    
56 :     // decore
57 :    
58 :    
59 :     typedef struct DINST
60 :     {
61 :     unsigned long key;
62 :     struct DINST * next;
63 :    
64 :     void * handle;
65 :     XVID_DEC_FRAME xframe;
66 :    
67 :     } DINST;
68 :    
69 :    
70 :     static DINST * dhead = NULL;
71 :    
72 :    
73 :     DINST * dinst_find(unsigned long key)
74 :     {
75 :     DINST * dcur = dhead;
76 :    
77 :     while (dcur)
78 :     {
79 :     if (dcur->key == key)
80 :     {
81 :     return dcur;
82 :     }
83 :     dcur = dcur->next;
84 :     }
85 :    
86 :     return NULL;
87 :     }
88 :    
89 :    
90 :     DINST * dinst_add(unsigned long key)
91 :     {
92 :     DINST * dnext = dhead;
93 :    
94 :     dhead = malloc(sizeof(DINST));
95 :     if (dhead == NULL)
96 :     {
97 :     dhead = dnext;
98 :     return NULL;
99 :     }
100 :    
101 :     dhead->key = key;
102 :     dhead->next = dnext;
103 :    
104 :     return dhead;
105 :     }
106 :    
107 :    
108 :     void dinst_remove(unsigned long key)
109 :     {
110 :     DINST * dcur = dhead;
111 :    
112 :     if (dhead == NULL)
113 :     {
114 :     return;
115 :     }
116 :    
117 :     if (dcur->key == key)
118 :     {
119 :     dhead = dcur->next;
120 :     free(dcur);
121 :     return;
122 :     }
123 :    
124 :     while (dcur->next)
125 :     {
126 :     if (dcur->next->key == key)
127 :     {
128 :     DINST * tmp = dcur->next;
129 :     dcur->next = tmp->next;
130 :     free(tmp);
131 :     return;
132 :     }
133 :     dcur = dcur->next;
134 :     }
135 :     }
136 :    
137 :    
138 :     int xvid_to_opendivx_dec_csp(int csp)
139 :     {
140 :     switch(csp)
141 :     {
142 :     case DEC_YV12 :
143 :     return XVID_CSP_YV12;
144 :     case DEC_420 :
145 :     return XVID_CSP_I420;
146 :     case DEC_YUY2 :
147 :     return XVID_CSP_YUY2;
148 :     case DEC_UYVY :
149 :     return XVID_CSP_UYVY;
150 :     case DEC_RGB32 :
151 :     return XVID_CSP_VFLIP | XVID_CSP_RGB32;
152 :     case DEC_RGB24 :
153 :     return XVID_CSP_VFLIP | XVID_CSP_RGB24;
154 :     case DEC_RGB565 :
155 :     return XVID_CSP_VFLIP | XVID_CSP_RGB565;
156 :     case DEC_RGB555 :
157 :     return XVID_CSP_VFLIP | XVID_CSP_RGB555;
158 :     case DEC_RGB32_INV :
159 :     return XVID_CSP_RGB32;
160 :     case DEC_RGB24_INV :
161 :     return XVID_CSP_RGB24;
162 :     case DEC_RGB565_INV :
163 :     return XVID_CSP_RGB565;
164 :     case DEC_RGB555_INV :
165 :     return XVID_CSP_RGB555;
166 :     case DEC_USER :
167 :     return XVID_CSP_USER;
168 :     default :
169 :     return -1;
170 :     }
171 :     }
172 :    
173 :    
174 :     int decore(unsigned long key, unsigned long opt,
175 :     void * param1, void * param2)
176 :     {
177 :     int xerr;
178 :    
179 :     switch (opt)
180 :     {
181 :     case DEC_OPT_MEMORY_REQS :
182 :     {
183 :     memset(param2, 0, sizeof(DEC_MEM_REQS));
184 :     return DEC_OK;
185 :     }
186 :    
187 :     case DEC_OPT_INIT :
188 :     {
189 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
190 :     XVID_INIT_PARAM xinit;
191 :     XVID_DEC_PARAM xparam;
192 :     DINST * dcur = dinst_find(key);
193 :     if (dcur == NULL)
194 :     {
195 :     dcur = dinst_add(key);
196 :     }
197 :    
198 :     xinit.cpu_flags = 0;
199 :     xvid_init(NULL, 0, &xinit, NULL);
200 :    
201 :     xparam.width = dparam->x_dim;
202 :     xparam.height = dparam->y_dim;
203 :     dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format);
204 :    
205 :     xerr = decoder_create(&xparam);
206 :    
207 :     dcur->handle = xparam.handle;
208 :    
209 :     break;
210 :     }
211 :    
212 :     case DEC_OPT_RELEASE :
213 :     {
214 :     DINST * dcur = dinst_find(key);
215 :     if (dcur == NULL)
216 :     {
217 :     return DEC_EXIT;
218 :     }
219 :    
220 :     xerr = decoder_destroy(dcur->handle);
221 :    
222 :     dinst_remove(key);
223 :    
224 :     break;
225 :     }
226 :    
227 :     case DEC_OPT_SETPP :
228 :     {
229 :     // DEC_SET * dset = (DEC_SET *)param1;
230 :     DINST * dcur = dinst_find(key);
231 :     if (dcur == NULL)
232 :     {
233 :     return DEC_EXIT;
234 :     }
235 :    
236 :     // dcur->xframe.pp = dset->postproc_level;
237 :    
238 :     return DEC_OK;
239 :     }
240 :    
241 :     case DEC_OPT_SETOUT :
242 :     {
243 :     DEC_PARAM * dparam = (DEC_PARAM *)param1;
244 :     DINST * dcur = dinst_find(key);
245 :     if (dcur == NULL)
246 :     {
247 :     return DEC_EXIT;
248 :     }
249 :    
250 :     dcur->xframe.colorspace = xvid_to_opendivx_dec_csp(dparam->output_format);
251 :    
252 :     return DEC_OK;
253 :     }
254 :    
255 :     case DEC_OPT_FRAME:
256 :     {
257 : edgomez 1.4 int csp_tmp = 0;
258 :    
259 : Isibaar 1.1 DEC_FRAME * dframe = (DEC_FRAME *)param1;
260 :     DINST * dcur = dinst_find(key);
261 :     if (dcur == NULL)
262 :     {
263 :     return DEC_EXIT;
264 :     }
265 :    
266 :     dcur->xframe.bitstream = dframe->bitstream;
267 :     dcur->xframe.length = dframe->length;
268 :     dcur->xframe.image = dframe->bmp;
269 :     dcur->xframe.stride = dframe->stride;
270 :    
271 :     if (!dframe->render_flag)
272 :     {
273 :     csp_tmp = dcur->xframe.colorspace;
274 :     dcur->xframe.colorspace = XVID_CSP_NULL;
275 :     }
276 :    
277 :     xerr = decoder_decode(dcur->handle, &dcur->xframe);
278 :    
279 :     if (!dframe->render_flag)
280 :     {
281 :     dcur->xframe.colorspace = csp_tmp;
282 :     }
283 :    
284 :     break;
285 :     }
286 :    
287 :    
288 :     case DEC_OPT_FRAME_311 :
289 :     return DEC_EXIT;
290 :    
291 :    
292 :     case DEC_OPT_VERSION:
293 :     return EMULATED_DIVX_VERSION;
294 :     default :
295 :     return DEC_EXIT;
296 :     }
297 :    
298 :    
299 :     switch(xerr)
300 :     {
301 :     case XVID_ERR_OK : return DEC_OK;
302 :     case XVID_ERR_MEMORY : return DEC_MEMORY;
303 :     case XVID_ERR_FORMAT : return DEC_BAD_FORMAT;
304 :     default : // case XVID_ERR_FAIL :
305 :     return DEC_EXIT;
306 :     }
307 :     }
308 :    
309 :    
310 :    
311 :    
312 :     // encore
313 :    
314 :     #define FRAMERATE_INCR 1001
315 :    
316 : Isibaar 1.3 int divx4_motion_presets[7] = {
317 : chl 1.8 0,
318 :     PMV_QUICKSTOP16,
319 :     PMV_EARLYSTOP16,
320 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
321 : Isibaar 1.2 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
322 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
323 : chl 1.8 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8
324 :     };
325 :    
326 :    
327 :     int divx4_general_presets[7] = {
328 :     0,
329 :     XVID_H263QUANT,
330 :     XVID_H263QUANT,
331 :     XVID_H263QUANT | XVID_HALFPEL,
332 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
333 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
334 :     XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL
335 : Isibaar 1.2 };
336 :    
337 :     int quality;
338 :    
339 : Isibaar 1.1 int encore(void * handle, int opt, void * param1, void * param2)
340 :     {
341 :     int xerr;
342 :    
343 :     switch(opt)
344 :     {
345 :     case ENC_OPT_INIT :
346 :     {
347 :     ENC_PARAM * eparam = (ENC_PARAM *)param1;
348 :     XVID_INIT_PARAM xinit;
349 :     XVID_ENC_PARAM xparam;
350 :    
351 :     xinit.cpu_flags = 0;
352 :     xvid_init(NULL, 0, &xinit, NULL);
353 :    
354 :     xparam.width = eparam->x_dim;
355 :     xparam.height = eparam->y_dim;
356 :     if ((eparam->framerate - (int)eparam->framerate) == 0)
357 :     {
358 :     xparam.fincr = 1;
359 :     xparam.fbase = (int)eparam->framerate;
360 :     }
361 :     else
362 :     {
363 :     xparam.fincr = FRAMERATE_INCR;
364 :     xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
365 :     }
366 : h 1.6 xparam.rc_bitrate = eparam->bitrate;
367 :     xparam.rc_reaction_delay_factor = 16;
368 :     xparam.rc_averaging_period = 100;
369 :     xparam.rc_buffer = 100;
370 : Isibaar 1.1 xparam.min_quantizer = eparam->min_quantizer;
371 :     xparam.max_quantizer = eparam->max_quantizer;
372 :     xparam.max_key_interval = eparam->max_key_interval;
373 : Isibaar 1.2 quality = eparam->quality;
374 : Isibaar 1.1
375 :     xerr = encoder_create(&xparam);
376 :    
377 :     eparam->handle = xparam.handle;
378 :    
379 :     break;
380 :     }
381 :    
382 :     case ENC_OPT_RELEASE :
383 :     {
384 :     xerr = encoder_destroy((Encoder *) handle);
385 :     break;
386 :     }
387 :    
388 :     case ENC_OPT_ENCODE :
389 :     case ENC_OPT_ENCODE_VBR :
390 :     {
391 :     ENC_FRAME * eframe = (ENC_FRAME *)param1;
392 :     ENC_RESULT * eresult = (ENC_RESULT *)param2;
393 :     XVID_ENC_FRAME xframe;
394 :     XVID_ENC_STATS xstats;
395 :    
396 :     xframe.bitstream = eframe->bitstream;
397 :     xframe.length = eframe->length;
398 : Isibaar 1.2
399 : Isibaar 1.3 xframe.motion = divx4_motion_presets[quality];
400 : chl 1.8 xframe.general = divx4_general_presets[quality];
401 : Isibaar 1.1
402 :     xframe.image = eframe->image;
403 :     switch (eframe->colorspace)
404 :     {
405 :     case ENC_CSP_RGB24 :
406 :     xframe.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24;
407 :     break;
408 :     case ENC_CSP_YV12 :
409 :     xframe.colorspace = XVID_CSP_YV12;
410 :     break;
411 :     case ENC_CSP_YUY2 :
412 :     xframe.colorspace = XVID_CSP_YUY2;
413 :     break;
414 :     case ENC_CSP_UYVY :
415 :     xframe.colorspace = XVID_CSP_UYVY;
416 :     break;
417 :     case ENC_CSP_I420 :
418 :     xframe.colorspace = XVID_CSP_I420;
419 :     break;
420 :     }
421 :    
422 :     if (opt == ENC_OPT_ENCODE_VBR)
423 :     {
424 :     xframe.intra = eframe->intra;
425 :     xframe.quant = eframe->quant;
426 :     }
427 :     else
428 :     {
429 :     xframe.intra = -1;
430 :     xframe.quant = 0;
431 :     }
432 :    
433 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
434 :    
435 :     if (eresult)
436 :     {
437 :     eresult->is_key_frame = xframe.intra;
438 :     eresult->quantizer = xstats.quant;
439 :     eresult->total_bits = xframe.length * 8;
440 :     eresult->motion_bits = xstats.hlength * 8;
441 :     eresult->texture_bits = eresult->total_bits - eresult->motion_bits;
442 :     }
443 :    
444 :     eframe->length = xframe.length;
445 :    
446 :     break;
447 :     }
448 :    
449 :     default:
450 :     return ENC_FAIL;
451 :     }
452 :    
453 :     switch(xerr)
454 :     {
455 :     case XVID_ERR_OK : return ENC_OK;
456 :     case XVID_ERR_MEMORY : return ENC_MEMORY;
457 :     case XVID_ERR_FORMAT : return ENC_BAD_FORMAT;
458 :     default : // case XVID_ERR_FAIL :
459 :     return ENC_FAIL;
460 :     }
461 :     }
462 :    

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