Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c5ba957

Browse files
committedMar 2, 2015
Merge pull request #101 from cchampet/dev_encodersAllocFrameOnce
Encoders: allocate AVFrame once
2 parents b8aaf61 + 5ce9de4 commit c5ba957

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed
 

‎src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,26 @@ namespace avtranscoder
1414

1515
AudioEncoder::AudioEncoder( const std::string& audioCodecName )
1616
: _codec( eCodecTypeEncoder, audioCodecName )
17+
, _frame( NULL )
1718
{
19+
#if LIBAVCODEC_VERSION_MAJOR > 54
20+
_frame = av_frame_alloc();
21+
#else
22+
_frame = avcodec_alloc_frame();
23+
#endif
24+
}
25+
26+
AudioEncoder::~AudioEncoder()
27+
{
28+
#if LIBAVCODEC_VERSION_MAJOR > 54
29+
av_frame_free( &_frame );
30+
#else
31+
#if LIBAVCODEC_VERSION_MAJOR > 53
32+
avcodec_free_frame( &_frame );
33+
#else
34+
av_free( _frame );
35+
#endif
36+
#endif
1837
}
1938

2039
void AudioEncoder::setup()
@@ -24,37 +43,31 @@ void AudioEncoder::setup()
2443

2544
bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
2645
{
27-
#if LIBAVCODEC_VERSION_MAJOR > 54
28-
AVFrame* frame = av_frame_alloc();
29-
#else
30-
AVFrame* frame = avcodec_alloc_frame();
31-
#endif
32-
3346
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
3447

3548
// Set default frame parameters
3649
#if LIBAVCODEC_VERSION_MAJOR > 54
37-
av_frame_unref( frame );
50+
av_frame_unref( _frame );
3851
#else
39-
avcodec_get_frame_defaults( frame );
52+
avcodec_get_frame_defaults( _frame );
4053
#endif
4154

4255
const AudioFrame& sourceAudioFrame = static_cast<const AudioFrame&>( sourceFrame );
4356

44-
frame->nb_samples = sourceAudioFrame.getNbSamples();
45-
frame->format = avCodecContext.sample_fmt;
46-
frame->channel_layout = avCodecContext.channel_layout;
57+
_frame->nb_samples = sourceAudioFrame.getNbSamples();
58+
_frame->format = avCodecContext.sample_fmt;
59+
_frame->channel_layout = avCodecContext.channel_layout;
4760

4861
// we calculate the size of the samples buffer in bytes
49-
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, frame->nb_samples, avCodecContext.sample_fmt, 0 );
62+
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, _frame->nb_samples, avCodecContext.sample_fmt, 0 );
5063
if( bufferSize < 0 )
5164
{
5265
char err[AV_ERROR_MAX_STRING_SIZE];
5366
av_strerror( bufferSize, err, sizeof(err) );
5467
throw std::runtime_error( "Encode audio frame error: buffer size < 0 - " + std::string(err) );
5568
}
5669

57-
int retvalue = avcodec_fill_audio_frame( frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
70+
int retvalue = avcodec_fill_audio_frame( _frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
5871
if( retvalue < 0 )
5972
{
6073
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -79,15 +92,15 @@ bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
7992

8093
#if LIBAVCODEC_VERSION_MAJOR > 53
8194
int gotPacket = 0;
82-
int ret = avcodec_encode_audio2( &avCodecContext, &packet, frame, &gotPacket );
95+
int ret = avcodec_encode_audio2( &avCodecContext, &packet, _frame, &gotPacket );
8396
if( ret != 0 && gotPacket == 0 )
8497
{
8598
char err[AV_ERROR_MAX_STRING_SIZE];
8699
av_strerror( ret, err, sizeof(err) );
87100
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + std::string( err ) );
88101
}
89102
#else
90-
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, frame );
103+
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, _frame );
91104
if( ret < 0 )
92105
{
93106
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -96,16 +109,8 @@ bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
96109
}
97110
#endif
98111

99-
#if LIBAVCODEC_VERSION_MAJOR > 54
100-
av_frame_free( &frame );
112+
#if LIBAVCODEC_VERSION_MAJOR > 53
101113
return ret == 0 && gotPacket == 1;
102-
#else
103-
#if LIBAVCODEC_VERSION_MAJOR > 53
104-
avcodec_free_frame( &frame );
105-
return ret == 0 && gotPacket == 1;
106-
#else
107-
av_free( frame );
108-
#endif
109114
#endif
110115
return ret == 0;
111116
}

‎src/AvTranscoder/encoder/AudioEncoder.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AvExport AudioEncoder : public IEncoder
1212
{
1313
public:
1414
AudioEncoder( const std::string& audioCodecName );
15+
~AudioEncoder();
1516

1617
void setup();
1718

@@ -25,6 +26,7 @@ class AvExport AudioEncoder : public IEncoder
2526

2627
private:
2728
AudioCodec _codec;
29+
AVFrame* _frame; ///< Contains the encoded data to pass to the Frame when encodeFrame
2830
};
2931

