New targets parameters class. Tool accepts video codec and bitrate parameters. All...
[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 -hq"
167                                                         " -title \"%s\" -vcodec %s -b %i"
168                                                         " -y -- \"%s\"\n",
169                                                         desc.get_frame_rate(),
170                                                         get_canvas()->get_name().c_str(),
171                                                         video_codec.c_str(), bitrate,
172                                                         filename.c_str());
173         else
174                 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
175                                                         " -r %f -i pipe: -loop -hq"
176                                                         " -title \"%s\" -vcodec %s -b %i"
177                                                         " -y -- \"%s\"\n",
178                                                         desc.get_frame_rate(),
179                                                         get_canvas()->get_name().c_str(),
180                                                         video_codec.c_str(), bitrate,
181                                                         filename.c_str());
182
183         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
184
185 #elif defined(UNIX_PIPE_TO_PROCESSES)
186
187         int p[2];
188
189         if (pipe(p)) {
190                 synfig::error(_("Unable to open pipe to ffmpeg"));
191                 return false;
192         };
193
194         pid = fork();
195
196         if (pid == -1) {
197                 synfig::error(_("Unable to open pipe to ffmpeg"));
198                 return false;
199         }
200
201         if (pid == 0){
202                 // Child process
203                 // Close pipeout, not needed
204                 close(p[1]);
205                 // Dup pipeout to stdin
206                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
207                         synfig::error(_("Unable to open pipe to ffmpeg"));
208                         return false;
209                 }
210                 // Close the unneeded pipeout
211                 close(p[0]);
212                 if( filename.c_str()[0] == '-' )
213                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
214                                    "ppm", "-an", "-r",
215                                    strprintf("%f", desc.get_frame_rate()).c_str(),
216                                    "-i", "pipe:", "-loop", "-hq",
217                                    "-title", get_canvas()->get_name().c_str(),
218                                    "-vcodec", video_codec.c_str(),
219                                    "-b", bitrate,
220                                    "-y", "--", filename.c_str(), (const char *)NULL);
221                 else
222                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
223                                    "ppm", "-an", "-r",
224                                    strprintf("%f", desc.get_frame_rate()).c_str(),
225                                    "-i", "pipe:", "-loop", "-hq",
226                                    "-title", get_canvas()->get_name().c_str(),
227                                    "-vcodec", video_codec.c_str(),
228                                    "-b", bitrate,
229                                    "-y", filename.c_str(), (const char *)NULL);
230
231                 // We should never reach here unless the exec failed
232                 synfig::error(_("Unable to open pipe to ffmpeg"));
233                 return false;
234         } else {
235                 // Parent process
236                 // Close pipein, not needed
237                 close(p[0]);
238                 // Save pipeout to file handle, will write to it later
239                 file = fdopen(p[1], "wb");
240         }
241
242 #else
243         #error There are no known APIs for creating child processes
244 #endif
245
246         // etl::yield();
247
248         if(!file)
249         {
250                 synfig::error(_("Unable to open pipe to ffmpeg"));
251                 return false;
252         }
253
254         return true;
255 }
256
257 void
258 ffmpeg_trgt::end_frame()
259 {
260         //fprintf(file, " ");
261         fflush(file);
262         imagecount++;
263 }
264
265 bool
266 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
267 {
268         int w=desc.get_w(),h=desc.get_h();
269
270         if(!file)
271                 return false;
272
273         fprintf(file, "P6\n");
274         fprintf(file, "%d %d\n", w, h);
275         fprintf(file, "%d\n", 255);
276
277         delete [] buffer;
278         buffer=new unsigned char[3*w];
279         delete [] color_buffer;
280         color_buffer=new Color[w];
281
282         return true;
283 }
284
285 Color *
286 ffmpeg_trgt::start_scanline(int /*scanline*/)
287 {
288         return color_buffer;
289 }
290
291 bool
292 ffmpeg_trgt::end_scanline()
293 {
294         if(!file)
295                 return false;
296
297         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
298
299         if(!fwrite(buffer,1,desc.get_w()*3,file))
300                 return false;
301
302         return true;
303 }