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