1 /* videoreader_avifile.cc
2 Read avi files using the avifile library.
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
25 #include "../config.h"
26 #include "videoreader.h"
35 #ifdef HAVE_AVIFILE_VERSION_H
36 #include <avifile/version.h>
39 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
44 #include <StreamInfo.h>
66 static int verbose = 0;
68 typedef struct _videoreader_avifile_internal
71 IAviReadStream* astream;
72 IAviReadStream* vstream;
80 ringbuffer_t audio_buffer;
81 } videoreader_avifile_internal;
83 static int readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
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);
90 int l = i->audio_buffer.available;
91 ringbuffer_read(&i->audio_buffer, buffer, l);
94 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
96 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
99 static int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
101 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
103 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
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;
111 for(t=num_read-2;t>=0;t-=2) {
112 unsigned char x = b[t/2];
116 if(!num_read) i->audio_eof=1;
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;
126 static int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
128 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
130 printf("videoreader_getimage()\n");fflush(stdout);
136 if(i->vstream->ReadFrame() < 0) {
137 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
142 CImage*img = i->vstream->GetFrame();
144 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
148 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
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);
160 unsigned char*data = img->Data();
161 int bpp = img->Bpp();
164 for(y=0;y<v->height;y++) {
165 unsigned char*from,*to;
166 to = &((unsigned char*)buffer)[y*v->width*4];
168 from = img->At(v->height-y-1);
171 for(x=0;x<v->width;x++) {
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];
178 if(img2) delete img2;
179 return v->width*v->height*4;
181 if(img2) delete img2;
182 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
187 static void videoreader_avifile_close(videoreader_t* v)
189 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
191 printf("videoreader_close()\n");fflush(stdout);
194 ringbuffer_clear(&i->audio_buffer);
197 static void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
199 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
200 if(!strcmp(name, "verbose")) {
201 verbose = atoi(value);
203 if(!strcmp(name, "flip")) {
204 i->flip = atoi(value);
207 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
211 int videoreader_avifile_open(videoreader_t* v, char* filename)
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;
232 i->player = CreateIAviReadFile(filename);
234 printf("%d streams (%d video, %d audio)\n",
235 i->player->StreamCount(),
236 i->player->VideoStreamCount(),
237 i->player->AudioStreamCount());
239 i->astream = i->player->GetStream(0, AviStream::Audio);
240 i->vstream = i->player->GetStream(0, AviStream::Video);
242 printf("Couldn't open video stream\n");
246 printf("Couldn't open video stream\n");
251 printf(stderr, "MP3 support has been disabled at compile time, not converting soundtrack");
256 if(!i->do_video && !i->do_audio) {
257 printf("File has neither audio nor video streams.(?)\n");
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", abs(head.dwWidth));
269 printf("height: %d\n", abs(head.dwHeight));
270 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
271 v->width = abs(head.dwWidth);
272 v->height = abs(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;
281 StreamInfo*videoinfo;
282 videoinfo = i->vstream->GetStreamInfo();
283 v->width = abs(videoinfo->GetVideoWidth());
284 v->height = abs(videoinfo->GetVideoHeight());
285 v->fps = (double)(videoinfo->GetFps());
290 StreamInfo*audioinfo;
292 i->astream->GetAudioFormatInfo(&wave,0);
293 audioinfo = i->astream->GetStreamInfo();
295 v->channels = wave.nChannels;
296 v->samplerate = wave.nSamplesPerSec;
297 i->soundbits = wave.wBitsPerSample;
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();
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());
309 if(i->soundbits != 8 && i->soundbits != 16) {
310 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
318 i->vstream -> StartStreaming();
320 i->astream -> StartStreaming();
321 ringbuffer_init(&i->audio_buffer);
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);
334 int videoreader_avifile_open(videoreader_t* v, char* filename)
339 #endif //HAVE_AVIFILE