Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_ffmpeg / trgt_ffmpeg.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: trgt_ffmpeg.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
5 **
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SYNFIG_TARGET
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <ETL/stringf>
34 #include "trgt_ffmpeg.h"
35 #include <stdio.h>
36 #include <algorithm>
37 #include <functional>
38 #include <ETL/clock>
39
40 #endif
41
42 /* === M A C R O S ========================================================= */
43
44 using namespace synfig;
45 using namespace std;
46 using namespace etl;
47
48 /* === G L O B A L S ======================================================= */
49
50 SYNFIG_TARGET_INIT(ffmpeg_trgt);
51 SYNFIG_TARGET_SET_NAME(ffmpeg_trgt,"ffmpeg");
52 SYNFIG_TARGET_SET_EXT(ffmpeg_trgt,"mpg");
53 SYNFIG_TARGET_SET_VERSION(ffmpeg_trgt,"0.1");
54 SYNFIG_TARGET_SET_CVS_ID(ffmpeg_trgt,"$Id: trgt_ffmpeg.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
55
56 /* === M E T H O D S ======================================================= */
57
58 ffmpeg_trgt::ffmpeg_trgt(const char *Filename)
59 {
60         file=NULL;
61         filename=Filename;
62         multi_image=false;
63         buffer=NULL;
64         color_buffer=0;
65         set_remove_alpha();
66 }
67
68 ffmpeg_trgt::~ffmpeg_trgt()
69 {
70         if(file)
71         {
72                 etl::yield();
73                 sleep(1);
74                 pclose(file);
75         }
76         file=NULL;
77         delete [] buffer;
78         delete [] color_buffer;
79 }
80
81 bool
82 ffmpeg_trgt::set_rend_desc(RendDesc *given_desc)
83 {
84         //given_desc->set_pixel_format(PF_RGB);
85         
86         // Make sure that the width and height
87         // are multiples of 8
88         given_desc->set_w((given_desc->get_w()+4)/8*8);
89         given_desc->set_h((given_desc->get_h()+4)/8*8);
90
91         /*
92         // Valid framerates:
93         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
94         float fps=given_desc->get_frame_rate();
95         if(fps <24.0)
96                 given_desc->set_frame_rate(23.976);
97         if(fps>=24.0 && fps <25.0)
98                 given_desc->set_frame_rate(24);
99         if(fps>=25.0 && fps <29.97)
100                 given_desc->set_frame_rate(25);
101         if(fps>=29.97 && fps <30.0)
102                 given_desc->set_frame_rate(29.97);
103         if(fps>=29.97 && fps <30.0)
104                 given_desc->set_frame_rate(29.97);
105         if(fps>=30.0 && fps <50.0)
106                 given_desc->set_frame_rate(30.0);
107         if(fps>=50.0 && fps <59.94)
108                 given_desc->set_frame_rate(50);
109         if(fps>=59.94)
110                 given_desc->set_frame_rate(59.94);
111     */
112         
113         desc=*given_desc;
114
115         return true;
116 }
117
118 bool
119 ffmpeg_trgt::init()
120 {
121         imagecount=desc.get_frame_start();
122         if(desc.get_frame_end()-desc.get_frame_start()>0)
123                 multi_image=true;
124         string command;
125         
126         command=strprintf("ffmpeg -f imagepipe -an -r %f -i pipe: -loop -hq -title \"%s\" -y \"%s\"\n",desc.get_frame_rate(),get_canvas()->get_name().c_str(),filename.c_str());
127         
128         file=popen(command.c_str(),"w");
129         
130         // etl::yield();
131         
132         if(!file)
133         {
134                 synfig::error(_("Unable to open pipe to ffmpeg"));
135                 return false;
136         }
137                         
138         return true;
139 }
140
141 void
142 ffmpeg_trgt::end_frame()
143 {
144         //fprintf(file, " ");
145         fflush(file);
146         imagecount++;
147 }
148
149 bool
150 ffmpeg_trgt::start_frame(synfig::ProgressCallback *callback)
151 {
152         int w=desc.get_w(),h=desc.get_h();
153                 
154         if(!file)
155                 return false;
156         
157         fprintf(file, "P6\n");
158         fprintf(file, "%d %d\n", w, h);
159         fprintf(file, "%d\n", 255);     
160         
161         delete [] buffer;
162         buffer=new unsigned char[3*w];
163         delete [] color_buffer;
164         color_buffer=new Color[w];
165         
166         return true;
167 }
168
169 Color *
170 ffmpeg_trgt::start_scanline(int scanline)
171 {
172         return color_buffer;
173 }
174
175 bool
176 ffmpeg_trgt::end_scanline()
177 {
178         if(!file)
179                 return false;
180                         
181         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
182
183         if(!fwrite(buffer,1,desc.get_w()*3,file))
184                 return false;
185         
186         return true;
187 }