author | zecke <zecke> | 2004-09-10 11:47:55 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-09-10 11:47:55 (UTC) |
commit | 501c17ed2bb97f2062cb11daddeb698a6a9f2828 (patch) (side-by-side diff) | |
tree | 84e5659af9e1b0a0b4a99badae77124e02087344 | |
parent | e2fa8fdfff6bb0460350d5f1017ead99deea7a0b (diff) | |
download | opie-501c17ed2bb97f2062cb11daddeb698a6a9f2828.zip opie-501c17ed2bb97f2062cb11daddeb698a6a9f2828.tar.gz opie-501c17ed2bb97f2062cb11daddeb698a6a9f2828.tar.bz2 |
Fix various warning
-rw-r--r-- | noncore/multimedia/camera/cmd/capture.cpp | 3 | ||||
-rw-r--r-- | noncore/multimedia/camera/gui/mainwindow.cpp | 2 | ||||
-rw-r--r-- | noncore/multimedia/camera/lib/avi.c | 1 |
3 files changed, 5 insertions, 1 deletions
diff --git a/noncore/multimedia/camera/cmd/capture.cpp b/noncore/multimedia/camera/cmd/capture.cpp index 688622b..6b8c63c 100644 --- a/noncore/multimedia/camera/cmd/capture.cpp +++ b/noncore/multimedia/camera/cmd/capture.cpp @@ -1,192 +1,195 @@ /********************************************************************** ** Copyright (C) 2003 Michael 'Mickey' Lauer. All rights reserved. ** ** This file is part of Opie Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** **********************************************************************/ #include "capture.h" #include "zcameraio.h" #include "imageio.h" #include "avi.h" #include <opie2/oapplication.h> #include <opie2/odebug.h> #include <qimage.h> using namespace Opie::Core; using namespace Opie::Core; Capturer::Capturer() :QFrame( 0 ), height( 320 ), width( 240 ), zoom( 1 ), quality( 90 ), flip( "A" ), format( "JPEG" ), name( "Untitled" ) { } Capturer::~Capturer() { } void Capturer::checkSettings() { if ( width > height ) { if ( 0 != width % 16 || width < 16 || width > 640 ) { printf( "Warning: Corrected X resolution to 320 px\n" ); width = 320; } if ( 0 != height % 16 || height < 16 || height > 480 ) { printf( "Warning: Corrected Y resolution to 240 px\n" ); height = 240; } } else { if ( 0 != width % 16 || width < 16 || width > 480 ) { printf( "Warning: Corrected X resolution to 240 px\n" ); width = 240; } if ( 0 != height % 16 || height < 16 || height > 640 ) { printf( "Warning: Corrected Y resolution to 320 px\n" ); height = 320; } } if ( quality > 100 || quality < 10 ) { printf( "Warning: Corrected quality to 75%%\n" ); quality = 75; } if ( zoom > 2 || zoom < 1 ) { printf( "Warning: Corrected zoom to x1\n" ); zoom = 1; } if ( format != "JPEG" && format != "PNG" && format != "BMP" ) { printf( "Warning: Corrected format to 'JPEG'\n" ); format = "JPEG"; } } void Capturer::capture() { if ( flip == "A" ) ZCameraIO::instance()->setFlip( ZCameraIO::AUTOMATICFLIP ); else if ( flip == "0" ) ZCameraIO::instance()->setFlip( ZCameraIO::XNOFLIP | ZCameraIO::YNOFLIP ); else if ( flip == "X" ) ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP ); else if ( flip == "Y" ) ZCameraIO::instance()->setFlip( ZCameraIO::YFLIP ); else if ( flip == "*" ) ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP | ZCameraIO::YFLIP ); ZCameraIO::instance()->captureFrame( width, height, zoom, &image ); QImage im = image.convertDepth( 32 ); bool result = im.save( name, format, quality ); if ( !result ) { printf( "QImageio-Problem while writing.\n" ); } else { printf( "Ok.\n" ); } } void usage() { printf( "Usage: ./capture [options] filename\n\n" ); printf( " -x xresolution (dividable by 16) [default=240]\n" ); printf( " -y xresolution (dividable by 16) [default=320]\n" ); printf( " -q quality (10-100) [default=75]\n" ); printf( " -f flip (A=auto, 0, X, Y, *=both) [default=Auto]\n" ); printf( " -o output format (JPEG,BMP,PNG) [default=JPEG]\n" ); printf( " -z zoom (1-2) [default=1]\n" ); } int main( int argc, char** argv ) { OApplication* a = new OApplication( argc, argv, "Capture" ); Capturer* c = new Capturer(); if ( argc < 2 ) { usage(); return -1; } #define I_HATE_WRITING_HARDCODED_PARSES int i = 1; while ( i < argc ) { // check for filename if ( argv[i][0] != '-' ) { if ( argc != i+1 ) { usage(); return -1; } else { c->name = argv[i]; break; } } else { i++; if ( argc == i ) { usage(); return -1; } switch ( argv[i-1][1] ) { case 'x': c->width = QString( argv[i] ).toInt(); break; case 'y': c->height = QString( argv[i] ).toInt(); break; case 'z': c->zoom = QString( argv[i] ).toInt(); break; case 'o': c->format = QString( argv[i] ); break; case 'q': c->quality = QString( argv[i] ).toInt(); break; case 'f': c->flip = QString( argv[i] )[0]; break; default: usage(); return -1; } i++; } #undef I_HATE_WRITING_HARDCODED_PARSES } if ( !ZCameraIO::instance()->isOpen() ) { printf( "Error: Can't detect your camera. Exiting.\n" ); return -1; } c->checkSettings(); c->capture(); + + delete c; + delete a; return 0; } diff --git a/noncore/multimedia/camera/gui/mainwindow.cpp b/noncore/multimedia/camera/gui/mainwindow.cpp index 7f2a9bd..bde448d 100644 --- a/noncore/multimedia/camera/gui/mainwindow.cpp +++ b/noncore/multimedia/camera/gui/mainwindow.cpp @@ -165,567 +165,567 @@ void CameraMainWindow::init() qualityg->setToggleAction( true ); new QAction( " 0 (&minimal)", 0, 0, qualityg, "0", true ); new QAction( " 25 (&low)", 0, 0, qualityg, "25", true ); new QAction( " 50 (&good)", 0, 0, qualityg, "50", true ); new QAction( " 75 (&better)", 0, 0, qualityg, "75", true ); new QAction( "100 (bes&t)", 0, 0, qualityg, "100", true ); a = (QAction*) qualityg->child( QString().sprintf( "%d", quality ) ); if ( a ) a->setOn( true ); else owarn << "can't set quality" << oendl; zoomg = new QActionGroup( 0, "zoom", true ); zoomg->setToggleAction( true ); new QAction( "x 1", 0, 0, zoomg, "1", true ); new QAction( "x 2", 0, 0, zoomg, "2", true ); a = (QAction*) zoomg->child( QString().sprintf( "%d", zoom ) ); if ( a ) a->setOn( true ); else owarn << "can't set zoom" << oendl; flipg = new QActionGroup( 0, "flip", true ); flipg->setToggleAction( true ); new QAction( "Auto (recommended)", 0, 0, flipg, "A", true ); new QAction( "0 (always off)", 0, 0, flipg, "0", true ); new QAction( "X (always horizontal)", 0, 0, flipg, "X", true ); new QAction( "Y (always vertical)", 0, 0, flipg, "Y", true ); new QAction( "* (always both)", 0, 0, flipg, "*", true ); a = (QAction*) flipg->child( QString().sprintf( "%s", (const char*) flip ) ); if ( a ) a->setOn( true ); else owarn << "can't set flip" << oendl; outputTog = new QActionGroup( 0, "output", true ); outputTog->setToggleAction( true ); new QAction( "/tmp/", 0, 0, outputTog, "/tmp/", true ); new QAction( "/mnt/card/", 0, 0, outputTog, "/mnt/card/", true ); new QAction( "/mnt/cf/", 0, 0, outputTog, "/mnt/cf/", true ); docfolder = new QAction( "Documents Folder", 0, 0, outputTog, "Documents Folder", true ); custom = new QAction( "&Custom...", 0, 0, outputTog, "custom", true ); //TODO: How to save custom!? a = (QAction*) outputTog->child( QString().sprintf( "%s", (const char*) outputTo ) ); if ( a ) a->setOn( true ); else owarn << "can't set outputTo" << oendl; outputg = new QActionGroup( 0, "output", true ); outputg->setToggleAction( true ); new QAction( "JPEG", 0, 0, outputg, "JPEG", true ); new QAction( "PNG", 0, 0, outputg, "PNG", true ); new QAction( "BMP", 0, 0, outputg, "BMP", true ); new QAction( "AVI", 0, 0, outputg, "AVI", true ); a = (QAction*) outputg->child( QString().sprintf( "%s", (const char*) captureFormat ) ); if ( a ) a->setOn( true ); else owarn << "can't set output format" << oendl; connect( resog, SIGNAL( selected(QAction*) ), this, SLOT( resoMenuItemClicked(QAction*) ) ); connect( qualityg, SIGNAL( selected(QAction*) ), this, SLOT( qualityMenuItemClicked(QAction*) ) ); connect( zoomg, SIGNAL( selected(QAction*) ), this, SLOT( zoomMenuItemClicked(QAction*) ) ); connect( flipg, SIGNAL( selected(QAction*) ), this, SLOT( flipMenuItemClicked(QAction*) ) ); connect( outputTog, SIGNAL( selected(QAction*) ), this, SLOT( outputToMenuItemClicked(QAction*) ) ); connect( outputg, SIGNAL( selected(QAction*) ), this, SLOT( outputMenuItemClicked(QAction*) ) ); } void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& data ) { int _newrotation; QDataStream stream( data, IO_ReadOnly ); odebug << "received system message: " << msg << oendl; if ( msg == "setCurrentRotation(int)" ) { stream >> _newrotation; odebug << "received setCurrentRotation(" << _newrotation << ")" << oendl; switch ( _newrotation ) { case 270: preview->resize( QSize( 240, 288 ) ); break; case 180: preview->resize( QSize( 320, 208 ) ); break; default: QMessageBox::warning( this, "opie-camera", "This rotation is not supported.\n" "Supported are 180° and 270°" ); } if ( _newrotation != _rotation ) { int tmp = captureX; captureX = captureY; captureY = tmp; _rotation = _newrotation; } updateCaption(); } } void CameraMainWindow::changeZoom( int zoom ) { int z; switch ( zoom ) { case 0: z = 128; break; case 1: z = 256; break; case 2: z = 512; break; default: assert( 0 ); break; } ZCameraIO::instance()->setCaptureFrame( 240, 160, z ); } void CameraMainWindow::showContextMenu() { QPopupMenu reso; reso.setCheckable( true ); resog->addTo( &reso ); QPopupMenu quality; quality.setCheckable( true ); qualityg->addTo( &quality ); QPopupMenu flip; flip.setCheckable( true ); flipg->addTo( &flip ); QPopupMenu zoom; zoom.setCheckable( true ); zoomg->addTo( &zoom ); QPopupMenu prefix; prefix.insertItem( "&Choose...", this, SLOT( prefixItemChoosen() ) ); int id = prefix.insertItem( "&Append Settings", this, SLOT( appendSettingsChoosen() ) ); prefix.setItemChecked( id, appendSettings ); QPopupMenu outputTo; outputTo.setCheckable( true ); outputTog->addTo( &outputTo ); QPopupMenu output; output.setCheckable( true ); outputg->addTo( &output ); QPopupMenu m( this ); m.insertItem( "&Resolution", &reso ); m.insertItem( "&Zoom", &zoom ); m.insertItem( "&Flip", &flip ); m.insertItem( "&Quality", &quality ); m.insertSeparator(); m.insertItem( "&Prefix", &prefix ); m.insertItem( "Output &To", &outputTo ); m.insertItem( "&Output As", &output ); #ifndef QT_NO_DEBUG m.insertItem( "&Debug!", this, SLOT( doSomething() ) ); #endif m.exec( QCursor::pos() ); } void CameraMainWindow::resoMenuItemClicked( QAction* a ) { switch ( _rotation ) { case 270: captureY = a->text().left(3).toInt(); captureX = a->text().right(3).toInt(); break; case 180: captureX = a->text().left(3).toInt(); captureY = a->text().right(3).toInt(); break; default: QMessageBox::warning( this, "opie-camera", "This rotation is not supported.\n" "Supported are 180° and 270°" ); } odebug << "Capture Resolution now: " << captureX << ", " << captureY << oendl; updateCaption(); } void CameraMainWindow::qualityMenuItemClicked( QAction* a ) { quality = a->text().left(3).toInt(); odebug << "Quality now: " << quality << oendl; updateCaption(); } void CameraMainWindow::zoomMenuItemClicked( QAction* a ) { zoom = QString( a->text().at(2) ).toInt(); odebug << "Zoom now: " << zoom << oendl; ZCameraIO::instance()->setZoom( zoom ); updateCaption(); } void CameraMainWindow::flipMenuItemClicked( QAction* a ) { flip = QString( a->text().at(0) ); odebug << "Flip now: " << flip << oendl; if ( flip == "A" ) ZCameraIO::instance()->setFlip( ZCameraIO::AUTOMATICFLIP ); else if ( flip == "0" ) ZCameraIO::instance()->setFlip( ZCameraIO::XNOFLIP | ZCameraIO::YNOFLIP ); else if ( flip == "X" ) ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP ); else if ( flip == "Y" ) ZCameraIO::instance()->setFlip( ZCameraIO::YFLIP ); else if ( flip == "*" ) ZCameraIO::instance()->setFlip( ZCameraIO::XFLIP | ZCameraIO::YFLIP ); updateCaption(); } void CameraMainWindow::outputToMenuItemClicked( QAction* a ) { if ( a->text() == "&Custom..." ) { QMap<QString, QStringList> map; map.insert( tr("All"), QStringList() ); QStringList text; text << "text/*"; map.insert(tr("Text"), text ); text << "*"; map.insert(tr("All"), text ); QString str; str = OFileDialog::getSaveFileName( 2, "/", QString::null, map ); if ( str.isEmpty() || !QFileInfo(str).isDir() ) { docfolder->setOn( true ); outputTo = "Documents Folder"; } else { outputTo = str; } } else { outputTo = a->text(); } odebug << "Output to now: " << outputTo << oendl; } void CameraMainWindow::outputMenuItemClicked( QAction* a ) { captureFormat = a->text(); odebug << "Output format now: " << captureFormat << oendl; updateCaption(); } void CameraMainWindow::prefixItemChoosen() { QDialog* d = new QDialog( this, "dialog", true ); d->setCaption( "Enter Prefix..." ); QVBoxLayout* v = new QVBoxLayout( d ); QLineEdit* le = new QLineEdit( prefix, d ); v->addWidget( le ); le->setFixedWidth( 150 ); //FIXME: 'tis a bit dirty if ( d->exec() == QDialog::Accepted ) prefix = le->text(); odebug << "Prefix now: " << prefix << oendl; } void CameraMainWindow::appendSettingsChoosen() { appendSettings = !appendSettings; odebug << "appendSettings now: " << appendSettings << oendl; } void CameraMainWindow::shutterClicked() { if ( captureFormat != "AVI" ) // capture one photo per shutterClick { Global::statusMessage( "CAPTURING..." ); qApp->processEvents(); odebug << "Shutter has been pressed" << oendl; ODevice::inst()->playTouchSound(); performCapture( captureFormat ); } else // capture video! start with one shutter click and stop with the next { !_capturing ? startVideoCapture() : stopVideoCapture(); } } void CameraMainWindow::performCapture( const QString& format ) { QString name; if ( outputTo == "Documents Folder" ) { name.sprintf( "%s/Documents/image/%s/", (const char*) QDir::homeDirPath(), (const char*) captureFormat.lower() ); if ( !QDir( name ).exists() ) { odebug << "creating directory " << name << oendl; QString msg = "mkdir -p " + name; system( msg.latin1() ); } } else name = outputTo; name.append( prefix ); if ( appendSettings ) { name.append( QString().sprintf( "_%d_%d_q%d", captureX, captureY, quality ) ); } name.append( QString().sprintf( "-%d.%s", _pics++, (const char*) captureFormat.lower() ) ); QImage i; ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, &i ); QImage im = i.convertDepth( 32 ); bool result = im.save( name, format, quality ); if ( !result ) { oerr << "imageio-Problem while writing." << oendl; Global::statusMessage( "Error!" ); } else { odebug << captureFormat << "-image has been successfully captured" << oendl; Global::statusMessage( "Ok." ); } } void CameraMainWindow::startVideoCapture() { ODevice::inst()->playTouchSound(); ODevice::inst()->setLedState( Led_Mail, Led_BlinkSlow ); _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT | O_TRUNC ); if ( _capturefd == -1 ) { owarn << "can't open capture file: " << strerror(errno) << oendl; return; } _capturebuf = new unsigned char[captureX*captureY*2]; _capturing = true; _videopics = 0; _framerate = 0; updateCaption(); _time.start(); preview->setRefreshingRate( 1000 ); startTimer( 100 ); // too fast but that is ok } void CameraMainWindow::timerEvent( QTimerEvent* ) { if ( !_capturing ) { odebug << "timer event in CameraMainWindow without capturing video ?" << oendl; return; } odebug << "timer event during video - now capturing frame #" << _videopics+1 << oendl; ZCameraIO::instance()->captureFrame( captureX, captureY, zoom, _capturebuf ); _videopics++; ::write( _capturefd, _capturebuf, captureX*captureY*2 ); setCaption( QString().sprintf( "Capturing %dx%d @ %.2f fps %d", captureX, captureY, 1000.0 / (_time.elapsed()/_videopics), _videopics ) ); } void CameraMainWindow::stopVideoCapture() { killTimers(); ODevice::inst()->playTouchSound(); ODevice::inst()->setLedState( Led_Mail, Led_Off ); _capturing = false; updateCaption(); ::close( _capturefd ); - _framerate = 1000.0 / (_time.elapsed()/_videopics); + _framerate = static_cast<int>( 1000.0 / (_time.elapsed()/_videopics) ); QString name; if ( outputTo == "Documents Folder" ) { name.sprintf( "%s/Documents/video/%s/", (const char*) QDir::homeDirPath(), (const char*) captureFormat.lower() ); if ( !QDir( name ).exists() ) { odebug << "creating directory " << name << oendl; QString msg = "mkdir -p " + name; system( msg.latin1() ); } } else name = outputTo; name.append( "/" ); // sure is sure and safe is safe ;-) name.append( prefix ); if ( appendSettings ) name.append( QString().sprintf( "_%d_%d_q%d_%dfps", captureX, captureY, quality, _framerate ) ); name.append( QString().sprintf( "-%d.%s", _videos++, (const char*) captureFormat.lower() ) ); postProcessVideo( CAPTUREFILE, name ); #ifndef QT_NO_DEBUG preview->setRefreshingRate( 1500 ); #else preview->setRefreshingRate( 200 ); #endif //delete[] _capturebuf; //FIXME: close memory leak } void CameraMainWindow::postProcessVideo( const QString& infile, const QString& outfile ) { odebug << "post processing " << infile << " --> " << outfile << oendl; preview->setRefreshingRate( 0 ); /* unsigned char buf[153600]; int fd = ::open( "/var/compile/opie/noncore/multimedia/camera/capture-320x240.dat", O_RDONLY ); ::read( fd, &buf, 153600 ); QImage i; bufferToImage( 240, 320, (unsigned char*) &buf, &i ); QPixmap p; p.convertFromImage( i ); preview->setPixmap( p ); imageToFile( &i, "/tmp/tmpfile", "JPEG", 100 ); return; */ QDialog* fr = new QDialog( this, "splash", false, QWidget::WStyle_StaysOnTop ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize ); fr->setCaption( "Please wait..." ); QVBoxLayout* box = new QVBoxLayout( fr, 2, 2 ); QProgressBar* bar = new QProgressBar( fr ); bar->setCenterIndicator( true ); bar->setTotalSteps( _videopics-1 ); QLabel* label = new QLabel( "Post processing frame bla/bla", fr ); box->addWidget( bar ); box->addWidget( label ); fr->show(); label->show(); bar->show(); fr->repaint(); qApp->processEvents(); // open files int infd = ::open( (const char*) infile, O_RDONLY ); if ( infd == -1 ) { owarn << "couldn't open capture file: " << strerror(errno) << oendl; return; } int outfd = ::open( (const char*) outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644 ); if ( outfd == -1 ) { owarn << "couldn't open output file: " << strerror(errno) << oendl; return; } int framesize = captureX*captureY*2; unsigned char* inbuffer = new unsigned char[ framesize ]; QImage image; avi_start( outfd, _videopics ); // write preambel // post process for ( int i = 0; i < _videopics; ++i ) { odebug << "processing frame " << i << oendl; // <gui> label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) ); bar->setProgress( i ); bar->repaint(); qApp->processEvents(); // </gui> int read = ::read( infd, inbuffer, framesize ); odebug << "read " << read << " bytes" << oendl; bufferToImage( captureX, captureY, inbuffer, &image ); QPixmap p; p.convertFromImage( image ); preview->setPixmap( p ); preview->repaint(); qApp->processEvents(); #ifdef CAMERA_EXTRA_DEBUG QString tmpfilename; tmpfilename.sprintf( "/tmp/test/%04d.jpg", i ); #else QString tmpfilename( "/tmp/tempfile" ); #endif imageToFile( &image, tmpfilename, "JPEG", quality ); QFile framefile( tmpfilename ); if ( !framefile.open( IO_ReadOnly ) ) { oerr << "can't process file: %s" << strerror(errno) << oendl; return; // TODO: clean up temp ressources } int filesize = framefile.size(); odebug << "filesize for frame " << i << " = " << filesize << oendl; unsigned char* tempbuffer = new unsigned char[ filesize ]; framefile.readBlock( (char*) tempbuffer, filesize ); avi_add( outfd, tempbuffer, filesize ); delete tempbuffer; framefile.close(); } avi_end( outfd, captureX, captureY, _framerate ); ::close( outfd ); ::close( infd ); label->setText( "deleting temp files..." ); qApp->processEvents(); odebug << "deleting temporary capturefile " << infile << oendl; QFile::remove( infile ); fr->hide(); delete fr; updateCaption(); } void CameraMainWindow::updateCaption() { if ( !_capturing ) setCaption( QString().sprintf( "Opie-Camera: %dx%d %s q%d z%d (%s)", captureX, captureY, (const char*) captureFormat.lower(), quality, zoom, (const char*) flip ) ); else setCaption( "Opie-Camera: => CAPTURING <=" ); qApp->processEvents(); } #ifndef QT_NO_DEBUG void CameraMainWindow::doSomething() { captureX = 240; captureY = 320; _videopics = 50; _framerate = 5; postProcessVideo( "/var/compile/opie/noncore/multimedia/camera/capture.dat", "/tmp/output.avi" ); } #else void CameraMainWindow::doSomething() { } #endif diff --git a/noncore/multimedia/camera/lib/avi.c b/noncore/multimedia/camera/lib/avi.c index 77aba33..d99c016 100644 --- a/noncore/multimedia/camera/lib/avi.c +++ b/noncore/multimedia/camera/lib/avi.c @@ -1,236 +1,237 @@ /********************************************************************** ** Copyright (C) 2003 Michael 'Mickey' Lauer. All rights reserved. ** Based on work from Andrew Tridgell and the jpegtoavi project ** ** This file is part of Opie Environment. ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** **********************************************************************/ #include "avi.h" #include <string.h> #include <stdio.h> +#include <stdlib.h> int nframes; int totalsize; unsigned int* sizes; void fprint_quartet(int fd, unsigned int i) { char data[4]; data[0] = (char) i%0x100; i /= 0x100; data[1] = (char) i%0x100; i /= 0x100; data[2] = (char) i%0x100; i /= 0x100; data[3] = (char) i%0x100; write( fd, &data, 4 ); } // start writing an AVI file void avi_start(int fd, int frames) { int ofs = sizeof(struct riff_head)+ sizeof(struct list_head)+ sizeof(struct avi_head)+ sizeof(struct list_head)+ sizeof(struct stream_head)+ sizeof(struct frame_head)+ sizeof(struct list_head)+ sizeof(struct dmlh_head)+ sizeof(struct list_head); printf( "avi_start: frames = %d\n", frames ); lseek(fd, ofs, SEEK_SET); nframes = 0; totalsize = 0; sizes = (unsigned int*) calloc( sizeof(unsigned int), frames ); // hold size of each frame } // add a jpeg frame to an AVI file void avi_add(int fd, u8 *buf, int size) { struct db_head db = {"00db", 0}; printf( "avi_add: nframes = %d, totalsize = %d, size = %d\n", nframes, totalsize, size ); // overwrite JFIF type with AVI1 buf[6]='A'; buf[7]='V'; buf[8]='I'; buf[9]='1'; while( size%4 ) size++; // align 0 modulo 4*/ db.size = size; write( fd, &db, sizeof(db) ); write( fd, buf, size ); sizes[nframes] = size; nframes++; totalsize += size; // total frame size } // finish writing the AVI file - filling in the header void avi_end(int fd, int width, int height, int fps) { struct idx1_head idx = {"idx1", 16*nframes }; struct db_head db = {"00db", 0}; struct riff_head rh = { "RIFF", 0, "AVI "}; struct list_head lh1 = {"LIST", 0, "hdrl"}; struct avi_head ah; struct list_head lh2 = {"LIST", 0, "strl"}; struct stream_head sh; struct frame_head fh; struct list_head lh3 = {"LIST", 0, "odml" }; struct dmlh_head dh = {"dmlh", 4, nframes }; struct list_head lh4 = {"LIST", 0, "movi"}; int i; unsigned int offset = 4; printf( "avi_end: nframes = %d, fps = %d\n", nframes, fps ); // write index write(fd, &idx, sizeof(idx)); for ( i = 0; i < nframes; i++ ) { write(fd, &db, 4 ); // only need the 00db fprint_quartet( fd, 18 ); // ??? fprint_quartet( fd, offset ); fprint_quartet( fd, sizes[i] ); offset += sizes[i] + 8; //+8 (for the additional header) } free( sizes ); bzero( &ah, sizeof(ah) ); strcpy(ah.avih, "avih"); ah.time = 1000000 / fps; ah.maxbytespersec = 1000000.0*(totalsize/nframes)/ah.time; ah.nframes = nframes; ah.numstreams = 1; ah.flags = AVIF_HASINDEX; ah.width = width; ah.height = height; bzero(&sh, sizeof(sh)); strcpy(sh.strh, "strh"); strcpy(sh.vids, "vids"); strcpy(sh.codec, "MJPG"); sh.scale = ah.time; sh.rate = 1000000; sh.length = nframes; bzero(&fh, sizeof(fh)); strcpy(fh.strf, "strf"); fh.width = width; fh.height = height; fh.planes = 1; fh.bitcount = 24; strcpy(fh.codec,"MJPG"); fh.unpackedsize = 3*width*height; rh.size = sizeof(lh1)+sizeof(ah)+sizeof(lh2)+sizeof(sh)+ sizeof(fh)+sizeof(lh3)+sizeof(dh)+sizeof(lh4)+ nframes*sizeof(struct db_head)+ totalsize + sizeof(struct idx1_head)+ (16*nframes) +4; // FIXME:16 bytes per nframe // the '4' - what for??? lh1.size = 4+sizeof(ah)+sizeof(lh2)+sizeof(sh)+sizeof(fh)+sizeof(lh3)+sizeof(dh); ah.size = sizeof(ah)-8; lh2.size = 4+sizeof(sh)+sizeof(fh)+sizeof(lh3)+sizeof(dh); //4+sizeof(sh)+sizeof(fh); sh.size = sizeof(sh)-8; fh.size = sizeof(fh)-8; fh.size2 = fh.size; lh3.size = 4+sizeof(dh); lh4.size = 4+ nframes*sizeof(struct db_head)+ totalsize; lseek(fd, 0, SEEK_SET); write(fd, &rh, sizeof(rh)); write(fd, &lh1, sizeof(lh1)); write(fd, &ah, sizeof(ah)); write(fd, &lh2, sizeof(lh2)); write(fd, &sh, sizeof(sh)); write(fd, &fh, sizeof(fh)); write(fd, &lh3, sizeof(lh3)); write(fd, &dh, sizeof(dh)); write(fd, &lh4, sizeof(lh4)); } /* NOTE: This is not a general purpose routine - it is meant to only cope with AVIs saved using the other functions in this file void avi_explode(char *fname) { struct riff_head rh; struct list_head lh1; struct avi_head ah; struct list_head lh2; struct stream_head sh; struct frame_head fh; struct list_head lh3; int hsize, qsize; u16 *htables = jpeg_huffman_tables(&hsize); u16 *qtables = jpeg_quantisation_tables(&qsize, image_quality); int fd, i; fd = open(fname,O_RDONLY); if (fd == -1) { perror(fname); return; } read(fd, &rh, sizeof(rh)); read(fd, &lh1, sizeof(lh1)); read(fd, &ah, sizeof(ah)); read(fd, &lh2, sizeof(lh2)); read(fd, &sh, sizeof(sh)); read(fd, &fh, sizeof(fh)); read(fd, &lh3, sizeof(lh3)); for (i=0; ; i++) { u8 buf[500*1024]; struct db_head db; char fname[100]; int fd2; if (read(fd, &db, sizeof(db)) != sizeof(db) || read(fd, buf, db.size) != db.size) break; snprintf(fname, sizeof(fname)-1,"frame.%d", i); fd2 = open(fname,O_WRONLY|O_CREAT, 0644); if (fd2 == -1) { perror(fname); continue; } write(fd2, buf, 2); write(fd2, qtables, qsize); write(fd2, htables, hsize); write(fd2, buf+2, db.size-2); close(fd2); } close(fd); printf("exploded %d frames\n", i); } */ |