Merge branch 'nikitakit_master'
[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 #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                                  const synfig::TargetParam& /* params */)
85 {
86         pid=-1;
87         file=NULL;
88         filename=Filename;
89         buffer=NULL;
90         wide_aspect=false;
91         color_buffer=0;
92                 set_remove_alpha();
93
94 }
95
96 dv_trgt::~dv_trgt()
97 {
98         if(file){
99 #if defined(WIN32_PIPE_TO_PROCESSES)
100                 pclose(file);
101 #elif defined(UNIX_PIPE_TO_PROCESSES)
102                 fclose(file);
103                 int status;
104                 waitpid(pid,&status,0);
105 #endif
106         }
107         file=NULL;
108         delete [] buffer;
109         delete [] color_buffer;
110 }
111
112 bool
113 dv_trgt::set_rend_desc(RendDesc *given_desc)
114 {
115         // Set the aspect ratio
116         if(wide_aspect)
117         {
118                 // 16:9 Aspect
119                 given_desc->set_wh(160,90);
120
121                 // Widescreen should be progressive scan
122                 given_desc->set_interlaced(false);
123         }
124         else
125         {
126                 // 4:3 Aspect
127                 given_desc->set_wh(400,300);
128
129                 // We should be interlaced
130                 given_desc->set_interlaced(true);
131         }
132
133         // but the pixel res should be 720x480
134         given_desc->clear_flags(),given_desc->set_wh(720,480);
135
136         // NTSC Frame rate is 29.97
137         given_desc->set_frame_rate(29.97);
138
139         // The pipe to encodedv is PPM, which needs RGB data
140         //given_desc->set_pixel_format(PF_RGB);
141
142         // Set the description
143         desc=*given_desc;
144
145         return true;
146 }
147
148 bool
149 dv_trgt::init()
150 {
151         imagecount=desc.get_frame_start();
152
153 #if defined(WIN32_PIPE_TO_PROCESSES)
154
155         string command;
156
157         if(wide_aspect)
158                 command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
159         else
160                 command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
161
162         // Open the pipe to encodedv
163         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
164
165         if(!file)
166         {
167                 synfig::error(_("Unable to open pipe to encodedv"));
168                 return false;
169         }
170
171 #elif defined(UNIX_PIPE_TO_PROCESSES)
172
173         int p[2];
174
175         if (pipe(p)) {
176                 synfig::error(_("Unable to open pipe to encodedv"));
177                 return false;
178         };
179
180         pid_t pid = fork();
181
182         if (pid == -1) {
183                 synfig::error(_("Unable to open pipe to encodedv"));
184                 return false;
185         }
186
187         if (pid == 0){
188                 // Child process
189                 // Close pipeout, not needed
190                 close(p[1]);
191                 // Dup pipeout to stdin
192                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
193                         synfig::error(_("Unable to open pipe to encodedv"));
194                         return false;
195                 }
196                 // Close the unneeded pipeout
197                 close(p[0]);
198                 // Open filename to stdout
199                 FILE* outfile = fopen(filename.c_str(),"wb");
200                 if( outfile == NULL ){
201                         synfig::error(_("Unable to open pipe to encodedv"));
202                         return false;
203                 }
204                 int outfilefd = fileno(outfile);
205                 if( outfilefd == -1 ){
206                         synfig::error(_("Unable to open pipe to encodedv"));
207                         return false;
208                 }
209                 if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
210                         synfig::error(_("Unable to open pipe to encodedv"));
211                         return false;
212                 }
213
214                 if(wide_aspect)
215                         execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
216                 else
217                         execlp("encodedv", "encodedv", "-", (const char *)NULL);
218                 // We should never reach here unless the exec failed
219                 synfig::error(_("Unable to open pipe to encodedv"));
220                 return false;
221         } else {
222                 // Parent process
223                 // Close pipein, not needed
224                 close(p[0]);
225                 // Save pipeout to file handle, will write to it later
226                 file = fdopen(p[1], "wb");
227                 if (file == NULL) {
228                         synfig::error(_("Unable to open pipe to encodedv"));
229                         return false;
230                 }
231         }
232
233 #else
234         #error There are no known APIs for creating child processes
235 #endif
236
237
238         // Sleep for a moment to let the pipe catch up
239         etl::clock().sleep(0.25f);
240
241         return true;
242 }
243
244 void
245 dv_trgt::end_frame()
246 {
247         fprintf(file, " ");
248         fflush(file);
249         imagecount++;
250 }
251
252 bool
253 dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
254 {
255         int w=desc.get_w(),h=desc.get_h();
256
257         if(!file)
258                 return false;
259
260         fprintf(file, "P6\n");
261         fprintf(file, "%d %d\n", w, h);
262         fprintf(file, "%d\n", 255);
263
264         delete [] buffer;
265         buffer=new unsigned char[3*w];
266
267         delete [] color_buffer;
268         color_buffer=new Color[w];
269
270         return true;
271 }
272
273 Color *
274 dv_trgt::start_scanline(int /*scanline*/)
275 {
276         return color_buffer;
277 }
278
279 bool
280 dv_trgt::end_scanline()
281 {
282         if(!file)
283                 return false;
284
285         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
286
287         if(!fwrite(buffer,1,desc.get_w()*3,file))
288                 return false;
289
290         return true;
291 }