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

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