Debian Codegraph @

src/core/audiosource.h

raw symbols SCIP
1 // Copyright (c) 2007-2011 Fredrik Mellbin
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
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 // delay in samples to apply to the audio
71 int64_t Delay = 0;
72 // cache of decoded audio blocks
73 std::list<AudioBlock> Cache;
74 // max size of the cache in blocks
75 size_t MaxCacheBlocks = 50;
76 // pointer to last element of the cache which should never be deleted
77 CacheIterator CacheNoDelete;
78 // bytes per sample * number of channels, *after* resampling if applicable
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 // Insert the current audio frame into the cache
93 AudioBlock *CacheBlock(CacheIterator &pos);
94
95 // Interleave the current audio frame and insert it into the cache
96 void ResampleAndCache(CacheIterator pos);
97
98 // Cache the unseekable beginning of the file once the output format is set
99 void CacheBeginning();
100
101 // Called after seeking
102 void Seek();
103 // Read the next packet from the file
104 bool ReadPacket(AVPacket *);
105
106 // Close and reopen the source file to seek back to the beginning. Only
107 // needs to do anything for formats that can't seek to the beginning otherwise.
108 //
109 // If the file is not already open, it is merely just opened.
110 void OpenFile();
111
112 // First sample which is stored in the decoding buffer
113 int64_t CurrentSample = -1;
114 // Next packet to be read
115 size_t PacketNumber = 0;
116 // Current audio frame
117 const FrameInfo *CurrentFrame = nullptr;
118 // Track which this corresponds to
119 int TrackNumber;
120 // Number of packets which the demuxer requires to know where it is
121 // If -1, seeking is assumed to be impossible
122 int SeekOffset = 0;
123
124 // Buffer which audio is decoded into
125 AVFrame *DecodeFrame = nullptr;
126 FFMS_Track Frames;
127 AVCodecContext *CodecContext = nullptr;
128 FFMS_AudioProperties AP = {};
129
130 int DecodeNextBlock(CacheIterator *cachePos = 0);
131 // Initialization which has to be done after the codec is opened
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