Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / modules / mod_ffmpeg / mptr_ffmpeg.cpp
blobf837c7bd83c8d5b82d30b4f2b02fc0a5de1dce34
1 /* === S Y N F I G ========================================================= */
2 /*! \file mptr_ffmpeg.cpp
3 ** \brief ppm Target Module
4 **
5 ** $Id$
6 **
7 ** \legal
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.
20 ** \endlegal
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
28 #ifdef USING_PCH
29 # include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
35 #include <ETL/stringf>
36 #include "mptr_ffmpeg.h"
37 #include <stdio.h>
38 #include <sys/types.h>
39 #if HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
42 #if HAVE_IO_H
43 #include <io.h>
44 #endif
45 #if HAVE_PROCESS_H
46 #include <process.h>
47 #endif
48 #if HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif
51 #include <unistd.h>
52 #include <iostream>
53 #include <algorithm>
54 #include <functional>
55 #include <ETL/stringf>
56 #endif
58 /* === M A C R O S ========================================================= */
60 using namespace synfig;
61 using namespace std;
62 using namespace etl;
64 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
65 #define UNIX_PIPE_TO_PROCESSES
66 #else
67 #define WIN32_PIPE_TO_PROCESSES
68 #endif
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()
82 return true;
85 bool
86 ffmpeg_mptr::seek_to(int frame)
88 if(frame<cur_frame || !file)
90 if(file)
92 #if defined(WIN32_PIPE_TO_PROCESSES)
93 pclose(file);
94 #elif defined(UNIX_PIPE_TO_PROCESSES)
95 fclose(file);
96 int status;
97 waitpid(pid,&status,0);
98 #endif
101 #if defined(WIN32_PIPE_TO_PROCESSES)
103 string command;
105 command=strprintf("ffmpeg -ss 00:00:00.%d -i \"%s\" -an -f image2pipe -vcodec ppm -\n",frame,filename.c_str());
107 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
109 #elif defined(UNIX_PIPE_TO_PROCESSES)
111 int p[2];
113 if (pipe(p)) {
114 cerr<<"Unable to open pipe to ffmpeg"<<endl;
115 return false;
118 pid = fork();
120 if (pid == -1) {
121 cerr<<"Unable to open pipe to ffmpeg"<<endl;
122 return false;
125 if (pid == 0){
126 // Child process
127 // Close pipein, not needed
128 close(p[0]);
129 // Dup pipein to stdout
130 if( dup2( p[1], STDOUT_FILENO ) == -1 ){
131 cerr<<"Unable to open pipe to ffmpeg"<<endl;
132 return false;
134 // Close the unneeded pipein
135 close(p[1]);
136 string time = strprintf("00:00:00.%d",frame);
137 execlp("ffmpeg", "ffmpeg", "-ss", time.c_str(), "-i", filename.c_str(), "-an", "-f", "image2pipe", "-vcodec", "ppm", "-", (const char *)NULL);
138 // We should never reach here unless the exec failed
139 cerr<<"Unable to open pipe to ffmpeg"<<endl;
140 _exit(1);
141 } else {
142 // Parent process
143 // Close pipeout, not needed
144 close(p[1]);
145 // Save pipein to file handle, will read from it later
146 file = fdopen(p[0], "rb");
149 #else
150 #error There are no known APIs for creating child processes
151 #endif
153 if(!file)
155 cerr<<"Unable to open pipe to ffmpeg"<<endl;
156 return false;
158 cur_frame=-1;
161 while(cur_frame<frame-1)
163 cerr<<"Seeking to..."<<frame<<'('<<cur_frame<<')'<<endl;
164 if(!grab_frame())
165 return false;
167 return true;
170 bool
171 ffmpeg_mptr::grab_frame(void)
173 if(!file)
175 cerr<<"unable to open "<<filename<<endl;
176 return false;
178 int w,h;
179 float divisor;
180 char cookie[2];
181 cookie[0]=fgetc(file);
183 if(feof(file))
184 return false;
186 cookie[1]=fgetc(file);
188 if(cookie[0]!='P' || cookie[1]!='6')
190 cerr<<"stream not in PPM format \""<<cookie[0]<<cookie[1]<<'"'<<endl;
191 return false;
194 fgetc(file);
195 fscanf(file,"%d %d\n",&w,&h);
196 fscanf(file,"%f",&divisor);
197 fgetc(file);
199 if(feof(file))
200 return false;
202 int x;
203 int y;
204 frame.set_wh(w,h);
205 for(y=0;y<frame.get_h();y++)
206 for(x=0;x<frame.get_w();x++)
208 if(feof(file))
209 return false;
211 frame[y][x]=Color(
212 (float)(unsigned char)fgetc(file)/divisor,
213 (float)(unsigned char)fgetc(file)/divisor,
214 (float)(unsigned char)fgetc(file)/divisor,
217 float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
218 float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
219 float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
220 frame[y][x]=Color(
227 cur_frame++;
228 return true;
231 ffmpeg_mptr::ffmpeg_mptr(const char *f)
233 pid=-1;
234 #ifdef HAVE_TERMIOS_H
235 tcgetattr (0, &oldtty);
236 #endif
237 filename=f;
238 file=NULL;
239 fps=23.98;
240 cur_frame=-1;
243 ffmpeg_mptr::~ffmpeg_mptr()
245 if(file)
247 #if defined(WIN32_PIPE_TO_PROCESSES)
248 pclose(file);
249 #elif defined(UNIX_PIPE_TO_PROCESSES)
250 fclose(file);
251 int status;
252 waitpid(pid,&status,0);
253 #endif
255 #ifdef HAVE_TERMIOS_H
256 tcsetattr(0,TCSANOW,&oldtty);
257 #endif
260 bool
261 ffmpeg_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *)
263 int i=(int)(time*fps);
264 if(i!=cur_frame)
266 if(!seek_to(i))
267 return false;
268 if(!grab_frame())
269 return false;
272 surface=frame;
273 return true;