Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / 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 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include <ETL/stringf>
38 #include "trgt_dv.h"
39 #include <stdio.h>
40 #include <sys/types.h>
41 #if HAVE_SYS_WAIT_H
42  #include <sys/wait.h>
43 #endif
44 #if HAVE_IO_H
45  #include <io.h>
46 #endif
47 #if HAVE_PROCESS_H
48  #include <process.h>
49 #endif
50 #if HAVE_FCNTL_H
51  #include <fcntl.h>
52 #endif
53 #include <unistd.h>
54 #include <algorithm>
55 #include <functional>
56 #include <ETL/clock>
57
58 #endif
59
60 /* === M A C R O S ========================================================= */
61
62 using namespace synfig;
63 using namespace std;
64 using namespace etl;
65
66 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
67  #define UNIX_PIPE_TO_PROCESSES
68 #else
69  #define WIN32_PIPE_TO_PROCESSES
70 #endif
71
72 /* === G L O B A L S ======================================================= */
73
74 SYNFIG_TARGET_INIT(dv_trgt);
75 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
76 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
77 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
78 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
79
80 /* === M E T H O D S ======================================================= */
81
82
83 dv_trgt::dv_trgt(const char *Filename)
84 {
85         pid=-1;
86         file=NULL;
87         filename=Filename;
88         buffer=NULL;
89         wide_aspect=false;
90         color_buffer=0;
91                 set_remove_alpha();
92
93 }
94
95 dv_trgt::~dv_trgt()
96 {
97         if(file){
98 #if defined(WIN32_PIPE_TO_PROCESSES)
99                 pclose(file);
100 #elif defined(UNIX_PIPE_TO_PROCESSES)
101                 fclose(file);
102                 int status;
103                 waitpid(pid,&status,0);
104 #endif
105         }
106         file=NULL;
107         delete [] buffer;
108         delete [] color_buffer;
109 }
110
111 bool
112 dv_trgt::set_rend_desc(RendDesc *given_desc)
113 {
114         // Set the aspect ratio
115         if(wide_aspect)
116         {
117                 // 16:9 Aspect
118                 given_desc->set_wh(160,90);
119
120                 // Widescreen should be progressive scan
121                 given_desc->set_interlaced(false);
122         }
123         else
124         {
125                 // 4:3 Aspect
126                 given_desc->set_wh(400,300);
127
128                 // We should be interlaced
129                 given_desc->set_interlaced(true);
130         }
131
132         // but the pixel res should be 720x480
133         given_desc->clear_flags(),given_desc->set_wh(720,480);
134
135         // NTSC Frame rate is 29.97
136         given_desc->set_frame_rate(29.97);
137
138         // The pipe to encodedv is PPM, which needs RGB data
139         //given_desc->set_pixel_format(PF_RGB);
140
141         // Set the description
142         desc=*given_desc;
143
144         return true;
145 }
146
147 bool
148 dv_trgt::init()
149 {
150         imagecount=desc.get_frame_start();
151
152 #if defined(WIN32_PIPE_TO_PROCESSES)
153
154         string command;
155         
156         if(wide_aspect)
157                 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
158         else
159                 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
160
161         // Open the pipe to encodedv
162         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
163
164         if(!file)
165         {
166                 synfig::error(_("Unable to open pipe to encodedv"));
167                 return false;
168         }
169
170 #elif defined(UNIX_PIPE_TO_PROCESSES)
171
172         int p[2];
173   
174         if (pipe(p)) {
175                 synfig::error(_("Unable to open pipe to encodedv"));
176                 return false;
177         };
178   
179         pid_t pid = fork();
180   
181         if (pid == -1) {
182                 synfig::error(_("Unable to open pipe to encodedv"));
183                 return false;
184         }
185   
186         if (pid == 0){
187                 // Child process
188                 // Close pipeout, not needed
189                 close(p[1]);
190                 // Dup pipeout to stdin
191                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
192                         synfig::error(_("Unable to open pipe to encodedv"));
193                         return false;
194                 }
195                 // Close the unneeded pipeout
196                 close(p[0]);
197                 // Open filename to stdout
198                 FILE* outfile = fopen(filename.c_str(),"wb");
199                 if( outfile == NULL ){
200                         synfig::error(_("Unable to open pipe to encodedv"));
201                         return false;
202                 }
203                 int outfilefd = fileno(outfile);
204                 if( outfilefd == -1 ){
205                         synfig::error(_("Unable to open pipe to encodedv"));
206                         return false;
207                 }
208                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
209                         synfig::error(_("Unable to open pipe to encodedv"));
210                         return false;
211                 }
212                 
213                 if(wide_aspect)
214                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
215                 else
216                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
217                 // We should never reach here unless the exec failed
218                 synfig::error(_("Unable to open pipe to encodedv"));
219                 return false;
220         } else {
221                 // Parent process
222                 // Close pipein, not needed
223                 close(p[0]);
224                 // Save pipeout to file handle, will write to it later
225                 file = fdopen(p[1], "wb");
226                 if (file == NULL) {
227                         synfig::error(_("Unable to open pipe to encodedv"));
228                         return false;
229                 }
230         }
231
232 #else
233         #error There are no known APIs for creating child processes
234 #endif
235
236
237         // Sleep for a moment to let the pipe catch up
238         etl::clock().sleep(0.25f);
239
240         return true;
241 }
242
243 void
244 dv_trgt::end_frame()
245 {
246         fprintf(file, " ");
247         fflush(file);
248         imagecount++;
249 }
250
251 bool
252 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
253 {
254         int w=desc.get_w(),h=desc.get_h();
255
256         if(!file)
257                 return false;
258
259         fprintf(file, "P6\n");
260         fprintf(file, "%d %d\n", w, h);
261         fprintf(file, "%d\n", 255);
262
263         delete [] buffer;
264         buffer=new unsigned char[3*w];
265
266         delete [] color_buffer;
267         color_buffer=new Color[w];
268
269         return true;
270 }
271
272 Color *
273 dv_trgt::start_scanline(int /*scanline*/)
274 {
275         return color_buffer;
276 }
277
278 bool
279 dv_trgt::end_scanline()
280 {
281         if(!file)
282                 return false;
283
284         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
285
286         if(!fwrite(buffer,1,desc.get_w()*3,file))
287                 return false;
288
289         return true;
290 }