OSDN Git Service

4621f1fee00e252afc99b5f04e2d802d19e959b8
[fpga-leon-mjpeg/leon-mjpeg.git] / snapgear-2.6-p42 / user / jpeg-6b / dmjpeg.c
1 /* dmjpeg.c
2  *
3  * This software is modified from djpeg.c
4  *    by Kenichi Kurimoto
5  */
6 /*
7  * djpeg.c
8  *
9  * Copyright (C) 1991-1997, Thomas G. Lane.
10  * This file is part of the Independent JPEG Group's software.
11  * For conditions of distribution and use, see the accompanying README file.
12  *
13  * This file contains a command-line user interface for the JPEG decompressor.
14  * It should work on any system with Unix- or MS-DOS-style command lines.
15  *
16  * Two different command line styles are permitted, depending on the
17  * compile-time switch TWO_FILE_COMMANDLINE:
18  *      djpeg [options]  inputfile outputfile
19  *      djpeg [options]  [inputfile]
20  * In the second style, output is always to standard output, which you'd
21  * normally redirect to a file or pipe to some other program.  Input is
22  * either from a named file or from standard input (typically redirected).
23  * The second style is convenient on Unix but is unhelpful on systems that
24  * don't support pipes.  Also, you MUST use the first style if your system
25  * doesn't do binary I/O to stdin/stdout.
26  * To simplify script writing, the "-outfile" switch is provided.  The syntax
27  *      djpeg [options]  -outfile outputfile  inputfile
28  * works regardless of which command line style is used.
29  */
30
31 #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
32 #include "jversion.h"           /* for version message */
33
34 #include <ctype.h>              /* to declare isprint() */
35
36 #ifdef USE_CCOMMAND             /* command-line reader for Macintosh */
37 #ifdef __MWERKS__
38 #include <SIOUX.h>              /* Metrowerks needs this */
39 #include <console.h>            /* ... and this */
40 #endif
41 #ifdef THINK_C
42 #include <console.h>            /* Think declares it here */
43 #endif
44 #endif
45
46 #define DEBUG
47
48 /* Create the add-on message string table. */
49
50 #define JMESSAGE(code,string)   string ,
51
52 static const char * const cdjpeg_message_table[] = {
53 #include "cderror.h"
54   NULL
55 };
56
57 /* Add for LEON MJPEG project */
58 #include <fcntl.h>
59 #include <linux/fb.h>
60 #include <linux/fs.h>
61 #include <sys/mman.h>
62 #include <sys/ioctl.h>
63 #include <sys/time.h>
64 #include "kmjpeg.h"
65 static int f_num=0;
66 static int markflag=0;
67
68 /*
69  * This list defines the known output image formats
70  * (not all of which need be supported by a given version).
71  * You can change the default output format by defining DEFAULT_FMT;
72  * indeed, you had better do so if you undefine PPM_SUPPORTED.
73  */
74
75 typedef enum {
76         FMT_BMP,                /* BMP format (Windows flavor) */
77         FMT_GIF,                /* GIF format */
78         FMT_OS2,                /* BMP format (OS/2 flavor) */
79         FMT_PPM,                /* PPM/PGM (PBMPLUS formats) */
80         FMT_RLE,                /* RLE format */
81         FMT_TARGA,              /* Targa format */
82         FMT_TIFF                /* TIFF format */
83 } IMAGE_FORMATS;
84
85 #ifndef DEFAULT_FMT             /* so can override from CFLAGS in Makefile */
86 #define DEFAULT_FMT     FMT_PPM
87 #endif
88
89 static IMAGE_FORMATS requested_fmt;
90
91
92 /*
93  * Argument-parsing code.
94  * The switch parser is designed to be useful with DOS-style command line
95  * syntax, ie, intermixed switches and file names, where only the switches
96  * to the left of a given file name affect processing of that file.
97  * The main program in this file doesn't actually use this capability...
98  */
99
100
101 static const char * progname;   /* program name for error messages */
102 static char * outfilename;      /* for -outfile switch */
103
104 //#define MAKESTMT(stuff)         do { stuff } while (0)
105
106 #define INPUT_VARS(cinfo)  \
107         struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
108         const JOCTET * next_input_byte = datasrc->next_input_byte;  \
109         size_t bytes_in_buffer = datasrc->bytes_in_buffer
110
111 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
112 #define INPUT_RELOAD(cinfo)  \
113         ( next_input_byte = datasrc->next_input_byte,  \
114           bytes_in_buffer = datasrc->bytes_in_buffer )
115
116 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
117  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
118  * but we must reload the local copies after a successful fill.
119  */
120 #define MAKE_BYTE_AVAIL(cinfo,action)  \
121         if (bytes_in_buffer == 0) {  \
122           if (! (*datasrc->fill_input_buffer) (cinfo))  \
123             { action; }  \
124           INPUT_RELOAD(cinfo);  \
125          }
126 #define INPUT_4BYTES(cinfo, V, action) \
127   MAKESTMT(MAKE_BYTE_AVAIL(cinfo,action);       \
128            bytes_in_buffer--;                                           \
129            V = ((unsigned int) GETJOCTET(*next_input_byte++))<<24;      \
130            MAKE_BYTE_AVAIL(cinfo,action);                               \
131            bytes_in_buffer--;                                           \
132            V += ((unsigned int)GETJOCTET(*next_input_byte++))<<16;      \
133            MAKE_BYTE_AVAIL(cinfo,action);                               \
134            bytes_in_buffer--;                                           \
135            V += ((unsigned int)GETJOCTET(*next_input_byte++))<<8;       \
136            MAKE_BYTE_AVAIL(cinfo,action);                               \
137            bytes_in_buffer--;                                           \
138            V += ((unsigned int)GETJOCTET(*next_input_byte++)) ; )
139
140
141 /*
142  * Add for LEON-mjpeg project
143  */
144 void setup_hardware(j_decompress_ptr cinfo)
145 {
146   //  int xres,yres,vbpp,line_len,yfac;
147   long int screensize;
148   unsigned int  xmcumax,ymcumax,incaddmcuy,size, incaddy, incadd;
149   int i, j, samp_f, incaddmcux;
150   jpeg_component_info *compptr;
151   int h_samp[3];
152   int v_samp[3];
153   int qtbl_no[3];
154   JQUANT_TBL *qtbl;
155   struct ioctl_cmdreg regset;
156   struct ioctl_cmdq ioctl_dataq;
157   struct ioctl_offdata offdata;
158   struct ioctl_cmdd datad;
159   unsigned int toregister, cadd;
160
161   /** V1 framebuffer & motionJPEG support **/
162   if(!(cinfo->fd_framebuffer = open( "/dev/fb0", O_RDWR))){
163     fprintf(stderr,"Framebuffer open error!\n");
164     exit(1);
165   }
166 #ifdef DEBUG
167   fprintf(stderr,"Framebuffer was opened successfully\n");
168 #endif
169
170   if(ioctl(cinfo->fd_framebuffer, FBIOGET_FSCREENINFO, &(cinfo->fb_finfo))){
171     fprintf(stderr,"Framebuffer fixed information get error\n");
172     exit(1);
173   }
174   if(ioctl(cinfo->fd_framebuffer, FBIOGET_VSCREENINFO, &(cinfo->fb_vinfo))){
175     fprintf(stderr, "Framebuffer variable information get error\n");
176     exit(1);
177   }
178   //#ifdef DEBUG
179   fprintf(stderr, "Framebuffer xsize:%d, ysize:%d, vbpp:%d\n", 
180           cinfo->fb_vinfo.xres, cinfo->fb_vinfo.yres, cinfo->fb_vinfo.bits_per_pixel);
181   //#endif
182   if(cinfo->fb_vinfo.xres % 16 != 0 || cinfo->fb_vinfo.yres % 16 != 0){
183     fprintf(stderr, "This system support only multiple of 16 X,Y size Framebuffer.\n");
184     exit(1);  
185   }
186   if(cinfo->fb_vinfo.bits_per_pixel != 16){
187     fprintf(stderr, "This system support only 16 bits per pixel Framebuffer\n");
188     exit(1);  
189   }
190   screensize = cinfo->fb_vinfo.xres * cinfo->fb_vinfo.yres * cinfo->fb_vinfo.bits_per_pixel / 8;
191   cinfo->fbptr = (char *)mmap(0,screensize,PROT_READ | PROT_WRITE, MAP_SHARED,cinfo->fd_framebuffer,0);
192   if((int)(cinfo->fbptr) == -1){
193     fprintf(stderr, "Can't get memory map of framebuffer device\n");
194     exit(1);  
195   }
196   cinfo->fb_yinc = cinfo->fb_vinfo.bits_per_pixel / 8;
197
198   /* access IP core */
199   printf("setting up hardware\n");
200   cinfo->dev_fd = open("/dev/kmjpeg", O_RDWR);
201   if(cinfo->dev_fd == -1){
202     printf("kmjpeg device file open error\n");
203     exit(1);
204   }
205   regset.fb = cinfo->fb_start_add;
206   xmcumax = cinfo->image_width / 16 -1;
207   ymcumax = cinfo->image_height /16 -1;
208   incaddmcuy = (cinfo->fb_vinfo.xres - (xmcumax +1)*16 )* 2 + 4;
209
210 #ifdef DEBUG
211   fprintf(stderr, "xmcumax = %d, ymcumax = %d, incaddmcuy = %x\n",xmcumax,ymcumax,incaddmcuy);
212 #endif
213
214   compptr = cinfo->comp_info;
215   for(j=0;j<3;j++,compptr++){
216     h_samp[j] = compptr->h_samp_factor;
217     v_samp[j] = compptr->v_samp_factor;
218     qtbl_no[j] = compptr->quant_tbl_no;
219     printf("h_samp = %d, v_samp = %d, quant table no =%d\n", h_samp[j],v_samp[j],qtbl_no[j]);
220   }
221
222   if(!((h_samp[0] == 2) && (v_samp[0]==2))){
223     printf("this mjpeg file is not 4:1:1 4:2:2\n");
224     exit(1);
225   }
226   if((h_samp[1]==1) && (v_samp[1]==1) && (h_samp[2]==1) && (v_samp[2]==1)){
227     samp_f = 0;
228     printf("this mjpeg file is 4:1:1\n");
229   }else if((h_samp[1]==1) && (v_samp[1]==2) && (h_samp[2]==1) && (v_samp[2]==2)){
230     samp_f = 1;
231     printf("this mjpeg file is 4:2:2\n");
232   }else{
233     printf("this mjpeg file is not 4:1:1, 4:2:2\n");
234     exit(1);
235   }
236
237
238   size = (unsigned int) ((samp_f <<22) | (ymcumax <<17) | (xmcumax <<11) | incaddmcuy) ;
239   regset.size_info = size;
240
241   incaddy = cinfo->fb_vinfo.xres * 2 - 28; 
242   incaddmcux = -1 *(15 * cinfo->fb_vinfo.xres * 2 - 4);
243
244   incadd = (unsigned int)((incaddy <<16) |(incaddmcux & 0x0000ffff));
245   regset.incadd = incadd; 
246
247 #ifdef DEBUG
248   fprintf(stderr, "ioctl fb_start_add = %x, size_info = %x, incadd = %x\n",regset.fb, regset.size_info, regset.incadd );
249   for(i=0;i<10000;i++);
250 #endif
251
252   ioctl(cinfo->dev_fd, IOCTL_REGSET, &regset);
253
254   /* call ioctl for ioremap */
255   ioctl(cinfo->dev_fd, IOCTL_MAP);
256
257 #ifdef DEBUG
258   fprintf(stderr, "Start writing quant table into hardware SRAM\n");
259 #endif
260   compptr = cinfo->comp_info;
261   for(j=0;j<3;j++,compptr++){
262     qtbl = cinfo->quant_tbl_ptrs[qtbl_no[j]];
263     for(i=0;i<64;i++){
264       //         printf(".%x.",(int)qtbl->quantval[i]);
265           ioctl_dataq.quant[j*64+i] = qtbl->quantval[i];
266     }
267   }
268   ioctl(cinfo->dev_fd, IOCTL_QTBL, &ioctl_dataq);
269
270
271 #ifdef DEBUG
272   fprintf(stderr, "Start writing DCCache value into hardware SRAM\n");
273 #endif
274
275   for(j=0;j<2;j++){
276     //    printf("DCcache id = %d\n", j);
277     for(i=0;i<512;i++){
278       //          printf("address=%x, nbits=%d, sym=%x\n", i, cinfo->harddc8nbits[j][i], cinfo->harddc8sym[j][i]);
279       toregister = ((cinfo->harddc8nbits[j][i] & 0x0000000f)<<4) | (cinfo->harddc8sym[j][i] & 0x0000000f);
280       cadd = (j << 9) | i;  
281       offdata.offset[i%128] = cadd;
282       offdata.data[i%128] = toregister;
283       //      printf("offset=%x,  data=%x\n",offdata.offset[i%128], offdata.data[i%128]);
284       if(i%128 == 127){
285         ioctl(cinfo->dev_fd, IOCTL_DCCACHE, &offdata);
286       }
287     }
288   }
289
290   memset(&offdata, 0, sizeof(offdata));
291
292   for(j=0;j<2;j++){
293     //  printf("ACcache id = %d\n", j);
294     for(i=0;i<512;i++){
295       //      printf("address=%x, nbits=%d, sym=%x\n", i, cinfo->hardac8nbits[j][i], cinfo->hardac8sym[j][i]);
296       toregister = ((cinfo->hardac8nbits[j][i] & 0x0000000f)<<8) | (cinfo->hardac8sym[j][i] & 0x000000ff);
297       cadd = (j<<9)| i;
298       offdata.offset[i%128] = cadd;
299       offdata.data[i%128] = toregister;
300       //     printf("offset=%x,  data=%x\n", offdata.offset[i%128], offdata.data[i%128]);
301       if(i%128 == 127){
302         ioctl(cinfo->dev_fd, IOCTL_ACCACHE, &offdata);
303       }
304     }
305   }
306
307   printf("maxcodemem\n");
308   for(i=0;i<4;i++){
309     for(j=0;j<32;j++){
310       if(j<18){
311         datad.data[j+i*32]=cinfo->maxcodemem[i][j];
312       }else{
313         datad.data[j+i*32]= 0;
314       }
315     }
316   }
317   ioctl(cinfo->dev_fd, IOCTL_MAXC, &datad);
318
319   printf("valoffset\n");
320   for(i=0;i<4;i++){
321     for(j=0;j<32;j++){
322       if(j<18){
323         datad.data[j+i*32]=cinfo->offsetmem[i][j];
324       }else{
325         datad.data[j+i*32]=0;
326       }
327     }
328   }
329   ioctl(cinfo->dev_fd, IOCTL_OFFS, &datad);
330      
331   
332   printf("huffvalmem\n");
333   for(i=0;i<4;i++){
334     printf("i=%d\n",i);
335     for(j=0;j<256;j++){
336       offdata.offset[j%128] = j+i*256;
337       offdata.data[j%128] = cinfo->huffvalmem[i][j];
338       if(j%128 == 127){
339         ioctl(cinfo->dev_fd, IOCTL_HUFFV, &offdata);
340       }
341     }
342   }
343
344   /*call ioctl for iounmap */
345   ioctl(cinfo->dev_fd, IOCTL_UNMAP);
346   printf(" end of setup hardware\n");
347 }
348
349 push_mjpegdata(j_decompress_ptr cinfo){
350   unsigned int mjdata;
351   struct ioctl_cmdwrite scandata; 
352   int i, ret,j;  
353   unsigned int putdata;
354
355   fprintf(stderr,"function : push_mjpegdata is starting\n");
356   memset(&scandata, 0, sizeof(scandata));
357   INPUT_VARS(cinfo);
358   while(1){
359     //    if(f_num == 200)break;
360     for(i=0;i<64;i++){
361       INPUT_4BYTES(cinfo, mjdata, exit(0));
362       scandata.fourbdata[i] = mjdata;
363       //         fprintf(stderr,"%08X\n",mjdata);
364     }
365     ioctl(cinfo->dev_fd, IOCTL_WRITE, &scandata);
366     if(mjdata == 0xd9ffd9ff){
367       jpeg_destroy_decompress(cinfo);
368       exit(0);
369     }
370     //              for(i=0;i<100000;i++);
371   }
372  }
373
374
375
376
377 LOCAL(void)
378 usage (void)
379 /* complain about bad command line */
380 {
381   fprintf(stderr, "usage: %s [switches] ", progname);
382 #ifdef TWO_FILE_COMMANDLINE
383   fprintf(stderr, "inputfile outputfile\n");
384 #else
385   fprintf(stderr, "[inputfile]\n");
386 #endif
387
388   fprintf(stderr, "Switches (names may be abbreviated):\n");
389   fprintf(stderr, "  -fb_add FramebufferAddress  (Hexadecimal number of Framebuffer start address. ex:41a00000) \n");
390   
391
392   exit(EXIT_FAILURE);
393 }
394
395
396 LOCAL(int)
397 parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
398                 int last_file_arg_seen, boolean for_real)
399 /* Parse optional switches.
400  * Returns argv[] index of first file-name argument (== argc if none).
401  * Any file names with indexes <= last_file_arg_seen are ignored;
402  * they have presumably been processed in a previous iteration.
403  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
404  * for_real is FALSE on the first (dummy) pass; we may skip any expensive
405  * processing.
406  */
407 {
408   int argn;
409   char * arg;
410
411   /* Set up default JPEG parameters. */
412   requested_fmt = DEFAULT_FMT;  /* set default output file format */
413   outfilename = NULL;
414   cinfo->err->trace_level = 0;
415
416   /* Scan command line options, adjust parameters */
417
418   for (argn = 1; argn < argc; argn++) {
419     arg = argv[argn];
420     if (*arg != '-') {
421       /* Not a switch, must be a file name argument */
422       if (argn <= last_file_arg_seen) {
423         outfilename = NULL;     /* -outfile applies to just one input file */
424         continue;               /* ignore this name if previously processed */
425       }
426       break;                    /* else done parsing switches */
427     }
428     arg++;                      /* advance past switch marker character */
429
430     if (keymatch(arg, "bmp", 1)) {
431       /* BMP output format. */
432       requested_fmt = FMT_BMP;
433
434     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
435                keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
436       /* Do color quantization. */
437       int val;
438
439       if (++argn >= argc)       /* advance to next argument */
440         usage();
441       if (sscanf(argv[argn], "%d", &val) != 1)
442         usage();
443       cinfo->desired_number_of_colors = val;
444       cinfo->quantize_colors = TRUE;
445
446     } else if (keymatch(arg, "dct", 2)) {
447       /* Select IDCT algorithm. */
448       if (++argn >= argc)       /* advance to next argument */
449         usage();
450       if (keymatch(argv[argn], "int", 1)) {
451         cinfo->dct_method = JDCT_ISLOW;
452       } else if (keymatch(argv[argn], "fast", 2)) {
453         cinfo->dct_method = JDCT_IFAST;
454       } else if (keymatch(argv[argn], "float", 2)) {
455         cinfo->dct_method = JDCT_FLOAT;
456       } else
457         usage();
458
459     } else if (keymatch(arg, "dither", 2)) {
460       /* Select dithering algorithm. */
461       if (++argn >= argc)       /* advance to next argument */
462         usage();
463       if (keymatch(argv[argn], "fs", 2)) {
464         cinfo->dither_mode = JDITHER_FS;
465       } else if (keymatch(argv[argn], "none", 2)) {
466         cinfo->dither_mode = JDITHER_NONE;
467       } else if (keymatch(argv[argn], "ordered", 2)) {
468         cinfo->dither_mode = JDITHER_ORDERED;
469       } else
470         usage();
471
472     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
473       /* Enable debug printouts. */
474       /* On first -d, print version identification */
475       static boolean printed_version = FALSE;
476
477       if (! printed_version) {
478         fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
479                 JVERSION, JCOPYRIGHT);
480         printed_version = TRUE;
481       }
482       cinfo->err->trace_level++;
483
484     } else if (keymatch(arg, "fast", 1)) {
485       /* Select recommended processing options for quick-and-dirty output. */
486       cinfo->two_pass_quantize = FALSE;
487       cinfo->dither_mode = JDITHER_ORDERED;
488       if (! cinfo->quantize_colors) /* don't override an earlier -colors */
489         cinfo->desired_number_of_colors = 216;
490       cinfo->dct_method = JDCT_FASTEST;
491       cinfo->do_fancy_upsampling = FALSE;
492
493     } else if (keymatch(arg, "gif", 1)) {
494       /* GIF output format. */
495       requested_fmt = FMT_GIF;
496
497     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
498       /* Force monochrome output. */
499       cinfo->out_color_space = JCS_GRAYSCALE;
500
501     } else if (keymatch(arg, "map", 3)) {
502       /* Quantize to a color map taken from an input file. */
503       if (++argn >= argc)       /* advance to next argument */
504         usage();
505       if (for_real) {           /* too expensive to do twice! */
506 #ifdef QUANT_2PASS_SUPPORTED    /* otherwise can't quantize to supplied map */
507         FILE * mapfile;
508
509         if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
510           fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
511           exit(EXIT_FAILURE);
512         }
513         read_color_map(cinfo, mapfile);
514         fclose(mapfile);
515         cinfo->quantize_colors = TRUE;
516 #else
517         ERREXIT(cinfo, JERR_NOT_COMPILED);
518 #endif
519       }
520
521     } else if (keymatch(arg, "maxmemory", 3)) {
522       /* Maximum memory in Kb (or Mb with 'm'). */
523       long lval;
524       char ch = 'x';
525
526       if (++argn >= argc)       /* advance to next argument */
527         usage();
528       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
529         usage();
530       if (ch == 'm' || ch == 'M')
531         lval *= 1000L;
532       cinfo->mem->max_memory_to_use = lval * 1000L;
533
534     } else if (keymatch(arg, "nosmooth", 3)) {
535       /* Suppress fancy upsampling */
536       cinfo->do_fancy_upsampling = FALSE;
537
538     } else if (keymatch(arg, "onepass", 3)) {
539       /* Use fast one-pass quantization. */
540       cinfo->two_pass_quantize = FALSE;
541
542     } else if (keymatch(arg, "os2", 3)) {
543       /* BMP output format (OS/2 flavor). */
544       requested_fmt = FMT_OS2;
545
546     } else if (keymatch(arg, "outfile", 4)) {
547       /* Set output file name. */
548       if (++argn >= argc)       /* advance to next argument */
549         usage();
550       outfilename = argv[argn]; /* save it away for later use */
551
552     } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
553       /* PPM/PGM output format. */
554       requested_fmt = FMT_PPM;
555
556     } else if (keymatch(arg, "rle", 1)) {
557       /* RLE output format. */
558       requested_fmt = FMT_RLE;
559
560     } else if (keymatch(arg, "scale", 1)) {
561       /* Scale the output image by a fraction M/N. */
562       if (++argn >= argc)       /* advance to next argument */
563         usage();
564       if (sscanf(argv[argn], "%d/%d",
565                  &cinfo->scale_num, &cinfo->scale_denom) != 2)
566         usage();
567
568     } else if (keymatch(arg, "targa", 1)) {
569       /* Targa output format. */
570       requested_fmt = FMT_TARGA;
571
572     } else if (keymatch(arg, "fb_add", 1)) {
573       /* Set Framebuffer address */
574       unsigned int xval;
575
576       if (++argn >= argc)       /* advance to next argument */
577         usage();
578       if (sscanf(argv[argn], "%x", &xval) != 1)
579         usage();
580       cinfo->fb_start_add = xval;
581
582     } else {
583       usage();                  /* bogus switch */
584     }
585   }
586
587   return argn;                  /* return index of next arg (file name) */
588 }
589
590
591 /*
592  * Marker processor for COM and interesting APPn markers.
593  * This replaces the library's built-in processor, which just skips the marker.
594  * We want to print out the marker as text, to the extent possible.
595  * Note this code relies on a non-suspending data source.
596  */
597
598 LOCAL(unsigned int)
599 jpeg_getc (j_decompress_ptr cinfo)
600 /* Read next byte */
601 {
602   struct jpeg_source_mgr * datasrc = cinfo->src;
603
604   if (datasrc->bytes_in_buffer == 0) {
605     if (! (*datasrc->fill_input_buffer) (cinfo))
606       ERREXIT(cinfo, JERR_CANT_SUSPEND);
607   }
608   datasrc->bytes_in_buffer--;
609   return GETJOCTET(*datasrc->next_input_byte++);
610 }
611
612
613 METHODDEF(boolean)
614 print_text_marker (j_decompress_ptr cinfo)
615 {
616   boolean traceit = (cinfo->err->trace_level >= 1);
617   INT32 length;
618   unsigned int ch;
619   unsigned int lastch = 0;
620
621   length = jpeg_getc(cinfo) << 8;
622   length += jpeg_getc(cinfo);
623   length -= 2;                  /* discount the length word itself */
624
625   if (traceit) {
626     if (cinfo->unread_marker == JPEG_COM)
627       fprintf(stderr, "Comment, length %ld:\n", (long) length);
628     else                        /* assume it is an APPn otherwise */
629       fprintf(stderr, "APP%d, length %ld:\n",
630               cinfo->unread_marker - JPEG_APP0, (long) length);
631   }
632
633   while (--length >= 0) {
634     ch = jpeg_getc(cinfo);
635     if (traceit) {
636       /* Emit the character in a readable form.
637        * Nonprintables are converted to \nnn form,
638        * while \ is converted to \\.
639        * Newlines in CR, CR/LF, or LF form will be printed as one newline.
640        */
641       if (ch == '\r') {
642         fprintf(stderr, "\n");
643       } else if (ch == '\n') {
644         if (lastch != '\r')
645           fprintf(stderr, "\n");
646       } else if (ch == '\\') {
647         fprintf(stderr, "\\\\");
648       } else if (isprint(ch)) {
649         putc(ch, stderr);
650       } else {
651         fprintf(stderr, "\\%03o", ch);
652       }
653       lastch = ch;
654     }
655   }
656
657   if (traceit)
658     fprintf(stderr, "\n");
659
660   return TRUE;
661 }
662
663
664 /*
665  * The main program.
666  */
667
668 int
669 main (int argc, char **argv)
670 {
671   struct jpeg_decompress_struct cinfo;
672   struct jpeg_error_mgr jerr;
673 #ifdef PROGRESS_REPORT
674   struct cdjpeg_progress_mgr progress;
675 #endif
676   int file_index;
677   djpeg_dest_ptr dest_mgr = NULL;
678   FILE * input_file;
679   FILE * output_file;
680   JDIMENSION num_scanlines;
681
682   struct timeval j_50f,j_100f;
683   double exe_fps;
684
685
686   /* On Mac, fetch a command line. */
687 #ifdef USE_CCOMMAND
688   argc = ccommand(&argv);
689 #endif
690
691   progname = argv[0];
692   if (progname == NULL || progname[0] == 0)
693     progname = "djpeg";         /* in case C library doesn't provide it */
694
695   /* Initialize the JPEG decompression object with default error handling. */
696   cinfo.err = jpeg_std_error(&jerr);
697   jpeg_create_decompress(&cinfo);
698   /* Add some application-specific error messages (from cderror.h) */
699   jerr.addon_message_table = cdjpeg_message_table;
700   jerr.first_addon_message = JMSG_FIRSTADDONCODE;
701   jerr.last_addon_message = JMSG_LASTADDONCODE;
702
703   /* Insert custom marker processor for COM and APP12.
704    * APP12 is used by some digital camera makers for textual info,
705    * so we provide the ability to display it as text.
706    * If you like, additional APPn marker types can be selected for display,
707    * but don't try to override APP0 or APP14 this way (see libjpeg.doc).
708    */
709   jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
710   jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
711
712   /* Now safe to enable signal catcher. */
713 #ifdef NEED_SIGNAL_CATCHER
714   enable_signal_catcher((j_common_ptr) &cinfo);
715 #endif
716
717   /* Scan command line to find file names. */
718   /* It is convenient to use just one switch-parsing routine, but the switch
719    * values read here are ignored; we will rescan the switches after opening
720    * the input file.
721    * (Exception: tracing level set here controls verbosity for COM markers
722    * found during jpeg_read_header...)
723    */
724
725   file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
726
727 #ifdef TWO_FILE_COMMANDLINE
728   /* Must have either -outfile switch or explicit output file name */
729   if (outfilename == NULL) {
730     if (file_index != argc-2) {
731       fprintf(stderr, "%s: must name one input and one output file\n",
732               progname);
733       usage();
734     }
735     outfilename = argv[file_index+1];
736   } else {
737     if (file_index != argc-1) {
738       fprintf(stderr, "%s: must name one input and one output file\n",
739               progname);
740       usage();
741     }
742   }
743 #else
744   /* Unix style: expect zero or one file name */
745   if (file_index < argc-1) {
746     fprintf(stderr, "%s: only one input file\n", progname);
747     usage();
748   }
749 #endif /* TWO_FILE_COMMANDLINE */
750
751   /* Open the input file. */
752   if (file_index < argc) {
753     if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
754       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
755       exit(EXIT_FAILURE);
756     }
757   } else {
758     /* default input file is stdin */
759     input_file = read_stdin();
760   }
761
762   /* Open the output file. */
763   if (outfilename != NULL) {
764     if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
765       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
766       exit(EXIT_FAILURE);
767     }
768   } else {
769     /* default output file is stdout */
770     output_file = write_stdout();
771   }
772
773 #ifdef PROGRESS_REPORT
774   start_progress_monitor((j_common_ptr) &cinfo, &progress);
775 #endif
776
777   /* Specify data source for decompression */
778   jpeg_stdio_src(&cinfo, input_file);
779
780  /*V1 LEON-mjpeg project */
781   for(f_num=0;f_num<110;f_num++){
782
783   if(f_num == 50){
784     gettimeofday(&j_50f,NULL);
785   }
786   if(f_num == 100){
787     gettimeofday(&j_100f,NULL);
788   }
789
790   /* Read file header, set default decompression parameters */
791   (void) jpeg_read_header(&cinfo, TRUE);
792  
793   /* Adjust default decompression parameters by re-parsing the options */
794    file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
795
796   /* Initialize the output module now to let it override any crucial
797    * option settings (for instance, GIF wants to force color quantization).
798    */
799   switch (requested_fmt) {
800 #ifdef BMP_SUPPORTED
801   case FMT_BMP:
802     dest_mgr = jinit_write_bmp(&cinfo, FALSE);
803     break;
804   case FMT_OS2:
805     dest_mgr = jinit_write_bmp(&cinfo, TRUE);
806     break;
807 #endif
808 #ifdef GIF_SUPPORTED
809   case FMT_GIF:
810     dest_mgr = jinit_write_gif(&cinfo);
811     break;
812 #endif
813 #ifdef PPM_SUPPORTED
814   case FMT_PPM:
815     dest_mgr = jinit_write_ppm(&cinfo);
816     break;
817 #endif
818 #ifdef RLE_SUPPORTED
819   case FMT_RLE:
820     dest_mgr = jinit_write_rle(&cinfo);
821     break;
822 #endif
823 #ifdef TARGA_SUPPORTED
824   case FMT_TARGA:
825     dest_mgr = jinit_write_targa(&cinfo);
826     break;
827 #endif
828   default:
829     ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
830     break;
831   }
832   dest_mgr->output_file = output_file;
833
834   /* Start decompressor */
835   (void) jpeg_start_decompress(&cinfo);
836
837   if(f_num == 0){
838     setup_hardware(&cinfo);
839   }
840
841   /* Write output file header */
842   //  (*dest_mgr->start_output) (&cinfo, dest_mgr);
843
844
845   push_mjpegdata(&cinfo);
846
847   /* Process data */
848   //  while (cinfo.output_scanline < cinfo.output_height) {
849   //   num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
850   //                                    dest_mgr->buffer_height);
851     //   (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
852   // }
853
854 #ifdef PROGRESS_REPORT
855   /* Hack: count final pass as done in case finish_output does an extra pass.
856    * The library won't have updated completed_passes.
857    */
858   progress.pub.completed_passes = progress.pub.total_passes;
859 #endif
860
861   /* Finish decompression and release memory.
862    * I must do it in this order because output module has allocated memory
863    * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
864    */
865   (*dest_mgr->finish_output) (&cinfo, dest_mgr);
866   (void) jpeg_finish_decompress(&cinfo);
867   }
868   jpeg_destroy_decompress(&cinfo);
869
870   /* Close files, if we opened them */
871   if (input_file != stdin)
872     fclose(input_file);
873   if (output_file != stdout)
874     fclose(output_file);
875
876 #ifdef PROGRESS_REPORT
877   end_progress_monitor((j_common_ptr) &cinfo);
878 #endif
879
880   fprintf(stderr, "Frame  #50 = %d.%d\n", j_50f.tv_sec,j_50f.tv_usec);
881   fprintf(stderr, "Frame #100 = %d.%d\n", j_100f.tv_sec,j_100f.tv_usec);
882
883   exe_fps = 50/(((double)j_100f.tv_sec + (double)j_100f.tv_usec / 1e6)
884                  -((double)j_50f.tv_sec + (double)j_50f.tv_usec / 1e6));
885
886
887   fprintf(stderr, "This program decode %f fps\n", exe_fps);
888
889
890
891   /* All done. */
892   exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
893   return 0;                     /* suppress no-return-value warnings */
894 }