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