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)
93 video_codec = params.video_codec;
94 bitrate = params.bitrate;
97 ffmpeg_trgt::~ffmpeg_trgt()
103 #if defined(WIN32_PIPE_TO_PROCESSES)
105 #elif defined(UNIX_PIPE_TO_PROCESSES)
108 waitpid(pid,&status,0);
113 delete [] color_buffer;
117 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
119 //given_desc->set_pixel_format(PF_RGB);
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);
128 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
129 float fps=given_desc->get_frame_rate();
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);
145 given_desc->set_frame_rate(59.94);
156 imagecount=desc.get_frame_start();
157 if(desc.get_frame_end()-desc.get_frame_start()>0)
160 #if defined(WIN32_PIPE_TO_PROCESSES)
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\" "
170 desc.get_frame_rate(),
171 get_canvas()->get_name().c_str(),
172 video_codec.c_str(), bitrate,
175 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
176 " -r %f -i pipe: -loop_input"
177 " -metadata title=\"%s\" "
180 desc.get_frame_rate(),
181 get_canvas()->get_name().c_str(),
182 video_codec.c_str(), bitrate,
185 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
187 #elif defined(UNIX_PIPE_TO_PROCESSES)
192 synfig::error(_("Unable to open pipe to ffmpeg"));
199 synfig::error(_("Unable to open pipe to ffmpeg"));
205 // Close pipeout, not needed
207 // Dup pipeout to stdin
208 if( dup2( p[0], STDIN_FILENO ) == -1 ){
209 synfig::error(_("Unable to open pipe to ffmpeg"));
212 // Close the unneeded pipeout
214 if( filename.c_str()[0] == '-' )
215 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
217 strprintf("%f", desc.get_frame_rate()).c_str(),
218 "-i", "pipe:", "-loop_input",
220 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
221 "-vcodec", video_codec.c_str(),
222 "-b", strprintf("%ik", bitrate).c_str(),
223 "-y", "--", filename.c_str(), (const char *)NULL);
225 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
227 strprintf("%f", desc.get_frame_rate()).c_str(),
228 "-i", "pipe:", "-loop_input",
230 strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
231 "-vcodec", video_codec.c_str(),
232 "-b", strprintf("%ik", bitrate).c_str(),
233 "-y", filename.c_str(), (const char *)NULL);
235 // We should never reach here unless the exec failed
236 synfig::error(_("Unable to open pipe to ffmpeg"));
240 // Close pipein, not needed
242 // Save pipeout to file handle, will write to it later
243 file = fdopen(p[1], "wb");
247 #error There are no known APIs for creating child processes
254 synfig::error(_("Unable to open pipe to ffmpeg"));
262 ffmpeg_trgt::end_frame()
264 //fprintf(file, " ");
270 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
272 int w=desc.get_w(),h=desc.get_h();
277 fprintf(file, "P6\n");
278 fprintf(file, "%d %d\n", w, h);
279 fprintf(file, "%d\n", 255);
282 buffer=new unsigned char[3*w];
283 delete [] color_buffer;
284 color_buffer=new Color[w];
290 ffmpeg_trgt::start_scanline(int /*scanline*/)
296 ffmpeg_trgt::end_scanline()
301 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
303 if(!fwrite(buffer,1,desc.get_w()*3,file))