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

Annotation of /xvidcore/examples/xvid_decraw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (view) (download)

1 : edgomez 1.2 /*****************************************************************************
2 : chl 1.1 *
3 : edgomez 1.2 * XVID MPEG-4 VIDEO CODEC
4 :     * - Console based decoding test application -
5 : chl 1.1 *
6 : edgomez 1.6 * Copyright(C) 2002-2003 Christoph Lampert
7 : edgomez 1.10 * 2002-2003 Edouard Gomez <ed.gomez@free.fr>
8 : chl 1.1 *
9 : edgomez 1.2 * 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 1.1 *
14 : edgomez 1.2 * 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 1.1 *
19 : edgomez 1.2 * 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 1.27 * $Id: xvid_decraw.c,v 1.26 2010/01/08 10:03:09 Isibaar Exp $
24 : edgomez 1.2 *
25 :     ****************************************************************************/
26 : chl 1.1
27 : edgomez 1.2 /*****************************************************************************
28 : chl 1.1 *
29 : edgomez 1.2 * Application notes :
30 : chl 1.1 *
31 : edgomez 1.2 * An MPEG-4 bitstream is read from an input file (or stdin) and decoded,
32 :     * the speed for this is measured.
33 : edgomez 1.10 *
34 :     * The program is plain C and needs no libraries except for libxvidcore,
35 :     * and maths-lib.
36 : chl 1.1 *
37 : edgomez 1.10 * Use ./xvid_decraw -help for a list of options
38 : chl 1.1 *
39 : edgomez 1.2 ****************************************************************************/
40 : chl 1.1
41 :     #include <stdio.h>
42 :     #include <stdlib.h>
43 : edgomez 1.2 #include <string.h>
44 :     #include <math.h>
45 : edgomez 1.5 #ifndef WIN32
46 : edgomez 1.2 #include <sys/time.h>
47 :     #else
48 :     #include <time.h>
49 :     #endif
50 :    
51 :     #include "xvid.h"
52 :    
53 :     /*****************************************************************************
54 :     * Global vars in module and constants
55 :     ****************************************************************************/
56 :    
57 : edgomez 1.12 #define USE_PNM 0
58 :     #define USE_TGA 1
59 : Isibaar 1.25 #define USE_YUV 2
60 : edgomez 1.12
61 : edgomez 1.2 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 1.12 static int CSP = XVID_CSP_I420;
67 :     static int BPP = 1;
68 :     static int FORMAT = USE_PNM;
69 : Isibaar 1.27 static int POSTPROC = 0;
70 : edgomez 1.2
71 :     static char filepath[256] = "./";
72 :     static void *dec_handle = NULL;
73 :    
74 : edgomez 1.10 #define BUFFER_SIZE (2*1024*1024)
75 : edgomez 1.2
76 : edgomez 1.19 static const int display_buffer_bytes = 0;
77 :    
78 : suxen_drol 1.21 #define MIN_USEFUL_BYTES 1
79 :    
80 : edgomez 1.2 /*****************************************************************************
81 :     * Local prototypes
82 :     ****************************************************************************/
83 :    
84 :     static double msecond();
85 : suxen_drol 1.11 static int dec_init(int use_assembler, int debug_level);
86 : edgomez 1.2 static int dec_main(unsigned char *istream,
87 : edgomez 1.6 unsigned char *ostream,
88 :     int istream_size,
89 : edgomez 1.10 xvid_dec_stats_t *xvid_dec_stats);
90 : edgomez 1.2 static int dec_stop();
91 :     static void usage();
92 : Isibaar 1.26 static int write_image(char *prefix, unsigned char *image, int filenr);
93 : edgomez 1.15 static int write_pnm(char *filename, unsigned char *image);
94 :     static int write_tga(char *filename, unsigned char *image);
95 : Isibaar 1.25 static int write_yuv(char *filename, unsigned char *image);
96 : edgomez 1.10
97 :     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 1.2 /*****************************************************************************
109 :     * Main program
110 :     ****************************************************************************/
111 :    
112 :     int main(int argc, char *argv[])
113 :     {
114 :     unsigned char *mp4_buffer = NULL;
115 :     unsigned char *mp4_ptr = NULL;
116 :     unsigned char *out_buffer = NULL;
117 : edgomez 1.10 int useful_bytes;
118 : edgomez 1.19 int chunk;
119 : edgomez 1.10 xvid_dec_stats_t xvid_dec_stats;
120 : edgomez 1.6
121 : edgomez 1.2 double totaldectime;
122 :    
123 :     long totalsize;
124 :     int status;
125 :    
126 : Isibaar 1.27 int use_assembler = 1;
127 : suxen_drol 1.11 int debug_level = 0;
128 : edgomez 1.2
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 1.27 printf("written by Christoph Lampert\n\n");
137 : edgomez 1.2
138 :     /*****************************************************************************
139 :     * Command line parsing
140 :     ****************************************************************************/
141 :    
142 :     for (i=1; i< argc; i++) {
143 :    
144 : Isibaar 1.27 if (strcmp("-noasm", argv[i]) == 0 ) {
145 :     use_assembler = 0;
146 : suxen_drol 1.11 } 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 1.10 } else if (strcmp("-d", argv[i]) == 0) {
152 :     ARG_SAVEDECOUTPUT = 1;
153 :     } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
154 : edgomez 1.2 i++;
155 :     ARG_INPUTFILE = argv[i];
156 : edgomez 1.10 } else if (strcmp("-m", argv[i]) == 0) {
157 :     ARG_SAVEMPEGSTREAM = 1;
158 : edgomez 1.12 } 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 1.27 } 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 1.12 } 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 1.25 } else if (strcmp(argv[i], "yuv") == 0) {
186 :     FORMAT = USE_YUV;
187 : edgomez 1.12 } else {
188 :     FORMAT = USE_PNM;
189 :     }
190 : edgomez 1.10 } else if (strcmp("-help", argv[i]) == 0) {
191 : edgomez 1.2 usage();
192 :     return(0);
193 : edgomez 1.10 } else {
194 : edgomez 1.2 usage();
195 :     exit(-1);
196 :     }
197 :     }
198 : edgomez 1.14
199 : suxen_drol 1.13 #if defined(_MSC_VER)
200 :     if (ARG_INPUTFILE==NULL) {
201 : edgomez 1.14 fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n");
202 : suxen_drol 1.13 }
203 :     #endif
204 : edgomez 1.14
205 : edgomez 1.2 /*****************************************************************************
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 1.10 return(-1);
218 : edgomez 1.2 }
219 :     }
220 :    
221 : edgomez 1.12 /* 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 1.25 if (BPP != 1 && FORMAT == USE_YUV) {
226 :     FORMAT = USE_TGA;
227 :     }
228 : edgomez 1.12
229 : edgomez 1.2 /*****************************************************************************
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 1.27 * Xvid PART Start
241 : edgomez 1.2 ****************************************************************************/
242 : chl 1.1
243 : suxen_drol 1.11 status = dec_init(use_assembler, debug_level);
244 : edgomez 1.2 if (status) {
245 :     fprintf(stderr,
246 : edgomez 1.10 "Decore INIT problem, return value %d\n", status);
247 : edgomez 1.2 goto release_all;
248 :     }
249 :    
250 :    
251 :     /*****************************************************************************
252 :     * Main loop
253 :     ****************************************************************************/
254 :    
255 : edgomez 1.6 /* Fill the buffer */
256 : edgomez 1.10 useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
257 : edgomez 1.2
258 :     totaldectime = 0;
259 :     totalsize = 0;
260 :     filenr = 0;
261 :     mp4_ptr = mp4_buffer;
262 : edgomez 1.19 chunk = 0;
263 :    
264 : edgomez 1.2 do {
265 :     int used_bytes = 0;
266 :     double dectime;
267 :    
268 : edgomez 1.6 /*
269 :     * If the buffer is half empty or there are no more bytes in it
270 :     * then fill it.
271 :     */
272 : edgomez 1.10 if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {
273 :     int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
274 : edgomez 1.6
275 :     /* Move data if needed */
276 : edgomez 1.10 if (already_in_buffer > 0)
277 :     memcpy(mp4_buffer, mp4_ptr, already_in_buffer);
278 : edgomez 1.2
279 : edgomez 1.6 /* Update mp4_ptr */
280 :     mp4_ptr = mp4_buffer;
281 : edgomez 1.2
282 : edgomez 1.6 /* read new data */
283 : Isibaar 1.24 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 1.6 }
289 : edgomez 1.2
290 :    
291 : edgomez 1.10 /* This loop is needed to handle VOL/NVOP reading */
292 : edgomez 1.6 do {
293 : edgomez 1.2
294 : edgomez 1.6 /* Decode frame */
295 :     dectime = msecond();
296 : edgomez 1.10 used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats);
297 : edgomez 1.6 dectime = msecond() - dectime;
298 : edgomez 1.2
299 : edgomez 1.10 /* 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 1.19
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 1.6 }
336 : edgomez 1.4
337 : edgomez 1.6 /* Update buffer pointers */
338 : edgomez 1.10 if(used_bytes > 0) {
339 :     mp4_ptr += used_bytes;
340 :     useful_bytes -= used_bytes;
341 : edgomez 1.2
342 : edgomez 1.10 /* Total size */
343 :     totalsize += used_bytes;
344 :     }
345 : edgomez 1.2
346 : edgomez 1.19 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 1.21 } while (xvid_dec_stats.type <= 0 && useful_bytes > MIN_USEFUL_BYTES);
350 : edgomez 1.2
351 : edgomez 1.10 /* 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 1.6
356 : edgomez 1.10 /* Updated data - Count only usefull decode time */
357 : edgomez 1.6 totaldectime += dectime;
358 : edgomez 1.2
359 : edgomez 1.10
360 : edgomez 1.19 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 1.6 /* Save individual mpeg4 stream if required */
366 : edgomez 1.10 if(ARG_SAVEMPEGSTREAM) {
367 : edgomez 1.2 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 1.6 "Error writing single mpeg4 stream to file %s\n",
374 :     filename);
375 : edgomez 1.2 }
376 :     else {
377 : edgomez 1.10 fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle);
378 : edgomez 1.2 fclose(filehandle);
379 :     }
380 :     }
381 : edgomez 1.6
382 : edgomez 1.2 /* Save output frame if required */
383 :     if (ARG_SAVEDECOUTPUT) {
384 : Isibaar 1.26 sprintf(filename, "%sdec", filepath);
385 :    
386 :     if(write_image(filename, out_buffer, filenr)) {
387 : edgomez 1.2 fprintf(stderr,
388 : edgomez 1.15 "Error writing decoded frame %s\n",
389 : edgomez 1.6 filename);
390 : edgomez 1.2 }
391 :     }
392 :    
393 :     filenr++;
394 :    
395 : suxen_drol 1.21 } while (useful_bytes>MIN_USEFUL_BYTES || !feof(in_file));
396 : edgomez 1.19
397 :     useful_bytes = 0; /* Empty buffer */
398 : edgomez 1.6
399 :     /*****************************************************************************
400 :     * Flush decoder buffers
401 :     ****************************************************************************/
402 : edgomez 1.10
403 :     do {
404 : edgomez 1.6
405 :     /* Fake vars */
406 : edgomez 1.10 int used_bytes;
407 : edgomez 1.6 double dectime;
408 :    
409 : edgomez 1.10 do {
410 :     dectime = msecond();
411 :     used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
412 :     dectime = msecond() - dectime;
413 : edgomez 1.19 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 1.10
418 :     if (used_bytes < 0) { /* XVID_ERR_END */
419 :     break;
420 :     }
421 : edgomez 1.6
422 :     /* Updated data - Count only usefull decode time */
423 :     totaldectime += dectime;
424 : edgomez 1.2
425 : edgomez 1.6 /* Prints some decoding stats */
426 : edgomez 1.19 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 1.6 /* Save output frame if required */
432 :     if (ARG_SAVEDECOUTPUT) {
433 : Isibaar 1.26 sprintf(filename, "%sdec", filepath);
434 :    
435 :     if(write_image(filename, out_buffer, filenr)) {
436 : edgomez 1.6 fprintf(stderr,
437 : suxen_drol 1.13 "Error writing decoded frame %s\n",
438 : edgomez 1.6 filename);
439 :     }
440 :     }
441 :    
442 :     filenr++;
443 :    
444 : edgomez 1.10 }while(1);
445 : edgomez 1.6
446 : edgomez 1.2 /*****************************************************************************
447 :     * Calculate totals and averages for output, print results
448 :     ****************************************************************************/
449 : chl 1.1
450 : suxen_drol 1.13 if (filenr>0) {
451 : edgomez 1.14 totalsize /= filenr;
452 : suxen_drol 1.13 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 1.2
459 :     /*****************************************************************************
460 : Isibaar 1.27 * Xvid PART Stop
461 : edgomez 1.4 ****************************************************************************/
462 : chl 1.1
463 : edgomez 1.2 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 : chl 1.1
470 : edgomez 1.2 free_all_memory:
471 :     free(out_buffer);
472 :     free(mp4_buffer);
473 : chl 1.1
474 : edgomez 1.10 return(0);
475 : edgomez 1.2 }
476 : chl 1.1
477 : edgomez 1.2 /*****************************************************************************
478 :     * Usage function
479 :     ****************************************************************************/
480 : chl 1.1
481 : edgomez 1.2 static void usage()
482 :     {
483 :    
484 : edgomez 1.10 fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n");
485 : edgomez 1.2 fprintf(stderr, "Options :\n");
486 : Isibaar 1.27 fprintf(stderr, " -noasm : don't use assembly optimizations (default=enabled)\n");
487 : suxen_drol 1.11 fprintf(stderr, " -debug : debug level (debug=0)\n");
488 : edgomez 1.2 fprintf(stderr, " -i string : input filename (default=stdin)\n");
489 : edgomez 1.10 fprintf(stderr, " -d : save decoder output\n");
490 : edgomez 1.12 fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n");
491 : Isibaar 1.25 fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm, yuv)\n");
492 : Isibaar 1.27 fprintf(stderr, " -postproc : postprocessing level (0=off, 1=deblock, 2=deblock+dering)\n");
493 : edgomez 1.10 fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n");
494 : edgomez 1.2 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 : chl 1.1 {
507 : edgomez 1.5 #ifndef WIN32
508 : chl 1.1 struct timeval tv;
509 :     gettimeofday(&tv, 0);
510 : edgomez 1.10 return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3);
511 : edgomez 1.2 #else
512 :     clock_t clk;
513 :     clk = clock();
514 : suxen_drol 1.22 return(clk * 1000.0 / CLOCKS_PER_SEC);
515 : edgomez 1.2 #endif
516 : chl 1.1 }
517 :    
518 : edgomez 1.2 /*****************************************************************************
519 :     * output functions
520 :     ****************************************************************************/
521 :    
522 : Isibaar 1.26 static int write_image(char *prefix, unsigned char *image, int filenr)
523 : edgomez 1.2 {
524 : edgomez 1.12 char filename[1024];
525 :     char *ext;
526 :     int ret;
527 : edgomez 1.2
528 : edgomez 1.12 if (FORMAT == USE_PNM && BPP == 1) {
529 :     ext = "pgm";
530 :     } else if (FORMAT == USE_PNM && BPP == 3) {
531 :     ext = "pnm";
532 : Isibaar 1.25 } else if (FORMAT == USE_YUV) {
533 :     ext = "yuv";
534 : edgomez 1.12 } 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 : chl 1.1
541 : Isibaar 1.26 if (FORMAT == USE_YUV) {
542 :     sprintf(filename, "%s.%s", prefix, ext);
543 :    
544 :     if (!filenr) {
545 :     FILE *fp = fopen(filename, "wb");
546 :     fclose(fp);
547 :     }
548 :     } else
549 :     sprintf(filename, "%s%05d.%s", prefix, filenr, ext);
550 : edgomez 1.2
551 : edgomez 1.12 if (FORMAT == USE_PNM) {
552 :     ret = write_pnm(filename, image);
553 : Isibaar 1.25 } else if (FORMAT == USE_YUV) {
554 :     ret = write_yuv(filename, image);
555 : edgomez 1.12 } else {
556 :     ret = write_tga(filename, image);
557 :     }
558 : chl 1.1
559 : edgomez 1.12 return(ret);
560 :     }
561 : chl 1.1
562 : edgomez 1.15 static int write_tga(char *filename, unsigned char *image)
563 : edgomez 1.12 {
564 :     FILE * f;
565 :     char hdr[18];
566 : edgomez 1.2
567 : edgomez 1.12 f = fopen(filename, "wb");
568 :     if ( f == NULL) {
569 :     return -1;
570 :     }
571 : edgomez 1.2
572 : edgomez 1.12 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 : chl 1.1
599 : edgomez 1.12 #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 1.20 for (i=0; i<XDIM*YDIM*BPP;i+=BPP) {
606 : edgomez 1.12 if (BPP == 1) {
607 : Isibaar 1.20 fputc(*(image+i), f);
608 : edgomez 1.12 } else if (BPP == 2) {
609 : Isibaar 1.20 fputc(*(image+i+1), f);
610 :     fputc(*(image+i+0), f);
611 : edgomez 1.12 } else if (BPP == 3) {
612 : Isibaar 1.20 fputc(*(image+i+2), f);
613 :     fputc(*(image+i+1), f);
614 :     fputc(*(image+i+0), f);
615 : edgomez 1.12 } else if (BPP == 4) {
616 : Isibaar 1.20 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 1.12 }
621 : edgomez 1.2 }
622 : edgomez 1.12 }
623 :     #endif
624 :    
625 :     /* Write Y and V planes for YUV formats */
626 :     if (BPP == 1) {
627 :     int i;
628 :    
629 :     /* 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 :     }
635 :    
636 :    
637 :     /* Close the file */
638 :     fclose(f);
639 :    
640 :     return(0);
641 :     }
642 :    
643 : edgomez 1.15 static int write_pnm(char *filename, unsigned char *image)
644 : edgomez 1.12 {
645 :     FILE * f;
646 : chl 1.1
647 : edgomez 1.12 f = fopen(filename, "wb");
648 :     if ( f == NULL) {
649 :     return -1;
650 :     }
651 : edgomez 1.2
652 : edgomez 1.12 if (BPP == 1) {
653 :     int i;
654 : chl 1.23 fprintf(f, "P5\n%i %i\n255\n", XDIM, YDIM*3/2);
655 : edgomez 1.12
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 : chl 1.1 }
677 : edgomez 1.12
678 :     fclose(f);
679 :    
680 :     return 0;
681 : chl 1.1 }
682 : edgomez 1.15
683 : Isibaar 1.25 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 1.26 fwrite(image, 1, 3*XDIM*YDIM/2, f);
693 : Isibaar 1.25
694 :     fclose(f);
695 :    
696 :     return 0;
697 :     }
698 :    
699 : edgomez 1.2 /*****************************************************************************
700 :     * Routines for decoding: init decoder, use, and stop decoder
701 :     ****************************************************************************/
702 :    
703 :     /* init decoder before first run */
704 :     static int
705 : suxen_drol 1.11 dec_init(int use_assembler, int debug_level)
706 : chl 1.1 {
707 : edgomez 1.10 int ret;
708 :    
709 :     xvid_gbl_init_t xvid_gbl_init;
710 :     xvid_dec_create_t xvid_dec_create;
711 :    
712 : edgomez 1.16 /* 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 1.10 /*------------------------------------------------------------------------
717 : Isibaar 1.27 * Xvid core initialization
718 : edgomez 1.10 *----------------------------------------------------------------------*/
719 : chl 1.1
720 : edgomez 1.10 /* Version */
721 :     xvid_gbl_init.version = XVID_VERSION;
722 : chl 1.1
723 : edgomez 1.10 /* Assembly setting */
724 :     if(use_assembler)
725 : suxen_drol 1.7 #ifdef ARCH_IS_IA64
726 : edgomez 1.10 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
727 : chl 1.1 #else
728 : edgomez 1.10 xvid_gbl_init.cpu_flags = 0;
729 : chl 1.1 #endif
730 : edgomez 1.10 else
731 :     xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
732 : suxen_drol 1.11
733 :     xvid_gbl_init.debug = debug_level;
734 : edgomez 1.10
735 :     xvid_global(NULL, 0, &xvid_gbl_init, NULL);
736 :    
737 :     /*------------------------------------------------------------------------
738 : Isibaar 1.27 * Xvid decoder initialization
739 : edgomez 1.10 *----------------------------------------------------------------------*/
740 : chl 1.1
741 : edgomez 1.10 /* Version */
742 :     xvid_dec_create.version = XVID_VERSION;
743 : chl 1.1
744 : edgomez 1.10 /*
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 : edgomez 1.6
751 : edgomez 1.10 ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
752 : chl 1.1
753 : edgomez 1.10 dec_handle = xvid_dec_create.handle;
754 :    
755 :     return(ret);
756 : chl 1.1 }
757 :    
758 : edgomez 1.2 /* decode one frame */
759 :     static int
760 :     dec_main(unsigned char *istream,
761 : edgomez 1.6 unsigned char *ostream,
762 :     int istream_size,
763 : edgomez 1.10 xvid_dec_stats_t *xvid_dec_stats)
764 : edgomez 1.2 {
765 : chl 1.1
766 : edgomez 1.10 int ret;
767 :    
768 :     xvid_dec_frame_t xvid_dec_frame;
769 : edgomez 1.18
770 :     /* 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 : edgomez 1.10
774 :     /* Set version */
775 :     xvid_dec_frame.version = XVID_VERSION;
776 :     xvid_dec_stats->version = XVID_VERSION;
777 : edgomez 1.6
778 : edgomez 1.10 /* No general flags to set */
779 : Isibaar 1.27 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 1.1
786 : edgomez 1.10 /* Input stream */
787 :     xvid_dec_frame.bitstream = istream;
788 :     xvid_dec_frame.length = istream_size;
789 : chl 1.1
790 : edgomez 1.10 /* Output frame structure */
791 :     xvid_dec_frame.output.plane[0] = ostream;
792 : edgomez 1.12 xvid_dec_frame.output.stride[0] = XDIM*BPP;
793 :     xvid_dec_frame.output.csp = CSP;
794 : chl 1.1
795 : edgomez 1.10 ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
796 : chl 1.1
797 : edgomez 1.10 return(ret);
798 : chl 1.1 }
799 :    
800 : edgomez 1.2 /* close decoder to release resources */
801 :     static int
802 :     dec_stop()
803 : chl 1.1 {
804 : edgomez 1.10 int ret;
805 : edgomez 1.2
806 : edgomez 1.10 ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
807 : chl 1.1
808 : edgomez 1.10 return(ret);
809 : chl 1.1 }

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