Audacity 3.2.0
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
AVCodecContextWrapper Class Referenceabstract

#include <AVCodecContextWrapper.h>

Collaboration diagram for AVCodecContextWrapper:
[legend]

Public Member Functions

 AVCodecContextWrapper (const AVCodecContextWrapper &)=delete
 
AVCodecContextWrapperoperator= (AVCodecContextWrapper &)=delete
 
 AVCodecContextWrapper (AVCodecContextWrapper &&)=delete
 
AVCodecContextWrapperoperator= (AVCodecContextWrapper &&)=delete
 
 AVCodecContextWrapper (const FFmpegFunctions &ffmpeg, std::unique_ptr< AVCodecWrapper > codec) noexcept
 
 AVCodecContextWrapper (const FFmpegFunctions &ffmpeg, AVCodecContext *wrapped) noexcept
 
AVCodecContextGetWrappedValue () noexcept
 
const AVCodecContextGetWrappedValue () const noexcept
 
virtual ~AVCodecContextWrapper ()
 
std::vector< uint8_t > DecodeAudioPacket (const AVPacketWrapper *packet)
 
virtual sampleFormat GetPreferredAudacitySampleFormat () const noexcept=0
 
virtual std::vector< int16_t > DecodeAudioPacketInt16 (const AVPacketWrapper *packet)=0
 
virtual std::vector< float > DecodeAudioPacketFloat (const AVPacketWrapper *packet)=0
 
virtual int GetBitRate () const noexcept=0
 
virtual void SetBitRate (int value) noexcept=0
 
virtual const AVChannelLayoutWrapperGetChannelLayout () const noexcept=0
 
virtual void SetChannelLayout (const AVChannelLayoutWrapper *value) noexcept=0
 
virtual int GetChannels () const noexcept=0
 
virtual const AVCodecWrapperGetCodec () const noexcept=0
 
virtual AVCodecIDFwd GetCodecId () const noexcept=0
 
void SetCodecTagFourCC (const char *fourCC) noexcept
 
virtual void SetCodecTag (unsigned int tag) noexcept=0
 
virtual unsigned int GetCodecTag () const noexcept=0
 
virtual AVMediaTypeFwd GetCodecType () const noexcept=0
 
virtual int GetCompressionLevel () const noexcept=0
 
virtual void SetCompressionLevel (int value) noexcept=0
 
virtual int GetCutoff () const noexcept=0
 
virtual void SetCutoff (int value) noexcept=0
 
virtual int GetFlags () const noexcept=0
 
virtual void SetFlags (int value) noexcept=0
 
virtual int GetFlags2 () const noexcept=0
 
virtual void SetFlags2 (int value) noexcept=0
 
virtual int GetFrameNumber () const noexcept=0
 
virtual int GetFrameSize () const noexcept=0
 
virtual void SetFrameSize (int value) noexcept=0
 
virtual int GetGlobalQuality () const noexcept=0
 
virtual void SetGlobalQuality (int value) noexcept=0
 
virtual int GetProfile () const noexcept=0
 
virtual void SetProfile (int value) noexcept=0
 
virtual AVSampleFormatFwd GetSampleFmt () const noexcept=0
 
virtual void SetSampleFmt (AVSampleFormatFwd value) noexcept=0
 
virtual int GetSampleRate () const noexcept=0
 
virtual void SetSampleRate (int value) noexcept=0
 
virtual int GetStrictStdCompliance () const noexcept=0
 
virtual void SetStrictStdCompliance (int value) noexcept=0
 
virtual struct AudacityAVRational GetTimeBase () const noexcept=0
 
virtual void SetTimeBase (struct AudacityAVRational value) noexcept=0
 
virtual int Open (const AVCodecWrapper *codec, AVDictionaryWrapper *options=nullptr)=0
 

Protected Attributes

const FFmpegFunctionsmFFmpeg
 
AVCodecContextmAVCodecContext { nullptr }
 
std::unique_ptr< AVCodecWrappermAVCodec
 
bool mIsOwned { false }
 

Private Member Functions

void ConsumeFrame (std::vector< uint8_t > &data, AVFrameWrapper &frame)
 

Detailed Description

Definition at line 30 of file AVCodecContextWrapper.h.

Constructor & Destructor Documentation

◆ AVCodecContextWrapper() [1/4]

