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