1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/**********************************************************************
Audacity: A Digital Audio Editor
AVCodecContextWrapper.cpp
Dmitry Vedenko
**********************************************************************/
#include "AVCodecContextWrapper.h"
#include <cstring>
#include "FFmpegFunctions.h"
#include "AVCodecWrapper.h"
AVCodecContextWrapper::AVCodecContextWrapper(
const FFmpegFunctions& ffmpeg, std::unique_ptr<AVCodecWrapper> codec) noexcept
: mFFmpeg(ffmpeg)
, mAVCodec(std::move(codec))
, mIsOwned(true)
{
mAVCodecContext =
mFFmpeg.avcodec_alloc_context3(mAVCodec->GetWrappedValue());
}
AVCodecContextWrapper::AVCodecContextWrapper(
const FFmpegFunctions& ffmpeg, AVCodecContext* wrapped) noexcept
: mFFmpeg(ffmpeg)
, mAVCodecContext(wrapped)
, mIsOwned(false)
{
}
AVCodecContext* AVCodecContextWrapper::GetWrappedValue() noexcept
{
return mAVCodecContext;
}
const AVCodecContext* AVCodecContextWrapper::GetWrappedValue() const noexcept
{
return mAVCodecContext;
}
AVCodecContextWrapper::~AVCodecContextWrapper()
{
if (mIsOwned && mAVCodecContext != nullptr)
{
// avcodec_free_context, complementary to avcodec_alloc_context3, is
// not necessarily loaded
if (mFFmpeg.avcodec_free_context != nullptr)
{
mFFmpeg.avcodec_free_context(&mAVCodecContext);
}
else
{
// Its not clear how to avoid the leak here, but let's close
// the codec at least
if (mFFmpeg.avcodec_is_open(mAVCodecContext))
mFFmpeg.avcodec_close(mAVCodecContext);
}
}
}
std::vector<uint8_t>
AVCodecContextWrapper::DecodeAudioPacket(const AVPacketWrapper* packet)
{
auto frame = mFFmpeg.CreateAVFrameWrapper();
std::vector<uint8_t> data;
if (mFFmpeg.avcodec_send_packet == nullptr)
{
std::unique_ptr<AVPacketWrapper> packetCopy =
packet ? packet->Clone() : mFFmpeg.CreateAVPacketWrapper();
/*
"Flushing is done by calling this function [avcodec_decode_audio4]
with packets with avpkt->data set to NULL and avpkt->size set to 0
until it stops returning samples."
(That implies, not necessarily just one loop pass to flush)
*/
bool flushing = packet
? (packetCopy->GetSize() == 0 && packetCopy->GetData() == nullptr)
: true;
if (!flushing && packetCopy->GetData() == nullptr)
return {};
int bytesDecoded = 0;
do
{
int gotFrame;
// Deprecated? https://ffmpeg.org/doxygen/3.3/group__lavc__decoding.html#gaaa1fbe477c04455cdc7a994090100db4
bytesDecoded = mFFmpeg.avcodec_decode_audio4(
mAVCodecContext, frame->GetWrappedValue(), &gotFrame,
packetCopy->GetWrappedValue());
if (bytesDecoded < 0)
return data; // Packet decoding has failed
if (gotFrame == 0)
{
/*
"Note that this field being set to zero does not mean that an
error has occurred. For decoders with AV_CODEC_CAP_DELAY set, no
given decode call is guaranteed to produce a frame."
*/
// (Let's assume this doesn't happen when flushing)
// Still, the data was consumed by the decoder, so we need to
// offset the packet
packetCopy->OffsetPacket(bytesDecoded);
continue;
}
ConsumeFrame(data, *frame);
packetCopy->OffsetPacket(bytesDecoded);
}
while ( flushing ? bytesDecoded > 0 : packetCopy->GetSize() > 0 );
}
else
{
auto ret = mFFmpeg.avcodec_send_packet(
mAVCodecContext,
packet != nullptr ? packet->GetWrappedValue() : nullptr);
if (ret < 0)
// send_packet has failed
return data;
while (ret >= 0)
{
ret = mFFmpeg.avcodec_receive_frame(mAVCodecContext, frame->GetWrappedValue());
if (ret == AUDACITY_AVERROR(EAGAIN) || ret == AUDACITY_AVERROR_EOF)
// The packet is fully consumed OR more data is needed
break;
else if (ret < 0)
// Decoding has failed
return data;
ConsumeFrame(data, *frame);
}
}
return data;
}
void AVCodecContextWrapper::ConsumeFrame(
std::vector<uint8_t>& data, AVFrameWrapper& frame)
{
const int channels = GetChannels();
const auto sampleSize = static_cast<size_t>(mFFmpeg.av_get_bytes_per_sample(
static_cast<AVSampleFormatFwd>(frame.GetFormat())));
const auto samplesCount = frame.GetSamplesCount();
const auto frameSize = channels * sampleSize * samplesCount;
auto oldSize = data.size();
data.resize(oldSize + frameSize);
auto pData = &data[oldSize];
if (frame.GetData(1) != nullptr)
{
// We return interleaved buffer
for (int channel = 0; channel < channels; channel++)
{
for (int sample = 0; sample < samplesCount; sample++)
{
const uint8_t* channelData =
frame.GetExtendedData(channel) + sampleSize * sample;
uint8_t* output =
pData + sampleSize * (channels * sample + channel);
std::copy(channelData, channelData + sampleSize, output);
}
}
}
else
{
uint8_t* frameData = frame.GetData(0);
std::copy(frameData, frameData + frameSize, pData);
}
}
namespace
{
unsigned int MakeTag(char a, char b, char c, char d) noexcept
{
return
(static_cast<unsigned>(a) << 0) | (static_cast<unsigned>(b) << 8) |
(static_cast<unsigned>(c) << 16) | (static_cast<unsigned>(d) << 24);
}
}
void AVCodecContextWrapper::SetCodecTagFourCC(const char* fourCC) noexcept
{
if (fourCC == nullptr || std::strlen(fourCC) != 4)
return;
SetCodecTag(MakeTag(fourCC[0], fourCC[1], fourCC[2], fourCC[3]));
}
|