Skip to content

Commit

Permalink
Some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Mar 29, 2024
1 parent 4a3f9c5 commit e6c38af
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
44 changes: 25 additions & 19 deletions src/audiosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ void LWAudioDecoder::GetAudioProperties(AudioProperties &AP) {
if (!PropFrame)
return;

AP.IsFloat = (PropFrame->format == AV_SAMPLE_FMT_FLTP || PropFrame->format == AV_SAMPLE_FMT_FLT || PropFrame->format == AV_SAMPLE_FMT_DBLP || PropFrame->format == AV_SAMPLE_FMT_DBL);
AP.BytesPerSample = av_get_bytes_per_sample(static_cast<AVSampleFormat>(PropFrame->format));
AP.BitsPerSample = CodecContext->bits_per_raw_sample ? (CodecContext->bits_per_raw_sample) : (AP.BytesPerSample * 8); // assume all bits are relevant if not specified
AP.AF.Set(PropFrame->format, CodecContext->bits_per_raw_sample);
AP.SampleRate = PropFrame->sample_rate;
AP.Channels = PropFrame->ch_layout.nb_channels;

Expand All @@ -230,7 +228,7 @@ void LWAudioDecoder::GetAudioProperties(AudioProperties &AP) {
if (PropFrame->pts != AV_NOPTS_VALUE)
AP.StartTime = (static_cast<double>(FormatContext->streams[TrackNumber]->time_base.num) * PropFrame->pts) / FormatContext->streams[TrackNumber]->time_base.den;

if (AP.BytesPerSample <= 0)
if (AP.AF.Bits <= 0)
throw AudioException("Codec returned zero size audio");
}

Expand Down Expand Up @@ -281,10 +279,18 @@ bool LWAudioDecoder::HasSeeked() const {
return Seeked;
}

void AudioFormat::Set(int Format, int BitsPerRawSample) {
Float = (Format == AV_SAMPLE_FMT_FLTP || Format == AV_SAMPLE_FMT_FLT || Format == AV_SAMPLE_FMT_DBLP || Format == AV_SAMPLE_FMT_DBL);
BytesPerSample = av_get_bytes_per_sample(static_cast<AVSampleFormat>(Format));
Bits = BitsPerRawSample ? BitsPerRawSample : (BytesPerSample * 8);
}

BestAudioFrame::BestAudioFrame(AVFrame *F) {
assert(F);
Frame = av_frame_clone(F);
// FIXME, fill in

AF.Set(F->format, 0); // FIXME, number of used bits is wrong for individual frames
NumChannels = F->ch_layout.nb_channels;
Pts = Frame->pts;
NumSamples = Frame->nb_samples;
}
Expand Down Expand Up @@ -848,7 +854,7 @@ BestAudioSource::FrameRange BestAudioSource::GetFrameRangeBySamples(int64_t Star
void BestAudioSource::ZeroFillStartPacked(uint8_t *&Data, int64_t &Start, int64_t &Count) {
if (Start < 0) {
int64_t Length = std::min(Count, -Start);
size_t ByteLength = Length * AP.BytesPerSample * AP.Channels;
size_t ByteLength = Length * AP.AF.BytesPerSample * AP.Channels;
memset(Data, 0, ByteLength);
Data += ByteLength;
Start += Length;
Expand All @@ -859,8 +865,8 @@ void BestAudioSource::ZeroFillStartPacked(uint8_t *&Data, int64_t &Start, int64_
void BestAudioSource::ZeroFillEndPacked(uint8_t *Data, int64_t Start, int64_t &Count) {
if (Start + Count > AP.NumSamples) {
int64_t Length = std::min(Start + Count - AP.NumSamples, Count);
size_t ByteOffset = std::min<int64_t>(AP.NumSamples - Start, 0) * AP.BytesPerSample * AP.Channels;
memset(Data + ByteOffset, 0, Length * AP.BytesPerSample * AP.Channels);
size_t ByteOffset = std::min<int64_t>(AP.NumSamples - Start, 0) * AP.AF.BytesPerSample * AP.Channels;
memset(Data + ByteOffset, 0, Length * AP.AF.BytesPerSample * AP.Channels);
Count -= Length;
}
}
Expand All @@ -886,13 +892,13 @@ bool BestAudioSource::FillInFramePacked(const BestAudioFrame *Frame, int64_t Fra
if (IsPlanar) {
std::vector<const uint8_t *> DataV;
DataV.reserve(F->ch_layout.nb_channels);
size_t ByteOffset = (Start - FrameStartSample) * AP.BytesPerSample;
size_t ByteOffset = (Start - FrameStartSample) * AP.AF.BytesPerSample;
for (int i = 0; i < F->ch_layout.nb_channels; i++)
DataV.push_back(F->extended_data[i] + ByteOffset);
PackChannels(DataV.data(), Data, Length, F->ch_layout.nb_channels, AP.BytesPerSample);
PackChannels(DataV.data(), Data, Length, F->ch_layout.nb_channels, AP.AF.BytesPerSample);
} else {
size_t ByteOffset = (Start - FrameStartSample) * AP.BytesPerSample * F->ch_layout.nb_channels;
size_t ByteLength = Length * AP.BytesPerSample * F->ch_layout.nb_channels;
size_t ByteOffset = (Start - FrameStartSample) * AP.AF.BytesPerSample * F->ch_layout.nb_channels;
size_t ByteLength = Length * AP.AF.BytesPerSample * F->ch_layout.nb_channels;
memcpy(Data, F->extended_data[0] + ByteOffset, ByteLength);
Data += ByteLength;
}
Expand All @@ -908,7 +914,7 @@ bool BestAudioSource::FillInFramePacked(const BestAudioFrame *Frame, int64_t Fra
void BestAudioSource::ZeroFillStartPlanar(uint8_t *Data[], int64_t &Start, int64_t &Count) {
if (Start < 0) {
int64_t Length = std::min(Count, -Start);
size_t ByteLength = Length * AP.BytesPerSample;
size_t ByteLength = Length * AP.AF.BytesPerSample;
for (int i = 0; i < AP.Channels; i++) {
memset(Data[i], 0, ByteLength);
Data[i] += ByteLength;
Expand All @@ -921,9 +927,9 @@ void BestAudioSource::ZeroFillStartPlanar(uint8_t *Data[], int64_t &Start, int64
void BestAudioSource::ZeroFillEndPlanar(uint8_t *Data[], int64_t Start, int64_t &Count) {
if (Start + Count > AP.NumSamples) {
int64_t Length = std::min(Start + Count - AP.NumSamples, Count);
size_t ByteOffset = std::min<int64_t>(AP.NumSamples - Start, 0) * AP.BytesPerSample;
size_t ByteOffset = std::min<int64_t>(AP.NumSamples - Start, 0) * AP.AF.BytesPerSample;
for (int i = 0; i < AP.Channels; i++)
memset(Data[i] + ByteOffset, 0, Length * AP.BytesPerSample);
memset(Data[i] + ByteOffset, 0, Length * AP.AF.BytesPerSample);
Count -= Length;
}
}
Expand All @@ -947,15 +953,15 @@ bool BestAudioSource::FillInFramePlanar(const BestAudioFrame *Frame, int64_t Fra
return false;

if (IsPlanar) {
size_t ByteLength = Length * AP.BytesPerSample;
size_t ByteOffset = (Start - FrameStartSample) * AP.BytesPerSample;
size_t ByteLength = Length * AP.AF.BytesPerSample;
size_t ByteOffset = (Start - FrameStartSample) * AP.AF.BytesPerSample;
for (int i = 0; i < AP.Channels; i++) {
memcpy(Data[i], F->extended_data[i] + ByteOffset, ByteLength);
Data[i] += ByteLength;
}
} else {
size_t ByteOffset = (Start - FrameStartSample) * AP.BytesPerSample * F->ch_layout.nb_channels;
UnpackChannels(F->extended_data[0] + ByteOffset, Data, Length, F->ch_layout.nb_channels, AP.BytesPerSample);
size_t ByteOffset = (Start - FrameStartSample) * AP.AF.BytesPerSample * F->ch_layout.nb_channels;
UnpackChannels(F->extended_data[0] + ByteOffset, Data, Length, F->ch_layout.nb_channels, AP.AF.BytesPerSample);
}
Start += Length;
Count -= Length;
Expand Down
24 changes: 13 additions & 11 deletions src/audiosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#include <array>
#include <memory>



struct AVFormatContext;
struct AVCodecContext;
struct AVBufferRef;
Expand All @@ -45,10 +43,16 @@ class AudioException : public std::runtime_error {
using std::runtime_error::runtime_error;
};

struct AudioFormat {
bool Float;
int Bits;
int BytesPerSample; // FIXME, kinda odd to have it exposed here but very useful to store internally

void Set(int Format, int BitsPerRawSample);
};

struct AudioProperties {
bool IsFloat;
int BytesPerSample;
int BitsPerSample;
AudioFormat AF;
int SampleRate;
int Channels;
uint64_t ChannelLayout;
Expand Down Expand Up @@ -98,13 +102,11 @@ class BestAudioFrame {
BestAudioFrame(AVFrame *Frame);
~BestAudioFrame();
[[nodiscard]] const AVFrame *GetAVFrame() const;
// FIXME, doesn't expose nearly enough information relative to BestVideoFrame
// NumChannels, Audio format info (float, bits)
int64_t Pts;
int64_t NumSamples;
AudioFormat AF;
int NumChannels;

// FIXME, add something like exportplanar and exportpacked to the frame?
};
int64_t Pts;
int64_t NumSamples;};

class BestAudioSource {
private:
Expand Down
8 changes: 4 additions & 4 deletions src/avisynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ class AvisynthAudioSource : public IClip {
A.reset(new BestAudioSource(Source, Track, AdjustDelay, false, Threads, CachePath ? CachePath : "", &Opts, DrcScale));

const AudioProperties &AP = A->GetAudioProperties();
if (AP.IsFloat && AP.BitsPerSample == 32) {
if (AP.AF.Float && AP.AF.Bits == 32) {
VI.sample_type = SAMPLE_FLOAT;
} else if (!AP.IsFloat && AP.BitsPerSample == 8) {
} else if (!AP.AF.Float && AP.AF.Bits <= 8) {
VI.sample_type = SAMPLE_INT8;
} else if (!AP.IsFloat && AP.BitsPerSample == 16) {
} else if (!AP.AF.Float && AP.AF.Bits <= 16) {
VI.sample_type = SAMPLE_INT16;
} else if (!AP.IsFloat && AP.BitsPerSample == 32) {
} else if (!AP.AF.Float && AP.AF.Bits <= 32) {
VI.sample_type = SAMPLE_INT32;
} else {
Env->ThrowError("BestAudioSource: Unsupported audio format");
Expand Down
4 changes: 2 additions & 2 deletions src/vapoursynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static const VSFrame *VS_CC BestVideoSourceGetFrame(int n, int ActivationReason,
}

ptrdiff_t AlphaStride = 0;
if (Src->HasAlpha()) {
if (Src->VF.Alpha) {
AlphaDst = vsapi->newVideoFrame(&AlphaFormat, Src->Width, Src->Height, nullptr, Core);
AlphaStride = vsapi->getStride(AlphaDst, 0);
vsapi->mapSetInt(vsapi->getFramePropertiesRW(AlphaDst), "_ColorRange", 0, maAppend);
Expand Down Expand Up @@ -382,7 +382,7 @@ static void VS_CC CreateBestAudioSource(const VSMap *In, VSMap *Out, void *, VSC
}

const AudioProperties &AP = D->A->GetAudioProperties();
if (!vsapi->queryAudioFormat(&D->AI.format, AP.IsFloat, AP.BitsPerSample, AP.ChannelLayout, Core))
if (!vsapi->queryAudioFormat(&D->AI.format, AP.AF.Float, AP.AF.Bits, AP.ChannelLayout, Core))
throw AudioException("Unsupported audio format from decoder (probably 8-bit)");
D->AI.sampleRate = AP.SampleRate;
D->AI.numSamples = AP.NumSamples;
Expand Down
6 changes: 0 additions & 6 deletions src/videosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ void LWVideoDecoder::GetVideoProperties(VideoProperties &VP) {

VP.Width = CodecContext->width;
VP.Height = CodecContext->height;
VP.PixFmt = CodecContext->pix_fmt;
VP.VF.Set(av_pix_fmt_desc_get(static_cast<AVPixelFormat>(PropFrame->format)));

VP.FPS = CodecContext->framerate;
Expand Down Expand Up @@ -542,11 +541,6 @@ const AVFrame *BestVideoFrame::GetAVFrame() const {
return Frame;
};

bool BestVideoFrame::HasAlpha() const {
auto Desc = av_pix_fmt_desc_get(static_cast<AVPixelFormat>(Frame->format));
return ::HasAlpha(Desc);
};

void BestVideoFrame::MergeField(bool Top, const BestVideoFrame *AFieldSrc) {
const AVFrame *FieldSrc = AFieldSrc->GetAVFrame();
if (Frame->format != FieldSrc->format || Frame->width != FieldSrc->width || Frame->height != FieldSrc->height)
Expand Down
2 changes: 0 additions & 2 deletions src/videosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ struct VideoProperties {
BSRational FPS;
BSRational SAR;

int PixFmt;
VideoFormat VF;
int Width;
int Height;
Expand Down Expand Up @@ -137,7 +136,6 @@ class BestVideoFrame {
BestVideoFrame(AVFrame *Frame);
~BestVideoFrame();
[[nodiscard]] const AVFrame *GetAVFrame() const;
[[nodiscard]] bool HasAlpha() const;
void MergeField(bool Top, const BestVideoFrame *FieldSrc); // Useful for RFF and other such things where fields from multiple decoded frames need to be combined, retains original frame's properties
bool ExportAsPlanar(uint8_t **Dsts, ptrdiff_t *Stride, uint8_t *AlphaDst = nullptr, ptrdiff_t AlphaStride = 0) const;

Expand Down

0 comments on commit e6c38af

Please sign in to comment.