Audacity  3.2.0
Public Member Functions | Protected Attributes | 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 uint64_t GetChannelLayout () const noexcept=0
 
virtual void SetChannelLayout (uint64_t value) noexcept=0
 
virtual int GetChannels () const noexcept=0
 
virtual void SetChannels (int value) 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 void SetFrameNumber (int value) 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 }
 

Detailed Description

Definition at line 29 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 }

◆ 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 }

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

Member Function Documentation

◆ 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 
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  const int channels = GetChannels();
91 
92  int bytesDecoded = 0;
93  do
94  {
95  int gotFrame;
96  // Deprecated? https://ffmpeg.org/doxygen/3.3/group__lavc__decoding.html#gaaa1fbe477c04455cdc7a994090100db4
97  bytesDecoded = mFFmpeg.avcodec_decode_audio4(
98  mAVCodecContext, frame->GetWrappedValue(), &gotFrame,
99  packetCopy->GetWrappedValue());
100 
101  if (bytesDecoded < 0)
102  return data; // Packet decoding has failed
103 
104  if (gotFrame == 0)
105  {
106  /*
107  "Note that this field being set to zero does not mean that an
108  error has occurred. For decoders with AV_CODEC_CAP_DELAY set, no
109  given decode call is guaranteed to produce a frame."
110  */
111  // (Let's assume this doesn't happen when flushing)
112  // Still, the data was consumed by the decoder, so we need to
113  // offset the packet
114  packetCopy->OffsetPacket(bytesDecoded);
115  continue;
116  }
117 
118  const auto sampleSize =
119  static_cast<size_t>(mFFmpeg.av_get_bytes_per_sample(
120  static_cast<AVSampleFormatFwd>(frame->GetFormat())));
121 
122  const auto samplesCount = frame->GetSamplesCount();
123  const auto frameSize = channels * sampleSize * samplesCount;
124 
125  auto oldSize = data.size();
126  data.resize(oldSize + frameSize);
127  auto pData = &data[oldSize];
128 
129  if (frame->GetData(1) != nullptr)
130  {
131  // We return interleaved buffer
132  for (int channel = 0; channel < channels; channel++)
133  {
134  for (int sample = 0; sample < samplesCount; sample++)
135  {
136  const uint8_t* channelData =
137  frame->GetExtendedData(channel) + sampleSize * sample;
138 
139  uint8_t* output =
140  pData + sampleSize * (channels * sample + channel);
141 
142  std::copy(channelData, channelData + sampleSize, output);
143  }
144  }
145  }
146  else
147  {
148  uint8_t* frameData = frame->GetData(0);
149  std::copy(frameData, frameData + frameSize, pData);
150  }
151 
152  packetCopy->OffsetPacket(bytesDecoded);
153  }
154  while ( flushing ? bytesDecoded > 0 : packetCopy->GetSize() > 0 );
155  }
156 
157  return data;
158 }

References AVUtilFunctions::av_get_bytes_per_sample, AVCodecFunctions::avcodec_decode_audio4, AVPacketWrapper::Clone(), FFmpegFunctions::CreateAVFrameWrapper(), FFmpegFunctions::CreateAVPacketWrapper(), GetChannels(), AVPacketWrapper::GetData(), AVPacketWrapper::GetSize(), AVPacketWrapper::GetWrappedValue(), mAVCodecContext, mFFmpeg, and AVPacketWrapper::OffsetPacket().

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 uint64_t AVCodecContextWrapper::GetChannelLayout ( ) const
pure virtualnoexcept

Referenced by encode_audio().

Here is the caller graph for this function:

◆ GetChannels()

virtual int AVCodecContextWrapper::GetChannels ( ) const
pure virtualnoexcept

Referenced by DecodeAudioPacket(), and encode_audio().

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

Referenced by encode_audio().

Here is the caller graph for this function:

◆ 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.

Referenced by encode_audio().

Here is the caller graph for this function:

◆ 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 ( uint64_t  value)
pure virtualnoexcept

◆ SetChannels()

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

◆ SetCodecTag()

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

◆ SetCodecTagFourCC()

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

Definition at line 170 of file AVCodecContextWrapper.cpp.

171 {
172  if (fourCC == nullptr || std::strlen(fourCC) != 4)
173  return;
174 
175  SetCodecTag(MakeTag(fourCC[0], fourCC[1], fourCC[2], fourCC[3]));
176 }

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

◆ SetFrameNumber()

virtual void AVCodecContextWrapper::SetFrameNumber ( 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 123 of file AVCodecContextWrapper.h.

◆ mAVCodecContext

AVCodecContext* AVCodecContextWrapper::mAVCodecContext { nullptr }
protected

◆ mFFmpeg

const FFmpegFunctions& AVCodecContextWrapper::mFFmpeg
protected

Definition at line 120 of file AVCodecContextWrapper.h.

Referenced by DecodeAudioPacket(), and ~AVCodecContextWrapper().

◆ mIsOwned

bool AVCodecContextWrapper::mIsOwned { false }
protected

Definition at line 125 of file AVCodecContextWrapper.h.

Referenced by ~AVCodecContextWrapper().


The documentation for this class was generated from the following files:
AVCodecContextWrapper::mAVCodec
std::unique_ptr< AVCodecWrapper > mAVCodec
Definition: AVCodecContextWrapper.h:123
AVCodecFunctions::avcodec_close
int(* avcodec_close)(AVCodecContext *avctx)
Definition: AVCodecFunctions.h:30
anonymous_namespace{AVCodecContextWrapper.cpp}::MakeTag
unsigned int MakeTag(char a, char b, char c, char d) noexcept
Definition: AVCodecContextWrapper.cpp:162
AVUtilFunctions::av_get_bytes_per_sample
int(* av_get_bytes_per_sample)(AVSampleFormatFwd sample_fmt)
Definition: AVUtilFunctions.h:29
FFmpegFunctions::CreateAVPacketWrapper
std::unique_ptr< AVPacketWrapper > CreateAVPacketWrapper() const
Definition: FFmpegFunctions.cpp:354
AVCodecContextWrapper::mAVCodecContext
AVCodecContext * mAVCodecContext
Definition: AVCodecContextWrapper.h:121
AVCodecFunctions::avcodec_alloc_context3
AVCodecContext *(* avcodec_alloc_context3)(const AVCodec *codec)
Definition: AVCodecFunctions.h:33
AVCodecFunctions::avcodec_free_context
void(* avcodec_free_context)(AVCodecContext **avctx)
Definition: AVCodecFunctions.h:43
FFmpegFunctions::CreateAVFrameWrapper
std::unique_ptr< AVFrameWrapper > CreateAVFrameWrapper() const
Definition: FFmpegFunctions.cpp:359
AVPacketWrapper::OffsetPacket
virtual bool OffsetPacket(size_t offset) noexcept=0
AVCodecFunctions::avcodec_decode_audio4
int(* avcodec_decode_audio4)(AVCodecContext *avctx, AVFrame *frame, int *got_output, const AVPacket *avpkt)
Definition: AVCodecFunctions.h:31
AVPacketWrapper::GetData
virtual uint8_t * GetData() const noexcept=0
AVPacketWrapper::Clone
virtual std::unique_ptr< AVPacketWrapper > Clone() const noexcept=0
AVPacketWrapper::GetSize
virtual int GetSize() const noexcept=0
AVCodecContextWrapper::GetChannels
virtual int GetChannels() const noexcept=0
AVSampleFormatFwd
int AVSampleFormatFwd
Definition: FFmpegTypes.h:127
AVCodecContextWrapper::mIsOwned
bool mIsOwned
Definition: AVCodecContextWrapper.h:125
AVCodecContextWrapper::SetCodecTag
virtual void SetCodecTag(unsigned int tag) noexcept=0
AVPacketWrapper::GetWrappedValue
AVPacket * GetWrappedValue() noexcept
Definition: AVPacketWrapper.cpp:20
AVCodecContextWrapper::mFFmpeg
const FFmpegFunctions & mFFmpeg
Definition: AVCodecContextWrapper.h:120
AVCodecFunctions::avcodec_is_open
int(* avcodec_is_open)(AVCodecContext *avctx)
Definition: AVCodecFunctions.h:29