Skip to content

Commit 58621e7

Browse files
author
Clement Champetier
committed
CodecContext could allocate a default context, or setup one from an AVCodec
* Update optionChecker app.
1 parent 40da88a commit 58621e7

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ void optionChecker( const std::string& inputfilename )
7373
displayOptions( formatOptions );
7474

7575
// codec options
76-
AVCodecContext* avCodecContext = avcodec_alloc_context3( NULL );
77-
avtranscoder::CodecContext codecContext( *avCodecContext, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM );
76+
avtranscoder::CodecContext codecContext( AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM );
7877
std::vector<avtranscoder::Option> codecOptions = codecContext.getOptions();
7978
displayOptions( codecOptions );
8079

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
#include "CodecContext.hpp"
22

3+
#include <stdexcept>
4+
35
namespace avtranscoder
46
{
57

6-
CodecContext::CodecContext( AVCodecContext& avCodecContext, int req_flags )
7-
: Context( &avCodecContext, req_flags )
8-
{}
8+
CodecContext::CodecContext( AVCodec& avCodec, int req_flags )
9+
: Context( NULL, req_flags )
10+
{
11+
_avContext = avcodec_alloc_context3( &avCodec );
12+
if( ! _avContext )
13+
{
14+
throw std::runtime_error( "unable to allocate the codecContext and set its fields to default values" );
15+
}
16+
17+
// Set default codec parameters
18+
if( avcodec_get_context_defaults3( &getAVCodecContext(), &avCodec ) != 0 )
19+
{
20+
throw std::runtime_error( "unable to find set codecContext to default values corresponding to the given codec" );
21+
}
22+
23+
loadOptions( _avContext, req_flags );
24+
}
25+
26+
CodecContext::CodecContext( int req_flags )
27+
: Context( NULL, req_flags )
28+
{
29+
_avContext = avcodec_alloc_context3( NULL );
30+
loadOptions( _avContext, req_flags );
31+
}
32+
33+
CodecContext::~CodecContext()
34+
{
35+
if( ! _avContext )
36+
return;
37+
38+
avcodec_close( &getAVCodecContext() );
39+
av_free( &getAVCodecContext() );
40+
_avContext = NULL;
41+
}
942

1043
}

src/AvTranscoder/option/CodecContext.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ namespace avtranscoder
1616
class AvExport CodecContext : public Context
1717
{
1818
public:
19-
CodecContext( AVCodecContext& avCodecContext, int req_flags = 0 );
19+
CodecContext( AVCodec& avCodec, int req_flags = 0 ); ///< Allocate an AVCodecContext with the given AVCodec
20+
CodecContext( int req_flags = 0 ); ///< Allocate an AVCodecContext with default values
21+
~CodecContext();
2022

2123
#ifndef SWIG
2224
AVCodecContext& getAVCodecContext() const { return *static_cast<AVCodecContext*>( _avContext ); }

0 commit comments

Comments
 (0)