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 ======================================================= */
38 #include <ETL/stringf>
39 #include "trgt_ffmpeg.h"
41 #include <sys/types.h>
61 /* === M A C R O S ========================================================= */
63 using namespace synfig;
67 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
68 #define UNIX_PIPE_TO_PROCESSES
70 #define WIN32_PIPE_TO_PROCESSES
73 /* === G L O B A L S ======================================================= */
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$");
81 /* === M E T H O D S ======================================================= */
83 ffmpeg_trgt::ffmpeg_trgt(const char *Filename,
84 const synfig::TargetParam& params)
94 // Set default video codec and bitrate if they weren't given.
95 if (params.video_codec == "none")
96 video_codec = "mpeg1video";
98 video_codec = params.video_codec;
100 if (params.bitrate == -1)
103 bitrate = params.bitrate;
106 ffmpeg_trgt::~ffmpeg_trgt()
112 #if defined(WIN32_PIPE_TO_PROCESSES)
114 #elif defined(UNIX_PIPE_TO_PROCESSES)
117 waitpid(pid,&status,0);
122 delete [] color_buffer;
126 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
128 //given_desc->set_pixel_format(PF_RGB);
130 // Make sure that the width and height
131 // are multiples of 8
132 given_desc->set_w((given_desc->get_w()+4)/8*8);
133 given_desc->set_h((given_desc->get_h()+4)/8*8);
137 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
138 float fps=given_desc->get_frame_rate();
140 given_desc->set_frame_rate(23.976);
141 if(fps>=24.0 && fps <25.0)
142 given_desc->set_frame_rate(24);
143 if(fps>=25.0 && fps <29.97)
144 given_desc->set_frame_rate(25);
145 if(fps>=29.97 && fps <30.0)
146 given_desc->set_frame_rate(29.97);
147 if(fps>=29.97 && fps <30.0)
148 given_desc->set_frame_rate(29.97);
149 if(fps>=30.0 && fps <50.0)
150 given_desc->set_frame_rate(30.0);
151 if(fps>=50.0 && fps <59.94)
152 given_desc->set_frame_rate(50);
154 given_desc->set_frame_rate(59.94);
165 imagecount=desc.get_frame_start();
166 if(desc.get_frame_end()-desc.get_frame_start()>0)
169 #if defined(WIN32_PIPE_TO_PROCESSES)
173 if( filename.c_str()[0] == '-' )
174 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
175 " -r %f -i pipe: -loop_input"
176 " -metadata title=\"%s\" "
179 desc.get_frame_rate(),
180 get_canvas()->get_name().c_str(),
181 video_codec.c_str(), bitrate,
184 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
185 " -r %f -i pipe: -loop_input"
186 " -metadata title=\"%s\" "
189 desc.get_frame_rate(),
190 get_canvas()->get_name().c_str(),
191 video_codec.c_str(), bitrate,
194 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
196 #elif defined(UNIX_PIPE_TO_PROCESSES)
201 synfig::error(_("Unable to open pipe to ffmpeg"));
208 synfig::error(_("Unable to open pipe to ffmpeg"));
214 // Close pipeout, not needed
216 // Dup pipeout to stdin
217 if( dup2( p[0], STDIN_FILENO ) == -1 ){
218 synfig::error(_("Unable to open pipe to ffmpeg"));
221 // Close the unneeded pipeout
223 if( filename.c_str()[0] == '-' )
225 // x264 codec needs -vpre hq parameters
226 if (video_codec == "libx264")
227 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
229 strprintf("%f", desc.get_frame_rate()).c_str(),
230 "-i", "pipe:", "-loop_input", "-metadata",
231 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
232 "-vcodec", video_codec.c_str(),
233 "-b", strprintf("%ik", bitrate).c_str(),
235 "-y", "--", filename.c_str(), (const char *)NULL);
237 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
239 strprintf("%f", desc.get_frame_rate()).c_str(),
240 "-i", "pipe:", "-loop_input", "-metadata",
241 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
242 "-vcodec", video_codec.c_str(),
243 "-b", strprintf("%ik", bitrate).c_str(),
244 "-y", "--", filename.c_str(), (const char *)NULL);
248 if (video_codec == "libx264")
249 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
251 strprintf("%f", desc.get_frame_rate()).c_str(),
252 "-i", "pipe:", "-loop_input",
254 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
255 "-vcodec", video_codec.c_str(),
256 "-b", strprintf("%ik", bitrate).c_str(),
258 "-y", filename.c_str(), (const char *)NULL);
260 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
262 strprintf("%f", desc.get_frame_rate()).c_str(),
263 "-i", "pipe:", "-loop_input",
265 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
266 "-vcodec", video_codec.c_str(),
267 "-b", strprintf("%ik", bitrate).c_str(),
268 "-y", filename.c_str(), (const char *)NULL);
271 // We should never reach here unless the exec failed
272 synfig::error(_("Unable to open pipe to ffmpeg"));
276 // Close pipein, not needed
278 // Save pipeout to file handle, will write to it later
279 file = fdopen(p[1], "wb");
283 #error There are no known APIs for creating child processes
290 synfig::error(_("Unable to open pipe to ffmpeg"));
298 ffmpeg_trgt::end_frame()
300 //fprintf(file, " ");
306 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
308 int w=desc.get_w(),h=desc.get_h();
313 fprintf(file, "P6\n");
314 fprintf(file, "%d %d\n", w, h);
315 fprintf(file, "%d\n", 255);
318 buffer=new unsigned char[3*w];
319 delete [] color_buffer;
320 color_buffer=new Color[w];
326 ffmpeg_trgt::start_scanline(int /*scanline*/)
332 ffmpeg_trgt::end_scanline()
337 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
339 if(!fwrite(buffer,1,desc.get_w()*3,file))