X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=avi2swf%2Favi2swf.cc;h=c68fedbbca3e0566260f60ac8f8d04022df749f2;hp=4d3d06da97802ed6c610051cd6b71974b39ef810;hb=f29d8006b1d8253c50808d6eb4941bdeb808c601;hpb=e2ac6fca142e8f7f81b971bd873d1c7c783f1142 diff --git a/avi2swf/avi2swf.cc b/avi2swf/avi2swf.cc index 4d3d06d..c68fedb 100644 --- a/avi2swf/avi2swf.cc +++ b/avi2swf/avi2swf.cc @@ -3,76 +3,76 @@ Part of the swftools package. - Copyright (c) 2001 Matthias Kramm + Copyright (c) 2001,2002,2003 Matthias Kramm + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - This file is distributed under the GPL, see file COPYING for details */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include +#include +#include #include -extern "C" { -#include "../lib/rfxswf.h" -#include "../lib/args.h" -} -#include -#if (AVIFILE_MAJOR_VERSION == 0) && (AVIFILE_MINOR_VERSION==6) - #include - #include - #include - #include - #include - #define VERSION6 -#else - #include - #include - #include - #define Width width - #define Height height - #define Data data - #define Bpp bpp -#endif - -/* -statistics: (for now) - 37 bytes per shape (rectangle) - 8-12 bytes per placeobject - 4 bytes per removeobject2 - 696+ bytes per definejpeg2 (576 jpegtables) - 576 bytes per jpegtables - 122+ bytes per definejpeg - - blocks*140 = minimal bytes per frames - 5000/140 = maximal blocks with framerate 5000 - - 2 bytes per showframe -*/ - -int cache_size=38; //in frames -char * filename = 0; -char * outputfilename = "output.swf"; -unsigned int firstframe = 0; -unsigned int lastframe = 0x7fffffff; +#include "../config.h" -int jpeg_quality = 20; - -#ifndef ST_DEFINEBITSJPEG -#define ST_DEFINEBITSJPEG 6 +#include "../lib/args.h" +#include "v2swf.h" +#ifdef WIN32 +#include "videoreader_vfw.hh" +#else +#include "videoreader_avifile.hh" #endif -struct options_t options[] = -{ - {"v","verbose"}, - {"o","output"}, - {"n","num"}, - {"s","start"}, - {"V","version"}, - {0,0} +static char * filename = 0; +static char * outputfilename = "output.swf"; +int verbose = 0; + +static int quality = 80; +static double scale = 1.0; +static int flip = 0; +static int expensive = 0; +static int flashversion = 6; +static int keyframe_interval = -1; +static int skip = 0; +static float audio_adjust = 0; +static int mp3_bitrate = 32; +static int samplerate = 11025; +static int numframes = 0; +static char* skipframes = 0; + +static struct options_t options[] = { +{"h", "help"}, +{"o", "output"}, +{"A", "adjust"}, +{"n", "num"}, +{"m", "mp3-bitrate"}, +{"r", "mp3-samplerate"}, +{"s", "scale"}, +{"S", "skipframes"}, +{"p", "flip"}, +{"q", "quality"}, +{"k", "keyframe"}, +{"x", "extragood"}, +{"T", "flashversion"}, +{"V", "version"}, +{0,0} }; int args_callback_option(char*name,char*val) { if(!strcmp(name, "V")) { - printf("avi2swf - part of %s %s\n", PACKAGE, VERSION); + printf("avi2swf-ng - part of %s %s\n", PACKAGE, VERSION); exit(0); } else if(!strcmp(name, "o")) { @@ -80,27 +80,107 @@ int args_callback_option(char*name,char*val) return 1; } else if(!strcmp(name, "n")) { - lastframe = atoi(val); + numframes = atoi(val); + return 1; + } + else if(!strcmp(name, "d")) { + scale = atof(val); + return 1; + } + else if(!strcmp(name, "q")) { + quality = atoi(val); + if(quality<0) + quality = 0; + if(quality>100) + quality = 100; + return 1; + } + else if(!strcmp(name, "p")) { + flip = 1; + return 0; + } + else if(!strcmp(name, "k")) { + keyframe_interval = atoi(val); + return 1; + } + else if(!strcmp(name, "A")) { + audio_adjust = atof(val); + return 1; + } + else if(!strcmp(name, "v")) { + verbose = 1; + return 0; + } + else if(!strcmp(name, "T")) { + flashversion = atoi(val); + return 1; + } + else if(!strcmp(name, "x")) { + expensive = 1; + return 0; + } + else if(!strcmp(name, "m")) { + mp3_bitrate = atoi(val); + return 1; + } + else if(!strcmp(name, "r")) { + samplerate = atoi(val); + if(samplerate >= 11000 && samplerate <= 12000) + samplerate = 11025; + else if(samplerate >= 22000 && samplerate <= 23000) + samplerate = 22050; + else if(samplerate >= 44000 && samplerate <= 45000) + samplerate = 44100; + else { + fprintf(stderr, "Invalid samplerate: %d\n", samplerate); + fprintf(stderr, "Allowed values: 11025, 22050, 44100\n", samplerate); + exit(1); + } + return 1; + } + else if(!strcmp(name, "S")) { + skip = atoi(val); + return 1; + } + else if(!strcmp(name, "C")) { + skipframes = strdup(val); return 1; } else if(!strcmp(name, "s")) { - firstframe = atoi(val); + scale = atoi(val)/100.0; + if(scale>1.0 || scale<=0) { + fprintf(stderr, "Scale must be in the range 1-100!\n"); + exit(1); + } return 1; } + fprintf(stderr, "Unknown option: -%s\n", name); + exit(1); } int args_callback_longoption(char*name,char*val) { return args_long2shortoption(options, name, val); } -void args_callback_usage(char*name) -{ - printf("\nUsage: %s file.swf\n", name); - printf("\t-h , --help\t\t Print help and exit\n"); - printf("\t-o , --output=filename\t Specify output filename\n"); - printf("\t-n , --num=frames\t\t Number of frames to encode\n"); - printf("\t-s , --start=frame\t\t First frame to encode\n"); - printf("\t-V , --version\t\t Print program version and exit\n"); - exit(0); +void args_callback_usage(char *name) +{ + printf("\n"); + printf("Usage: %s file.avi [-o output.swf]\n", name); + printf("\n"); + printf("-h , --help Print help and exit\n"); + printf("-o , --output filename Specify output filename\n"); + printf("-A , --adjust seconds Audio adjust: Shift sound -seconds to the future or +seconds into the past.\n"); + printf("-n , --num frames Number of frames to encode\n"); + printf("-m , --mp3-bitrate Set the mp3 bitrate to encode audio with\n"); + printf("-r , --mp3-samplerate Set the mp3 samplerate to encode audio with (default: 11025)\n"); + printf("-s , --scale Scale down to factor . (in %, e.g. 100 = original size)\n"); + printf("-S , --skipframes Skip frames before starting the conversion.\n"); + printf("-p , --flip Turn movie upside down\n"); + printf("-q , --quality Set the quality to . (0-100, 0=worst, 100=best, default:80)\n"); + printf("-k , --keyframe Set the number of intermediate frames between keyframes.\n"); + printf("-x , --extragood Enable some *very* expensive compression strategies.\n"); + printf("-T , --flashversion Set output flash version to .\n"); + printf("-V , --version Print program version and exit\n"); + printf("\n"); } int args_callback_command(char*name,char*val) { @@ -112,584 +192,156 @@ int args_callback_command(char*name,char*val) return 0; } -/* id allocation/deallocation */ -char idtab[65536]; -unsigned int idtab_pos=1; -int get_free_id() -{ - while(idtab[idtab_pos] || !idtab_pos) - idtab_pos++; - idtab[idtab_pos]=1; - return idtab_pos; -} -void free_id(int id) +static char toabuf[128]; +static char*ftoa(double a) { - idtab[id] = 0; + sprintf(toabuf, "%f", a); + return toabuf; } - -void makeshape(int file, int id, int gfxid, int width, int height) +static char*itoa(int a) { - TAG*tag; - RGBA rgb; - MATRIX m; - SHAPE*s; - SRECT r; - int lines = 0; - int ls,fs; - tag = swf_InsertTag(NULL, ST_DEFINESHAPE); - swf_ShapeNew(&s); - rgb.b = rgb.g = rgb.r = 0xff; - if(lines) - ls = swf_ShapeAddLineStyle(s,20,&rgb); - swf_GetMatrix(NULL,&m); - m.sx = 20*65536; - m.sy = 20*65536; - - fs = swf_ShapeAddBitmapFillStyle(s,&m,gfxid,0); - swf_SetU16(tag,id); // ID - r.xmin = 0; - r.ymin = 0; - r.xmax = width*20; - r.ymax = height*20; - swf_SetRect(tag,&r); - - swf_SetShapeStyles(tag,s); - swf_ShapeCountBits(s,NULL,NULL); - swf_SetShapeBits(tag,s); - - swf_ShapeSetAll(tag,s,0,0,lines?ls:0,fs,0); - - swf_ShapeSetLine(tag,s,width*20,0); - swf_ShapeSetLine(tag,s,0,height*20); - swf_ShapeSetLine(tag,s,-width*20,0); - swf_ShapeSetLine(tag,s,0,-height*20); - swf_ShapeSetEnd(tag); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - swf_ShapeFree(s); + sprintf(toabuf, "%d", a); + return toabuf; } -void setshape(int file,int id,int depth,int x,int y,CXFORM*cx) +#ifdef DO_SIGNALS +pthread_t main_thread; +static void sigterm(int sig) { - TAG*tag; - MATRIX m; - m.sx = 0x10000; m.sy = 0x10000; - m.r0 = 0; m.r1 = 0; - m.tx = x*20; - m.ty = y*20; - if(cx && !((cx->a0!=256)||(cx->r0!=256)||(cx->g0!=256)||(cx->b0!=256) - ||(cx->a1|cx->r1|cx->g1|cx->b1))) cx = 0; - tag = swf_InsertTag(NULL,ST_PLACEOBJECT2); - swf_ObjectPlace(tag,id,depth,&m,cx,0); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); -} - - -int xblocksize; -int yblocksize; -struct GfxBlock { -// static int xblocksize; -// static int yblocksize; - U8*data; - int len; -}; - -int width=0; -int height=0; - -int xblocks; -int yblocks; - -U8* blockbuffer = 0; - -class GfxBlockCache { - - GfxBlock*list; - char*expire; //0=block's free - int*ids; - int size; - int pos; - int hits; - int misses; - - public: - - GfxBlockCache(int file) - { - list=0; - size = xblocks*yblocks*cache_size; - printf("initializing cache (%d entries)\n", size); - list = new GfxBlock[size]; - expire = new char[size]; - ids = new int[size]; - memset(expire,0,size); - memset(list,0,sizeof(GfxBlock)*size); - memset(ids,0,sizeof(int)*size); - pos = 0; - hits =0; - misses =0; - } - void insert(GfxBlock*block, int gfxid) - { - int oldpos = pos; - while(++pos!=oldpos) - { - if(pos==size) pos=0; - if(!expire[pos]) - break; - } - if(pos==oldpos) { - // cache full- don't insert item - return; - } - if(list[pos].data) { - free(list[pos].data); - list[pos].data = 0; - //TODO: free this in the SWF, also - } - list[pos].data=(U8*)malloc(block->len); - memcpy(list[pos].data,block->data,block->len); - list[pos].len = block->len; - expire[pos] = cache_size; - ids[pos] = gfxid; - } - int find(GfxBlock*block, CXFORM*cxform) - { - //TODO: do least square regression here to derive cxform - int s; - int bestsum=-1; - int bestid; - float best; - for(s=0;slen); - U8*ptr1 = block->data; - U8*ptr2 = list[s].data; - int sum2 = 0; - // notice: we treat r,g,b as equal here. - do { - int a = (*ptr1++)-(*ptr2++); - sum2 += a*a; - } while(--t); - if(bestsum < 0 || bestsum > sum2) { - bestid = s; - bestsum = sum2; - } - } - if(bestsum<0) { - misses++; - return -1; - } - best = bestsum/block->len; - - if(best > 96.0) { - misses++; - return -1; - } - expire[bestid]= cache_size; - hits++; - cxform->a0 = 256; - cxform->r0 = 256; - cxform->g0 = 256; - cxform->b0 = 256; - cxform->a1 = 0; - cxform->r1 = 0; - cxform->g1 = 0; - cxform->b1 = 0; - return ids[bestid]; - } - void newframe() - { - int t; - for(t=0;tbasedepth = depth; - this->posx = posx; - this->posy = posy; - this->sizex = sizex; - this->sizey = sizey; - this->depth[0] = this->depth[1] = this->depth[2] = -1; - } - void clear(int file) - { - /* clear everything in the block */ - int t; - for(t=0;t<3;t++) - if(depth[t]>=0) - { - TAG*tag; - tag = swf_InsertTag(NULL, ST_REMOVEOBJECT2); - swf_SetU16(tag, basedepth+t); //depth - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - depth[t] = -1; - } - } - void writeiframe(int file, GfxBlock*block) + if(pthread_equal (pthread_self(), main_thread)) { - clear(file); - - int gfxid = get_free_id(); - int shapeid = get_free_id(); - - //memset(data,0,sizex*sizey*3); - TAG*tag = swf_InsertTag(NULL, ST_DEFINEBITS); - JPEGBITS * jb = swf_SetJPEGBitsStart(tag,sizex,sizey,jpeg_quality); - tag->len = 0; //bad hack - swf_SetU16(tag, gfxid); - int y; - for(y=0;ydata[y*sizex*3]); - swf_SetJPEGBitsFinish(jb); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - - cache->insert(block, shapeid); - - makeshape(file, shapeid, gfxid, sizex, sizey); - setshape(file, shapeid, basedepth+1, posx, posy, 0); - depth[1] = shapeid; - } - void writereference(int file, int shapeid, CXFORM*form) - { - if(depth[1]!=shapeid) - { - clear(file); - setshape(file, shapeid, basedepth+1, posx, posy, form); - depth[1] = shapeid; - } - } - void compress(int file, GfxBlock*block) - { - CXFORM form; - int id = cache->find(block, &form); - if(id<0) - writeiframe(file, block); - else { - writereference(file, id, &form); + if(frameno>0 && !shutdown_avi2swf) { + if(verbose) + printf("Thread [%08x] got sigterm %d\n", pthread_self(), sig); + shutdown_avi2swf++; + } else { + exit(1); } } -} *blocks = 0; - -void initdisplay(int file) -{ - if(blockbuffer) - free(blockbuffer); - if(blocks) { - int t; - for(t=0;tGetStream(0, AviStream::Audio); - vstream = player->GetStream(0, AviStream::Video); -#ifndef VERSION6 - MainAVIHeader head; - int dwMicroSecPerFrame = 0; - player->GetFileHeader(&head); - printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame); - printf("frames: %d\n", head.dwTotalFrames); - printf("streams: %d\n", head.dwStreams); - printf("width: %d\n", head.dwWidth); - printf("height: %d\n", head.dwHeight); - printf("sound: %u samples (%f seconds)\n", astream->GetEndPos(), - astream->GetEndTime()); - width = head.dwWidth; - height = head.dwHeight; - dwMicroSecPerFrame = head.dwMicroSecPerFrame; - samplesperframe = astream->GetEndPos()/astream->GetEndTime()*head.dwMicroSecPerFrame/1000000; - samplerate = (int)(astream->GetEndPos()/astream->GetEndTime()); - fps = 1000000.0/dwMicroSecPerFrame; + processargs(argc, argv); + if(!filename) { + fprintf(stderr, "You must supply a filename"); + exit(0); + } + if(keyframe_interval<0) { + if(flashversion>=6) + keyframe_interval=20; + else + keyframe_interval=5; + } + + fi = fopen(outputfilename, "wb"); + if(!fi) { + fflush(stdout); fflush(stderr); + fprintf(stderr, "Couldn't open %s\n", outputfilename); + exit(1); + } + +#ifdef WIN32 + ret = videoreader_vfw_open(&video, filename); #else - StreamInfo*audioinfo; - StreamInfo*videoinfo; - audioinfo = astream->GetStreamInfo(); - videoinfo = vstream->GetStreamInfo(); - width = videoinfo->GetVideoWidth(); - height = videoinfo->GetVideoHeight(); - samplerate = audioinfo->GetAudioSamplesPerSec(); - samplesperframe = audioinfo->GetAudioSamplesPerSec()/videoinfo->GetFps(); - fps = (double)(videoinfo->GetFps()); - delete(audioinfo); - delete(videoinfo); + ret = videoreader_avifile_open(&video, filename); #endif - vstream -> StartStreaming(); - astream -> StartStreaming(); - - printf("%f samples/frame\n", samplesperframe); - printf("%d samplerate\n", samplerate); - samplefix = 44100/samplerate; - - if(!samplefix) { - printf("samplerate too high!\n"); - return 0; - } - printf("%d mp3 samples per movie sample\n", samplefix); - - file = open(outputfilename,O_WRONLY|O_CREAT|O_TRUNC, 0644); - - memset(&swf, 0, sizeof(swf)); - swf.frameRate = (int)(fps*256); - swf.fileVersion = 4; - swf.fileSize = 476549;//0x0fffffff; - swf.frameCount = lastframe - firstframe; - r.xmin = 0; - r.ymin = 0; - r.xmax = width*20; - r.ymax = height*20; - swf.movieSize = r; - - swf_WriteHeader(file, &swf); - - tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR); - swf_SetU8(tag,0); //black - swf_SetU8(tag,0); - swf_SetU8(tag,0); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - - tag = swf_InsertTag(NULL, ST_SOUNDSTREAMHEAD2); - swf_SetSoundStreamHead(tag, 1152); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - - int frame = 0; - initdisplay(file); - - int mp3_block_size = 1152; - - int bufsize = mp3_block_size; - if(mp3_block_size < (int)(samplesperframe+1)) - bufsize = (int)(samplesperframe + 1); - unsigned char*buffer = (unsigned char*)malloc(bufsize); - short*block = (short*)malloc(bufsize*2*samplefix); - - unsigned samples_read, bytes_read; - - double movie_sound_pos = 0; - int mp3_sound_pos = 0; - - WAVEFORMATEX wave; - astream->GetAudioFormatInfo(&wave,0); - - printf("nChannels:%d\n", wave.nChannels); - printf("nSamplesPerSec:%d\n", wave.nChannels); - printf("nAvgBytesPerSec:%d\n", wave.nAvgBytesPerSec); - printf("nBlockAlign:%d\n", wave.nBlockAlign); - printf("wBitsPerSample:%d\n", wave.wBitsPerSample); - printf("cbSize:%d\n", wave.cbSize); - - while(1) { - if(vstream->ReadFrame()<0) { - printf("\n"); - break; + if(ret<0) { + fprintf(stderr, "Error opening %s\n", filename); + exit(1); } - if(frame < firstframe) - { - if(astream->ReadFrames(buffer, bufsize, - (int)samplesperframe, - samples_read, bytes_read)<0) { - printf("\n"); - break; - }; - printf("\rskipping frame %d",frame); - fflush(stdout); - frame++; - if(frame == firstframe) - printf("\n"); - continue; + if(verbose) { + printf("| video framerate: %f\n", video.fps); + printf("| video size: %dx%d\n", video.width, video.height); + printf("| audio rate: %d\n", video.samplerate); + printf("| audio channels: %d\n", video.channels); } - printf("\rconvert frame %d",frame); - fflush(stdout); - - // audio - movie_sound_pos += samplesperframe; - - int first=1; - while(mp3_sound_posReadFrames(buffer, bufsize, - mp3_block_size/samplefix, - samples_read, bytes_read)<0) { - printf("couldn't read %d samples\n", mp3_block_size); - break; - }; - int t=0; - int s; - int c=0; - for(s=0;s=movie_sound_pos) { // last run - swf_WriteTag(file, tag); - swf_DeleteTag(tag); + if(audio_adjust>0) { + int num = ((int)(audio_adjust*video.samplerate))*video.channels*2; + void*buf = malloc(num); + video.getsamples(&video, buf, num); + free(buf); + } else if(audio_adjust<0) { + int num = (int)(-audio_adjust*video.fps); + void*buf = malloc(video.width*video.height*4); + int t; + for(t=0;tGetFrame(); - img->ToRGB(); - U8*data = img->Data(); - int bpp = img->Bpp(); - int x,y; - int xx,yy; - int fs,ls; - SHAPE*s; - MATRIX m; - SRECT r; - RGBA rgb; - /* some movies have changing dimensions */ - if(img->Width() != width || - img->Height() != height) { - printf("\n"); - width = img->Width(); - height = img->Height(); - initdisplay(file); - } - - for(yy=0;yyAt(yy*yblocksize+y); - for(x=0;xnewframe(); - - frame++; - if(frame == lastframe) - break; - } - printf("\n"); - destroydisplay(file); - - tag = swf_InsertTag(NULL, ST_END); - swf_WriteTag(file, tag); - swf_DeleteTag(tag); - - close(file); - - return 0; + char buffer[4096]; + while(1) { + int l=v2swf_read(&v2swf, buffer, 4096); + fwrite(buffer, l, 1, fi); + if(!l) + break; + if(!verbose) { + printf("\rConverting frame %d", video.frame);fflush(stdout); + } + } + if(!verbose) + printf("\n"); + fclose(fi); + v2swf_backpatch(&v2swf, outputfilename); + v2swf_close(&v2swf); }