2 Convert avi movie files into swf.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
13 #include "../lib/rfxswf.h"
14 #include "../lib/args.h"
17 #include <avifile/version.h>
18 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION>=6)
23 #include <StreamInfo.h>
37 37 bytes per shape (rectangle)
38 8-12 bytes per placeobject
39 4 bytes per removeobject2
40 696+ bytes per definejpeg2 (576 jpegtables)
41 576 bytes per jpegtables
42 122+ bytes per definejpeg
44 blocks*140 = minimal bytes per frames
45 5000/140 = maximal blocks with framerate 5000
50 static int cache_size=38; //in frames
52 static char * filename = 0;
53 static char * outputfilename = "output.swf";
54 static unsigned int firstframe = 0;
55 static unsigned int lastframe = 0x7fffffff;
57 static int jpeg_quality = 20;
60 static double scale = 1.0;
63 #ifndef ST_DEFINEBITSJPEG
64 #define ST_DEFINEBITSJPEG 6
69 struct options_t options[] =
82 int args_callback_option(char*name,char*val)
84 if(!strcmp(name, "V")) {
85 printf("avi2swf - part of %s %s\n", PACKAGE, VERSION);
88 else if(!strcmp(name, "o")) {
92 else if(!strcmp(name, "n")) {
93 lastframe = atoi(val);
96 else if(!strcmp(name, "s")) {
97 firstframe = atoi(val);
100 else if(!strcmp(name, "p")) {
104 else if(!strcmp(name, "d")) {
105 scale = atoi(val)/100.0;
106 if(scale>1.0 || scale<=0) {
107 fprintf(stderr, "Scale must be in the range 1-100!\n");
112 else if(!strcmp(name, "z")) {
116 fprintf(stderr, "Unknown option: -%s\n", name);
119 int args_callback_longoption(char*name,char*val)
121 return args_long2shortoption(options, name, val);
123 void args_callback_usage(char*name)
125 printf("\nUsage: %s file.avi\n", name);
126 printf("\t-h , --help\t\t Print help and exit\n");
127 printf("\t-o , --output filename\t Specify output filename\n");
128 printf("\t-n , --num frames\t Number of frames to encode\n");
129 printf("\t-s , --start frame\t First frame to encode\n");
130 printf("\t-d , --scale factor\t Scale to factor percent\n");
131 printf("\t-p , --flip\t\t Turn movie upside down\n");
132 printf("\t-V , --version\t\t Print program version and exit\n");
135 int args_callback_command(char*name,char*val)
138 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
145 /* id allocation/deallocation */
147 unsigned int idtab_pos=1;
150 while(idtab[idtab_pos] || !idtab_pos)
160 void makeshape(int file, int id, int gfxid, int width, int height)
169 tag = swf_InsertTag(NULL, ST_DEFINESHAPE);
171 rgb.b = rgb.g = rgb.r = 0xff;
173 ls = swf_ShapeAddLineStyle(s,20,&rgb);
174 swf_GetMatrix(NULL,&m);
178 fs = swf_ShapeAddBitmapFillStyle(s,&m,gfxid,0);
179 swf_SetU16(tag,id); // ID
186 swf_SetShapeStyles(tag,s);
187 swf_ShapeCountBits(s,NULL,NULL);
188 swf_SetShapeBits(tag,s);
190 swf_ShapeSetAll(tag,s,0,0,lines?ls:0,fs,0);
192 swf_ShapeSetLine(tag,s,width*20,0);
193 swf_ShapeSetLine(tag,s,0,height*20);
194 swf_ShapeSetLine(tag,s,-width*20,0);
195 swf_ShapeSetLine(tag,s,0,-height*20);
196 swf_ShapeSetEnd(tag);
197 filesize += swf_WriteTag(file, tag);
202 void setshape(int file,int id,int depth,int x,int y,CXFORM*cx)
206 m.sx = 0x10000; m.sy = 0x10000;
210 if(cx && !((cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256)
211 ||(cx->a1|cx->r1|cx->g1|cx->b1))) cx = 0;
212 tag = swf_InsertTag(NULL,ST_PLACEOBJECT2);
213 swf_ObjectPlace(tag,id,depth,&m,cx,0);
214 filesize += swf_WriteTag(file, tag);
222 // static int xblocksize;
223 // static int yblocksize;
236 class GfxBlockCache {
239 char*expire; //0=block's free
248 GfxBlockCache(int file)
251 size = xblocks*yblocks*cache_size;
252 printf("initializing cache (%d entries)\n", size);
253 list = new GfxBlock[size];
254 expire = new char[size];
256 memset(expire,0,size);
257 memset(list,0,sizeof(GfxBlock)*size);
258 memset(ids,0,sizeof(int)*size);
263 void insert(GfxBlock*block, int gfxid)
273 // cache full- don't insert item
277 free(list[pos].data);
279 //TODO: free this in the SWF, also
281 list[pos].data=(U8*)malloc(block->len);
282 memcpy(list[pos].data,block->data,block->len);
283 list[pos].len = block->len;
284 expire[pos] = cache_size;
287 int find(GfxBlock*block, CXFORM*cxform)
289 //TODO: do least square regression here to derive cxform
297 int t = (block->len);
298 U8*ptr1 = block->data;
299 U8*ptr2 = list[s].data;
301 // notice: we treat r,g,b as equal here.
303 int a = (*ptr1++)-(*ptr2++);
306 if(bestsum < 0 || bestsum > sum2) {
315 best = bestsum/block->len;
321 expire[bestid]= cache_size;
344 printf("destroying cache...\n");
346 printf("hits:%d (%02d%%)\n", hits, hits*100/(hits+misses));
347 printf("misses:%d (%02d%%)\n", misses, misses*100/(hits+misses));
350 if(expire[t] && list[t].data)
358 class GfxBlockEncoder {
366 void init(int depth, int posx,int posy, int sizex, int sizey)
368 this->basedepth = depth;
373 this->depth[0] = this->depth[1] = this->depth[2] = -1;
377 /* clear everything in the block */
383 tag = swf_InsertTag(NULL, ST_REMOVEOBJECT2);
384 swf_SetU16(tag, basedepth+t); //depth
385 filesize += swf_WriteTag(file, tag);
390 void writeiframe(int file, GfxBlock*block)
394 int gfxid = get_free_id();
395 int shapeid = get_free_id();
397 //memset(data,0,sizex*sizey*3);
398 TAG*tag = swf_InsertTag(NULL, ST_DEFINEBITS);
399 JPEGBITS * jb = swf_SetJPEGBitsStart(tag,sizex,sizey,jpeg_quality);
400 tag->len = 0; //bad hack
401 swf_SetU16(tag, gfxid);
404 swf_SetJPEGBitsLine(jb,&block->data[y*sizex*3]);
405 swf_SetJPEGBitsFinish(jb);
406 filesize += swf_WriteTag(file, tag);
409 cache->insert(block, shapeid);
411 makeshape(file, shapeid, gfxid, sizex, sizey);
412 setshape(file, shapeid, basedepth+1, posx, posy, 0);
415 void writereference(int file, int shapeid, CXFORM*form)
417 if(depth[1]!=shapeid)
420 setshape(file, shapeid, basedepth+1, posx, posy, form);
424 void compress(int file, GfxBlock*block)
427 int id = cache->find(block, &form);
429 writeiframe(file, block);
431 writereference(file, id, &form);
436 void initdisplay(int file)
442 for(t=0;t<xblocks;t++)
443 blocks[t].clear(file);
448 xblocksize = (width/3)&~7;
449 yblocksize = (height/2)&~7;
450 xblocks = width/xblocksize;
451 yblocks = height/yblocksize;
452 printf("%dx%d blocks of size %dx%d\n", xblocks,yblocks, xblocksize, yblocksize);
453 printf("cutting lower %d lines, right %d columns\n",
454 height-yblocks*yblocksize, width-xblocks*xblocksize);
455 blocks = new GfxBlockEncoder[xblocks*yblocks];
456 blockbuffer = new U8[xblocksize*yblocksize*4]; //should be 3
457 cache = new GfxBlockCache(file);
459 for(t=0;t<xblocks*yblocks;t++) {
461 (t%xblocks)*xblocksize,
462 (t/xblocks)*yblocksize,
463 xblocksize, yblocksize);
466 TAG*tag = swf_InsertTag(NULL, ST_JPEGTABLES);
467 JPEGBITS * jpeg = swf_SetJPEGBitsStart(tag, xblocksize, yblocksize, jpeg_quality);
468 filesize += swf_WriteTag(file, tag);
473 void destroydisplay(int file)
486 short int* sound_buffer;
490 IAviReadStream* astream;
493 unsigned samples_read, bytes_read;
495 short int tmpbuf[4096];
496 ret = astream->ReadFrames(tmpbuf, 4096*sizeof(short int),
497 4096, samples_read, bytes_read);
499 printf("couldn't read %d samples\n", mp3_block_size);
503 samples_read = bytes_read/sizeof(short int);
504 for(t=0;t<samples_read/2;t++) {
505 sound_buffer[write_pos+t] = tmpbuf[t*2];
507 write_pos += samples_read/2;
509 if(write_pos >= mp3_block_size*8)
511 if(write_pos > mp3_block_size*8)
512 memcpy(&sound_buffer[0],&sound_buffer[mp3_block_size*8],write_pos - mp3_block_size*8);
513 write_pos %= (mp3_block_size*8);
518 SoundReader(IAviReadStream*astream)
520 this->astream = astream;
523 this->mp3_block_size = 2304;
524 this->sound_buffer = new short int[mp3_block_size*16];
532 if(read_pos<=write_pos)
533 return write_pos-read_pos;
535 return (write_pos+mp3_block_size*8)-read_pos;
537 short int* readFrame()
540 while(available()<mp3_block_size) {
544 read_pos += mp3_block_size;
545 read_pos %= mp3_block_size*8;
546 return &sound_buffer[tmp];
551 static int mp3_block_size = 2304;
553 static int do_video = 1;
554 static int do_audio = 1;
556 int main (int argc,char ** argv)
559 IAviReadFile* player;
560 IAviReadStream* astream;
561 IAviReadStream* vstream;
563 double samplesperframe;
571 processargs(argc, argv);
572 lastframe += firstframe;
576 memset(idtab, 0, sizeof(idtab));
578 player = CreateIAviReadFile(filename);
579 astream = player->GetStream(0, AviStream::Audio);
580 vstream = player->GetStream(0, AviStream::Video);
587 int dwMicroSecPerFrame = 0;
588 player->GetFileHeader(&head);
589 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
590 printf("frames: %d\n", head.dwTotalFrames);
591 printf("streams: %d\n", head.dwStreams);
592 printf("width: %d\n", head.dwWidth);
593 printf("height: %d\n", head.dwHeight);
594 printf("sound: %u samples (%f seconds)\n", astream->GetEndPos(),
595 astream->GetEndTime());
596 oldwidth = head.dwWidth;
597 oldheight = head.dwHeight;
598 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
599 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
600 samplerate = (int)(astream->GetEndPos()/astream->GetEndTime());
601 fps = 1000000.0/dwMicroSecPerFrame;
603 StreamInfo*audioinfo;
604 StreamInfo*videoinfo;
607 videoinfo = vstream->GetStreamInfo();
608 oldwidth = videoinfo->GetVideoWidth();
609 oldheight = videoinfo->GetVideoHeight();
610 fps = (double)(videoinfo->GetFps());
615 audioinfo = astream->GetStreamInfo();
616 samplerate = audioinfo->GetAudioSamplesPerSec();
617 samplesperframe = audioinfo->GetAudioSamplesPerSec()/videoinfo->GetFps();
621 width = (int)(oldwidth*scale);
622 height = (int)(oldheight*scale);
625 vstream -> StartStreaming();
628 astream -> StartStreaming();
629 printf("%f framerate\n", fps);
630 printf("%f samples/frame\n", samplesperframe);
631 printf("%d samplerate\n", samplerate);
635 file = open("__tmp__.swf", O_WRONLY|O_CREAT|O_TRUNC, 0644);
637 file = open(outputfilename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
639 memset(&swf, 0, sizeof(swf));
640 swf.frameRate = (int)(fps*256);
642 swf.fileSize = 0x0fffffff;
643 swf.frameCount = lastframe - firstframe;
650 filesize += swf_WriteHeader(file, &swf);
652 tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
653 swf_SetU8(tag,0); //black
656 filesize += swf_WriteTag(file, tag);
659 tag = swf_InsertTag(NULL, ST_SOUNDSTREAMHEAD2);
660 swf_SetSoundStreamHead(tag, (int)samplesperframe/4);
661 filesize += swf_WriteTag(file, tag);
667 double movie_sound_pos = 0;
668 int mp3_sound_pos = 0;
673 astream->GetAudioFormatInfo(&wave,0);
675 printf("nChannels:%d\n", wave.nChannels);
676 printf("nSamplesPerSec:%d\n", wave.nSamplesPerSec);
677 printf("nAvgBytesPerSec:%d\n", wave.nAvgBytesPerSec);
678 printf("nBlockAlign:%d\n", wave.nBlockAlign);
679 printf("wBitsPerSample:%d\n", wave.wBitsPerSample);
680 printf("cbSize:%d\n", wave.cbSize);
683 SoundReader* sound = new SoundReader(astream);
686 if(vstream->ReadFrame()<0) {
691 if(frame < firstframe)
693 movie_sound_pos += samplesperframe;
695 while(mp3_sound_pos<movie_sound_pos) {
696 short int* samples = sound->readFrame();
697 mp3_sound_pos += mp3_block_size;
699 printf("\rskipping frame %d",frame);
702 if(frame == firstframe)
707 printf("\rconvert frame %d",frame);
711 movie_sound_pos += samplesperframe;
715 while(mp3_sound_pos<movie_sound_pos) {
716 // rawplay -s 44100 -f s16_le -c 2 samples.test
717 short int* samples = sound->readFrame();
720 if(first) { //first run
721 tag = swf_InsertTag(NULL, ST_SOUNDSTREAMBLOCK);
722 swf_SetSoundStreamBlock(tag, samples, 0, 1);
724 swf_SetSoundStreamBlock(tag, samples, 0, 0);
727 mp3_sound_pos += mp3_block_size;
729 if(mp3_sound_pos>=movie_sound_pos) { // last run
730 filesize += swf_WriteTag(file, tag);
738 CImage*img = vstream->GetFrame();
740 U8*data = img->Data();
741 int bpp = img->Bpp();
750 /* some movies have changing dimensions */
751 if(img->Width() != oldwidth ||
752 img->Height() != oldheight) {
754 oldwidth = img->Width();
755 oldheight = img->Height();
756 width = (int)(oldwidth*scale);
757 height = (int)(oldheight*scale);
761 for(yy=0;yy<yblocks;yy++)
762 for(xx=0;xx<xblocks;xx++)
765 for(y=0;y<yblocksize;y++) {
766 /* some avifile versions flip the image some don't. Maybe this is
767 even movie dependent. We just let the user decide which side's up. */
770 mydata = img->At(oldheight-(int)((yy*yblocksize+y)*reziscale));
772 mydata = img->At((int)((yy*yblocksize+y)*reziscale));
774 for(x=0;x<xblocksize;x++) {
775 blockbuffer[(y*xblocksize+x)*3+2] = mydata[((int)(((xx*xblocksize+x)*reziscale)))*3+0];
776 blockbuffer[(y*xblocksize+x)*3+1] = mydata[((int)(((xx*xblocksize+x)*reziscale)))*3+1];
777 blockbuffer[(y*xblocksize+x)*3+0] = mydata[((int)(((xx*xblocksize+x)*reziscale)))*3+2];
781 b.data = blockbuffer;
782 b.len = xblocksize*yblocksize*3;
783 blocks[yy*xblocks+xx].compress(file, &b);
786 tag = swf_InsertTag(NULL, ST_SHOWFRAME);
787 filesize += swf_WriteTag(file, tag);
793 if(frame == lastframe)
798 destroydisplay(file);
800 tag = swf_InsertTag(NULL, ST_END);
801 filesize += swf_WriteTag(file, tag);
808 fi=fopen("tmp.swf", "r+");
810 fi=fopen(outputfilename, "r+");
814 fseek(fi,4,SEEK_SET);
816 f = filesize ;fwrite(&f,1,1,fi);
817 f = filesize >> 8 ;fwrite(&f,1,1,fi);
818 f = filesize >> 16;fwrite(&f,1,1,fi);
819 f = filesize >> 24;fwrite(&f,1,1,fi);
825 snprintf(buffer, 1024, "swfcombine -dz __tmp__.swf -o %s", outputfilename);
826 printf("%s\n", buffer);
828 sprintf(buffer, "rm __tmp__.swf");
829 printf("%s\n", buffer);