Skip to content

Commit 725be7e

Browse files
author
Clement Champetier
committed
ICodec: getAVCodec and getAVCodecContext return a reference
No more getter of pointer in the API.
1 parent 5095d8c commit 725be7e

File tree

7 files changed

+81
-79
lines changed

7 files changed

+81
-79
lines changed

src/AvTranscoder/codec/ICodec.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class AvExport ICodec
3434
void setCodec( const ECodecType type, const AVCodecID codecId );
3535

3636
#ifndef SWIG
37-
AVCodec* getAVCodec() const { return _codec; }
38-
AVCodecContext* getAVCodecContext() const { return _codecContext; }
37+
AVCodec& getAVCodec() const { return *_codec; }
38+
AVCodecContext& getAVCodecContext() const { return *_codecContext; }
3939
#endif
4040

4141
private:

src/AvTranscoder/essenceStream/AvInputAudio.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ AvInputAudio::~AvInputAudio()
4545

4646
void AvInputAudio::setup()
4747
{
48-
AVCodecContext* avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
49-
AVCodec* avCodec = _inputStream->getAudioCodec().getAVCodec();
48+
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
49+
AVCodec& avCodec = _inputStream->getAudioCodec().getAVCodec();
5050

51-
avCodecContext->channels = _inputStream->getAudioCodec().getAudioFrameDesc().getChannels();
51+
avCodecContext.channels = _inputStream->getAudioCodec().getAudioFrameDesc().getChannels();
5252

53-
int ret = avcodec_open2( avCodecContext, avCodec, NULL );
53+
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
5454

55-
if( ret < 0 || avCodecContext == NULL || avCodec == NULL )
55+
if( ret < 0 || &avCodecContext == NULL || &avCodec == NULL )
5656
{
5757
std::string msg = "unable open audio codec: ";
58-
msg += avCodec->long_name;
58+
msg += avCodec.long_name;
5959
msg += " (";
60-
msg += avCodec->name;
60+
msg += avCodec.name;
6161
msg += ") ";
62-
avcodec_close( avCodecContext );
62+
avcodec_close( &avCodecContext );
6363

6464
char err[AV_ERROR_MAX_STRING_SIZE];
6565
av_strerror( ret, err, sizeof(err) );
@@ -84,9 +84,9 @@ bool AvInputAudio::decodeNextFrame( Frame& frameBuffer )
8484
if( ! decodeNextFrame() )
8585
return false;
8686

87-
AVCodecContext* avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
87+
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
8888

89-
size_t decodedSize = av_samples_get_buffer_size( NULL, avCodecContext->channels, _frame->nb_samples, avCodecContext->sample_fmt, 1 );
89+
size_t decodedSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, _frame->nb_samples, avCodecContext.sample_fmt, 1 );
9090

9191
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
9292

@@ -101,7 +101,7 @@ bool AvInputAudio::decodeNextFrame( Frame& frameBuffer )
101101
unsigned char* const src = _frame->data[0];
102102
unsigned char* dst = audioBuffer.getPtr();
103103

104-
av_samples_copy( &dst, &src, 0, 0, _frame->nb_samples, avCodecContext->channels, avCodecContext->sample_fmt );
104+
av_samples_copy( &dst, &src, 0, 0, _frame->nb_samples, avCodecContext.channels, avCodecContext.sample_fmt );
105105
}
106106

