Port the security fixes to Windows. Thanks to PXEGeek for much patience and testing...
[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 <sys/types.h>
40 #if HAVE_SYS_WAIT_H
41  #include <sys/wait.h>
42 #endif
43 #if HAVE_IO_H
44  #include <io.h>
45 #endif
46 #if HAVE_PROCESS_H
47  #include <process.h>
48 #endif
49 #if HAVE_FCNTL_H
50  #include <fcntl.h>
51 #endif
52 #include <unistd.h>
53 #include <algorithm>
54 #include <functional>
55 #include <ETL/clock>
56
57 #endif
58
59 /* === M A C R O S ========================================================= */
60
61 using namespace synfig;
62 using namespace std;
63 using namespace etl;
64
65 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
66  #define UNIX_PIPE_TO_PROCESSES
67 #elif defined(HAVE__SPAWNLP) && defined(HAVE__PIPE) && defined(HAVE_CWAIT)
68  #define WIN32_PIPE_TO_PROCESSES
69 #endif
70
71 /* === G L O B A L S ======================================================= */
72
73 SYNFIG_TARGET_INIT(dv_trgt);
74 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
75 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
76 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
77 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
78
79 /* === M E T H O D S ======================================================= */
80
81
82 dv_trgt::dv_trgt(const char *Filename)
83 {
84         pid=-1;
85         file=NULL;
86         filename=Filename;
87         buffer=NULL;
88         wide_aspect=false;
89         color_buffer=0;
90                 set_remove_alpha();
91
92 }
93
94 dv_trgt::~dv_trgt()
95 {
96         if(file){
97                 fclose(file);
98                 int status;
99 #if defined(WIN32_PIPE_TO_PROCESSES)
100                 cwait(&status,pid,0);
101 #elif defined(UNIX_PIPE_TO_PROCESSES)
102                 waitpid(pid,&status,0);
103 #endif
104         }
105         file=NULL;
106         delete [] buffer;
107         delete [] color_buffer;
108 }
109
110 bool
111 dv_trgt::set_rend_desc(RendDesc *given_desc)
112 {
113         // Set the aspect ratio
114         if(wide_aspect)
115         {
116                 // 16:9 Aspect
117                 given_desc->set_wh(160,90);
118
119                 // Widescreen should be progressive scan
120                 given_desc->set_interlaced(false);
121         }
122         else
123         {
124                 // 4:3 Aspect
125                 given_desc->set_wh(400,300);
126
127                 // We should be interlaced
128                 given_desc->set_interlaced(true);
129         }
130
131         // but the pixel res should be 720x480
132         given_desc->clear_flags(),given_desc->set_wh(720,480);
133
134         // NTSC Frame rate is 29.97
135         given_desc->set_frame_rate(29.97);
136
137         // The pipe to encodedv is PPM, which needs RGB data
138         //given_desc->set_pixel_format(PF_RGB);
139
140         // Set the description
141         desc=*given_desc;
142
143         return true;
144 }
145
146 bool
147 dv_trgt::init()
148 {
149         imagecount=desc.get_frame_start();
150
151 #if defined(WIN32_PIPE_TO_PROCESSES)
152
153         int p[2];
154         int stdin_fileno, stdout_fileno;
155
156         if(_pipe(p, 512, O_BINARY | O_NOINHERIT) < 0) {
157                 synfig::error(_("Unable to open pipe to encodedv"));
158                 return false;
159         }
160
161         // Save stdin/stdout so we can restore them later
162         stdin_fileno  = _dup(_fileno(stdin));
163         stdout_fileno = _dup(_fileno(stdout));
164
165         // encodedv should read from the pipe
166         if(_dup2(p[0], _fileno(stdin)) != 0) {
167                 synfig::error(_("Unable to open pipe to encodedv"));
168                 return false;
169         }
170
171         FILE* outfile = fopen(filename.c_str(),"wb");
172         if( outfile == NULL ){
173                 synfig::error(_("Unable to open pipe to encodedv"));
174                 return false;
175         }
176         if(_dup2(_fileno(outfile), _fileno(stdout)) != 0) {
177                 synfig::error(_("Unable to open pipe to encodedv"));
178                 return false;
179         }
180
181         if(wide_aspect)
182                 pid = _spawnlp(_P_NOWAIT, "encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
183         else
184                 pid = _spawnlp(_P_NOWAIT, "encodedv", "encodedv", "-", (const char *)NULL);
185
186         if( pid < 0) {
187                 synfig::error(_("Unable to open pipe to encodedv"));
188                 return false;
189         }
190
191         // Restore stdin/stdout
192         if(_dup2(stdin_fileno, _fileno(stdin)) != 0) {
193                 synfig::error(_("Unable to open pipe to encodedv"));
194                 return false;
195         }
196         if(_dup2(stdout_fileno, _fileno(stdout)) != 0) {
197                 synfig::error(_("Unable to open pipe to encodedv"));
198                 return false;
199         }
200         close(stdin_fileno);
201         close(stdout_fileno);
202
203         // Close the pipe read end - encodedv uses it
204         close(p[0]);
205         
206         // We write data to the write end of the pipe
207         file = fdopen(p[1], "wb");
208
209 #elif defined(UNIX_PIPE_TO_PROCESSES)
210
211         int p[2];
212   
213         if (pipe(p)) {
214                 synfig::error(_("Unable to open pipe to encodedv"));
215                 return false;
216         };
217   
218         pid_t pid = fork();
219   
220         if (pid == -1) {
221                 synfig::error(_("Unable to open pipe to encodedv"));
222                 return false;
223         }
224   
225         if (pid == 0){
226                 // Child process
227                 // Close pipeout, not needed
228                 close(p[1]);
229                 // Dup pipeout to stdin
230                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
231                         synfig::error(_("Unable to open pipe to encodedv"));
232                         return false;
233                 }
234                 // Close the unneeded pipeout
235                 close(p[0]);
236                 // Open filename to stdout
237                 FILE* outfile = fopen(filename.c_str(),"wb");
238                 if( outfile == NULL ){
239                         synfig::error(_("Unable to open pipe to encodedv"));
240                         return false;
241                 }
242                 int outfilefd = fileno(outfile);
243                 if( outfilefd == -1 ){
244                         synfig::error(_("Unable to open pipe to encodedv"));
245                         return false;
246                 }
247                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
248                         synfig::error(_("Unable to open pipe to encodedv"));
249                         return false;
250                 }
251                 
252                 if(wide_aspect)
253                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
254                 else
255                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
256                 // We should never reach here unless the exec failed
257                 synfig::error(_("Unable to open pipe to encodedv"));
258                 return false;
259         } else {
260                 // Parent process
261                 // Close pipein, not needed
262                 close(p[0]);
263                 // Save pipeout to file handle, will write to it later
264                 file = fdopen(p[1], "wb");
265                 if (file == NULL) {
266                         synfig::error(_("Unable to open pipe to encodedv"));
267                         return false;
268                 }
269         }
270
271 #else
272         #error There are no known APIs for creating child processes
273 #endif
274
275
276         // Sleep for a moment to let the pipe catch up
277         etl::clock().sleep(0.25f);
278
279         return true;
280 }
281
282 void
283 dv_trgt::end_frame()
284 {
285         fprintf(file, " ");
286         fflush(file);
287         imagecount++;
288 }
289
290 bool
291 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
292 {
293         int w=desc.get_w(),h=desc.get_h();
294
295         if(!file)
296                 return false;
297
298         fprintf(file, "P6\n");
299         fprintf(file, "%d %d\n", w, h);
300         fprintf(file, "%d\n", 255);
301
302         delete [] buffer;
303         buffer=new unsigned char[3*w];
304
305         delete [] color_buffer;
306         color_buffer=new Color[w];
307
308         return true;
309 }
310
311 Color *
312 dv_trgt::start_scanline(int /*scanline*/)
313 {
314         return color_buffer;
315 }
316
317 bool
318 dv_trgt::end_scanline()
319 {
320         if(!file)
321                 return false;
322
323         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
324
325         if(!fwrite(buffer,1,desc.get_w()*3,file))
326                 return false;
327
328         return true;
329 }