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