[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 19 - (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 :    
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 : edgomez 19 int csp_tmp = 0;
255 :    
256 : Isibaar 3 DEC_FRAME * dframe = (DEC_FRAME *)param1;
257 :     DINST * dcur = dinst_find(key);
258 :     if (dcur == NULL)
259 :     {
260 :     return DEC_EXIT;
261 :     }
262 :    
263 :     dcur->xframe.bitstream = dframe->bitstream;
264 :     dcur->xframe.length = dframe->length;
265 :     dcur->xframe.image = dframe->bmp;
266 :     dcur->xframe.stride = dframe->stride;
267 :    
268 :     if (!dframe->render_flag)
269 :     {
270 :     csp_tmp = dcur->xframe.colorspace;
271 :     dcur->xframe.colorspace = XVID_CSP_NULL;
272 :     }
273 :    
274 :     xerr = decoder_decode(dcur->handle, &dcur->xframe);
275 :    
276 :     if (!dframe->render_flag)
277 :     {
278 :     dcur->xframe.colorspace = csp_tmp;
279 :     }
280 :    
281 :     break;
282 :     }
283 :    
284 :    
285 :     case DEC_OPT_FRAME_311 :
286 :     return DEC_EXIT;
287 :    
288 :    
289 :     case DEC_OPT_VERSION:
290 :     return EMULATED_DIVX_VERSION;
291 :     default :
292 :     return DEC_EXIT;
293 :     }
294 :    
295 :    
296 :     switch(xerr)
297 :     {
298 :     case XVID_ERR_OK : return DEC_OK;
299 :     case XVID_ERR_MEMORY : return DEC_MEMORY;
300 :     case XVID_ERR_FORMAT : return DEC_BAD_FORMAT;
301 :     default : // case XVID_ERR_FAIL :
302 :     return DEC_EXIT;
303 :     }
304 :     }
305 :    
306 :    
307 :    
308 :    
309 :     // encore
310 :    
311 :     #define FRAMERATE_INCR 1001
312 :    
313 : Isibaar 10 int divx4_motion_presets[7] = {
314 : Isibaar 9 0, PMV_QUICKSTOP16, PMV_EARLYSTOP16, PMV_EARLYSTOP16 | PMV_EARLYSTOP8,
315 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
316 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8,
317 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 |
318 :     PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8
319 :     };
320 :    
321 :     int quality;
322 :    
323 : Isibaar 3 int encore(void * handle, int opt, void * param1, void * param2)
324 :     {
325 :     int xerr;
326 :    
327 :     switch(opt)
328 :     {
329 :     case ENC_OPT_INIT :
330 :     {
331 :     ENC_PARAM * eparam = (ENC_PARAM *)param1;
332 :     XVID_INIT_PARAM xinit;
333 :     XVID_ENC_PARAM xparam;
334 :    
335 :     xinit.cpu_flags = 0;
336 :     xvid_init(NULL, 0, &xinit, NULL);
337 :    
338 :     xparam.width = eparam->x_dim;
339 :     xparam.height = eparam->y_dim;
340 :     if ((eparam->framerate - (int)eparam->framerate) == 0)
341 :     {
342 :     xparam.fincr = 1;
343 :     xparam.fbase = (int)eparam->framerate;
344 :     }
345 :     else
346 :     {
347 :     xparam.fincr = FRAMERATE_INCR;
348 :     xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
349 :     }
350 :     xparam.bitrate = eparam->bitrate;
351 : Isibaar 9 xparam.rc_buffersize = 10 * eparam->bitrate;
352 : Isibaar 3 xparam.min_quantizer = eparam->min_quantizer;
353 :     xparam.max_quantizer = eparam->max_quantizer;
354 :     xparam.max_key_interval = eparam->max_key_interval;
355 : Isibaar 9 quality = eparam->quality;
356 : Isibaar 3
357 :     xerr = encoder_create(&xparam);
358 :    
359 :     eparam->handle = xparam.handle;
360 :    
361 :     break;
362 :     }
363 :    
364 :     case ENC_OPT_RELEASE :
365 :     {
366 :     xerr = encoder_destroy((Encoder *) handle);
367 :     break;
368 :     }
369 :    
370 :     case ENC_OPT_ENCODE :
371 :     case ENC_OPT_ENCODE_VBR :
372 :     {
373 :     ENC_FRAME * eframe = (ENC_FRAME *)param1;
374 :     ENC_RESULT * eresult = (ENC_RESULT *)param2;
375 :     XVID_ENC_FRAME xframe;
376 :     XVID_ENC_STATS xstats;
377 :    
378 :     xframe.bitstream = eframe->bitstream;
379 :     xframe.length = eframe->length;
380 :    
381 : Isibaar 9 xframe.general = XVID_HALFPEL | XVID_H263QUANT;
382 :    
383 :     if(quality > 3)
384 :     xframe.general |= XVID_INTER4V;
385 :    
386 : Isibaar 10 xframe.motion = divx4_motion_presets[quality];
387 : Isibaar 9
388 : Isibaar 3 xframe.image = eframe->image;
389 :     switch (eframe->colorspace)
390 :     {
391 :     case ENC_CSP_RGB24 :
392 :     xframe.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24;
393 :     break;
394 :     case ENC_CSP_YV12 :
395 :     xframe.colorspace = XVID_CSP_YV12;
396 :     break;
397 :     case ENC_CSP_YUY2 :
398 :     xframe.colorspace = XVID_CSP_YUY2;
399 :     break;
400 :     case ENC_CSP_UYVY :
401 :     xframe.colorspace = XVID_CSP_UYVY;
402 :     break;
403 :     case ENC_CSP_I420 :
404 :     xframe.colorspace = XVID_CSP_I420;
405 :     break;
406 :     }
407 :    
408 :     if (opt == ENC_OPT_ENCODE_VBR)
409 :     {
410 :     xframe.intra = eframe->intra;
411 :     xframe.quant = eframe->quant;
412 :     }
413 :     else
414 :     {
415 :     xframe.intra = -1;
416 :     xframe.quant = 0;
417 :     }
418 :    
419 :     xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );
420 :    
421 :     if (eresult)
422 :     {
423 :     eresult->is_key_frame = xframe.intra;
424 :     eresult->quantizer = xstats.quant;
425 :     eresult->total_bits = xframe.length * 8;
426 :     eresult->motion_bits = xstats.hlength * 8;
427 :     eresult->texture_bits = eresult->total_bits - eresult->motion_bits;
428 :     }
429 :    
430 :     eframe->length = xframe.length;
431 :    
432 :     break;
433 :     }
434 :    
435 :     default:
436 :     return ENC_FAIL;
437 :     }
438 :    
439 :     switch(xerr)
440 :     {
441 :     case XVID_ERR_OK : return ENC_OK;
442 :     case XVID_ERR_MEMORY : return ENC_MEMORY;
443 :     case XVID_ERR_FORMAT : return ENC_BAD_FORMAT;
444 :     default : // case XVID_ERR_FAIL :
445 :     return ENC_FAIL;
446 :     }
447 :     }
448 :    

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