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"
34 #ifdef HAVE_AVIFILE_VERSION_H
35 #include <avifile/version.h>
38 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
43 #include <StreamInfo.h>
64 #include "videoreader.h"
66 static int shutdown_avi2swf = 0;
67 static int verbose = 0;
69 typedef struct _videoreader_avifile_internal
72 IAviReadStream* astream;
73 IAviReadStream* vstream;
80 ringbuffer_t audio_buffer;
81 } videoreader_avifile_internal;
83 static void 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);
91 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
93 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
95 static int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
98 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
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;
105 for(t=num-2;t>=0;t-=2) {
106 unsigned char x = b[t/2];
112 if(i->soundbits == 16) {
113 readSamples(i, buffer, num, num/(v->channels*2));
118 static int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
120 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
122 printf("videoreader_getimage()\n");fflush(stdout);
131 if(i->vstream->ReadFrame() < 0) {
132 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
137 CImage*img = i->vstream->GetFrame();
139 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
143 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
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);
155 unsigned char*data = img->Data();
156 int bpp = img->Bpp();
159 for(y=0;y<v->height;y++) {
160 unsigned char*from,*to;
161 to = &((unsigned char*)buffer)[y*v->width*4];
163 from = img->At(v->height-y-1);
166 for(x=0;x<v->width;x++) {
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];
173 if(img2) delete img2;
174 return v->width*v->height*4;
176 if(img2) delete img2;
177 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
181 static bool videoreader_avifile_eof(videoreader_t* v)
183 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
185 printf("videoreader_eof()\n");fflush(stdout);
189 static void videoreader_avifile_close(videoreader_t* v)
191 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
193 printf("videoreader_close()\n");fflush(stdout);
196 ringbuffer_clear(&i->audio_buffer);
199 static void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
201 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
202 if(!strcmp(name, "verbose")) {
203 verbose = atoi(value);
205 if(!strcmp(name, "flip")) {
206 i->flip = atoi(value);
209 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
213 int videoreader_avifile_open(videoreader_t* v, char* filename)
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;
235 i->player = CreateIAviReadFile(filename);
237 printf("%d streams (%d video, %d audio)\n",
238 i->player->StreamCount(),
239 i->player->VideoStreamCount(),
240 i->player->AudioStreamCount());
242 i->astream = i->player->GetStream(0, AviStream::Audio);
243 i->vstream = i->player->GetStream(0, AviStream::Video);
245 printf("Couldn't open video stream\n");
249 printf("Couldn't open video stream\n");
254 printf(stderr, "MP3 support has been disabled at compile time, not converting soundtrack");
259 if(!i->do_video && !i->do_audio) {
260 printf("File has neither audio nor video streams.(?)\n");
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;
284 StreamInfo*videoinfo;
285 videoinfo = i->vstream->GetStreamInfo();
286 v->width = videoinfo->GetVideoWidth();
287 v->height = videoinfo->GetVideoHeight();
288 v->fps = (double)(videoinfo->GetFps());
293 StreamInfo*audioinfo;
295 i->astream->GetAudioFormatInfo(&wave,0);
296 audioinfo = i->astream->GetStreamInfo();
298 v->channels = wave.nChannels;
299 v->samplerate = wave.nSamplesPerSec;
300 i->soundbits = wave.wBitsPerSample;
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();
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());
312 if(i->soundbits != 8 && i->soundbits != 16) {
313 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
321 i->vstream -> StartStreaming();
323 i->astream -> StartStreaming();
324 ringbuffer_init(&i->audio_buffer);
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);
338 int videoreader_avifile_open(videoreader_t* v, char* filename)
343 #endif //HAVE_AVIFILE