New targets parameters class. Tool accepts video codec and bitrate parameters. All...
[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 #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                                          const synfig::TargetParam& /* params */)
69 {
70         filename=Filename;
71         passthru=ppm::New((tmp_dir+"temp.ppm").c_str());
72         paramfile=NULL;
73
74 }
75
76 bsd_mpeg1::~bsd_mpeg1()
77 {
78         if(paramfile)
79                 fclose(paramfile);
80         delete passthru;
81         cerr<<"Encoding "<<filename<<"with \"mpeg_encode\" utility..."<<endl;
82         if(system("mpeg_encode -float-dct -realquiet /tmp/temp.param")!=0)
83         {
84                 cerr<<"Failed to encode "<<filename<<"with \"mpeg_encode\" utility"<<endl;
85                 cerr<<"Are you sure it is installed?"<<endl;
86         }
87 }
88
89 bool
90 bsd_mpeg1::set_rend_desc(RendDesc *given_desc)
91 {
92         if(paramfile)
93                 fclose(paramfile);
94
95
96         paramfile=fopen((tmp_dir+"temp.param").c_str(),"wt");
97         int bitrate=150; // kbytes per second
98         int buffer_drift=50; // bitrate drift (in kbytes per second)
99
100         bitrate*=8*1024;
101         buffer_drift*=8*1024;
102
103         fprintf(paramfile,
104                 "PATTERN                IBBPBBPBBPBBPBBP\n"
105                 "OUTPUT         %s\n"
106                 "BASE_FILE_FORMAT       PPM\n"
107                 "INPUT_CONVERT  *\n"
108                 "GOP_SIZE       16\n"
109                 "SLICES_PER_FRAME       1\n"
110                 "INPUT_DIR      \n"
111                 "PIXEL          HALF\n"
112                 "RANGE          10\n"
113                 "PSEARCH_ALG    LOGARITHMIC\n"
114                 "BSEARCH_ALG    CROSS2\n"
115 //              "IQSCALE                8\n"
116 //              "PQSCALE                10\n"
117 //              "BQSCALE                25\n"
118                 "IQSCALE                3\n"
119                 "PQSCALE                5\n"
120                 "BQSCALE                10\n"
121                 "REFERENCE_FRAME        ORIGINAL\n"
122                 "BIT_RATE  %d\n"
123 //              "BIT_RATE  1000000\n"
124 //              "BUFFER_SIZE 327680\n"
125                 "BUFFER_SIZE %d\n"
126                 ,filename.c_str(),bitrate,buffer_drift);
127          float fps=given_desc->get_frame_rate();
128
129         // Valid framerates:
130         // 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60
131
132         if(fps <24.0)
133         {
134                 fprintf(paramfile,"FRAME_RATE 23.976\n");
135                 given_desc->set_frame_rate(23.976);
136         }
137         if(fps>=24.0 && fps <25.0)
138         {
139                 fprintf(paramfile,"FRAME_RATE 24\n");
140                 given_desc->set_frame_rate(24);
141         }
142         if(fps>=25.0 && fps <29.97)
143         {
144                 fprintf(paramfile,"FRAME_RATE 25\n");
145                 given_desc->set_frame_rate(25);
146         }
147         if(fps>=29.97 && fps <30.0)
148         {
149                 fprintf(paramfile,"FRAME_RATE 29.97\n");
150                 given_desc->set_frame_rate(29.97);
151         }
152         if(fps>=29.97 && fps <30.0)
153         {
154                 fprintf(paramfile,"FRAME_RATE 29.97\n");
155                 given_desc->set_frame_rate(29.97);
156         }
157         if(fps>=30.0 && fps <50.0)
158         {
159                 fprintf(paramfile,"FRAME_RATE 30\n");
160                 given_desc->set_frame_rate(30.0);
161         }
162         if(fps>=50.0 && fps <59.94)
163         {
164                 fprintf(paramfile,"FRAME_RATE 50\n");
165                 given_desc->set_frame_rate(50);
166         }
167         if(fps>=59.94)
168         {
169                 fprintf(paramfile,"FRAME_RATE 59.94\n");
170                 given_desc->set_frame_rate(59.94);
171         }
172
173         // Make sure that the width and height
174         // are multiples of 8
175         given_desc->set_w((given_desc->get_w()+4)/8*8);
176         given_desc->set_h((given_desc->get_h()+4)/8*8);
177
178         if(!passthru->set_rend_desc(given_desc))
179                 return false;
180
181         desc=*given_desc;
182
183         fprintf(paramfile,
184                 "INPUT\n"
185                 "tmp/temp*.ppm  [%04d-%04d]\n"
186                 "END_INPUT\n",desc.get_frame_start(),desc.get_frame_end()-1);
187
188         fclose(paramfile);
189         paramfile=NULL;
190
191         return true;
192 }
193
194 void
195 bsd_mpeg1::end_frame()
196 {
197         passthru->end_frame();
198 }
199
200 bool
201 bsd_mpeg1::start_frame(synfig::ProgressCallback *callback)
202 {
203         return passthru->start_frame(callback);
204 }
205
206 unsigned char *
207 bsd_mpeg1::start_scanline(int scanline)
208 {
209         return passthru->start_scanline(scanline);
210 }
211
212 bool
213 bsd_mpeg1::end_scanline(void)
214 {
215         return passthru->end_scanline();
216 }