Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mptr_mplayer / mptr_mplayer.cpp
1 /*! ========================================================================
2 ** Sinfg
3 ** ppm Target Module
4 ** $Id: mptr_mplayer.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include <sinfg/sinfg.h>
31 #include <ETL/stringf>
32 #include "mptr_mplayer.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 sinfg;
43 using namespace std;
44 using namespace etl;
45
46 /* === G L O B A L S ======================================================= */
47
48 const char mplayer_mptr::Name[]="avi";
49 const char mplayer_mptr::Ext[]="avi";
50
51 /* === M E T H O D S ======================================================= */
52
53 Importer *
54 mplayer_mptr::New(const char *file)
55 {
56         return new mplayer_mptr(file);
57 }
58
59 mplayer_mptr::mplayer_mptr(const char *file)
60 {
61         filename=file;
62 }
63
64 mplayer_mptr::~mplayer_mptr()
65 {
66 }
67
68 bool
69 mplayer_mptr::GetFrame(Time time, sinfg::Surface &surface, sinfg::ProgressCallback *)
70 {
71         int ret;
72         ret=system(
73                 strprintf("/usr/local/bin/mencoder \"%s\" -ovc rawrgb -ss %f -endpos 0 -nosound -o /tmp/tmp.sinfg.rgbdata | grep \"VIDEO\" > /tmp/tmp.sinfg.size",
74                         filename.c_str(),
75                         time
76                 ).c_str()
77         );
78         /*
79         if(ret!=0)
80         {
81                 cerr<<"mencoder execution failed."<<endl;
82                 return false;
83         }
84 */
85         FILE *sizefile=fopen("/tmp/tmp.sinfg.size","rt");
86         FILE *rgbfile=fopen("/tmp/tmp.sinfg.rgbdata","rb");
87         if(!rgbfile)
88         {
89                 cerr<<"unable to open /tmp/tmp.sinfg.rgbdata"<<endl;
90                 return false;
91         }
92         if(!sizefile)
93         {
94                 cerr<<"unable to open /tmp/tmp.sinfg.size"<<endl;
95                 return false;
96         }
97         
98         int w=4,h=4,x,y;
99         char bleh[500];
100         
101         fscanf(sizefile,"%s %s %dx%d",bleh,bleh,&w,&h);
102         
103         cerr<<strprintf("w:%d, h:%d, time:%f",w,h,time)<<endl;
104         fseek(rgbfile,2047+3*8,SEEK_CUR);
105         surface.set_wh(w,h);
106         for(y=0;y<h;y++)
107                 for(x=0;x<w;x++)
108                 {
109                         unsigned char
110                                 b=(unsigned char)fgetc(rgbfile),
111                                 g=(unsigned char)fgetc(rgbfile),
112                                 r=(unsigned char)fgetc(rgbfile);
113                                 
114                         surface[h-y-1][x]=Color(
115                                 (float)r/255.0,
116                                 (float)g/255.0,
117                                 (float)b/255.0,
118                                 1.0
119                         );
120                 }
121         
122         fclose(rgbfile);
123         fclose(sizefile);
124         
125         return true;
126 }