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"
16 #include <avifile/version.h>
17 #if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION==6)
22 #include <StreamInfo.h>
36 37 bytes per shape (rectangle)
37 8-12 bytes per placeobject
38 4 bytes per removeobject2
39 696+ bytes per definejpeg2 (576 jpegtables)
40 576 bytes per jpegtables
41 122+ bytes per definejpeg
43 blocks*140 = minimal bytes per frames
44 5000/140 = maximal blocks with framerate 5000
49 int cache_size=38; //in frames
52 char * outputfilename = "output.swf";
53 unsigned int firstframe = 0;
54 unsigned int lastframe = 0x7fffffff;
56 int jpeg_quality = 20;
58 #ifndef ST_DEFINEBITSJPEG
59 #define ST_DEFINEBITSJPEG 6
62 struct options_t options[] =
72 int args_callback_option(char*name,char*val)
74 if(!strcmp(name, "V")) {
75 printf("avi2swf - part of %s %s\n", PACKAGE, VERSION);
78 else if(!strcmp(name, "o")) {
82 else if(!strcmp(name, "n")) {
83 lastframe = atoi(val);
86 else if(!strcmp(name, "s")) {
87 firstframe = atoi(val);
91 int args_callback_longoption(char*name,char*val)
93 return args_long2shortoption(options, name, val);
95 void args_callback_usage(char*name)
97 printf("\nUsage: %s file.swf\n", name);
98 printf("\t-h , --help\t\t Print help and exit\n");
99 printf("\t-o , --output=filename\t Specify output filename\n");
100 printf("\t-n , --num=frames\t\t Number of frames to encode\n");
101 printf("\t-s , --start=frame\t\t First frame to encode\n");
102 printf("\t-V , --version\t\t Print program version and exit\n");
105 int args_callback_command(char*name,char*val)
108 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
115 /* id allocation/deallocation */
117 unsigned int idtab_pos=1;
120 while(idtab[idtab_pos] || !idtab_pos)
130 void makeshape(int file, int id, int gfxid, int width, int height)
139 tag = swf_InsertTag(NULL, ST_DEFINESHAPE);
141 rgb.b = rgb.g = rgb.r = 0xff;
143 ls = swf_ShapeAddLineStyle(s,20,&rgb);
144 swf_GetMatrix(NULL,&m);
148 fs = swf_ShapeAddBitmapFillStyle(s,&m,gfxid,0);
149 swf_SetU16(tag,id); // ID
156 swf_SetShapeStyles(tag,s);
157 swf_ShapeCountBits(s,NULL,NULL);
158 swf_SetShapeBits(tag,s);
160 swf_ShapeSetAll(tag,s,0,0,lines?ls:0,fs,0);
162 swf_ShapeSetLine(tag,s,width*20,0);
163 swf_ShapeSetLine(tag,s,0,height*20);
164 swf_ShapeSetLine(tag,s,-width*20,0);
165 swf_ShapeSetLine(tag,s,0,-height*20);
166 swf_ShapeSetEnd(tag);
167 swf_WriteTag(file, tag);
172 void setshape(int file,int id,int depth,int x,int y,CXFORM*cx)
176 m.sx = 0x10000; m.sy = 0x10000;
180 if(cx && !((cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256)
181 ||(cx->a1|cx->r1|cx->g1|cx->b1))) cx = 0;
182 tag = swf_InsertTag(NULL,ST_PLACEOBJECT2);
183 swf_ObjectPlace(tag,id,depth,&m,cx,0);
184 swf_WriteTag(file, tag);
192 // static int xblocksize;
193 // static int yblocksize;
206 class GfxBlockCache {
209 char*expire; //0=block's free
218 GfxBlockCache(int file)
221 size = xblocks*yblocks*cache_size;
222 printf("initializing cache (%d entries)\n", size);
223 list = new GfxBlock[size];
224 expire = new char[size];
226 memset(expire,0,size);
227 memset(list,0,sizeof(GfxBlock)*size);
228 memset(ids,0,sizeof(int)*size);
233 void insert(GfxBlock*block, int gfxid)
243 // cache full- don't insert item
247 free(list[pos].data);
249 //TODO: free this in the SWF, also
251 list[pos].data=(U8*)malloc(block->len);
252 memcpy(list[pos].data,block->data,block->len);
253 list[pos].len = block->len;
254 expire[pos] = cache_size;
257 int find(GfxBlock*block, CXFORM*cxform)
259 //TODO: do least square regression here to derive cxform
267 int t = (block->len);
268 U8*ptr1 = block->data;
269 U8*ptr2 = list[s].data;
271 // notice: we treat r,g,b as equal here.
273 int a = (*ptr1++)-(*ptr2++);
276 if(bestsum < 0 || bestsum > sum2) {
285 best = bestsum/block->len;
291 expire[bestid]= cache_size;
314 printf("destroying cache...\n");
315 printf("hits:%d (%02d%%)\n", hits, hits*100/(hits+misses));
316 printf("misses:%d (%02d%%)\n", misses, misses*100/(hits+misses));
318 if(expire[t] && list[t].data)
326 class GfxBlockEncoder {
334 void init(int depth, int posx,int posy, int sizex, int sizey)
336 this->basedepth = depth;
341 this->depth[0] = this->depth[1] = this->depth[2] = -1;
345 /* clear everything in the block */
351 tag = swf_InsertTag(NULL, ST_REMOVEOBJECT2);
352 swf_SetU16(tag, basedepth+t); //depth
353 swf_WriteTag(file, tag);
358 void writeiframe(int file, GfxBlock*block)
362 int gfxid = get_free_id();
363 int shapeid = get_free_id();
365 //memset(data,0,sizex*sizey*3);
366 TAG*tag = swf_InsertTag(NULL, ST_DEFINEBITS);
367 JPEGBITS * jb = swf_SetJPEGBitsStart(tag,sizex,sizey,jpeg_quality);
368 tag->len = 0; //bad hack
369 swf_SetU16(tag, gfxid);
372 swf_SetJPEGBitsLine(jb,&block->data[y*sizex*3]);
373 swf_SetJPEGBitsFinish(jb);
374 swf_WriteTag(file, tag);
377 cache->insert(block, shapeid);
379 makeshape(file, shapeid, gfxid, sizex, sizey);
380 setshape(file, shapeid, basedepth+1, posx, posy, 0);
383 void writereference(int file, int shapeid, CXFORM*form)
385 if(depth[1]!=shapeid)
388 setshape(file, shapeid, basedepth+1, posx, posy, form);
392 void compress(int file, GfxBlock*block)
395 int id = cache->find(block, &form);
397 writeiframe(file, block);
399 writereference(file, id, &form);
404 void initdisplay(int file)
410 for(t=0;t<xblocks;t++)
411 blocks[t].clear(file);
416 xblocksize = (width/3)&~7;
417 yblocksize = (height/2)&~7;
418 xblocks = width/xblocksize;
419 yblocks = height/yblocksize;
420 printf("%dx%d blocks of size %dx%d\n", xblocks,yblocks, xblocksize, yblocksize);
421 printf("cutting lower %d lines, right %d columns\n",
422 height-yblocks*yblocksize, width-xblocks*xblocksize);
423 blocks = new GfxBlockEncoder[xblocks*yblocks];
424 blockbuffer = new U8[xblocksize*yblocksize*4]; //should be 3
425 cache = new GfxBlockCache(file);
427 for(t=0;t<xblocks*yblocks;t++) {
429 (t%xblocks)*xblocksize,
430 (t/xblocks)*yblocksize,
431 xblocksize, yblocksize);
434 TAG*tag = swf_InsertTag(NULL, ST_JPEGTABLES);
435 JPEGBITS * jpeg = swf_SetJPEGBitsStart(tag, xblocksize, yblocksize, jpeg_quality);
436 swf_WriteTag(file, tag);
441 void destroydisplay(int file)
451 int main (int argc,char ** argv)
454 IAviReadFile* player;
455 IAviReadStream* astream;
456 IAviReadStream* vstream;
458 double samplesperframe;
463 processargs(argc, argv);
464 lastframe += firstframe;
468 memset(idtab, 0, sizeof(idtab));
470 player = CreateIAviReadFile(filename);
471 astream = player->GetStream(0, AviStream::Audio);
472 vstream = player->GetStream(0, AviStream::Video);
475 int dwMicroSecPerFrame = 0;
476 player->GetFileHeader(&head);
477 printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame);
478 printf("frames: %d\n", head.dwTotalFrames);
479 printf("streams: %d\n", head.dwStreams);
480 printf("width: %d\n", head.dwWidth);
481 printf("height: %d\n", head.dwHeight);
482 printf("sound: %u samples (%f seconds)\n", astream->GetEndPos(),
483 astream->GetEndTime());
484 width = head.dwWidth;
485 height = head.dwHeight;
486 dwMicroSecPerFrame = head.dwMicroSecPerFrame;
487 samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000;
488 samplerate = (int)(astream->GetEndPos()/astream->GetEndTime());
489 fps = 1000000.0/dwMicroSecPerFrame;
491 StreamInfo*audioinfo;
492 StreamInfo*videoinfo;
493 audioinfo = astream->GetStreamInfo();
494 videoinfo = vstream->GetStreamInfo();
495 width = videoinfo->GetVideoWidth();
496 height = videoinfo->GetVideoHeight();
497 samplerate = audioinfo->GetAudioSamplesPerSec();
498 samplesperframe = audioinfo->GetAudioSamplesPerSec()/videoinfo->GetFps();
499 fps = (double)(videoinfo->GetFps());
504 vstream -> StartStreaming();
505 astream -> StartStreaming();
507 printf("%f samples/frame\n", samplesperframe);
508 printf("%d samplerate\n", samplerate);
509 samplefix = 44100/samplerate;
512 printf("samplerate too high!\n");
515 printf("%d mp3 samples per movie sample\n", samplefix);
517 file = open(outputfilename,O_WRONLY|O_CREAT|O_TRUNC, 0644);
519 memset(&swf, 0, sizeof(swf));
520 swf.frameRate = (int)(fps*256);
522 swf.fileSize = 476549;//0x0fffffff;
523 swf.frameCount = lastframe - firstframe;
530 swf_WriteHeader(file, &swf);
532 tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
533 swf_SetU8(tag,0); //black
536 swf_WriteTag(file, tag);
539 tag = swf_InsertTag(NULL, ST_SOUNDSTREAMHEAD2);
540 swf_SetSoundStreamHead(tag, 1152);
541 swf_WriteTag(file, tag);
547 int mp3_block_size = 1152;
549 int bufsize = mp3_block_size;
550 if(mp3_block_size < (int)(samplesperframe+1))
551 bufsize = (int)(samplesperframe + 1);
552 unsigned char*buffer = (unsigned char*)malloc(bufsize);
553 short*block = (short*)malloc(bufsize*2*samplefix);
555 unsigned samples_read, bytes_read;
557 double movie_sound_pos = 0;
558 int mp3_sound_pos = 0;
561 astream->GetAudioFormatInfo(&wave,0);
563 printf("nChannels:%d\n", wave.nChannels);
564 printf("nSamplesPerSec:%d\n", wave.nChannels);
565 printf("nAvgBytesPerSec:%d\n", wave.nAvgBytesPerSec);
566 printf("nBlockAlign:%d\n", wave.nBlockAlign);
567 printf("wBitsPerSample:%d\n", wave.wBitsPerSample);
568 printf("cbSize:%d\n", wave.cbSize);
571 if(vstream->ReadFrame()<0) {
576 if(frame < firstframe)
578 if(astream->ReadFrames(buffer, bufsize,
579 (int)samplesperframe,
580 samples_read, bytes_read)<0) {
584 printf("\rskipping frame %d",frame);
587 if(frame == firstframe)
592 printf("\rconvert frame %d",frame);
596 movie_sound_pos += samplesperframe;
599 while(mp3_sound_pos<movie_sound_pos) {
600 if(astream->ReadFrames(buffer, bufsize,
601 mp3_block_size/samplefix,
602 samples_read, bytes_read)<0) {
603 printf("couldn't read %d samples\n", mp3_block_size);
609 for(s=0;s<mp3_block_size;s++) {
610 block[s] = ((int)buffer[t]-128)*256;
617 if(first) { //first run
618 tag = swf_InsertTag(NULL, ST_SOUNDSTREAMBLOCK);
619 swf_SetSoundStreamBlock(tag, block, mp3_block_size,1);
621 swf_SetSoundStreamBlock(tag, block, mp3_block_size,0);
624 mp3_sound_pos += mp3_block_size/samplefix;
626 if(mp3_sound_pos>=movie_sound_pos) { // last run
627 swf_WriteTag(file, tag);
635 CImage*img = vstream->GetFrame();
637 U8*data = img->Data();
638 int bpp = img->Bpp();
647 /* some movies have changing dimensions */
648 if(img->Width() != width ||
649 img->Height() != height) {
651 width = img->Width();
652 height = img->Height();
656 for(yy=0;yy<yblocks;yy++)
657 for(xx=0;xx<xblocks;xx++)
660 for(y=0;y<yblocksize;y++) {
661 U8*mydata = img->At(yy*yblocksize+y);
662 for(x=0;x<xblocksize;x++) {
663 blockbuffer[(y*xblocksize+x)*3+2] = mydata[(xx*xblocksize+x)*3+0];
664 blockbuffer[(y*xblocksize+x)*3+1] = mydata[(xx*xblocksize+x)*3+1];
665 blockbuffer[(y*xblocksize+x)*3+0] = mydata[(xx*xblocksize+x)*3+2];
669 b.data = blockbuffer;
670 b.len = xblocksize*yblocksize*3;
671 blocks[yy*xblocks+xx].compress(file, &b);
674 tag = swf_InsertTag(NULL, ST_SHOWFRAME);
675 swf_WriteTag(file, tag);
681 if(frame == lastframe)
685 destroydisplay(file);
687 tag = swf_InsertTag(NULL, ST_END);
688 swf_WriteTag(file, tag);