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

Annotation of /trunk/xvidcore/src/divx4.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (view) (download)

1 : Isibaar 3 /**************************************************************************
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 129 #ifndef FREEBSD
43 : Isibaar 3 #include <malloc.h>
44 : knhor 129 #else
45 :     #include <stdlib.h>
46 :     #endif
47 : Isibaar 3 #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 19 int csp_tmp = 0;
258 :    
259 : Isibaar 3 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 10 int divx4_motion_presets[7] = {
317 : Isibaar 9 0, PMV_QUICKSTOP16, PMV_EARLYSTOP16, PMV_EARLYSTOP16 | PMV_EARLYSTOP8,
318 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
319 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
320 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 |
321 :     PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8
322 :     };
323 :    
324 :     int quality;
325 :    
326 : Isibaar 3 int encore(void * handle, int opt, void * param1, void * param2)
327 :     {
328 :     int xerr;
329 :    
330 :     switch(opt)
331 :     {
332 :     case ENC_OPT_INIT :
333 :     {
334 :     ENC_PARAM * eparam = (ENC_PARAM *)param1;
335 :     XVID_INIT_PARAM xinit;
336 :     XVID_ENC_PARAM xparam;
337 :    
338 :     xinit.cpu_flags = 0;
339 :     xvid_init(NULL, 0, &xinit, NULL);
340 :    
341 :     xparam.width = eparam->x_dim;
342 :     xparam.height = eparam->y_dim;
343 :     if ((eparam->framerate - (int)eparam->framerate) == 0)
344 :     {
345 :     xparam.fincr = 1;
346 :     xparam.fbase = (int)eparam->framerate;
347 :     }
348 :     else
349 :     {
350 :     xparam.fincr = FRAMERATE_INCR;
351 :     xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
352 :     }
353 : h 121 xparam.rc_bitrate = eparam->bitrate;
354 :     xparam.rc_reaction_delay_factor = 16;
355 :     xparam.rc_averaging_period = 100;
356 :     xparam.rc_buffer = 100;
357 : Isibaar 3 xparam.min_quantizer = eparam->min_quantizer;
358 :     xparam.max_quantizer = eparam->max_quantizer;
359 :     xparam.max_key_interval = eparam->max_key_interval;
360 : Isibaar 9 quality = eparam->quality;
361 : Isibaar 3
362 :     xerr = encoder_create(&xparam);
363 :    
364 :     eparam->handle = xparam.handle;
365 :    
366 :     break;
367 :     }
368 :    
369 :     case ENC_OPT_RELEASE :
370 :     {
371 :     xerr = encoder_destroy((Encoder *) handle);
372 :     break;
373 :     }
374 :    
375 :     case ENC_OPT_ENCODE :
376 :     case ENC_OPT_ENCODE_VBR :
377 :     {
378 :     ENC_FRAME * eframe = (ENC_FRAME *)param1;
379 :     ENC_RESULT * eresult = (ENC_RESULT *)param2;
380 :     XVID_ENC_FRAME xframe;
381 :     XVID_ENC_STATS xstats;
382 :    
383 :     xframe.bitstream = eframe->bitstream;
384 :     xframe.length = eframe->length;
385 :    
386 : Isibaar 9 xframe.general = XVID_HALFPEL | XVID_H263QUANT;
387 :    
388 :     if(quality > 3)
389 :     xframe.general |= XVID_INTER4V;
390 :    
391 : Isibaar 10 xframe.motion = divx4_motion_presets[quality];
392 : Isibaar 9
393 : Isibaar 3 xframe.image = eframe->image;
394 :     switch (eframe->colorspace)
395 :     {
396 :     case ENC_CSP_RGB24 :
397 :     xframe.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24;
398 :     break;
399 :     case ENC_CSP_YV12 :
400 :     xframe.colorspace = XVID_CSP_YV12;
401 :     break;
402 :     case ENC_CSP_YUY2 :
403 :     xframe.colorspace = XVID_CSP_YUY2;
404 :     break;
405 :     case ENC_CSP_UYVY :
406 :     xframe.colorspace = XVID_CSP_UYVY;
407 :     break;
408 :     case ENC_CSP_I420 :
409 :     xframe.colorspace = XVID_CSP_I420;
410 :     break;
411 :     }
412 :    
413 :     if (opt == ENC_OPT_ENCODE_VBR)
414 :     {
415 :     xframe.intra = eframe->intra;
416 :     xframe.quant = eframe->quant;
417 :     }
418 :     else
419 :     {
420 :     xframe.intra = -1;
421 :     xframe.quant = 0;
422 :     }
423 :    
424 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
425 :    
426 :     if (eresult)
427 :     {
428 :     eresult->is_key_frame = xframe.intra;
429 :     eresult->quantizer = xstats.quant;
430 :     eresult->total_bits = xframe.length * 8;
431 :     eresult->motion_bits = xstats.hlength * 8;
432 :     eresult->texture_bits = eresult->total_bits - eresult->motion_bits;
433 :     }
434 :    
435 :     eframe->length = xframe.length;
436 :    
437 :     break;
438 :     }
439 :    
440 :     default:
441 :     return ENC_FAIL;
442 :     }
443 :    
444 :     switch(xerr)
445 :     {
446 :     case XVID_ERR_OK : return ENC_OK;
447 :     case XVID_ERR_MEMORY : return ENC_MEMORY;
448 :     case XVID_ERR_FORMAT : return ENC_BAD_FORMAT;
449 :     default : // case XVID_ERR_FAIL :
450 :     return ENC_FAIL;
451 :     }
452 :     }
453 :    

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