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>
48 /* === M A C R O S ========================================================= */
50 using namespace synfig;
54 /* === G L O B A L S ======================================================= */
56 SYNFIG_TARGET_INIT(ffmpeg_trgt);
57 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
58 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
59 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
60 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
62 /* === M E T H O D S ======================================================= */
64 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
75 ffmpeg_trgt::~ffmpeg_trgt()
83 waitpid(pid,&status,0);
87 delete [] color_buffer;
91 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
93 //given_desc->set_pixel_format(PF_RGB);
95 // Make sure that the width and height
97 given_desc->set_w((given_desc->get_w()+4)/8*8);
98 given_desc->set_h((given_desc->get_h()+4)/8*8);
102 // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
103 float fps=given_desc->get_frame_rate();
105 given_desc->set_frame_rate(23.976);
106 if(fps>=24.0 && fps <25.0)
107 given_desc->set_frame_rate(24);
108 if(fps>=25.0 && fps <29.97)
109 given_desc->set_frame_rate(25);
110 if(fps>=29.97 && fps <30.0)
111 given_desc->set_frame_rate(29.97);
112 if(fps>=29.97 && fps <30.0)
113 given_desc->set_frame_rate(29.97);
114 if(fps>=30.0 && fps <50.0)
115 given_desc->set_frame_rate(30.0);
116 if(fps>=50.0 && fps <59.94)
117 given_desc->set_frame_rate(50);
119 given_desc->set_frame_rate(59.94);
130 imagecount=desc.get_frame_start();
131 if(desc.get_frame_end()-desc.get_frame_start()>0)
137 synfig::error(_("Unable to open pipe to ffmpeg"));
144 synfig::error(_("Unable to open pipe to ffmpeg"));
150 // Close pipeout, not needed
152 // Dup pipeout to stdin
153 if( dup2( p[0], STDIN_FILENO ) == -1 ){
154 synfig::error(_("Unable to open pipe to ffmpeg"));
157 // Close the unneeded pipeout
159 if( filename.c_str()[0] == '-' )
160 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);
162 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);
163 // We should never reach here unless the exec failed
164 synfig::error(_("Unable to open pipe to ffmpeg"));
168 // Close pipein, not needed
170 // Save pipeout to file handle, will write to it later
171 file = fdopen(p[1], "wb");
178 synfig::error(_("Unable to open pipe to ffmpeg"));
186 ffmpeg_trgt::end_frame()
188 //fprintf(file, " ");
194 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
196 int w=desc.get_w(),h=desc.get_h();
201 fprintf(file, "P6\n");
202 fprintf(file, "%d %d\n", w, h);
203 fprintf(file, "%d\n", 255);
206 buffer=new unsigned char[3*w];
207 delete [] color_buffer;
208 color_buffer=new Color[w];
214 ffmpeg_trgt::start_scanline(int /*scanline*/)
220 ffmpeg_trgt::end_scanline()
225 convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
227 if(!fwrite(buffer,1,desc.get_w()*3,file))