/* * yuvnull - reads a yuv stream from stdin and writes it to stdout. * * Copyright (C) 2001, pHilipp Zabel * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include "yuv4mpeg.h" #define OUT 0 static void usage (progname) { fprintf (stderr, "usage: %s [-b ] [-f ]\n",progname); } int do_pipe(char * pipestream,int dir,int *fd){ int rev[]={1,0}; int fda[2]; int pid; pipe(fda); pid=fork(); switch(pid){ case -1: break; case 0: close(0); dup(fda[0]); close(fda[0]); close(fda[1]); execlp("mpeg2enc","mpeg2enc","-f","1","-v","0", "-o",pipestream,0 ); break; default: close(fda[0]); break; } *fd=fda[rev[dir]]; // return open(pipestream,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); return pid; } int main (int argc, char *argv[]){ int in_fd = 0; /* stdin */ int out_fd = 1; /* stdout */ unsigned char *yuv[3]; /* output */ y4m_stream_info_t streaminfo; y4m_frame_info_t frameinfo; int i; int w, h; int frames=100; char *basename=NULL; char filename[1024]; int count,filenum=-1,pid; i=0; while (i=argc){ usage(argv[0]); exit(1); } frames=atoi(argv[i]); if (frames==0){ usage(argv[0]); exit(1); } break; case 'b': i++; if (i>=argc){ usage(argv[0]); exit(1); } basename=argv[i]; break; case 'h': usage(argv[0]); exit(0); break; default: } i++; } if (basename==NULL){ basename="chunk%02d.mpg"; } fprintf(stderr,"%s %d\n",basename,frames); count=frames; y4m_init_stream_info (&streaminfo); y4m_init_frame_info (&frameinfo); i = y4m_read_stream_header (in_fd, &streaminfo); if (i != Y4M_OK) { fprintf (stderr, "%s: input stream error - %s\n", argv[0], y4m_strerr(i)); exit (1); } w = y4m_si_get_width(&streaminfo); h = y4m_si_get_height(&streaminfo); yuv[0] = malloc (w * h); yuv[1] = malloc (w * h / 4); yuv[2] = malloc (w * h / 4); while (1) { if (count==frames) { count=0; if(out_fd>1){ close(out_fd); } filenum++; sprintf(filename,basename,filenum); pid=do_pipe(filename,OUT, &out_fd); y4m_write_stream_header (out_fd, &streaminfo); } i = y4m_read_frame(in_fd, &streaminfo, &frameinfo, yuv); if (i == Y4M_ERR_EOF) { exit (0); } else if (i != Y4M_OK){ exit (1); } y4m_write_frame (out_fd, &streaminfo, &frameinfo, yuv); count++; } }