c63df799872c70d42552d903eae672abc3e6d566
[swftools.git] / avi2swf / avi2swf.cc
1 /* avi2swf.cc
2    Convert avi movie files into swf.
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26
27 #include "../config.h"
28
29 #ifdef HAVE_SIGNAL_H
30 #ifdef HAVE_PTHREAD_H
31 #include <pthread.h>
32 #include <signal.h>
33 #define DO_SIGNALS
34 #endif
35 #endif
36
37 extern "C" {
38 #include "../lib/args.h"
39 }
40 #include "v2swf.h"
41 #include "../lib/q.h"
42
43 #undef HAVE_CONFIG_H
44 #include <avifile/version.h>
45 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6) 
46    #include <avifile.h>
47    #include <aviplay.h>
48    #include <fourcc.h>
49    #include <creators.h>
50    #include <StreamInfo.h>
51    #define VERSION6
52 #else
53    #include <avifile.h>
54    #include <aviplay.h>
55    #include <aviutil.h>
56    #define Width width
57    #define Height height
58    #define Data data
59    #define Bpp bpp
60 #endif
61
62 static char * filename = 0;
63 static char * outputfilename = "output.swf";
64 int verbose = 0;
65
66 static int quality = 80;
67 static double scale = 1.0;
68 static int flip = 0;
69 static int expensive = 0;
70 static int flashversion = 6;
71 static int keyframe_interval = -1;
72 static int skip = 0;
73 static float audio_adjust = 0;
74
75 struct options_t options[] =
76 {
77  {"v","verbose"},
78  {"A","adjust"},
79  {"o","output"},
80  {"p","flip"},
81  {"q","quality"},
82  {"s","scale"},
83  {"S","skip"},
84  {"x","extragood"},
85  {"T","flashversion"},
86  {"V","version"},
87  {0,0}
88 };
89
90 int args_callback_option(char*name,char*val)
91 {
92     if(!strcmp(name, "V")) {
93         printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
94         exit(0);
95     } 
96     else if(!strcmp(name, "o")) {
97         outputfilename = val;
98         return 1;
99     }
100     else if(!strcmp(name, "q")) {
101         quality = atoi(val);
102         if(quality<0)
103             quality = 0;
104         if(quality>100)
105             quality = 100;
106         return 1;
107     }
108     else if(!strcmp(name, "p")) {
109         flip = 1;
110         return 0;
111     }
112     else if(!strcmp(name, "A")) {
113         audio_adjust = atof(val);
114         return 1;
115     }
116     else if(!strcmp(name, "v")) {
117         verbose = 1;
118         return 0;
119     }
120     else if(!strcmp(name, "T")) {
121         flashversion = atoi(val);
122         return 1;
123     }
124     else if(!strcmp(name, "x")) {
125         expensive = 1;
126         return 0;
127     }
128     else if(!strcmp(name, "S")) {
129         skip = atoi(val);
130         return 1;
131     }
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");
136             exit(1);
137         }
138         return 1;
139     }
140     fprintf(stderr, "Unknown option: -%s\n", name);
141     exit(1);
142 }
143 int args_callback_longoption(char*name,char*val)
144 {
145     return args_long2shortoption(options, name, val);
146 }
147 void args_callback_usage(char*name)
148 {    
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");
162     exit(0);
163 }
164 int args_callback_command(char*name,char*val)
165 {
166     if(filename) {
167         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
168                  filename, name);
169     }
170     filename = name;
171     return 0;
172 }
173
174 static char toabuf[128];
175 static char*ftoa(double a)
176 {
177     sprintf(toabuf, "%f", a);
178     return toabuf;
179 }
180 static char*itoa(int a)
181 {
182     sprintf(toabuf, "%d", a);
183     return toabuf;
184 }
185
186 typedef struct _videoreader_avifile_internal
187 {
188     IAviReadFile* player;
189     IAviReadStream* astream;
190     IAviReadStream* vstream;
191     int do_audio;
192     int do_video;
193     int eof;
194     int frame;
195     int soundbits;
196     ringbuffer_t audio_buffer;
197 } videoreader_avifile_internal;
198
199 static int shutdown_avi2swf = 0;
200 static int frameno = 0;
201
202 #ifdef DO_SIGNALS
203 pthread_t main_thread;
204 static void sigterm(int sig)
205 {
206     if(pthread_equal (pthread_self(), main_thread))
207     {
208         if(frameno>0 && !shutdown_avi2swf) {
209             if(verbose)
210                 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
211             shutdown_avi2swf++;
212         } else {
213             exit(1);
214         }
215     }
216 }
217 #endif
218
219 static void readSamples(videoreader_avifile_internal*i, void*buffer, int buffer_size, int numsamples)
220 {
221     int ret;
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);
225         if(samples_read<=0)
226             return;
227         ringbuffer_put(&i->audio_buffer, buffer, bytes_read);
228     }
229     ringbuffer_read(&i->audio_buffer, buffer, buffer_size);
230 }
231 int videoreader_avifile_getsamples(videoreader_t* v, void*buffer, int num)
232 {
233     if(verbose) {
234         printf("videoreader_getsamples(%d)\n", num);fflush(stdout);
235     }
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;
240         int t;
241         for(t=num-2;t>=0;t-=2) {
242             unsigned char x = b[t/2];
243             b[t] = 0;
244             b[t+1] = x-128;
245         }
246         return num;
247     }
248     if(i->soundbits == 16) {
249         readSamples(i, buffer, num, num/(v->channels*2));
250         return num;
251     }
252     return 0;
253 }
254 int videoreader_avifile_getimage(videoreader_t* v, void*buffer)
255 {
256     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
257     if(verbose) {
258         printf("videoreader_getimage()\n");fflush(stdout);
259     }
260
261     if(shutdown_avi2swf)
262         i->eof = 1;
263     
264     if(i->eof)
265         return 0;
266
267     if(i->vstream->ReadFrame() < 0) {
268         if(verbose) printf("vstream->ReadFrame() returned value < 0, shutting down...\n");
269         i->eof = 1;
270         return 0;
271     }
272     CImage*img2 = 0;
273     CImage*img = i->vstream->GetFrame();
274     if(!img) {
275         if(verbose) printf("vstream->GetFrame() returned NULL, shutting down...\n");
276         i->eof = 1;
277         return 0;
278     }
279     /* we convert the image to YUV first, because we can convert to RGB from YUV only */
280     img->ToYUV();
281     img->ToRGB();
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());
285         return 0;
286         BitmapInfo tmp(v->width, v->height, 24);
287         img2 = new CImage(img, &tmp);
288         img = img2;
289     }
290
291
292     frameno++;
293     i->frame++;
294     unsigned char*data = img->Data();
295     int bpp = img->Bpp();
296     if(bpp == 3) {
297         int x,y;
298         for(y=0;y<v->height;y++) {
299             unsigned char*from,*to;
300             to = &((unsigned char*)buffer)[y*v->width*4];
301             if(flip)
302                 from = img->At(v->height-y-1);
303             else
304                 from = img->At(y);
305             for(x=0;x<v->width;x++) {
306                 to[x*4+0] = 0;
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];
310             }
311         }
312         if(img2) delete img2;
313         return v->width*v->height*4;
314     } else {
315         if(img2) delete img2;
316         if(verbose) printf("Can't handle bpp %d, shutting down...\n", bpp);
317         return 0;
318     }
319 }
320 bool videoreader_avifile_eof(videoreader_t* v)
321 {
322     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
323     if(verbose) {
324         printf("videoreader_eof()\n");fflush(stdout);
325     }
326     return i->eof;
327 }
328 void videoreader_avifile_close(videoreader_t* v)
329 {
330     videoreader_avifile_internal*i = (videoreader_avifile_internal*)v->internal;
331     if(verbose) {
332         printf("videoreader_close()\n");fflush(stdout);
333     }
334     if(i->do_audio) {
335         ringbuffer_clear(&i->audio_buffer);
336     }
337 }
338 void* videoreader_avifile_getinfo(videoreader_t* v, char* name)
339 {
340     return 0;
341 }
342 void videoreader_avifile_setparameter(videoreader_t*v, char*name, char*value)
343 {
344     if(verbose) {
345         printf("videoreader_setparameter(%s, %s)\n", name, value);fflush(stdout);
346     }
347 }
348
349 int videoreader_avifile_open(videoreader_t* v, char* filename)
350 {
351     videoreader_avifile_internal* i;
352     i = (videoreader_avifile_internal*)malloc(sizeof(videoreader_avifile_internal));
353     memset(i, 0, sizeof(videoreader_avifile_internal));
354     memset(v, 0, sizeof(videoreader_t));
355     v->getsamples = videoreader_avifile_getsamples;
356     v->getinfo = videoreader_avifile_getinfo;
357     v->close = videoreader_avifile_close;
358     v->eof = videoreader_avifile_eof;
359     v->getimage = videoreader_avifile_getimage;
360     v->getsamples = videoreader_avifile_getsamples;
361     v->setparameter = videoreader_avifile_setparameter;
362     v->internal = i;
363     
364     i->do_video = 1;
365     i->do_audio = 1;
366
367     i->player = CreateIAviReadFile(filename);    
368     if(verbose) {
369         printf("%d streams (%d video, %d audio)\n", 
370                 i->player->StreamCount(),
371                 i->player->VideoStreamCount(),
372                 i->player->AudioStreamCount());
373     }
374     i->astream = i->player->GetStream(0, AviStream::Audio);
375     i->vstream = i->player->GetStream(0, AviStream::Video);
376     if(!i->vstream) {
377         printf("Couldn't open video stream\n");
378         i->do_video = 0;
379     }
380     if(!i->astream) {
381         printf("Couldn't open video stream\n");
382         i->do_audio = 0;
383     }
384
385     if(!i->do_video && !i->do_audio) {
386         printf("File has neither audio nor video streams.(?)\n");
387         return 0;
388     }
389
390 #ifndef VERSION6
391     MainAVIHeader head;
392     int dwMicroSecPerFrame = 0;
393     player->GetFileHeader(&head);
394     printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
395     printf("frames: %d\n", head.dwTotalFrames);
396     printf("streams: %d\n", head.dwStreams);
397     printf("width: %d\n", head.dwWidth);
398     printf("height: %d\n", head.dwHeight);
399     printf("sound: %u samples (%f seconds)\n", i->astream->GetEndPos(), i->astream->GetEndTime());
400     v->width = head.dwWidth;
401     v->height = head.dwHeight;
402     dwMicroSecPerFrame = head.dwMicroSecPerFrame;
403     samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
404     v->rate = (int)(astream->GetEndPos()/astream->GetEndTime());
405     v->fps = 1000000.0/dwMicroSecPerFrame;
406     i->soundbits = 16;
407 #else
408     if(i->do_video)
409     {
410         StreamInfo*videoinfo;
411         videoinfo = i->vstream->GetStreamInfo();
412         v->width = videoinfo->GetVideoWidth();
413         v->height = videoinfo->GetVideoHeight();
414         v->fps = (double)(videoinfo->GetFps());
415     }
416     if(i->do_audio)
417     {
418         WAVEFORMATEX wave;
419         StreamInfo*audioinfo;
420
421         i->astream->GetAudioFormatInfo(&wave,0);
422         audioinfo = i->astream->GetStreamInfo();
423
424         v->channels = wave.nChannels;
425         v->rate = wave.nSamplesPerSec;
426         i->soundbits = wave.wBitsPerSample;
427
428         if(v->channels==0 || v->rate==0 || i->soundbits==0 || wave.wFormatTag!=1) {
429             v->rate = audioinfo->GetAudioSamplesPerSec();
430             v->channels = audioinfo->GetAudioChannels();
431             i->soundbits = audioinfo->GetAudioBitsPerSample();
432         }
433
434         if(verbose) {
435             printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
436             printf("audioinfo: %d channels, %d bits/sample, rate %d\n", audioinfo->GetAudioChannels(), audioinfo->GetAudioBitsPerSample(), audioinfo->GetAudioSamplesPerSec());
437         }
438         if(i->soundbits != 8 && i->soundbits != 16) {
439             printf("Can't handle %d bit audio, disabling sound\n", wave.wBitsPerSample);
440             i->do_audio = 0;
441             i->soundbits = 0;
442             v->channels = 0;
443             v->rate = 0;
444         }
445     }
446 #endif
447     i->vstream -> StartStreaming();
448     if(i->do_audio) {
449         i->astream -> StartStreaming();
450         ringbuffer_init(&i->audio_buffer);
451 #ifdef VERSION6
452         WAVEFORMATEX wave;
453         i->astream -> GetOutputFormat(&wave, sizeof(wave));
454         printf("formatinfo: format %d, %d channels, %d bits/sample, rate %d, blockalign %d\n", wave.wFormatTag, wave.nChannels, wave.wBitsPerSample, wave.nSamplesPerSec, wave.nBlockAlign);
455 #endif
456     }
457
458     return 1;
459 }
460
461 int main (int argc,char ** argv)
462
463     videoreader_t video;
464     v2swf_t v2swf;
465     int ret;
466     FILE*fi;
467
468 #ifdef DO_SIGNALS
469     signal(SIGTERM, sigterm);
470     signal(SIGINT , sigterm);
471     signal(SIGQUIT, sigterm);
472     main_thread = pthread_self();
473 #endif
474
475     processargs(argc, argv);
476     if(!filename)
477         exit(0);
478     if(keyframe_interval<0) {
479         if(flashversion>=6)
480             keyframe_interval=200;
481         else
482             keyframe_interval=5;
483     }
484     
485     fi = fopen(outputfilename, "wb");
486     if(!fi) {
487         fflush(stdout); fflush(stderr);
488         fprintf(stderr, "Couldn't open %s\n", outputfilename);
489         exit(1);
490     }
491
492     ret = videoreader_avifile_open(&video, filename);
493
494     if(!ret) {
495         printf("Error opening %s\n", filename);
496         exit(1);
497     }
498
499     if(verbose) {
500         printf("| video framerate: %f\n", video.fps);
501         printf("| video size: %dx%d\n", video.width, video.height);
502         printf("| audio rate: %d\n", video.rate);
503         printf("| audio channels: %d\n", video.channels);
504     }
505
506     ret = v2swf_init(&v2swf, &video);
507     if(verbose)
508         v2swf_setparameter(&v2swf, "verbose", "1");
509     v2swf_setparameter(&v2swf, "quality", itoa(quality));
510     v2swf_setparameter(&v2swf, "blockdiff", "0");
511     v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
512     v2swf_setparameter(&v2swf, "mp3_bitrate", "32");
513     //v2swf_setparameter(&v2swf, "fixheader", "1");
514     //v2swf_setparameter(&v2swf, "framerate", "15");
515     v2swf_setparameter(&v2swf, "scale", ftoa(scale));
516     v2swf_setparameter(&v2swf, "prescale", "1");
517     v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
518     v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
519     if(expensive)
520         v2swf_setparameter(&v2swf, "motioncompensation", "1");
521
522     if(!verbose)
523         printf("\n");
524
525     if(audio_adjust>0) {
526         int num = ((int)(audio_adjust*video.rate))*video.channels*2;
527         void*buf = malloc(num);
528         video.getsamples(&video, buf, num);
529         free(buf);
530     } else if(audio_adjust<0) {
531         int num = (int)(-audio_adjust*video.fps);
532         void*buf = malloc(video.width*video.height*4);
533         int t;
534         for(t=0;t<num;t++) {
535             video.getimage(&video, buf);
536         }
537         free(buf);
538     }
539
540     if(skip) {
541         int t;
542         void*buf = malloc(video.width*video.height*4);
543         for(t=0;t<skip;t++) {
544             video.getimage(&video, buf);
545             video.getsamples(&video, buf, (int)((video.rate/video.fps)*video.channels*2));
546             if(!verbose) {
547                 printf("\rSkipping frame %d", frameno);fflush(stdout);
548             }
549         }
550         free(buf);
551     }
552
553     char buffer[4096];
554     while(1) {
555         int l=v2swf_read(&v2swf, buffer, 4096);
556         fwrite(buffer, l, 1, fi);
557         if(!l)
558             break;
559         if(!verbose) {
560             printf("\rConverting frame %d", frameno);fflush(stdout);
561         }
562     }
563     if(!verbose)
564         printf("\n");
565     fclose(fi);
566     v2swf_backpatch(&v2swf, outputfilename);
567     v2swf_close(&v2swf);
568 }
569