AVCodecContextWrapper::AVCodecContextWrapper ( const AVCodecContextWrapper )
delete

◆ AVCodecContextWrapper() [2/4]

AVCodecContextWrapper::AVCodecContextWrapper ( AVCodecContextWrapper &&  )
delete

◆ AVCodecContextWrapper() [3/4]

AVCodecContextWrapper::AVCodecContextWrapper ( const FFmpegFunctions ffmpeg,
std::unique_ptr< AVCodecWrapper codec 
)
noexcept

Definition at line 18 of file AVCodecContextWrapper.cpp.

20 : mFFmpeg(ffmpeg)
21 , mAVCodec(std::move(codec))
22 , mIsOwned(true)
23{
25 mFFmpeg.avcodec_alloc_context3(mAVCodec->GetWrappedValue());
26}
std::unique_ptr< AVCodecWrapper > mAVCodec
AVCodecContext * mAVCodecContext
const FFmpegFunctions & mFFmpeg
AVCodecContext *(* avcodec_alloc_context3)(const AVCodec *codec)

◆ AVCodecContextWrapper() [4/4]

AVCodecContextWrapper::AVCodecContextWrapper ( const FFmpegFunctions ffmpeg,
AVCodecContext wrapped 
)
noexcept

Definition at line 28 of file AVCodecContextWrapper.cpp.

30 : mFFmpeg(ffmpeg)
31 , mAVCodecContext(wrapped)
32 , mIsOwned(false)
33{
34
35}

◆ ~AVCodecContextWrapper()

AVCodecContextWrapper::~AVCodecContextWrapper ( )
virtual

Definition at line 47 of file AVCodecContextWrapper.cpp.

48{
49 if (mIsOwned && mAVCodecContext != nullptr)
50 {
51 // avcodec_free_context, complementary to avcodec_alloc_context3, is
52 // not necessarily loaded
53 if (mFFmpeg.avcodec_free_context != nullptr)
54 {
56 }
57 else
58 {
59 // Its not clear how to avoid the leak here, but let's close
60 // the codec at least
63 }
64 }
65}
int(* avcodec_close)(AVCodecContext *avctx)
int(* avcodec_is_open)(AVCodecContext *avctx)
void(* avcodec_free_context)(AVCodecContext **avctx)

References AVCodecFunctions::avcodec_close, AVCodecFunctions::avcodec_free_context, AVCodecFunctions::avcodec_is_open, mAVCodecContext, mFFmpeg, and mIsOwned.

Member Function Documentation

◆ ConsumeFrame()

void AVCodecContextWrapper::ConsumeFrame ( std::vector< uint8_t > &  data,
AVFrameWrapper frame 
)
private

Definition at line 150 of file AVCodecContextWrapper.cpp.

152{
153 const int channels = GetChannels();
154
155 const auto sampleSize = static_cast<size_t>(mFFmpeg.av_get_bytes_per_sample(
156 static_cast<AVSampleFormatFwd>(frame.GetFormat())));
157
158 const auto samplesCount = frame.GetSamplesCount();
159 const auto frameSize = channels * sampleSize * samplesCount;
160
161 auto oldSize = data.size();
162 data.resize(oldSize + frameSize);
163 auto pData = &data[oldSize];
164
165 if (frame.GetData(1) != nullptr)
166 {
167 // We return interleaved buffer
168 for (int channel = 0; channel < channels; channel++)
169 {
170 for (int sample = 0; sample < samplesCount; sample++)
171 {
172 const uint8_t* channelData =
173 frame.GetExtendedData(channel) + sampleSize * sample;
174
175 uint8_t* output =
176 pData + sampleSize * (channels * sample + channel);
177
178 std::copy(channelData, channelData + sampleSize, output);
179 }
180 }
181 }
182 else
183 {
184 uint8_t* frameData = frame.GetData(0);
185 std::copy(frameData, frameData + frameSize, pData);
186 }
187}
int AVSampleFormatFwd
Definition: FFmpegTypes.h:132
virtual int GetChannels() const noexcept=0
virtual AVSampleFormatFwd GetFormat() const noexcept=0
virtual int GetSamplesCount() const noexcept=0
virtual uint8_t * GetData(int index) const noexcept=0
virtual uint8_t * GetExtendedData(int index) const noexcept=0
void copy(const T *src, T *dst, int32_t n)
Definition: VectorOps.h:40
int(* av_get_bytes_per_sample)(AVSampleFormatFwd sample_fmt)

