2 Convert avi movie files into swf.
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 */
27 #include "../config.h"
38 #include "../lib/args.h"
44 #include <avifile/version.h>
45 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
50 #include <StreamInfo.h>
62 static char * filename = 0;
63 static char * outputfilename = "output.swf";
66 static int quality = 80;
67 static double scale = 1.0;
69 static int expensive = 0;
70 static int flashversion = 6;
71 static int keyframe_interval = -1;
73 static float audio_adjust = 0;
74 static int mp3_bitrate = 32;
75 static int samplerate = 11025;
77 struct options_t options[] =
84 {"r","mp3-samplerate"},
94 int args_callback_option(char*name,char*val)
96 if(!strcmp(name, "V")) {
97 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
100 else if(!strcmp(name, "o")) {
101 outputfilename = val;
104 else if(!strcmp(name, "q")) {
112 else if(!strcmp(name, "p")) {
116 else if(!strcmp(name, "A")) {
117 audio_adjust = atof(val);
120 else if(!strcmp(name, "v")) {
124 else if(!strcmp(name, "T")) {
125 flashversion = atoi(val);
128 else if(!strcmp(name, "x")) {
132 else if(!strcmp(name, "m")) {
133 mp3_bitrate = atoi(val);
136 else if(!strcmp(name, "r")) {
137 samplerate = atoi(val);
138 if(samplerate >= 11000 && samplerate <= 12000)
140 else if(samplerate >= 22000 && samplerate <= 23000)
142 else if(samplerate >= 44000 && samplerate <= 45000)
145 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
146 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
151 else if(!strcmp(name, "S")) {
155 else if(!strcmp(name, "s")) {
156 scale = atoi(val)/100.0;
157 if(scale>1.0 || scale<=0) {
158 fprintf(stderr, "Scale must be in the range 1-100!\n");
163 fprintf(stderr, "Unknown option: -%s\n", name);
166 int args_callback_longoption(char*name,char*val)
168 return args_long2shortoption(options, name, val);
170 void args_callback_usage(char*name)
172 printf("\nUsage: %s file.avi\n", name);
173 printf("\t-h , --help\t\t Print help and exit\n");
174 printf("\t-o , --output filename\t Specify output filename\n");
175 printf("\t-A , --adjust seconds\t Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
176 printf("\t-n , --num frames\t Number of frames to encode\n");
177 printf("\t-m , --mp3-bitrate <rate> (kbps)\t Set the mp3 bitrate to encode audio with\n");
178 printf("\t-r , --mp3-samplerate <rate> (Hz)\t Set the mp3 samplerate to encode audio with (default: 11025)\n");
179 printf("\t-d , --scale <val>\t Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
180 printf("\t-p , --flip\t\t Turn movie upside down\n");
181 printf("\t-q , --quality <val>\t Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
182 printf("\t-x , --extragood\t Enable some *very* expensive compression strategies. You may\n");
183 printf("\t \t want to let this run overnight.\n");
184 printf("\t-T , --flashversion <n>\t Set output flash version to <n>. Notice: H.263 compression will only be\n");
185 printf("\t \t used for n >= 6.\n");
186 printf("\t-V , --version\t\t Print program version and exit\n");
189 int args_callback_command(char*name,char*val)
192 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
199 static char toabuf[128];
200 static char*ftoa(double a)
202 sprintf(toabuf, "%f", a);
205 static char*itoa(int a)
207 sprintf(toabuf, "%d", a);
211 typedef struct _videoreader_avifile_internal
213 IAviReadFile* player;
214 IAviReadStream* astream;
215 IAviReadStream* vstream;
221 ringbuffer_t audio_buffer;
222 } videoreader_avifile_internal;
224 static int shutdown_avi2swf = 0;
225 static int frameno = 0;
228 pthread_t main_thread;
229 static void sigterm(int sig)
231 if(pthread_equal (pthread_self(), main_thread))
233 if(frameno>0 && !shutdown_avi2swf) {
235 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
244 static void readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
247 while(i->audio_buffer.available < buffer_size) {
248 unsigned int samples_read = 0, bytes_read = 0;
249 ret = i->astream->ReadFrames(buffer, buffer_size, numsamples, samples_read, bytes_read);
252 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
254 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
256 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
259 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
261 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
262 if(i->soundbits == 8) {
263 readSamples(i, buffer, num/2, num/(v->channels*2));
264 unsigned char*b = (unsigned char*)buffer;
266 for(t=num-2;t>=0;t-=2) {
267 unsigned char x = b[t/2];
273 if(i->soundbits == 16) {
274 readSamples(i, buffer, num, num/(v->channels*2));
279 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
281 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
283 printf("videoreader_getimage()\n");fflush(stdout);
292 if(i->vstream->ReadFrame() < 0) {
293 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
298 CImage*img = i->vstream->GetFrame();
300 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
304 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
307 if(img->Bpp() != 3) {
308 /* TODO: this doesn't work yet */
309 if(verbose) printf("Can't handle Bpp %d, shutting down...\n", img->Bpp());
311 BitmapInfo tmp(v->width, v->height, 24);
312 img2 = new CImage(img, &tmp);
319 unsigned char*data = img->Data();
320 int bpp = img->Bpp();
323 for(y=0;y<v->height;y++) {
324 unsigned char*from,*to;
325 to = &((unsigned char*)buffer)[y*v->width*4];
327 from = img->At(v->height-y-1);
330 for(x=0;x<v->width;x++) {
332 to[x*4+1] = from[x*3+2];
333 to[x*4+2] = from[x*3+1];
334 to[x*4+3] = from[x*3+0];
337 if(img2) delete img2;
338 return v->width*v->height*4;
340 if(img2) delete img2;
341 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
345 bool videoreader_avifile_eof(videoreader_t* v)
347 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
349 printf("videoreader_eof()\n");fflush(stdout);
353 void videoreader_avifile_close(videoreader_t* v)
355 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
357 printf("videoreader_close()\n");fflush(stdout);
360 ringbuffer_clear(&i->audio_buffer);
363 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
367 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
370 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
374 int videoreader_avifile_open(videoreader_t* v, char* filename)
376 videoreader_avifile_internal* i;
377 i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
378 memset(i, 0, sizeof(videoreader_avifile_internal));
379 memset(v, 0, sizeof(videoreader_t));
380 v->getsamples = videoreader_avifile_getsamples;
381 v->getinfo = videoreader_avifile_getinfo;
382 v->close = videoreader_avifile_close;
383 v->eof = videoreader_avifile_eof;
384 v->getimage = videoreader_avifile_getimage;
385 v->getsamples = videoreader_avifile_getsamples;
386 v->setparameter = videoreader_avifile_setparameter;
392 i->player = CreateIAviReadFile(filename);
394 printf("%d streams (%d video, %d audio)\n",
395 i->player->StreamCount(),
396 i->player->VideoStreamCount(),
397 i->player->AudioStreamCount());
399 i->astream = i->player->GetStream(0, AviStream::Audio);
400 i->vstream = i->player->GetStream(0, AviStream::Video);
402 printf("Couldn't open video stream\n");
406 printf("Couldn't open video stream\n");
410 if(!i->do_video && !i->do_audio) {
411 printf("File has neither audio nor video streams.(?)\n");
417 int dwMicroSecPerFrame = 0;
418 player->GetFileHeader(&head);
419 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
420 printf("frames: %d\n", head.dwTotalFrames);
421 printf("streams: %d\n", head.dwStreams);
422 printf("width: %d\n", head.dwWidth);
423 printf("height: %d\n", head.dwHeight);
424 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
425 v->width = head.dwWidth;
426 v->height = head.dwHeight;
427 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
428 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
429 v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
430 v->fps = 1000000.0/dwMicroSecPerFrame;
435 StreamInfo*videoinfo;
436 videoinfo = i->vstream->GetStreamInfo();
437 v->width = videoinfo->GetVideoWidth();
438 v->height = videoinfo->GetVideoHeight();
439 v->fps = (double)(videoinfo->GetFps());
444 StreamInfo*audioinfo;
446 i->astream->GetAudioFormatInfo(&wave,0);
447 audioinfo = i->astream->GetStreamInfo();
449 v->channels = wave.nChannels;
450 v->rate = wave.nSamplesPerSec;
451 i->soundbits = wave.wBitsPerSample;
453 if(v->channels==0 || v->rate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
454 v->rate = audioinfo->GetAudioSamplesPerSec();
455 v->channels = audioinfo->GetAudioChannels();
456 i->soundbits = audioinfo->GetAudioBitsPerSample();
460 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
461 printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
463 if(i->soundbits != 8 && i->soundbits != 16) {
464 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
472 i->vstream -> StartStreaming();
474 i->astream -> StartStreaming();
475 ringbuffer_init(&i->audio_buffer);
478 i->astream -> GetOutputFormat(&wave, sizeof(wave));
479 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
486 int main (int argc,char ** argv)
494 signal(SIGTERM, sigterm);
495 signal(SIGINT , sigterm);
496 signal(SIGQUIT, sigterm);
497 main_thread = pthread_self();
500 processargs(argc, argv);
503 if(keyframe_interval<0) {
505 keyframe_interval=200;
510 fi = fopen(outputfilename, "wb");
512 fflush(stdout); fflush(stderr);
513 fprintf(stderr, "Couldn't open %s\n", outputfilename);
517 ret = videoreader_avifile_open(&video, filename);
520 printf("Error opening %s\n", filename);
525 printf("| video framerate: %f\n", video.fps);
526 printf("| video size: %dx%d\n", video.width, video.height);
527 printf("| audio rate: %d\n", video.rate);
528 printf("| audio channels: %d\n", video.channels);
531 ret = v2swf_init(&v2swf, &video);
533 v2swf_setparameter(&v2swf, "verbose", "1");
534 v2swf_setparameter(&v2swf, "quality", itoa(quality));
535 v2swf_setparameter(&v2swf, "blockdiff", "0");
536 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
537 v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
538 v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
539 //v2swf_setparameter(&v2swf, "fixheader", "1");
540 //v2swf_setparameter(&v2swf, "framerate", "15");
541 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
542 v2swf_setparameter(&v2swf, "prescale", "1");
543 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
544 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
546 v2swf_setparameter(&v2swf, "motioncompensation", "1");
552 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
553 void*buf = malloc(num);
554 video.getsamples(&video, buf, num);
556 } else if(audio_adjust<0) {
557 int num = (int)(-audio_adjust*video.fps);
558 void*buf = malloc(video.width*video.height*4);
561 video.getimage(&video, buf);
568 void*buf = malloc(video.width*video.height*4);
569 for(t=0;t<skip;t++) {
570 video.getimage(&video, buf);
571 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
573 printf("\rSkipping frame %d", frameno);fflush(stdout);
581 int l=v2swf_read(&v2swf, buffer, 4096);
582 fwrite(buffer, l, 1, fi);
586 printf("\rConverting frame %d", frameno);fflush(stdout);
592 v2swf_backpatch(&v2swf, outputfilename);