vo_gl: Make it possible to select GL_NEAREST scaling
[mplayer/glamo.git] / libmpdemux / demux_mf.c
blobcc6f6be6a8d3db15d4d25230712d8293b5c7df2e
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
26 #include "config.h"
27 #include "mp_msg.h"
29 #include "stream/stream.h"
30 #include "demuxer.h"
31 #include "stheader.h"
32 #include "mf.h"
34 static void demux_seek_mf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
35 mf_t * mf = (mf_t *)demuxer->priv;
36 sh_video_t * sh_video = demuxer->video->sh;
37 int newpos = (flags & SEEK_ABSOLUTE)?0:mf->curr_frame - 1;
39 if ( flags & SEEK_FACTOR ) newpos+=rel_seek_secs*(mf->nr_of_files - 1);
40 else newpos+=rel_seek_secs * sh_video->fps;
41 if ( newpos < 0 ) newpos=0;
42 if( newpos >= mf->nr_of_files) newpos=mf->nr_of_files - 1;
43 demuxer->filepos=mf->curr_frame=newpos;
46 // return value:
47 // 0 = EOF or no stream found
48 // 1 = successfully read a packet
49 static int demux_mf_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds){
50 mf_t * mf;
51 struct stat fs;
52 FILE * f;
54 mf=(mf_t*)demuxer->priv;
55 if ( mf->curr_frame >= mf->nr_of_files ) return 0;
57 stat( mf->names[mf->curr_frame],&fs );
58 // printf( "[demux_mf] frame: %d (%s,%d)\n",mf->curr_frame,mf->names[mf->curr_frame],fs.st_size );
60 if ( !( f=fopen( mf->names[mf->curr_frame],"rb" ) ) ) return 0;
62 sh_video_t * sh_video = demuxer->video->sh;
63 demux_packet_t * dp = new_demux_packet( fs.st_size );
64 if ( !fread( dp->buffer,fs.st_size,1,f ) ) return 0;
65 dp->pts=mf->curr_frame / sh_video->fps;
66 dp->pos=mf->curr_frame;
67 dp->flags=0;
68 // append packet to DS stream:
69 ds_add_packet( demuxer->video,dp );
71 fclose( f );
73 demuxer->filepos=mf->curr_frame++;
74 return 1;
77 // force extension/type to have a fourcc
79 static const struct {
80 const char *type;
81 uint32_t format;
82 } type2format[] = {
83 { "bmp", mmioFOURCC('b', 'm', 'p', ' ') },
84 { "dpx", mmioFOURCC('d', 'p', 'x', ' ') },
85 { "j2k", mmioFOURCC('M', 'J', '2', 'C') },
86 { "jp2", mmioFOURCC('M', 'J', '2', 'C') },
87 { "jpeg", mmioFOURCC('I', 'J', 'P', 'G') },
88 { "jpg", mmioFOURCC('I', 'J', 'P', 'G') },
89 { "jls", mmioFOURCC('I', 'J', 'P', 'G') },
90 { "thm", mmioFOURCC('I', 'J', 'P', 'G') },
91 { "db", mmioFOURCC('I', 'J', 'P', 'G') },
92 { "pcx", mmioFOURCC('p', 'c', 'x', ' ') },
93 { "png", mmioFOURCC('M', 'P', 'N', 'G') },
94 { "ptx", mmioFOURCC('p', 't', 'x', ' ') },
95 { "tga", mmioFOURCC('M', 'T', 'G', 'A') },
96 { "tif", mmioFOURCC('t', 'i', 'f', 'f') },
97 { "sgi", mmioFOURCC('S', 'G', 'I', '1') },
98 { "sun", mmioFOURCC('s', 'u', 'n', ' ') },
99 { "ras", mmioFOURCC('s', 'u', 'n', ' ') },
100 { "ra", mmioFOURCC('s', 'u', 'n', ' ') },
101 { "im1", mmioFOURCC('s', 'u', 'n', ' ') },
102 { "im8", mmioFOURCC('s', 'u', 'n', ' ') },
103 { "im24", mmioFOURCC('s', 'u', 'n', ' ') },
104 { "sunras", mmioFOURCC('s', 'u', 'n', ' ') },
105 { NULL, 0 }
108 static demuxer_t* demux_open_mf(demuxer_t* demuxer){
109 sh_video_t *sh_video = NULL;
110 mf_t *mf = NULL;
111 int i;
113 if(!demuxer->stream->url) return NULL;
114 if(strncmp(demuxer->stream->url, "mf://", 5)) return NULL;
117 mf=open_mf(demuxer->stream->url + 5);
118 if(!mf) return NULL;
120 if(!mf_type){
121 char* p=strrchr(mf->names[0],'.');
122 if(!p){
123 mp_msg(MSGT_DEMUX, MSGL_INFO, "[demux_mf] file type was not set! (try -mf type=xxx)\n" );
124 free( mf ); return NULL;
126 mf_type=strdup(p+1);
127 mp_msg(MSGT_DEMUX, MSGL_INFO, "[demux_mf] file type was not set! trying 'type=%s'...\n", mf_type);
130 demuxer->filepos=mf->curr_frame=0;
132 demuxer->movi_start = 0;
133 demuxer->movi_end = mf->nr_of_files - 1;
135 // create a new video stream header
136 sh_video = new_sh_video(demuxer, 0);
137 // make sure the demuxer knows about the new video stream header
138 // (even though new_sh_video() ought to take care of it)
139 demuxer->video->sh = sh_video;
141 // make sure that the video demuxer stream header knows about its
142 // parent video demuxer stream (this is getting wacky), or else
143 // video_read_properties() will choke
144 sh_video->ds = demuxer->video;
146 for (i = 0; type2format[i].type; i++)
147 if (strcasecmp(mf_type, type2format[i].type) == 0)
148 break;
149 if (!type2format[i].type) {
150 mp_msg(MSGT_DEMUX, MSGL_INFO, "[demux_mf] unknown input file type.\n" );
151 free(mf);
152 return NULL;
154 sh_video->format = type2format[i].format;
156 sh_video->disp_w = mf_w;
157 sh_video->disp_h = mf_h;
158 sh_video->fps = mf_fps;
159 sh_video->frametime = 1 / sh_video->fps;
161 // emulate BITMAPINFOHEADER:
162 sh_video->bih=malloc(sizeof(BITMAPINFOHEADER));
163 memset(sh_video->bih,0,sizeof(BITMAPINFOHEADER));
164 sh_video->bih->biSize=40;
165 sh_video->bih->biWidth = mf_w;
166 sh_video->bih->biHeight = mf_h;
167 sh_video->bih->biPlanes=1;
168 sh_video->bih->biBitCount=24;
169 sh_video->bih->biCompression=sh_video->format;
170 sh_video->bih->biSizeImage=sh_video->bih->biWidth*sh_video->bih->biHeight*3;
172 /* disable seeking */
173 // demuxer->seekable = 0;
175 demuxer->priv=(void*)mf;
177 return demuxer;
180 static void demux_close_mf(demuxer_t* demuxer) {
181 mf_t *mf = demuxer->priv;
183 if(!mf)
184 return;
185 free(mf);
188 static int demux_control_mf(demuxer_t *demuxer, int cmd, void *arg) {
189 mf_t *mf = (mf_t *)demuxer->priv;
190 sh_video_t *sh_video = demuxer->video->sh;
192 switch(cmd) {
193 case DEMUXER_CTRL_GET_TIME_LENGTH:
194 *((double *)arg) = (double)mf->nr_of_files / sh_video->fps;
195 return DEMUXER_CTRL_OK;
197 case DEMUXER_CTRL_GET_PERCENT_POS:
198 if (mf->nr_of_files <= 1)
199 return DEMUXER_CTRL_DONTKNOW;
200 *((int *)arg) = 100 * mf->curr_frame / (mf->nr_of_files - 1);
201 return DEMUXER_CTRL_OK;
203 default:
204 return DEMUXER_CTRL_NOTIMPL;
208 const demuxer_desc_t demuxer_desc_mf = {
209 "mf demuxer",
210 "mf",
211 "MF",
212 "?",
213 "multiframe?, pictures demuxer",
214 DEMUXER_TYPE_MF,
215 0, // no autodetect
216 NULL,
217 demux_mf_fill_buffer,
218 demux_open_mf,
219 demux_close_mf,
220 demux_seek_mf,
221 demux_control_mf