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;
75 struct options_t options[] =
90 int args_callback_option(char*name,char*val)
92 if(!strcmp(name, "V")) {
93 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
96 else if(!strcmp(name, "o")) {
100 else if(!strcmp(name, "q")) {
108 else if(!strcmp(name, "p")) {
112 else if(!strcmp(name, "A")) {
113 audio_adjust = atof(val);
116 else if(!strcmp(name, "v")) {
120 else if(!strcmp(name, "T")) {
121 flashversion = atoi(val);
124 else if(!strcmp(name, "x")) {
128 else if(!strcmp(name, "S")) {
132 else if(!strcmp(name, "s")) {
133 scale = atoi(val)/100.0;
134 if(scale>1.0 || scale<=0) {
135 fprintf(stderr, "Scale must be in the range 1-100!\n");
140 fprintf(stderr, "Unknown option: -%s\n", name);
143 int args_callback_longoption(char*name,char*val)
145 return args_long2shortoption(options, name, val);
147 void args_callback_usage(char*name)
149 printf("\nUsage: %s file.avi\n", name);
150 printf("\t-h , --help\t\t Print help and exit\n");
151 printf("\t-o , --output filename\t Specify output filename\n");
152 printf("\t-A , --adjust seconds\t Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
153 printf("\t-n , --num frames\t Number of frames to encode\n");
154 printf("\t-d , --scale <val>\t Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
155 printf("\t-p , --flip\t\t Turn movie upside down\n");
156 printf("\t-q , --quality <val>\t Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
157 printf("\t-x , --extragood\t Enable some *very* expensive compression strategies. You may\n");
158 printf("\t \t want to let this run overnight.\n");
159 printf("\t-T , --flashversion <n>\t Set output flash version to <n>. Notice: H.263 compression will only be\n");
160 printf("\t \t used for n >= 6.\n");
161 printf("\t-V , --version\t\t Print program version and exit\n");
164 int args_callback_command(char*name,char*val)
167 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
174 static char toabuf[128];
175 static char*ftoa(double a)
177 sprintf(toabuf, "%f", a);
180 static char*itoa(int a)
182 sprintf(toabuf, "%d", a);
186 typedef struct _videoreader_avifile_internal
188 IAviReadFile* player;
189 IAviReadStream* astream;
190 IAviReadStream* vstream;
196 ringbuffer_t audio_buffer;
197 } videoreader_avifile_internal;
199 static int shutdown_avi2swf = 0;
200 static int frameno = 0;
203 pthread_t main_thread;
204 static void sigterm(int sig)
206 if(pthread_equal (pthread_self(), main_thread))
208 if(frameno>0 && !shutdown_avi2swf) {
210 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
219 static void readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
222 while(i->audio_buffer.available < buffer_size) {
223 unsigned int samples_read = 0, bytes_read = 0;
224 ret = i->astream->ReadFrames(buffer, buffer_size, numsamples, samples_read, bytes_read);
227 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
229 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
231 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
234 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
236 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
237 if(i->soundbits == 8) {
238 readSamples(i, buffer, num/2, num/(v->channels*2));
239 unsigned char*b = (unsigned char*)buffer;
241 for(t=num-2;t>=0;t-=2) {
242 unsigned char x = b[t/2];
248 if(i->soundbits == 16) {
249 readSamples(i, buffer, num, num/(v->channels*2));
254 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
256 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
258 printf("videoreader_getimage()\n");fflush(stdout);
267 if(i->vstream->ReadFrame() < 0) {
268 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
273 CImage*img = i->vstream->GetFrame();
275 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
279 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
282 if(img->Bpp() != 3) {
283 /* TODO: this doesn't work yet */
284 if(verbose) printf("Can't handle Bpp %d, shutting down...\n", img->Bpp());
286 BitmapInfo tmp(v->width, v->height, 24);
287 img2 = new CImage(img, &tmp);
294 unsigned char*data = img->Data();
295 int bpp = img->Bpp();
298 for(y=0;y<v->height;y++) {
299 unsigned char*from,*to;
300 to = &((unsigned char*)buffer)[y*v->width*4];
302 from = img->At(v->height-y-1);
305 for(x=0;x<v->width;x++) {
307 to[x*4+1] = from[x*3+2];
308 to[x*4+2] = from[x*3+1];
309 to[x*4+3] = from[x*3+0];
312 if(img2) delete img2;
313 return v->width*v->height*4;
315 if(img2) delete img2;
316 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
320 bool videoreader_avifile_eof(videoreader_t* v)
322 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
324 printf("videoreader_eof()\n");fflush(stdout);
328 void videoreader_avifile_close(videoreader_t* v)
330 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
331 ringbuffer_clear(&i->audio_buffer);
333 printf("videoreader_close()\n");fflush(stdout);
336 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
340 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
343 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
347 int videoreader_avifile_open(videoreader_t* v, char* filename)
349 videoreader_avifile_internal* i;
350 i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
351 memset(i, 0, sizeof(videoreader_avifile_internal));
352 memset(v, 0, sizeof(videoreader_t));
353 v->getsamples = videoreader_avifile_getsamples;
354 v->getinfo = videoreader_avifile_getinfo;
355 v->close = videoreader_avifile_close;
356 v->eof = videoreader_avifile_eof;
357 v->getimage = videoreader_avifile_getimage;
358 v->getsamples = videoreader_avifile_getsamples;
359 v->setparameter = videoreader_avifile_setparameter;
365 i->player = CreateIAviReadFile(filename);
367 printf("%d streams (%d video, %d audio)\n",
368 i->player->StreamCount(),
369 i->player->VideoStreamCount(),
370 i->player->AudioStreamCount());
372 i->astream = i->player->GetStream(0, AviStream::Audio);
373 i->vstream = i->player->GetStream(0, AviStream::Video);
375 printf("Couldn't open video stream\n");
379 printf("Couldn't open video stream\n");
383 if(!i->do_video && !i->do_audio) {
384 printf("File has neither audio nor video streams.(?)\n");
390 int dwMicroSecPerFrame = 0;
391 player->GetFileHeader(&head);
392 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
393 printf("frames: %d\n", head.dwTotalFrames);
394 printf("streams: %d\n", head.dwStreams);
395 printf("width: %d\n", head.dwWidth);
396 printf("height: %d\n", head.dwHeight);
397 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
398 v->width = head.dwWidth;
399 v->height = head.dwHeight;
400 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
401 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
402 v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
403 v->fps = 1000000.0/dwMicroSecPerFrame;
408 StreamInfo*videoinfo;
409 videoinfo = i->vstream->GetStreamInfo();
410 v->width = videoinfo->GetVideoWidth();
411 v->height = videoinfo->GetVideoHeight();
412 v->fps = (double)(videoinfo->GetFps());
417 StreamInfo*audioinfo;
419 i->astream->GetAudioFormatInfo(&wave,0);
420 audioinfo = i->astream->GetStreamInfo();
422 v->channels = wave.nChannels;
423 v->rate = wave.nSamplesPerSec;
424 i->soundbits = wave.wBitsPerSample;
426 if(v->channels==0 || v->rate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
427 v->rate = audioinfo->GetAudioSamplesPerSec();
428 v->channels = audioinfo->GetAudioChannels();
429 i->soundbits = audioinfo->GetAudioBitsPerSample();
433 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
434 printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
436 if(i->soundbits != 8 && i->soundbits != 16) {
437 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
445 i->vstream -> StartStreaming();
447 i->astream -> StartStreaming();
448 ringbuffer_init(&i->audio_buffer);
451 i->astream -> GetOutputFormat(&wave, sizeof(wave));
452 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
459 int main (int argc,char ** argv)
467 signal(SIGTERM, sigterm);
468 signal(SIGINT , sigterm);
469 signal(SIGQUIT, sigterm);
470 main_thread = pthread_self();
473 processargs(argc, argv);
476 if(keyframe_interval<0) {
478 keyframe_interval=200;
483 fi = fopen(outputfilename, "wb");
485 fflush(stdout); fflush(stderr);
486 fprintf(stderr, "Couldn't open %s\n", outputfilename);
490 ret = videoreader_avifile_open(&video, filename);
493 printf("Error opening %s\n", filename);
498 printf("| video framerate: %f\n", video.fps);
499 printf("| video size: %dx%d\n", video.width, video.height);
500 printf("| audio rate: %d\n", video.rate);
501 printf("| audio channels: %d\n", video.channels);
504 ret = v2swf_init(&v2swf, &video);
506 v2swf_setparameter(&v2swf, "verbose", "1");
507 v2swf_setparameter(&v2swf, "quality", itoa(quality));
508 v2swf_setparameter(&v2swf, "blockdiff", "0");
509 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
510 v2swf_setparameter(&v2swf, "mp3_bitrate", "32");
511 //v2swf_setparameter(&v2swf, "fixheader", "1");
512 //v2swf_setparameter(&v2swf, "framerate", "15");
513 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
514 v2swf_setparameter(&v2swf, "prescale", "1");
515 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
516 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
518 v2swf_setparameter(&v2swf, "motioncompensation", "1");
524 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
525 void*buf = malloc(num);
526 video.getsamples(&video, buf, num);
528 } else if(audio_adjust<0) {
529 int num = (int)(-audio_adjust*video.fps);
530 void*buf = malloc(video.width*video.height*4);
533 video.getimage(&video, buf);
540 void*buf = malloc(video.width*video.height*4);
541 for(t=0;t<skip;t++) {
542 video.getimage(&video, buf);
543 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
545 printf("\rSkipping frame %d", frameno);fflush(stdout);
553 int l=v2swf_read(&v2swf, buffer, 4096);
554 fwrite(buffer, l, 1, fi);
558 printf("\rConverting frame %d", frameno);fflush(stdout);
564 v2swf_backpatch(&v2swf, outputfilename);