From 342608247c76d809a99bfde52616e0b4bcaf9403 Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 3 Dec 2014 19:10:27 +0100 Subject: [PATCH 1/2] InputFile: into readNextPacket, return expected data (instead of caching it) --- src/AvTranscoder/file/InputFile.cpp | 16 +++++++++++----- src/AvTranscoder/file/InputFile.hpp | 11 ++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/AvTranscoder/file/InputFile.cpp b/src/AvTranscoder/file/InputFile.cpp index e957c473..1c15b859 100644 --- a/src/AvTranscoder/file/InputFile.cpp +++ b/src/AvTranscoder/file/InputFile.cpp @@ -144,7 +144,7 @@ AvInputStream& InputFile::getStream( size_t index ) return *_inputStreams.at( index ); } -bool InputFile::readNextPacket( const size_t streamIndex ) +bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex ) { AVPacket packet; av_init_packet( &packet ); @@ -157,15 +157,21 @@ bool InputFile::readNextPacket( const size_t streamIndex ) return false; } - // send packet to stream buffer - _inputStreams.at( packet.stream_index )->addPacket( packet ); - - // We only read one stream and skip others + // if the packet stream is the expected one + // copy and return the packet data if( packet.stream_index == (int)streamIndex ) { + data.getBuffer().resize( packet.size ); + if( packet.size != 0 ) + memcpy( data.getPtr(), packet.data, packet.size ); av_free_packet( &packet ); return true; } + // else add the packet data to the stream cache + else + { + _inputStreams.at( packet.stream_index )->addPacket( packet ); + } // do not delete these 2 lines // need to skip packet, delete this one and re-init for reading the next one diff --git a/src/AvTranscoder/file/InputFile.hpp b/src/AvTranscoder/file/InputFile.hpp index 6283f977..291000ee 100644 --- a/src/AvTranscoder/file/InputFile.hpp +++ b/src/AvTranscoder/file/InputFile.hpp @@ -86,16 +86,17 @@ class AvExport InputFile AVFormatContext& getFormatContext() const { return *_formatContext; } /** - * @brief Read the next packet for the specified stream - * @note For performances, each readed stream needs to be bufferized using the readStream() method. - * @return if next packet was succefully readed + * @brief Read the next packet of the specified stream + * @param data: data of next packet read + * @note For performances, each read stream needs to be bufferized using the readStream() method. + * @return if next packet was succefully read **/ - bool readNextPacket( const size_t streamIndex ); + bool readNextPacket( CodedData& data, const size_t streamIndex ); /** * @brief Seek input stream at specified frame * @note clean also buffers in each InputStream - * @return if next packet was succefully readed + * @return if next packet was succefully read **/ void seekAtFrame( const size_t frame ); From 6f99c61547716674bb411e8640fb127acaa2a7de Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 3 Dec 2014 19:13:04 +0100 Subject: [PATCH 2/2] AvInputStream: extract data from file only if the stream cache is empty --- .../codedStream/AvInputStream.cpp | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/AvTranscoder/codedStream/AvInputStream.cpp b/src/AvTranscoder/codedStream/AvInputStream.cpp index e1440f06..1a1c3e25 100644 --- a/src/AvTranscoder/codedStream/AvInputStream.cpp +++ b/src/AvTranscoder/codedStream/AvInputStream.cpp @@ -72,43 +72,32 @@ bool AvInputStream::readNextPacket( CodedData& data ) if( ! _bufferized ) throw std::runtime_error( "Can't read packet on non-bufferized input stream." ); - if( _streamCache.empty() ) - _inputFile->readNextPacket( _streamIndex ); - - if( _streamCache.empty() ) - return false; - - _streamCache.front().getBuffer().swap( data.getBuffer() ); - - _streamCache.erase( _streamCache.begin() ); + // if packet is already cached + if( ! _streamCache.empty() ) + { + _streamCache.front().getBuffer().swap( data.getBuffer() ); + _streamCache.erase( _streamCache.begin() ); + } + // else read next packet + else + { + return _inputFile->readNextPacket( data, _streamIndex ) && _streamCache.empty(); + } return true; } void AvInputStream::addPacket( AVPacket& packet ) { - //std::cout << "add packet for stream " << _streamIndex << std::endl; - CodedData data; - _streamCache.push_back( data ); - + // Do not cache data if the stream is declared as unused in process if( ! _bufferized ) return; - // is it possible to remove this copy ? - // using : av_packet_unref ? + CodedData data; + _streamCache.push_back( data ); _streamCache.back().getBuffer().resize( packet.size ); if( packet.size != 0 ) memcpy( _streamCache.back().getPtr(), packet.data, packet.size ); - - // std::vector tmpData( 0,0 ); - // &tmpData[0] = packet.data; - // tmpData.size( packet.size ); - - // remove reference on packet because it's passed to CodedData - // packet.data = NULL; - // packet.size = 0; - - // std::cout << this << " buffer size " << _streamCache.size() << std::endl; } VideoCodec& AvInputStream::getVideoCodec()