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 */
27 #include "videoreader.h"
29 typedef struct _videoreader_vfw_internal {
37 BITMAPINFOHEADER bitmap;
38 WAVEFORMATEX waveformat;
52 } videoreader_vfw_internal_t;
54 static int avifile_initialized = 0;
56 #define _TRACE_ {printf("%s: %d (%s)\n",__FILE__,__LINE__,__func__);fflush(stdout);}
58 bool videoreader_vfw_eof(videoreader_t* vr)
60 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
61 return (i->video_pos >= i->video_end);
64 static int bitmap_to_rgba(BITMAPINFOHEADER*bi, void*buffer, const int dest_width, const int dest_height)
66 UCHAR*data = (UCHAR*)(bi+1); // actual bitmap data starts after the header
68 if(bi->biPlanes!=1 || bi->biCompression!=0 || bi->biBitCount%4!=0) {
69 /* unsupported format */
70 fprintf(stderr, "bitmap_to_rgba: unsupported format: biPlanes=%d, biCompression=%d biBitCount=%d\n",
71 bi->biPlanes, bi->biCompression, bi->biBitCount);
75 ULONG*dest = (ULONG*)buffer;
77 int width = bi->biWidth;
78 int height = bi->biHeight;
79 if(dest_width != width || dest_height != height) {
80 /* TODO: size conversion */
81 fprintf(stderr, "size mismatch: %dx%d != %dx%d\n", width, height, dest_width, dest_height);
85 /* convert the various image types to RGBA-
86 TODO: is there some way to let the Windows API do this? */
87 int bytesperpixel = ((bi->biWidth*bi->biBitCount)+7)&~7;
88 int linex = ((bytesperpixel/8)+3)&~3;
89 memset(dest, 255, dest_width*dest_height*4);//pre-fill alpha channel
90 if(bi->biBitCount==1) {
93 for(y=0;y<dest_height;y++) {
94 UCHAR*line = &img[linex*y];
96 for(x=0;x<dest_width;x++) {
97 *dest++ = 255*((line[x/8]>>(x&7))&1);
100 } else if(bi->biBitCount==4) {
102 UCHAR*img = &data[bi->biClrUsed*4];
104 for(y=0;y<dest_height;y++) {
105 UCHAR*line = &img[linex*y];
107 for(x=0;x<dest_width/2;x++) {
108 *dest++ = 255|pal[(line[0]>>4)<<2|0]<<8|pal[(line[0]>>4)<<2|1]<<16|pal[(line[0]>>4)<<2|2]<<24;
109 *dest++ = 255|pal[(line[0]&0x0f)<<2|0]<<8|pal[(line[0]&0x0f)<<2|1]<<16|pal[(line[0]&0x0f)<<2|2]<<24;
113 } else if(bi->biBitCount==8) {
115 UCHAR*img = &data[bi->biClrUsed*4];
117 for(y=0;y<dest_height;y++) {
118 UCHAR*line = &img[linex*y];
120 for(x=0;x<dest_width;x++) {
121 *dest++ = 255|pal[line[0]*4+2]<<8|pal[line[0]*4+1]<<16|pal[line[0]*4+0]<<24;
125 } else if(bi->biBitCount==24) {
128 for(y=0;y<dest_height;y++) {
129 UCHAR*line = &img[linex*y];
131 for(x=0;x<dest_width;x++) {
132 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
136 } else if(bi->biBitCount==32) {
139 for(y=0;y<dest_height;y++) {
140 UCHAR*line = &img[linex*y];
142 for(x=0;x<dest_width;x++) {
143 *dest++ = 255|line[0]<<8|line[1]<<16|line[2]<<24;
148 fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
154 int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
156 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
158 if(videoreader_vfw_eof(vr))
161 LPBITMAPINFOHEADER bi;
162 bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
168 fprintf(stderr, "AVIStreamGetFrame failed\n");
172 if(!bitmap_to_rgba(bi, buffer, i->width, i->height)) {
173 fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
176 return i->width*i->height*4;
179 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
183 AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
184 i->audio_pos += samples;
188 int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
190 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
192 switch(i->waveformat.wBitsPerSample) {
194 int len = readAudioBlock(i, buf, num);
197 ((SHORT*)buf)[t] = ((((BYTE*)buf)[t>>3])>>(t&7))<<15;
202 int len = readAudioBlock(i, buf, num);
205 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
210 return readAudioBlock(i, buf, num);
218 void videoreader_vfw_close(videoreader_t* vr)
220 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
222 AVIStreamGetFrameClose(i->getframe);
224 AVIStreamRelease(i->vs); i->vs = 0;
227 AVIStreamRelease(i->as); i->vs = 0;
229 AVIFileRelease(i->avifile); i->avifile = 0;
231 AVIFileExit(); avifile_initialized=0;
233 free(vr->internal); vr->internal = 0;
236 void videoreader_vfw_setparameter(videoreader_t* vr, char*name, char*value) {}
238 int videoreader_vfw_open(videoreader_t* vr, char* filename)
240 memset(vr, 0, sizeof(videoreader_t));
246 videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
247 memset(i, 0, sizeof(videoreader_vfw_internal_t));
250 vr->eof = videoreader_vfw_eof;
251 vr->getimage = videoreader_vfw_getimage;
252 vr->getsamples = videoreader_vfw_getsamples;
253 vr->close = videoreader_vfw_close;
254 vr->setparameter = videoreader_vfw_setparameter;
256 if(!avifile_initialized) {
259 if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
260 fprintf(stderr, "Couldn't open %s\n", filename);
264 AVIFileInfo(i->avifile, &info, sizeof(info));
266 /* calculate framerate */
267 i->fps = (double)info.dwRate/(double)info.dwScale;
270 while(t<info.dwStreams) {
272 if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
273 break; //video_end of (working) streams
275 AVISTREAMINFO streaminfo;
276 AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
278 if (streaminfo.fccType == streamtypeVIDEO) {
281 BITMAPINFOHEADER bitmap;
282 LONG size = sizeof(bitmap);
283 AVIStreamReadFormat(stream, 0, &bitmap, &size);
288 i->width = bitmap.biWidth;
289 i->height = bitmap.biHeight;
291 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n",
292 bitmap.biWidth, bitmap.biHeight,
293 bitmap.biCompression,bitmap.biPlanes);
296 else if (streaminfo.fccType == streamtypeAUDIO) {
299 WAVEFORMATEX waveformat;
300 LONG size = sizeof(waveformat);
301 AVIStreamReadFormat(stream, 0, &waveformat, &size);
303 if(waveformat.wBitsPerSample == 16 ||
304 waveformat.wBitsPerSample == 8 ||
305 waveformat.wBitsPerSample == 1
307 i->waveformat = waveformat;
309 i->channels = waveformat.nChannels;
310 i->samplerate = waveformat.nSamplesPerSec;
312 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
319 vr->width = i->width;
320 vr->height = i->height;
323 fprintf(stderr, "AVIReader: Warning: No video stream\n");
326 vr->channels = i->channels;
327 vr->samplerate = i->samplerate;
329 fprintf(stderr, "AVIReader: Warning: No audio stream\n");
332 i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
334 fprintf(stderr, "Couldn't initialize AVIStream for %s- codec missing?\n", filename);
338 i->video_pos = AVIStreamStart(i->vs);
339 i->video_end = AVIStreamEnd(i->vs);
341 i->audio_end = 0x7fffffff;
348 int videoreader_vfw_open(videoreader_t* vr, char* filename)