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