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