Fix some more issues with the mod_ffmpeg, mod_dv security patches
[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                 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);
160                 // We should never reach here unless the exec failed
161                 synfig::error(_("Unable to open pipe to ffmpeg"));
162                 return false;
163         } else {
164                 // Parent process
165                 // Close pipein, not needed
166                 close(p[0]);
167                 // Save pipeout to file handle, will write to it later
168                 file = fdopen(p[1], "wb");
169         }
170
171         // etl::yield();
172
173         if(!file)
174         {
175                 synfig::error(_("Unable to open pipe to ffmpeg"));
176                 return false;
177         }
178
179         return true;
180 }
181
182 void
183 ffmpeg_trgt::end_frame()
184 {
185         //fprintf(file, " ");
186         fflush(file);
187         imagecount++;
188 }
189
190 bool
191 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
192 {
193         int w=desc.get_w(),h=desc.get_h();
194
195         if(!file)
196                 return false;
197
198         fprintf(file, "P6\n");
199         fprintf(file, "%d %d\n", w, h);
200         fprintf(file, "%d\n", 255);
201
202         delete [] buffer;
203         buffer=new unsigned char[3*w];
204         delete [] color_buffer;
205         color_buffer=new Color[w];
206
207         return true;
208 }
209
210 Color *
211 ffmpeg_trgt::start_scanline(int /*scanline*/)
212 {
213         return color_buffer;
214 }
215
216 bool
217 ffmpeg_trgt::end_scanline()
218 {
219         if(!file)
220                 return false;
221
222         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
223
224         if(!fwrite(buffer,1,desc.get_w()*3,file))
225                 return false;
226
227         return true;
228 }