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