2 Read avi files using Video For Windows (vfw).
4 Part of the swftools package.
6 Copyright (c) 2004 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include "videoreader.h"
29 typedef struct _videoreader_vfw_internal {
37 BITMAPINFOHEADER bitmap;
38 WAVEFORMATEX waveformat;
53 } videoreader_vfw_internal_t;
55 static int avifile_initialized = 0;
58 #define _TRACE_ {printf("vfw: %s: %d (%s)\n",__FILE__,__LINE__,__func__);fflush(stdout);}
60 static bool videoreader_vfw_eof(videoreader_t* vr)
62 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
63 return (i->video_pos >= i->video_end);
66 static int bitmap_to_rgba(BITMAPINFOHEADER*bi, void*buffer, const int dest_width, const int dest_height, int flip)
68 UCHAR*data = (UCHAR*)(bi+1); // actual bitmap data starts after the header
70 if(bi->biPlanes!=1 || bi->biCompression!=0 || bi->biBitCount%4!=0) {
71 /* unsupported format */
72 fprintf(stderr, "bitmap_to_rgba: unsupported format: biPlanes=%d, biCompression=%d biBitCount=%d\n",
73 bi->biPlanes, bi->biCompression, bi->biBitCount);
77 ULONG*dest = (ULONG*)buffer;
79 int width = bi->biWidth;
80 int height = bi->biHeight;
81 if(dest_width != width || dest_height != height) {
82 /* TODO: size conversion */
83 fprintf(stderr, "size mismatch: %dx%d != %dx%d\n", width, height, dest_width, dest_height);
87 /* convert the various image types to RGBA-
88 TODO: is there some way to let the Windows API do this? */
89 int bytesperpixel = ((bi->biWidth*bi->biBitCount)+7)&~7;
90 int linex = ((bytesperpixel/8)+3)&~3;
91 memset(dest, 255, dest_width*dest_height*4);//pre-fill alpha channel
93 const int starty = flip? 0 : dest_height-1;
94 const int endy = flip? dest_height : -1;
95 const int yinc = flip? 1 : -1;
98 printf("vfw: Convering scanlines %d to %d from bpp %d, %d stepping, flip=%d\n", starty, endy, bi->biBitCount, yinc, flip);
101 if(bi->biBitCount==1) {
104 for(y=starty;y!=endy;y+=yinc) {
105 UCHAR*line = &img[linex*y];
107 for(x=0;x<dest_width;x++) {
108 *dest++ = 255*((line[x/8]>>(x&7))&1);
111 } else if(bi->biBitCount==4) {
112 UCHAR*img = &data[bi->biClrUsed*4];
115 for(y=starty;y!=endy;y+=yinc) {
116 UCHAR*line = &img[linex*y];
118 for(x=0;x<dest_width/2;x++) {
119 *dest++ = 255|pal[(line[0]>>4)<<2|0]<<8|pal[(line[0]>>4)<<2|1]<<16|pal[(line[0]>>4)<<2|2]<<24;
120 *dest++ = 255|pal[(line[0]&0x0f)<<2|0]<<8|pal[(line[0]&0x0f)<<2|1]<<16|pal[(line[0]&0x0f)<<2|2]<<24;
124 } else if(bi->biBitCount==8) {
125 UCHAR*img = &data[bi->biClrUsed*4];
128 for(y=starty;y!=endy;y+=yinc) {
129 UCHAR*line = &img[linex*y];
131 for(x=0;x<dest_width;x++) {
132 *dest++ = 255|pal[line[0]*4+2]<<8|pal[line[0]*4+1]<<16|pal[line[0]*4+0]<<24;
136 } else if(bi->biBitCount==24) {
139 for(y=starty;y!=endy;y+=yinc) {
140 UCHAR*line = &img[linex*y];
142 for(x=0;x<dest_width;x++) {
143 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
147 } else if(bi->biBitCount==32) {
150 for(y=starty;y!=endy;y+=yinc) {
151 UCHAR*line = &img[linex*y];
153 for(x=0;x<dest_width;x++) {
154 *dest++ = 255|line[0]<<8|line[1]<<16|line[2]<<24;
159 fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
165 static int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
167 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
169 if(videoreader_vfw_eof(vr))
172 LPBITMAPINFOHEADER bi;
173 bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
179 fprintf(stderr, "AVIStreamGetFrame failed\n");
183 if(!bitmap_to_rgba(bi, buffer, i->width, i->height, i->flip)) {
184 fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
187 return i->width*i->height*4;
190 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
194 AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
195 i->audio_pos += samples;
199 static int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
201 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
203 switch(i->waveformat.wBitsPerSample) {
205 int len = readAudioBlock(i, buf, num);
208 ((SHORT*)buf)[t] = ((((BYTE*)buf)[t>>3])>>(t&7))<<15;
213 int len = readAudioBlock(i, buf, num);
216 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
221 return readAudioBlock(i, buf, num);
229 static void videoreader_vfw_close(videoreader_t* vr)
231 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
233 AVIStreamGetFrameClose(i->getframe);
235 AVIStreamRelease(i->vs); i->vs = 0;
238 AVIStreamRelease(i->as); i->vs = 0;
240 AVIFileRelease(i->avifile); i->avifile = 0;
242 AVIFileExit(); avifile_initialized=0;
244 free(vr->internal); vr->internal = 0;
247 static void videoreader_vfw_setparameter(videoreader_t*vr, char*name, char*value)
249 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
250 if(!strcmp(name, "flip")) {
251 i->flip = atoi(value);
252 } else if(!strcmp(name, "verbose")) {
253 verbose = atoi(value);
257 int videoreader_vfw_open(videoreader_t* vr, char* filename)
259 memset(vr, 0, sizeof(videoreader_t));
265 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
266 memset(i, 0, sizeof(videoreader_vfw_internal_t));
269 vr->eof = videoreader_vfw_eof;
270 vr->getimage = videoreader_vfw_getimage;
271 vr->getsamples = videoreader_vfw_getsamples;
272 vr->close = videoreader_vfw_close;
273 vr->setparameter = videoreader_vfw_setparameter;
275 if(!avifile_initialized) {
278 if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
279 fprintf(stderr, "Couldn't open %s\n", filename);
283 AVIFileInfo(i->avifile, &info, sizeof(info));
285 /* calculate framerate */
286 i->fps = (double)info.dwRate/(double)info.dwScale;
289 printf("vfw: file %s has %f fps, and %d streams\n", i->fps, info.dwStreams);
293 while(t<info.dwStreams) {
295 if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
296 break; //video_end of (working) streams
298 AVISTREAMINFO streaminfo;
299 AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
301 if (streaminfo.fccType == streamtypeVIDEO) {
304 BITMAPINFOHEADER bitmap;
305 LONG size = sizeof(bitmap);
306 AVIStreamReadFormat(stream, 0, &bitmap, &size);
311 i->width = bitmap.biWidth;
312 i->height = bitmap.biHeight;
314 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n",
315 bitmap.biWidth, bitmap.biHeight,
316 bitmap.biCompression,bitmap.biPlanes);
319 else if (streaminfo.fccType == streamtypeAUDIO) {
322 WAVEFORMATEX waveformat;
323 LONG size = sizeof(waveformat);
324 AVIStreamReadFormat(stream, 0, &waveformat, &size);
326 if(waveformat.wBitsPerSample == 16 ||
327 waveformat.wBitsPerSample == 8 ||
328 waveformat.wBitsPerSample == 1
330 i->waveformat = waveformat;
332 i->channels = waveformat.nChannels;
333 i->samplerate = waveformat.nSamplesPerSec;
335 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
343 printf("vfw: video stream: %dx%d, %.2f\n", i->width, i->height, i->fps);
345 vr->width = i->width;
346 vr->height = i->height;
349 fprintf(stderr, "AVIReader: Warning: No video stream\n");
353 printf("vfw: audio stream: %d channels, %d samples/sec", i->channels, i->samplerate);
355 vr->channels = i->channels;
356 vr->samplerate = i->samplerate;
358 fprintf(stderr, "AVIReader: Warning: No audio stream\n");
361 i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
363 fprintf(stderr, "Couldn't initialize AVIStream for %s- codec missing?\n", filename);
367 i->video_pos = AVIStreamStart(i->vs);
368 i->video_end = AVIStreamEnd(i->vs);
370 i->audio_end = 0x7fffffff;
377 int videoreader_vfw_open(videoreader_t* vr, char* filename)