From 74e953401e1f5b89e3fa355413f0ec92744d78c6 Mon Sep 17 00:00:00 2001 From: kramm Date: Sun, 2 Dec 2001 15:00:55 +0000 Subject: [PATCH] initial revision. --- lib/example/avi2swf.cc | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 lib/example/avi2swf.cc diff --git a/lib/example/avi2swf.cc b/lib/example/avi2swf.cc new file mode 100644 index 0000000..11d812b --- /dev/null +++ b/lib/example/avi2swf.cc @@ -0,0 +1,231 @@ +/* avi2swf.cc + Convert avi movie files into swf. + As soon as there's an algorithm implemented for writing the + data directly to disk, this file should maybe go to ../src. + + Part of the swftools package. + + Copyright (c) 2001 Matthias Kramm + + This file is distributed under the GPL, see file COPYING for details */ + +#include +#include +extern "C" { +#include "../rfxswf.h" +#include "../args.h" +} +#include "avifile.h" +#include "aviplay.h" + +char * filename = 0; +char * outputfilename = "output.swf"; + +struct options_t options[] = +{ + {"v","verbose"}, + {"o","output"}, + {"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); + exit(0); + } else + if(!strcmp(name, "o")) { + outputfilename = val; + return 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-V , --version\t\t Print program version and exit\n"); + exit(0); +} +int args_callback_command(char*name,char*val) +{ + if(filename) { + fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n", + filename, name); + } + filename = name; + return 0; +} + +SWF swf; +TAG*tag; + +int main (int argc,char ** argv) +{ int f; + IAviReadFile* player; + IAviReadStream* astream; + IAviReadStream* vstream; + MainAVIHeader head; + SRECT r; + memset(&swf, 0, sizeof(swf)); + + processargs(argc, argv); + if(!filename) + exit(0); + + player = CreateIAviReadFile(filename); + player->GetFileHeader(&head); + printf("fps: %d\n", 1000000/head.dwMicroSecPerFrame); + printf("frames: %d\n", head.dwTotalFrames); + printf("streams: %d\n", head.dwStreams); + printf("streams: %d\n", player->StreamCount()); + printf("width: %d\n", head.dwWidth); + printf("height: %d\n", head.dwHeight); + + astream = player->GetStream(0, AviStream::Audio); + vstream = player->GetStream(0, AviStream::Video); + + vstream -> StartStreaming(); + + swf.frameRate = (int)(1000000.0/head.dwMicroSecPerFrame*256); + swf.fileVersion = 4; + r.xmin = 0; + r.ymin = 0; + r.xmax = head.dwWidth*20; + r.ymax = head.dwHeight*20; + swf.movieSize = r; + tag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR); + swf.firstTag = tag; + swf_SetU8(tag,0); //black + swf_SetU8(tag,0); + swf_SetU8(tag,0); + + U8*newdata = (U8*)malloc((head.dwWidth+3) * head.dwHeight * 4); + + int frame = 0; + + while(1) { + if(vstream->ReadFrame()<0) { + printf("\n"); + break; + } + printf("\rconvert frame %d",frame); + fflush(stdout); + CImage*img = vstream->GetFrame(); + img->ToRGB(); + U8* data = img->data(); + int width = img->width(); + int bpp = img->bpp(); + int width4 = width*4; + int height = img->height(); + int x,y; + int fs,ls; + SHAPE*s; + MATRIX m; + SRECT r; + RGBA rgb; + + if(frame!=0) { + tag = swf_InsertTag(tag, ST_REMOVEOBJECT2); + swf_SetU16(tag, 1); //depth + } + + /* todo: dynamically decide whether to generate jpeg/lossless + bitmaps, (using transparency to modify the previous + picture), and which jpeg compression depth to use. + (btw: Are there video frame transitions which can + reasonably approximated by shapes?) + */ + + int type = 1; + if(type == 0) { + tag = swf_InsertTag(tag, ST_DEFINEBITSLOSSLESS); + swf_SetU16(tag, frame*2); + for(y=0;yat(y); + for(x=0;xat(y); + for(x=0;x