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

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