1 /* === S Y N F I G ========================================================= */
2 /*! \file trgt_ffmpeg.cpp
3 ** \brief ppm Target Module
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
10 ** Copyright (c) 2010 Diego Barrios Romero
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.
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.
23 ** === N O T E S ===========================================================
25 ** ========================================================================= */
27 /* === H E A D E R S ======================================================= */
36 #include <ETL/stringf>
37 #include "trgt_ffmpeg.h"
39 #include <sys/types.h>
59 /* === M A C R O S ========================================================= */
61 using namespace synfig;
65 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
66 #define UNIX_PIPE_TO_PROCESSES
68 #define WIN32_PIPE_TO_PROCESSES
71 /* === G L O B A L S ======================================================= */
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$");
79 /* === M E T H O D S ======================================================= */
81 ffmpeg_trgt::ffmpeg_trgt(const char *Filename,
82 const synfig::TargetParam& params)
92 // Set default video codec and bitrate if they weren't given.
93 if (params.video_codec == "none")
94 video_codec = "mpeg1video";
96 video_codec = params.video_codec;
98 if (params.bitrate == -1)
101 bitrate = params.bitrate;
104 ffmpeg_trgt::~ffmpeg_trgt()
110 #if defined(WIN32_PIPE_TO_PROCESSES)
112 #elif defined(UNIX_PIPE_TO_PROCESSES)
115 waitpid(pid,&status,0);
120 delete [] color_buffer;
124 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
126 //given_desc->set_pixel_format(PF_RGB);
128 // Make sure that the width and height
129 // are multiples of 8
130 given_desc->set_w((given_desc->get_w()+4)/8*8);
131 given_desc->set_h((given_desc->get_h()+4)/8*8);
135 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
136 float fps=given_desc->get_frame_rate();
138 given_desc->set_frame_rate(23.976);
139 if(fps>=24.0 && fps <25.0)
140 given_desc->set_frame_rate(24);
141 if(fps>=25.0 && fps <29.97)
142 given_desc->set_frame_rate(25);
143 if(fps>=29.97 && fps <30.0)
144 given_desc->set_frame_rate(29.97);
145 if(fps>=29.97 && fps <30.0)
146 given_desc->set_frame_rate(29.97);
147 if(fps>=30.0 && fps <50.0)
148 given_desc->set_frame_rate(30.0);
149 if(fps>=50.0 && fps <59.94)
150 given_desc->set_frame_rate(50);
152 given_desc->set_frame_rate(59.94);
163 imagecount=desc.get_frame_start();
164 if(desc.get_frame_end()-desc.get_frame_start()>0)
167 #if defined(WIN32_PIPE_TO_PROCESSES)
171 if( filename.c_str()[0] == '-' )
172 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
173 " -r %f -i pipe: -loop_input"
174 " -metadata title=\"%s\" "
177 desc.get_frame_rate(),
178 get_canvas()->get_name().c_str(),
179 video_codec.c_str(), bitrate,
182 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
183 " -r %f -i pipe: -loop_input"
184 " -metadata title=\"%s\" "
187 desc.get_frame_rate(),
188 get_canvas()->get_name().c_str(),
189 video_codec.c_str(), bitrate,
192 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
194 #elif defined(UNIX_PIPE_TO_PROCESSES)
199 synfig::error(_("Unable to open pipe to ffmpeg"));
206 synfig::error(_("Unable to open pipe to ffmpeg"));
212 // Close pipeout, not needed
214 // Dup pipeout to stdin
215 if( dup2( p[0], STDIN_FILENO ) == -1 ){
216 synfig::error(_("Unable to open pipe to ffmpeg"));
219 // Close the unneeded pipeout
221 if( filename.c_str()[0] == '-' )
223 // x264 codec needs -vpre hq parameters
224 if (video_codec == "libx264")
225 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
227 strprintf("%f", desc.get_frame_rate()).c_str(),
228 "-i", "pipe:", "-loop_input", "-metadata",
229 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
230 "-vcodec", video_codec.c_str(),
231 "-b", strprintf("%ik", bitrate).c_str(),
233 "-y", "--", filename.c_str(), (const char *)NULL);
235 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
237 strprintf("%f", desc.get_frame_rate()).c_str(),
238 "-i", "pipe:", "-loop_input", "-metadata",
239 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
240 "-vcodec", video_codec.c_str(),
241 "-b", strprintf("%ik", bitrate).c_str(),
242 "-y", "--", filename.c_str(), (const char *)NULL);
246 if (video_codec == "libx264")
247 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
249 strprintf("%f", desc.get_frame_rate()).c_str(),
250 "-i", "pipe:", "-loop_input",
252 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
253 "-vcodec", video_codec.c_str(),
254 "-b", strprintf("%ik", bitrate).c_str(),
256 "-y", filename.c_str(), (const char *)NULL);
258 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
260 strprintf("%f", desc.get_frame_rate()).c_str(),
261 "-i", "pipe:", "-loop_input",
263 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
264 "-vcodec", video_codec.c_str(),
265 "-b", strprintf("%ik", bitrate).c_str(),
266 "-y", filename.c_str(), (const char *)NULL);
269 // We should never reach here unless the exec failed
270 synfig::error(_("Unable to open pipe to ffmpeg"));
274 // Close pipein, not needed
276 // Save pipeout to file handle, will write to it later
277 file = fdopen(p[1], "wb");
281 #error There are no known APIs for creating child processes
288 synfig::error(_("Unable to open pipe to ffmpeg"));
296 ffmpeg_trgt::end_frame()
298 //fprintf(file, " ");
304 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
306 int w=desc.get_w(),h=desc.get_h();
311 fprintf(file, "P6\n");
312 fprintf(file, "%d %d\n", w, h);
313 fprintf(file, "%d\n", 255);
316 buffer=new unsigned char[3*w];
317 delete [] color_buffer;
318 color_buffer=new Color[w];
324 ffmpeg_trgt::start_scanline(int /*scanline*/)
330 ffmpeg_trgt::end_scanline()
335 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
337 if(!fwrite(buffer,1,desc.get_w()*3,file))