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;
57 #define _TRACE_ {printf("%s: %d (%s)\n",__FILE__,__LINE__,__func__);fflush(stdout);}
59 static bool videoreader_vfw_eof(videoreader_t* vr)
61 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
62 return (i->video_pos >= i->video_end);
65 static int bitmap_to_rgba(BITMAPINFOHEADER*bi, void*buffer, const int dest_width, const int dest_height, int flip)
67 UCHAR*data = (UCHAR*)(bi+1); // actual bitmap data starts after the header
69 if(bi->biPlanes!=1 || bi->biCompression!=0 || bi->biBitCount%4!=0) {
70 /* unsupported format */
71 fprintf(stderr, "bitmap_to_rgba: unsupported format: biPlanes=%d, biCompression=%d biBitCount=%d\n",
72 bi->biPlanes, bi->biCompression, bi->biBitCount);
76 ULONG*dest = (ULONG*)buffer;
78 int width = bi->biWidth;
79 int height = bi->biHeight;
80 if(dest_width != width || dest_height != height) {
81 /* TODO: size conversion */
82 fprintf(stderr, "size mismatch: %dx%d != %dx%d\n", width, height, dest_width, dest_height);
86 /* convert the various image types to RGBA-
87 TODO: is there some way to let the Windows API do this? */
88 int bytesperpixel = ((bi->biWidth*bi->biBitCount)+7)&~7;
89 int linex = ((bytesperpixel/8)+3)&~3;
90 memset(dest, 255, dest_width*dest_height*4);//pre-fill alpha channel
92 const int starty = flip? 0 : dest_height-1;
93 const int endy = flip? dest_height : -1;
94 const int yinc = flip? 1 : -1;
96 if(bi->biBitCount==1) {
99 for(y=starty;y!=endy;y+=yinc) {
100 UCHAR*line = &img[linex*y];
102 for(x=0;x<dest_width;x++) {
103 *dest++ = 255*((line[x/8]>>(x&7))&1);
106 } else if(bi->biBitCount==4) {
107 UCHAR*img = &data[bi->biClrUsed*4];
110 for(y=starty;y!=endy;y+=yinc) {
111 UCHAR*line = &img[linex*y];
113 for(x=0;x<dest_width/2;x++) {
114 *dest++ = 255|pal[(line[0]>>4)<<2|0]<<8|pal[(line[0]>>4)<<2|1]<<16|pal[(line[0]>>4)<<2|2]<<24;
115 *dest++ = 255|pal[(line[0]&0x0f)<<2|0]<<8|pal[(line[0]&0x0f)<<2|1]<<16|pal[(line[0]&0x0f)<<2|2]<<24;
119 } else if(bi->biBitCount==8) {
120 UCHAR*img = &data[bi->biClrUsed*4];
123 for(y=starty;y!=endy;y+=yinc) {
124 UCHAR*line = &img[linex*y];
126 for(x=0;x<dest_width;x++) {
127 *dest++ = 255|pal[line[0]*4+2]<<8|pal[line[0]*4+1]<<16|pal[line[0]*4+0]<<24;
131 } else if(bi->biBitCount==24) {
134 for(y=starty;y!=endy;y+=yinc) {
135 UCHAR*line = &img[linex*y];
137 for(x=0;x<dest_width;x++) {
138 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
142 } else if(bi->biBitCount==32) {
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[0]<<8|line[1]<<16|line[2]<<24;
154 fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
160 static int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
162 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
164 if(videoreader_vfw_eof(vr))
167 LPBITMAPINFOHEADER bi;
168 bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
174 fprintf(stderr, "AVIStreamGetFrame failed\n");
178 if(!bitmap_to_rgba(bi, buffer, i->width, i->height, i->flip)) {
179 fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
182 return i->width*i->height*4;
185 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
189 AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
190 i->audio_pos += samples;
194 static int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
196 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
198 switch(i->waveformat.wBitsPerSample) {
200 int len = readAudioBlock(i, buf, num);
203 ((SHORT*)buf)[t] = ((((BYTE*)buf)[t>>3])>>(t&7))<<15;
208 int len = readAudioBlock(i, buf, num);
211 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
216 return readAudioBlock(i, buf, num);
224 static void videoreader_vfw_close(videoreader_t* vr)
226 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
228 AVIStreamGetFrameClose(i->getframe);
230 AVIStreamRelease(i->vs); i->vs = 0;
233 AVIStreamRelease(i->as); i->vs = 0;
235 AVIFileRelease(i->avifile); i->avifile = 0;
237 AVIFileExit(); avifile_initialized=0;
239 free(vr->internal); vr->internal = 0;
242 static void videoreader_vfw_setparameter(videoreader_t*vr, char*name, char*value)
244 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
245 if(!strcmp(name, "flip")) {
246 i->flip = atoi(value);
250 int videoreader_vfw_open(videoreader_t* vr, char* filename)
252 memset(vr, 0, sizeof(videoreader_t));
258 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
259 memset(i, 0, sizeof(videoreader_vfw_internal_t));
262 vr->eof = videoreader_vfw_eof;
263 vr->getimage = videoreader_vfw_getimage;
264 vr->getsamples = videoreader_vfw_getsamples;
265 vr->close = videoreader_vfw_close;
266 vr->setparameter = videoreader_vfw_setparameter;
268 if(!avifile_initialized) {
271 if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
272 fprintf(stderr, "Couldn't open %s\n", filename);
276 AVIFileInfo(i->avifile, &info, sizeof(info));
278 /* calculate framerate */
279 i->fps = (double)info.dwRate/(double)info.dwScale;
282 while(t<info.dwStreams) {
284 if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
285 break; //video_end of (working) streams
287 AVISTREAMINFO streaminfo;
288 AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
290 if (streaminfo.fccType == streamtypeVIDEO) {
293 BITMAPINFOHEADER bitmap;
294 LONG size = sizeof(bitmap);
295 AVIStreamReadFormat(stream, 0, &bitmap, &size);
300 i->width = bitmap.biWidth;
301 i->height = bitmap.biHeight;
303 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n",
304 bitmap.biWidth, bitmap.biHeight,
305 bitmap.biCompression,bitmap.biPlanes);
308 else if (streaminfo.fccType == streamtypeAUDIO) {
311 WAVEFORMATEX waveformat;
312 LONG size = sizeof(waveformat);
313 AVIStreamReadFormat(stream, 0, &waveformat, &size);
315 if(waveformat.wBitsPerSample == 16 ||
316 waveformat.wBitsPerSample == 8 ||
317 waveformat.wBitsPerSample == 1
319 i->waveformat = waveformat;
321 i->channels = waveformat.nChannels;
322 i->samplerate = waveformat.nSamplesPerSec;
324 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
331 vr->width = i->width;
332 vr->height = i->height;
335 fprintf(stderr, "AVIReader: Warning: No video stream\n");
338 vr->channels = i->channels;
339 vr->samplerate = i->samplerate;
341 fprintf(stderr, "AVIReader: Warning: No audio stream\n");
344 i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
346 fprintf(stderr, "Couldn't initialize AVIStream for %s- codec missing?\n", filename);
350 i->video_pos = AVIStreamStart(i->vs);
351 i->video_end = AVIStreamEnd(i->vs);
353 i->audio_end = 0x7fffffff;
360 int videoreader_vfw_open(videoreader_t* vr, char* filename)