Fix issue with r1181 and output filenames that begin with a dash.
[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 <algorithm>
40 #include <functional>
41 #include <ETL/clock>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_TARGET_INIT(ffmpeg_trgt);
54 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
55 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
56 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
57 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
58
59 /* === M E T H O D S ======================================================= */
60
61 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
62 {
63         file=NULL;
64         filename=Filename;
65         multi_image=false;
66         buffer=NULL;
67         color_buffer=0;
68         set_remove_alpha();
69 }
70
71 ffmpeg_trgt::~ffmpeg_trgt()
72 {
73         if(file)
74         {
75                 etl::yield();
76                 sleep(1);
77                 pclose(file);
78         }
79         file=NULL;
80         delete [] buffer;
81         delete [] color_buffer;
82 }
83
84 bool
85 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
86 {
87         //given_desc->set_pixel_format(PF_RGB);
88
89         // Make sure that the width and height
90         // are multiples of 8
91         given_desc->set_w((given_desc->get_w()+4)/8*8);
92         given_desc->set_h((given_desc->get_h()+4)/8*8);
93
94         /*
95         // Valid framerates:
96         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
97         float fps=given_desc->get_frame_rate();
98         if(fps <24.0)
99                 given_desc->set_frame_rate(23.976);
100         if(fps>=24.0 && fps <25.0)
101                 given_desc->set_frame_rate(24);
102         if(fps>=25.0 && fps <29.97)
103                 given_desc->set_frame_rate(25);
104         if(fps>=29.97 && fps <30.0)
105                 given_desc->set_frame_rate(29.97);
106         if(fps>=29.97 && fps <30.0)
107                 given_desc->set_frame_rate(29.97);
108         if(fps>=30.0 && fps <50.0)
109                 given_desc->set_frame_rate(30.0);
110         if(fps>=50.0 && fps <59.94)
111                 given_desc->set_frame_rate(50);
112         if(fps>=59.94)
113                 given_desc->set_frame_rate(59.94);
114     */
115
116         desc=*given_desc;
117
118         return true;
119 }
120
121 bool
122 ffmpeg_trgt::init()
123 {
124         imagecount=desc.get_frame_start();
125         if(desc.get_frame_end()-desc.get_frame_start()>0)
126                 multi_image=true;
127
128         int p[2];
129   
130         if (pipe(p)) {
131                 synfig::error(_("Unable to open pipe to ffmpeg"));
132                 return false;
133         };
134   
135         pid_t pid = fork();
136   
137         if (pid == -1) {
138                 synfig::error(_("Unable to open pipe to ffmpeg"));
139                 return false;
140         }
141   
142         if (pid == 0){
143                 // Child process
144                 // Dup pipeout to stdin
145                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
146                         synfig::error(_("Unable to open pipe to ffmpeg"));
147                         return false;
148                 }
149                 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);
150                 // We should never reach here unless the exec failed
151                 synfig::error(_("Unable to open pipe to ffmpeg"));
152                 return false;
153         } else {
154                 // Parent process
155                 // Close pipein, not needed
156                 close(p[0]);
157                 // Save pipeout to file handle, will write to it later
158                 file = fdopen(p[1], "wb");
159         }
160
161         // etl::yield();
162
163         if(!file)
164         {
165                 synfig::error(_("Unable to open pipe to ffmpeg"));
166                 return false;
167         }
168
169         return true;
170 }
171
172 void
173 ffmpeg_trgt::end_frame()
174 {
175         //fprintf(file, " ");
176         fflush(file);
177         imagecount++;
178 }
179
180 bool
181 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
182 {
183         int w=desc.get_w(),h=desc.get_h();
184
185         if(!file)
186                 return false;
187
188         fprintf(file, "P6\n");
189         fprintf(file, "%d %d\n", w, h);
190         fprintf(file, "%d\n", 255);
191
192         delete [] buffer;
193         buffer=new unsigned char[3*w];
194         delete [] color_buffer;
195         color_buffer=new Color[w];
196
197         return true;
198 }
199
200 Color *
201 ffmpeg_trgt::start_scanline(int /*scanline*/)
202 {
203         return color_buffer;
204 }
205
206 bool
207 ffmpeg_trgt::end_scanline()
208 {
209         if(!file)
210                 return false;
211
212         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
213
214         if(!fwrite(buffer,1,desc.get_w()*3,file))
215                 return false;
216
217         return true;
218 }