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;
76 struct options_t options[] =
92 int args_callback_option(char*name,char*val)
94 if(!strcmp(name, "V")) {
95 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
98 else if(!strcmp(name, "o")) {
102 else if(!strcmp(name, "q")) {
110 else if(!strcmp(name, "p")) {
114 else if(!strcmp(name, "A")) {
115 audio_adjust = atof(val);
118 else if(!strcmp(name, "v")) {
122 else if(!strcmp(name, "T")) {
123 flashversion = atoi(val);
126 else if(!strcmp(name, "x")) {
130 else if(!strcmp(name, "m")) {
131 mp3_bitrate = atoi(val);
134 else if(!strcmp(name, "S")) {
138 else if(!strcmp(name, "s")) {
139 scale = atoi(val)/100.0;
140 if(scale>1.0 || scale<=0) {
141 fprintf(stderr, "Scale must be in the range 1-100!\n");
146 fprintf(stderr, "Unknown option: -%s\n", name);
149 int args_callback_longoption(char*name,char*val)
151 return args_long2shortoption(options, name, val);
153 void args_callback_usage(char*name)
155 printf("\nUsage: %s file.avi\n", name);
156 printf("\t-h , --help\t\t Print help and exit\n");
157 printf("\t-o , --output filename\t Specify output filename\n");
158 printf("\t-A , --adjust seconds\t Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
159 printf("\t-n , --num frames\t Number of frames to encode\n");
160 printf("\t-m , --mp3-bitrate <rate>\t Set the mp3 bitrate to encode audio with\n");
161 printf("\t-d , --scale <val>\t Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
162 printf("\t-p , --flip\t\t Turn movie upside down\n");
163 printf("\t-q , --quality <val>\t Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
164 printf("\t-x , --extragood\t Enable some *very* expensive compression strategies. You may\n");
165 printf("\t \t want to let this run overnight.\n");
166 printf("\t-T , --flashversion <n>\t Set output flash version to <n>. Notice: H.263 compression will only be\n");
167 printf("\t \t used for n >= 6.\n");
168 printf("\t-V , --version\t\t Print program version and exit\n");
171 int args_callback_command(char*name,char*val)
174 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
181 static char toabuf[128];
182 static char*ftoa(double a)
184 sprintf(toabuf, "%f", a);
187 static char*itoa(int a)
189 sprintf(toabuf, "%d", a);
193 typedef struct _videoreader_avifile_internal
195 IAviReadFile* player;
196 IAviReadStream* astream;
197 IAviReadStream* vstream;
203 ringbuffer_t audio_buffer;
204 } videoreader_avifile_internal;
206 static int shutdown_avi2swf = 0;
207 static int frameno = 0;
210 pthread_t main_thread;
211 static void sigterm(int sig)
213 if(pthread_equal (pthread_self(), main_thread))
215 if(frameno>0 && !shutdown_avi2swf) {
217 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
226 static void readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
229 while(i->audio_buffer.available < buffer_size) {
230 unsigned int samples_read = 0, bytes_read = 0;
231 ret = i->astream->ReadFrames(buffer, buffer_size, numsamples, samples_read, bytes_read);
234 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
236 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
238 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
241 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
243 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
244 if(i->soundbits == 8) {
245 readSamples(i, buffer, num/2, num/(v->channels*2));
246 unsigned char*b = (unsigned char*)buffer;
248 for(t=num-2;t>=0;t-=2) {
249 unsigned char x = b[t/2];
255 if(i->soundbits == 16) {
256 readSamples(i, buffer, num, num/(v->channels*2));
261 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
263 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
265 printf("videoreader_getimage()\n");fflush(stdout);
274 if(i->vstream->ReadFrame() < 0) {
275 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
280 CImage*img = i->vstream->GetFrame();
282 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
286 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
289 if(img->Bpp() != 3) {
290 /* TODO: this doesn't work yet */
291 if(verbose) printf("Can't handle Bpp %d, shutting down...\n", img->Bpp());
293 BitmapInfo tmp(v->width, v->height, 24);
294 img2 = new CImage(img, &tmp);
301 unsigned char*data = img->Data();
302 int bpp = img->Bpp();
305 for(y=0;y<v->height;y++) {
306 unsigned char*from,*to;
307 to = &((unsigned char*)buffer)[y*v->width*4];
309 from = img->At(v->height-y-1);
312 for(x=0;x<v->width;x++) {
314 to[x*4+1] = from[x*3+2];
315 to[x*4+2] = from[x*3+1];
316 to[x*4+3] = from[x*3+0];
319 if(img2) delete img2;
320 return v->width*v->height*4;
322 if(img2) delete img2;
323 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
327 bool videoreader_avifile_eof(videoreader_t* v)
329 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
331 printf("videoreader_eof()\n");fflush(stdout);
335 void videoreader_avifile_close(videoreader_t* v)
337 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
339 printf("videoreader_close()\n");fflush(stdout);
342 ringbuffer_clear(&i->audio_buffer);
345 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
349 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
352 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
356 int videoreader_avifile_open(videoreader_t* v, char* filename)
358 videoreader_avifile_internal* i;
359 i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
360 memset(i, 0, sizeof(videoreader_avifile_internal));
361 memset(v, 0, sizeof(videoreader_t));
362 v->getsamples = videoreader_avifile_getsamples;
363 v->getinfo = videoreader_avifile_getinfo;
364 v->close = videoreader_avifile_close;
365 v->eof = videoreader_avifile_eof;
366 v->getimage = videoreader_avifile_getimage;
367 v->getsamples = videoreader_avifile_getsamples;
368 v->setparameter = videoreader_avifile_setparameter;
374 i->player = CreateIAviReadFile(filename);
376 printf("%d streams (%d video, %d audio)\n",
377 i->player->StreamCount(),
378 i->player->VideoStreamCount(),
379 i->player->AudioStreamCount());
381 i->astream = i->player->GetStream(0, AviStream::Audio);
382 i->vstream = i->player->GetStream(0, AviStream::Video);
384 printf("Couldn't open video stream\n");
388 printf("Couldn't open video stream\n");
392 if(!i->do_video && !i->do_audio) {
393 printf("File has neither audio nor video streams.(?)\n");
399 int dwMicroSecPerFrame = 0;
400 player->GetFileHeader(&head);
401 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
402 printf("frames: %d\n", head.dwTotalFrames);
403 printf("streams: %d\n", head.dwStreams);
404 printf("width: %d\n", head.dwWidth);
405 printf("height: %d\n", head.dwHeight);
406 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
407 v->width = head.dwWidth;
408 v->height = head.dwHeight;
409 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
410 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
411 v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
412 v->fps = 1000000.0/dwMicroSecPerFrame;
417 StreamInfo*videoinfo;
418 videoinfo = i->vstream->GetStreamInfo();
419 v->width = videoinfo->GetVideoWidth();
420 v->height = videoinfo->GetVideoHeight();
421 v->fps = (double)(videoinfo->GetFps());
426 StreamInfo*audioinfo;
428 i->astream->GetAudioFormatInfo(&wave,0);
429 audioinfo = i->astream->GetStreamInfo();
431 v->channels = wave.nChannels;
432 v->rate = wave.nSamplesPerSec;
433 i->soundbits = wave.wBitsPerSample;
435 if(v->channels==0 || v->rate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
436 v->rate = audioinfo->GetAudioSamplesPerSec();
437 v->channels = audioinfo->GetAudioChannels();
438 i->soundbits = audioinfo->GetAudioBitsPerSample();
442 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
443 printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
445 if(i->soundbits != 8 && i->soundbits != 16) {
446 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
454 i->vstream -> StartStreaming();
456 i->astream -> StartStreaming();
457 ringbuffer_init(&i->audio_buffer);
460 i->astream -> GetOutputFormat(&wave, sizeof(wave));
461 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
468 int main (int argc,char ** argv)
476 signal(SIGTERM, sigterm);
477 signal(SIGINT , sigterm);
478 signal(SIGQUIT, sigterm);
479 main_thread = pthread_self();
482 processargs(argc, argv);
485 if(keyframe_interval<0) {
487 keyframe_interval=200;
492 fi = fopen(outputfilename, "wb");
494 fflush(stdout); fflush(stderr);
495 fprintf(stderr, "Couldn't open %s\n", outputfilename);
499 ret = videoreader_avifile_open(&video, filename);
502 printf("Error opening %s\n", filename);
507 printf("| video framerate: %f\n", video.fps);
508 printf("| video size: %dx%d\n", video.width, video.height);
509 printf("| audio rate: %d\n", video.rate);
510 printf("| audio channels: %d\n", video.channels);
513 ret = v2swf_init(&v2swf, &video);
515 v2swf_setparameter(&v2swf, "verbose", "1");
516 v2swf_setparameter(&v2swf, "quality", itoa(quality));
517 v2swf_setparameter(&v2swf, "blockdiff", "0");
518 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
519 v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
520 //v2swf_setparameter(&v2swf, "fixheader", "1");
521 //v2swf_setparameter(&v2swf, "framerate", "15");
522 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
523 v2swf_setparameter(&v2swf, "prescale", "1");
524 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
525 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
527 v2swf_setparameter(&v2swf, "motioncompensation", "1");
533 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
534 void*buf = malloc(num);
535 video.getsamples(&video, buf, num);
537 } else if(audio_adjust<0) {
538 int num = (int)(-audio_adjust*video.fps);
539 void*buf = malloc(video.width*video.height*4);
542 video.getimage(&video, buf);
549 void*buf = malloc(video.width*video.height*4);
550 for(t=0;t<skip;t++) {
551 video.getimage(&video, buf);
552 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
554 printf("\rSkipping frame %d", frameno);fflush(stdout);
562 int l=v2swf_read(&v2swf, buffer, 4096);
563 fwrite(buffer, l, 1, fi);
567 printf("\rConverting frame %d", frameno);fflush(stdout);
573 v2swf_backpatch(&v2swf, outputfilename);