2 Convert avi movie files into swf.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
15 #include "../config.h"
26 #include "../lib/args.h"
31 #include <avifile/version.h>
32 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
37 #include <StreamInfo.h>
49 static char * filename = 0;
50 static char * outputfilename = "output.swf";
53 static int quality = 80;
54 static double scale = 1.0;
56 static int expensive = 0;
58 struct options_t options[] =
70 int args_callback_option(char*name,char*val)
72 if(!strcmp(name, "V")) {
73 printf("avi2swf - part of %s %s\n", PACKAGE, VERSION);
76 else if(!strcmp(name, "o")) {
80 else if(!strcmp(name, "q")) {
88 else if(!strcmp(name, "p")) {
92 else if(!strcmp(name, "v")) {
96 else if(!strcmp(name, "x")) {
100 else if(!strcmp(name, "d")) {
101 scale = atoi(val)/100.0;
102 if(scale>1.0 || scale<=0) {
103 fprintf(stderr, "Scale must be in the range 1-100!\n");
108 fprintf(stderr, "Unknown option: -%s\n", name);
111 int args_callback_longoption(char*name,char*val)
113 return args_long2shortoption(options, name, val);
115 void args_callback_usage(char*name)
117 printf("\nUsage: %s file.avi\n", name);
118 printf("\t-h , --help\t\t Print help and exit\n");
119 printf("\t-o , --output filename\t Specify output filename\n");
120 printf("\t-n , --num frames\t Number of frames to encode\n");
121 printf("\t-d , --scale <val>\t Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
122 printf("\t-p , --flip\t\t Turn movie upside down\n");
123 printf("\t-q , --quality <val>\t Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
124 printf("\t-x , --extragood\t Enable some *very* expensive compression strategies. You may\n");
125 printf("\t \t want to let this run overnight.\n");
126 printf("\t-V , --version\t\t Print program version and exit\n");
129 int args_callback_command(char*name,char*val)
132 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
139 static char toabuf[128];
140 static char*ftoa(double a)
142 sprintf(toabuf, "%f", a);
145 static char*itoa(int a)
147 sprintf(toabuf, "%d", a);
151 typedef struct _videoreader_avifile_internal
153 IAviReadFile* player;
154 IAviReadStream* astream;
155 IAviReadStream* vstream;
161 } videoreader_avifile_internal;
163 static int shutdown_avi2swf = 0;
164 static int frameno = 0;
167 pthread_t main_thread;
168 static void sigterm(int sig)
170 if(pthread_equal (pthread_self(), main_thread))
172 if(frameno>0 && !shutdown_avi2swf) {
174 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
183 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
186 unsigned int samples_read, bytes_read;
188 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
190 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
191 if(i->soundbits == 8) {
192 ret = i->astream->ReadFrames(buffer, num/2, num/(v->channels*2), samples_read, bytes_read);
193 unsigned char*b = (unsigned char*)buffer;
195 for(t=num-2;t>=0;t-=2) {
196 unsigned char x = b[t/2];
202 if(i->soundbits == 16) {
203 ret = i->astream->ReadFrames(buffer, num, num/(v->channels*2), samples_read, bytes_read);
208 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
210 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
212 printf("videoreader_getimage()\n");fflush(stdout);
221 if(i->vstream->ReadFrame() < 0) {
225 CImage*img = i->vstream->GetFrame(true);
233 unsigned char*data = img->Data();
234 int bpp = img->Bpp();
237 for(y=0;y<v->height;y++) {
238 unsigned char*from,*to;
239 to = &((unsigned char*)buffer)[y*v->width*4];
241 from = img->At(v->height-y-1);
244 for(x=0;x<v->width;x++) {
246 to[x*4+1] = from[x*3+2];
247 to[x*4+2] = from[x*3+1];
248 to[x*4+3] = from[x*3+0];
251 return v->width*v->height*4;
256 bool videoreader_avifile_eof(videoreader_t* v)
258 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
260 printf("videoreader_eof()\n");fflush(stdout);
264 void videoreader_avifile_close(videoreader_t* v)
266 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
268 printf("videoreader_close()\n");fflush(stdout);
271 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
275 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
278 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
282 int videoreader_avifile_open(videoreader_t* v, char* filename)
284 videoreader_avifile_internal* i;
285 i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
286 memset(i, 0, sizeof(videoreader_avifile_internal));
287 memset(v, 0, sizeof(videoreader_t));
288 v->getsamples = videoreader_avifile_getsamples;
289 v->getinfo = videoreader_avifile_getinfo;
290 v->close = videoreader_avifile_close;
291 v->eof = videoreader_avifile_eof;
292 v->getimage = videoreader_avifile_getimage;
293 v->getsamples = videoreader_avifile_getsamples;
294 v->setparameter = videoreader_avifile_setparameter;
300 i->player = CreateIAviReadFile(filename);
302 printf("%d streams (%d video, %d audio)\n",
303 i->player->StreamCount(),
304 i->player->VideoStreamCount(),
305 i->player->AudioStreamCount());
307 i->astream = i->player->GetStream(0, AviStream::Audio);
308 i->vstream = i->player->GetStream(0, AviStream::Video);
310 printf("Couldn't open video stream\n");
314 printf("Couldn't open video stream\n");
318 if(!i->do_video && !i->do_audio) {
319 printf("File has neither audio nor video streams.(?)\n");
325 int dwMicroSecPerFrame = 0;
326 player->GetFileHeader(&head);
327 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
328 printf("frames: %d\n", head.dwTotalFrames);
329 printf("streams: %d\n", head.dwStreams);
330 printf("width: %d\n", head.dwWidth);
331 printf("height: %d\n", head.dwHeight);
332 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
333 v->width = head.dwWidth;
334 v->height = head.dwHeight;
335 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
336 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
337 v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
338 v->fps = 1000000.0/dwMicroSecPerFrame;
341 StreamInfo*audioinfo;
342 StreamInfo*videoinfo;
345 videoinfo = i->vstream->GetStreamInfo();
346 v->width = videoinfo->GetVideoWidth();
347 v->height = videoinfo->GetVideoHeight();
348 v->fps = (double)(videoinfo->GetFps());
353 i->astream->GetAudioFormatInfo(&wave,0);
354 v->channels = wave.nChannels;
355 i->soundbits = wave.wBitsPerSample;
356 if(wave.wBitsPerSample != 8 && wave.wBitsPerSample != 16) {
357 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
365 audioinfo = i->astream->GetStreamInfo();
366 v->rate = audioinfo->GetAudioSamplesPerSec();
369 i->vstream -> StartStreaming();
371 i->astream -> StartStreaming();
376 int main (int argc,char ** argv)
384 signal(SIGTERM, sigterm);
385 signal(SIGINT , sigterm);
386 signal(SIGQUIT, sigterm);
387 main_thread = pthread_self();
390 processargs(argc, argv);
394 fi = fopen(outputfilename, "wb");
396 fflush(stdout); fflush(stderr);
397 fprintf(stderr, "Couldn't open %s\n", outputfilename);
401 ret = videoreader_avifile_open(&video, filename);
404 printf("Error opening %s\n", filename);
409 printf("| video framerate: %f\n", video.fps);
410 printf("| video size: %dx%d\n", video.width, video.height);
411 printf("| audio rate: %d\n", video.rate);
412 printf("| audio channels: %d\n", video.channels);
415 ret = v2swf_init(&v2swf, &video);
417 v2swf_setparameter(&v2swf, "verbose", "1");
418 v2swf_setparameter(&v2swf, "quality", itoa(quality));
419 v2swf_setparameter(&v2swf, "blockdiff", "0");
420 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
421 v2swf_setparameter(&v2swf, "mp3_bitrate", "128");
422 //v2swf_setparameter(&v2swf, "fixheader", "1");
423 //v2swf_setparameter(&v2swf, "framerate", "15");
424 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
425 v2swf_setparameter(&v2swf, "prescale", "1");
426 v2swf_setparameter(&v2swf, "keyframe_interval", "200");
427 v2swf_setparameter(&v2swf, "flash_version", "6");
429 v2swf_setparameter(&v2swf, "motioncompensation", "1");
436 int l=v2swf_read(&v2swf, buffer, 4096);
437 fwrite(buffer, l, 1, fi);
441 printf("\rConverting frame %d", frameno);fflush(stdout);
447 v2swf_backpatch(&v2swf, outputfilename);