Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
sample2.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 #include "convert.h"
00030
00031 char *prefix = NULL ;
00032 int count = 0 ;
00033 int docount = 0 ;
00034 #include <string.h>
00035
00036 static void save_ppm (int width, int height, uint8_t * buf, int num)
00037 {
00038 char filename[1000];
00039 FILE * ppmfile;
00040
00041 count++ ;
00042 if( docount ) return ;
00043
00044 if( prefix == NULL )
00045 sprintf (filename, "%06d.ppm", num);
00046 else
00047 sprintf (filename, "%s%06d.ppm", prefix,num);
00048 ppmfile = fopen (filename, "wb");
00049 if (!ppmfile) return;
00050 fprintf (ppmfile, "P6\n%d %d\n255\n", width, height);
00051 fwrite (buf, 3 * width, height, ppmfile);
00052 fclose (ppmfile);
00053 }
00054
00055 static void sample2 (FILE * file)
00056 {
00057 #define BUFFER_SIZE 4096
00058 uint8_t buffer[BUFFER_SIZE];
00059 mpeg2dec_t * mpeg2dec;
00060 const mpeg2_info_t * info;
00061 int state;
00062 int size;
00063 int framenum = 0;
00064
00065 mpeg2dec = mpeg2_init ();
00066 if (mpeg2dec == NULL)
00067 exit (1);
00068 info = mpeg2_info (mpeg2dec);
00069
00070 size = BUFFER_SIZE;
00071 do {
00072 state = mpeg2_parse (mpeg2dec);
00073 switch (state) {
00074 case -1:
00075 size = fread (buffer, 1, BUFFER_SIZE, file);
00076 mpeg2_buffer (mpeg2dec, buffer, buffer + size);
00077 break;
00078 case STATE_SEQUENCE:
00079 mpeg2_convert (mpeg2dec, convert_rgb24, NULL);
00080 break;
00081 case STATE_SLICE:
00082 case STATE_END:
00083 if (info->display_fbuf)
00084 save_ppm (info->sequence->width, info->sequence->height,
00085 info->display_fbuf->buf[0], framenum++);
00086 break;
00087 }
00088 } while (size);
00089
00090 mpeg2_close (mpeg2dec);
00091 }
00092
00093 int main (int argc, char ** argv)
00094 {
00095 FILE * file;
00096 int iarg=1 ;
00097
00098 if( argc < 2 || strstr(argv[1],"-help") != NULL ){
00099 printf("Usage: mpegtoppm [-prefix ppp] file.mpg\n"
00100 "Writes files named 'ppp'000001.ppm, etc.\n" ) ;
00101 exit(0) ;
00102 }
00103
00104 while( iarg < argc && argv[iarg][0] == '-' ){
00105 if( strncmp(argv[iarg],"-c",2) == 0 ){
00106 docount = 1 ; ++iarg ; continue ;
00107 }
00108
00109 if( strncmp(argv[iarg],"-p",2) == 0 ){
00110 prefix = argv[++iarg] ; ++iarg ; continue ;
00111 }
00112 }
00113
00114 if( iarg < argc ){
00115 file = fopen (argv[iarg], "rb");
00116 if (!file) {
00117 fprintf (stderr, "Could not open file %s\n", argv[iarg]);
00118 exit (1);
00119 }
00120 } else
00121 file = stdin;
00122
00123 sample2 (file);
00124
00125 if( docount && count > 0 ){
00126 char filename[1000] = "\0" ;
00127 FILE *fp ;
00128 if( prefix != NULL ) strcpy(filename,prefix) ;
00129 strcat(filename,"COUNT") ;
00130 fp = fopen (filename, "wb");
00131 fprintf(fp,"%d\n",count) ;
00132 fclose(fp) ;
00133 }
00134
00135 return 0;
00136 }