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
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
37 #include <ETL/stringf>
38 #include "trgt_ffmpeg.h"
40 #include <sys/types.h>
60 /* === M A C R O S ========================================================= */
62 using namespace synfig;
66 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
67 #define UNIX_PIPE_TO_PROCESSES
69 #define WIN32_PIPE_TO_PROCESSES
72 /* === G L O B A L S ======================================================= */
74 SYNFIG_TARGET_INIT(ffmpeg_trgt);
75 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
76 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
77 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
78 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
80 /* === M E T H O D S ======================================================= */
82 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
93 ffmpeg_trgt::~ffmpeg_trgt()
99 #if defined(WIN32_PIPE_TO_PROCESSES)
101 #elif defined(UNIX_PIPE_TO_PROCESSES)
104 waitpid(pid,&status,0);
109 delete [] color_buffer;
113 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
115 //given_desc->set_pixel_format(PF_RGB);
117 // Make sure that the width and height
118 // are multiples of 8
119 given_desc->set_w((given_desc->get_w()+4)/8*8);
120 given_desc->set_h((given_desc->get_h()+4)/8*8);
124 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
125 float fps=given_desc->get_frame_rate();
127 given_desc->set_frame_rate(23.976);
128 if(fps>=24.0 && fps <25.0)
129 given_desc->set_frame_rate(24);
130 if(fps>=25.0 && fps <29.97)
131 given_desc->set_frame_rate(25);
132 if(fps>=29.97 && fps <30.0)
133 given_desc->set_frame_rate(29.97);
134 if(fps>=29.97 && fps <30.0)
135 given_desc->set_frame_rate(29.97);
136 if(fps>=30.0 && fps <50.0)
137 given_desc->set_frame_rate(30.0);
138 if(fps>=50.0 && fps <59.94)
139 given_desc->set_frame_rate(50);
141 given_desc->set_frame_rate(59.94);
152 imagecount=desc.get_frame_start();
153 if(desc.get_frame_end()-desc.get_frame_start()>0)
156 #if defined(WIN32_PIPE_TO_PROCESSES)
160 if( filename.c_str()[0] == '-' )
161 command=strprintf("ffmpeg -f image2pipe -vcodec ppm -an -r %f -i pipe: -loop -hq -title \"%s\" -vcodec mpeg1video -y -- \"%s\"\n",desc.get_frame_rate(),get_canvas()->get_name().c_str(),filename.c_str());
163 command=strprintf("ffmpeg -f image2pipe -vcodec ppm -an -r %f -i pipe: -loop -hq -title \"%s\" -vcodec mpeg1video -y \"%s\"\n",desc.get_frame_rate(),get_canvas()->get_name().c_str(),filename.c_str());
165 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
167 #elif defined(UNIX_PIPE_TO_PROCESSES)
172 synfig::error(_("Unable to open pipe to ffmpeg"));
179 synfig::error(_("Unable to open pipe to ffmpeg"));
185 // Close pipeout, not needed
187 // Dup pipeout to stdin
188 if( dup2( p[0], STDIN_FILENO ) == -1 ){
189 synfig::error(_("Unable to open pipe to ffmpeg"));
192 // Close the unneeded pipeout
194 if( filename.c_str()[0] == '-' )
195 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec", "ppm", "-an", "-r", strprintf("%f", desc.get_frame_rate()).c_str(), "-i", "pipe:", "-loop", "-hq", "-title", get_canvas()->get_name().c_str(), "-vcodec", "mpeg1video", "-y", "--", filename.c_str(), (const char *)NULL);
197 execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec", "ppm", "-an", "-r", strprintf("%f", desc.get_frame_rate()).c_str(), "-i", "pipe:", "-loop", "-hq", "-title", get_canvas()->get_name().c_str(), "-vcodec", "mpeg1video", "-y", filename.c_str(), (const char *)NULL);
198 // We should never reach here unless the exec failed
199 synfig::error(_("Unable to open pipe to ffmpeg"));
203 // Close pipein, not needed
205 // Save pipeout to file handle, will write to it later
206 file = fdopen(p[1], "wb");
210 #error There are no known APIs for creating child processes
217 synfig::error(_("Unable to open pipe to ffmpeg"));
225 ffmpeg_trgt::end_frame()
227 //fprintf(file, " ");
233 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
235 int w=desc.get_w(),h=desc.get_h();
240 fprintf(file, "P6\n");
241 fprintf(file, "%d %d\n", w, h);
242 fprintf(file, "%d\n", 255);
245 buffer=new unsigned char[3*w];
246 delete [] color_buffer;
247 color_buffer=new Color[w];
253 ffmpeg_trgt::start_scanline(int /*scanline*/)
259 ffmpeg_trgt::end_scanline()
264 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
266 if(!fwrite(buffer,1,desc.get_w()*3,file))