References AVUtilFunctions::av_get_bytes_per_sample, staffpad::vo::copy(), GetChannels(), AVFrameWrapper::GetData(), AVFrameWrapper::GetExtendedData(), AVFrameWrapper::GetFormat(), AVFrameWrapper::GetSamplesCount(), and mFFmpeg.

Referenced by DecodeAudioPacket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DecodeAudioPacket()

std::vector< uint8_t > AVCodecContextWrapper::DecodeAudioPacket ( const AVPacketWrapper packet)

Definition at line 68 of file AVCodecContextWrapper.cpp.

69{
70 auto frame = mFFmpeg.CreateAVFrameWrapper();
71 std::vector<uint8_t> data;
72
73 if (mFFmpeg.avcodec_send_packet == nullptr)
74 {
75 std::unique_ptr<AVPacketWrapper> packetCopy =
76 packet ? packet->Clone() : mFFmpeg.CreateAVPacketWrapper();
77
78 /*
79 "Flushing is done by calling this function [avcodec_decode_audio4]
80 with packets with avpkt->data set to NULL and avpkt->size set to 0
81 until it stops returning samples."
82 (That implies, not necessarily just one loop pass to flush)
83 */
84 bool flushing = packet
85 ? (packetCopy->GetSize() == 0 && packetCopy->GetData() == nullptr)
86 : true;
87 if (!flushing && packetCopy->GetData() == nullptr)
88 return {};
89
90 int bytesDecoded = 0;
91
92 do
93 {
94 int gotFrame;
95 // Deprecated? https://ffmpeg.org/doxygen/3.3/group__lavc__decoding.html#gaaa1fbe477c04455cdc7a994090100db4
96 bytesDecoded = mFFmpeg.avcodec_decode_audio4(
97 mAVCodecContext, frame->GetWrappedValue(), &gotFrame,
98 packetCopy->GetWrappedValue());
99
100 if (bytesDecoded < 0)
101 return data; // Packet decoding has failed
102
103 if (gotFrame == 0)
104 {
105 /*
106 "Note that this field being set to zero does not mean that an
107 error has occurred. For decoders with AV_CODEC_CAP_DELAY set, no
108 given decode call is guaranteed to produce a frame."
109 */
110 // (Let's assume this doesn't happen when flushing)
111 // Still, the data was consumed by the decoder, so we need to
112 // offset the packet
113 packetCopy->OffsetPacket(bytesDecoded);
114 continue;
115 }
116
117 ConsumeFrame(data, *frame);
118
119 packetCopy->OffsetPacket(bytesDecoded);
120 }
121 while ( flushing ? bytesDecoded > 0 : packetCopy->GetSize() > 0 );
122 }
123 else
124 {
125 auto ret = mFFmpeg.avcodec_send_packet(
127 packet != nullptr ? packet->GetWrappedValue() : nullptr);
128
129 if (ret < 0)
130 // send_packet has failed
131 return data;
132
133 while (ret >= 0)
134 {
135 ret = mFFmpeg.avcodec_receive_frame(mAVCodecContext, frame->GetWrappedValue());
136 if (ret == AUDACITY_AVERROR(EAGAIN) || ret == AUDACITY_AVERROR_EOF)
137 // The packet is fully consumed OR more data is needed
138 break;
139 else if (ret < 0)
140 // Decoding has failed
141 return data;
142
143 ConsumeFrame(data, *frame);
144 }
145 }
146
147 return data;
148}
#define AUDACITY_AVERROR(e)
Definition: FFmpegTypes.h:28
#define AUDACITY_AVERROR_EOF
Definition: FFmpegTypes.h:32
void ConsumeFrame(std::vector< uint8_t > &data, AVFrameWrapper &frame)
AVPacket * GetWrappedValue() noexcept
virtual std::unique_ptr< AVPacketWrapper > Clone() const noexcept=0
int(* avcodec_receive_frame)(AVCodecContext *avctx, AVFrame *frame)
int(* avcodec_send_packet)(AVCodecContext *avctx, const AVPacket *avpkt)
int(* avcodec_decode_audio4)(AVCodecContext *avctx, AVFrame *frame, int *got_output, const AVPacket *avpkt)
std::unique_ptr< AVFrameWrapper > CreateAVFrameWrapper() const
std::unique_ptr< AVPacketWrapper > CreateAVPacketWrapper() const

