[svn] / branches / dev-api-4 / xvidcore / examples / xvid_encraw.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/examples/xvid_encraw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 991 - (view) (download)

1 : edgomez 558 /*****************************************************************************
2 : chl 376 *
3 : edgomez 558 * XVID MPEG-4 VIDEO CODEC
4 :     * - Console based test application -
5 : chl 376 *
6 : edgomez 851 * Copyright(C) 2002-2003 Christoph Lampert
7 : chl 376 *
8 : edgomez 558 * 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
10 :     * the Free Software Foundation; either version 2 of the License, or
11 :     * (at your option) any later version.
12 : chl 376 *
13 : edgomez 558 * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 : chl 376 *
18 : edgomez 558 * You should have received a copy of the GNU General Public License
19 :     * along with this program; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 : chl 991 * $Id: xvid_encraw.c,v 1.11.2.18 2003-04-19 11:26:47 chl Exp $
23 : edgomez 558 *
24 :     ****************************************************************************/
25 : chl 376
26 : edgomez 558 /*****************************************************************************
27 :     * Application notes :
28 : chl 376 *
29 : edgomez 918 * A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded
30 :     * The speed is measured and frames' PSNR are taken from core.
31 : chl 376 *
32 :     * The program is plain C and needs no libraries except for libxvidcore,
33 : edgomez 558 * and maths-lib.
34 : chl 376 *
35 :     ************************************************************************/
36 :    
37 :     #include <stdio.h>
38 :     #include <stdlib.h>
39 : edgomez 566 #include <string.h>
40 : edgomez 558 #include <math.h>
41 : edgomez 851 #ifndef WIN32
42 : edgomez 558 #include <sys/time.h>
43 :     #else
44 :     #include <time.h>
45 :     #endif
46 : chl 376
47 : edgomez 558 #include "xvid.h"
48 : chl 376
49 : suxen_drol 920
50 : edgomez 558 /*****************************************************************************
51 :     * Quality presets
52 :     ****************************************************************************/
53 : chl 376
54 : edgomez 909 static xvid_motion_t const motion_presets[] = {
55 :     0,
56 : edgomez 949 XVID_ME_HALFPELREFINE16,
57 :     XVID_ME_HALFPELREFINE16,
58 :     XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8,
59 :     XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH16 |
60 :     XVID_ME_USESQUARES16,
61 :     XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH16 |
62 :     XVID_ME_USESQUARES16 | XVID_ME_CHROMA16 | XVID_ME_CHROMA8,
63 : edgomez 558 };
64 :    
65 : edgomez 909 static xvid_vol_t const vol_presets[] = {
66 : edgomez 949 XVID_VOL_MPEGQUANT,
67 : edgomez 909 0,
68 :     0,
69 : edgomez 949 XVID_VOL_QUARTERPEL,
70 :     XVID_VOL_QUARTERPEL | XVID_VOL_GMC,
71 : edgomez 932 0
72 : edgomez 558 };
73 : chl 376
74 : edgomez 909 static xvid_vop_t const vop_presets[] = {
75 : edgomez 949 XVID_VOP_DYNAMIC_BFRAMES,
76 :     XVID_VOP_DYNAMIC_BFRAMES,
77 :     XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL,
78 :     XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_INTER4V,
79 :     XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_HQACPRED,
80 :     XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | XVID_VOP_HQACPRED |
81 :     XVID_VOP_MODEDECISION_BITS
82 : edgomez 909 };
83 :    
84 : edgomez 558 /*****************************************************************************
85 :     * Command line global variables
86 :     ****************************************************************************/
87 : chl 376
88 : edgomez 558 /* Maximum number of frames to encode */
89 :     #define ABS_MAXFRAMENR 9999
90 : chl 376
91 : edgomez 932 static int ARG_STATS = 0;
92 :     static int ARG_DUMP = 0;
93 :     static int ARG_LUMIMASKING = 0;
94 : suxen_drol 938 static int ARG_BITRATE = 0;
95 : edgomez 944 static char *ARG_PASS1 = 0;
96 :     static char *ARG_PASS2 = 0;
97 : suxen_drol 977 static int ARG_PASS2_BITRATE = 0;
98 : edgomez 944 static float ARG_QUANTI = 0.0f;
99 : edgomez 932 static int ARG_QUALITY = 5;
100 : edgomez 558 static float ARG_FRAMERATE = 25.00f;
101 : edgomez 932 static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
102 : edgomez 558 static char *ARG_INPUTFILE = NULL;
103 : edgomez 932 static int ARG_INPUTTYPE = 0;
104 :     static int ARG_SAVEMPEGSTREAM = 0;
105 : edgomez 558 static char *ARG_OUTPUTFILE = NULL;
106 : edgomez 932 static int XDIM = 0;
107 :     static int YDIM = 0;
108 :     static int ARG_BQRATIO = 150;
109 :     static int ARG_BQOFFSET = 100;
110 :     static int ARG_MAXBFRAMES = 0;
111 :     static int ARG_PACKED = 0;
112 :     static int ARG_DEBUG = 0;
113 :    
114 : edgomez 558 #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
115 : chl 376
116 : edgomez 558 #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
117 : edgomez 909 #define SMALL_EPS (1e-10)
118 : chl 376
119 : edgomez 558 #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
120 :     (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) )
121 : chl 376
122 : edgomez 558 /****************************************************************************
123 :     * Nasty global vars ;-)
124 :     ***************************************************************************/
125 : chl 376
126 : suxen_drol 920 static int i;
127 : chl 376
128 : edgomez 558 /* the path where to save output */
129 :     static char filepath[256] = "./";
130 : chl 376
131 : edgomez 558 /* Internal structures (handles) for encoding and decoding */
132 :     static void *enc_handle = NULL;
133 :    
134 :     /*****************************************************************************
135 :     * Local prototypes
136 :     ****************************************************************************/
137 :    
138 :     /* Prints program usage message */
139 :     static void usage();
140 :    
141 :     /* Statistical functions */
142 :     static double msecond();
143 :    
144 :     /* PGM related functions */
145 : edgomez 932 static int read_pgmheader(FILE * handle);
146 :     static int read_pgmdata(FILE * handle,
147 :     unsigned char *image);
148 :     static int read_yuvdata(FILE * handle,
149 :     unsigned char *image);
150 : edgomez 558
151 :     /* Encoder related functions */
152 :     static int enc_init(int use_assembler);
153 :     static int enc_stop();
154 : edgomez 932 static int enc_main(unsigned char *image,
155 :     unsigned char *bitstream,
156 :     int *key,
157 : suxen_drol 919 int *stats_type,
158 : edgomez 932 int *stats_quant,
159 :     int *stats_length,
160 : edgomez 913 int stats[3]);
161 : edgomez 558
162 :     /*****************************************************************************
163 :     * Main function
164 :     ****************************************************************************/
165 :    
166 : edgomez 932 int
167 :     main(int argc,
168 :     char *argv[])
169 : edgomez 558 {
170 :    
171 :     unsigned char *mp4_buffer = NULL;
172 :     unsigned char *in_buffer = NULL;
173 :     unsigned char *out_buffer = NULL;
174 :    
175 :     double enctime;
176 : edgomez 932 double totalenctime = 0.;
177 : chl 991 float totalPSNR[3] = {0., 0., 0.};
178 : edgomez 932
179 : suxen_drol 919 int totalsize;
180 : suxen_drol 920 int result;
181 : suxen_drol 919 int m4v_size;
182 : edgomez 932 int key;
183 : suxen_drol 919 int stats_type;
184 : edgomez 932 int stats_quant;
185 :     int stats_length;
186 :     int use_assembler = 0;
187 : suxen_drol 920
188 : edgomez 932 int input_num;
189 :     int output_num;
190 :    
191 : edgomez 558 char filename[256];
192 : edgomez 932
193 : edgomez 558 FILE *in_file = stdin;
194 :     FILE *out_file = NULL;
195 :    
196 : edgomez 741 printf("xvid_encraw - raw mpeg4 bitstream encoder ");
197 : edgomez 851 printf("written by Christoph Lampert 2002-2003\n\n");
198 : edgomez 558
199 :     /*****************************************************************************
200 :     * Command line parsing
201 :     ****************************************************************************/
202 :    
203 : edgomez 932 for (i = 1; i < argc; i++) {
204 :    
205 :     if (strcmp("-asm", argv[i]) == 0) {
206 : edgomez 558 use_assembler = 1;
207 : edgomez 932 } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {
208 : edgomez 558 i++;
209 :     XDIM = atoi(argv[i]);
210 : edgomez 932 } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
211 : edgomez 558 i++;
212 :     YDIM = atoi(argv[i]);
213 : edgomez 932 } else if (strcmp("-bitrate", argv[i]) == 0 && i < argc - 1) {
214 : edgomez 558 i++;
215 :     ARG_BITRATE = atoi(argv[i]);
216 : suxen_drol 938 } else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) {
217 :     i++;
218 :     ARG_PASS1 = argv[i];
219 : suxen_drol 942 } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 2) {
220 :     i++;
221 : suxen_drol 977 ARG_PASS2 = argv[i];
222 : edgomez 944 i++;
223 : suxen_drol 977 ARG_PASS2_BITRATE = atoi(argv[i]);
224 : edgomez 932 } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) {
225 : edgomez 851 i++;
226 :     ARG_MAXBFRAMES = atoi(argv[i]);
227 : edgomez 932 } else if (strcmp("-packed", argv[i]) == 0) {
228 :     ARG_PACKED = 1;
229 :     } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
230 : edgomez 851 i++;
231 :     ARG_BQRATIO = atoi(argv[i]);
232 : edgomez 932 } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) {
233 : edgomez 851 i++;
234 :     ARG_BQOFFSET = atoi(argv[i]);
235 : edgomez 932 } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {
236 : edgomez 558 i++;
237 :     ARG_QUALITY = atoi(argv[i]);
238 : edgomez 932 } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
239 : edgomez 558 i++;
240 : edgomez 932 ARG_FRAMERATE = (float) atof(argv[i]);
241 :     } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {
242 : edgomez 558 i++;
243 :     ARG_INPUTFILE = argv[i];
244 : edgomez 932 } else if (strcmp("-stats", argv[i]) == 0) {
245 : edgomez 916 ARG_STATS = 1;
246 : edgomez 932 } else if (strcmp("-dump", argv[i]) == 0) {
247 : suxen_drol 920 ARG_DUMP = 1;
248 : edgomez 932 } else if (strcmp("-lumimasking", argv[i]) == 0) {
249 : suxen_drol 926 ARG_LUMIMASKING = 1;
250 : edgomez 932 } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
251 : edgomez 558 i++;
252 :     ARG_INPUTTYPE = atoi(argv[i]);
253 : edgomez 932 } else if (strcmp("-nframes", argv[i]) == 0 && i < argc - 1) {
254 : edgomez 558 i++;
255 : edgomez 932 ARG_MAXFRAMENR = atoi(argv[i]);
256 :     } else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1) {
257 : edgomez 558 i++;
258 : edgomez 944 ARG_QUANTI = (float) atof(argv[i]);
259 : edgomez 932 } else if (strcmp("-save", argv[i]) == 0) {
260 : edgomez 916 ARG_SAVEMPEGSTREAM = 1;
261 : edgomez 932 } else if (strcmp("-debug", argv[i]) == 0) {
262 :     ARG_DEBUG = 1;
263 :     } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {
264 : edgomez 558 i++;
265 :     ARG_OUTPUTFILE = argv[i];
266 : edgomez 932 } else if (strcmp("-help", argv[i])) {
267 : edgomez 558 usage();
268 : edgomez 932 return (0);
269 :     } else {
270 : edgomez 558 usage();
271 :     exit(-1);
272 :     }
273 : edgomez 932
274 : edgomez 558 }
275 : edgomez 932
276 : edgomez 558 /*****************************************************************************
277 :     * Arguments checking
278 :     ****************************************************************************/
279 :    
280 : edgomez 932 if (XDIM <= 0 || XDIM >= 2048 || YDIM <= 0 || YDIM >= 2048) {
281 :     fprintf(stderr,
282 :     "Trying to retreive width and height from PGM header\n");
283 :     ARG_INPUTTYPE = 1; /* pgm */
284 : edgomez 558 }
285 : edgomez 932
286 :     if (ARG_QUALITY < 0 || ARG_QUALITY > 5) {
287 :     fprintf(stderr, "Wrong Quality\n");
288 :     return (-1);
289 : edgomez 558 }
290 :    
291 : edgomez 932 if (ARG_FRAMERATE <= 0) {
292 :     fprintf(stderr, "Wrong Framerate %s \n", argv[5]);
293 :     return (-1);
294 : edgomez 558 }
295 :    
296 : edgomez 932 if (ARG_MAXFRAMENR <= 0) {
297 :     fprintf(stderr, "Wrong number of frames\n");
298 :     return (-1);
299 : edgomez 558 }
300 :    
301 : edgomez 932 if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
302 : edgomez 558 in_file = stdin;
303 : edgomez 932 } else {
304 : edgomez 558
305 :     in_file = fopen(ARG_INPUTFILE, "rb");
306 :     if (in_file == NULL) {
307 :     fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
308 : edgomez 932 return (-1);
309 : edgomez 558 }
310 :     }
311 :    
312 :     if (ARG_INPUTTYPE) {
313 :     if (read_pgmheader(in_file)) {
314 : edgomez 932 fprintf(stderr,
315 :     "Wrong input format, I want YUV encapsulated in PGM\n");
316 :     return (-1);
317 : edgomez 558 }
318 :     }
319 :    
320 :     /* now we know the sizes, so allocate memory */
321 : edgomez 932 in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM));
322 : edgomez 558 if (!in_buffer)
323 :     goto free_all_memory;
324 :    
325 :     /* this should really be enough memory ! */
326 : edgomez 932 mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);
327 : edgomez 558 if (!mp4_buffer)
328 : edgomez 932 goto free_all_memory;
329 : edgomez 558
330 :     /*****************************************************************************
331 :     * XviD PART Start
332 :     ****************************************************************************/
333 :    
334 :    
335 : suxen_drol 920 result = enc_init(use_assembler);
336 : edgomez 932 if (result) {
337 : suxen_drol 920 fprintf(stderr, "Encore INIT problem, return value %d\n", result);
338 : edgomez 558 goto release_all;
339 :     }
340 :    
341 :     /*****************************************************************************
342 :     * Main loop
343 :     ****************************************************************************/
344 :    
345 : edgomez 851 if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) {
346 : edgomez 561
347 : edgomez 932 if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
348 : edgomez 558 fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
349 :     goto release_all;
350 :     }
351 :    
352 : edgomez 932 } else {
353 : edgomez 558 out_file = NULL;
354 :     }
355 :    
356 :     /*****************************************************************************
357 :     * Encoding loop
358 :     ****************************************************************************/
359 :    
360 :     totalsize = 0;
361 :    
362 : edgomez 932 result = 0;
363 : suxen_drol 920
364 : edgomez 932 input_num = 0; /* input frame counter */
365 :     output_num = 0; /* output frame counter */
366 : suxen_drol 920
367 : edgomez 558 do {
368 :    
369 : edgomez 913 char *type;
370 : edgomez 932 int sse[3];
371 : edgomez 913
372 : edgomez 932 if (input_num >= ARG_MAXFRAMENR) {
373 :     result = 1;
374 :     }
375 : edgomez 558
376 : edgomez 932 if (!result) {
377 :     if (ARG_INPUTTYPE) {
378 :     /* read PGM data (YUV-format) */
379 :     result = read_pgmdata(in_file, in_buffer);
380 :     } else {
381 :     /* read raw data (YUV-format) */
382 :     result = read_yuvdata(in_file, in_buffer);
383 :     }
384 :     }
385 : suxen_drol 920
386 : edgomez 558 /*****************************************************************************
387 :     * Encode and decode this frame
388 :     ****************************************************************************/
389 :    
390 :     enctime = msecond();
391 : edgomez 932 m4v_size =
392 :     enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
393 :     &stats_quant, &stats_length, sse);
394 : edgomez 558 enctime = msecond() - enctime;
395 :    
396 : edgomez 913 /* Write the Frame statistics */
397 : edgomez 932
398 : chl 990 printf("%5d: key=%i, time= %6.0f, length= %7d", !result ? input_num : -1,
399 : edgomez 944 key, (float) enctime, (int) m4v_size);
400 : edgomez 851
401 : edgomez 932 if (stats_type > 0) { /* !XVID_TYPE_NOTHING */
402 : suxen_drol 919
403 : edgomez 932 switch (stats_type) {
404 :     case XVID_TYPE_IVOP:
405 :     type = "I";
406 :     break;
407 :     case XVID_TYPE_PVOP:
408 :     type = "P";
409 :     break;
410 :     case XVID_TYPE_BVOP:
411 :     type = "B";
412 :     break;
413 :     case XVID_TYPE_SVOP:
414 :     type = "S";
415 :     break;
416 :     default:
417 :     type = "U";
418 :     break;
419 :     }
420 : suxen_drol 919
421 : chl 990 printf(" | type=%s, quant= %2d, length= %7d", type, stats_quant,
422 : edgomez 932 stats_length);
423 : suxen_drol 923
424 : edgomez 932 #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
425 : suxen_drol 923
426 : edgomez 932 if (ARG_STATS) {
427 :     printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
428 : edgomez 944 SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2,
429 :     YDIM / 2),
430 :     SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));
431 : chl 991
432 :     totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);
433 :     totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);
434 :     totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);
435 : edgomez 932 }
436 :    
437 : edgomez 916 }
438 : edgomez 932 #undef SSE2PSNR
439 :    
440 : edgomez 916 printf("\n");
441 :    
442 : edgomez 932 if (m4v_size < 0) {
443 :     break;
444 :     }
445 : suxen_drol 919
446 : edgomez 851 /* Update encoding time stats */
447 : edgomez 558 totalenctime += enctime;
448 :     totalsize += m4v_size;
449 :    
450 : edgomez 728 /*****************************************************************************
451 :     * Save stream to file
452 :     ****************************************************************************/
453 :    
454 : edgomez 932 if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {
455 : edgomez 558 /* Save single files */
456 :     if (out_file == NULL) {
457 : suxen_drol 920 sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
458 : edgomez 558 out_file = fopen(filename, "wb");
459 :     fwrite(mp4_buffer, m4v_size, 1, out_file);
460 :     fclose(out_file);
461 :     out_file = NULL;
462 : edgomez 932 output_num++;
463 :     } else {
464 : edgomez 558
465 :     /* Write mp4 data */
466 : edgomez 851 fwrite(mp4_buffer, 1, m4v_size, out_file);
467 : edgomez 558
468 :     }
469 :     }
470 :    
471 : suxen_drol 920 input_num++;
472 : edgomez 918
473 : edgomez 932 /* Read the header if it's pgm stream */
474 :     if (!result && ARG_INPUTTYPE)
475 : suxen_drol 920 result = read_pgmheader(in_file);
476 : edgomez 558
477 : suxen_drol 920 } while (1);
478 : edgomez 558
479 : edgomez 932
480 :    
481 : edgomez 558 /*****************************************************************************
482 :     * Calculate totals and averages for output, print results
483 :     ****************************************************************************/
484 : chl 376
485 : suxen_drol 926 printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n",
486 : edgomez 932 totalenctime, (int) totalsize);
487 : suxen_drol 926
488 : edgomez 932 if (input_num > 0) {
489 :     totalsize /= input_num;
490 :     totalenctime /= input_num;
491 : chl 991 totalPSNR[0] /= input_num;
492 :     totalPSNR[1] /= input_num;
493 :     totalPSNR[2] /= input_num;
494 : edgomez 932 } else {
495 :     totalsize = -1;
496 :     totalenctime = -1;
497 :     }
498 : chl 376
499 : chl 991 printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d, ",
500 : edgomez 932 totalenctime, 1000 / totalenctime, (int) totalsize);
501 : chl 991 if (ARG_STATS) {
502 :     printf("psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
503 :     totalPSNR[0],totalPSNR[1],totalPSNR[2]);
504 :     }
505 :     printf("\n");
506 : chl 376
507 : edgomez 913
508 : edgomez 558 /*****************************************************************************
509 :     * XviD PART Stop
510 :     ****************************************************************************/
511 : chl 376
512 : edgomez 932 release_all:
513 : chl 376
514 : edgomez 932 if (enc_handle) {
515 : suxen_drol 920 result = enc_stop();
516 : edgomez 932 if (result)
517 :     fprintf(stderr, "Encore RELEASE problem return value %d\n",
518 :     result);
519 : edgomez 558 }
520 : chl 376
521 : edgomez 932 if (in_file)
522 : edgomez 728 fclose(in_file);
523 : edgomez 932 if (out_file)
524 : edgomez 558 fclose(out_file);
525 :    
526 : edgomez 932 free_all_memory:
527 : edgomez 558 free(out_buffer);
528 :     free(mp4_buffer);
529 :     free(in_buffer);
530 :    
531 : edgomez 932 return (0);
532 : edgomez 558
533 :     }
534 :    
535 :    
536 :     /*****************************************************************************
537 :     * "statistical" functions
538 :     *
539 :     * these are not needed for encoding or decoding, but for measuring
540 :     * time and quality, there in nothing specific to XviD in these
541 :     *
542 :     *****************************************************************************/
543 :    
544 :     /* Return time elapsed time in miliseconds since the program started */
545 : edgomez 932 static double
546 :     msecond()
547 :     {
548 : edgomez 824 #ifndef WIN32
549 : edgomez 932 struct timeval tv;
550 :    
551 : chl 376 gettimeofday(&tv, 0);
552 : edgomez 932 return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);
553 : edgomez 558 #else
554 :     clock_t clk;
555 : edgomez 932
556 : edgomez 558 clk = clock();
557 : edgomez 932 return (clk * 1000 / CLOCKS_PER_SEC);
558 : edgomez 558 #endif
559 : chl 376 }
560 :    
561 : edgomez 558 /*****************************************************************************
562 :     * Usage message
563 :     *****************************************************************************/
564 : chl 376
565 : edgomez 932 static void
566 :     usage()
567 : edgomez 558 {
568 : edgomez 932 fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n");
569 :     fprintf(stderr, "Input options:\n");
570 :     fprintf(stderr, " -i string : input filename (default=stdin)\n");
571 :     fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n");
572 :     fprintf(stderr, " -w integer: frame width ([1.2048])\n");
573 :     fprintf(stderr, " -h integer: frame height ([1.2048])\n");
574 :     fprintf(stderr, " -nframes integer: number of frames to encode\n");
575 :     fprintf(stderr, "\n");
576 :     fprintf(stderr, "Output options:\n");
577 :     fprintf(stderr, " -dump : save decoder output\n");
578 :     fprintf(stderr, " -save : save mpeg4 raw stream\n");
579 : edgomez 944 fprintf(stderr, " -o string: output filename\n");
580 : edgomez 932 fprintf(stderr, "\n");
581 : edgomez 944 fprintf(stderr, "BFrames options:\n");
582 : edgomez 932 fprintf(stderr, " -max_bframes integer: max bframes (default=0)\n");
583 :     fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (default=150)\n");
584 :     fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (default=100)\n");
585 :     fprintf(stderr, "\n");
586 : edgomez 944 fprintf(stderr, "Rate control options:\n");
587 :     fprintf(stderr, " -framerate float : target framerate (>0 | default=25.0)\n");
588 :     fprintf(stderr, " -bitrate integer : bitrate -- for CBR/VBR pass2\n");
589 :     fprintf(stderr, " -quant float : quantizer -- for \"Fixed\" quantizer RC\n");
590 : suxen_drol 977 fprintf(stderr, " -pass1 filename : output stats filename\n");
591 :     fprintf(stderr, " -pass2 filename bitrate : input stats filename, target bitrate\n");
592 : edgomez 944 fprintf(stderr, "\n");
593 : edgomez 932 fprintf(stderr, "Other options\n");
594 :     fprintf(stderr, " -asm : use assembly optmized code\n");
595 :     fprintf(stderr, " -quality integer: quality ([0..5])\n");
596 :     fprintf(stderr, " -packed : packed mode\n");
597 :     fprintf(stderr, " -lumimasking : use lumimasking algorithm\n");
598 :     fprintf(stderr, " -stats : print stats about encoded frames\n");
599 : edgomez 944 fprintf(stderr, " -debug : print all MB dquants\n");
600 : edgomez 932 fprintf(stderr, " -help : prints this help message\n");
601 : edgomez 558 }
602 :    
603 :     /*****************************************************************************
604 :     * Input and output functions
605 :     *
606 :     * the are small and simple routines to read and write PGM and YUV
607 :     * image. It's just for convenience, again nothing specific to XviD
608 :     *
609 :     *****************************************************************************/
610 :    
611 : edgomez 932 static int
612 :     read_pgmheader(FILE * handle)
613 :     {
614 :     int bytes, xsize, ysize, depth;
615 : chl 376 char dummy[2];
616 :    
617 : edgomez 932 bytes = fread(dummy, 1, 2, handle);
618 : edgomez 558
619 : edgomez 932 if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
620 :     return (1);
621 :    
622 :     fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
623 :     if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {
624 :     fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
625 :     return (2);
626 : chl 376 }
627 : edgomez 932 if ((XDIM == 0) || (YDIM == 0)) {
628 :     XDIM = xsize;
629 :     YDIM = ysize * 2 / 3;
630 : chl 376 }
631 :    
632 : edgomez 932 return (0);
633 : chl 376 }
634 :    
635 : edgomez 932 static int
636 :     read_pgmdata(FILE * handle,
637 :     unsigned char *image)
638 :     {
639 : edgomez 558 int i;
640 : chl 376 char dummy;
641 : edgomez 932
642 : edgomez 558 unsigned char *y = image;
643 : edgomez 932 unsigned char *u = image + XDIM * YDIM;
644 :     unsigned char *v = image + XDIM * YDIM + XDIM / 2 * YDIM / 2;
645 : chl 376
646 : edgomez 558 /* read Y component of picture */
647 : edgomez 932 fread(y, 1, XDIM * YDIM, handle);
648 :    
649 :     for (i = 0; i < YDIM / 2; i++) {
650 : edgomez 558 /* read U */
651 : edgomez 932 fread(u, 1, XDIM / 2, handle);
652 : edgomez 558
653 :     /* read V */
654 : edgomez 932 fread(v, 1, XDIM / 2, handle);
655 : edgomez 558
656 :     /* Update pointers */
657 : edgomez 932 u += XDIM / 2;
658 :     v += XDIM / 2;
659 : chl 376 }
660 : edgomez 558
661 : edgomez 932 /* I don't know why, but this seems needed */
662 : edgomez 558 fread(&dummy, 1, 1, handle);
663 :    
664 : edgomez 932 return (0);
665 : chl 376 }
666 :    
667 : edgomez 932 static int
668 :     read_yuvdata(FILE * handle,
669 :     unsigned char *image)
670 : edgomez 558 {
671 : edgomez 932
672 :     if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) !=
673 :     (unsigned int) IMAGE_SIZE(XDIM, YDIM))
674 :     return (1);
675 :     else
676 :     return (0);
677 : chl 376 }
678 :    
679 : edgomez 558 /*****************************************************************************
680 :     * Routines for encoding: init encoder, frame step, release encoder
681 :     ****************************************************************************/
682 : chl 376
683 : suxen_drol 919 /* sample plugin */
684 :    
685 : edgomez 932 int
686 :     rawenc_debug(void *handle,
687 :     int opt,
688 :     void *param1,
689 :     void *param2)
690 : suxen_drol 919 {
691 : edgomez 932 switch (opt) {
692 :     case XVID_PLG_INFO:
693 :     {
694 :     xvid_plg_info_t *info = (xvid_plg_info_t *) param1;
695 : suxen_drol 926
696 : edgomez 932 info->flags = XVID_REQDQUANTS;
697 :     return 0;
698 :     }
699 : suxen_drol 919
700 : edgomez 932 case XVID_PLG_CREATE:
701 :     case XVID_PLG_DESTROY:
702 :     case XVID_PLG_BEFORE:
703 :     return 0;
704 : suxen_drol 926
705 : edgomez 932 case XVID_PLG_AFTER:
706 :     {
707 :     xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
708 :     int i, j;
709 : suxen_drol 919
710 : edgomez 932 printf("---[ frame: %5i quant: %2i length: %6i ]---\n",
711 :     data->frame_num, data->quant, data->length);
712 :     for (j = 0; j < data->mb_height; j++) {
713 :     for (i = 0; i < data->mb_width; i++)
714 :     printf("%2i ", data->dquant[j * data->dquant_stride + i]);
715 :     printf("\n");
716 :     }
717 :    
718 :     return 0;
719 :     }
720 :     }
721 :    
722 :     return XVID_ERR_FAIL;
723 : suxen_drol 919 }
724 :    
725 :    
726 : chl 376 #define FRAMERATE_INCR 1001
727 :    
728 : suxen_drol 938
729 : edgomez 558 /* Initialize encoder for first use, pass all needed parameters to the codec */
730 : edgomez 932 static int
731 :     enc_init(int use_assembler)
732 : edgomez 558 {
733 : chl 376 int xerr;
734 : edgomez 944 xvid_plugin_cbr_t cbr;
735 :     xvid_plugin_2pass1_t rc2pass1;
736 :     xvid_plugin_2pass2_t rc2pass2;
737 :     xvid_plugin_fixed_t rcfixed;
738 :     xvid_enc_plugin_t plugins[7];
739 : edgomez 932 xvid_gbl_init_t xvid_gbl_init;
740 : edgomez 909 xvid_enc_create_t xvid_enc_create;
741 : chl 376
742 : edgomez 909 /*------------------------------------------------------------------------
743 :     * XviD core initialization
744 :     *----------------------------------------------------------------------*/
745 :    
746 : edgomez 932 /* Set version -- version checking will done by xvidcore */
747 :     memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
748 : edgomez 909 xvid_gbl_init.version = XVID_VERSION;
749 : edgomez 932
750 :    
751 : edgomez 909 /* Do we have to enable ASM optimizations ? */
752 : edgomez 932 if (use_assembler) {
753 : chl 376
754 : suxen_drol 860 #ifdef ARCH_IS_IA64
755 : edgomez 909 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
756 : chl 376 #else
757 : edgomez 909 xvid_gbl_init.cpu_flags = 0;
758 : chl 376 #endif
759 : edgomez 932 } else {
760 : edgomez 909 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
761 : edgomez 558 }
762 : chl 376
763 : edgomez 909 /* Initialize XviD core -- Should be done once per __process__ */
764 :     xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
765 : chl 376
766 : edgomez 909 /*------------------------------------------------------------------------
767 :     * XviD encoder initialization
768 :     *----------------------------------------------------------------------*/
769 :    
770 :     /* Version again */
771 : edgomez 932 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
772 : edgomez 909 xvid_enc_create.version = XVID_VERSION;
773 :    
774 :     /* Width and Height of input frames */
775 :     xvid_enc_create.width = XDIM;
776 :     xvid_enc_create.height = YDIM;
777 :    
778 : edgomez 932 /* init plugins */
779 : suxen_drol 919
780 : edgomez 932 xvid_enc_create.plugins = plugins;
781 :     xvid_enc_create.num_plugins = 0;
782 : suxen_drol 926
783 : edgomez 944 if (ARG_BITRATE) {
784 : edgomez 988 memset(&cbr, 0, sizeof(xvid_plugin_cbr_t));
785 : edgomez 944 cbr.version = XVID_VERSION;
786 :     cbr.bitrate = ARG_BITRATE;
787 : suxen_drol 938
788 : edgomez 944 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_cbr;
789 : suxen_drol 938 plugins[xvid_enc_create.num_plugins].param = &cbr;
790 :     xvid_enc_create.num_plugins++;
791 : edgomez 944 }
792 : suxen_drol 938
793 : edgomez 944 if (ARG_QUANTI) {
794 : edgomez 988 memset(&rcfixed, 0, sizeof(xvid_plugin_fixed_t));
795 : edgomez 944 rcfixed.version = XVID_VERSION;
796 :     /* We will use a 1/10 precision, just to make sure it works */
797 :     rcfixed.quant_base = 10;
798 :     rcfixed.quant_increment = (int) (ARG_QUANTI * 10);
799 : suxen_drol 942
800 : edgomez 944 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_fixed;
801 :     plugins[xvid_enc_create.num_plugins].param = &rcfixed;
802 :     xvid_enc_create.num_plugins++;
803 :     }
804 :    
805 : suxen_drol 977 if (ARG_PASS2) {
806 : edgomez 988 memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
807 : edgomez 944 rc2pass2.version = XVID_VERSION;
808 : suxen_drol 977 rc2pass2.filename = ARG_PASS2;
809 :     rc2pass2.bitrate = ARG_PASS2_BITRATE;
810 : edgomez 944
811 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
812 : suxen_drol 942 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
813 :     xvid_enc_create.num_plugins++;
814 : suxen_drol 977 }
815 :    
816 :     if (ARG_PASS1) {
817 : edgomez 988 memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
818 : edgomez 944 rc2pass1.version = XVID_VERSION;
819 :     rc2pass1.filename = ARG_PASS1;
820 : suxen_drol 938
821 : edgomez 944 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
822 : suxen_drol 938 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
823 :     xvid_enc_create.num_plugins++;
824 : edgomez 944 }
825 : suxen_drol 938
826 : edgomez 944 if (ARG_LUMIMASKING) {
827 : edgomez 932 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
828 :     plugins[xvid_enc_create.num_plugins].param = NULL;
829 :     xvid_enc_create.num_plugins++;
830 :     }
831 : suxen_drol 920
832 : edgomez 932 if (ARG_DUMP) {
833 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
834 :     plugins[xvid_enc_create.num_plugins].param = NULL;
835 :     xvid_enc_create.num_plugins++;
836 :     }
837 : suxen_drol 926
838 : edgomez 932 if (ARG_DEBUG) {
839 :     plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
840 :     plugins[xvid_enc_create.num_plugins].param = NULL;
841 :     xvid_enc_create.num_plugins++;
842 :     }
843 :    
844 : edgomez 909 /* No fancy thread tests */
845 :     xvid_enc_create.num_threads = 0;
846 :    
847 :     /* Frame rate - Do some quick float fps = fincr/fbase hack */
848 : edgomez 932 if ((ARG_FRAMERATE - (int) ARG_FRAMERATE) < SMALL_EPS) {
849 : edgomez 909 xvid_enc_create.fincr = 1;
850 : edgomez 932 xvid_enc_create.fbase = (int) ARG_FRAMERATE;
851 : edgomez 909 } else {
852 :     xvid_enc_create.fincr = FRAMERATE_INCR;
853 : edgomez 932 xvid_enc_create.fbase = (int) (FRAMERATE_INCR * ARG_FRAMERATE);
854 : chl 376 }
855 :    
856 : edgomez 909 /* Maximum key frame interval */
857 : edgomez 932 xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;
858 : edgomez 909
859 :     /* Bframes settings */
860 :     xvid_enc_create.max_bframes = ARG_MAXBFRAMES;
861 :     xvid_enc_create.bquant_ratio = ARG_BQRATIO;
862 : edgomez 932 xvid_enc_create.bquant_offset = ARG_BQOFFSET;
863 : edgomez 909
864 :     /* Dropping ratio frame -- we don't need that */
865 :     xvid_enc_create.frame_drop_ratio = 0;
866 :    
867 :     /* Global encoder options */
868 : suxen_drol 919 xvid_enc_create.global = 0;
869 : edgomez 909
870 : edgomez 944 if (ARG_PACKED)
871 : edgomez 949 xvid_enc_create.global |=XVID_GLOBAL_PACKED;
872 : edgomez 944
873 :     if (ARG_STATS)
874 : edgomez 949 xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE;
875 : edgomez 944
876 : edgomez 558 /* I use a small value here, since will not encode whole movies, but short clips */
877 : edgomez 909 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
878 : chl 376
879 : edgomez 909 /* Retrieve the encoder instance from the structure */
880 :     enc_handle = xvid_enc_create.handle;
881 : chl 376
882 : edgomez 932 return (xerr);
883 : chl 376 }
884 :    
885 : edgomez 932 static int
886 :     enc_stop()
887 : edgomez 558 {
888 :     int xerr;
889 : chl 376
890 : edgomez 909 /* Destroy the encoder instance */
891 : chl 376 xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
892 : edgomez 558
893 : edgomez 932 return (xerr);
894 : chl 376 }
895 :    
896 : edgomez 932 static int
897 :     enc_main(unsigned char *image,
898 :     unsigned char *bitstream,
899 :     int *key,
900 :     int *stats_type,
901 :     int *stats_quant,
902 :     int *stats_length,
903 :     int sse[3])
904 : edgomez 558 {
905 : edgomez 909 int ret;
906 : chl 376
907 : edgomez 909 xvid_enc_frame_t xvid_enc_frame;
908 : suxen_drol 919 xvid_enc_stats_t xvid_enc_stats;
909 : chl 376
910 : edgomez 909 /* Version for the frame and the stats */
911 : edgomez 932 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
912 : edgomez 909 xvid_enc_frame.version = XVID_VERSION;
913 : edgomez 932
914 :     memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
915 : suxen_drol 919 xvid_enc_stats.version = XVID_VERSION;
916 : edgomez 932
917 : edgomez 909 /* Bind output buffer */
918 :     xvid_enc_frame.bitstream = bitstream;
919 :     xvid_enc_frame.length = -1;
920 : chl 376
921 : edgomez 909 /* Initialize input image fields */
922 : edgomez 932 if (image) {
923 :     xvid_enc_frame.input.plane[0] = image;
924 :     xvid_enc_frame.input.csp = XVID_CSP_I420;
925 :     xvid_enc_frame.input.stride[0] = XDIM;
926 :     } else {
927 :     xvid_enc_frame.input.csp = XVID_CSP_NULL;
928 :     }
929 : chl 376
930 : edgomez 909 /* Set up core's general features */
931 :     xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY];
932 : edgomez 932 if (ARG_STATS)
933 : edgomez 949 xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;
934 : chl 376
935 : edgomez 909 /* Set up core's general features */
936 :     xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
937 : edgomez 728
938 : edgomez 909 /* Frame type -- let core decide for us */
939 : edgomez 932 xvid_enc_frame.type = XVID_TYPE_AUTO;
940 : edgomez 728
941 : edgomez 944 /* Force the right quantizer -- It is internally managed by RC plugins */
942 :     xvid_enc_frame.quant = 0;
943 : edgomez 932
944 : edgomez 909 /* Set up motion estimation flags */
945 :     xvid_enc_frame.motion = motion_presets[ARG_QUALITY];
946 : edgomez 728
947 : edgomez 909 /* We don't use special matrices */
948 :     xvid_enc_frame.quant_intra_matrix = NULL;
949 :     xvid_enc_frame.quant_inter_matrix = NULL;
950 : chl 376
951 : edgomez 913 /* Encode the frame */
952 : edgomez 932 ret =
953 :     xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
954 :     &xvid_enc_stats);
955 : edgomez 728
956 : edgomez 932 *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
957 : suxen_drol 919 *stats_type = xvid_enc_stats.type;
958 : edgomez 932 *stats_quant = xvid_enc_stats.quant;
959 :     *stats_length = xvid_enc_stats.length;
960 :     sse[0] = xvid_enc_stats.sse_y;
961 :     sse[1] = xvid_enc_stats.sse_u;
962 :     sse[2] = xvid_enc_stats.sse_v;
963 : chl 376
964 : edgomez 932 return (ret);
965 : chl 376 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4