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 file is distributed under the GPL, see file COPYING for details */
15 #include "../config.h"
26 #include "../lib/args.h"
32 #include <avifile/version.h>
33 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
38 #include <StreamInfo.h>
50 static char * filename = 0;
51 static char * outputfilename = "output.swf";
54 static int quality = 80;
55 static double scale = 1.0;
57 static int expensive = 0;
58 static int flashversion = 6;
59 static int keyframe_interval = -1;
61 static float audio_adjust = 0;
63 struct options_t options[] =
78 int args_callback_option(char*name,char*val)
80 if(!strcmp(name, "V")) {
81 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
84 else if(!strcmp(name, "o")) {
88 else if(!strcmp(name, "q")) {
96 else if(!strcmp(name, "p")) {
100 else if(!strcmp(name, "A")) {
101 audio_adjust = atof(val);
104 else if(!strcmp(name, "v")) {
108 else if(!strcmp(name, "T")) {
109 flashversion = atoi(val);
112 else if(!strcmp(name, "x")) {
116 else if(!strcmp(name, "S")) {
120 else if(!strcmp(name, "s")) {
121 scale = atoi(val)/100.0;
122 if(scale>1.0 || scale<=0) {
123 fprintf(stderr, "Scale must be in the range 1-100!\n");
128 fprintf(stderr, "Unknown option: -%s\n", name);
131 int args_callback_longoption(char*name,char*val)
133 return args_long2shortoption(options, name, val);
135 void args_callback_usage(char*name)
137 printf("\nUsage: %s file.avi\n", name);
138 printf("\t-h , --help\t\t Print help and exit\n");
139 printf("\t-o , --output filename\t Specify output filename\n");
140 printf("\t-A , --adjust seconds\t Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
141 printf("\t-n , --num frames\t Number of frames to encode\n");
142 printf("\t-d , --scale <val>\t Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
143 printf("\t-p , --flip\t\t Turn movie upside down\n");
144 printf("\t-q , --quality <val>\t Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
145 printf("\t-x , --extragood\t Enable some *very* expensive compression strategies. You may\n");
146 printf("\t \t want to let this run overnight.\n");
147 printf("\t-T , --flashversion <n>\t Set output flash version to <n>. Notice: H.263 compression will only be\n");
148 printf("\t \t used for n >= 6.\n");
149 printf("\t-V , --version\t\t Print program version and exit\n");
152 int args_callback_command(char*name,char*val)
155 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
162 static char toabuf[128];
163 static char*ftoa(double a)
165 sprintf(toabuf, "%f", a);
168 static char*itoa(int a)
170 sprintf(toabuf, "%d", a);
174 typedef struct _videoreader_avifile_internal
176 IAviReadFile* player;
177 IAviReadStream* astream;
178 IAviReadStream* vstream;
184 ringbuffer_t audio_buffer;
185 } videoreader_avifile_internal;
187 static int shutdown_avi2swf = 0;
188 static int frameno = 0;
191 pthread_t main_thread;
192 static void sigterm(int sig)
194 if(pthread_equal (pthread_self(), main_thread))
196 if(frameno>0 && !shutdown_avi2swf) {
198 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
207 static void readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
210 while(i->audio_buffer.available < buffer_size) {
211 unsigned int samples_read = 0, bytes_read = 0;
212 ret = i->astream->ReadFrames(buffer, buffer_size, numsamples, samples_read, bytes_read);
215 ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
217 ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
219 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
222 printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
224 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
225 if(i->soundbits == 8) {
226 readSamples(i, buffer, num/2, num/(v->channels*2));
227 unsigned char*b = (unsigned char*)buffer;
229 for(t=num-2;t>=0;t-=2) {
230 unsigned char x = b[t/2];
236 if(i->soundbits == 16) {
237 readSamples(i, buffer, num, num/(v->channels*2));
242 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
244 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
246 printf("videoreader_getimage()\n");fflush(stdout);
255 if(i->vstream->ReadFrame() < 0) {
256 if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
261 CImage*img = i->vstream->GetFrame();
263 if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
267 /* we convert the image to YUV first, because we can convert to RGB from YUV only */
270 if(img->Bpp() != 3) {
271 /* TODO: this doesn't work yet */
272 if(verbose) printf("Can't handle Bpp %d, shutting down...\n", img->Bpp());
274 BitmapInfo tmp(v->width, v->height, 24);
275 img2 = new CImage(img, &tmp);
282 unsigned char*data = img->Data();
283 int bpp = img->Bpp();
286 for(y=0;y<v->height;y++) {
287 unsigned char*from,*to;
288 to = &((unsigned char*)buffer)[y*v->width*4];
290 from = img->At(v->height-y-1);
293 for(x=0;x<v->width;x++) {
295 to[x*4+1] = from[x*3+2];
296 to[x*4+2] = from[x*3+1];
297 to[x*4+3] = from[x*3+0];
300 if(img2) delete img2;
301 return v->width*v->height*4;
303 if(img2) delete img2;
304 if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
308 bool videoreader_avifile_eof(videoreader_t* v)
310 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
312 printf("videoreader_eof()\n");fflush(stdout);
316 void videoreader_avifile_close(videoreader_t* v)
318 videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
319 ringbuffer_clear(&i->audio_buffer);
321 printf("videoreader_close()\n");fflush(stdout);
324 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
328 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
331 printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
335 int videoreader_avifile_open(videoreader_t* v, char* filename)
337 videoreader_avifile_internal* i;
338 i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
339 memset(i, 0, sizeof(videoreader_avifile_internal));
340 memset(v, 0, sizeof(videoreader_t));
341 v->getsamples = videoreader_avifile_getsamples;
342 v->getinfo = videoreader_avifile_getinfo;
343 v->close = videoreader_avifile_close;
344 v->eof = videoreader_avifile_eof;
345 v->getimage = videoreader_avifile_getimage;
346 v->getsamples = videoreader_avifile_getsamples;
347 v->setparameter = videoreader_avifile_setparameter;
353 i->player = CreateIAviReadFile(filename);
355 printf("%d streams (%d video, %d audio)\n",
356 i->player->StreamCount(),
357 i->player->VideoStreamCount(),
358 i->player->AudioStreamCount());
360 i->astream = i->player->GetStream(0, AviStream::Audio);
361 i->vstream = i->player->GetStream(0, AviStream::Video);
363 printf("Couldn't open video stream\n");
367 printf("Couldn't open video stream\n");
371 if(!i->do_video && !i->do_audio) {
372 printf("File has neither audio nor video streams.(?)\n");
378 int dwMicroSecPerFrame = 0;
379 player->GetFileHeader(&head);
380 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
381 printf("frames: %d\n", head.dwTotalFrames);
382 printf("streams: %d\n", head.dwStreams);
383 printf("width: %d\n", head.dwWidth);
384 printf("height: %d\n", head.dwHeight);
385 printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
386 v->width = head.dwWidth;
387 v->height = head.dwHeight;
388 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
389 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
390 v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
391 v->fps = 1000000.0/dwMicroSecPerFrame;
396 StreamInfo*videoinfo;
397 videoinfo = i->vstream->GetStreamInfo();
398 v->width = videoinfo->GetVideoWidth();
399 v->height = videoinfo->GetVideoHeight();
400 v->fps = (double)(videoinfo->GetFps());
405 StreamInfo*audioinfo;
407 i->astream->GetAudioFormatInfo(&wave,0);
408 audioinfo = i->astream->GetStreamInfo();
410 v->channels = wave.nChannels;
411 v->rate = wave.nSamplesPerSec;
412 i->soundbits = wave.wBitsPerSample;
414 if(v->channels==0 || v->rate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
415 v->rate = audioinfo->GetAudioSamplesPerSec();
416 v->channels = audioinfo->GetAudioChannels();
417 i->soundbits = audioinfo->GetAudioBitsPerSample();
421 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
422 printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
424 if(i->soundbits != 8 && i->soundbits != 16) {
425 printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
433 i->vstream -> StartStreaming();
435 i->astream -> StartStreaming();
436 ringbuffer_init(&i->audio_buffer);
439 i->astream -> GetOutputFormat(&wave, sizeof(wave));
440 printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
447 int main (int argc,char ** argv)
455 signal(SIGTERM, sigterm);
456 signal(SIGINT , sigterm);
457 signal(SIGQUIT, sigterm);
458 main_thread = pthread_self();
461 processargs(argc, argv);
464 if(keyframe_interval<0) {
466 keyframe_interval=200;
471 fi = fopen(outputfilename, "wb");
473 fflush(stdout); fflush(stderr);
474 fprintf(stderr, "Couldn't open %s\n", outputfilename);
478 ret = videoreader_avifile_open(&video, filename);
481 printf("Error opening %s\n", filename);
486 printf("| video framerate: %f\n", video.fps);
487 printf("| video size: %dx%d\n", video.width, video.height);
488 printf("| audio rate: %d\n", video.rate);
489 printf("| audio channels: %d\n", video.channels);
492 ret = v2swf_init(&v2swf, &video);
494 v2swf_setparameter(&v2swf, "verbose", "1");
495 v2swf_setparameter(&v2swf, "quality", itoa(quality));
496 v2swf_setparameter(&v2swf, "blockdiff", "0");
497 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
498 v2swf_setparameter(&v2swf, "mp3_bitrate", "32");
499 //v2swf_setparameter(&v2swf, "fixheader", "1");
500 //v2swf_setparameter(&v2swf, "framerate", "15");
501 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
502 v2swf_setparameter(&v2swf, "prescale", "1");
503 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
504 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
506 v2swf_setparameter(&v2swf, "motioncompensation", "1");
512 int num = ((int)(audio_adjust*video.rate))*video.channels*2;
513 void*buf = malloc(num);
514 video.getsamples(&video, buf, num);
516 } else if(audio_adjust<0) {
517 int num = (int)(-audio_adjust*video.fps);
518 void*buf = malloc(video.width*video.height*4);
521 video.getimage(&video, buf);
528 void*buf = malloc(video.width*video.height*4);
529 for(t=0;t<skip;t++) {
530 video.getimage(&video, buf);
531 video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
533 printf("\rSkipping frame %d", frameno);fflush(stdout);
541 int l=v2swf_read(&v2swf, buffer, 4096);
542 fwrite(buffer, l, 1, fi);
546 printf("\rConverting frame %d", frameno);fflush(stdout);
552 v2swf_backpatch(&v2swf, outputfilename);