107107
return true;
@@ -112,11 +112,13 @@ bool AvInputAudio::decodeNextFrame( Frame& frameBuffer, const size_t subStreamIn
112112
if( ! decodeNextFrame() )
113113
return false;
114114

115+
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
116+
115117
const int output_nbChannels = 1;
116118
const int output_align = 1;
117-
size_t decodedSize = av_samples_get_buffer_size(NULL, output_nbChannels, _frame->nb_samples, _inputStream->getAudioCodec().getAVCodecContext()->sample_fmt, output_align);
119+
size_t decodedSize = av_samples_get_buffer_size(NULL, output_nbChannels, _frame->nb_samples, avCodecContext.sample_fmt, output_align);
118120

119-
size_t nbSubStreams = _inputStream->getAudioCodec().getAVCodecContext()->channels;
121+
size_t nbSubStreams = avCodecContext.channels;
120122
size_t bytePerSample = av_get_bytes_per_sample( (AVSampleFormat)_frame->format );
121123

122124
if( subStreamIndex > nbSubStreams - 1 )
@@ -165,7 +167,7 @@ bool AvInputAudio::decodeNextFrame()
165167
packet.data = nextPacketRead ? data.getPtr(): NULL;
166168
packet.size = data.getSize();
167169

168-
int ret = avcodec_decode_audio4( _inputStream->getAudioCodec().getAVCodecContext(), _frame, &got_frame, &packet );
170+
int ret = avcodec_decode_audio4( &_inputStream->getAudioCodec().getAVCodecContext(), _frame, &got_frame, &packet );
169171
av_free_packet( &packet );
170172

171173
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // error or end of file

src/AvTranscoder/essenceStream/AvInputVideo.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ AvInputVideo::~AvInputVideo()
4545

4646
void AvInputVideo::setup()
4747
{
48-
AVCodecContext* avCodecContext = _inputStream->getVideoCodec().getAVCodecContext();
49-
AVCodec* avCodec = _inputStream->getVideoCodec().getAVCodec();
48+
AVCodecContext& avCodecContext = _inputStream->getVideoCodec().getAVCodecContext();
49+
AVCodec& avCodec = _inputStream->getVideoCodec().getAVCodec();
5050

5151
// if( avCodec->capabilities & CODEC_CAP_TRUNCATED )
5252
// avCodecContext->flags |= CODEC_FLAG_TRUNCATED;
5353

54-
int ret = avcodec_open2( avCodecContext, avCodec, NULL );
54+
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
5555

56-
if( ret < 0 || avCodecContext == NULL || avCodec == NULL )
56+
if( ret < 0 || &avCodecContext == NULL || &avCodec == NULL )
5757
{
5858
std::string msg = "unable open video codec: ";
59-
msg += avCodec->long_name;
59+
msg += avCodec.long_name;
6060
msg += " (";
61-
msg += avCodec->name;
61+
msg += avCodec.name;
6262
msg += ")";
63-
avcodec_close( avCodecContext );
63+
avcodec_close( &avCodecContext );
6464
throw std::runtime_error( msg );
6565
}
6666

@@ -116,7 +116,7 @@ bool AvInputVideo::decodeNextFrame()
116116
packet.data = nextPacketRead ? data.getPtr(): NULL;
117117
packet.size = data.getSize();
118118

119-
int ret = avcodec_decode_video2( _inputStream->getVideoCodec().getAVCodecContext(), _frame, &got_frame, &packet );
119+
int ret = avcodec_decode_video2( &_inputStream->getVideoCodec().getAVCodecContext(), _frame, &got_frame, &packet );
120120
av_free_packet( &packet );
121121

122122
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // error or end of file
@@ -134,12 +134,12 @@ bool AvInputVideo::decodeNextFrame()
134134

135135
void AvInputVideo::flushDecoder()
136136
{
137-
avcodec_flush_buffers( _inputStream->getVideoCodec().getAVCodecContext() );
137+
avcodec_flush_buffers( &_inputStream->getVideoCodec().getAVCodecContext() );
138138
}
139139

140140
void AvInputVideo::setProfile( const ProfileLoader::Profile& profile )
141141
{
142-
Context codecContext( _inputStream->getVideoCodec().getAVCodecContext(), AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM );
142+
Context codecContext( &_inputStream->getVideoCodec().getAVCodecContext(), AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM );
143143

144144
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
145145
{

src/AvTranscoder/essenceStream/AvOutputAudio.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ void AvOutputAudio::setup()
2424
{
2525
av_register_all();
2626

27-
AVCodecContext* codecContext( _codec.getAVCodecContext() );
27+
AVCodecContext& avCodecContext( _codec.getAVCodecContext() );
2828

29-
if( codecContext == NULL )
29+
if( &avCodecContext == NULL )
3030
{
3131
throw std::runtime_error( "could not allocate audio codec context" );
3232
}
3333

3434
// try to open encoder with parameters.
35-
int ret = avcodec_open2( codecContext, _codec.getAVCodec(), NULL );
35+
int ret = avcodec_open2( &avCodecContext, &_codec.getAVCodec(), NULL );
3636
if( ret < 0 )
3737
{
3838
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -51,7 +51,7 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
5151
AVFrame* frame = avcodec_alloc_frame();
5252
#endif
5353

54-
AVCodecContext* codecContext = _codec.getAVCodecContext();
54+
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
5555

5656
// Set default frame parameters
5757
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -63,19 +63,19 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
6363
const AudioFrame& sourceAudioFrame = static_cast<const AudioFrame&>( sourceFrame );
6464

6565
frame->nb_samples = sourceAudioFrame.getNbSamples();
66-
frame->format = codecContext->sample_fmt;
67-
frame->channel_layout = codecContext->channel_layout;
66+
frame->format = avCodecContext.sample_fmt;
67+
frame->channel_layout = avCodecContext.channel_layout;
6868

6969
// we calculate the size of the samples buffer in bytes
70-
int buffer_size = av_samples_get_buffer_size( NULL, codecContext->channels, frame->nb_samples, codecContext->sample_fmt, 0 );
70+
int buffer_size = av_samples_get_buffer_size( NULL, avCodecContext.channels, frame->nb_samples, avCodecContext.sample_fmt, 0 );
7171
if( buffer_size < 0 )
7272
{
7373
char err[AV_ERROR_MAX_STRING_SIZE];
7474
av_strerror( buffer_size, err, sizeof(err) );
7575
throw std::runtime_error( "EncodeFrame error: buffer size < 0 - " + std::string(err) );
7676
}
7777

78-
int retvalue = avcodec_fill_audio_frame( frame, codecContext->channels, codecContext->sample_fmt, sourceAudioFrame.getPtr(), buffer_size, 0 );
78+
int retvalue = avcodec_fill_audio_frame( frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getPtr(), buffer_size, 0 );
7979
if( retvalue < 0 )
8080
{
8181
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -90,27 +90,27 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
9090
packet.data = NULL;
9191
packet.stream_index = 0;
9292

93-
if( ( codecContext->coded_frame ) &&
94-
( codecContext->coded_frame->pts != (int)AV_NOPTS_VALUE ) )
93+
if( ( avCodecContext.coded_frame ) &&
94+
( avCodecContext.coded_frame->pts != (int)AV_NOPTS_VALUE ) )
9595
{
96-
packet.pts = codecContext->coded_frame->pts;
96+
packet.pts = avCodecContext.coded_frame->pts;
9797
}
9898

99-
if( codecContext->coded_frame &&
100-
codecContext->coded_frame->key_frame )
99+
if( avCodecContext.coded_frame &&
100+
avCodecContext.coded_frame->key_frame )
101101
{
102102
packet.flags |= AV_PKT_FLAG_KEY;
103103
}
104104

105105
#if LIBAVCODEC_VERSION_MAJOR > 53
106106
int gotPacket = 0;
107-
int ret = avcodec_encode_audio2( codecContext, &packet, frame, &gotPacket );
107+
int ret = avcodec_encode_audio2( &avCodecContext, &packet, frame, &gotPacket );
108108
if( ret == 0 && gotPacket == 1 )
109109
{
110110
codedFrame.copyData( packet.data, packet.size );
111111
}
112112
#else
113-
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, frame );
113+
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, frame );
114114
if( ret > 0 )
115115
{
116116
codedFrame.copyData( packet.data, packet.size );
@@ -135,7 +135,7 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
135135

136136
bool AvOutputAudio::encodeFrame( Frame& codedFrame )
137137
{
138-
AVCodecContext* codecContext = _codec.getAVCodecContext();
138+
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
139139

140140
AVPacket packet;
141141
av_init_packet( &packet );
@@ -146,7 +146,7 @@ bool AvOutputAudio::encodeFrame( Frame& codedFrame )
146146

147147
#if LIBAVCODEC_VERSION_MAJOR > 53
148148
int gotPacket = 0;
149-
int ret = avcodec_encode_audio2( codecContext, &packet, NULL, &gotPacket );
149+
int ret = avcodec_encode_audio2( &avCodecContext, &packet, NULL, &gotPacket );
150150
if( ret == 0 && gotPacket == 1 )
151151
{
152152
codedFrame.copyData( packet.data, packet.size );
@@ -155,7 +155,7 @@ bool AvOutputAudio::encodeFrame( Frame& codedFrame )
155155
return ret == 0 && gotPacket == 1;
156156

157157
#else
158-
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, NULL );
158+
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, NULL );
159159
if( ret > 0 )
160160
{
161161
codedFrame.copyData( packet.data, packet.size );
@@ -176,7 +176,7 @@ void AvOutputAudio::setProfile( const ProfileLoader::Profile& profile, const Aud
176176
_codec.setCodec( eCodecTypeEncoder, profile.find( constants::avProfileCodec )->second );
177177
_codec.setAudioParameters( frameDesc );
178178

179-
Context codecContext( _codec.getAVCodecContext(), AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM );
179+
Context codecContext( &_codec.getAVCodecContext(), AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM );
180180

181181
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
182182
{

src/AvTranscoder/essenceStream/AvOutputVideo.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ void AvOutputVideo::setup( )
2525
{
2626
av_register_all();
2727

28-
AVCodecContext* codecContext( _codec.getAVCodecContext() );
28+
AVCodecContext& avCodecContext( _codec.getAVCodecContext() );
2929

30-
if( codecContext == NULL )
30+
if( &avCodecContext == NULL )
3131
{
3232
throw std::runtime_error( "could not allocate video codec context" );
3333
}
3434

3535
// try to open encoder with parameters
36-
int ret = avcodec_open2( codecContext, _codec.getAVCodec(), NULL );
36+
int ret = avcodec_open2( &avCodecContext, &_codec.getAVCodec(), NULL );
3737
if( ret < 0 )
3838
{
3939
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -53,7 +53,7 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
5353
AVFrame* frame = avcodec_alloc_frame();
5454
#endif
5555

56-
AVCodecContext* codecContext = _codec.getAVCodecContext();
56+
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
5757

5858
// Set default frame parameters
5959
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -64,10 +64,10 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
6464

6565
const VideoFrame& sourceImageFrame = static_cast<const VideoFrame&>( sourceFrame );
6666

67-
frame->width = codecContext->width;
68-
frame->height = codecContext->height;
69-
frame->format = codecContext->pix_fmt;
70-
avpicture_fill( (AVPicture*)frame, const_cast< unsigned char * >( sourceImageFrame.getPtr() ), codecContext->pix_fmt, codecContext->width, codecContext->height );
67+
frame->width = avCodecContext.width;
68+
frame->height = avCodecContext.height;
69+
frame->format = avCodecContext.pix_fmt;
70+
avpicture_fill( (AVPicture*)frame, const_cast< unsigned char * >( sourceImageFrame.getPtr() ), avCodecContext.pix_fmt, avCodecContext.width, avCodecContext.height );
7171

7272
AVPacket packet;
7373
av_init_packet( &packet );
@@ -76,27 +76,27 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
7676
packet.data = NULL;
7777
packet.stream_index = 0;
7878

79-
if( ( codecContext->coded_frame ) &&
80-
( codecContext->coded_frame->pts != (int)AV_NOPTS_VALUE ) )
79+
if( ( avCodecContext.coded_frame ) &&
80+
( avCodecContext.coded_frame->pts != (int)AV_NOPTS_VALUE ) )
8181
{
82-
packet.pts = codecContext->coded_frame->pts;
82+
packet.pts = avCodecContext.coded_frame->pts;
8383
}
8484

85-
if( codecContext->coded_frame &&
86-
codecContext->coded_frame->key_frame )
85+
if( avCodecContext.coded_frame &&
86+
avCodecContext.coded_frame->key_frame )
8787
{
8888
packet.flags |= AV_PKT_FLAG_KEY;
8989
}
9090

9191
#if LIBAVCODEC_VERSION_MAJOR > 53
9292
int gotPacket = 0;
93-
int ret = avcodec_encode_video2( codecContext, &packet, frame, &gotPacket );
93+
int ret = avcodec_encode_video2( &avCodecContext, &packet, frame, &gotPacket );
9494
if( ret == 0 && gotPacket == 1 )
9595
{
9696
codedFrame.copyData( packet.data, packet.size );
9797
}
9898
#else
99-
int ret = avcodec_encode_video( codecContext, packet.data, packet.size, frame );
99+
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, frame );
100100
if( ret > 0 )
101101
{
102102
codedFrame.copyData( packet.data, packet.size );
@@ -120,7 +120,7 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
120120

121121
bool AvOutputVideo::encodeFrame( Frame& codedFrame )
122122
{
123-
AVCodecContext* codecContext = _codec.getAVCodecContext();
123+
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
124124

125125
AVPacket packet;
126126
av_init_packet( &packet );
@@ -131,15 +131,15 @@ bool AvOutputVideo::encodeFrame( Frame& codedFrame )
131131

132132
#if LIBAVCODEC_VERSION_MAJOR > 53
133133
int gotPacket = 0;
134-
int ret = avcodec_encode_video2( codecContext, &packet, NULL, &gotPacket );
134+
int ret = avcodec_encode_video2( &avCodecContext, &packet, NULL, &gotPacket );
135135
if( ret == 0 && gotPacket == 1 )
136136
{
137137
codedFrame.copyData( packet.data, packet.size );
138138
}
139139
av_free_packet( &packet );
140140
return ret == 0 && gotPacket == 1;
141141
#else
142-
int ret = avcodec_encode_video( codecContext, packet.data, packet.size, NULL );
142+
int ret = avcodec_encode_video( &avCodecContext, packet.data, packet.size, NULL );
143143
if( ret > 0 )
144144
{
145145
codedFrame.copyData( packet.data, packet.size );
@@ -164,7 +164,7 @@ void AvOutputVideo::setProfile( const ProfileLoader::Profile& profile, const avt
164164

165165
_codec.setImageParameters( frameDesc );
166166

167-
Context codecContext( _codec.getAVCodecContext(), AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM );
167+
Context codecContext( &_codec.getAVCodecContext(), AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM );
168168

169169
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
170170
{

0 commit comments

Comments
 (0)