Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / src / modules / mod_ppm / trgt_mpg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_mpg.cpp
3 **      \brief bsd_mpeg1 Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <ETL/stringf>
37 #include "trgt_mpg.h"
38 #include <stdio.h>
39 #include <iostream>
40 #include <algorithm>
41 #include <functional>
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === G L O B A L S ======================================================= */
51
52 const char bsd_mpeg1::Name[]="mpeg1";
53 const char bsd_mpeg1::Ext[]="mpg";
54
55 #define tmp_dir         string("/tmp/")
56
57 /* === M E T H O D S ======================================================= */
58
59 #error This code has tmpfile vulnerabilites
60
61 Target *
62 bsd_mpeg1::New(const char *filename)
63 {
64         return new bsd_mpeg1(filename);
65 }
66
67 bsd_mpeg1::bsd_mpeg1(const char *Filename)
68 {
69         filename=Filename;
70         passthru=ppm::New((tmp_dir+"temp.ppm").c_str());
71         paramfile=NULL;
72
73 }
74
75 bsd_mpeg1::~bsd_mpeg1()
76 {
77         if(paramfile)
78                 fclose(paramfile);
79         delete passthru;
80         cerr<<"Encoding "<<filename<<"with \"mpeg_encode\" utility..."<<endl;
81         if(system("mpeg_encode -float-dct -realquiet /tmp/temp.param")!=0)
82         {
83                 cerr<<"Failed to encode "<<filename<<"with \"mpeg_encode\" utility"<<endl;
84                 cerr<<"Are you sure it is installed?"<<endl;
85         }
86 }
87
88 bool
89 bsd_mpeg1::set_rend_desc(RendDesc *given_desc)
90 {
91         if(paramfile)
92                 fclose(paramfile);
93
94
95         paramfile=fopen((tmp_dir+"temp.param").c_str(),"wt");
96         int bitrate=150; // kbytes per second
97         int buffer_drift=50; // bitrate drift (in kbytes per second)
98
99         bitrate*=8*1024;
100         buffer_drift*=8*1024;
101
102         fprintf(paramfile,
103                 "PATTERN                IBBPBBPBBPBBPBBP\n"
104                 "OUTPUT         %s\n"
105                 "BASE_FILE_FORMAT       PPM\n"
106                 "INPUT_CONVERT  *\n"
107                 "GOP_SIZE       16\n"
108                 "SLICES_PER_FRAME       1\n"
109                 "INPUT_DIR      \n"
110                 "PIXEL          HALF\n"
111                 "RANGE          10\n"
112                 "PSEARCH_ALG    LOGARITHMIC\n"
113                 "BSEARCH_ALG    CROSS2\n"
114 //              "IQSCALE                8\n"
115 //              "PQSCALE                10\n"
116 //              "BQSCALE                25\n"
117                 "IQSCALE                3\n"
118                 "PQSCALE                5\n"
119                 "BQSCALE                10\n"
120                 "REFERENCE_FRAME        ORIGINAL\n"
121                 "BIT_RATE  %d\n"
122 //              "BIT_RATE  1000000\n"
123 //              "BUFFER_SIZE 327680\n"
124                 "BUFFER_SIZE %d\n"
125                 ,filename.c_str(),bitrate,buffer_drift);
126          float fps=given_desc->get_frame_rate();
127
128         // Valid framerates:
129         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
130
131         if(fps <24.0)
132         {
133                 fprintf(paramfile,"FRAME_RATE 23.976\n");
134                 given_desc->set_frame_rate(23.976);
135         }
136         if(fps>=24.0 && fps <25.0)
137         {
138                 fprintf(paramfile,"FRAME_RATE 24\n");
139                 given_desc->set_frame_rate(24);
140         }
141         if(fps>=25.0 && fps <29.97)
142         {
143                 fprintf(paramfile,"FRAME_RATE 25\n");
144                 given_desc->set_frame_rate(25);
145         }
146         if(fps>=29.97 && fps <30.0)
147         {
148                 fprintf(paramfile,"FRAME_RATE 29.97\n");
149                 given_desc->set_frame_rate(29.97);
150         }
151         if(fps>=29.97 && fps <30.0)
152         {
153                 fprintf(paramfile,"FRAME_RATE 29.97\n");
154                 given_desc->set_frame_rate(29.97);
155         }
156         if(fps>=30.0 && fps <50.0)
157         {
158                 fprintf(paramfile,"FRAME_RATE 30\n");
159                 given_desc->set_frame_rate(30.0);
160         }
161         if(fps>=50.0 && fps <59.94)
162         {
163                 fprintf(paramfile,"FRAME_RATE 50\n");
164                 given_desc->set_frame_rate(50);
165         }
166         if(fps>=59.94)
167         {
168                 fprintf(paramfile,"FRAME_RATE 59.94\n");
169                 given_desc->set_frame_rate(59.94);
170         }
171
172         // Make sure that the width and height
173         // are multiples of 8
174         given_desc->set_w((given_desc->get_w()+4)/8*8);
175         given_desc->set_h((given_desc->get_h()+4)/8*8);
176
177         if(!passthru->set_rend_desc(given_desc))
178                 return false;
179
180         desc=*given_desc;
181
182         fprintf(paramfile,
183                 "INPUT\n"
184                 "tmp/temp*.ppm  [%04d-%04d]\n"
185                 "END_INPUT\n",desc.get_frame_start(),desc.get_frame_end()-1);
186
187         fclose(paramfile);
188         paramfile=NULL;
189
190         return true;
191 }
192
193 void
194 bsd_mpeg1::end_frame()
195 {
196         passthru->end_frame();
197 }
198
199 bool
200 bsd_mpeg1::start_frame(synfig::ProgressCallback *callback)
201 {
202         return passthru->start_frame(callback);
203 }
204
205 unsigned char *
206 bsd_mpeg1::start_scanline(int scanline)
207 {
208         return passthru->start_scanline(scanline);
209 }
210
211 bool
212 bsd_mpeg1::end_scanline(void)
213 {
214         return passthru->end_scanline();
215 }