Skip to content

Commit 4565048

Browse files
committed
Merge pull request #14 from valnoel/fix_read_next_packet
Avoid a copy of packet data when readNextPacket
2 parents 920c1cb + 6f99c61 commit 4565048

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

src/AvTranscoder/codedStream/AvInputStream.cpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,32 @@ bool AvInputStream::readNextPacket( CodedData& data )
7272
if( ! _bufferized )
7373
throw std::runtime_error( "Can't read packet on non-bufferized input stream." );
7474

75-
if( _streamCache.empty() )
76-
_inputFile->readNextPacket( _streamIndex );
77-
78-
if( _streamCache.empty() )
79-
return false;
80-
81-
_streamCache.front().getBuffer().swap( data.getBuffer() );
82-
83-
_streamCache.erase( _streamCache.begin() );
75+
// if packet is already cached
76+
if( ! _streamCache.empty() )
77+
{
78+
_streamCache.front().getBuffer().swap( data.getBuffer() );
79+
_streamCache.erase( _streamCache.begin() );
80+
}
81+
// else read next packet
82+
else
83+
{
84+
return _inputFile->readNextPacket( data, _streamIndex ) && _streamCache.empty();
85+
}
8486

8587
return true;
8688
}
8789

8890
void AvInputStream::addPacket( AVPacket& packet )
8991
{
90-
//std::cout << "add packet for stream " << _streamIndex << std::endl;
91-
CodedData data;
92-
_streamCache.push_back( data );
93-
92+
// Do not cache data if the stream is declared as unused in process
9493
if( ! _bufferized )
9594
return;
9695

97-
// is it possible to remove this copy ?
98-
// using : av_packet_unref ?
96+
CodedData data;
97+
_streamCache.push_back( data );
9998
_streamCache.back().getBuffer().resize( packet.size );
10099
if( packet.size != 0 )
101100
memcpy( _streamCache.back().getPtr(), packet.data, packet.size );
102-
103-
// std::vector<unsigned char> tmpData( 0,0 );
104-
// &tmpData[0] = packet.data;
105-
// tmpData.size( packet.size );
106-
107-
// remove reference on packet because it's passed to CodedData
108-
// packet.data = NULL;
109-
// packet.size = 0;
110-
111-
// std::cout << this << " buffer size " << _streamCache.size() << std::endl;
112101
}
113102

114103
VideoCodec& AvInputStream::getVideoCodec()

src/AvTranscoder/file/InputFile.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ AvInputStream& InputFile::getStream( size_t index )
144144
return *_inputStreams.at( index );
145145
}
146146

147-
bool InputFile::readNextPacket( const size_t streamIndex )
147+
bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
148148
{
149149
AVPacket packet;
150150
av_init_packet( &packet );
@@ -157,15 +157,21 @@ bool InputFile::readNextPacket( const size_t streamIndex )
157157
return false;
158158
}
159159

160-
// send packet to stream buffer
161-
_inputStreams.at( packet.stream_index )->addPacket( packet );
162-
163-
// We only read one stream and skip others
160+
// if the packet stream is the expected one
161+
// copy and return the packet data
164162
if( packet.stream_index == (int)streamIndex )
165163
{
164+
data.getBuffer().resize( packet.size );
165+
if( packet.size != 0 )
166+
memcpy( data.getPtr(), packet.data, packet.size );
166167
av_free_packet( &packet );
167168
return true;
168169
}
170+
// else add the packet data to the stream cache
171+
else
172+
{
173+
_inputStreams.at( packet.stream_index )->addPacket( packet );
174+
}
169175

170176
// do not delete these 2 lines
171177
// need to skip packet, delete this one and re-init for reading the next one

src/AvTranscoder/file/InputFile.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ class AvExport InputFile
8686
AVFormatContext& getFormatContext() const { return *_formatContext; }
8787

8888
/**
89-
* @brief Read the next packet for the specified stream
90-
* @note For performances, each readed stream needs to be bufferized using the readStream() method.
91-
* @return if next packet was succefully readed
89+
* @brief Read the next packet of the specified stream
90+
* @param data: data of next packet read
91+
* @note For performances, each read stream needs to be bufferized using the readStream() method.
92+
* @return if next packet was succefully read
9293
**/
93-
bool readNextPacket( const size_t streamIndex );
94+
bool readNextPacket( CodedData& data, const size_t streamIndex );
9495

9596
/**
9697
* @brief Seek input stream at specified frame
9798
* @note clean also buffers in each InputStream
98-
* @return if next packet was succefully readed
99+
* @return if next packet was succefully read
99100
**/
100101
void seekAtFrame( const size_t frame );
101102

0 commit comments

Comments
 (0)