Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
sample1.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <inttypes.h>
00027 
00028 #include "mpeg2.h"
00029 
00030 static void save_pgm (int width, int height, uint8_t * const * buf, int num)
00031 {
00032     char filename[100];
00033     FILE * pgmfile;
00034     int i;
00035 
00036     sprintf (filename, "%d.pgm", num);
00037     pgmfile = fopen (filename, "wb");
00038     if (!pgmfile)
00039         return;
00040     fprintf (pgmfile, "P5\n%d %d\n255\n", width, height * 3 / 2);
00041     fwrite (buf[0], width, height, pgmfile);
00042     width >>= 1;
00043     height >>= 1;
00044     for (i = 0; i < height; i++) {
00045         fwrite (buf[1] + i * width, width, 1, pgmfile);
00046         fwrite (buf[2] + i * width, width, 1, pgmfile);
00047     }
00048     fclose (pgmfile);
00049 }
00050 
00051 static void sample1 (FILE * file)
00052 {
00053 #define BUFFER_SIZE 4096
00054     uint8_t buffer[BUFFER_SIZE];
00055     mpeg2dec_t * mpeg2dec;
00056     const mpeg2_info_t * info;
00057     int state;
00058     int size;
00059     int framenum = 0;
00060 
00061     mpeg2dec = mpeg2_init ();
00062     if (mpeg2dec == NULL)
00063         exit (1);
00064     info = mpeg2_info (mpeg2dec);
00065 
00066     size = BUFFER_SIZE;
00067     do {
00068         state = mpeg2_parse (mpeg2dec);
00069         switch (state) {
00070         case -1:
00071             size = fread (buffer, 1, BUFFER_SIZE, file);
00072             mpeg2_buffer (mpeg2dec, buffer, buffer + size);
00073             break;
00074         case STATE_SLICE:
00075         case STATE_END:
00076             if (info->display_fbuf)
00077                 save_pgm (info->sequence->width, info->sequence->height,
00078                           info->display_fbuf->buf, framenum++);
00079             break;
00080         }
00081     } while (size);
00082 
00083     mpeg2_close (mpeg2dec);
00084 }
00085 
00086 int main (int argc, char ** argv)
00087 {
00088     FILE * file;
00089 
00090     if (argc > 1) {
00091         file = fopen (argv[1], "rb");
00092         if (!file) {
00093             fprintf (stderr, "Could not open file %s\n", argv[1]);
00094             exit (1);
00095         }
00096     } else
00097         file = stdin;
00098 
00099     sample1 (file);
00100 
00101     return 0;
00102 }