From 606f5d9cd2ecba4df175bc722621e4e4df3bc516 Mon Sep 17 00:00:00 2001 From: mickeyl Date: Sun, 20 Apr 2003 17:36:20 +0000 Subject: too much work for that damn video feature... some progress, but it's still not right --- (limited to 'noncore/multimedia/camera') diff --git a/noncore/multimedia/camera/avi.c b/noncore/multimedia/camera/avi.c new file mode 100644 index 0000000..0c757c9 --- a/dev/null +++ b/noncore/multimedia/camera/avi.c @@ -0,0 +1,235 @@ +/********************************************************************** +** 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 +#include + +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]; + } + + free( sizes ); + + bzero( &ah, sizeof(ah) ); + strcpy(ah.avih, "avih"); + ah.time = 1000000 / fps; + ah.maxbytespersec = 1000000.0*(totalsize/nframes)/ah.time; + 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); +} + +*/ + diff --git a/noncore/multimedia/camera/avi.h b/noncore/multimedia/camera/avi.h new file mode 100644 index 0000000..fbdc14a --- a/dev/null +++ b/noncore/multimedia/camera/avi.h @@ -0,0 +1,142 @@ +/********************************************************************** +** 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. +** +**********************************************************************/ + +#ifndef AVI_H +#define AVI_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned u32; + +// header flags + +const u32 AVIF_HASINDEX=0x00000010; /* index at end of file */ +const u32 AVIF_MUSTUSEINDEX=0x00000020; +const u32 AVIF_ISINTERLEAVED=0x00000100; +const u32 AVIF_TRUSTCKTYPE=0x00000800; +const u32 AVIF_WASCAPTUREFILE=0x00010000; +const u32 AVIF_COPYRIGHTED=0x00020000; + +// function prototypes + +void avi_start(int fd, int frame); +void avi_add(int fd, u8 *buf, int size); +void avi_end(int fd, int width, int height, int fps); +void fprint_quartet(int fd, unsigned int i); + +// the following structures are ordered as they appear in a typical AVI + +struct riff_head { + char riff[4]; // chunk type = "RIFF" + u32 size; // chunk size + char avistr[4]; // avi magic = "AVI " +}; + +// the avih chunk contains a number of list chunks + +struct avi_head { + char avih[4]; // chunk type = "avih" + u32 size; // chunk size + u32 time; // microsec per frame == 1e6 / fps + u32 maxbytespersec; // = 1e6*(total size/frames)/per_usec) + u32 pad; // pad = 0 + u32 flags; // e.g. AVIF_HASINDEX + u32 nframes; // total number of frames + u32 initialframes; // = 0 + u32 numstreams; // = 1 for now (later = 2 because of audio) + u32 suggested_bufsize; // = 0 (no suggestion) + u32 width; // width + u32 height; // height + u32 reserved[4]; // reserved for future use = 0 +}; + + +// the LIST chunk contains a number (==#numstreams) of stream chunks + +struct list_head { + char list[4]; // chunk type = "LIST" + u32 size; + char type[4]; +}; + + +struct dmlh_head { + char dmlh[4]; // chunk type dmlh + u32 size; // 4 + u32 nframes; // number of frames +}; + + +struct stream_head { + char strh[4]; // chunk type = "strh" + u32 size; // chunk size + char vids[4]; // stream type = "vids" + char codec[4]; // codec name (for us, = "MJPG") + u32 flags; // contains AVIT_F* flags + u16 priority; // = 0 + u16 language; // = 0 + u32 initialframes; // = 0 + u32 scale; // = usec per frame + u32 rate; // 1e6 + u32 start; // = 0 + u32 length; // number of frames + u32 suggested_bufsize; // = 0 + u32 quality; // = 0 ? + u32 samplesize; // = 0 ? +}; + + +struct db_head { + char db[4]; // "00db" + u32 size; +}; + +// a frame chunk contains one JPEG image + +struct frame_head { + char strf[4]; // chunk type = "strf" + u32 size; // sizeof chunk (big endian) ? + u32 size2; // sizeof chunk (little endian) ? + u32 width; + u32 height; + u16 planes; // 1 bitplane + u16 bitcount; // 24 bpl + char codec[4]; // MJPG (for us) + u32 unpackedsize; // = 3*w*h + u32 r1; // reserved + u32 r2; // reserved + u32 clr_used; // reserved + u32 clr_important; // reserved +}; + +struct idx1_head { + char idx1[4]; // chunk type = "idx1" + u32 size; // chunk size +}; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/noncore/multimedia/camera/camera.pro b/noncore/multimedia/camera/camera.pro index 8aedcea..c0e6ca8 100644 --- a/noncore/multimedia/camera/camera.pro +++ b/noncore/multimedia/camera/camera.pro @@ -4,12 +4,14 @@ DESTDIR = $(OPIEDIR)/bin TEMPLATE = app CONFIG = qt warn_on debug -HEADERS = imageio.h \ +HEADERS = avi.h \ + imageio.h \ zcameraio.h \ previewwidget.h \ mainwindow.h -SOURCES = imageio.cpp \ +SOURCES = avi.c \ + imageio.cpp \ zcameraio.cpp \ previewwidget.cpp \ mainwindow.cpp \ diff --git a/noncore/multimedia/camera/mainwindow.cpp b/noncore/multimedia/camera/mainwindow.cpp index 6141fd1..e27a50e 100644 --- a/noncore/multimedia/camera/mainwindow.cpp +++ b/noncore/multimedia/camera/mainwindow.cpp @@ -17,6 +17,7 @@ #include "previewwidget.h" #include "zcameraio.h" #include "imageio.h" +#include "avi.h" #include #include @@ -50,9 +51,13 @@ using namespace Opie; #include #define CAPTUREFILE "/tmp/capture.dat" +#define OUTPUTFILE "/tmp/output.avi" CameraMainWindow::CameraMainWindow( QWidget * parent, const char * name, WFlags f ) - :QMainWindow( parent, name, f ), _capturing( false ), _pics( 0 ) + :QMainWindow( parent, name, f ), + _rotation( 270 ), // FIXME: get this from current settings (ODevice?) + _capturing( false ), + _pics( 0 ), _videos( 0 ) { #ifdef QT_NO_DEBUG if ( !ZCameraIO::instance()->isOpen() ) @@ -105,8 +110,8 @@ void CameraMainWindow::init() flip = 'A'; // auto quality = 50; zoom = 1; - captureX = 640; - captureY = 480; + captureX = 480; + captureY = 640; captureFormat = "JPEG"; resog = new QActionGroup( 0, "reso", true ); @@ -161,14 +166,16 @@ void CameraMainWindow::init() 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 >> _rotation; - odebug << "received setCurrentRotation(" << _rotation << ")" << oendl; + stream >> _newrotation; + odebug << "received setCurrentRotation(" << _newrotation << ")" << oendl; - switch ( _rotation ) + switch ( _newrotation ) { case 270: preview->resize( QSize( 240, 288 ) ); break; case 180: preview->resize( QSize( 320, 208 ) ); break; @@ -176,6 +183,17 @@ void CameraMainWindow::systemMessage( const QCString& msg, const QByteArray& dat "This rotation is not supported.\n" "Supported are 180° and 270°" ); } + + if ( _newrotation != _rotation ) + { + int tmp = captureX; + captureX = captureY; + captureY = tmp; + _rotation = _newrotation; + } + + updateCaption(); + } } @@ -223,14 +241,31 @@ void CameraMainWindow::showContextMenu() m.insertItem( "&Flip", &flip ); m.insertItem( "&Quality", &quality ); 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 ) { - captureX = a->text().left(3).toInt(); - captureY = a->text().right(3).toInt(); + 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(); } @@ -325,7 +360,7 @@ void CameraMainWindow::startVideoCapture() //ODevice::inst()->touchSound(); ODevice::inst()->setLedState( Led_Mail, Led_BlinkSlow ); - _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT ); + _capturefd = ::open( CAPTUREFILE, O_WRONLY | O_CREAT | O_TRUNC ); if ( _capturefd == -1 ) { owarn << "can't open capture file: " << strerror(errno) << oendl; @@ -335,6 +370,7 @@ void CameraMainWindow::startVideoCapture() _capturebuf = new unsigned char[captureX*captureY*2]; _capturing = true; _videopics = 0; + _framerate = 0; updateCaption(); _time.start(); preview->setRefreshingRate( 1000 ); @@ -346,10 +382,12 @@ void CameraMainWindow::timerEvent( QTimerEvent* ) { if ( !_capturing ) { - owarn << "timer event in CameraMainWindow without capturing video ?" << oendl; + 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 ); @@ -366,8 +404,9 @@ void CameraMainWindow::stopVideoCapture() _capturing = false; updateCaption(); ::close( _capturefd ); + _framerate = 1000.0 / (_time.elapsed()/_videopics); - //postProcessVideo(); + postProcessVideo( CAPTUREFILE, QString().sprintf( "/tmp/video-%d_%d_%d_q%d-%dfps.avi", _videos++, captureX, captureY, quality, _framerate ) ); #ifndef QT_NO_DEBUG preview->setRefreshingRate( 1500 ); @@ -378,13 +417,25 @@ void CameraMainWindow::stopVideoCapture() //delete[] _capturebuf; //FIXME: close memory leak } -void CameraMainWindow::postProcessVideo() +void CameraMainWindow::postProcessVideo( const QString& infile, const QString& outfile ) { preview->setRefreshingRate( 0 ); /* + unsigned char buf[153600]; - QDialog* fr = new QDialog( this, "splash" ); //, false, QWidget::WStyle_NoBorder | QWidget::WStyle_Customize ); + 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 ); @@ -394,36 +445,89 @@ void CameraMainWindow::postProcessVideo() box->addWidget( bar ); box->addWidget( label ); fr->show(); + label->show(); + bar->show(); + fr->repaint(); qApp->processEvents(); - for ( int i = 0; i < _videopics; ++i ) - { - label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) ); - bar->setProgress( i ); - qApp->processEvents(); - } - - */ + // open files - int infd = ::open( CAPTUREFILE, O_RDONLY ); + int infd = ::open( (const char*) infile, O_RDONLY ); if ( infd == -1 ) { owarn << "couldn't open capture file: " << strerror(errno) << oendl; return; } - int outfd = ::open( "/tmp/output.avi", O_WRONLY ); + 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; + + // + label->setText( QString().sprintf( "Post processing frame %d / %d", i+1, _videopics ) ); + bar->setProgress( i ); + bar->repaint(); + qApp->processEvents(); + // + + 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(); + + QString tmpfilename( "/tmp/tempfile" ); + //tmpfilename.sprintf( "/tmp/test/%d.jpg", i ); + + 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 ); + + fr->hide(); + delete fr; + + updateCaption(); } + void CameraMainWindow::updateCaption() { if ( !_capturing ) @@ -433,3 +537,15 @@ void CameraMainWindow::updateCaption() } +#ifndef QT_NO_DEBUG +void CameraMainWindow::doSomething() +{ + captureX = 240; + captureY = 320; + _videopics = 176; + _framerate = 5; + postProcessVideo( "/var/compile/opie/noncore/multimedia/camera/capture-320x240.dat", + "/tmp/output.avi" ); +} +#endif + diff --git a/noncore/multimedia/camera/mainwindow.h b/noncore/multimedia/camera/mainwindow.h index ad8d1b1..ac83488 100644 --- a/noncore/multimedia/camera/mainwindow.h +++ b/noncore/multimedia/camera/mainwindow.h @@ -57,11 +57,16 @@ class CameraMainWindow: public QMainWindow void init(); void startVideoCapture(); void stopVideoCapture(); - void postProcessVideo(); + void postProcessVideo( const QString&, const QString& ); void performCapture( const QString& ); virtual void timerEvent( QTimerEvent* ); + protected slots: + #ifndef QT_NO_DEBUG + void doSomething(); // solely for debugging purposes + #endif + private: PreviewWidget* preview; int _rotation; @@ -82,10 +87,12 @@ class CameraMainWindow: public QMainWindow bool _capturing; int _pics; + int _videos; QTime _time; int _videopics; int _capturefd; + int _framerate; unsigned char* _capturebuf; }; diff --git a/noncore/multimedia/camera/zcameraio.cpp b/noncore/multimedia/camera/zcameraio.cpp index 1c449e7..c940b45 100644 --- a/noncore/multimedia/camera/zcameraio.cpp +++ b/noncore/multimedia/camera/zcameraio.cpp @@ -293,10 +293,7 @@ void ZCameraIO::captureFrame( int w, int h, int zoom, QImage* image ) { int pw = _width; int ph = _height; - if ( _rot ) - setCaptureFrame( h, w, zoom*256, true ); - else - setCaptureFrame( w, h, zoom*256, false ); + setCaptureFrame( w, h, zoom*256, _rot ); snapshot( image ); setCaptureFrame( pw, ph, _zoom, _rot ); } @@ -307,11 +304,7 @@ void ZCameraIO::captureFrame( int w, int h, int zoom, unsigned char* buf ) //FIXME: this is too slow int pw = _width; int ph = _height; - if ( _rot ) - setCaptureFrame( h, w, zoom*256, true ); - else - setCaptureFrame( w, h, zoom*256, false ); - + setCaptureFrame( w, h, zoom*256, _rot ); snapshot( buf ); setCaptureFrame( pw, ph, _zoom, _rot ); } -- cgit v0.9.0.2