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