several more bugfixes.
[swftools.git] / avi2swf / videoreader_vfw.cc
1 /* videoreader_vfw.cc
2    Read avi files using Video For Windows (vfw).
3
4    Part of the swftools package.
5    
6    Copyright (c) 2004 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #ifdef WIN32
23 #include <windows.h>
24 #include <vfw.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "videoreader.h"
28
29 typedef struct _videoreader_vfw_internal { 
30     //video:
31     PAVISTREAM vs;
32     //audio:
33     PAVISTREAM as;
34
35     PGETFRAME getframe;
36     IAVIFile* avifile;
37     BITMAPINFOHEADER bitmap;
38     WAVEFORMATEX waveformat;
39
40     int video_pos;
41     int video_end;
42
43     int audio_pos;
44     int audio_end;
45
46     float fps;
47     int width,height;
48
49     int samplerate;
50     int channels;
51
52 } videoreader_vfw_internal_t;
53
54 static int avifile_initialized = 0;
55
56 #define _TRACE_ {printf("%s: %d (%s)\n",__FILE__,__LINE__,__func__);fflush(stdout);}
57
58 bool videoreader_vfw_eof(videoreader_t* vr)
59 {
60     videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
61     return (i->video_pos >= i->video_end);
62 }
63
64 static int bitmap_to_rgba(BITMAPINFOHEADER*bi, void*buffer, const int dest_width, const int dest_height)
65 {
66     UCHAR*data = (UCHAR*)(bi+1); // actual bitmap data starts after the header
67
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);
72         return 0;
73     }
74     
75     ULONG*dest = (ULONG*)buffer;
76
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);
82         return 0;
83     }
84
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) {
91         int y;
92         UCHAR*img = data;
93         for(y=0;y<dest_height;y++) {
94             UCHAR*line = &img[linex*y];
95             int x;
96             for(x=0;x<dest_width;x++) {
97                 *dest++ = 255*((line[x/8]>>(x&7))&1);
98             }
99         }
100     } else if(bi->biBitCount==4) {
101         int y;
102         UCHAR*img = &data[bi->biClrUsed*4];
103         UCHAR*pal = data;
104         for(y=0;y<dest_height;y++) {
105             UCHAR*line = &img[linex*y];
106             int x;
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;
110                 line++;
111             }
112         }
113     } else if(bi->biBitCount==8) {
114         int y;
115         UCHAR*img = &data[bi->biClrUsed*4];
116         UCHAR*pal = data;
117         for(y=0;y<dest_height;y++) {
118             UCHAR*line = &img[linex*y];
119             int x;
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;
122                 line++;
123             }
124         }
125     } else if(bi->biBitCount==24) {
126         UCHAR*img = data;
127         int y;
128         for(y=0;y<dest_height;y++) {
129             UCHAR*line = &img[linex*y];
130             int x;
131             for(x=0;x<dest_width;x++) {
132                 *dest++ = 255|line[2]<<8|line[1]<<16|line[0]<<24;
133                 line+=3;
134             }
135         }
136     } else if(bi->biBitCount==32) {
137         UCHAR*img = data;
138         int y;
139         for(y=0;y<dest_height;y++) {
140             UCHAR*line = &img[linex*y];
141             int x;
142             for(x=0;x<dest_width;x++) {
143                 *dest++ = 255|line[0]<<8|line[1]<<16|line[2]<<24;
144                 line+=4;
145             }
146         }
147     } else {
148         fprintf(stderr, "Unsupported format: bitcount=%d\n", bi->biBitCount);
149         return 0;
150     }
151     return 1;
152 }
153
154 int videoreader_vfw_getimage(videoreader_t* vr, void*buffer)
155 {
156     videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
157
158     if(videoreader_vfw_eof(vr))
159         return 0;
160
161     LPBITMAPINFOHEADER bi;
162     bi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(i->getframe, i->video_pos);
163         
164     i->video_pos++;
165
166     if(!bi) {
167         fprintf(stderr, "AVIStreamGetFrame failed\n");
168         return 0;
169     }
170     
171     if(!bitmap_to_rgba(bi, buffer, i->width, i->height)) {
172         fprintf(stderr, "couldn't convert bitmap to RGBA.\n");
173         return 0;
174     }
175     return i->width*i->height*4;
176 }
177
178 static int readAudioBlock(videoreader_vfw_internal_t* i, void*buf, int len)
179 {
180     LONG bytes;
181     LONG samples;
182     AVIStreamRead(i->as, i->audio_pos, len/(2*i->waveformat.nChannels), buf, len, &bytes, &samples);
183     i->audio_pos += samples;
184     return bytes;
185 }
186
187 int videoreader_vfw_getsamples(videoreader_t* vr, void*buf, int num)
188 {
189     videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
190    
191     switch(i->waveformat.wBitsPerSample) {
192         case 1: {
193             int len = readAudioBlock(i, buf, num);
194             int t = len-1;
195             do {
196                 ((SHORT*)buf)[t] = ((((BYTE*)buf)[t>>3])>>(t&7))<<15;
197             } while(--t>=0);
198             return len*8;
199         }
200         case 8: {
201             int len = readAudioBlock(i, buf, num);
202             int t = len-1;
203             do {
204                 ((SHORT*)buf)[t] = (((BYTE*)buf)[t]<<8)^0x8000;
205             } while(--t>=0);
206             return len*2;
207         }
208         case 16: {
209             return readAudioBlock(i, buf, num);
210         }
211         default: {
212             return 0;
213         }
214     }
215 }
216
217 void videoreader_vfw_close(videoreader_t* vr)
218 {
219     videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)vr->internal;
220
221     AVIStreamGetFrameClose(i->getframe);
222     if(i->vs) {
223         AVIStreamRelease(i->vs); i->vs = 0;
224     }
225     if(i->as) {
226         AVIStreamRelease(i->as); i->vs = 0;
227     }
228     AVIFileRelease(i->avifile); i->avifile = 0;
229     
230     AVIFileExit(); avifile_initialized=0;
231
232     free(vr->internal); vr->internal = 0;
233 }
234
235 void videoreader_vfw_setparameter(videoreader_t* vr, char*name, char*value) {}
236
237 int videoreader_vfw_open(videoreader_t* vr, char* filename)
238 {
239     memset(vr, 0, sizeof(videoreader_t));
240     if(!filename) {
241         /* codec query */
242         return 1;
243     }
244
245     videoreader_vfw_internal_t* i = (videoreader_vfw_internal_t*)malloc(sizeof(videoreader_vfw_internal_t));
246     memset(i, 0, sizeof(videoreader_vfw_internal_t));
247
248     vr->internal = i;
249     vr->eof = videoreader_vfw_eof;
250     vr->getimage = videoreader_vfw_getimage;
251     vr->getsamples = videoreader_vfw_getsamples;
252     vr->close = videoreader_vfw_close;
253     vr->setparameter = videoreader_vfw_setparameter;
254
255     if(!avifile_initialized) {
256         AVIFileInit();
257     }
258     if(AVIFileOpen(&i->avifile, filename, OF_SHARE_DENY_WRITE, 0)) {
259         fprintf(stderr, "Couldn't open %s\n", filename);
260         return -1;
261     }
262     AVIFILEINFO info;
263     AVIFileInfo(i->avifile, &info, sizeof(info));
264    
265     /* calculate framerate */
266     i->fps = (double)info.dwRate/(double)info.dwScale;
267
268     unsigned int t=0;
269     while(t<info.dwStreams) {
270         PAVISTREAM stream;
271         if(AVIFileGetStream(i->avifile, &stream, streamtypeANY, t) != AVIERR_OK || !stream)
272             break; //video_end of (working) streams
273
274         AVISTREAMINFO streaminfo;
275         AVIStreamInfo(stream, &streaminfo, sizeof(streaminfo));
276
277         if (streaminfo.fccType == streamtypeVIDEO) {
278             /* video stream */
279
280             BITMAPINFOHEADER bitmap;
281             LONG size = sizeof(bitmap);
282             AVIStreamReadFormat(stream, 0, &bitmap, &size);
283
284             if(1) {
285                 i->bitmap = bitmap;
286                 i->vs = stream;
287                 i->width = bitmap.biWidth;
288                 i->height = bitmap.biHeight;
289             } else {
290                 fprintf(stderr, "Ignoring video stream: %dx%d compression=%d planes=%d\n", 
291                         bitmap.biWidth, bitmap.biHeight,
292                         bitmap.biCompression,bitmap.biPlanes);
293             }
294         }
295         else if (streaminfo.fccType == streamtypeAUDIO) {
296             /* audio stream */
297
298             WAVEFORMATEX waveformat;
299             LONG size = sizeof(waveformat);
300             AVIStreamReadFormat(stream, 0, &waveformat, &size);
301
302             if(waveformat.wBitsPerSample == 16 || 
303                waveformat.wBitsPerSample == 8 ||
304                waveformat.wBitsPerSample == 1
305                ) {
306                 i->waveformat = waveformat;
307                 i->as = stream;
308                 i->channels = waveformat.nChannels;
309                 i->samplerate = waveformat.nSamplesPerSec;
310             } else {
311                 fprintf(stderr, "Ignoring audio stream: bitspersample=%d\n", waveformat.wBitsPerSample);
312             }
313         }
314         t++;
315     }
316
317     if(i->vs) {
318         vr->width = i->width;
319         vr->height = i->height;
320         vr->fps = i->fps;
321     } else {
322         fprintf(stderr, "AVIReader: Warning: No video stream\n");
323     }
324     if(i->as) {
325         vr->channels = i->channels;
326         vr->samplerate = i->samplerate;
327     } else {
328         fprintf(stderr, "AVIReader: Warning: No audio stream\n");
329     }
330     
331     i->getframe = AVIStreamGetFrameOpen(i->vs, 0);
332     if(!i->getframe) {
333         return -1;
334     }
335     
336     i->video_pos = AVIStreamStart(i->vs);
337     i->video_end = AVIStreamEnd(i->vs);
338     i->audio_pos = 0;
339     i->audio_end = 0x7fffffff;
340
341     return 0;
342 }
343
344 #else //WIN32
345
346 int videoreader_vfw_open(videoreader_t* vr, char* filename)
347 {
348     return -1;
349 }
350
351 #endif //WIN32
352