3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Console based decoding test application - |
* - Console based decoding test application - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert |
* Copyright(C) 2002-2003 Christoph Lampert |
7 |
|
* 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
20 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: xvid_decraw.c,v 1.3 2002-09-27 20:58:30 edgomez Exp $ |
* $Id: xvid_decraw.c,v 1.10.2.1 2004-04-07 22:02:56 edgomez Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
32 |
* the speed for this is measured. |
* the speed for this is measured. |
33 |
* |
* |
34 |
* The program is plain C and needs no libraries except for libxvidcore, |
* The program is plain C and needs no libraries except for libxvidcore, |
35 |
* and maths-lib, so with UN*X you simply compile by |
* and maths-lib. |
36 |
* |
* |
37 |
* gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw |
* Use ./xvid_decraw -help for a list of options |
|
* |
|
|
* You have to specify the image dimensions (until the add the feature |
|
|
* to read this from the bitstream) |
|
|
* |
|
|
* Usage : xvid_decraw <-w width> <-h height> [OPTIONS] |
|
|
* Options : |
|
|
* -asm : use assembly optimizations (default=disabled) |
|
|
* -w integer : frame width ([1.2048]) |
|
|
* -h integer : frame height ([1.2048]) |
|
|
* -i string : input filename (default=stdin) |
|
|
* -t integer : input data type (raw=0, mp4u=1) |
|
|
* -d boolean : save decoder output (0 False*, !=0 True) |
|
|
* -m boolean : save mpeg4 raw stream to single files (0 False*, !=0 True) |
|
|
* -help : This help message |
|
|
* (* means default) |
|
38 |
* |
* |
39 |
****************************************************************************/ |
****************************************************************************/ |
40 |
|
|
42 |
#include <stdlib.h> |
#include <stdlib.h> |
43 |
#include <string.h> |
#include <string.h> |
44 |
#include <math.h> |
#include <math.h> |
45 |
#ifndef _MSC_VER |
#ifndef WIN32 |
46 |
#include <sys/time.h> |
#include <sys/time.h> |
47 |
#else |
#else |
48 |
#include <time.h> |
#include <time.h> |
61 |
static int YDIM = 0; |
static int YDIM = 0; |
62 |
static int ARG_SAVEDECOUTPUT = 0; |
static int ARG_SAVEDECOUTPUT = 0; |
63 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
|
static int ARG_STREAMTYPE = 0; |
|
64 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
65 |
|
static int CSP = XVID_CSP_I420; |
66 |
|
static int BPP = 1; |
67 |
|
|
68 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
69 |
static void *dec_handle = NULL; |
static void *dec_handle = NULL; |
70 |
|
|
71 |
# define BUFFER_SIZE 10*XDIM*YDIM |
#define BUFFER_SIZE (2*1024*1024) |
|
|
|
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
|
|
(((long)(c))<<8) |((long)(d))) |
|
|
|
|
|
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
|
|
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
|
72 |
|
|
73 |
/***************************************************************************** |
/***************************************************************************** |
74 |
* Local prototypes |
* Local prototypes |
81 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
82 |
unsigned char *ostream, |
unsigned char *ostream, |
83 |
int istream_size, |
int istream_size, |
84 |
int *ostream_size); |
xvid_dec_stats_t *xvid_dec_stats); |
85 |
static int dec_stop(); |
static int dec_stop(); |
86 |
static void usage(); |
static void usage(); |
87 |
|
|
88 |
|
|
89 |
|
const char * type2str(int type) |
90 |
|
{ |
91 |
|
if (type==XVID_TYPE_IVOP) |
92 |
|
return "I"; |
93 |
|
if (type==XVID_TYPE_PVOP) |
94 |
|
return "P"; |
95 |
|
if (type==XVID_TYPE_BVOP) |
96 |
|
return "B"; |
97 |
|
return "S"; |
98 |
|
} |
99 |
|
|
100 |
/***************************************************************************** |
/***************************************************************************** |
101 |
* Main program |
* Main program |
102 |
****************************************************************************/ |
****************************************************************************/ |
106 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
107 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
108 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
109 |
int bigendian = 0; |
int useful_bytes; |
110 |
|
xvid_dec_stats_t xvid_dec_stats; |
111 |
|
|
112 |
double totaldectime; |
double totaldectime; |
113 |
|
|
123 |
int i; |
int i; |
124 |
|
|
125 |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
126 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
127 |
|
|
128 |
/***************************************************************************** |
/***************************************************************************** |
129 |
* Command line parsing |
* Command line parsing |
133 |
|
|
134 |
if (strcmp("-asm", argv[i]) == 0 ) { |
if (strcmp("-asm", argv[i]) == 0 ) { |
135 |
use_assembler = 1; |
use_assembler = 1; |
136 |
} |
} else if (strcmp("-d", argv[i]) == 0) { |
137 |
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
ARG_SAVEDECOUTPUT = 1; |
138 |
i++; |
} else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
|
XDIM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
YDIM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_SAVEDECOUTPUT = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
|
139 |
i++; |
i++; |
140 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
141 |
} |
} else if (strcmp("-m", argv[i]) == 0) { |
142 |
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
ARG_SAVEMPEGSTREAM = 1; |
143 |
i++; |
} else if (strcmp("-c", argv[i]) == 0 && i < argc - 1 ) { |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
|
144 |
i++; |
i++; |
145 |
ARG_STREAMTYPE = atoi(argv[i]); |
if (strcmp(argv[i], "rgb16") == 0) { |
146 |
|
CSP = XVID_CSP_RGB555; |
147 |
|
BPP = 2; |
148 |
|
} else if (strcmp(argv[i], "rgb24") == 0) { |
149 |
|
CSP = XVID_CSP_BGR; |
150 |
|
BPP = 3; |
151 |
|
} else if (strcmp(argv[i], "rgb32") == 0) { |
152 |
|
CSP = XVID_CSP_BGRA; |
153 |
|
BPP = 4; |
154 |
|
} else if (strcmp(argv[i], "yv12") == 0) { |
155 |
|
CSP = XVID_CSP_YV12; |
156 |
|
BPP = 1; |
157 |
|
} else { |
158 |
|
CSP = XVID_CSP_I420; |
159 |
|
BPP = 1; |
160 |
} |
} |
161 |
else if (strcmp("-help", argv[i])) { |
} else if (strcmp("-help", argv[i]) == 0) { |
162 |
usage(); |
usage(); |
163 |
return(0); |
return(0); |
164 |
} |
} else { |
|
else { |
|
165 |
usage(); |
usage(); |
166 |
exit(-1); |
exit(-1); |
167 |
} |
} |
|
|
|
168 |
} |
} |
169 |
|
|
170 |
/***************************************************************************** |
/***************************************************************************** |
171 |
* Values checking |
* Values checking |
172 |
****************************************************************************/ |
****************************************************************************/ |
173 |
|
|
|
if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) { |
|
|
usage(); |
|
|
return -1; |
|
|
} |
|
|
|
|
174 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
175 |
in_file = stdin; |
in_file = stdin; |
176 |
} |
} |
179 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
180 |
if (in_file == NULL) { |
if (in_file == NULL) { |
181 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
182 |
return -1; |
return(-1); |
183 |
} |
} |
184 |
} |
} |
185 |
|
|
193 |
if (!mp4_buffer) |
if (!mp4_buffer) |
194 |
goto free_all_memory; |
goto free_all_memory; |
195 |
|
|
|
/* Memory for frame output */ |
|
|
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
|
|
if (!out_buffer) |
|
|
goto free_all_memory; |
|
|
|
|
|
|
|
196 |
/***************************************************************************** |
/***************************************************************************** |
197 |
* XviD PART Start |
* XviD PART Start |
198 |
****************************************************************************/ |
****************************************************************************/ |
209 |
* Main loop |
* Main loop |
210 |
****************************************************************************/ |
****************************************************************************/ |
211 |
|
|
212 |
if(ARG_STREAMTYPE) { |
/* Fill the buffer */ |
213 |
|
useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); |
|
unsigned char header[4]; |
|
|
|
|
|
/* MP4U format : read header */ |
|
|
if(feof(in_file)) |
|
|
goto release_all; |
|
|
fread(header, 4, 1, in_file); |
|
|
|
|
|
if(header[0] != '2' || header[1] != 'M' || |
|
|
header[2] != 'O' || header[3] != 'G') { |
|
|
fprintf(stderr, "Error, this not a mp4u container file\n"); |
|
|
goto release_all; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
totalsize = LONG_PACK('M','P','4','U'); |
|
|
mp4_ptr = (unsigned char *)&totalsize; |
|
|
if(*mp4_ptr == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
214 |
|
|
215 |
totaldectime = 0; |
totaldectime = 0; |
216 |
totalsize = 0; |
totalsize = 0; |
218 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
219 |
|
|
220 |
do { |
do { |
|
|
|
|
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
|
221 |
int used_bytes = 0; |
int used_bytes = 0; |
222 |
double dectime; |
double dectime; |
223 |
|
|
|
/* Read data from input file */ |
|
|
if(ARG_STREAMTYPE) { |
|
|
|
|
|
/* MP4U container */ |
|
|
|
|
|
/* Read stream size first */ |
|
|
if(feof(in_file)) |
|
|
break; |
|
|
fread(&mp4_size, sizeof(long), 1, in_file); |
|
|
|
|
|
if(bigendian) |
|
|
mp4_size = SWAP(mp4_size); |
|
|
|
|
|
/* Read mp4_size_bytes */ |
|
|
if(feof(in_file)) |
|
|
break; |
|
|
fread(mp4_buffer, mp4_size, 1, in_file); |
|
|
|
|
224 |
/* |
/* |
225 |
* When reading mp4u, we don't have to care about buffer |
* If the buffer is half empty or there are no more bytes in it |
226 |
* filling as we know exactly how much bytes there are in |
* then fill it. |
|
* next frame |
|
227 |
*/ |
*/ |
|
mp4_ptr = mp4_buffer; |
|
|
|
|
|
} |
|
|
else { |
|
|
|
|
|
/* Real raw stream */ |
|
|
|
|
|
/* buffer more than half empty -> Fill it */ |
|
228 |
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { |
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { |
229 |
int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
230 |
|
|
231 |
/* Move data if needed */ |
/* Move data if needed */ |
232 |
if (rest) |
if (already_in_buffer > 0) |
233 |
memcpy(mp4_buffer, mp4_ptr, rest); |
memcpy(mp4_buffer, mp4_ptr, already_in_buffer); |
234 |
|
|
235 |
/* Update mp4_ptr */ |
/* Update mp4_ptr */ |
236 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
238 |
/* read new data */ |
/* read new data */ |
239 |
if(feof(in_file)) |
if(feof(in_file)) |
240 |
break; |
break; |
241 |
fread(mp4_buffer + rest, BUFFER_SIZE - rest, 1, in_file); |
|
242 |
} |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
243 |
|
1, BUFFER_SIZE - already_in_buffer, |
244 |
|
in_file); |
245 |
|
|
246 |
} |
} |
247 |
|
|
248 |
|
|
249 |
|
/* This loop is needed to handle VOL/NVOP reading */ |
250 |
|
do { |
251 |
|
|
252 |
/* Decode frame */ |
/* Decode frame */ |
253 |
dectime = msecond(); |
dectime = msecond(); |
254 |
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes); |
used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats); |
255 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
256 |
|
|
257 |
if (status) { |
/* Resize image buffer if needed */ |
258 |
break; |
if(xvid_dec_stats.type == XVID_TYPE_VOL) { |
259 |
|
|
260 |
|
/* Check if old buffer is smaller */ |
261 |
|
if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) { |
262 |
|
|
263 |
|
/* Copy new witdh and new height from the vol structure */ |
264 |
|
XDIM = xvid_dec_stats.data.vol.width; |
265 |
|
YDIM = xvid_dec_stats.data.vol.height; |
266 |
|
|
267 |
|
/* Free old output buffer*/ |
268 |
|
if(out_buffer) free(out_buffer); |
269 |
|
|
270 |
|
/* Allocate the new buffer */ |
271 |
|
out_buffer = (unsigned char*)malloc(XDIM*YDIM*4); |
272 |
|
if(out_buffer == NULL) |
273 |
|
goto free_all_memory; |
274 |
|
|
275 |
|
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
276 |
|
} |
277 |
} |
} |
278 |
|
|
279 |
/* |
/* Update buffer pointers */ |
280 |
* Only needed for real raw stream, mp4u uses |
if(used_bytes > 0) { |
|
* mp4_ptr = mp4_buffer for each frame |
|
|
*/ |
|
281 |
mp4_ptr += used_bytes; |
mp4_ptr += used_bytes; |
282 |
|
useful_bytes -= used_bytes; |
283 |
|
|
284 |
/* Updated data */ |
/* Total size */ |
285 |
totalsize += used_bytes; |
totalsize += used_bytes; |
286 |
|
} |
287 |
|
|
288 |
|
}while(xvid_dec_stats.type <= 0 && useful_bytes > 0); |
289 |
|
|
290 |
|
/* Check if there is a negative number of useful bytes left in buffer |
291 |
|
* This means we went too far */ |
292 |
|
if(useful_bytes < 0) |
293 |
|
break; |
294 |
|
|
295 |
|
/* Updated data - Count only usefull decode time */ |
296 |
totaldectime += dectime; |
totaldectime += dectime; |
297 |
|
|
|
/* Prints some decoding stats */ |
|
|
printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n", |
|
|
filenr, dectime, used_bytes); |
|
298 |
|
|
299 |
/* Save individual mpeg4 strean if required */ |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
300 |
|
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
301 |
|
|
302 |
|
/* Save individual mpeg4 stream if required */ |
303 |
if (ARG_SAVEMPEGSTREAM) { |
if (ARG_SAVEMPEGSTREAM) { |
304 |
FILE *filehandle = NULL; |
FILE *filehandle = NULL; |
305 |
|
|
311 |
filename); |
filename); |
312 |
} |
} |
313 |
else { |
else { |
314 |
fwrite(mp4_buffer, used_bytes, 1, filehandle); |
fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle); |
315 |
fclose(filehandle); |
fclose(filehandle); |
316 |
} |
} |
317 |
} |
} |
318 |
|
|
|
|
|
319 |
/* Save output frame if required */ |
/* Save output frame if required */ |
320 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
321 |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
sprintf(filename, "%sdec%05d.tga", filepath, filenr); |
322 |
if(write_pgm(filename,out_buffer)) { |
if(write_tga(filename,out_buffer)) { |
323 |
fprintf(stderr, |
fprintf(stderr, |
324 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded PGM frame %s\n", |
325 |
filename); |
filename); |
330 |
|
|
331 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR) ); |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR) ); |
332 |
|
|
333 |
|
/***************************************************************************** |
334 |
|
* Flush decoder buffers |
335 |
|
****************************************************************************/ |
336 |
|
|
337 |
|
do { |
338 |
|
|
339 |
|
/* Fake vars */ |
340 |
|
int used_bytes; |
341 |
|
double dectime; |
342 |
|
|
343 |
|
do { |
344 |
|
dectime = msecond(); |
345 |
|
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
346 |
|
dectime = msecond() - dectime; |
347 |
|
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
348 |
|
|
349 |
|
if (used_bytes < 0) { /* XVID_ERR_END */ |
350 |
|
break; |
351 |
|
} |
352 |
|
|
353 |
|
/* Updated data - Count only usefull decode time */ |
354 |
|
totaldectime += dectime; |
355 |
|
|
356 |
|
/* Prints some decoding stats */ |
357 |
|
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
358 |
|
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
359 |
|
|
360 |
|
/* Save output frame if required */ |
361 |
|
if (ARG_SAVEDECOUTPUT) { |
362 |
|
sprintf(filename, "%sdec%05d.tga", filepath, filenr); |
363 |
|
if(write_tga(filename, out_buffer)) { |
364 |
|
fprintf(stderr, |
365 |
|
"Error writing decoded PGM frame %s\n", |
366 |
|
filename); |
367 |
|
} |
368 |
|
} |
369 |
|
|
370 |
|
filenr++; |
371 |
|
|
372 |
|
}while(1); |
373 |
|
|
374 |
/***************************************************************************** |
/***************************************************************************** |
375 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
378 |
totalsize /= filenr; |
totalsize /= filenr; |
379 |
totaldectime /= filenr; |
totaldectime /= filenr; |
380 |
|
|
381 |
printf("Avg: dectime %5.2f ms, %5.2f fps, mp4 stream size =%d\n", |
printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", |
382 |
totaldectime, 1000/totaldectime, totalsize); |
totaldectime, 1000/totaldectime, (int)totalsize); |
383 |
|
|
384 |
/***************************************************************************** |
/***************************************************************************** |
385 |
* XviD PART Stop |
* XviD PART Stop |
386 |
/****************************************************************************/ |
****************************************************************************/ |
387 |
|
|
388 |
release_all: |
release_all: |
389 |
if (dec_handle) { |
if (dec_handle) { |
396 |
free(out_buffer); |
free(out_buffer); |
397 |
free(mp4_buffer); |
free(mp4_buffer); |
398 |
|
|
399 |
return 0; |
return(0); |
400 |
} |
} |
401 |
|
|
402 |
/***************************************************************************** |
/***************************************************************************** |
406 |
static void usage() |
static void usage() |
407 |
{ |
{ |
408 |
|
|
409 |
fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n"); |
fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); |
410 |
fprintf(stderr, "Options :\n"); |
fprintf(stderr, "Options :\n"); |
411 |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
|
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
|
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
|
412 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
413 |
fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
414 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -c csp : choose colorspace output (csp can be one of rgb16, rgb24, rgb32, yv12, i420)\n"); |
415 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
416 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
417 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
418 |
|
|
426 |
static double |
static double |
427 |
msecond() |
msecond() |
428 |
{ |
{ |
429 |
#ifndef _MSC_VER |
#ifndef WIN32 |
430 |
struct timeval tv; |
struct timeval tv; |
431 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
432 |
return (double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3; |
return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3); |
433 |
#else |
#else |
434 |
clock_t clk; |
clock_t clk; |
435 |
clk = clock(); |
clk = clock(); |
436 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
437 |
#endif |
#endif |
438 |
} |
} |
439 |
|
|
|
|
|
440 |
/***************************************************************************** |
/***************************************************************************** |
441 |
* output functions |
* output functions |
442 |
****************************************************************************/ |
****************************************************************************/ |
443 |
|
|
444 |
static int |
int write_tga(char *filename, unsigned char *image) |
|
write_pgm(char *filename, |
|
|
unsigned char *image) |
|
445 |
{ |
{ |
446 |
int loop; |
FILE * f; |
447 |
|
char hdr[18]; |
448 |
|
|
449 |
unsigned char *y = image; |
f = fopen(filename, "wb"); |
450 |
unsigned char *u = image + XDIM*YDIM; |
if ( f == NULL) { |
451 |
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
return -1; |
452 |
|
} |
|
FILE *filehandle; |
|
|
filehandle=fopen(filename,"w+b"); |
|
|
if (filehandle) { |
|
453 |
|
|
454 |
/* Write header */ |
hdr[0] = 0; /* ID length */ |
455 |
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
hdr[1] = 0; /* Color map type */ |
456 |
|
hdr[2] = (BPP>1)?2:3; /* Uncompressed true color (2) or greymap (3) */ |
457 |
|
hdr[3] = 0; /* Color map specification (not used) */ |
458 |
|
hdr[4] = 0; /* Color map specification (not used) */ |
459 |
|
hdr[5] = 0; /* Color map specification (not used) */ |
460 |
|
hdr[6] = 0; /* Color map specification (not used) */ |
461 |
|
hdr[7] = 0; /* Color map specification (not used) */ |
462 |
|
hdr[8] = 0; /* LSB X origin */ |
463 |
|
hdr[9] = 0; /* MSB X origin */ |
464 |
|
hdr[10] = 0; /* LSB Y origin */ |
465 |
|
hdr[11] = 0; /* MSB Y origin */ |
466 |
|
hdr[12] = (XDIM>>0)&0xff; /* LSB Width */ |
467 |
|
hdr[13] = (XDIM>>8)&0xff; /* MSB Width */ |
468 |
|
if (BPP > 1) { |
469 |
|
hdr[14] = (YDIM>>0)&0xff; /* LSB Height */ |
470 |
|
hdr[15] = (YDIM>>8)&0xff; /* MSB Height */ |
471 |
|
} else { |
472 |
|
hdr[14] = ((YDIM*3)>>1)&0xff; /* LSB Height */ |
473 |
|
hdr[15] = ((YDIM*3)>>9)&0xff; /* MSB Height */ |
474 |
|
} |
475 |
|
hdr[16] = BPP*8; |
476 |
|
hdr[17] = 0x00 | (1<<5) /* Up to down */ | (0<<4); /* Image descriptor */ |
477 |
|
|
478 |
/* Write Y data */ |
/* Write header */ |
479 |
fwrite(y, 1, XDIM*YDIM, filehandle); |
fwrite(hdr, 1, sizeof(hdr), f); |
480 |
|
|
481 |
for(loop=0; loop<YDIM/2; loop++) |
#ifdef ARCH_IS_LITTLE_ENDIAN |
482 |
|
/* write first plane */ |
483 |
|
fwrite(image, 1, XDIM*YDIM*BPP, f); |
484 |
|
#else |
485 |
{ |
{ |
486 |
/* Write U scanline */ |
int i; |
487 |
fwrite(u, 1, XDIM/2, filehandle); |
for (i=0; i<width*height*BPP;i+=BPP) { |
488 |
|
if (BPP == 1) { |
489 |
/* Write V scanline */ |
fputc(image+i, f); |
490 |
fwrite(v, 1, XDIM/2, filehandle); |
} else if (BPP == 2) { |
491 |
|
fputc(image+i+1, f); |
492 |
|
fputc(image+i+0, f); |
493 |
|
} else if (BPP == 3) { |
494 |
|
fputc(image+i+2, f); |
495 |
|
fputc(image+i+1, f); |
496 |
|
fputc(image+i+0, f); |
497 |
|
} else if (BPP == 4) { |
498 |
|
fputc(image+i+3, f); |
499 |
|
fputc(image+i+2, f); |
500 |
|
fputc(image+i+1, f); |
501 |
|
fputc(image+i+0, f); |
502 |
|
} |
503 |
|
} |
504 |
|
} |
505 |
|
#endif |
506 |
|
|
507 |
/* Update pointers */ |
/* Write Y and V planes for YUV formats */ |
508 |
u += XDIM/2; |
if (BPP == 1) { |
509 |
v += XDIM/2; |
int i; |
510 |
|
|
511 |
|
/* Write the two chrominance planes */ |
512 |
|
for (i=0; i<YDIM/2; i++) { |
513 |
|
fwrite(image+XDIM*YDIM+XDIM/2*i, 1, XDIM/2, f); |
514 |
|
fwrite(image+5*XDIM*YDIM/4 + XDIM/2*i, 1, XDIM/2, f); |
515 |
|
} |
516 |
} |
} |
517 |
|
|
|
/* Close file */ |
|
|
fclose(filehandle); |
|
518 |
|
|
519 |
return 0; |
/* Close the file */ |
520 |
} |
fclose(f); |
521 |
else |
|
522 |
return 1; |
return(0); |
523 |
} |
} |
524 |
|
|
525 |
/***************************************************************************** |
/***************************************************************************** |
530 |
static int |
static int |
531 |
dec_init(int use_assembler) |
dec_init(int use_assembler) |
532 |
{ |
{ |
533 |
int xerr; |
int ret; |
534 |
|
|
535 |
|
xvid_gbl_init_t xvid_gbl_init; |
536 |
|
xvid_dec_create_t xvid_dec_create; |
537 |
|
|
538 |
|
/*------------------------------------------------------------------------ |
539 |
|
* XviD core initialization |
540 |
|
*----------------------------------------------------------------------*/ |
541 |
|
|
542 |
XVID_INIT_PARAM xinit; |
/* Version */ |
543 |
XVID_DEC_PARAM xparam; |
xvid_gbl_init.version = XVID_VERSION; |
544 |
|
|
545 |
|
/* Assembly setting */ |
546 |
if(use_assembler) |
if(use_assembler) |
547 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
548 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
549 |
#else |
#else |
550 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
551 |
#endif |
#endif |
552 |
else |
else |
553 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
554 |
|
|
555 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
|
xparam.width = XDIM; |
|
|
xparam.height = YDIM; |
|
556 |
|
|
557 |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
/*------------------------------------------------------------------------ |
558 |
dec_handle = xparam.handle; |
* XviD encoder initialization |
559 |
|
*----------------------------------------------------------------------*/ |
560 |
|
|
561 |
return xerr; |
/* Version */ |
562 |
|
xvid_dec_create.version = XVID_VERSION; |
563 |
|
|
564 |
|
/* |
565 |
|
* Image dimensions -- set to 0, xvidcore will resize when ever it is |
566 |
|
* needed |
567 |
|
*/ |
568 |
|
xvid_dec_create.width = 0; |
569 |
|
xvid_dec_create.height = 0; |
570 |
|
|
571 |
|
ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL); |
572 |
|
|
573 |
|
dec_handle = xvid_dec_create.handle; |
574 |
|
|
575 |
|
return(ret); |
576 |
} |
} |
577 |
|
|
578 |
/* decode one frame */ |
/* decode one frame */ |
580 |
dec_main(unsigned char *istream, |
dec_main(unsigned char *istream, |
581 |
unsigned char *ostream, |
unsigned char *ostream, |
582 |
int istream_size, |
int istream_size, |
583 |
int *ostream_size) |
xvid_dec_stats_t *xvid_dec_stats) |
584 |
{ |
{ |
585 |
|
|
586 |
int xerr; |
int ret; |
587 |
XVID_DEC_FRAME xframe; |
|
588 |
|
xvid_dec_frame_t xvid_dec_frame; |
589 |
|
|
590 |
|
/* Set version */ |
591 |
|
xvid_dec_frame.version = XVID_VERSION; |
592 |
|
xvid_dec_stats->version = XVID_VERSION; |
593 |
|
|
594 |
|
/* No general flags to set */ |
595 |
|
xvid_dec_frame.general = 0; |
596 |
|
|
597 |
xframe.bitstream = istream; |
/* Input stream */ |
598 |
xframe.length = istream_size; |
xvid_dec_frame.bitstream = istream; |
599 |
xframe.image = ostream; |
xvid_dec_frame.length = istream_size; |
|
xframe.stride = XDIM; |
|
|
xframe.colorspace = XVID_CSP_YV12; |
|
600 |
|
|
601 |
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL); |
/* Output frame structure */ |
602 |
|
xvid_dec_frame.output.plane[0] = ostream; |
603 |
|
xvid_dec_frame.output.stride[0] = XDIM*BPP; |
604 |
|
xvid_dec_frame.output.csp = CSP; |
605 |
|
|
606 |
*ostream_size = xframe.length; |
ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats); |
607 |
|
|
608 |
return xerr; |
return(ret); |
609 |
} |
} |
610 |
|
|
611 |
/* close decoder to release resources */ |
/* close decoder to release resources */ |
612 |
static int |
static int |
613 |
dec_stop() |
dec_stop() |
614 |
{ |
{ |
615 |
int xerr; |
int ret; |
616 |
|
|
617 |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
618 |
|
|
619 |
return xerr; |
return(ret); |
620 |
} |
} |