Minor security fix: mod_dv used popen to run the DV encoder, change it to use pipe...
[synfig.git] / synfig-core / trunk / src / modules / mod_dv / trgt_dv.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_dv.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_dv.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(dv_trgt);
54 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
55 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
56 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
57 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
58
59 /* === M E T H O D S ======================================================= */
60
61
62 dv_trgt::dv_trgt(const char *Filename)
63 {
64         file=NULL;
65         filename=Filename;
66         buffer=NULL;
67         wide_aspect=false;
68         color_buffer=0;
69                 set_remove_alpha();
70
71 }
72
73 dv_trgt::~dv_trgt()
74 {
75         if(file)
76                 pclose(file);
77         file=NULL;
78         delete [] buffer;
79         delete [] color_buffer;
80 }
81
82 bool
83 dv_trgt::set_rend_desc(RendDesc *given_desc)
84 {
85         // Set the aspect ratio
86         if(wide_aspect)
87         {
88                 // 16:9 Aspect
89                 given_desc->set_wh(160,90);
90
91                 // Widescreen should be progressive scan
92                 given_desc->set_interlaced(false);
93         }
94         else
95         {
96                 // 4:3 Aspect
97                 given_desc->set_wh(400,300);
98
99                 // We should be interlaced
100                 given_desc->set_interlaced(true);
101         }
102
103         // but the pixel res should be 720x480
104         given_desc->clear_flags(),given_desc->set_wh(720,480);
105
106         // NTSC Frame rate is 29.97
107         given_desc->set_frame_rate(29.97);
108
109         // The pipe to encodedv is PPM, which needs RGB data
110         //given_desc->set_pixel_format(PF_RGB);
111
112         // Set the description
113         desc=*given_desc;
114
115         return true;
116 }
117
118 bool
119 dv_trgt::init()
120 {
121         imagecount=desc.get_frame_start();
122
123         string command;
124
125         int p[2];
126   
127         if (pipe(p)) {
128                 synfig::error(_("Unable to open pipe to encodedv"));
129                 return false;
130         };
131   
132         pid_t pid = fork();
133   
134         if (pid == -1) {
135                 synfig::error(_("Unable to open pipe to encodedv"));
136                 return false;
137         }
138   
139         if (pid == 0){
140                 // Child process
141                 // Dup pipeout to stdin
142                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
143                         synfig::error(_("Unable to open pipe to encodedv"));
144                         return false;
145                 }
146                 
147                 // Open filename to stdout
148                 FILE* outfile = fopen(filename.c_str(),"wb");
149                 if( outfile == NULL ){
150                         synfig::error(_("Unable to open pipe to encodedv"));
151                         return false;
152                 }
153                 int outfilefd = fileno(outfile);
154                 if( outfilefd == -1 ){
155                         synfig::error(_("Unable to open pipe to encodedv"));
156                         return false;
157                 }
158                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
159                         synfig::error(_("Unable to open pipe to encodedv"));
160                         return false;
161                 }
162                 
163                 if(wide_aspect)
164                         execlp("encodedv", "encodedv", "-w", "1", "-");
165                 else
166                         execlp("encodedv", "encodedv", "-");
167                 // We should never reach here unless the exec failed
168                 synfig::error(_("Unable to open pipe to encodedv"));
169                 return false;
170         } else {
171                 // Parent process
172                 // Close pipein, not needed
173                 close(p[0]);
174                 // Save pipeout to file handle, will write to it later
175                 file = fdopen(p[1], "wb");
176                 if (file == NULL) {
177                         synfig::error(_("Unable to open pipe to encodedv"));
178                         return false;
179                 }
180         }
181
182         // Sleep for a moment to let the pipe catch up
183         etl::clock().sleep(0.25f);
184
185         return true;
186 }
187
188 void
189 dv_trgt::end_frame()
190 {
191         fprintf(file, " ");
192         fflush(file);
193         imagecount++;
194 }
195
196 bool
197 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
198 {
199         int w=desc.get_w(),h=desc.get_h();
200
201         if(!file)
202                 return false;
203
204         fprintf(file, "P6\n");
205         fprintf(file, "%d %d\n", w, h);
206         fprintf(file, "%d\n", 255);
207
208         delete [] buffer;
209         buffer=new unsigned char[3*w];
210
211         delete [] color_buffer;
212         color_buffer=new Color[w];
213
214         return true;
215 }
216
217 Color *
218 dv_trgt::start_scanline(int /*scanline*/)
219 {
220         return color_buffer;
221 }
222
223 bool
224 dv_trgt::end_scanline()
225 {
226         if(!file)
227                 return false;
228
229         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
230
231         if(!fwrite(buffer,1,desc.get_w()*3,file))
232                 return false;
233
234         return true;
235 }