FFMPEG title metadata parameter.
[synfig.git] / synfig-core / src / modules / mod_ffmpeg / trgt_ffmpeg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_ffmpeg.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 **      Copyright (c) 2010 Diego Barrios Romero
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 **
23 ** === N O T E S ===========================================================
24 **
25 ** ========================================================================= */
26
27 /* === H E A D E R S ======================================================= */
28
29 #define SYNFIG_TARGET
30
31 #ifdef USING_PCH
32 #       include "pch.h"
33 #else
34 #ifdef HAVE_CONFIG_H
35 #       include <config.h>
36 #endif
37
38 #include <ETL/stringf>
39 #include "trgt_ffmpeg.h"
40 #include <stdio.h>
41 #include <sys/types.h>
42 #if HAVE_SYS_WAIT_H
43  #include <sys/wait.h>
44 #endif
45 #if HAVE_IO_H
46  #include <io.h>
47 #endif
48 #if HAVE_PROCESS_H
49  #include <process.h>
50 #endif
51 #if HAVE_FCNTL_H
52  #include <fcntl.h>
53 #endif
54 #include <unistd.h>
55 #include <algorithm>
56 #include <functional>
57 #include <ETL/clock>
58
59 #endif
60
61 /* === M A C R O S ========================================================= */
62
63 using namespace synfig;
64 using namespace std;
65 using namespace etl;
66
67 #if defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID)
68  #define UNIX_PIPE_TO_PROCESSES
69 #else
70  #define WIN32_PIPE_TO_PROCESSES
71 #endif
72
73 /* === G L O B A L S ======================================================= */
74
75 SYNFIG_TARGET_INIT(ffmpeg_trgt);
76 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
77 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
78 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
79 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id$");
80
81 /* === M E T H O D S ======================================================= */
82
83 ffmpeg_trgt::ffmpeg_trgt(const char *Filename,
84                                                  const synfig::TargetParam& params)
85 {
86         pid=-1;
87         file=NULL;
88         filename=Filename;
89         multi_image=false;
90         buffer=NULL;
91         color_buffer=0;
92         set_remove_alpha();
93         video_codec = params.video_codec;
94         bitrate = params.bitrate;
95 }
96
97 ffmpeg_trgt::~ffmpeg_trgt()
98 {
99         if(file)
100         {
101                 etl::yield();
102                 sleep(1);
103 #if defined(WIN32_PIPE_TO_PROCESSES)
104                 pclose(file);
105 #elif defined(UNIX_PIPE_TO_PROCESSES)
106                 fclose(file);
107                 int status;
108                 waitpid(pid,&status,0);
109 #endif
110         }
111         file=NULL;
112         delete [] buffer;
113         delete [] color_buffer;
114 }
115
116 bool
117 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
118 {
119         //given_desc->set_pixel_format(PF_RGB);
120
121         // Make sure that the width and height
122         // are multiples of 8
123         given_desc->set_w((given_desc->get_w()+4)/8*8);
124         given_desc->set_h((given_desc->get_h()+4)/8*8);
125
126         /*
127         // Valid framerates:
128         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
129         float fps=given_desc->get_frame_rate();
130         if(fps <24.0)
131                 given_desc->set_frame_rate(23.976);
132         if(fps>=24.0 && fps <25.0)
133                 given_desc->set_frame_rate(24);
134         if(fps>=25.0 && fps <29.97)
135                 given_desc->set_frame_rate(25);
136         if(fps>=29.97 && fps <30.0)
137                 given_desc->set_frame_rate(29.97);
138         if(fps>=29.97 && fps <30.0)
139                 given_desc->set_frame_rate(29.97);
140         if(fps>=30.0 && fps <50.0)
141                 given_desc->set_frame_rate(30.0);
142         if(fps>=50.0 && fps <59.94)
143                 given_desc->set_frame_rate(50);
144         if(fps>=59.94)
145                 given_desc->set_frame_rate(59.94);
146     */
147
148         desc=*given_desc;
149
150         return true;
151 }
152
153 bool
154 ffmpeg_trgt::init()
155 {
156         imagecount=desc.get_frame_start();
157         if(desc.get_frame_end()-desc.get_frame_start()>0)
158                 multi_image=true;
159
160 #if defined(WIN32_PIPE_TO_PROCESSES)
161
162         string command;
163
164         if( filename.c_str()[0] == '-' )
165                 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
166                                                         " -r %f -i pipe: -loop_input"
167                                                         " -metadata title=\"%s\" "
168                                                         " -vcodec %s -b %ik"
169                                                         " -y -- \"%s\"\n",
170                                                         desc.get_frame_rate(),
171                                                         get_canvas()->get_name().c_str(),
172                                                         video_codec.c_str(), bitrate,
173                                                         filename.c_str());
174         else
175                 command = strprintf("ffmpeg -f image2pipe -vcodec ppm -an"
176                                                         " -r %f -i pipe: -loop_input"
177                                                         " -metadata title=\"%s\" "
178                                                         "-vcodec %s -b %ik"
179                                                         " -y -- \"%s\"\n",
180                                                         desc.get_frame_rate(),
181                                                         get_canvas()->get_name().c_str(),
182                                                         video_codec.c_str(), bitrate,
183                                                         filename.c_str());
184
185         file=popen(command.c_str(),POPEN_BINARY_WRITE_TYPE);
186
187 #elif defined(UNIX_PIPE_TO_PROCESSES)
188
189         int p[2];
190
191         if (pipe(p)) {
192                 synfig::error(_("Unable to open pipe to ffmpeg"));
193                 return false;
194         };
195
196         pid = fork();
197
198         if (pid == -1) {
199                 synfig::error(_("Unable to open pipe to ffmpeg"));
200                 return false;
201         }
202
203         if (pid == 0){
204                 // Child process
205                 // Close pipeout, not needed
206                 close(p[1]);
207                 // Dup pipeout to stdin
208                 if( dup2( p[0], STDIN_FILENO ) == -1 ){
209                         synfig::error(_("Unable to open pipe to ffmpeg"));
210                         return false;
211                 }
212                 // Close the unneeded pipeout
213                 close(p[0]);
214                 if( filename.c_str()[0] == '-' )
215                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
216                                    "ppm", "-an", "-r",
217                                    strprintf("%f", desc.get_frame_rate()).c_str(),
218                                    "-i", "pipe:", "-loop_input",
219                                    "-metadata",
220                                    strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
221                                    "-vcodec", video_codec.c_str(),
222                                    "-b", strprintf("%ik", bitrate).c_str(),
223                                    "-y", "--", filename.c_str(), (const char *)NULL);
224                 else
225                         execlp("ffmpeg", "ffmpeg", "-f", "image2pipe", "-vcodec",
226                                    "ppm", "-an", "-r",
227                                    strprintf("%f", desc.get_frame_rate()).c_str(),
228                                    "-i", "pipe:", "-loop_input",
229                                    "-metadata",
230                                    strprintf("title=\"%s\"", get_canvas()->get_name().c_str()).c_str(),
231                                    "-vcodec", video_codec.c_str(),
232                                    "-b", strprintf("%ik", bitrate).c_str(),
233                                    "-y", filename.c_str(), (const char *)NULL);
234
235                 // We should never reach here unless the exec failed
236                 synfig::error(_("Unable to open pipe to ffmpeg"));
237                 return false;
238         } else {
239                 // Parent process
240                 // Close pipein, not needed
241                 close(p[0]);
242                 // Save pipeout to file handle, will write to it later
243                 file = fdopen(p[1], "wb");
244         }
245
246 #else
247         #error There are no known APIs for creating child processes
248 #endif
249
250         // etl::yield();
251
252         if(!file)
253         {
254                 synfig::error(_("Unable to open pipe to ffmpeg"));
255                 return false;
256         }
257
258         return true;
259 }
260
261 void
262 ffmpeg_trgt::end_frame()
263 {
264         //fprintf(file, " ");
265         fflush(file);
266         imagecount++;
267 }
268
269 bool
270 ffmpeg_trgt::start_frame(synfig::ProgressCallback */*callback*/)
271 {
272         int w=desc.get_w(),h=desc.get_h();
273
274         if(!file)
275                 return false;
276
277         fprintf(file, "P6\n");
278         fprintf(file, "%d %d\n", w, h);
279         fprintf(file, "%d\n", 255);
280
281         delete [] buffer;
282         buffer=new unsigned char[3*w];
283         delete [] color_buffer;
284         color_buffer=new Color[w];
285
286         return true;
287 }
288
289 Color *
290 ffmpeg_trgt::start_scanline(int /*scanline*/)
291 {
292         return color_buffer;
293 }
294
295 bool
296 ffmpeg_trgt::end_scanline()
297 {
298         if(!file)
299                 return false;
300
301         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
302
303         if(!fwrite(buffer,1,desc.get_w()*3,file))
304                 return false;
305
306         return true;
307 }