3032
}

‎src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,26 @@ namespace avtranscoder
1515

1616
VideoEncoder::VideoEncoder( const std::string& videoCodecName )
1717
: _codec( eCodecTypeEncoder, videoCodecName )
18+
, _frame( NULL )
1819
{
20+
#if LIBAVCODEC_VERSION_MAJOR > 54
21+
_frame = av_frame_alloc();
22+
#else
23+
_frame = avcodec_alloc_frame();
24+
#endif
25+
}
26+
27+
VideoEncoder::~VideoEncoder()
28+
{
29+
#if LIBAVCODEC_VERSION_MAJOR > 54
30+
av_frame_free( &_frame );
31+
#else
32+
#if LIBAVCODEC_VERSION_MAJOR > 53
33+
avcodec_free_frame( &_frame );
34+
#else
35+
av_free( _frame );
36+
#endif
37+
#endif
1938
}
2039

2140
void VideoEncoder::setup()
@@ -26,28 +45,22 @@ void VideoEncoder::setup()
2645

2746
bool VideoEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
2847
{
29-
#if LIBAVCODEC_VERSION_MAJOR > 54
30-
AVFrame* frame = av_frame_alloc();
31-
#else
32-
AVFrame* frame = avcodec_alloc_frame();
33-
#endif
34-
3548
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
3649

3750
// Set default frame parameters
3851
#if LIBAVCODEC_VERSION_MAJOR > 54
39-
av_frame_unref( frame );
52+
av_frame_unref( _frame );
4053
#else
41-
avcodec_get_frame_defaults( frame );
54+
avcodec_get_frame_defaults( _frame );
4255
#endif
4356

4457
const VideoFrame& sourceImageFrame = static_cast<const VideoFrame&>( sourceFrame );
4558

46-
frame->width = avCodecContext.width;
47-
frame->height = avCodecContext.height;
48-
frame->format = avCodecContext.pix_fmt;
59+
_frame->width = avCodecContext.width;
60+
_frame->height = avCodecContext.height;
61+
_frame->format = avCodecContext.pix_fmt;
4962

50-
int bufferSize = avpicture_fill( (AVPicture*)frame, const_cast< unsigned char * >( sourceImageFrame.getData() ), avCodecContext.pix_fmt, avCodecContext.width, avCodecContext.height );
63+
int bufferSize = avpicture_fill( (AVPicture*)_frame, const_cast< unsigned char * >( sourceImageFrame.getData() ), avCodecContext.pix_fmt, avCodecContext.width, avCodecContext.height );
5164
if( bufferSize < 0 )
5265
{
5366
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -72,15 +85,15 @@ bool VideoEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
7285

7386
#if LIBAVCODEC_VERSION_MAJOR > 53
7487
int gotPacket = 0;
75-
int ret = avcodec_encode_video2( &avCodecContext, &packet, frame, &gotPacket );
88+
int ret = avcodec_encode_video2( &avCodecContext, &packet, _frame, &gotPacket );
7689
if( ret != 0 && gotPacket == 0 )
7790
{
7891
char err[AV_ERROR_MAX_STRING_SIZE];
7992
av_strerror( ret, err, sizeof(err) );
8093
throw std::runtime_error( "Encode video frame error: avcodec encode video frame - " + std::string( err ) );
8194
}
8295
#else
83-
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, frame );
96+
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, _frame );
8497
if( ret < 0 )
8598
{
8699
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -89,16 +102,8 @@ bool VideoEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
89102
}
90103
#endif
91104

92-
#if LIBAVCODEC_VERSION_MAJOR > 54
93-
av_frame_free( &frame );
94-
return ret == 0 && gotPacket == 1;
95-
#else
96-
#if LIBAVCODEC_VERSION_MAJOR > 53
97-
avcodec_free_frame( &frame );
105+
#if LIBAVCODEC_VERSION_MAJOR > 53
98106
return ret == 0 && gotPacket == 1;
99-
#else
100-
av_free( frame );
101-
#endif
102107
#endif
103108
return ret == 0;
104109
}

‎src/AvTranscoder/encoder/VideoEncoder.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AvExport VideoEncoder : public IEncoder
1212
{
1313
public:
1414
VideoEncoder( const std::string& videoCodecName );
15+
~VideoEncoder();
1516

1617
void setup();
1718

@@ -25,6 +26,7 @@ class AvExport VideoEncoder : public IEncoder
2526

2627
private:
2728
VideoCodec _codec;
29+
AVFrame* _frame; ///< Contains the encoded data to pass to the Frame when encodeFrame
2830
};
2931

3032
}

0 commit comments

Comments
 (0)
Failed to load comments.