Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / 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 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include <ETL/stringf>
36 #include "trgt_dv.h"
37 #include <stdio.h>
38 #include <sys/types.h>
39 #if HAVE_SYS_WAIT_H
40  #include <sys/wait.h>
41 #endif
42 #if HAVE_IO_H
43  #include <io.h>
44 #endif
45 #if HAVE_PROCESS_H
46  #include <process.h>
47 #endif
48 #if HAVE_FCNTL_H
49  #include <fcntl.h>
50 #endif
51 #include <unistd.h>
52 #include <algorithm>
53 #include <functional>
54 #include <ETL/clock>
55
56 #endif
57
58 /* === M A C R O S ========================================================= */
59
60 using namespace synfig;
61 using namespace std;
62 using namespace etl;
63
64 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
65  #define UNIX_PIPE_TO_PROCESSES
66 #else
67  #define WIN32_PIPE_TO_PROCESSES
68 #endif
69
70 /* === G L O B A L S ======================================================= */
71
72 SYNFIG_TARGET_INIT(dv_trgt);
73 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
74 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
75 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
76 SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
77
78 /* === M E T H O D S ======================================================= */
79
80
81 dv_trgt::dv_trgt(const char *Filename,
82                                  const synfig::TargetParam& /* params */)
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 #if defined(WIN32_PIPE_TO_PROCESSES)
98                 pclose(file);
99 #elif defined(UNIX_PIPE_TO_PROCESSES)
100                 fclose(file);
101                 int status;
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         string command;
154
155         if(wide_aspect)
156                 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
157         else
158                 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
159
160         // Open the pipe to encodedv
161         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
162
163         if(!file)
164         {
165                 synfig::error(_("Unable to open pipe to encodedv"));
166                 return false;
167         }
168
169 #elif defined(UNIX_PIPE_TO_PROCESSES)
170
171         int p[2];
172
173         if (pipe(p)) {
174                 synfig::error(_("Unable to open pipe to encodedv"));
175                 return false;
176         };
177
178         pid_t pid = fork();
179
180         if (pid == -1) {
181                 synfig::error(_("Unable to open pipe to encodedv"));
182                 return false;
183         }
184
185         if (pid == 0){
186                 // Child process
187                 // Close pipeout, not needed
188                 close(p[1]);
189                 // Dup pipeout to stdin
190                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
191                         synfig::error(_("Unable to open pipe to encodedv"));
192                         return false;
193                 }
194                 // Close the unneeded pipeout
195                 close(p[0]);
196                 // Open filename to stdout
197                 FILE* outfile = fopen(filename.c_str(),"wb");
198                 if( outfile == NULL ){
199                         synfig::error(_("Unable to open pipe to encodedv"));
200                         return false;
201                 }
202                 int outfilefd = fileno(outfile);
203                 if( outfilefd == -1 ){
204                         synfig::error(_("Unable to open pipe to encodedv"));
205                         return false;
206                 }
207                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
208                         synfig::error(_("Unable to open pipe to encodedv"));
209                         return false;
210                 }
211
212                 if(wide_aspect)
213                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
214                 else
215                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
216                 // We should never reach here unless the exec failed
217                 synfig::error(_("Unable to open pipe to encodedv"));
218                 return false;
219         } else {
220                 // Parent process
221                 // Close pipein, not needed
222                 close(p[0]);
223                 // Save pipeout to file handle, will write to it later
224                 file = fdopen(p[1], "wb");
225                 if (file == NULL) {
226                         synfig::error(_("Unable to open pipe to encodedv"));
227                         return false;
228                 }
229         }
230
231 #else
232         #error There are no known APIs for creating child processes
233 #endif
234
235
236         // Sleep for a moment to let the pipe catch up
237         etl::clock().sleep(0.25f);
238
239         return true;
240 }
241
242 void
243 dv_trgt::end_frame()
244 {
245         fprintf(file, " ");
246         fflush(file);
247         imagecount++;
248 }
249
250 bool
251 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
252 {
253         int w=desc.get_w(),h=desc.get_h();
254
255         if(!file)
256                 return false;
257
258         fprintf(file, "P6\n");
259         fprintf(file, "%d %d\n", w, h);
260         fprintf(file, "%d\n", 255);
261
262         delete [] buffer;
263         buffer=new unsigned char[3*w];
264
265         delete [] color_buffer;
266         color_buffer=new Color[w];
267
268         return true;
269 }
270
271 Color *
272 dv_trgt::start_scanline(int /*scanline*/)
273 {
274         return color_buffer;
275 }
276
277 bool
278 dv_trgt::end_scanline()
279 {
280         if(!file)
281                 return false;
282
283         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
284
285         if(!fwrite(buffer,1,desc.get_w()*3,file))
286                 return false;
287
288         return true;
289 }