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