forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioProperties.cpp
More file actions
192 lines (161 loc) · 5.94 KB
/
AudioProperties.cpp
File metadata and controls
192 lines (161 loc) · 5.94 KB
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
#include "AudioProperties.hpp"
#include <AvTranscoder/properties/util.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/pixdesc.h>
#include <libavutil/channel_layout.h>
}
#include <stdexcept>
namespace avtranscoder
{
AudioProperties::AudioProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}
std::string AudioProperties::getSampleFormatName() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
const char* fmtName = av_get_sample_fmt_name(_codecContext->sample_fmt);
if(!fmtName)
throw std::runtime_error("unknown sample format");
return std::string(fmtName);
}
std::string AudioProperties::getSampleFormatLongName() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
switch(_codecContext->sample_fmt)
{
case AV_SAMPLE_FMT_NONE:
return "none";
case AV_SAMPLE_FMT_U8:
return "unsigned 8 bits";
case AV_SAMPLE_FMT_S16:
return "signed 16 bits";
case AV_SAMPLE_FMT_S32:
return "signed 32 bits";
case AV_SAMPLE_FMT_S64:
return "signed 64 bits";
case AV_SAMPLE_FMT_FLT:
return "float";
case AV_SAMPLE_FMT_DBL:
return "double";
case AV_SAMPLE_FMT_U8P:
return "unsigned 8 bits, planar";
case AV_SAMPLE_FMT_S16P:
return "signed 16 bits, planar";
case AV_SAMPLE_FMT_S32P:
return "signed 32 bits, planar";
case AV_SAMPLE_FMT_S64P:
return "signed 64 bits, planar";
case AV_SAMPLE_FMT_FLTP:
return "float, planar";
case AV_SAMPLE_FMT_DBLP:
return "double, planar";
case AV_SAMPLE_FMT_NB:
return "number of sample formats";
}
return "unknown sample format";
}
std::string AudioProperties::getChannelLayout() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
char buf1[512];
av_get_channel_layout_string(buf1, sizeof(buf1), getNbChannels(), _codecContext->channel_layout);
return std::string(buf1);
}
std::string AudioProperties::getChannelName() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
const char* channelName = av_get_channel_name(_codecContext->channel_layout);
if(!channelName)
throw std::runtime_error("unknown channel name");
return std::string(channelName);
}
std::string AudioProperties::getChannelDescription() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
#ifdef AVTRANSCODER_FFMPEG_DEPENDENCY
const char* channelDescription = av_get_channel_description(_codecContext->channel_layout);
if(!channelDescription)
throw std::runtime_error("unknown channel description");
return std::string(channelDescription);
#else
throw std::runtime_error("can't access channel description");
#endif
}
size_t AudioProperties::getBitRate() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
// return bit rate of stream if present
if(_codecContext->bit_rate)
return _codecContext->bit_rate;
LOG_WARN("The bitrate of the stream '" << _streamIndex << "' of file '" << _formatContext->url << "' is unknown.")
LOG_INFO("Compute the audio bitrate (suppose PCM audio data).")
const int bitsPerSample = av_get_bits_per_sample(_codecContext->codec_id); // 0 if unknown for the given codec
return getSampleRate() * getNbChannels() * bitsPerSample;
}
size_t AudioProperties::getSampleRate() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->sample_rate;
}
size_t AudioProperties::getNbChannels() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->channels;
}
size_t AudioProperties::getNbSamples() const
{
if(!_formatContext)
throw std::runtime_error("unknown format context");
size_t nbSamples = _formatContext->streams[_streamIndex]->nb_frames;
if(nbSamples == 0)
nbSamples = getSampleRate() * getDuration();
return nbSamples;
}
size_t AudioProperties::getTicksPerFrame() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->ticks_per_frame;
}
PropertyVector& AudioProperties::fillVector(PropertyVector& data) const
{
// Add properties of base class
PropertyVector basedProperty;
StreamProperties::fillVector(basedProperty);
data.insert(data.begin(), basedProperty.begin(), basedProperty.end());
addProperty(data, "sampleFormatName", &AudioProperties::getSampleFormatName);
addProperty(data, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName);
addProperty(data, "bitRate", &AudioProperties::getBitRate);
addProperty(data, "sampleRate", &AudioProperties::getSampleRate);
addProperty(data, "nbSamples", &AudioProperties::getNbSamples);
addProperty(data, "nbChannels", &AudioProperties::getNbChannels);
addProperty(data, "channelLayout", &AudioProperties::getChannelLayout);
addProperty(data, "channelName", &AudioProperties::getChannelName);
addProperty(data, "channelDescription", &AudioProperties::getChannelDescription);
addProperty(data, "ticksPerFrame", &AudioProperties::getTicksPerFrame);
return data;
}
std::ostream& operator<<(std::ostream& flux, const AudioProperties& audioProperties)
{
flux << std::left;
flux << detail::separator << " Audio stream " << detail::separator << std::endl;
PropertyVector properties = audioProperties.asVector();
for(PropertyVector::iterator it = properties.begin(); it != properties.end(); ++it)
{
flux << std::setw(detail::keyWidth) << it->first << ": " << it->second << std::endl;
}
return flux;
}
}