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 int bitmap_to_rgba(BITMAPINFOHEADER*bi, void*buffer, const int dest_width, const int dest_height, int flip)
62 UCHAR*data = (UCHAR*)(bi+1); // actual bitmap data starts after the header
64 if(bi->biPlanes!=1 || bi->biCompression!=0 || bi->biBitCount%4!=0) {
65 /* unsupported format */
66 fprintf(stderr, "bitmap_to_rgba: unsupported format: biPlanes=%d, biCompression=%d biBitCount=%d\n",
67 bi->biPlanes, bi->biCompression, bi->biBitCount);
71 ULONG*dest = (ULONG*)buffer;
73 int width = bi->biWidth;
74 int height = bi->biHeight;
75 if(dest_width != width || dest_height != height) {
76 /* TODO: size conversion */
77 fprintf(stderr, "size mismatch: %dx%d != %dx%d\n", width, height, dest_width, dest_height);
81 /* convert the various image types to RGBA-
82 TODO: is there some way to let the Windows API do this? */
83 int bytesperpixel = ((bi->biWidth*bi->biBitCount)+7)&~7;
84 int linex = ((bytesperpixel/8)+3)&~3;
85 memset(dest, 255, dest_width*dest_height*4);//pre-fill alpha channel
87 const int starty = flip? 0 : dest_height-1;
88 const int endy = flip? dest_height : -1;
89 const int yinc = flip? 1 : -1;
92 printf("vfw: Convering scanlines %d to %d from bpp %d, %d stepping, flip=%d\n", starty, endy, bi->biBitCount, yinc, flip);
95 if(bi->biBitCount==1) {
98 for(y=starty;y!=endy;y+=yinc) {
99 UCHAR*line = &img[linex*y];
101 for(x=0;x<dest_width;x++) {
102 *dest++ = 255*((line[x/8]>>(x&7))&1);
105 } else if(bi->biBitCount==4) {
106 UCHAR*img = &data[bi->biClrUsed*4];
109 for(y=starty;y!=endy;y+=yinc) {
110 UCHAR*line = &img[linex*y];
112 for(x=0;x<dest_width/2;x++) {
113 *dest++ = 255|pal[(line[0]>>4)<<2|0]<<8|pal[(line[0]>>4)<<2|1]<<16|pal[(line[0]>>4)<<2|2]<<24;
114 *dest++ = 255|pal[(line[0]&0x0f)<<2|0]<<8|pal[(line[0]&0x0f)<<2|1]<<16|pal[(line[0]&0x0f)<<2|2]<<24;
118 } else if(bi->biBitCount==8) {
119 UCHAR*img = &data[bi->biClrUsed*4];
122 for(y=starty;y!=endy;y+=yinc) {
123 UCHAR*line = &img[linex*y];
125 for(x=0;x<dest_width;x++) {
126 *dest++ = 255|pal[line[0]*4+2]<<8|pal[line[0]*4+1]<<16|pal[line[0]*4+0]<<24;
130 } else if(bi->biBitCount==16) {
133 for(y=starty;y!=endy;y+=yinc) {
134 UCHAR*line = &img[linex*y];
136 for(x=0;x<dest_width;x++) {
137 USHORT c = line[0]|line[1]<<8;
138 *dest++ = 255|(c&0x1f)<<(24+3)|(c>>5&0x1f)<<(16+3)|(c>>10&0x1f)<<(8+3);
142 } else if(bi->biBitCount==24) {
145 for(y=starty;y!=endy;y+=yinc) {
146 UCHAR*line = &img[linex*y];
148 for(x=0;x<dest_width;x++) {
149 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
153 } else if(bi->biBitCount==32) {
156 for(y=starty;y!=endy;y+=yinc) {
157 UCHAR*line = &img[linex*y];
159 for(x=0;x<dest_width;x++) {
160 *dest++ = 255|line[0]<<8|line[1]<<16|line[2]<<24;
165 fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
171 static int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
173 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
175 if (i->video_pos >= i->video_end)
181 LPBITMAPINFOHEADER bi;
182 bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
188 fprintf(stderr, "AVIStreamGetFrame failed\n");
192 if(!bitmap_to_rgba(bi, buffer, i->width, i->height, i->flip)) {
193 fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
196 return i->width*i->height*4;
199 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
203 AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
204 i->audio_pos += samples;
208 static int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
210 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
215 switch(i->waveformat.wBitsPerSample) {
217 int len = readAudioBlock(i, buf, num);
220 ((SHORT*)buf)[t] = ((((BYTE*)buf)[t>>3])>>(t&7))<<15;
222 if(!len) i->audio_eof = 1;
226 int len = readAudioBlock(i, buf, num);
229 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
231 if(!len) i->audio_eof = 1;
235 int len = readAudioBlock(i, buf, num);
236 if(!len) i->audio_eof = 1;
245 static void videoreader_vfw_close(videoreader_t* vr)
247 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
249 AVIStreamGetFrameClose(i->getframe);
251 AVIStreamRelease(i->vs); i->vs = 0;
254 AVIStreamRelease(i->as); i->vs = 0;
256 AVIFileRelease(i->avifile); i->avifile = 0;
258 AVIFileExit(); avifile_initialized=0;
260 free(vr->internal); vr->internal = 0;
263 static void videoreader_vfw_setparameter(videoreader_t*vr, char*name, char*value)
265 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
266 if(!strcmp(name, "flip")) {
267 i->flip = atoi(value);
268 } else if(!strcmp(name, "verbose")) {
269 verbose = atoi(value);
273 int videoreader_vfw_open(videoreader_t* vr, char* filename)
275 memset(vr, 0, sizeof(videoreader_t));
281 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
282 memset(i, 0, sizeof(videoreader_vfw_internal_t));
285 vr->getimage = videoreader_vfw_getimage;
286 vr->getsamples = videoreader_vfw_getsamples;
287 vr->close = videoreader_vfw_close;
288 vr->setparameter = videoreader_vfw_setparameter;
290 if(!avifile_initialized) {
293 if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
294 fprintf(stderr, "Couldn't open %s\n", filename);
298 AVIFileInfo(i->avifile, &info, sizeof(info));
300 /* calculate framerate */
301 i->fps = (double)info.dwRate/(double)info.dwScale;
304 printf("vfw: file %s has %f fps, and %d streams\n", i->fps, info.dwStreams);
308 while(t<info.dwStreams) {
310 if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
311 break; //video_end of (working) streams
313 AVISTREAMINFO streaminfo;
314 AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
316 if (streaminfo.fccType == streamtypeVIDEO) {
319 BITMAPINFOHEADER bitmap;
320 LONG size = sizeof(bitmap);
321 AVIStreamReadFormat(stream, 0, &bitmap, &size);
326 i->width = bitmap.biWidth;
327 i->height = bitmap.biHeight;
329 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n",
330 bitmap.biWidth, bitmap.biHeight,
331 bitmap.biCompression,bitmap.biPlanes);
334 else if (streaminfo.fccType == streamtypeAUDIO) {
337 WAVEFORMATEX waveformat;
338 LONG size = sizeof(waveformat);
339 AVIStreamReadFormat(stream, 0, &waveformat, &size);
341 if(waveformat.wBitsPerSample == 16 ||
342 waveformat.wBitsPerSample == 8 ||
343 waveformat.wBitsPerSample == 1
345 i->waveformat = waveformat;
347 i->channels = waveformat.nChannels;
348 i->samplerate = waveformat.nSamplesPerSec;
350 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
358 printf("vfw: video stream: %dx%d, %.2f\n", i->width, i->height, i->fps);
360 vr->width = i->width;
361 vr->height = i->height;
364 fprintf(stderr, "AVIReader: Warning: No video stream\n");
368 printf("vfw: audio stream: %d channels, %d samples/sec", i->channels, i->samplerate);
370 vr->channels = i->channels;
371 vr->samplerate = i->samplerate;
373 fprintf(stderr, "AVIReader: Warning: No audio stream\n");
376 i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
378 fprintf(stderr, "Couldn't initialize AVIStream for %s- codec missing?\n", filename);
382 i->video_pos = AVIStreamStart(i->vs);
383 i->video_end = AVIStreamEnd(i->vs);
385 i->audio_end = 0x7fffffff;
392 int videoreader_vfw_open(videoreader_t* vr, char* filename)