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