Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / modules / mod_ffmpeg / mptr_ffmpeg.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_ffmpeg.cpp
3 **      \brief ppm 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 "mptr_ffmpeg.h"
36 #include <stdio.h>
37 #include <iostream>
38 #include <algorithm>
39 #include <functional>
40 #include <ETL/stringf>
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_IMPORTER_INIT(ffmpeg_mptr);
52 SYNFIG_IMPORTER_SET_NAME(ffmpeg_mptr,"ffmpeg");
53 SYNFIG_IMPORTER_SET_EXT(ffmpeg_mptr,"avi");
54 SYNFIG_IMPORTER_SET_VERSION(ffmpeg_mptr,"0.1");
55 SYNFIG_IMPORTER_SET_CVS_ID(ffmpeg_mptr,"$Id$");
56
57 /* === M E T H O D S ======================================================= */
58
59 bool
60 ffmpeg_mptr::seek_to(int frame)
61 {
62         if(frame<cur_frame || !file)
63         {
64                 if(file)
65                 {
66                         pclose(file);
67                 }
68
69                 string command;
70
71                 command=strprintf("ffmpeg -i \"%s\" -an -f image2pipe -vcodec ppm -\n",filename.c_str());
72
73                 file=popen(command.c_str(),"r");
74
75                 if(!file)
76                 {
77                         cerr<<"Unable to open pipe to ffmpeg"<<endl;
78                         return false;
79                 }
80                 cur_frame=-1;
81         }
82
83         while(cur_frame<frame-1)
84         {
85                 cerr<<"Seeking to..."<<frame<<'('<<cur_frame<<')'<<endl;
86                 if(!grab_frame())
87                         return false;
88         }
89         return true;
90 }
91
92 bool
93 ffmpeg_mptr::grab_frame(void)
94 {
95         if(!file)
96         {
97                 cerr<<"unable to open "<<filename<<endl;
98                 return false;
99         }
100         int w,h;
101         float divisor;
102         char cookie[2];
103         cookie[0]=fgetc(file);
104         cookie[1]=fgetc(file);
105
106         if(cookie[0]!='P' || cookie[1]!='6')
107         {
108                 cerr<<"stream not in PPM format \""<<cookie[0]<<cookie[1]<<'"'<<endl;
109                 return false;
110         }
111
112         fgetc(file);
113         fscanf(file,"%d %d\n",&w,&h);
114         fscanf(file,"%f",&divisor);
115         fgetc(file);
116
117         if(feof(file))
118                 return false;
119
120         int x;
121         int y;
122         frame.set_wh(w,h);
123         for(y=0;y<frame.get_h();y++)
124                 for(x=0;x<frame.get_w();x++)
125                 {
126                         if(feof(file))
127                                 return false;
128 /*
129                         frame[y][x]=Color(
130                                 (float)(unsigned char)fgetc(file)/divisor,
131                                 (float)(unsigned char)fgetc(file)/divisor,
132                                 (float)(unsigned char)fgetc(file)/divisor,
133                                 1.0
134 */
135                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));
136                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
137                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
138                         frame[y][x]=Color(
139                                 r,
140                                 g,
141                                 b,
142                                 1.0
143                         );
144                 }
145         cur_frame++;
146         return true;
147 }
148
149 ffmpeg_mptr::ffmpeg_mptr(const char *f)
150 {
151 #ifdef HAVE_TERMIOS_H
152         tcgetattr (0, &oldtty);
153 #endif
154         filename=f;
155         file=NULL;
156         fps=23.98;
157         cur_frame=-1;
158 }
159
160 ffmpeg_mptr::~ffmpeg_mptr()
161 {
162         if(file)
163                 pclose(file);
164 #ifdef HAVE_TERMIOS_H
165         tcsetattr(0,TCSANOW,&oldtty);
166 #endif
167 }
168
169 bool
170 ffmpeg_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *)
171 {
172         int i=(int)(time*fps);
173         if(i!=cur_frame)
174         {
175                 if(!seek_to(i))
176                         return false;
177                 if(!grab_frame());
178                         return false;
179         }
180
181         surface=frame;
182         return false;
183 }