2 Convert avi movie files into swf.
3 As soon as there's an algorithm implemented for writing the
4 data directly to disk, this file should maybe go to ../src.
6 Part of the swftools package.
8 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
10 This file is distributed under the GPL, see file COPYING for details */
15 #include "../rfxswf.h"
22 char * outputfilename = "output.swf";
24 struct options_t options[] =
32 int args_callback_option(char*name,char*val)
34 if(!strcmp(name, "V")) {
35 printf("avi2swf - part of %s %s\n", PACKAGE, VERSION);
38 if(!strcmp(name, "o")) {
43 int args_callback_longoption(char*name,char*val)
45 return args_long2shortoption(options, name, val);
47 void args_callback_usage(char*name)
49 printf("\nUsage: %s file.swf\n", name);
50 printf("\t-h , --help\t\t Print help and exit\n");
51 printf("\t-o , --output=filename\t Specify output filename\n");
52 printf("\t-V , --version\t\t Print program version and exit\n");
55 int args_callback_command(char*name,char*val)
58 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
68 int main (int argc,char ** argv)
71 IAviReadStream* astream;
72 IAviReadStream* vstream;
75 memset(&swf, 0, sizeof(swf));
77 processargs(argc, argv);
81 player = CreateIAviReadFile(filename);
82 player->GetFileHeader(&head);
83 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
84 printf("frames: %d\n", head.dwTotalFrames);
85 printf("streams: %d\n", head.dwStreams);
86 printf("streams: %d\n", player->StreamCount());
87 printf("width: %d\n", head.dwWidth);
88 printf("height: %d\n", head.dwHeight);
90 astream = player->GetStream(0, AviStream::Audio);
91 vstream = player->GetStream(0, AviStream::Video);
93 vstream -> StartStreaming();
95 swf.frameRate = (int)(1000000.0/head.dwMicroSecPerFrame*256);
99 r.xmax = head.dwWidth*20;
100 r.ymax = head.dwHeight*20;
102 tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
104 swf_SetU8(tag,0); //black
108 U8*newdata = (U8*)malloc((head.dwWidth+3) * head.dwHeight * 4);
112 int lastsize = (head.dwWidth+3) * head.dwHeight * 4;
113 U8* lastdata = (U8*)malloc(lastsize);
115 memset(lastdata,0, lastsize);
118 if(vstream->ReadFrame()<0) {
122 printf("\rconvert frame %d",frame);
124 CImage*img = vstream->GetFrame();
127 int width = img->width();
128 int bpp = img->bpp();
129 int width4 = width*4;
130 int height = img->height();
139 tag = swf_InsertTag(tag, ST_REMOVEOBJECT2);
140 swf_SetU16(tag, 1); //depth
143 /* todo: dynamically decide whether to generate jpeg/lossless
144 bitmaps, (using transparency to modify the previous
145 picture), and which jpeg compression depth to use.
146 (btw: Are there video frame transitions which can
147 reasonably approximated by shapes?)
153 tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS);
154 swf_SetU16(tag, frame*2);
155 U8*mylastdata = lastdata;
156 for(y=0;y<height;y++) {
157 U8*nd = &newdata[width4*y];
158 U8*mydata = img->at(y);
160 for(x=0;x<width;x++) {
164 mylastdata[2] = mydata[2];
165 mylastdata[1] = mydata[1];
166 mylastdata[0] = mydata[0];
172 for(x=0;x<width;x++) {
173 int a = mylastdata[3]-data[3];
174 int b = mylastdata[2]-data[2];
175 int c = mylastdata[1]-data[1];
188 mylastdata[2] = mydata[2];
189 mylastdata[1] = mydata[1];
190 mylastdata[0] = mydata[0];
196 swf_SetLosslessBits(tag,width,height,newdata,BMF_32BIT);
199 tag = swf_InsertTag(tag, ST_DEFINEBITSJPEG2);
200 swf_SetU16(tag, frame*2);
201 JPEGBITS * jb = swf_SetJPEGBitsStart(tag,width,height,1);
202 U8*mylastdata = lastdata;
203 for(y=0;y<height;y++) {
205 U8*mydata = img->at(y);
206 for(x=0;x<width;x++) {
211 nd[0] = (mydata[2]-mylastdata[2])/2+0x80;
212 nd[1] = (mydata[1]-mylastdata[1])/2+0x80;
213 nd[2] = (mydata[0]-mylastdata[0])/2+0x80;
215 mylastdata[2] = mydata[2];
216 mylastdata[1] = mydata[1];
217 mylastdata[0] = mydata[0];
222 swf_SetJPEGBitsLine(jb,newdata);
224 swf_SetJPEGBitsFinish(jb);
227 tag = swf_InsertTag(tag, ST_DEFINESHAPE);
229 rgb.b = rgb.g = rgb.r = 0xff;
230 ls = swf_ShapeAddLineStyle(s,20,&rgb);
231 swf_GetMatrix(NULL,&m);
235 fs = swf_ShapeAddBitmapFillStyle(s,&m,frame*2,0);
236 swf_SetU16(tag ,frame*2+1); // ID
243 swf_SetShapeStyles(tag,s);
244 swf_ShapeCountBits(s,NULL,NULL);
245 swf_SetShapeBits(tag,s);
247 swf_ShapeSetAll(tag,s,0,0,ls,fs,0);
249 swf_ShapeSetLine(tag,s,width*20,0);
250 swf_ShapeSetLine(tag,s,0,height*20);
251 swf_ShapeSetLine(tag,s,-width*20,0);
252 swf_ShapeSetLine(tag,s,0,-height*20);
253 swf_ShapeSetEnd(tag);
255 tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
256 swf_ObjectPlace(tag,frame*2+1,1,0,0,0);
258 tag = swf_InsertTag(tag, ST_SHOWFRAME);
266 tag = swf_InsertTag(tag, ST_END);
268 f = open(outputfilename,O_WRONLY|O_CREAT|O_TRUNC, 0644);
269 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
272 swf_FreeTags(&swf); // cleanup