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 / trgt_ffmpeg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_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 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include <ETL/stringf>
38 #include "trgt_ffmpeg.h"
39 #include <stdio.h>
40 #include <sys/types.h>
41 #if HAVE_SYS_WAIT_H
42  #include <sys/wait.h>
43 #endif
44 #if HAVE_IO_H
45  #include <io.h>
46 #endif
47 #if HAVE_PROCESS_H
48  #include <process.h>
49 #endif
50 #if HAVE_FCNTL_H
51  #include <fcntl.h>
52 #endif
53 #include <unistd.h>
54 #include <algorithm>
55 #include <functional>
56 #include <ETL/clock>
57
58 #endif
59
60 /* === M A C R O S ========================================================= */
61
62 using namespace synfig;
63 using namespace std;
64 using namespace etl;
65
66 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
67  #define UNIX_PIPE_TO_PROCESSES
68 #else
69  #define WIN32_PIPE_TO_PROCESSES
70 #endif
71
72 /* === G L O B A L S ======================================================= */
73
74 SYNFIG_TARGET_INIT(ffmpeg_trgt);
75 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
76 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
77 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
78 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
79
80 /* === M E T H O D S ======================================================= */
81
82 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
83 {
84         pid=-1;
85         file=NULL;
86         filename=Filename;
87         multi_image=false;
88         buffer=NULL;
89         color_buffer=0;
90         set_remove_alpha();
91 }
92
93 ffmpeg_trgt::~ffmpeg_trgt()
94 {
95         if(file)
96         {
97                 etl::yield();
98                 sleep(1);
99 #if defined(WIN32_PIPE_TO_PROCESSES)
100                 pclose(file);
101 #elif defined(UNIX_PIPE_TO_PROCESSES)
102                 fclose(file);
103                 int status;
104                 waitpid(pid,&status,0);
105 #endif
106         }
107         file=NULL;
108         delete [] buffer;
109         delete [] color_buffer;
110 }
111
112 bool
113 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
114 {
115         //given_desc->set_pixel_format(PF_RGB);
116
117         // Make sure that the width and height
118         // are multiples of 8
119         given_desc->set_w((given_desc->get_w()+4)/8*8);
120         given_desc->set_h((given_desc->get_h()+4)/8*8);
121
122         /*
123         // Valid framerates:
124         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
125         float fps=given_desc->get_frame_rate();
126         if(fps <24.0)
127                 given_desc->set_frame_rate(23.976);
128         if(fps>=24.0 && fps <25.0)
129                 given_desc->set_frame_rate(24);
130         if(fps>=25.0 && fps <29.97)
131                 given_desc->set_frame_rate(25);
132         if(fps>=29.97 && fps <30.0)
133                 given_desc->set_frame_rate(29.97);
134         if(fps>=29.97 && fps <30.0)
135                 given_desc->set_frame_rate(29.97);
136         if(fps>=30.0 && fps <50.0)
137                 given_desc->set_frame_rate(30.0);
138         if(fps>=50.0 && fps <59.94)
139                 given_desc->set_frame_rate(50);
140         if(fps>=59.94)
141                 given_desc->set_frame_rate(59.94);
142     */
143
144         desc=*given_desc;
145
146         return true;
147 }
148
149 bool
150 ffmpeg_trgt::init()
151 {
152         imagecount=desc.get_frame_start();
153         if(desc.get_frame_end()-desc.get_frame_start()>0)
154                 multi_image=true;
155
156 #if defined(WIN32_PIPE_TO_PROCESSES)
157
158         string command;
159
160         if( filename.c_str()[0] == '-' )
161                         command=strprintf("ffmpeg -f image2pipe -vcodec ppm -an -r %f -i pipe: -loop -hq -title \"%s\" -vcodec mpeg1video -y -- \"%s\"\n",desc.get_frame_rate(),get_canvas()->get_name().c_str(),filename.c_str());
162         else
163                         command=strprintf("ffmpeg -f image2pipe -vcodec ppm -an -r %f -i pipe: -loop -hq -title \"%s\" -vcodec mpeg1video -y \"%s\"\n",desc.get_frame_rate(),get_canvas()->get_name().c_str(),filename.c_str());
164                                 
165         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
166
167 #elif defined(UNIX_PIPE_TO_PROCESSES)
168
169         int p[2];
170   
171         if (pipe(p)) {
172                 synfig::error(_("Unable to open pipe to ffmpeg"));
173                 return false;
174         };
175   
176         pid = fork();
177   
178         if (pid == -1) {
179                 synfig::error(_("Unable to open pipe to ffmpeg"));
180                 return false;
181         }
182   
183         if (pid == 0){
184                 // Child process
185                 // Close pipeout, not needed
186                 close(p[1]);
187                 // Dup pipeout to stdin
188                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
189                         synfig::error(_("Unable to open pipe to ffmpeg"));
190                         return false;
191                 }
192                 // Close the unneeded pipeout
193                 close(p[0]);
194                 if( filename.c_str()[0] == '-' )
195                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec", "ppm", "-an", "-r", strprintf("%f", desc.get_frame_rate()).c_str(), "-i", "pipe:", "-loop", "-hq", "-title", get_canvas()->get_name().c_str(), "-vcodec", "mpeg1video", "-y", "--", filename.c_str(), (const char *)NULL);
196                 else
197                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec", "ppm", "-an", "-r", strprintf("%f", desc.get_frame_rate()).c_str(), "-i", "pipe:", "-loop", "-hq", "-title", get_canvas()->get_name().c_str(), "-vcodec", "mpeg1video", "-y", filename.c_str(), (const char *)NULL);
198                 // We should never reach here unless the exec failed
199                 synfig::error(_("Unable to open pipe to ffmpeg"));
200                 return false;
201         } else {
202                 // Parent process
203                 // Close pipein, not needed
204                 close(p[0]);
205                 // Save pipeout to file handle, will write to it later
206                 file = fdopen(p[1], "wb");
207         }
208
209 #else
210         #error There are no known APIs for creating child processes
211 #endif
212
213         // etl::yield();
214
215         if(!file)
216         {
217                 synfig::error(_("Unable to open pipe to ffmpeg"));
218                 return false;
219         }
220
221         return true;
222 }
223
224 void
225 ffmpeg_trgt::end_frame()
226 {
227         //fprintf(file, " ");
228         fflush(file);
229         imagecount++;
230 }
231
232 bool
233 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
234 {
235         int w=desc.get_w(),h=desc.get_h();
236
237         if(!file)
238                 return false;
239
240         fprintf(file, "P6\n");
241         fprintf(file, "%d %d\n", w, h);
242         fprintf(file, "%d\n", 255);
243
244         delete [] buffer;
245         buffer=new unsigned char[3*w];
246         delete [] color_buffer;
247         color_buffer=new Color[w];
248
249         return true;
250 }
251
252 Color *
253 ffmpeg_trgt::start_scanline(int /*scanline*/)
254 {
255         return color_buffer;
256 }
257
258 bool
259 ffmpeg_trgt::end_scanline()
260 {
261         if(!file)
262                 return false;
263
264         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
265
266         if(!fwrite(buffer,1,desc.get_w()*3,file))
267                 return false;
268
269         return true;
270 }