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 -hq"
167 " -title \"%s\" -vcodec %s -b %i"
169 desc.get_frame_rate(),
170 get_canvas()->get_name().c_str(),
171 video_codec.c_str(), bitrate,
174 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
175 " -r %f -i pipe: -loop -hq"
176 " -title \"%s\" -vcodec %s -b %i"
178 desc.get_frame_rate(),
179 get_canvas()->get_name().c_str(),
180 video_codec.c_str(), bitrate,
183 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
185 #elif defined(UNIX_PIPE_TO_PROCESSES)
190 synfig::error(_("Unable to open pipe to ffmpeg"));
197 synfig::error(_("Unable to open pipe to ffmpeg"));
203 // Close pipeout, not needed
205 // Dup pipeout to stdin
206 if( dup2( p[0], STDIN_FILENO ) == -1 ){
207 synfig::error(_("Unable to open pipe to ffmpeg"));
210 // Close the unneeded pipeout
212 if( filename.c_str()[0] == '-' )
213 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
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(),
220 "-y", "--", filename.c_str(), (const char *)NULL);
222 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
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(),
229 "-y", filename.c_str(), (const char *)NULL);
231 // We should never reach here unless the exec failed
232 synfig::error(_("Unable to open pipe to ffmpeg"));
236 // Close pipein, not needed
238 // Save pipeout to file handle, will write to it later
239 file = fdopen(p[1], "wb");
243 #error There are no known APIs for creating child processes
250 synfig::error(_("Unable to open pipe to ffmpeg"));
258 ffmpeg_trgt::end_frame()
260 //fprintf(file, " ");
266 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
268 int w=desc.get_w(),h=desc.get_h();
273 fprintf(file, "P6\n");
274 fprintf(file, "%d %d\n", w, h);
275 fprintf(file, "%d\n", 255);
278 buffer=new unsigned char[3*w];
279 delete [] color_buffer;
280 color_buffer=new Color[w];
286 ffmpeg_trgt::start_scanline(int /*scanline*/)
292 ffmpeg_trgt::end_scanline()
297 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
299 if(!fwrite(buffer,1,desc.get_w()*3,file))