References AUDACITY_AVERROR, AUDACITY_AVERROR_EOF, AVCodecFunctions::avcodec_decode_audio4, AVCodecFunctions::avcodec_receive_frame, AVCodecFunctions::avcodec_send_packet, AVPacketWrapper::Clone(), ConsumeFrame(), FFmpegFunctions::CreateAVFrameWrapper(), FFmpegFunctions::CreateAVPacketWrapper(), AVPacketWrapper::GetWrappedValue(), mAVCodecContext, and mFFmpeg.

Here is the call graph for this function:

◆ DecodeAudioPacketFloat()

virtual std::vector< float > AVCodecContextWrapper::DecodeAudioPacketFloat ( const AVPacketWrapper packet)
pure virtual

◆ DecodeAudioPacketInt16()

virtual std::vector< int16_t > AVCodecContextWrapper::DecodeAudioPacketInt16 ( const AVPacketWrapper packet)
pure virtual

◆ GetBitRate()

virtual int AVCodecContextWrapper::GetBitRate ( ) const
pure virtualnoexcept

◆ GetChannelLayout()

virtual const AVChannelLayoutWrapper * AVCodecContextWrapper::GetChannelLayout ( ) const
pure virtualnoexcept

◆ GetChannels()

virtual int AVCodecContextWrapper::GetChannels ( ) const
pure virtualnoexcept

Referenced by ConsumeFrame().

Here is the caller graph for this function:

◆ GetCodec()

virtual const AVCodecWrapper * AVCodecContextWrapper::GetCodec ( ) const
pure virtualnoexcept

◆ GetCodecId()

virtual AVCodecIDFwd AVCodecContextWrapper::GetCodecId ( ) const
pure virtualnoexcept

◆ GetCodecTag()

virtual unsigned int AVCodecContextWrapper::GetCodecTag ( ) const
pure virtualnoexcept

◆ GetCodecType()

virtual AVMediaTypeFwd AVCodecContextWrapper::GetCodecType ( ) const
pure virtualnoexcept

◆ GetCompressionLevel()

virtual int AVCodecContextWrapper::GetCompressionLevel ( ) const
pure virtualnoexcept

◆ GetCutoff()

virtual int AVCodecContextWrapper::GetCutoff ( ) const
pure virtualnoexcept

◆ GetFlags()

virtual int AVCodecContextWrapper::GetFlags ( ) const
pure virtualnoexcept

◆ GetFlags2()

virtual int AVCodecContextWrapper::GetFlags2 ( ) const
pure virtualnoexcept

◆ GetFrameNumber()

virtual int AVCodecContextWrapper::GetFrameNumber ( ) const
pure virtualnoexcept

◆ GetFrameSize()

virtual int AVCodecContextWrapper::GetFrameSize ( ) const
pure virtualnoexcept

◆ GetGlobalQuality()

virtual int AVCodecContextWrapper::GetGlobalQuality ( ) const
pure virtualnoexcept

◆ GetPreferredAudacitySampleFormat()

virtual sampleFormat AVCodecContextWrapper::GetPreferredAudacitySampleFormat ( ) const
pure virtualnoexcept

◆ GetProfile()

virtual int AVCodecContextWrapper::GetProfile ( ) const
pure virtualnoexcept

◆ GetSampleFmt()

virtual AVSampleFormatFwd AVCodecContextWrapper::GetSampleFmt ( ) const
pure virtualnoexcept

◆ GetSampleRate()

virtual int AVCodecContextWrapper::GetSampleRate ( ) const
pure virtualnoexcept

◆ GetStrictStdCompliance()

virtual int AVCodecContextWrapper::GetStrictStdCompliance ( ) const
pure virtualnoexcept

◆ GetTimeBase()

virtual struct AudacityAVRational AVCodecContextWrapper::GetTimeBase ( ) const
pure virtualnoexcept

◆ GetWrappedValue() [1/2]

const AVCodecContext * AVCodecContextWrapper::GetWrappedValue ( ) const
noexcept

Definition at line 42 of file AVCodecContextWrapper.cpp.

43{
44 return mAVCodecContext;
45}

