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==16) {
139 for(y=starty;y!=endy;y+=yinc) {
140 UCHAR*line = &img[linex*y];
142 for(x=0;x<dest_width;x++) {
143 USHORT c = line[0]|line[1]<<8;
144 *dest++ = 255|(c&0x1f)<<(24+3)|(c>>5&0x1f)<<(16+3)|(c>>10&0x1f)<<(8+3);
148 } else if(bi->biBitCount==24) {
151 for(y=starty;y!=endy;y+=yinc) {
152 UCHAR*line = &img[linex*y];
154 for(x=0;x<dest_width;x++) {
155 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
159 } else if(bi->biBitCount==32) {
162 for(y=starty;y!=endy;y+=yinc) {
163 UCHAR*line = &img[linex*y];
165 for(x=0;x<dest_width;x++) {
166 *dest++ = 255|line[0]<<8|line[1]<<16|line[2]<<24;
171 fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
177 static int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
179 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
181 if(videoreader_vfw_eof(vr))
184 LPBITMAPINFOHEADER bi;
185 bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
191 fprintf(stderr, "AVIStreamGetFrame failed\n");
195 if(!bitmap_to_rgba(bi, buffer, i->width, i->height, i->flip)) {
196 fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
199 return i->width*i->height*4;
202 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
206 AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
207 i->audio_pos += samples;
211 static int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
213 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;
225 int len = readAudioBlock(i, buf, num);
228 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
233 return readAudioBlock(i, buf, num);
241 static void videoreader_vfw_close(videoreader_t* vr)
243 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
245 AVIStreamGetFrameClose(i->getframe);
247 AVIStreamRelease(i->vs); i->vs = 0;
250 AVIStreamRelease(i->as); i->vs = 0;
252 AVIFileRelease(i->avifile); i->avifile = 0;
254 AVIFileExit(); avifile_initialized=0;
256 free(vr->internal); vr->internal = 0;
259 static void videoreader_vfw_setparameter(videoreader_t*vr, char*name, char*value)
261 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
262 if(!strcmp(name, "flip")) {
263 i->flip = atoi(value);
264 } else if(!strcmp(name, "verbose")) {
265 verbose = atoi(value);
269 int videoreader_vfw_open(videoreader_t* vr, char* filename)
271 memset(vr, 0, sizeof(videoreader_t));
277 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
278 memset(i, 0, sizeof(videoreader_vfw_internal_t));
281 vr->eof = videoreader_vfw_eof;
282 vr->getimage = videoreader_vfw_getimage;
283 vr->getsamples = videoreader_vfw_getsamples;
284 vr->close = videoreader_vfw_close;
285 vr->setparameter = videoreader_vfw_setparameter;
287 if(!avifile_initialized) {
290 if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
291 fprintf(stderr, "Couldn't open %s\n", filename);
295 AVIFileInfo(i->avifile, &info, sizeof(info));
297 /* calculate framerate */
298 i->fps = (double)info.dwRate/(double)info.dwScale;
301 printf("vfw: file %s has %f fps, and %d streams\n", i->fps, info.dwStreams);
305 while(t<info.dwStreams) {
307 if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
308 break; //video_end of (working) streams
310 AVISTREAMINFO streaminfo;
311 AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
313 if (streaminfo.fccType == streamtypeVIDEO) {
316 BITMAPINFOHEADER bitmap;
317 LONG size = sizeof(bitmap);
318 AVIStreamReadFormat(stream, 0, &bitmap, &size);
323 i->width = bitmap.biWidth;
324 i->height = bitmap.biHeight;
326 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n",
327 bitmap.biWidth, bitmap.biHeight,
328 bitmap.biCompression,bitmap.biPlanes);
331 else if (streaminfo.fccType == streamtypeAUDIO) {
334 WAVEFORMATEX waveformat;
335 LONG size = sizeof(waveformat);
336 AVIStreamReadFormat(stream, 0, &waveformat, &size);
338 if(waveformat.wBitsPerSample == 16 ||
339 waveformat.wBitsPerSample == 8 ||
340 waveformat.wBitsPerSample == 1
342 i->waveformat = waveformat;
344 i->channels = waveformat.nChannels;
345 i->samplerate = waveformat.nSamplesPerSec;
347 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
355 printf("vfw: video stream: %dx%d, %.2f\n", i->width, i->height, i->fps);
357 vr->width = i->width;
358 vr->height = i->height;
361 fprintf(stderr, "AVIReader: Warning: No video stream\n");
365 printf("vfw: audio stream: %d channels, %d samples/sec", i->channels, i->samplerate);
367 vr->channels = i->channels;
368 vr->samplerate = i->samplerate;
370 fprintf(stderr, "AVIReader: Warning: No audio stream\n");
373 i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
375 fprintf(stderr, "Couldn't initialize AVIStream for %s- codec missing?\n", filename);
379 i->video_pos = AVIStreamStart(i->vs);
380 i->video_end = AVIStreamEnd(i->vs);
382 i->audio_end = 0x7fffffff;
389 int videoreader_vfw_open(videoreader_t* vr, char* filename)