Skip to content

Fix read next packet #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions src/AvTranscoder/codedStream/AvInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list)."

Use an std::queue instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, indeed !

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in PR #15

}
// 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<unsigned char> 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()
Expand Down
16 changes: 11 additions & 5 deletions src/AvTranscoder/file/InputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a function into Frame (CodedData) instead of duplicating this code, no?

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
Expand Down
11 changes: 6 additions & 5 deletions src/AvTranscoder/file/InputFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not clear...

* @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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@warning It will clear the buffering of all InputStream.

* @return if next packet was succefully readed
* @return if next packet was succefully read

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return if next packet was read successfully

**/
void seekAtFrame( const size_t frame );

Expand Down