References mAVCodecContext.

◆ GetWrappedValue() [2/2]

AVCodecContext * AVCodecContextWrapper::GetWrappedValue ( )
noexcept

Definition at line 37 of file AVCodecContextWrapper.cpp.

38{
39 return mAVCodecContext;
40}

References mAVCodecContext.

◆ Open()

virtual int AVCodecContextWrapper::Open ( const AVCodecWrapper codec,
AVDictionaryWrapper options = nullptr 
)
pure virtual
Parameters
optionsA dictionary filled with AVCodecContext and codec-private options. On return this object will be filled with options that were not found.
Returns
zero if success, negative if error

◆ operator=() [1/2]

AVCodecContextWrapper & AVCodecContextWrapper::operator= ( AVCodecContextWrapper &&  )
delete

◆ operator=() [2/2]

AVCodecContextWrapper & AVCodecContextWrapper::operator= ( AVCodecContextWrapper )
delete

◆ SetBitRate()

virtual void AVCodecContextWrapper::SetBitRate ( int  value)
pure virtualnoexcept

◆ SetChannelLayout()

virtual void AVCodecContextWrapper::SetChannelLayout ( const AVChannelLayoutWrapper value)
pure virtualnoexcept

◆ SetCodecTag()

virtual void AVCodecContextWrapper::SetCodecTag ( unsigned int  tag)
pure virtualnoexcept

◆ SetCodecTagFourCC()

void AVCodecContextWrapper::SetCodecTagFourCC ( const char *  fourCC)
noexcept

Definition at line 199 of file AVCodecContextWrapper.cpp.

200{
201 if (fourCC == nullptr || std::strlen(fourCC) != 4)
202 return;
203
204 SetCodecTag(MakeTag(fourCC[0], fourCC[1], fourCC[2], fourCC[3]));
205}
virtual void SetCodecTag(unsigned int tag) noexcept=0
unsigned int MakeTag(char a, char b, char c, char d) noexcept

References anonymous_namespace{AVCodecContextWrapper.cpp}::MakeTag().

Here is the call graph for this function:

◆ SetCompressionLevel()

virtual void AVCodecContextWrapper::SetCompressionLevel ( int  value)
pure virtualnoexcept

◆ SetCutoff()

virtual void AVCodecContextWrapper::SetCutoff ( int  value)
pure virtualnoexcept

◆ SetFlags()

virtual void AVCodecContextWrapper::SetFlags ( int  value)
pure virtualnoexcept

◆ SetFlags2()

virtual void AVCodecContextWrapper::SetFlags2 ( int  value)
pure virtualnoexcept

◆ SetFrameSize()

virtual void AVCodecContextWrapper::SetFrameSize ( int  value)
pure virtualnoexcept

◆ SetGlobalQuality()

virtual void AVCodecContextWrapper::SetGlobalQuality ( int  value)
pure virtualnoexcept

◆ SetProfile()

virtual void AVCodecContextWrapper::SetProfile ( int  value)
pure virtualnoexcept

◆ SetSampleFmt()

virtual void AVCodecContextWrapper::SetSampleFmt ( AVSampleFormatFwd  value)
pure virtualnoexcept

◆ SetSampleRate()

virtual void AVCodecContextWrapper::SetSampleRate ( int  value)
pure virtualnoexcept

◆ SetStrictStdCompliance()

virtual void AVCodecContextWrapper::SetStrictStdCompliance ( int  value)
pure virtualnoexcept

◆ SetTimeBase()

virtual void AVCodecContextWrapper::SetTimeBase ( struct AudacityAVRational  value)
pure virtualnoexcept

Member Data Documentation

◆ mAVCodec

std::unique_ptr<AVCodecWrapper> AVCodecContextWrapper::mAVCodec
mutableprotected

Definition at line 125 of file AVCodecContextWrapper.h.

◆ mAVCodecContext

AVCodecContext* AVCodecContextWrapper::mAVCodecContext { nullptr }
protected

◆ mFFmpeg

const FFmpegFunctions& AVCodecContextWrapper::mFFmpeg
protected

◆ mIsOwned

bool AVCodecContextWrapper::mIsOwned { false }
protected

Definition at line 127 of file AVCodecContextWrapper.h.

Referenced by ~AVCodecContextWrapper().


The documentation for this class was generated from the following files: