Only use the double dash (--) to escape filenames that begin with a dash (-) when...
[synfig.git] / synfig-core / trunk / src / modules / mod_ffmpeg / trgt_ffmpeg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_ffmpeg.cpp
3 **      \brief ppm Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <ETL/stringf>
37 #include "trgt_ffmpeg.h"
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <algorithm>
43 #include <functional>
44 #include <ETL/clock>
45
46 #endif
47
48 /* === M A C R O S ========================================================= */
49
50 using namespace synfig;
51 using namespace std;
52 using namespace etl;
53
54 /* === G L O B A L S ======================================================= */
55
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$");
61
62 /* === M E T H O D S ======================================================= */
63
64 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
65 {
66         pid=-1;
67         file=NULL;
68         filename=Filename;
69         multi_image=false;
70         buffer=NULL;
71         color_buffer=0;
72         set_remove_alpha();
73 }
74
75 ffmpeg_trgt::~ffmpeg_trgt()
76 {
77         if(file)
78         {
79                 etl::yield();
80                 sleep(1);
81                 fclose(file);
82                 int status;
83                 waitpid(pid,&status,0);
84         }
85         file=NULL;
86         delete [] buffer;
87         delete [] color_buffer;
88 }
89
90 bool
91 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
92 {
93         //given_desc->set_pixel_format(PF_RGB);
94
95         // Make sure that the width and height
96         // are multiples of 8
97         given_desc->set_w((given_desc->get_w()+4)/8*8);
98         given_desc->set_h((given_desc->get_h()+4)/8*8);
99
100         /*
101         // Valid framerates:
102         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
103         float fps=given_desc->get_frame_rate();
104         if(fps <24.0)
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);
118         if(fps>=59.94)
119                 given_desc->set_frame_rate(59.94);
120     */
121
122         desc=*given_desc;
123
124         return true;
125 }
126
127 bool
128 ffmpeg_trgt::init()
129 {
130         imagecount=desc.get_frame_start();
131         if(desc.get_frame_end()-desc.get_frame_start()>0)
132                 multi_image=true;
133
134         int p[2];
135   
136         if (pipe(p)) {
137                 synfig::error(_("Unable to open pipe to ffmpeg"));
138                 return false;
139         };
140   
141         pid = fork();
142   
143         if (pid == -1) {
144                 synfig::error(_("Unable to open pipe to ffmpeg"));
145                 return false;
146         }
147   
148         if (pid == 0){
149                 // Child process
150                 // Close pipeout, not needed
151                 close(p[1]);
152                 // Dup pipeout to stdin
153                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
154                         synfig::error(_("Unable to open pipe to ffmpeg"));
155                         return false;
156                 }
157                 // Close the unneeded pipeout
158                 close(p[0]);
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);
161                 else
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"));
165                 return false;
166         } else {
167                 // Parent process
168                 // Close pipein, not needed
169                 close(p[0]);
170                 // Save pipeout to file handle, will write to it later
171                 file = fdopen(p[1], "wb");
172         }
173
174         // etl::yield();
175
176         if(!file)
177         {
178                 synfig::error(_("Unable to open pipe to ffmpeg"));
179                 return false;
180         }
181
182         return true;
183 }
184
185 void
186 ffmpeg_trgt::end_frame()
187 {
188         //fprintf(file, " ");
189         fflush(file);
190         imagecount++;
191 }
192
193 bool
194 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
195 {
196         int w=desc.get_w(),h=desc.get_h();
197
198         if(!file)
199                 return false;
200
201         fprintf(file, "P6\n");
202         fprintf(file, "%d %d\n", w, h);
203         fprintf(file, "%d\n", 255);
204
205         delete [] buffer;
206         buffer=new unsigned char[3*w];
207         delete [] color_buffer;
208         color_buffer=new Color[w];
209
210         return true;
211 }
212
213 Color *
214 ffmpeg_trgt::start_scanline(int /*scanline*/)
215 {
216         return color_buffer;
217 }
218
219 bool
220 ffmpeg_trgt::end_scanline()
221 {
222         if(!file)
223                 return false;
224
225         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
226
227         if(!fwrite(buffer,1,desc.get_w()*3,file))
228                 return false;
229
230         return true;
231 }