diff options
author | handbrake <[email protected]> | 2006-01-14 12:56:59 +0000 |
---|---|---|
committer | handbrake <[email protected]> | 2006-01-14 12:56:59 +0000 |
commit | 452f36599a1ea52f3bd42361b534594568c428fd (patch) | |
tree | 7f53f79212cf524f0a13ed5ee2250992f21eabcb /core/Mpeg4Encoder.cpp | |
parent | a9a84221af31ca7d11d1aa182d8b152270203f9f (diff) |
HandBrake 0.4
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'core/Mpeg4Encoder.cpp')
-rw-r--r-- | core/Mpeg4Encoder.cpp | 114 |
1 files changed, 74 insertions, 40 deletions
diff --git a/core/Mpeg4Encoder.cpp b/core/Mpeg4Encoder.cpp index 42fb38b9e..4ef0a4c3f 100644 --- a/core/Mpeg4Encoder.cpp +++ b/core/Mpeg4Encoder.cpp @@ -1,4 +1,4 @@ -/* $Id: Mpeg4Encoder.cpp,v 1.15 2003/09/30 14:38:15 titer Exp $ +/* $Id: Mpeg4Encoder.cpp,v 1.23 2003/10/09 13:24:48 titer Exp $ This file is part of the HandBrake source code. Homepage: <http://beos.titer.org/handbrake/>. @@ -11,49 +11,97 @@ #include <ffmpeg/avcodec.h> HBMpeg4Encoder::HBMpeg4Encoder( HBManager * manager, HBTitle * title ) - : HBThread( "mpeg4encoder" ) { fManager = manager; fTitle = title; + + fLock = new HBLock(); + fUsed = false; + + fPass = 42; + fMpeg4Buffer = NULL; + fFile = NULL; + fFrame = avcodec_alloc_frame(); + fLog = NULL; } -void HBMpeg4Encoder::DoWork() +bool HBMpeg4Encoder::Work() { - if( !( fResizedBuffer = Pop( fTitle->fResizedFifo ) ) ) + if( !Lock() ) { - return; + return false; } - fPass = fResizedBuffer->fPass; - Init(); + bool didSomething = false; - do + for( ;; ) { - while( fSuspend ) + if( fMpeg4Buffer ) { - Snooze( 10000 ); + if( fTitle->fMpeg4Fifo->Push( fMpeg4Buffer ) ) + { + fMpeg4Buffer = NULL; + } + else + { + break; + } + } + + if( !( fResizedBuffer = fTitle->fResizedFifo->Pop() ) ) + { + break; } if( fResizedBuffer->fPass != fPass ) { - Close(); fPass = fResizedBuffer->fPass; Init(); } fManager->SetPosition( fResizedBuffer->fPosition ); EncodeBuffer(); + + didSomething = true; + } + + Unlock(); + return didSomething; +} + +bool HBMpeg4Encoder::Lock() +{ + fLock->Lock(); + if( fUsed ) + { + fLock->Unlock(); + return false; } - while( ( fResizedBuffer = Pop( fTitle->fResizedFifo ) ) ); + fUsed = true; + fLock->Unlock(); + return true; +} + +void HBMpeg4Encoder::Unlock() +{ + fLock->Lock(); + fUsed = false; + fLock->Unlock(); } void HBMpeg4Encoder::Init() { + /* Clean up if needed */ + if( fFile ) + { + fclose( fFile ); + } + AVCodec * codec = avcodec_find_encoder( CODEC_ID_MPEG4 ); if( !codec ) { Log( "HBMpeg4Encoder: avcodec_find_encoder() failed" ); - fManager->Error(); + fManager->Error( HB_ERROR_MPEG4_INIT ); return; } @@ -94,19 +142,9 @@ void HBMpeg4Encoder::Init() if( avcodec_open( fContext, codec ) < 0 ) { Log( "HBMpeg4Encoder: avcodec_open() failed" ); - fManager->Error(); + fManager->Error( HB_ERROR_MPEG4_INIT ); return; } - - fFrame = avcodec_alloc_frame(); -} - -void HBMpeg4Encoder::Close() -{ - if( fPass == 1 ) - { - fclose( fFile ); - } } void HBMpeg4Encoder::EncodeBuffer() @@ -120,31 +158,27 @@ void HBMpeg4Encoder::EncodeBuffer() fFrame->linesize[1] = fTitle->fOutWidth / 2; fFrame->linesize[2] = fTitle->fOutWidth / 2; - HBBuffer * mpeg4Buffer; - mpeg4Buffer = new HBBuffer( 3 * fTitle->fOutWidth * + fMpeg4Buffer = new HBBuffer( 3 * fTitle->fOutWidth * fTitle->fOutHeight / 2 ); /* Should be really too much... */ - mpeg4Buffer->fPosition = fResizedBuffer->fPosition; - mpeg4Buffer->fSize = - avcodec_encode_video( fContext, mpeg4Buffer->fData, - mpeg4Buffer->fAllocSize, fFrame ); - mpeg4Buffer->fKeyFrame = ( fContext->coded_frame->key_frame != 0 ); - - delete fResizedBuffer; + fMpeg4Buffer->fPosition = fResizedBuffer->fPosition; + fMpeg4Buffer->fSize = + avcodec_encode_video( fContext, fMpeg4Buffer->fData, + fMpeg4Buffer->fAllocSize, fFrame ); + fMpeg4Buffer->fKeyFrame = ( fContext->coded_frame->key_frame != 0 ); - if( fPass == 1 ) + if( fResizedBuffer->fPass == 1 ) { if( fContext->stats_out ) { fprintf( fFile, "%s", fContext->stats_out ); } - delete mpeg4Buffer; - } - else - { - /* Mux it */ - Push( fTitle->fMpeg4Fifo, mpeg4Buffer ); + delete fMpeg4Buffer; + fMpeg4Buffer = NULL; } + + delete fResizedBuffer; + fResizedBuffer = NULL; } |