1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC - Example for encoding and decoding |
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Console based decoding test application - |
5 |
|
* |
6 |
|
* Copyright(C) 2002-2003 Christoph Lampert |
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
9 |
* 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 |
17 |
* |
* |
18 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
|
* |
22 |
|
* $Id: xvid_decraw.c,v 1.7 2003-02-16 05:11:39 suxen_drol Exp $ |
23 |
* |
* |
24 |
*************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
/************************************************************************ |
/***************************************************************************** |
27 |
* |
* |
28 |
* Test routine for XviD decoding |
* Application notes : |
|
* (C) Christoph Lampert, 2002/08/17 |
|
29 |
* |
* |
30 |
* An MPEG-4 bitstream is read from stdin and decoded, |
* An MPEG-4 bitstream is read from an input file (or stdin) and decoded, |
31 |
* the speed for this is measured |
* the speed for this is measured. |
32 |
* |
* |
33 |
* The program is plain C and needs no libraries except for libxvidcore, |
* The program is plain C and needs no libraries except for libxvidcore, |
34 |
* and maths-lib, so with UN*X you simply compile by |
* and maths-lib, so with UN*X you simply compile by |
38 |
* You have to specify the image dimensions (until the add the feature |
* You have to specify the image dimensions (until the add the feature |
39 |
* to read this from the bitstream) |
* to read this from the bitstream) |
40 |
* |
* |
41 |
* Parameters are: xvid_stat XDIM YDIM |
* Usage : xvid_decraw <-w width> <-h height> [OPTIONS] |
42 |
|
* Options : |
43 |
|
* -asm : use assembly optimizations (default=disabled) |
44 |
|
* -w integer : frame width ([1.2048]) |
45 |
|
* -h integer : frame height ([1.2048]) |
46 |
|
* -i string : input filename (default=stdin) |
47 |
|
* -t integer : input data type (raw=0, mp4u=1) |
48 |
|
* -d boolean : save decoder output (0 False*, !=0 True) |
49 |
|
* -m boolean : save mpeg4 raw stream to single files (0 False*, !=0 True) |
50 |
|
* -help : This help message |
51 |
|
* (* means default) |
52 |
* |
* |
53 |
* output and indivual m4v-files are saved, if corresponding flags are set |
****************************************************************************/ |
|
* |
|
|
************************************************************************/ |
|
54 |
|
|
55 |
#include <stdio.h> |
#include <stdio.h> |
56 |
#include <stdlib.h> |
#include <stdlib.h> |
57 |
#include <math.h> // needed for log10 |
#include <string.h> |
58 |
#include <sys/time.h> // only needed for gettimeofday |
#include <math.h> |
59 |
|
#ifndef WIN32 |
60 |
|
#include <sys/time.h> |
61 |
|
#else |
62 |
|
#include <time.h> |
63 |
|
#endif |
64 |
|
|
65 |
#include "../src/xvid.h" /* comes with XviD */ |
#include "xvid.h" |
66 |
|
|
67 |
#define ABS_MAXFRAMENR 9999 // max number of frames |
/***************************************************************************** |
68 |
|
* Global vars in module and constants |
69 |
|
****************************************************************************/ |
70 |
|
|
71 |
|
/* max number of frames */ |
72 |
|
#define ABS_MAXFRAMENR 9999 |
73 |
|
|
74 |
|
static int XDIM = 0; |
75 |
|
static int YDIM = 0; |
76 |
|
static int ARG_SAVEDECOUTPUT = 0; |
77 |
|
static int ARG_SAVEMPEGSTREAM = 0; |
78 |
|
static char *ARG_INPUTFILE = NULL; |
79 |
|
|
80 |
|
|
81 |
|
static char filepath[256] = "./"; |
82 |
|
static void *dec_handle = NULL; |
83 |
|
|
84 |
|
# define BUFFER_SIZE 10*XDIM*YDIM |
85 |
|
|
86 |
|
/***************************************************************************** |
87 |
|
* Local prototypes |
88 |
|
****************************************************************************/ |
89 |
|
|
90 |
|
static double msecond(); |
91 |
|
static int write_pgm(char *filename, |
92 |
|
unsigned char *image); |
93 |
|
static int dec_init(int use_assembler); |
94 |
|
static int dec_main(unsigned char *istream, |
95 |
|
unsigned char *ostream, |
96 |
|
int istream_size, |
97 |
|
int *ostream_size, |
98 |
|
int *isframe); |
99 |
|
static int dec_stop(); |
100 |
|
static void usage(); |
101 |
|
|
102 |
|
/***************************************************************************** |
103 |
|
* Main program |
104 |
|
****************************************************************************/ |
105 |
|
|
106 |
int XDIM=720; |
int main(int argc, char *argv[]) |
107 |
int YDIM=576; |
{ |
108 |
int i,filenr = 0; |
unsigned char *mp4_buffer = NULL; |
109 |
|
unsigned char *mp4_ptr = NULL; |
110 |
|
unsigned char *out_buffer = NULL; |
111 |
|
int bigendian = 0; |
112 |
|
int still_left_in_buffer; |
113 |
|
int delayed_frames; |
114 |
|
|
115 |
int save_dec_flag = 1; // save decompressed bytestream? |
double totaldectime; |
|
int save_m4v_flag = 0; // save bytestream itself? |
|
116 |
|
|
117 |
char filepath[256] = "./"; // the path where to save output |
long totalsize; |
118 |
|
int status; |
119 |
|
|
120 |
void *dec_handle = NULL; // handle for decoding |
int use_assembler = 0; |
121 |
|
|
122 |
/*********************************************************************/ |
char filename[256]; |
|
/* "statistical" functions */ |
|
|
/* */ |
|
|
/* these are not needed for decoding itself, but for measuring */ |
|
|
/* time (and maybe later quality), there in nothing specific to */ |
|
|
/* XviD in these */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
123 |
|
|
124 |
double msecond() |
FILE *in_file; |
125 |
/* return the current time in seconds(!) */ |
int filenr; |
126 |
{ |
int i; |
|
struct timeval tv; |
|
|
gettimeofday(&tv, 0); |
|
|
return tv.tv_sec + tv.tv_usec * 1.0e-6; |
|
|
} |
|
127 |
|
|
128 |
|
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
129 |
|
printf("written by Christoph Lampert 2002-2003\n\n"); |
130 |
|
|
131 |
/*********************************************************************/ |
/***************************************************************************** |
132 |
/* input and output functions */ |
* Command line parsing |
133 |
/* */ |
****************************************************************************/ |
|
/* the are small and simple routines for writing image */ |
|
|
/* image. It's just for convenience, again nothing specific to XviD */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
134 |
|
|
135 |
int write_pgm(char *filename, unsigned char *image) |
for (i=1; i< argc; i++) { |
|
{ |
|
|
FILE *filehandle; |
|
|
filehandle=fopen(filename,"wb"); |
|
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P5\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2); |
|
|
fwrite(image,XDIM,YDIM*3/2,filehandle); |
|
136 |
|
|
137 |
fclose(filehandle); |
if (strcmp("-asm", argv[i]) == 0 ) { |
138 |
return 0; |
use_assembler = 1; |
139 |
} |
} |
140 |
else |
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
141 |
return 1; |
i++; |
142 |
|
XDIM = atoi(argv[i]); |
143 |
|
} |
144 |
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
145 |
|
i++; |
146 |
|
YDIM = atoi(argv[i]); |
147 |
|
} |
148 |
|
else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) { |
149 |
|
i++; |
150 |
|
ARG_SAVEDECOUTPUT = atoi(argv[i]); |
151 |
|
} |
152 |
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
153 |
|
i++; |
154 |
|
ARG_INPUTFILE = argv[i]; |
155 |
|
} |
156 |
|
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
157 |
|
i++; |
158 |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
159 |
|
} |
160 |
|
else if (strcmp("-help", argv[i])) { |
161 |
|
usage(); |
162 |
|
return(0); |
163 |
|
} |
164 |
|
else { |
165 |
|
usage(); |
166 |
|
exit(-1); |
167 |
} |
} |
168 |
|
|
169 |
|
} |
170 |
|
|
171 |
int write_ppm(char *filename, unsigned char *image) |
/***************************************************************************** |
172 |
{ |
* Values checking |
173 |
FILE *filehandle; |
****************************************************************************/ |
|
filehandle=fopen(filename,"wb"); |
|
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P6\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM); |
|
|
fwrite(image,XDIM,YDIM*3,filehandle); |
|
174 |
|
|
175 |
fclose(filehandle); |
if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) { |
176 |
return 0; |
usage(); |
177 |
|
return -1; |
178 |
} |
} |
179 |
else |
|
180 |
return 1; |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
181 |
|
in_file = stdin; |
182 |
} |
} |
183 |
|
else { |
184 |
|
|
185 |
/*********************************************************************/ |
in_file = fopen(ARG_INPUTFILE, "rb"); |
186 |
/* Routines for decoding: init encoder, frame step, release encoder */ |
if (in_file == NULL) { |
187 |
/*********************************************************************/ |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
188 |
|
return -1; |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
int dec_init(int use_assembler) /* init decoder before first run */ |
/***************************************************************************** |
193 |
{ |
* Memory allocation |
194 |
int xerr; |
****************************************************************************/ |
195 |
|
|
196 |
XVID_INIT_PARAM xinit; |
/* Memory for encoded mp4 stream */ |
197 |
XVID_DEC_PARAM xparam; |
mp4_buffer = (unsigned char *) malloc(BUFFER_SIZE); |
198 |
|
mp4_ptr = mp4_buffer; |
199 |
|
if (!mp4_buffer) |
200 |
|
goto free_all_memory; |
201 |
|
|
202 |
if(use_assembler) |
/* Memory for frame output */ |
203 |
#ifdef ARCH_IA64 |
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
204 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
if (!out_buffer) |
205 |
#else |
goto free_all_memory; |
|
xinit.cpu_flags = 0; |
|
|
#endif |
|
|
else |
|
|
xinit.cpu_flags = XVID_CPU_FORCE; |
|
206 |
|
|
|
xvid_init(NULL, 0, &xinit, NULL); |
|
|
xparam.width = XDIM; |
|
|
xparam.height = YDIM; |
|
207 |
|
|
208 |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
/***************************************************************************** |
209 |
dec_handle = xparam.handle; |
* XviD PART Start |
210 |
|
****************************************************************************/ |
211 |
|
|
212 |
return xerr; |
status = dec_init(use_assembler); |
213 |
|
if (status) { |
214 |
|
fprintf(stderr, |
215 |
|
"Decore INIT problem, return value %d\n", status); |
216 |
|
goto release_all; |
217 |
} |
} |
218 |
|
|
|
int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,int *m4v_size) |
|
|
{ /* decode one frame */ |
|
219 |
|
|
220 |
int xerr; |
/***************************************************************************** |
221 |
XVID_DEC_FRAME xframe; |
* Main loop |
222 |
|
****************************************************************************/ |
223 |
|
|
224 |
|
/* Fill the buffer */ |
225 |
|
still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); |
226 |
|
|
227 |
|
totaldectime = 0; |
228 |
|
totalsize = 0; |
229 |
|
filenr = 0; |
230 |
|
delayed_frames = 0; |
231 |
|
mp4_ptr = mp4_buffer; |
232 |
|
|
233 |
xframe.bitstream = m4v_buffer; |
do { |
234 |
xframe.length = 9999; |
|
235 |
xframe.image = out_buffer; |
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
236 |
xframe.stride = XDIM; |
int used_bytes = 0; |
237 |
xframe.colorspace = XVID_CSP_YV12; |
double dectime; |
238 |
|
int notification; |
239 |
|
|
240 |
|
/* |
241 |
|
* If the buffer is half empty or there are no more bytes in it |
242 |
|
* then fill it. |
243 |
|
*/ |
244 |
|
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 || |
245 |
|
still_left_in_buffer <= 0) { |
246 |
|
int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
247 |
|
|
248 |
|
/* Move data if needed */ |
249 |
|
if (rest) |
250 |
|
memcpy(mp4_buffer, mp4_ptr, rest); |
251 |
|
|
252 |
|
/* Update mp4_ptr */ |
253 |
|
mp4_ptr = mp4_buffer; |
254 |
|
|
255 |
|
/* read new data */ |
256 |
|
if(feof(in_file)) |
257 |
|
break; |
258 |
|
|
259 |
|
still_left_in_buffer = fread(mp4_buffer + rest, |
260 |
|
1, |
261 |
|
BUFFER_SIZE - rest, |
262 |
|
in_file); |
263 |
|
|
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
/* This loop flushes N_VOPS (with vop_coded bit set to 0) */ |
268 |
do { |
do { |
|
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL); |
|
269 |
|
|
270 |
*m4v_size = xframe.length; |
/* Decode frame */ |
271 |
xframe.bitstream += *m4v_size; |
dectime = msecond(); |
272 |
|
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, ¬ification); |
273 |
|
dectime = msecond() - dectime; |
274 |
|
|
275 |
} while (*m4v_size<7); /* this skips N-VOPs */ |
if (status) { |
276 |
|
break; |
277 |
|
} |
278 |
|
|
279 |
return xerr; |
/* Update buffer pointers */ |
280 |
|
mp4_ptr += used_bytes; |
281 |
|
still_left_in_buffer -= used_bytes; |
282 |
|
|
283 |
|
/* Total size */ |
284 |
|
totalsize += used_bytes; |
285 |
|
|
286 |
|
}while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */ |
287 |
|
|
288 |
|
/* Negative buffer would mean we went too far */ |
289 |
|
if(still_left_in_buffer < 0) break; |
290 |
|
|
291 |
|
/* Skip when decoder is buffering images or decoding VOL information */ |
292 |
|
if(notification != XVID_DEC_VOP) { |
293 |
|
/* It's a delay only if it notifies NOTHING */ |
294 |
|
if(notification == XVID_DEC_NOTHING) |
295 |
|
delayed_frames++; |
296 |
|
continue; |
297 |
} |
} |
298 |
|
|
299 |
int dec_stop() /* close decoder to release resources */ |
/* Updated data - Count only usefull decode time */ |
300 |
{ |
totaldectime += dectime; |
|
int xerr; |
|
|
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
|
301 |
|
|
302 |
return xerr; |
/* Prints some decoding stats */ |
303 |
|
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
304 |
|
filenr, dectime, used_bytes); |
305 |
|
|
306 |
|
/* Save individual mpeg4 stream if required */ |
307 |
|
if (ARG_SAVEMPEGSTREAM) { |
308 |
|
FILE *filehandle = NULL; |
309 |
|
|
310 |
|
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
311 |
|
filehandle = fopen(filename, "wb"); |
312 |
|
if(!filehandle) { |
313 |
|
fprintf(stderr, |
314 |
|
"Error writing single mpeg4 stream to file %s\n", |
315 |
|
filename); |
316 |
|
} |
317 |
|
else { |
318 |
|
fwrite(mp4_buffer, 1, used_bytes, filehandle); |
319 |
|
fclose(filehandle); |
320 |
|
} |
321 |
} |
} |
322 |
|
|
323 |
|
/* Save output frame if required */ |
324 |
|
if (ARG_SAVEDECOUTPUT) { |
325 |
|
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
326 |
|
if(write_pgm(filename,out_buffer)) { |
327 |
|
fprintf(stderr, |
328 |
|
"Error writing decoded PGM frame %s\n", |
329 |
|
filename); |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
/*********************************************************************/ |
filenr++; |
|
/* Main program */ |
|
|
/*********************************************************************/ |
|
334 |
|
|
335 |
int main(int argc, char *argv[]) |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
336 |
{ |
|
337 |
unsigned char *divx_buffer = NULL; |
/***************************************************************************** |
338 |
unsigned char *divx_ptr = NULL; |
* Flush decoder buffers |
339 |
unsigned char *out_buffer = NULL; |
****************************************************************************/ |
340 |
|
while(delayed_frames--) { |
341 |
|
|
342 |
|
/* Fake vars */ |
343 |
|
int used_bytes, isframe; |
344 |
double dectime; |
double dectime; |
|
double totaldectime=0.; |
|
345 |
|
|
346 |
long totalsize=0; |
/* Decode frame */ |
347 |
int status; |
dectime = msecond(); |
348 |
|
status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe); |
349 |
|
dectime = msecond() - dectime; |
350 |
|
|
351 |
int m4v_size; |
if (status) { |
352 |
int frame_type[ABS_MAXFRAMENR]; |
break; |
353 |
int use_assembler=1; |
} |
354 |
|
|
355 |
char filename[256]; |
/* Updated data - Count only usefull decode time */ |
356 |
|
totaldectime += dectime; |
357 |
|
|
358 |
FILE *filehandle; |
/* Prints some decoding stats */ |
359 |
|
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
360 |
|
filenr, dectime, used_bytes); |
361 |
|
|
362 |
if (argc>=3) |
/* Save output frame if required */ |
363 |
{ XDIM = atoi(argv[1]); |
if (ARG_SAVEDECOUTPUT) { |
364 |
YDIM = atoi(argv[2]); |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
365 |
if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) ) |
if(write_pgm(filename,out_buffer)) { |
366 |
{ fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM); |
fprintf(stderr, |
367 |
|
"Error writing decoded PGM frame %s\n", |
368 |
|
filename); |
369 |
} |
} |
370 |
} |
} |
|
if (argc>=4 && !strcmp(argv[3],"noasm")) |
|
|
use_assembler = 0; |
|
371 |
|
|
372 |
|
filenr++; |
373 |
|
|
374 |
/* allocate memory */ |
} |
375 |
|
|
376 |
divx_buffer = (unsigned char *) malloc(10*XDIM*YDIM); |
/***************************************************************************** |
377 |
// this should really be enough memory! |
* Calculate totals and averages for output, print results |
378 |
if (!divx_buffer) |
****************************************************************************/ |
|
goto free_all_memory; |
|
|
divx_ptr = divx_buffer+10*XDIM*YDIM; |
|
379 |
|
|
380 |
out_buffer = (unsigned char *) malloc(4*XDIM*YDIM); /* YUV needs less */ |
totalsize /= filenr; |
381 |
if (!out_buffer) |
totaldectime /= filenr; |
|
goto free_all_memory; |
|
382 |
|
|
383 |
|
printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", |
384 |
|
totaldectime, 1000/totaldectime, (int)totalsize); |
385 |
|
|
386 |
/*********************************************************************/ |
/***************************************************************************** |
387 |
/* XviD PART Start */ |
* XviD PART Stop |
388 |
/*********************************************************************/ |
****************************************************************************/ |
389 |
|
|
390 |
status = dec_init(use_assembler); |
release_all: |
391 |
|
if (dec_handle) { |
392 |
|
status = dec_stop(); |
393 |
if (status) |
if (status) |
394 |
{ |
fprintf(stderr, "decore RELEASE problem return value %d\n", status); |
|
printf("Decore INIT problem, return value %d\n", status); |
|
|
goto release_all; |
|
395 |
} |
} |
396 |
|
|
397 |
|
free_all_memory: |
398 |
|
free(out_buffer); |
399 |
|
free(mp4_buffer); |
400 |
|
|
401 |
/*********************************************************************/ |
return 0; |
402 |
/* Main loop */ |
} |
|
/*********************************************************************/ |
|
403 |
|
|
404 |
do |
/***************************************************************************** |
405 |
|
* Usage function |
406 |
|
****************************************************************************/ |
407 |
|
|
408 |
|
static void usage() |
409 |
{ |
{ |
410 |
|
|
411 |
if (divx_ptr > divx_buffer+5*XDIM*YDIM) /* buffer more than half empty */ |
fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n"); |
412 |
{ int rest=(divx_buffer+10*XDIM*YDIM-divx_ptr); |
fprintf(stderr, "Options :\n"); |
413 |
if (rest) |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
414 |
memcpy(divx_buffer, divx_ptr, rest); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
415 |
divx_ptr = divx_buffer; |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
416 |
fread(divx_buffer+rest, 1, 5*XDIM*YDIM, stdin); /* read new data */ |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
417 |
|
fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); |
418 |
|
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
419 |
|
fprintf(stderr, " -m boolean : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n"); |
420 |
|
fprintf(stderr, " -help : This help message\n"); |
421 |
|
fprintf(stderr, " (* means default)\n"); |
422 |
|
|
423 |
} |
} |
424 |
|
|
425 |
dectime = -msecond(); |
/***************************************************************************** |
426 |
status = dec_main(divx_ptr, out_buffer, &m4v_size); |
* "helper" functions |
427 |
|
****************************************************************************/ |
428 |
|
|
429 |
if (status) |
/* return the current time in milli seconds */ |
430 |
|
static double |
431 |
|
msecond() |
432 |
{ |
{ |
433 |
break; |
#ifndef WIN32 |
434 |
|
struct timeval tv; |
435 |
|
gettimeofday(&tv, 0); |
436 |
|
return (double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3; |
437 |
|
#else |
438 |
|
clock_t clk; |
439 |
|
clk = clock(); |
440 |
|
return clk * 1000 / CLOCKS_PER_SEC; |
441 |
|
#endif |
442 |
} |
} |
|
dectime += msecond(); |
|
|
divx_ptr += m4v_size; |
|
443 |
|
|
444 |
totalsize += m4v_size; |
/***************************************************************************** |
445 |
|
* output functions |
446 |
|
****************************************************************************/ |
447 |
|
|
448 |
|
static int |
449 |
|
write_pgm(char *filename, |
450 |
|
unsigned char *image) |
451 |
|
{ |
452 |
|
int loop; |
453 |
|
|
454 |
|
unsigned char *y = image; |
455 |
|
unsigned char *u = image + XDIM*YDIM; |
456 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
457 |
|
|
458 |
printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n", |
FILE *filehandle; |
459 |
filenr, dectime*1000, m4v_size); |
filehandle=fopen(filename,"w+b"); |
460 |
|
if (filehandle) { |
461 |
|
|
462 |
|
/* Write header */ |
463 |
|
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
464 |
|
|
465 |
|
/* Write Y data */ |
466 |
|
fwrite(y, 1, XDIM*YDIM, filehandle); |
467 |
|
|
468 |
if (save_m4v_flag) |
for(loop=0; loop<YDIM/2; loop++) |
469 |
{ |
{ |
470 |
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
/* Write U scanline */ |
471 |
filehandle = fopen(filename, "wb"); |
fwrite(u, 1, XDIM/2, filehandle); |
|
fwrite(divx_buffer, m4v_size, 1, filehandle); |
|
|
fclose(filehandle); |
|
|
} |
|
|
totaldectime += dectime; |
|
472 |
|
|
473 |
|
/* Write V scanline */ |
474 |
|
fwrite(v, 1, XDIM/2, filehandle); |
475 |
|
|
476 |
/*********************************************************************/ |
/* Update pointers */ |
477 |
/* analyse the decoded frame and compare to original */ |
u += XDIM/2; |
478 |
/*********************************************************************/ |
v += XDIM/2; |
479 |
|
|
|
if (save_dec_flag) |
|
|
{ |
|
|
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
|
|
write_pgm(filename,out_buffer); |
|
480 |
} |
} |
481 |
|
|
482 |
filenr++; |
/* Close file */ |
483 |
|
fclose(filehandle); |
484 |
|
|
485 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR) ); |
return 0; |
486 |
|
} |
487 |
|
else |
488 |
|
return 1; |
489 |
|
} |
490 |
|
|
491 |
|
/***************************************************************************** |
492 |
|
* Routines for decoding: init decoder, use, and stop decoder |
493 |
|
****************************************************************************/ |
494 |
|
|
495 |
|
/* init decoder before first run */ |
496 |
|
static int |
497 |
|
dec_init(int use_assembler) |
498 |
|
{ |
499 |
|
int xerr; |
500 |
|
|
501 |
/*********************************************************************/ |
XVID_INIT_PARAM xinit; |
502 |
/* calculate totals and averages for output, print results */ |
XVID_DEC_PARAM xparam; |
|
/*********************************************************************/ |
|
503 |
|
|
504 |
totalsize /= filenr; |
if(use_assembler) |
505 |
totaldectime /= filenr; |
#ifdef ARCH_IS_IA64 |
506 |
|
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
507 |
|
#else |
508 |
|
xinit.cpu_flags = 0; |
509 |
|
#endif |
510 |
|
else |
511 |
|
xinit.cpu_flags = XVID_CPU_FORCE; |
512 |
|
|
513 |
fprintf(stderr,"Avg: dectime %5.2f ms, %5.2f fps, filesize =%d\n", |
xvid_init(NULL, 0, &xinit, NULL); |
514 |
1000*totaldectime, 1./totaldectime, totalsize); |
xparam.width = XDIM; |
515 |
|
xparam.height = YDIM; |
516 |
|
|
517 |
/*********************************************************************/ |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
|
/* XviD PART Stop */ |
|
|
/*********************************************************************/ |
|
518 |
|
|
519 |
release_all: |
dec_handle = xparam.handle; |
520 |
|
|
521 |
if (dec_handle) |
return xerr; |
522 |
|
} |
523 |
|
|
524 |
|
/* decode one frame */ |
525 |
|
static int |
526 |
|
dec_main(unsigned char *istream, |
527 |
|
unsigned char *ostream, |
528 |
|
int istream_size, |
529 |
|
int *ostream_size, |
530 |
|
int *notification) |
531 |
{ |
{ |
532 |
status = dec_stop(); |
|
533 |
if (status) |
int xerr; |
534 |
printf("Decore RELEASE problem return value %d\n", status); |
XVID_DEC_FRAME xframe; |
535 |
|
XVID_DEC_STATS xstats; |
536 |
|
|
537 |
|
xframe.general = 0; |
538 |
|
xframe.bitstream = istream; |
539 |
|
xframe.length = istream_size; |
540 |
|
xframe.image = ostream; |
541 |
|
xframe.stride = XDIM; |
542 |
|
xframe.colorspace = XVID_CSP_YV12; |
543 |
|
|
544 |
|
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats); |
545 |
|
|
546 |
|
*ostream_size = xframe.length; |
547 |
|
|
548 |
|
*notification = xstats.notify; |
549 |
|
|
550 |
|
return xerr; |
551 |
} |
} |
552 |
|
|
553 |
free_all_memory: |
/* close decoder to release resources */ |
554 |
free(out_buffer); |
static int |
555 |
free(divx_buffer); |
dec_stop() |
556 |
|
{ |
557 |
|
int xerr; |
558 |
|
|
559 |
return 0; |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
560 |
|
|
561 |
|
return xerr; |
562 |
} |
} |