Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / 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
81 ffmpeg_mptr::seek_to(int frame)
82 {
83         if(frame<cur_frame || !file)
84         {
85                 if(file)
86                 {
87 #if defined(WIN32_PIPE_TO_PROCESSES)
88                         pclose(file);
89 #elif defined(UNIX_PIPE_TO_PROCESSES)
90                         fclose(file);
91                         int status;
92                         waitpid(pid,&status,0);
93 #endif
94                 }
95
96 #if defined(WIN32_PIPE_TO_PROCESSES)
97
98                 string command;
99                 
100                 command=strprintf("ffmpeg -i \"%s\" -an -f image2pipe -vcodec ppm -\n",filename.c_str());
101                 
102                 file=popen(command.c_str(),POPEN_BINARY_READ_TYPE);
103
104 #elif defined(UNIX_PIPE_TO_PROCESSES)
105
106                 int p[2];
107           
108                 if (pipe(p)) {
109                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
110                         return false;
111                 };
112           
113                 pid = fork();
114           
115                 if (pid == -1) {
116                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
117                         return false;
118                 }
119           
120                 if (pid == 0){
121                         // Child process
122                         // Close pipein, not needed
123                         close(p[0]);
124                         // Dup pipein to stdout
125                         if( dup2( p[1], STDOUT_FILENO ) == -1 ){
126                                 cerr<<"Unable to open pipe to ffmpeg"<<endl;
127                                 return false;
128                         }
129                         // Close the unneeded pipein
130                         close(p[1]);
131                         execlp("ffmpeg", "ffmpeg", "-i", filename.c_str(), "-an", "-f", "image2pipe", "-vcodec", "ppm", "-", (const char *)NULL);
132                         // We should never reach here unless the exec failed
133                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
134                         return false;
135                 } else {
136                         // Parent process
137                         // Close pipeout, not needed
138                         close(p[1]);
139                         // Save pipein to file handle, will read from it later
140                         file = fdopen(p[0], "rb");
141                 }
142
143 #else
144         #error There are no known APIs for creating child processes
145 #endif
146
147                 if(!file)
148                 {
149                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
150                         return false;
151                 }
152                 cur_frame=-1;
153         }
154
155         while(cur_frame<frame-1)
156         {
157                 cerr<<"Seeking to..."<<frame<<'('<<cur_frame<<')'<<endl;
158                 if(!grab_frame())
159                         return false;
160         }
161         return true;
162 }
163
164 bool
165 ffmpeg_mptr::grab_frame(void)
166 {
167         if(!file)
168         {
169                 cerr<<"unable to open "<<filename<<endl;
170                 return false;
171         }
172         int w,h;
173         float divisor;
174         char cookie[2];
175         cookie[0]=fgetc(file);
176         cookie[1]=fgetc(file);
177
178         if(cookie[0]!='P' || cookie[1]!='6')
179         {
180                 cerr<<"stream not in PPM format \""<<cookie[0]<<cookie[1]<<'"'<<endl;
181                 return false;
182         }
183
184         fgetc(file);
185         fscanf(file,"%d %d\n",&w,&h);
186         fscanf(file,"%f",&divisor);
187         fgetc(file);
188
189         if(feof(file))
190                 return false;
191
192         int x;
193         int y;
194         frame.set_wh(w,h);
195         for(y=0;y<frame.get_h();y++)
196                 for(x=0;x<frame.get_w();x++)
197                 {
198                         if(feof(file))
199                                 return false;
200 /*
201                         frame[y][x]=Color(
202                                 (float)(unsigned char)fgetc(file)/divisor,
203                                 (float)(unsigned char)fgetc(file)/divisor,
204                                 (float)(unsigned char)fgetc(file)/divisor,
205                                 1.0
206 */
207                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
208                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
209                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
210                         frame[y][x]=Color(
211                                 r,
212                                 g,
213                                 b,
214                                 1.0
215                         );
216                 }
217         cur_frame++;
218         return true;
219 }
220
221 ffmpeg_mptr::ffmpeg_mptr(const char *f)
222 {
223         pid=-1;
224 #ifdef HAVE_TERMIOS_H
225         tcgetattr (0, &oldtty);
226 #endif
227         filename=f;
228         file=NULL;
229         fps=23.98;
230         cur_frame=-1;
231 }
232
233 ffmpeg_mptr::~ffmpeg_mptr()
234 {
235         if(file)
236         {
237 #if defined(WIN32_PIPE_TO_PROCESSES)
238                 pclose(file);
239 #elif defined(UNIX_PIPE_TO_PROCESSES)
240                 fclose(file);
241                 int status;
242                 waitpid(pid,&status,0);
243 #endif
244         }
245 #ifdef HAVE_TERMIOS_H
246         tcsetattr(0,TCSANOW,&oldtty);
247 #endif
248 }
249
250 bool
251 ffmpeg_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *)
252 {
253         int i=(int)(time*fps);
254         if(i!=cur_frame)
255         {
256                 if(!seek_to(i))
257                         return false;
258                 if(!grab_frame())
259                         return false;
260         }
261
262         surface=frame;
263         return false;
264 }