optimized freetype detection.
[swftools.git] / avi2swf / videoreader_avifile.cc
1 /* videoreader_avifile.cc
2    Read avi files using the avifile library.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 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 #include <stdlib.h>
23 #include <stdio.h>
24 #include <memory.h>
25 #include "../config.h"
26 #include "videoreader.h"
27
28 #ifdef HAVE_AVIFILE
29
30 #undef HAVE_CONFIG_H
31
32 #ifdef HAVE_VERSION_H
33   #include <version.h>
34 #endif
35 #ifdef HAVE_AVIFILE_VERSION_H
36   #include <avifile/version.h>
37 #endif
38
39 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6) 
40    #include <avifile.h>
41    #include <aviplay.h>
42    #include <fourcc.h>
43    #include <creators.h>
44    #include <StreamInfo.h>
45    #define VERSION6
46 #else
47    #include <avifile.h>
48    #include <aviplay.h>
49    #include <aviutil.h>
50    #define Width width
51    #define Height height
52    #define Data data
53    #define Bpp bpp
54 #endif
55
56 #ifdef HAVE_SIGNAL_H
57 #ifdef HAVE_PTHREAD_H
58 #include <pthread.h>
59 #include <signal.h>
60 #define DO_SIGNALS
61 #endif
62 #endif
63
64 #include "../lib/q.h"
65
66 static int verbose = 0;
67
68 typedef struct _videoreader_avifile_internal
69 {
70     IAviReadFile* player;
71     IAviReadStream* astream;
72     IAviReadStream* vstream;
73     int do_audio;
74     int do_video;
75     int video_eof;
76     int audio_eof;
77     int flip;
78     int frame;
79     int soundbits;
80     ringbuffer_t audio_buffer;
81 } videoreader_avifile_internal;
82
83 static int readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
84 {
85     int ret;
86     while(i->audio_buffer.available < buffer_size) {
87         unsigned int samples_read = 0, bytes_read = 0;
88         ret = i->astream->ReadFrames(buffer, buffer_size, numsamples, samples_read, bytes_read);
89         if(samples_read<=0) {
90             int l = i->audio_buffer.available; 
91             ringbuffer_read(&i->audio_buffer, buffer, l);
92             return l;
93         }
94         ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
95     }
96     ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
97     return buffer_size;
98 }
99 static int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
100 {
101     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
102     if(verbose) {
103         printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
104     }
105     if(i->audio_eof)
106         return 0;
107     if(i->soundbits == 8) {
108         int num_read = readSamples(i, buffer, num/2, num/(v->channels*2))*2;
109         unsigned char*b = (unsigned char*)buffer;
110         int t;
111         for(t=num_read-2;t>=0;t-=2) {
112             unsigned char x = b[t/2];
113             b[t] = 0;
114             b[t+1] = x-128;
115         }
116         if(!num_read) i->audio_eof=1;
117         return num_read;
118     }
119     if(i->soundbits == 16) {
120         int num_read = readSamples(i, buffer, num, num/(v->channels*2));
121         if(!num_read) i->audio_eof=1;
122         return num_read;
123     }
124     return 0;
125 }
126 static int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
127 {
128     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
129     if(verbose) {
130         printf("videoreader_getimage()\n");fflush(stdout);
131     }
132
133     if(i->video_eof)
134         return 0;
135
136     if(i->vstream->ReadFrame() < 0) {
137         if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
138         i->video_eof = 1;
139         return 0;
140     }
141     CImage*img2 = 0;
142     CImage*img = i->vstream->GetFrame();
143     if(!img) {
144         if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
145         i->video_eof = 1;
146         return 0;
147     }
148     /* we convert the image to YUV first, because we can convert to RGB from YUV only */
149     img->ToYUV();
150     img->ToRGB();
151     if(img->Bpp() != 3) {
152         if(verbose) printf("Warning: converthing from bpp %d to bpp 3, this fails on older avifile versions...\n", img->Bpp());
153         BitmapInfo tmp(v->width, v->height, 24);
154         img2 = new CImage(img, &tmp);
155         img = img2;
156     }
157
158     v->frame++;
159     i->frame++;
160     unsigned char*data = img->Data();
161     int bpp = img->Bpp();
162     if(bpp == 3) {
163         int x,y;
164         for(y=0;y<v->height;y++) {
165             unsigned char*from,*to;
166             to = &((unsigned char*)buffer)[y*v->width*4];
167             if(i->flip)
168                 from = img->At(v->height-y-1);
169             else
170                 from = img->At(y);
171             for(x=0;x<v->width;x++) {
172                 to[x*4+0] = 0;
173                 to[x*4+1] = from[x*3+2];
174                 to[x*4+2] = from[x*3+1];
175                 to[x*4+3] = from[x*3+0];
176             }
177         }
178         if(img2) delete img2;
179         return v->width*v->height*4;
180     } else {
181         if(img2) delete img2;
182         if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
183         i->video_eof = 1;
184         return 0;
185     }
186 }
187 static void videoreader_avifile_close(videoreader_t* v)
188 {
189     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
190     if(verbose) {
191         printf("videoreader_close()\n");fflush(stdout);
192     }
193     if(i->do_audio) {
194         ringbuffer_clear(&i->audio_buffer);
195     }
196 }
197 static void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
198 {
199     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
200     if(!strcmp(name, "verbose")) {
201         verbose = atoi(value);
202     }
203     if(!strcmp(name, "flip")) {
204         i->flip = atoi(value);
205     }
206     if(verbose) {
207         printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
208     }
209 }
210
211 int videoreader_avifile_open(videoreader_t* v, char* filename)
212 {
213     if(!filename) {
214         /* codec query */
215         return 0;
216     }
217     videoreader_avifile_internal* i;
218     i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
219     memset(i, 0, sizeof(videoreader_avifile_internal));
220     memset(v, 0, sizeof(videoreader_t));
221     v->getsamples = videoreader_avifile_getsamples;
222     v->close = videoreader_avifile_close;
223     v->getimage = videoreader_avifile_getimage;
224     v->getsamples = videoreader_avifile_getsamples;
225     v->setparameter = videoreader_avifile_setparameter;
226     v->internal = i;
227     v->frame = 0;
228     
229     i->do_video = 1;
230     i->do_audio = 1;
231
232     i->player = CreateIAviReadFile(filename);    
233     if(verbose) {
234         printf("%d streams (%d video, %d audio)\n", 
235                 i->player->StreamCount(),
236                 i->player->VideoStreamCount(),
237                 i->player->AudioStreamCount());
238     }
239     i->astream = i->player->GetStream(0, AviStream::Audio);
240     i->vstream = i->player->GetStream(0, AviStream::Video);
241     if(!i->vstream) {
242         printf("Couldn't open video stream\n");
243         i->do_video = 0;
244     }
245     if(!i->astream) {
246         printf("Couldn't open video stream\n");
247         i->do_audio = 0;
248     }
249 #ifdef NO_MP3
250     if(i->do_audio) {
251         printf(stderr, "MP3 support has been disabled at compile time, not converting soundtrack");
252         i->do_audio = 0;
253     }
254 #endif
255
256     if(!i->do_video && !i->do_audio) {
257         printf("File has neither audio nor video streams.(?)\n");
258         return -1;
259     }
260
261 #ifndef VERSION6
262     MainAVIHeader head;
263     int dwMicroSecPerFrame = 0;
264     player->GetFileHeader(&head);
265     printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
266     printf("frames: %d\n", head.dwTotalFrames);
267     printf("streams: %d\n", head.dwStreams);
268     printf("width: %d\n", head.dwWidth);
269     printf("height: %d\n", head.dwHeight);
270     printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
271     v->width = head.dwWidth;
272     v->height = head.dwHeight;
273     dwMicroSecPerFrame = head.dwMicroSecPerFrame;
274     samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
275     v->samplerate = (int)(astream->GetEndPos()/astream->GetEndTime());
276     v->fps = 1000000.0/dwMicroSecPerFrame;
277     i->soundbits = 16;
278 #else
279     if(i->do_video)
280     {
281         StreamInfo*videoinfo;
282         videoinfo = i->vstream->GetStreamInfo();
283         v->width = videoinfo->GetVideoWidth();
284         v->height = videoinfo->GetVideoHeight();
285         v->fps = (double)(videoinfo->GetFps());
286     }
287     if(i->do_audio)
288     {
289         WAVEFORMATEX wave;
290         StreamInfo*audioinfo;
291
292         i->astream->GetAudioFormatInfo(&wave,0);
293         audioinfo = i->astream->GetStreamInfo();
294
295         v->channels = wave.nChannels;
296         v->samplerate = wave.nSamplesPerSec;
297         i->soundbits = wave.wBitsPerSample;
298
299         if(v->channels==0 || v->samplerate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
300             v->samplerate = audioinfo->GetAudioSamplesPerSec();
301             v->channels = audioinfo->GetAudioChannels();
302             i->soundbits = audioinfo->GetAudioBitsPerSample();
303         }
304
305         if(verbose) {
306             printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
307             printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
308         }
309         if(i->soundbits != 8 && i->soundbits != 16) {
310             printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
311             i->do_audio = 0;
312             i->soundbits = 0;
313             v->channels = 0;
314             v->samplerate = 0;
315         }
316     }
317 #endif
318     i->vstream -> StartStreaming();
319     if(i->do_audio) {
320         i->astream -> StartStreaming();
321         ringbuffer_init(&i->audio_buffer);
322 #ifdef VERSION6
323         WAVEFORMATEX wave;
324         i->astream -> GetOutputFormat(&wave, sizeof(wave));
325         printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
326 #endif
327     }
328
329     return 0;
330 }
331
332 #else  //HAVE_AVIFILE
333
334 int videoreader_avifile_open(videoreader_t* v, char* filename)
335 {
336     return -1;
337 }
338
339 #endif //HAVE_AVIFILE