Remove command variable that is unused after the security fixes
[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         int p[2];
131   
132         if (pipe(p)) {
133                 synfig::error(_("Unable to open pipe to encodedv"));
134                 return false;
135         };
136   
137         pid_t pid = fork();
138   
139         if (pid == -1) {
140                 synfig::error(_("Unable to open pipe to encodedv"));
141                 return false;
142         }
143   
144         if (pid == 0){
145                 // Child process
146                 // Close pipeout, not needed
147                 close(p[1]);
148                 // Dup pipeout to stdin
149                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
150                         synfig::error(_("Unable to open pipe to encodedv"));
151                         return false;
152                 }
153                 // Close the unneeded pipeout
154                 close(p[0]);
155                 // Open filename to stdout
156                 FILE* outfile = fopen(filename.c_str(),"wb");
157                 if( outfile == NULL ){
158                         synfig::error(_("Unable to open pipe to encodedv"));
159                         return false;
160                 }
161                 int outfilefd = fileno(outfile);
162                 if( outfilefd == -1 ){
163                         synfig::error(_("Unable to open pipe to encodedv"));
164                         return false;
165                 }
166                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
167                         synfig::error(_("Unable to open pipe to encodedv"));
168                         return false;
169                 }
170                 
171                 if(wide_aspect)
172                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
173                 else
174                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
175                 // We should never reach here unless the exec failed
176                 synfig::error(_("Unable to open pipe to encodedv"));
177                 return false;
178         } else {
179                 // Parent process
180                 // Close pipein, not needed
181                 close(p[0]);
182                 // Save pipeout to file handle, will write to it later
183                 file = fdopen(p[1], "wb");
184                 if (file == NULL) {
185                         synfig::error(_("Unable to open pipe to encodedv"));
186                         return false;
187                 }
188         }
189
190         // Sleep for a moment to let the pipe catch up
191         etl::clock().sleep(0.25f);
192
193         return true;
194 }
195
196 void
197 dv_trgt::end_frame()
198 {
199         fprintf(file, " ");
200         fflush(file);
201         imagecount++;
202 }
203
204 bool
205 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
206 {
207         int w=desc.get_w(),h=desc.get_h();
208
209         if(!file)
210                 return false;
211
212         fprintf(file, "P6\n");
213         fprintf(file, "%d %d\n", w, h);
214         fprintf(file, "%d\n", 255);
215
216         delete [] buffer;
217         buffer=new unsigned char[3*w];
218
219         delete [] color_buffer;
220         color_buffer=new Color[w];
221
222         return true;
223 }
224
225 Color *
226 dv_trgt::start_scanline(int /*scanline*/)
227 {
228         return color_buffer;
229 }
230
231 bool
232 dv_trgt::end_scanline()
233 {
234         if(!file)
235                 return false;
236
237         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
238
239         if(!fwrite(buffer,1,desc.get_w()*3,file))
240                 return false;
241
242         return true;
243 }