Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / 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 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <ETL/stringf>
35 #include "trgt_mpg.h"
36 #include <stdio.h>
37 #include <iostream>
38 #include <algorithm>
39 #include <functional>
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 const char bsd_mpeg1::Name[]="mpeg1";
51 const char bsd_mpeg1::Ext[]="mpg";
52
53 #define tmp_dir         string("/tmp/")
54
55 /* === M E T H O D S ======================================================= */
56
57 #error This code has tmpfile vulnerabilites
58
59 Target *
60 bsd_mpeg1::New(const char *filename)
61 {
62         return new bsd_mpeg1(filename);
63 }
64
65 bsd_mpeg1::bsd_mpeg1(const char *Filename,
66                                          const synfig::TargetParam& /* params */)
67 {
68         filename=Filename;
69         passthru=ppm::New((tmp_dir+"temp.ppm").c_str());
70         paramfile=NULL;
71
72 }
73
74 bsd_mpeg1::~bsd_mpeg1()
75 {
76         if(paramfile)
77                 fclose(paramfile);
78         delete passthru;
79         cerr<<"Encoding "<<filename<<"with \"mpeg_encode\" utility..."<<endl;
80         if(system("mpeg_encode -float-dct -realquiet /tmp/temp.param")!=0)
81         {
82                 cerr<<"Failed to encode "<<filename<<"with \"mpeg_encode\" utility"<<endl;
83                 cerr<<"Are you sure it is installed?"<<endl;
84         }
85 }
86
87 bool
88 bsd_mpeg1::set_rend_desc(RendDesc *given_desc)
89 {
90         if(paramfile)
91                 fclose(paramfile);
92
93
94         paramfile=fopen((tmp_dir+"temp.param").c_str(),"wt");
95         int bitrate=150; // kbytes per second
96         int buffer_drift=50; // bitrate drift (in kbytes per second)
97
98         bitrate*=8*1024;
99         buffer_drift*=8*1024;
100
101         fprintf(paramfile,
102                 "PATTERN                IBBPBBPBBPBBPBBP\n"
103                 "OUTPUT         %s\n"
104                 "BASE_FILE_FORMAT       PPM\n"
105                 "INPUT_CONVERT  *\n"
106                 "GOP_SIZE       16\n"
107                 "SLICES_PER_FRAME       1\n"
108                 "INPUT_DIR      \n"
109                 "PIXEL          HALF\n"
110                 "RANGE          10\n"
111                 "PSEARCH_ALG    LOGARITHMIC\n"
112                 "BSEARCH_ALG    CROSS2\n"
113 //              "IQSCALE                8\n"
114 //              "PQSCALE                10\n"
115 //              "BQSCALE                25\n"
116                 "IQSCALE                3\n"
117                 "PQSCALE                5\n"
118                 "BQSCALE                10\n"
119                 "REFERENCE_FRAME        ORIGINAL\n"
120                 "BIT_RATE  %d\n"
121 //              "BIT_RATE  1000000\n"
122 //              "BUFFER_SIZE 327680\n"
123                 "BUFFER_SIZE %d\n"
124                 ,filename.c_str(),bitrate,buffer_drift);
125          float fps=given_desc->get_frame_rate();
126
127         // Valid framerates:
128         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
129
130         if(fps <24.0)
131         {
132                 fprintf(paramfile,"FRAME_RATE 23.976\n");
133                 given_desc->set_frame_rate(23.976);
134         }
135         if(fps>=24.0 && fps <25.0)
136         {
137                 fprintf(paramfile,"FRAME_RATE 24\n");
138                 given_desc->set_frame_rate(24);
139         }
140         if(fps>=25.0 && fps <29.97)
141         {
142                 fprintf(paramfile,"FRAME_RATE 25\n");
143                 given_desc->set_frame_rate(25);
144         }
145         if(fps>=29.97 && fps <30.0)
146         {
147                 fprintf(paramfile,"FRAME_RATE 29.97\n");
148                 given_desc->set_frame_rate(29.97);
149         }
150         if(fps>=29.97 && fps <30.0)
151         {
152                 fprintf(paramfile,"FRAME_RATE 29.97\n");
153                 given_desc->set_frame_rate(29.97);
154         }
155         if(fps>=30.0 && fps <50.0)
156         {
157                 fprintf(paramfile,"FRAME_RATE 30\n");
158                 given_desc->set_frame_rate(30.0);
159         }
160         if(fps>=50.0 && fps <59.94)
161         {
162                 fprintf(paramfile,"FRAME_RATE 50\n");
163                 given_desc->set_frame_rate(50);
164         }
165         if(fps>=59.94)
166         {
167                 fprintf(paramfile,"FRAME_RATE 59.94\n");
168                 given_desc->set_frame_rate(59.94);
169         }
170
171         // Make sure that the width and height
172         // are multiples of 8
173         given_desc->set_w((given_desc->get_w()+4)/8*8);
174         given_desc->set_h((given_desc->get_h()+4)/8*8);
175
176         if(!passthru->set_rend_desc(given_desc))
177                 return false;
178
179         desc=*given_desc;
180
181         fprintf(paramfile,
182                 "INPUT\n"
183                 "tmp/temp*.ppm  [%04d-%04d]\n"
184                 "END_INPUT\n",desc.get_frame_start(),desc.get_frame_end()-1);
185
186         fclose(paramfile);
187         paramfile=NULL;
188
189         return true;
190 }
191
192 void
193 bsd_mpeg1::end_frame()
194 {
195         passthru->end_frame();
196 }
197
198 bool
199 bsd_mpeg1::start_frame(synfig::ProgressCallback *callback)
200 {
201         return passthru->start_frame(callback);
202 }
203
204 unsigned char *
205 bsd_mpeg1::start_scanline(int scanline)
206 {
207         return passthru->start_scanline(scanline);
208 }
209
210 bool
211 bsd_mpeg1::end_scanline(void)
212 {
213         return passthru->end_scanline();
214 }