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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === 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)
92 ffmpeg_trgt::~ffmpeg_trgt()
98 #if defined(WIN32_PIPE_TO_PROCESSES)
100 #elif defined(UNIX_PIPE_TO_PROCESSES)
103 waitpid(pid,&status,0);
108 delete [] color_buffer;
112 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
114 //given_desc->set_pixel_format(PF_RGB);
116 // Make sure that the width and height
117 // are multiples of 8
118 given_desc->set_w((given_desc->get_w()+4)/8*8);
119 given_desc->set_h((given_desc->get_h()+4)/8*8);
123 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
124 float fps=given_desc->get_frame_rate();
126 given_desc->set_frame_rate(23.976);
127 if(fps>=24.0 && fps <25.0)
128 given_desc->set_frame_rate(24);
129 if(fps>=25.0 && fps <29.97)
130 given_desc->set_frame_rate(25);
131 if(fps>=29.97 && fps <30.0)
132 given_desc->set_frame_rate(29.97);
133 if(fps>=29.97 && fps <30.0)
134 given_desc->set_frame_rate(29.97);
135 if(fps>=30.0 && fps <50.0)
136 given_desc->set_frame_rate(30.0);
137 if(fps>=50.0 && fps <59.94)
138 given_desc->set_frame_rate(50);
140 given_desc->set_frame_rate(59.94);
151 imagecount=desc.get_frame_start();
152 if(desc.get_frame_end()-desc.get_frame_start()>0)
155 #if defined(WIN32_PIPE_TO_PROCESSES)
159 if( filename.c_str()[0] == '-' )
160 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());
162 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());
164 file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
166 #elif defined(UNIX_PIPE_TO_PROCESSES)
171 synfig::error(_("Unable to open pipe to ffmpeg"));
178 synfig::error(_("Unable to open pipe to ffmpeg"));
184 // Close pipeout, not needed
186 // Dup pipeout to stdin
187 if( dup2( p[0], STDIN_FILENO ) == -1 ){
188 synfig::error(_("Unable to open pipe to ffmpeg"));
191 // Close the unneeded pipeout
193 if( filename.c_str()[0] == '-' )
194 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);
196 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 // We should never reach here unless the exec failed
198 synfig::error(_("Unable to open pipe to ffmpeg"));
202 // Close pipein, not needed
204 // Save pipeout to file handle, will write to it later
205 file = fdopen(p[1], "wb");
209 #error There are no known APIs for creating child processes
216 synfig::error(_("Unable to open pipe to ffmpeg"));
224 ffmpeg_trgt::end_frame()
226 //fprintf(file, " ");
232 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
234 int w=desc.get_w(),h=desc.get_h();
239 fprintf(file, "P6\n");
240 fprintf(file, "%d %d\n", w, h);
241 fprintf(file, "%d\n", 255);
244 buffer=new unsigned char[3*w];
245 delete [] color_buffer;
246 color_buffer=new Color[w];
252 ffmpeg_trgt::start_scanline(int /*scanline*/)
258 ffmpeg_trgt::end_scanline()
263 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
265 if(!fwrite(buffer,1,desc.get_w()*3,file))