1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_ffmpeg.cpp
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
35 #include <ETL/stringf>
36 #include "mptr_ffmpeg.h"
38 #include <sys/types.h>
55 #include <ETL/stringf>
58 /* === M A C R O S ========================================================= */
60 using namespace synfig;
64 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
65 #define UNIX_PIPE_TO_PROCESSES
67 #define WIN32_PIPE_TO_PROCESSES
70 /* === G L O B A L S ======================================================= */
72 SYNFIG_IMPORTER_INIT(ffmpeg_mptr);
73 SYNFIG_IMPORTER_SET_NAME(ffmpeg_mptr,"ffmpeg");
74 SYNFIG_IMPORTER_SET_EXT(ffmpeg_mptr,"avi");
75 SYNFIG_IMPORTER_SET_VERSION(ffmpeg_mptr,"0.1");
76 SYNFIG_IMPORTER_SET_CVS_ID(ffmpeg_mptr,"$Id$");
78 /* === M E T H O D S ======================================================= */
80 bool ffmpeg_mptr::is_animated()
86 ffmpeg_mptr::seek_to(int frame)
88 if(frame<cur_frame || !file)
92 #if defined(WIN32_PIPE_TO_PROCESSES)
94 #elif defined(UNIX_PIPE_TO_PROCESSES)
97 waitpid(pid,&status,0);
101 #if defined(WIN32_PIPE_TO_PROCESSES)
105 command=strprintf("ffmpeg -i \"%s\" -an -f image2pipe -vcodec ppm -\n",filename.c_str());
107 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
109 #elif defined(UNIX_PIPE_TO_PROCESSES)
114 cerr<<"Unable to open pipe to ffmpeg"<<endl;
121 cerr<<"Unable to open pipe to ffmpeg"<<endl;
127 // Close pipein, not needed
129 // Dup pipein to stdout
130 if( dup2( p[1], STDOUT_FILENO ) == -1 ){
131 cerr<<"Unable to open pipe to ffmpeg"<<endl;
134 // Close the unneeded pipein
136 execlp("ffmpeg", "ffmpeg", "-i", filename.c_str(), "-an", "-f", "image2pipe", "-vcodec", "ppm", "-", (const char *)NULL);
137 // We should never reach here unless the exec failed
138 cerr<<"Unable to open pipe to ffmpeg"<<endl;
142 // Close pipeout, not needed
144 // Save pipein to file handle, will read from it later
145 file = fdopen(p[0], "rb");
149 #error There are no known APIs for creating child processes
154 cerr<<"Unable to open pipe to ffmpeg"<<endl;
160 while(cur_frame<frame-1)
162 cerr<<"Seeking to..."<<frame<<'('<<cur_frame<<')'<<endl;
170 ffmpeg_mptr::grab_frame(void)
174 cerr<<"unable to open "<<filename<<endl;
180 cookie[0]=fgetc(file);
185 cookie[1]=fgetc(file);
187 if(cookie[0]!='P' || cookie[1]!='6')
189 cerr<<"stream not in PPM format \""<<cookie[0]<<cookie[1]<<'"'<<endl;
194 fscanf(file,"%d %d\n",&w,&h);
195 fscanf(file,"%f",&divisor);
204 for(y=0;y<frame.get_h();y++)
205 for(x=0;x<frame.get_w();x++)
211 (float)(unsigned char)fgetc(file)/divisor,
212 (float)(unsigned char)fgetc(file)/divisor,
213 (float)(unsigned char)fgetc(file)/divisor,
216 float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
217 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
218 float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
230 ffmpeg_mptr::ffmpeg_mptr(const char *f)
233 #ifdef HAVE_TERMIOS_H
234 tcgetattr (0, &oldtty);
242 ffmpeg_mptr::~ffmpeg_mptr()
246 #if defined(WIN32_PIPE_TO_PROCESSES)
248 #elif defined(UNIX_PIPE_TO_PROCESSES)
251 waitpid(pid,&status,0);
254 #ifdef HAVE_TERMIOS_H
255 tcsetattr(0,TCSANOW,&oldtty);
260 ffmpeg_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *)
262 int i=(int)(time*fps);