| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef FFAUDIOSOURCE_H |
| 22 |
#define FFAUDIOSOURCE_H |
| 23 |
|
| 24 |
#include "utils.h" |
| 25 |
#include "track.h" |
| 26 |
|
| 27 |
#include <list> |
| 28 |
#include <vector> |
| 29 |
#include <atomic> |
| 30 |
|
| 31 |
struct FFMS_AudioSource { |
| 32 |
struct AudioBlock { |
| 33 |
struct Free { |
| 34 |
void operator()(uint8_t *ptr) const { |
| 35 |
free(ptr); |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
int64_t Age; |
| 40 |
int64_t Start; |
| 41 |
int64_t Samples = 0; |
| 42 |
size_t DataSize = 0; |
| 43 |
std::unique_ptr<uint8_t, Free> Data; |
| 44 |
|
| 45 |
AudioBlock(int64_t Start) |
| 46 |
: Start(Start) { |
| 47 |
static std::atomic<int64_t> Now{ 0 }; |
| 48 |
Age = Now++; |
| 49 |
} |
| 50 |
|
| 51 |
uint8_t *Grow(size_t size) { |
| 52 |
auto ptr = static_cast<uint8_t *>(realloc(Data.get(), DataSize + size)); |
| 53 |
if (!ptr) |
| 54 |
throw std::bad_alloc(); |
| 55 |
Data.release(); |
| 56 |
Data.reset(ptr); |
| 57 |
ptr += DataSize; |
| 58 |
DataSize += size; |
| 59 |
return ptr; |
| 60 |
} |
| 61 |
}; |
| 62 |
typedef std::list<AudioBlock>::iterator CacheIterator; |
| 63 |
|
| 64 |
AVFormatContext *FormatContext = nullptr; |
| 65 |
std::map<std::string, std::string> LAVFOpts; |
| 66 |
double DrcScale; |
| 67 |
int64_t LastValidTS; |
| 68 |
std::string SourceFile; |
| 69 |
|
| 70 |
|
| 71 |
int64_t Delay = 0; |
| 72 |
|
| 73 |
std::list<AudioBlock> Cache; |
| 74 |
|
| 75 |
size_t MaxCacheBlocks = 50; |
| 76 |
|
| 77 |
CacheIterator CacheNoDelete; |
| 78 |
|
| 79 |
size_t BytesPerSample = 0; |
| 80 |
|
| 81 |
bool NeedsResample = false; |
| 82 |
|
| 83 |
struct SwrFreeWrapper { |
| 84 |
void operator()(SwrContext *c) const { |
| 85 |
swr_free(&c); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
typedef std::unique_ptr<SwrContext, SwrFreeWrapper> FFResampleContext; |
| 90 |
FFResampleContext ResampleContext; |
| 91 |
|
| 92 |
|
| 93 |
AudioBlock *CacheBlock(CacheIterator &pos); |
| 94 |
|
| 95 |
|
| 96 |
void ResampleAndCache(CacheIterator pos); |
| 97 |
|
| 98 |
|
| 99 |
void CacheBeginning(); |
| 100 |
|
| 101 |
|
| 102 |
void Seek(); |
| 103 |
|
| 104 |
bool ReadPacket(AVPacket *); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
void OpenFile(); |
| 111 |
|
| 112 |
|
| 113 |
int64_t CurrentSample = -1; |
| 114 |
|
| 115 |
size_t PacketNumber = 0; |
| 116 |
|
| 117 |
const FrameInfo *CurrentFrame = nullptr; |
| 118 |
|
| 119 |
int TrackNumber; |
| 120 |
|
| 121 |
|
| 122 |
int SeekOffset = 0; |
| 123 |
|
| 124 |
|
| 125 |
AVFrame *DecodeFrame = nullptr; |
| 126 |
FFMS_Track Frames; |
| 127 |
AVCodecContext *CodecContext = nullptr; |
| 128 |
FFMS_AudioProperties AP = {}; |
| 129 |
|
| 130 |
int DecodeNextBlock(CacheIterator *cachePos = 0); |
| 131 |
|
| 132 |
void Init(const FFMS_Index &Index, int DelayMode); |
| 133 |
|
| 134 |
int64_t FrameTS(size_t Packet) const; |
| 135 |
|
| 136 |
void Free(); |
| 137 |
public: |
| 138 |
FFMS_AudioSource(const char *SourceFile, FFMS_Index &Index, int Track, int DelayMode, int FillGaps, double DrcScale); |
| 139 |
~FFMS_AudioSource(); |
| 140 |
FFMS_Track *GetTrack() { return &Frames; } |
| 141 |
const FFMS_AudioProperties& GetAudioProperties() const { return AP; } |
| 142 |
void GetAudio(void *Buf, int64_t Start, int64_t Count); |
| 143 |
|
| 144 |
std::unique_ptr<FFMS_ResampleOptions> CreateResampleOptions() const; |
| 145 |
void SetOutputFormat(FFMS_ResampleOptions const& opt); |
| 146 |
|
| 147 |
static size_t GetSeekablePacketNumber(FFMS_Track const& Frames, size_t PacketNumber); |
| 148 |
}; |
| 149 |
|
| 150 |
#endif |