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