-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodec.cpp
185 lines (147 loc) · 4.69 KB
/
Codec.cpp
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
// Copyright (c) 2021-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <Codec.h>
AM_API_PRIVATE size_t read_callback(void* ptr, size_t size, size_t nmemb, void* userdata)
{
auto* file = static_cast<File*>(userdata);
return file->Read(static_cast<AmUInt8Buffer>(ptr), nmemb);
}
AM_API_PRIVATE int seek_callback(void* userdata, ogg_int64_t offset, int whence)
{
auto* file = static_cast<File*>(userdata);
file->Seek(offset, static_cast<eFileSeekOrigin>(whence));
return 0;
}
AM_API_PRIVATE long tell_callback(void* userdata)
{
auto* file = static_cast<File*>(userdata);
return file->Position();
}
AM_API_PRIVATE ov_callbacks OV_CALLBACKS = { read_callback, seek_callback, nullptr, tell_callback };
bool VorbisCodec::VorbisDecoder::Open(std::shared_ptr<File> file)
{
if (!m_codec->CanHandleFile(file))
{
amLogError("The Vorbis codec cannot handle the file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}
_file = file;
if (ov_open_callbacks(_file.get(), &_vorbis, nullptr, 0, OV_CALLBACKS) < 0)
{
_file.reset();
amLogError("Unable to open the file: '" AM_OS_CHAR_FMT "'.", file->GetPath().c_str());
return false;
}
const vorbis_info* info = ov_info(&_vorbis, -1);
const AmUInt32 framesCount = ov_pcm_total(&_vorbis, -1);
m_format.SetAll(info->rate, info->channels, 16, framesCount, info->channels * sizeof(AmAudioSample), eAudioSampleFormat_Float32);
_initialized = true;
return true;
}
bool VorbisCodec::VorbisDecoder::Close()
{
if (_initialized)
{
ov_clear(&_vorbis);
_file.reset();
m_format = SoundFormat();
_initialized = false;
return true;
}
// true because it is already closed
return true;
}
AmUInt64 VorbisCodec::VorbisDecoder::Load(AudioBuffer* out)
{
return Stream(out, 0, 0, m_format.GetFramesCount());
}
AmUInt64 VorbisCodec::VorbisDecoder::Stream(AudioBuffer* out, AmUInt64 bufferOffset, AmUInt64 seekOffset, AmUInt64 length)
{
if (!_initialized)
return 0;
const AmUInt16 channels = m_format.GetNumChannels();
AmInt64 size = length;
AmUInt64 read = 0;
AmReal32** data;
while (size > 0)
{
if (!Seek(seekOffset + read))
return 0;
const AmInt64 ret = ov_read_float(&_vorbis, &data, static_cast<AmInt32>(size), nullptr);
if (ret == 0)
break;
if (ret > 0)
{
for (AmUInt16 i = 0; i < channels; ++i)
{
auto& channel = out->GetChannel(i);
std::memcpy(&channel[bufferOffset] + read, data[i], ret * sizeof(AmAudioSample));
}
size -= ret;
read += ret;
}
else
{
if (ret == OV_EBADLINK)
{
amLogError("Corrupt bitstream section!.");
return 0;
}
if (ret == OV_EINVAL)
{
amLogError("Invalid bitstream section!.");
return 0;
}
}
}
return read;
}
bool VorbisCodec::VorbisDecoder::Seek(AmUInt64 offset)
{
return ov_pcm_seek(&_vorbis, offset) >= 0;
}
bool VorbisCodec::VorbisEncoder::Open(std::shared_ptr<File> file)
{
_initialized = true;
return false;
}
bool VorbisCodec::VorbisEncoder::Close()
{
return true;
}
AmUInt64 VorbisCodec::VorbisEncoder::Write(AudioBuffer* in, AmUInt64 offset, AmUInt64 length)
{
return 0;
}
Codec::Decoder* VorbisCodec::CreateDecoder()
{
return ampoolnew(eMemoryPoolKind_Codec, VorbisDecoder, this);
}
void VorbisCodec::DestroyDecoder(Decoder* decoder)
{
ampooldelete(eMemoryPoolKind_Codec, VorbisDecoder, (VorbisDecoder*)decoder);
}
Codec::Encoder* VorbisCodec::CreateEncoder()
{
return ampoolnew(eMemoryPoolKind_Codec, VorbisEncoder, this);
}
void VorbisCodec::DestroyEncoder(Encoder* encoder)
{
ampooldelete(eMemoryPoolKind_Codec, VorbisEncoder, (VorbisEncoder*)encoder);
}
bool VorbisCodec::CanHandleFile(std::shared_ptr<File> file) const
{
const auto& path = file->GetPath();
return path.find(AM_OS_STRING(".ogg")) != AmOsString::npos; // OGG/Vorbis extension
}