Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

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  * sample1.c
00003  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
00004  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
00005  *
00006  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
00007  * See http://libmpeg2.sourceforge.net/ for updates.
00008  *
00009  * mpeg2dec is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * mpeg2dec is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }
 

Powered by Plone

This site conforms to the following standards: