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