[svn] / trunk / xvidcore / examples / xvid_decraw.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/examples/xvid_decraw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1627 - (view) (download)

1 : edgomez 547 /*****************************************************************************
2 : chl 376 *
3 : edgomez 547 * XVID MPEG-4 VIDEO CODEC
4 :     * - Console based decoding test application -
5 : chl 376 *
6 : edgomez 851 * Copyright(C) 2002-2003 Christoph Lampert
7 : edgomez 1382 * 2002-2003 Edouard Gomez <ed.gomez@free.fr>
8 : chl 376 *
9 : edgomez 547 * This program is free software; you can redistribute it and/or modify
10 :     * it under the terms of the GNU General Public License as published by
11 :     * the Free Software Foundation; either version 2 of the License, or
12 :     * (at your option) any later version.
13 : chl 376 *
14 : edgomez 547 * This program is distributed in the hope that it will be useful,
15 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 :     * GNU General Public License for more details.
18 : chl 376 *
19 : edgomez 547 * You should have received a copy of the GNU General Public License
20 :     * along with this program; if not, write to the Free Software
21 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 :     *
23 : Isibaar 1627 * $Id: xvid_decraw.c,v 1.20 2005-08-01 10:53:46 Isibaar Exp $
24 : edgomez 547 *
25 :     ****************************************************************************/
26 : chl 376
27 : edgomez 547 /*****************************************************************************
28 : chl 376 *
29 : edgomez 547 * Application notes :
30 : chl 376 *
31 : edgomez 547 * An MPEG-4 bitstream is read from an input file (or stdin) and decoded,
32 :     * the speed for this is measured.
33 : edgomez 1382 *
34 :     * The program is plain C and needs no libraries except for libxvidcore,
35 :     * and maths-lib.
36 : chl 376 *
37 : edgomez 1382 * Use ./xvid_decraw -help for a list of options
38 : chl 376 *
39 : edgomez 547 ****************************************************************************/
40 : chl 376
41 :     #include <stdio.h>
42 :     #include <stdlib.h>
43 : edgomez 547 #include <string.h>
44 :     #include <math.h>
45 : edgomez 824 #ifndef WIN32
46 : edgomez 547 #include <sys/time.h>
47 :     #else
48 :     #include <time.h>
49 :     #endif
50 : chl 376
51 : edgomez 547 #include "xvid.h"
52 : chl 376
53 : edgomez 547 /*****************************************************************************
54 :     * Global vars in module and constants
55 :     ****************************************************************************/
56 : chl 376
57 : edgomez 1423 #define USE_PNM 0
58 :     #define USE_TGA 1
59 :    
60 : edgomez 547 static int XDIM = 0;
61 :     static int YDIM = 0;
62 :     static int ARG_SAVEDECOUTPUT = 0;
63 :     static int ARG_SAVEMPEGSTREAM = 0;
64 :     static char *ARG_INPUTFILE = NULL;
65 : edgomez 1423 static int CSP = XVID_CSP_I420;
66 :     static int BPP = 1;
67 :     static int FORMAT = USE_PNM;
68 : chl 376
69 : edgomez 547 static char filepath[256] = "./";
70 :     static void *dec_handle = NULL;
71 : chl 376
72 : edgomez 1382 #define BUFFER_SIZE (2*1024*1024)
73 : chl 376
74 : edgomez 1547 static const int display_buffer_bytes = 0;
75 :    
76 : edgomez 547 /*****************************************************************************
77 :     * Local prototypes
78 :     ****************************************************************************/
79 : chl 376
80 : edgomez 547 static double msecond();
81 : suxen_drol 1416 static int dec_init(int use_assembler, int debug_level);
82 : edgomez 547 static int dec_main(unsigned char *istream,
83 : edgomez 851 unsigned char *ostream,
84 :     int istream_size,
85 : edgomez 1382 xvid_dec_stats_t *xvid_dec_stats);
86 : edgomez 547 static int dec_stop();
87 :     static void usage();
88 : edgomez 1442 static int write_image(char *prefix, unsigned char *image);
89 :     static int write_pnm(char *filename, unsigned char *image);
90 :     static int write_tga(char *filename, unsigned char *image);
91 : edgomez 547
92 : edgomez 1382 const char * type2str(int type)
93 :     {
94 :     if (type==XVID_TYPE_IVOP)
95 :     return "I";
96 :     if (type==XVID_TYPE_PVOP)
97 :     return "P";
98 :     if (type==XVID_TYPE_BVOP)
99 :     return "B";
100 :     return "S";
101 :     }
102 :    
103 : edgomez 547 /*****************************************************************************
104 :     * Main program
105 :     ****************************************************************************/
106 :    
107 :     int main(int argc, char *argv[])
108 : chl 376 {
109 : edgomez 547 unsigned char *mp4_buffer = NULL;
110 :     unsigned char *mp4_ptr = NULL;
111 :     unsigned char *out_buffer = NULL;
112 : edgomez 1382 int useful_bytes;
113 : edgomez 1547 int chunk;
114 : edgomez 1382 xvid_dec_stats_t xvid_dec_stats;
115 : edgomez 851
116 : edgomez 547 double totaldectime;
117 :    
118 :     long totalsize;
119 :     int status;
120 :    
121 :     int use_assembler = 0;
122 : suxen_drol 1416 int debug_level = 0;
123 : edgomez 547
124 :     char filename[256];
125 :    
126 :     FILE *in_file;
127 :     int filenr;
128 :     int i;
129 :    
130 :     printf("xvid_decraw - raw mpeg4 bitstream decoder ");
131 : edgomez 851 printf("written by Christoph Lampert 2002-2003\n\n");
132 : edgomez 547
133 :     /*****************************************************************************
134 :     * Command line parsing
135 :     ****************************************************************************/
136 :    
137 :     for (i=1; i< argc; i++) {
138 :    
139 :     if (strcmp("-asm", argv[i]) == 0 ) {
140 :     use_assembler = 1;
141 : suxen_drol 1416 } else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) {
142 :     i++;
143 :     if (sscanf(argv[i], "0x%x", &debug_level) != 1) {
144 :     debug_level = atoi(argv[i]);
145 :     }
146 : edgomez 1382 } else if (strcmp("-d", argv[i]) == 0) {
147 :     ARG_SAVEDECOUTPUT = 1;
148 :     } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
149 : edgomez 547 i++;
150 :     ARG_INPUTFILE = argv[i];
151 : edgomez 1382 } else if (strcmp("-m", argv[i]) == 0) {
152 :     ARG_SAVEMPEGSTREAM = 1;
153 : edgomez 1423 } else if (strcmp("-c", argv[i]) == 0 && i < argc - 1 ) {
154 :     i++;
155 :     if (strcmp(argv[i], "rgb16") == 0) {
156 :     CSP = XVID_CSP_RGB555;
157 :     BPP = 2;
158 :     } else if (strcmp(argv[i], "rgb24") == 0) {
159 :     CSP = XVID_CSP_BGR;
160 :     BPP = 3;
161 :     } else if (strcmp(argv[i], "rgb32") == 0) {
162 :     CSP = XVID_CSP_BGRA;
163 :     BPP = 4;
164 :     } else if (strcmp(argv[i], "yv12") == 0) {
165 :     CSP = XVID_CSP_YV12;
166 :     BPP = 1;
167 :     } else {
168 :     CSP = XVID_CSP_I420;
169 :     BPP = 1;
170 :     }
171 :     } else if (strcmp("-f", argv[i]) == 0 && i < argc -1) {
172 :     i++;
173 :     if (strcmp(argv[i], "tga") == 0) {
174 :     FORMAT = USE_TGA;
175 :     } else {
176 :     FORMAT = USE_PNM;
177 :     }
178 : edgomez 1382 } else if (strcmp("-help", argv[i]) == 0) {
179 : edgomez 547 usage();
180 :     return(0);
181 : edgomez 1382 } else {
182 : edgomez 547 usage();
183 :     exit(-1);
184 :     }
185 : chl 376 }
186 : edgomez 1432
187 : suxen_drol 1426 #if defined(_MSC_VER)
188 :     if (ARG_INPUTFILE==NULL) {
189 : edgomez 1432 fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n");
190 : suxen_drol 1426 }
191 :     #endif
192 : edgomez 1432
193 : edgomez 547 /*****************************************************************************
194 :     * Values checking
195 :     ****************************************************************************/
196 :    
197 :     if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
198 :     in_file = stdin;
199 :     }
200 :     else {
201 :    
202 :     in_file = fopen(ARG_INPUTFILE, "rb");
203 :     if (in_file == NULL) {
204 :     fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
205 : edgomez 1382 return(-1);
206 : edgomez 547 }
207 :     }
208 :    
209 : edgomez 1423 /* PNM/PGM format can't handle 16/32 bit data */
210 :     if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) {
211 :     FORMAT = USE_TGA;
212 :     }
213 :    
214 : edgomez 547 /*****************************************************************************
215 :     * Memory allocation
216 :     ****************************************************************************/
217 :    
218 :     /* Memory for encoded mp4 stream */
219 :     mp4_buffer = (unsigned char *) malloc(BUFFER_SIZE);
220 :     mp4_ptr = mp4_buffer;
221 :     if (!mp4_buffer)
222 :     goto free_all_memory;
223 :    
224 :     /*****************************************************************************
225 :     * XviD PART Start
226 :     ****************************************************************************/
227 :    
228 : suxen_drol 1416 status = dec_init(use_assembler, debug_level);
229 : edgomez 547 if (status) {
230 :     fprintf(stderr,
231 : edgomez 1382 "Decore INIT problem, return value %d\n", status);
232 : edgomez 547 goto release_all;
233 :     }
234 :    
235 :    
236 :     /*****************************************************************************
237 :     * Main loop
238 :     ****************************************************************************/
239 :    
240 : edgomez 851 /* Fill the buffer */
241 : edgomez 1382 useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
242 : edgomez 559
243 : edgomez 547 totaldectime = 0;
244 :     totalsize = 0;
245 :     filenr = 0;
246 :     mp4_ptr = mp4_buffer;
247 : edgomez 1547 chunk = 0;
248 :    
249 : edgomez 547 do {
250 :     int used_bytes = 0;
251 :     double dectime;
252 :    
253 : edgomez 851 /*
254 :     * If the buffer is half empty or there are no more bytes in it
255 :     * then fill it.
256 :     */
257 : edgomez 1382 if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {
258 :     int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
259 : edgomez 547
260 : edgomez 851 /* Move data if needed */
261 : edgomez 1382 if (already_in_buffer > 0)
262 :     memcpy(mp4_buffer, mp4_ptr, already_in_buffer);
263 : edgomez 547
264 : edgomez 851 /* Update mp4_ptr */
265 :     mp4_ptr = mp4_buffer;
266 : edgomez 547
267 : edgomez 851 /* read new data */
268 : edgomez 1382 if(feof(in_file))
269 : edgomez 547 break;
270 :    
271 : edgomez 1382 useful_bytes += fread(mp4_buffer + already_in_buffer,
272 :     1, BUFFER_SIZE - already_in_buffer,
273 :     in_file);
274 : edgomez 547
275 :     }
276 :    
277 :    
278 : edgomez 1382 /* This loop is needed to handle VOL/NVOP reading */
279 : edgomez 851 do {
280 : edgomez 547
281 : edgomez 851 /* Decode frame */
282 :     dectime = msecond();
283 : edgomez 1382 used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats);
284 : edgomez 851 dectime = msecond() - dectime;
285 : edgomez 547
286 : edgomez 1382 /* Resize image buffer if needed */
287 :     if(xvid_dec_stats.type == XVID_TYPE_VOL) {
288 :    
289 :     /* Check if old buffer is smaller */
290 :     if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) {
291 :    
292 :     /* Copy new witdh and new height from the vol structure */
293 :     XDIM = xvid_dec_stats.data.vol.width;
294 :     YDIM = xvid_dec_stats.data.vol.height;
295 :    
296 :     /* Free old output buffer*/
297 :     if(out_buffer) free(out_buffer);
298 :    
299 :     /* Allocate the new buffer */
300 :     out_buffer = (unsigned char*)malloc(XDIM*YDIM*4);
301 :     if(out_buffer == NULL)
302 :     goto free_all_memory;
303 :    
304 :     fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM);
305 :     }
306 : edgomez 1547
307 :     /* Save individual mpeg4 stream if required */
308 :     if(ARG_SAVEMPEGSTREAM) {
309 :     FILE *filehandle = NULL;
310 :    
311 :     sprintf(filename, "%svolhdr.m4v", filepath);
312 :     filehandle = fopen(filename, "wb");
313 :     if(!filehandle) {
314 :     fprintf(stderr,
315 :     "Error writing vol header mpeg4 stream to file %s\n",
316 :     filename);
317 :     } else {
318 :     fwrite(mp4_ptr, 1, used_bytes, filehandle);
319 :     fclose(filehandle);
320 :     }
321 :     }
322 : edgomez 851 }
323 : edgomez 547
324 : edgomez 851 /* Update buffer pointers */
325 : edgomez 1382 if(used_bytes > 0) {
326 :     mp4_ptr += used_bytes;
327 :     useful_bytes -= used_bytes;
328 : edgomez 559
329 : edgomez 1382 /* Total size */
330 :     totalsize += used_bytes;
331 :     }
332 : edgomez 547
333 : edgomez 1547 if (display_buffer_bytes) {
334 :     printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes);
335 :     }
336 :     } while (xvid_dec_stats.type <= 0 && useful_bytes > 0);
337 : edgomez 547
338 : edgomez 1382 /* Check if there is a negative number of useful bytes left in buffer
339 :     * This means we went too far */
340 :     if(useful_bytes < 0)
341 :     break;
342 : edgomez 851
343 : edgomez 1382 /* Updated data - Count only usefull decode time */
344 : edgomez 851 totaldectime += dectime;
345 : edgomez 547
346 : edgomez 851
347 : edgomez 1547 if (!display_buffer_bytes) {
348 :     printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
349 :     filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
350 :     }
351 :    
352 : edgomez 851 /* Save individual mpeg4 stream if required */
353 : edgomez 1382 if(ARG_SAVEMPEGSTREAM) {
354 : edgomez 547 FILE *filehandle = NULL;
355 :    
356 :     sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
357 :     filehandle = fopen(filename, "wb");
358 :     if(!filehandle) {
359 :     fprintf(stderr,
360 : edgomez 851 "Error writing single mpeg4 stream to file %s\n",
361 :     filename);
362 : edgomez 547 }
363 :     else {
364 : edgomez 1382 fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle);
365 : edgomez 547 fclose(filehandle);
366 :     }
367 :     }
368 : edgomez 851
369 : edgomez 547 /* Save output frame if required */
370 :     if (ARG_SAVEDECOUTPUT) {
371 : edgomez 1423 sprintf(filename, "%sdec%05d", filepath, filenr);
372 :     if(write_image(filename, out_buffer)) {
373 : edgomez 547 fprintf(stderr,
374 : edgomez 1442 "Error writing decoded frame %s\n",
375 : edgomez 851 filename);
376 : edgomez 547 }
377 :     }
378 :    
379 :     filenr++;
380 :    
381 : edgomez 1547 } while (useful_bytes>0 || !feof(in_file));
382 : edgomez 547
383 : edgomez 1547 useful_bytes = 0; /* Empty buffer */
384 :    
385 : edgomez 547 /*****************************************************************************
386 : edgomez 851 * Flush decoder buffers
387 :     ****************************************************************************/
388 :    
389 : edgomez 1382 do {
390 :    
391 : edgomez 851 /* Fake vars */
392 : edgomez 1382 int used_bytes;
393 : edgomez 851 double dectime;
394 :    
395 : edgomez 1382 do {
396 :     dectime = msecond();
397 :     used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
398 :     dectime = msecond() - dectime;
399 : edgomez 1547 if (display_buffer_bytes) {
400 :     printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes);
401 :     }
402 :     } while(used_bytes>=0 && xvid_dec_stats.type <= 0);
403 : edgomez 851
404 : edgomez 1382 if (used_bytes < 0) { /* XVID_ERR_END */
405 :     break;
406 :     }
407 : edgomez 851
408 :     /* Updated data - Count only usefull decode time */
409 :     totaldectime += dectime;
410 :    
411 :     /* Prints some decoding stats */
412 : edgomez 1547 if (!display_buffer_bytes) {
413 :     printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
414 :     filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
415 :     }
416 :    
417 : edgomez 851 /* Save output frame if required */
418 :     if (ARG_SAVEDECOUTPUT) {
419 : edgomez 1423 sprintf(filename, "%sdec%05d", filepath, filenr);
420 :     if(write_image(filename, out_buffer)) {
421 : edgomez 851 fprintf(stderr,
422 : suxen_drol 1426 "Error writing decoded frame %s\n",
423 : edgomez 851 filename);
424 :     }
425 :     }
426 :    
427 :     filenr++;
428 :    
429 : edgomez 1382 }while(1);
430 : edgomez 851
431 :     /*****************************************************************************
432 : edgomez 547 * Calculate totals and averages for output, print results
433 :     ****************************************************************************/
434 :    
435 : suxen_drol 1426 if (filenr>0) {
436 : edgomez 1432 totalsize /= filenr;
437 : suxen_drol 1426 totaldectime /= filenr;
438 :     printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n",
439 :     totaldectime, 1000/totaldectime, (int)totalsize);
440 :     }else{
441 :     printf("Nothing was decoded!\n");
442 :     }
443 : edgomez 547
444 :     /*****************************************************************************
445 :     * XviD PART Stop
446 : edgomez 559 ****************************************************************************/
447 : edgomez 547
448 :     release_all:
449 :     if (dec_handle) {
450 :     status = dec_stop();
451 :     if (status)
452 :     fprintf(stderr, "decore RELEASE problem return value %d\n", status);
453 :     }
454 :    
455 :     free_all_memory:
456 :     free(out_buffer);
457 :     free(mp4_buffer);
458 :    
459 : edgomez 1382 return(0);
460 : chl 376 }
461 :    
462 : edgomez 547 /*****************************************************************************
463 :     * Usage function
464 :     ****************************************************************************/
465 : chl 376
466 : edgomez 547 static void usage()
467 : chl 376 {
468 : edgomez 547
469 : edgomez 1382 fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n");
470 : edgomez 547 fprintf(stderr, "Options :\n");
471 :     fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n");
472 : suxen_drol 1416 fprintf(stderr, " -debug : debug level (debug=0)\n");
473 : edgomez 547 fprintf(stderr, " -i string : input filename (default=stdin)\n");
474 : edgomez 1382 fprintf(stderr, " -d : save decoder output\n");
475 : edgomez 1423 fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n");
476 :     fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm)\n");
477 : edgomez 1382 fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n");
478 : edgomez 547 fprintf(stderr, " -help : This help message\n");
479 :     fprintf(stderr, " (* means default)\n");
480 :    
481 :     }
482 :    
483 :     /*****************************************************************************
484 :     * "helper" functions
485 :     ****************************************************************************/
486 :    
487 :     /* return the current time in milli seconds */
488 :     static double
489 :     msecond()
490 :     {
491 : edgomez 824 #ifndef WIN32
492 : edgomez 547 struct timeval tv;
493 :     gettimeofday(&tv, 0);
494 : edgomez 1382 return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3);
495 : edgomez 547 #else
496 :     clock_t clk;
497 :     clk = clock();
498 : edgomez 1382 return(clk * 1000 / CLOCKS_PER_SEC);
499 : edgomez 547 #endif
500 :     }
501 :    
502 :     /*****************************************************************************
503 :     * output functions
504 :     ****************************************************************************/
505 :    
506 : edgomez 1442 static int write_image(char *prefix, unsigned char *image)
507 : edgomez 547 {
508 : edgomez 1423 char filename[1024];
509 :     char *ext;
510 :     int ret;
511 : edgomez 547
512 : edgomez 1423 if (FORMAT == USE_PNM && BPP == 1) {
513 :     ext = "pgm";
514 :     } else if (FORMAT == USE_PNM && BPP == 3) {
515 :     ext = "pnm";
516 :     } else if (FORMAT == USE_TGA) {
517 :     ext = "tga";
518 :     } else {
519 :     fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used");
520 :     exit(-1);
521 :     }
522 : edgomez 547
523 : edgomez 1423 sprintf(filename, "%s.%s", prefix, ext);
524 : chl 376
525 : edgomez 1423 if (FORMAT == USE_PNM) {
526 :     ret = write_pnm(filename, image);
527 :     } else {
528 :     ret = write_tga(filename, image);
529 :     }
530 : edgomez 547
531 : edgomez 1423 return(ret);
532 :     }
533 : edgomez 547
534 : edgomez 1442 static int write_tga(char *filename, unsigned char *image)
535 : edgomez 1423 {
536 :     FILE * f;
537 :     char hdr[18];
538 : edgomez 547
539 : edgomez 1423 f = fopen(filename, "wb");
540 :     if ( f == NULL) {
541 :     return -1;
542 :     }
543 : edgomez 547
544 : edgomez 1423 hdr[0] = 0; /* ID length */
545 :     hdr[1] = 0; /* Color map type */
546 :     hdr[2] = (BPP>1)?2:3; /* Uncompressed true color (2) or greymap (3) */
547 :     hdr[3] = 0; /* Color map specification (not used) */
548 :     hdr[4] = 0; /* Color map specification (not used) */
549 :     hdr[5] = 0; /* Color map specification (not used) */
550 :     hdr[6] = 0; /* Color map specification (not used) */
551 :     hdr[7] = 0; /* Color map specification (not used) */
552 :     hdr[8] = 0; /* LSB X origin */
553 :     hdr[9] = 0; /* MSB X origin */
554 :     hdr[10] = 0; /* LSB Y origin */
555 :     hdr[11] = 0; /* MSB Y origin */
556 :     hdr[12] = (XDIM>>0)&0xff; /* LSB Width */
557 :     hdr[13] = (XDIM>>8)&0xff; /* MSB Width */
558 :     if (BPP > 1) {
559 :     hdr[14] = (YDIM>>0)&0xff; /* LSB Height */
560 :     hdr[15] = (YDIM>>8)&0xff; /* MSB Height */
561 :     } else {
562 :     hdr[14] = ((YDIM*3)>>1)&0xff; /* LSB Height */
563 :     hdr[15] = ((YDIM*3)>>9)&0xff; /* MSB Height */
564 :     }
565 :     hdr[16] = BPP*8;
566 :     hdr[17] = 0x00 | (1<<5) /* Up to down */ | (0<<4); /* Image descriptor */
567 :    
568 :     /* Write header */
569 :     fwrite(hdr, 1, sizeof(hdr), f);
570 : edgomez 547
571 : edgomez 1423 #ifdef ARCH_IS_LITTLE_ENDIAN
572 :     /* write first plane */
573 :     fwrite(image, 1, XDIM*YDIM*BPP, f);
574 :     #else
575 :     {
576 :     int i;
577 : Isibaar 1627 for (i=0; i<XDIM*YDIM*BPP;i+=BPP) {
578 : edgomez 1423 if (BPP == 1) {
579 : Isibaar 1627 fputc(*(image+i), f);
580 : edgomez 1423 } else if (BPP == 2) {
581 : Isibaar 1627 fputc(*(image+i+1), f);
582 :     fputc(*(image+i+0), f);
583 : edgomez 1423 } else if (BPP == 3) {
584 : Isibaar 1627 fputc(*(image+i+2), f);
585 :     fputc(*(image+i+1), f);
586 :     fputc(*(image+i+0), f);
587 : edgomez 1423 } else if (BPP == 4) {
588 : Isibaar 1627 fputc(*(image+i+3), f);
589 :     fputc(*(image+i+2), f);
590 :     fputc(*(image+i+1), f);
591 :     fputc(*(image+i+0), f);
592 : edgomez 1423 }
593 : edgomez 547 }
594 : edgomez 1423 }
595 :     #endif
596 : edgomez 547
597 : edgomez 1423 /* Write Y and V planes for YUV formats */
598 :     if (BPP == 1) {
599 :     int i;
600 : edgomez 547
601 : edgomez 1423 /* Write the two chrominance planes */
602 :     for (i=0; i<YDIM/2; i++) {
603 :     fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f);
604 :     fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f);
605 :     }
606 : chl 376 }
607 : edgomez 1423
608 :    
609 :     /* Close the file */
610 :     fclose(f);
611 :    
612 :     return(0);
613 : chl 376 }
614 :    
615 : edgomez 1442 static int write_pnm(char *filename, unsigned char *image)
616 : edgomez 1423 {
617 :     FILE * f;
618 :    
619 :     f = fopen(filename, "wb");
620 :     if ( f == NULL) {
621 :     return -1;
622 :     }
623 :    
624 :     if (BPP == 1) {
625 :     int i;
626 :     fprintf(f, "P5\n#xvid\n%i %i\n255\n", XDIM, YDIM*3/2);
627 :    
628 :     fwrite(image, 1, XDIM*YDIM, f);
629 :    
630 :     for (i=0; i<YDIM/2;i++) {
631 :     fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f);
632 :     fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f);
633 :     }
634 :     } else if (BPP == 3) {
635 :     int i;
636 :     fprintf(f, "P6\n#xvid\n%i %i\n255\n", XDIM, YDIM);
637 :     for (i=0; i<XDIM*YDIM*3; i+=3) {
638 :     #ifdef ARCH_IS_LITTLE_ENDIAN
639 :     fputc(image[i+2], f);
640 :     fputc(image[i+1], f);
641 :     fputc(image[i+0], f);
642 :     #else
643 :     fputc(image[i+0], f);
644 :     fputc(image[i+1], f);
645 :     fputc(image[i+2], f);
646 :     #endif
647 :     }
648 :     }
649 :    
650 :     fclose(f);
651 :    
652 :     return 0;
653 :     }
654 : edgomez 1442
655 : edgomez 547 /*****************************************************************************
656 :     * Routines for decoding: init decoder, use, and stop decoder
657 :     ****************************************************************************/
658 : chl 376
659 : edgomez 547 /* init decoder before first run */
660 :     static int
661 : suxen_drol 1416 dec_init(int use_assembler, int debug_level)
662 : chl 376 {
663 : edgomez 1382 int ret;
664 : chl 376
665 : edgomez 1382 xvid_gbl_init_t xvid_gbl_init;
666 :     xvid_dec_create_t xvid_dec_create;
667 : chl 376
668 : edgomez 1501 /* Reset the structure with zeros */
669 :     memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t));
670 :     memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t));
671 :    
672 : edgomez 1382 /*------------------------------------------------------------------------
673 :     * XviD core initialization
674 :     *----------------------------------------------------------------------*/
675 :    
676 :     /* Version */
677 :     xvid_gbl_init.version = XVID_VERSION;
678 :    
679 :     /* Assembly setting */
680 :     if(use_assembler)
681 : suxen_drol 860 #ifdef ARCH_IS_IA64
682 : edgomez 1382 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
683 : chl 376 #else
684 : edgomez 1382 xvid_gbl_init.cpu_flags = 0;
685 : chl 376 #endif
686 : edgomez 1382 else
687 :     xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
688 : chl 376
689 : suxen_drol 1416 xvid_gbl_init.debug = debug_level;
690 :    
691 : edgomez 1382 xvid_global(NULL, 0, &xvid_gbl_init, NULL);
692 : chl 376
693 : edgomez 1382 /*------------------------------------------------------------------------
694 :     * XviD encoder initialization
695 :     *----------------------------------------------------------------------*/
696 : edgomez 851
697 : edgomez 1382 /* Version */
698 :     xvid_dec_create.version = XVID_VERSION;
699 : chl 376
700 : edgomez 1382 /*
701 :     * Image dimensions -- set to 0, xvidcore will resize when ever it is
702 :     * needed
703 :     */
704 :     xvid_dec_create.width = 0;
705 :     xvid_dec_create.height = 0;
706 :    
707 :     ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
708 :    
709 :     dec_handle = xvid_dec_create.handle;
710 :    
711 :     return(ret);
712 : chl 376 }
713 :    
714 : edgomez 547 /* decode one frame */
715 :     static int
716 :     dec_main(unsigned char *istream,
717 : edgomez 851 unsigned char *ostream,
718 :     int istream_size,
719 : edgomez 1382 xvid_dec_stats_t *xvid_dec_stats)
720 : edgomez 547 {
721 : chl 376
722 : edgomez 1382 int ret;
723 : chl 376
724 : edgomez 1382 xvid_dec_frame_t xvid_dec_frame;
725 : chl 376
726 : edgomez 1523 /* Reset all structures */
727 :     memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t));
728 :     memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t));
729 :    
730 : edgomez 1382 /* Set version */
731 :     xvid_dec_frame.version = XVID_VERSION;
732 :     xvid_dec_stats->version = XVID_VERSION;
733 : chl 376
734 : edgomez 1382 /* No general flags to set */
735 :     xvid_dec_frame.general = 0;
736 : chl 376
737 : edgomez 1382 /* Input stream */
738 :     xvid_dec_frame.bitstream = istream;
739 :     xvid_dec_frame.length = istream_size;
740 : edgomez 851
741 : edgomez 1382 /* Output frame structure */
742 :     xvid_dec_frame.output.plane[0] = ostream;
743 : edgomez 1423 xvid_dec_frame.output.stride[0] = XDIM*BPP;
744 :     xvid_dec_frame.output.csp = CSP;
745 : edgomez 1382
746 :     ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
747 :    
748 :     return(ret);
749 : chl 376 }
750 :    
751 : edgomez 547 /* close decoder to release resources */
752 :     static int
753 :     dec_stop()
754 : chl 376 {
755 : edgomez 1382 int ret;
756 : edgomez 547
757 : edgomez 1382 ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
758 : chl 376
759 : edgomez 1382 return(ret);
760 : chl 376 }

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