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