From 452f36599a1ea52f3bd42361b534594568c428fd Mon Sep 17 00:00:00 2001 From: handbrake Date: Sat, 14 Jan 2006 12:56:59 +0000 Subject: HandBrake 0.4 git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- core/Mpeg4Encoder.cpp | 114 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 40 deletions(-) (limited to 'core/Mpeg4Encoder.cpp') 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: . @@ -11,49 +11,97 @@ #include 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; } -- cgit v1.2.3