fixed -d (--scale) parameter
[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 #include "../lib/args.h"
30 #include "v2swf.h"
31 #ifdef WIN32
32 #include "videoreader_vfw.hh"
33 #else
34 #include "videoreader_avifile.hh"
35 #endif
36
37 static char * filename = 0;
38 static char * outputfilename = "output.swf";
39 int verbose = 0;
40
41 static int quality = 80;
42 static double scale = 1.0;
43 static int flip = 0;
44 static int expensive = 0;
45 static int flashversion = 6;
46 static int keyframe_interval = -1;
47 static int skip = 0;
48 static float audio_adjust = 0;
49 static int mp3_bitrate = 32;
50 static int samplerate = 11025;
51 static int numframes = 0;
52
53 static struct options_t options[] = {
54 {"h", "help"},
55 {"o", "output"},
56 {"A", "adjust"},
57 {"n", "num"},
58 {"m", "mp3-bitrate"},
59 {"r", "mp3-samplerate"},
60 {"d", "scale"},
61 {"k", "keyframe"},
62 {"p", "flip"},
63 {"q", "quality"},
64 {"x", "extragood"},
65 {"T", "flashversion"},
66 {"V", "version"},
67 {0,0}
68 };
69
70 int args_callback_option(char*name,char*val)
71 {
72     if(!strcmp(name, "V")) {
73         printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
74         exit(0);
75     } 
76     else if(!strcmp(name, "o")) {
77         outputfilename = val;
78         return 1;
79     }
80     else if(!strcmp(name, "n")) {
81         numframes = atoi(val);
82         return 1;
83     }
84     else if(!strcmp(name, "d")) {
85         scale = atof(val);
86         return 1;
87     }
88     else if(!strcmp(name, "q")) {
89         quality = atoi(val);
90         if(quality<0)
91             quality = 0;
92         if(quality>100)
93             quality = 100;
94         return 1;
95     }
96     else if(!strcmp(name, "p")) {
97         flip = 1;
98         return 0;
99     }
100     else if(!strcmp(name, "k")) {
101         keyframe_interval = atoi(val);
102         return 1;
103     }
104     else if(!strcmp(name, "A")) {
105         audio_adjust = atof(val);
106         return 1;
107     }
108     else if(!strcmp(name, "v")) {
109         verbose = 1;
110         return 0;
111     }
112     else if(!strcmp(name, "T")) {
113         flashversion = atoi(val);
114         return 1;
115     }
116     else if(!strcmp(name, "x")) {
117         expensive = 1;
118         return 0;
119     }
120     else if(!strcmp(name, "m")) {
121         mp3_bitrate = atoi(val);
122         return 0;
123     }
124     else if(!strcmp(name, "r")) {
125         samplerate = atoi(val);
126         if(samplerate >= 11000 && samplerate <= 12000)
127             samplerate = 11025;
128         else if(samplerate >= 22000 && samplerate <= 23000)
129             samplerate = 22050;
130         else if(samplerate >= 44000 && samplerate <= 45000)
131             samplerate = 44100;
132         else {
133             fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
134             fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
135             exit(1);
136         }
137         return 1;
138     }
139     else if(!strcmp(name, "S")) {
140         skip = atoi(val);
141         return 1;
142     }
143     else if(!strcmp(name, "s")) {
144         scale = atoi(val)/100.0;
145         if(scale>1.0 || scale<=0) {
146             fprintf(stderr, "Scale must be in the range 1-100!\n");
147             exit(1);
148         }
149         return 1;
150     }
151     fprintf(stderr, "Unknown option: -%s\n", name);
152     exit(1);
153 }
154 int args_callback_longoption(char*name,char*val)
155 {
156     return args_long2shortoption(options, name, val);
157 }
158 void args_callback_usage(char *name)
159 {
160     printf("\n");
161     printf("Usage: %s file.avi [-o output.swf]\n", name);
162     printf("\n");
163     printf("-h , --help                    Print help and exit\n");
164     printf("-o , --output filename         Specify output filename\n");
165     printf("-A , --adjust seconds          Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
166     printf("-n , --num frames              Number of frames to encode\n");
167     printf("-m , --mp3-bitrate <kbps>      Set the mp3 bitrate to encode audio with\n");
168     printf("-r , --mp3-samplerate <hz>     Set the mp3 samplerate to encode audio with (default: 11025)\n");
169     printf("-d , --scale <val>             Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
170     printf("-p , --flip                    Turn movie upside down\n");
171     printf("-q , --quality <val>           Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
172     printf("-x , --extragood               Enable some *very* expensive compression strategies.\n");
173     printf("-T , --flashversion <n>        Set output flash version to <n>.\n");
174     printf("-V , --version                 Print program version and exit\n");
175     printf("\n");
176 }
177 int args_callback_command(char*name,char*val)
178 {
179     if(filename) {
180         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
181                  filename, name);
182     }
183     filename = name;
184     return 0;
185 }
186
187 static char toabuf[128];
188 static char*ftoa(double a)
189 {
190     sprintf(toabuf, "%f", a);
191     return toabuf;
192 }
193 static char*itoa(int a)
194 {
195     sprintf(toabuf, "%d", a);
196     return toabuf;
197 }
198
199 #ifdef DO_SIGNALS
200 pthread_t main_thread;
201 static void sigterm(int sig)
202 {
203     if(pthread_equal (pthread_self(), main_thread))
204     {
205         if(frameno>0 && !shutdown_avi2swf) {
206             if(verbose)
207                 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
208             shutdown_avi2swf++;
209         } else {
210             exit(1);
211         }
212     }
213 }
214 #endif
215
216 int main (int argc,char ** argv)
217
218     videoreader_t video;
219     v2swf_t v2swf;
220     int ret;
221     FILE*fi;
222
223 #ifdef DO_SIGNALS
224     signal(SIGTERM, sigterm);
225     signal(SIGINT , sigterm);
226     signal(SIGQUIT, sigterm);
227     main_thread = pthread_self();
228 #endif
229
230     processargs(argc, argv);
231     if(!filename) {
232         fprintf(stderr, "You must supply a filename");
233         exit(0);
234     }
235     if(keyframe_interval<0) {
236         if(flashversion>=6)
237             keyframe_interval=20;
238         else
239             keyframe_interval=5;
240     }
241     
242     fi = fopen(outputfilename, "wb");
243     if(!fi) {
244         fflush(stdout); fflush(stderr);
245         fprintf(stderr, "Couldn't open %s\n", outputfilename);
246         exit(1);
247     }
248     
249 #ifdef WIN32
250     ret = videoreader_vfw_open(&video, filename);
251 #else
252     ret = videoreader_avifile_open(&video, filename);
253 #endif
254
255     if(ret<0) {
256         fprintf(stderr, "Error opening %s\n", filename);
257         exit(1);
258     }
259
260     if(verbose) {
261         printf("| video framerate: %f\n", video.fps);
262         printf("| video size: %dx%d\n", video.width, video.height);
263         printf("| audio rate: %d\n", video.samplerate);
264         printf("| audio channels: %d\n", video.channels);
265     }
266
267     ret = v2swf_init(&v2swf, &video);
268     if(verbose)
269         v2swf_setparameter(&v2swf, "verbose", "1");
270     if(numframes)
271         v2swf_setparameter(&v2swf, "numframes", itoa(numframes));
272     v2swf_setparameter(&v2swf, "quality", itoa(quality));
273     v2swf_setparameter(&v2swf, "blockdiff", "0");
274     v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
275     v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
276     v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
277     //v2swf_setparameter(&v2swf, "fixheader", "1");
278     //v2swf_setparameter(&v2swf, "framerate", "15");
279     v2swf_setparameter(&v2swf, "scale", ftoa(scale));
280     v2swf_setparameter(&v2swf, "prescale", "1");
281     v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
282     v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
283     if(expensive)
284         v2swf_setparameter(&v2swf, "motioncompensation", "1");
285     if(flip)
286         video.setparameter(&video, "flip", "1");
287     if(verbose)
288         video.setparameter(&video, "verbose", "1");
289
290     if(!verbose)
291         printf("\n");
292
293     if(audio_adjust>0) {
294         int num = ((int)(audio_adjust*video.samplerate))*video.channels*2;
295         void*buf = malloc(num);
296         video.getsamples(&video, buf, num);
297         free(buf);
298     } else if(audio_adjust<0) {
299         int num = (int)(-audio_adjust*video.fps);
300         void*buf = malloc(video.width*video.height*4);
301         int t;
302         for(t=0;t<num;t++) {
303             video.getimage(&video, buf);
304         }
305         free(buf);
306     }
307
308     if(skip) {
309         int t;
310         void*buf = malloc(video.width*video.height*4);
311         for(t=0;t<skip;t++) {
312             video.getimage(&video, buf);
313             video.getsamples(&video, buf, (int)((video.samplerate/video.fps)*video.channels*2));
314             if(!verbose) {
315                 printf("\rSkipping frame %d", video.frame);fflush(stdout);
316             }
317         }
318         free(buf);
319     }
320
321     char buffer[4096];
322     while(1) {
323         int l=v2swf_read(&v2swf, buffer, 4096);
324         fwrite(buffer, l, 1, fi);
325         if(!l)
326             break;
327         if(!verbose) {
328             printf("\rConverting frame %d", video.frame);fflush(stdout);
329         }
330     }
331     if(!verbose)
332         printf("\n");
333     fclose(fi);
334     v2swf_backpatch(&v2swf, outputfilename);
335     v2swf_close(&v2swf);
336 }
337