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

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