When we cannot launch ffmpeg in the ffmpeg importer, kill the child process.
[synfig.git] / synfig-core / trunk / src / modules / mod_ffmpeg / mptr_ffmpeg.cpp
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
10 **
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.
15 **
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
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
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
57
58 /* === M A C R O S ========================================================= */
59
60 using namespace synfig;
61 using namespace std;
62 using namespace etl;
63
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
69
70 /* === G L O B A L S ======================================================= */
71
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$");
77
78 /* === M E T H O D S ======================================================= */
79
80 bool ffmpeg_mptr::is_animated()
81 {
82         return true;
83 }
84
85 bool
86 ffmpeg_mptr::seek_to(int frame)
87 {
88         if(frame<cur_frame || !file)
89         {
90                 if(file)
91                 {
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
99                 }
100
101 #if defined(WIN32_PIPE_TO_PROCESSES)
102
103                 string command;
104                 
105                 command=strprintf("ffmpeg -i \"%s\" -an -f image2pipe -vcodec ppm -\n",filename.c_str());
106                 
107                 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
108
109 #elif defined(UNIX_PIPE_TO_PROCESSES)
110
111                 int p[2];
112           
113                 if (pipe(p)) {
114                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
115                         return false;
116                 };
117           
118                 pid = fork();
119           
120                 if (pid == -1) {
121                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
122                         return false;
123                 }
124           
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;
133                         }
134                         // Close the unneeded pipein
135                         close(p[1]);
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;
139                         _exit(1);
140                 } else {
141                         // Parent process
142                         // Close pipeout, not needed
143                         close(p[1]);
144                         // Save pipein to file handle, will read from it later
145                         file = fdopen(p[0], "rb");
146                 }
147
148 #else
149         #error There are no known APIs for creating child processes
150 #endif
151
152                 if(!file)
153                 {
154                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
155                         return false;
156                 }
157                 cur_frame=-1;
158         }
159
160         while(cur_frame<frame-1)
161         {
162                 cerr<<"Seeking to..."<<frame<<'('<<cur_frame<<')'<<endl;
163                 if(!grab_frame())
164                         return false;
165         }
166         return true;
167 }
168
169 bool
170 ffmpeg_mptr::grab_frame(void)
171 {
172         if(!file)
173         {
174                 cerr<<"unable to open "<<filename<<endl;
175                 return false;
176         }
177         int w,h;
178         float divisor;
179         char cookie[2];
180         cookie[0]=fgetc(file);
181         cookie[1]=fgetc(file);
182
183         if(cookie[0]!='P' || cookie[1]!='6')
184         {
185                 cerr<<"stream not in PPM format \""<<cookie[0]<<cookie[1]<<'"'<<endl;
186                 return false;
187         }
188
189         fgetc(file);
190         fscanf(file,"%d %d\n",&w,&h);
191         fscanf(file,"%f",&divisor);
192         fgetc(file);
193
194         if(feof(file))
195                 return false;
196
197         int x;
198         int y;
199         frame.set_wh(w,h);
200         for(y=0;y<frame.get_h();y++)
201                 for(x=0;x<frame.get_w();x++)
202                 {
203                         if(feof(file))
204                                 return false;
205 /*
206                         frame[y][x]=Color(
207                                 (float)(unsigned char)fgetc(file)/divisor,
208                                 (float)(unsigned char)fgetc(file)/divisor,
209                                 (float)(unsigned char)fgetc(file)/divisor,
210                                 1.0
211 */
212                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
213                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
214                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
215                         frame[y][x]=Color(
216                                 r,
217                                 g,
218                                 b,
219                                 1.0
220                         );
221                 }
222         cur_frame++;
223         return true;
224 }
225
226 ffmpeg_mptr::ffmpeg_mptr(const char *f)
227 {
228         pid=-1;
229 #ifdef HAVE_TERMIOS_H
230         tcgetattr (0, &oldtty);
231 #endif
232         filename=f;
233         file=NULL;
234         fps=23.98;
235         cur_frame=-1;
236 }
237
238 ffmpeg_mptr::~ffmpeg_mptr()
239 {
240         if(file)
241         {
242 #if defined(WIN32_PIPE_TO_PROCESSES)
243                 pclose(file);
244 #elif defined(UNIX_PIPE_TO_PROCESSES)
245                 fclose(file);
246                 int status;
247                 waitpid(pid,&status,0);
248 #endif
249         }
250 #ifdef HAVE_TERMIOS_H
251         tcsetattr(0,TCSANOW,&oldtty);
252 #endif
253 }
254
255 bool
256 ffmpeg_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *)
257 {
258         int i=(int)(time*fps);
259         if(i!=cur_frame)
260         {
261                 if(!seek_to(i))
262                         return false;
263                 if(!grab_frame())
264                         return false;
265         }
266
267         surface=frame;
268         return true;
269 }