fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / avi2swf / avi2swf.cc
blobc68fedbbca3e0566260f60ac8f8d04022df749f2
1 /* avi2swf.cc
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 */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
27 #include "../config.h"
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
37 static char * filename = 0;
38 static char * outputfilename = "output.swf";
39 int verbose = 0;
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 static char* skipframes = 0;
54 static struct options_t options[] = {
55 {"h", "help"},
56 {"o", "output"},
57 {"A", "adjust"},
58 {"n", "num"},
59 {"m", "mp3-bitrate"},
60 {"r", "mp3-samplerate"},
61 {"s", "scale"},
62 {"S", "skipframes"},
63 {"p", "flip"},
64 {"q", "quality"},
65 {"k", "keyframe"},
66 {"x", "extragood"},
67 {"T", "flashversion"},
68 {"V", "version"},
69 {0,0}
72 int args_callback_option(char*name,char*val)
74 if(!strcmp(name, "V")) {
75 printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION);
76 exit(0);
78 else if(!strcmp(name, "o")) {
79 outputfilename = val;
80 return 1;
82 else if(!strcmp(name, "n")) {
83 numframes = atoi(val);
84 return 1;
86 else if(!strcmp(name, "d")) {
87 scale = atof(val);
88 return 1;
90 else if(!strcmp(name, "q")) {
91 quality = atoi(val);
92 if(quality<0)
93 quality = 0;
94 if(quality>100)
95 quality = 100;
96 return 1;
98 else if(!strcmp(name, "p")) {
99 flip = 1;
100 return 0;
102 else if(!strcmp(name, "k")) {
103 keyframe_interval = atoi(val);
104 return 1;
106 else if(!strcmp(name, "A")) {
107 audio_adjust = atof(val);
108 return 1;
110 else if(!strcmp(name, "v")) {
111 verbose = 1;
112 return 0;
114 else if(!strcmp(name, "T")) {
115 flashversion = atoi(val);
116 return 1;
118 else if(!strcmp(name, "x")) {
119 expensive = 1;
120 return 0;
122 else if(!strcmp(name, "m")) {
123 mp3_bitrate = atoi(val);
124 return 1;
126 else if(!strcmp(name, "r")) {
127 samplerate = atoi(val);
128 if(samplerate >= 11000 && samplerate <= 12000)
129 samplerate = 11025;
130 else if(samplerate >= 22000 && samplerate <= 23000)
131 samplerate = 22050;
132 else if(samplerate >= 44000 && samplerate <= 45000)
133 samplerate = 44100;
134 else {
135 fprintf(stderr, "Invalid samplerate: %d\n", samplerate);
136 fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate);
137 exit(1);
139 return 1;
141 else if(!strcmp(name, "S")) {
142 skip = atoi(val);
143 return 1;
145 else if(!strcmp(name, "C")) {
146 skipframes = strdup(val);
147 return 1;
149 else if(!strcmp(name, "s")) {
150 scale = atoi(val)/100.0;
151 if(scale>1.0 || scale<=0) {
152 fprintf(stderr, "Scale must be in the range 1-100!\n");
153 exit(1);
155 return 1;
157 fprintf(stderr, "Unknown option: -%s\n", name);
158 exit(1);
160 int args_callback_longoption(char*name,char*val)
162 return args_long2shortoption(options, name, val);
164 void args_callback_usage(char *name)
166 printf("\n");
167 printf("Usage: %s file.avi [-o output.swf]\n", name);
168 printf("\n");
169 printf("-h , --help Print help and exit\n");
170 printf("-o , --output filename Specify output filename\n");
171 printf("-A , --adjust seconds Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n");
172 printf("-n , --num frames Number of frames to encode\n");
173 printf("-m , --mp3-bitrate <kbps> Set the mp3 bitrate to encode audio with\n");
174 printf("-r , --mp3-samplerate <hz> Set the mp3 samplerate to encode audio with (default: 11025)\n");
175 printf("-s , --scale <val> Scale down to factor <val>. (in %, e.g. 100 = original size)\n");
176 printf("-S , --skipframes <num> Skip <num> frames before starting the conversion.\n");
177 printf("-p , --flip Turn movie upside down\n");
178 printf("-q , --quality <val> Set the quality to <val>. (0-100, 0=worst, 100=best, default:80)\n");
179 printf("-k , --keyframe Set the number of intermediate frames between keyframes.\n");
180 printf("-x , --extragood Enable some *very* expensive compression strategies.\n");
181 printf("-T , --flashversion <n> Set output flash version to <n>.\n");
182 printf("-V , --version Print program version and exit\n");
183 printf("\n");
185 int args_callback_command(char*name,char*val)
187 if(filename) {
188 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
189 filename, name);
191 filename = name;
192 return 0;
195 static char toabuf[128];
196 static char*ftoa(double a)
198 sprintf(toabuf, "%f", a);
199 return toabuf;
201 static char*itoa(int a)
203 sprintf(toabuf, "%d", a);
204 return toabuf;
207 #ifdef DO_SIGNALS
208 pthread_t main_thread;
209 static void sigterm(int sig)
211 if(pthread_equal (pthread_self(), main_thread))
213 if(frameno>0 && !shutdown_avi2swf) {
214 if(verbose)
215 printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig);
216 shutdown_avi2swf++;
217 } else {
218 exit(1);
222 #endif
224 int main (int argc,char ** argv)
226 videoreader_t video;
227 v2swf_t v2swf;
228 int ret;
229 FILE*fi;
231 #ifdef DO_SIGNALS
232 signal(SIGTERM, sigterm);
233 signal(SIGINT , sigterm);
234 signal(SIGQUIT, sigterm);
235 main_thread = pthread_self();
236 #endif
238 processargs(argc, argv);
239 if(!filename) {
240 fprintf(stderr, "You must supply a filename");
241 exit(0);
243 if(keyframe_interval<0) {
244 if(flashversion>=6)
245 keyframe_interval=20;
246 else
247 keyframe_interval=5;
250 fi = fopen(outputfilename, "wb");
251 if(!fi) {
252 fflush(stdout); fflush(stderr);
253 fprintf(stderr, "Couldn't open %s\n", outputfilename);
254 exit(1);
257 #ifdef WIN32
258 ret = videoreader_vfw_open(&video, filename);
259 #else
260 ret = videoreader_avifile_open(&video, filename);
261 #endif
263 if(ret<0) {
264 fprintf(stderr, "Error opening %s\n", filename);
265 exit(1);
268 if(verbose) {
269 printf("| video framerate: %f\n", video.fps);
270 printf("| video size: %dx%d\n", video.width, video.height);
271 printf("| audio rate: %d\n", video.samplerate);
272 printf("| audio channels: %d\n", video.channels);
275 ret = v2swf_init(&v2swf, &video);
276 if(verbose)
277 v2swf_setparameter(&v2swf, "verbose", "1");
278 if(numframes)
279 v2swf_setparameter(&v2swf, "numframes", itoa(numframes));
280 v2swf_setparameter(&v2swf, "quality", itoa(quality));
281 v2swf_setparameter(&v2swf, "blockdiff", "0");
282 v2swf_setparameter(&v2swf, "blockdiff_mode", "exact");
283 v2swf_setparameter(&v2swf, "mp3_bitrate", itoa(mp3_bitrate));
284 v2swf_setparameter(&v2swf, "samplerate", itoa(samplerate));
285 //v2swf_setparameter(&v2swf, "fixheader", "1");
286 //v2swf_setparameter(&v2swf, "framerate", "15");
287 v2swf_setparameter(&v2swf, "scale", ftoa(scale));
288 v2swf_setparameter(&v2swf, "prescale", "1");
289 v2swf_setparameter(&v2swf, "flash_version", itoa(flashversion));
290 v2swf_setparameter(&v2swf, "keyframe_interval", itoa(keyframe_interval));
291 if(skipframes)
292 v2swf_setparameter(&v2swf, "skipframes", skipframes);
293 if(expensive)
294 v2swf_setparameter(&v2swf, "motioncompensation", "1");
295 if(flip)
296 video.setparameter(&video, "flip", "1");
297 if(verbose)
298 video.setparameter(&video, "verbose", "1");
300 if(!verbose)
301 printf("\n");
303 if(audio_adjust>0) {
304 int num = ((int)(audio_adjust*video.samplerate))*video.channels*2;
305 void*buf = malloc(num);
306 video.getsamples(&video, buf, num);
307 free(buf);
308 } else if(audio_adjust<0) {
309 int num = (int)(-audio_adjust*video.fps);
310 void*buf = malloc(video.width*video.height*4);
311 int t;
312 for(t=0;t<num;t++) {
313 video.getimage(&video, buf);
315 free(buf);
318 if(skip) {
319 int t;
320 void*buf = malloc(video.width*video.height*4);
321 for(t=0;t<skip;t++) {
322 video.getimage(&video, buf);
323 video.getsamples(&video, buf, (int)((video.samplerate/video.fps)*video.channels*2));
324 if(!verbose) {
325 printf("\rSkipping frame %d", video.frame);fflush(stdout);
328 free(buf);
331 char buffer[4096];
332 while(1) {
333 int l=v2swf_read(&v2swf, buffer, 4096);
334 fwrite(buffer, l, 1, fi);
335 if(!l)
336 break;
337 if(!verbose) {
338 printf("\rConverting frame %d", video.frame);fflush(stdout);
341 if(!verbose)
342 printf("\n");
343 fclose(fi);
344 v2swf_backpatch(&v2swf, outputfilename);
345 v2swf_close(&v2swf);