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

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