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