forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioFormat.cpp
423 lines (349 loc) · 10.8 KB
/
AudioFormat.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/AudioFormat.h"
#include "QtAV/QtAV_Compat.h"
//FF_API_OLD_SAMPLE_FMT. e.g. FFmpeg 0.9
#ifdef SampleFormat
#undef SampleFormat
#endif
namespace QtAV {
const qint64 kHz = 1000000LL;
typedef struct {
qint64 ff;
AudioFormat::ChannelLayout cl;
} ChannelLayoutMap;
static const ChannelLayoutMap kChannelLayoutMap[] = {
{ AV_CH_FRONT_LEFT, AudioFormat::ChannelLayout_Left },
{ AV_CH_FRONT_RIGHT, AudioFormat::ChannelLayout_Right },
{ AV_CH_FRONT_CENTER, AudioFormat::ChannelLayout_Center },
{ AV_CH_LAYOUT_MONO, AudioFormat::ChannelLayout_Mono },
{ AV_CH_LAYOUT_STEREO, AudioFormat::ChannelLayout_Stero },
{ 0, AudioFormat::ChannelLayout_Unsupported}
};
AudioFormat::ChannelLayout AudioFormat::channelLayoutFromFFmpeg(qint64 clff)
{
for (unsigned int i = 0; i < sizeof(kChannelLayoutMap)/sizeof(ChannelLayoutMap); i) {
if (kChannelLayoutMap[i].ff == clff)
return kChannelLayoutMap[i].cl;
}
return AudioFormat::ChannelLayout_Unsupported;
}
qint64 AudioFormat::channelLayoutToFFmpeg(AudioFormat::ChannelLayout cl)
{
for (unsigned int i = 0; i < sizeof(kChannelLayoutMap)/sizeof(ChannelLayoutMap); i) {
if (kChannelLayoutMap[i].cl == cl)
return kChannelLayoutMap[i].ff;
}
return 0;
}
class AudioFormatPrivate : public QSharedData
{
public:
AudioFormatPrivate():
planar(false)
, sample_format(AudioFormat::SampleFormat_Input)
, channels(0)
, sample_rate(0)
, channel_layout(AudioFormat::ChannelLayout_Unsupported)
, channel_layout_ff(0)
{}
void setChannels(int cs) {
channels = cs;
if (av_get_channel_layout_nb_channels(channel_layout_ff) != channels) {
channel_layout_ff = av_get_default_channel_layout(channels);
channel_layout = AudioFormat::channelLayoutFromFFmpeg(channel_layout_ff);
}
}
void setChannelLayoutFF(qint64 clff) {
channel_layout_ff = clff;
if (av_get_channel_layout_nb_channels(channel_layout_ff) != channels) {
channels = av_get_channel_layout_nb_channels(channel_layout_ff);
}
}
bool planar;
AudioFormat::SampleFormat sample_format;
int channels;
int sample_rate;
int bytes_per_sample;
AudioFormat::ChannelLayout channel_layout;
qint64 channel_layout_ff;
};
bool AudioFormat::isPlanar(SampleFormat format)
{
return format == SampleFormat_Unsigned8Planar
|| format == SampleFormat_Signed16Planar
|| format == SampleFormat_Signed32Planar
|| format == SampleFormat_FloatPlanar
|| format == SampleFormat_DoublePlanar;
}
AudioFormat::AudioFormat():
d(new AudioFormatPrivate())
{
}
AudioFormat::AudioFormat(const AudioFormat &other):
d(other.d)
{
}
AudioFormat::~AudioFormat()
{
}
/*!
Assigns \a other to this AudioFormat implementation.
*/
AudioFormat& AudioFormat::operator=(const AudioFormat &other)
{
d = other.d;
return *this;
}
/*!
Returns true if this AudioFormat is equal to the \a other
AudioFormat; otherwise returns false.
All elements of AudioFormat are used for the comparison.
*/
bool AudioFormat::operator==(const AudioFormat &other) const
{
return d->sample_rate == other.d->sample_rate &&
//compare channel layout first because it determines channel count
d->channel_layout_ff == other.d->channel_layout_ff &&
d->channel_layout == other.d->channel_layout &&
d->channels == other.d->channels &&
//d->sampleSize == other.d->sampleSize &&
d->sample_format == other.d->sample_format;
}
/*!
Returns true if this AudioFormat is not equal to the \a other
AudioFormat; otherwise returns false.
All elements of AudioFormat are used for the comparison.
*/
bool AudioFormat::operator!=(const AudioFormat& other) const
{
return !(*this == other);
}
/*!
Returns true if all of the parameters are valid->
*/
bool AudioFormat::isValid() const
{
return d->sample_rate > 0 && (d->channels > 0 || d->channel_layout > 0) &&
d->sample_format != AudioFormat::SampleFormat_Unknown;
}
bool AudioFormat::isPlanar() const
{
return d->planar;
}
int AudioFormat::planeCount() const
{
return isPlanar() ? 1 : channels();
}
/*!
Sets the sample rate to \a samplerate Hertz.
*/
void AudioFormat::setSampleRate(int sampleRate)
{
d->sample_rate = sampleRate;
}
/*!
Returns the current sample rate in Hertz.
*/
int AudioFormat::sampleRate() const
{
return d->sample_rate;
}
/*!
Sets the channel layout to \a layout. Currently use FFmpeg's. see avutil/channel_layout.h
*/
void AudioFormat::setChannelLayoutFFmpeg(qint64 layout)
{
//FFmpeg channel layout is more complete, so we just it
d->channel_layout = channelLayoutFromFFmpeg(layout);
d->setChannelLayoutFF(layout);
}
qint64 AudioFormat::channelLayoutFFmpeg() const
{
return d->channel_layout_ff;
}
void AudioFormat::setChannelLayout(ChannelLayout layout)
{
qint64 clff = channelLayoutToFFmpeg(layout);
d->channel_layout = layout;
//TODO: shall we set ffmpeg channel layout to 0(not valid value)?
if (!clff)
return;
d->setChannelLayoutFF(clff);
}
AudioFormat::ChannelLayout AudioFormat::channelLayout() const
{
return d->channel_layout;
}
QString AudioFormat::channelLayoutName() const
{
char cl[128];
av_get_channel_layout_string(cl, sizeof(cl), -1, channelLayoutFFmpeg()); //TODO: ff version
return cl;
}
/*!
Sets the channel count to \a channels.
*/
void AudioFormat::setChannels(int channels)
{
d->setChannels(channels);
}
/*!
Returns the current channel count value.
*/
int AudioFormat::channels() const
{
return d->channels;
}
/*!
Sets the sampleFormat to \a sampleFormat.
*/
void AudioFormat::setSampleFormat(AudioFormat::SampleFormat sampleFormat)
{
d->sample_format = sampleFormat;
d->planar = AudioFormat::isPlanar(sampleFormat);
switch (d->sample_format) {
case AudioFormat::SampleFormat_Unsigned8:
case AudioFormat::SampleFormat_Unsigned8Planar:
d->bytes_per_sample = 8 >> 3;
break;
case AudioFormat::SampleFormat_Signed16:
case AudioFormat::SampleFormat_Signed16Planar:
d->bytes_per_sample = 16 >> 3;
break;
case AudioFormat::SampleFormat_Signed32:
case AudioFormat::SampleFormat_Signed32Planar:
case AudioFormat::SampleFormat_Float:
case AudioFormat::SampleFormat_FloatPlanar:
d->bytes_per_sample = 32 >> 3;
break;
case AudioFormat::SampleFormat_Double:
case AudioFormat::SampleFormat_DoublePlanar:
d->bytes_per_sample = 64 >> 3;
break;
default:
d->bytes_per_sample = 0;
break;
}
}
/*!
Returns the current SampleFormat value.
*/
AudioFormat::SampleFormat AudioFormat::sampleFormat() const
{
return d->sample_format;
}
void AudioFormat::setSampleFormatFFmpeg(int ffSampleFormat)
{
//currently keep the values the same as latest FFmpeg's
setSampleFormat((AudioFormat::SampleFormat)ffSampleFormat);
}
int AudioFormat::sampleFormatFFmpeg() const
{
return d->sample_format;
}
QString AudioFormat::sampleFormatName() const
{
return av_get_sample_fmt_name((AVSampleFormat)sampleFormatFFmpeg());
}
/*!
Returns the number of bytes required for this audio format for \a duration microseconds.
Returns 0 if this format is not valid->
Note that some rounding may occur if \a duration is not an exact fraction of the
sampleRate().
\sa durationForBytes()
*/
qint32 AudioFormat::bytesForDuration(qint64 duration) const
{
return bytesPerFrame() * framesForDuration(duration);
}
/*!
Returns the number of microseconds represented by \a bytes in this format.
Returns 0 if this format is not valid->
Note that some rounding may occur if \a bytes is not an exact multiple
of the number of bytes per frame.
\sa bytesForDuration()
*/
qint64 AudioFormat::durationForBytes(qint32 bytes) const
{
if (!isValid() || bytes <= 0)
return 0;
// We round the byte count to ensure whole frames
return qint64(kHz * (bytes / bytesPerFrame())) / sampleRate();
}
/*!
Returns the number of bytes required for \a frameCount frames of this format.
Returns 0 if this format is not valid->
\sa bytesForDuration()
*/
qint32 AudioFormat::bytesForFrames(qint32 frameCount) const
{
return frameCount * bytesPerFrame();
}
/*!
Returns the number of frames represented by \a byteCount in this format.
Note that some rounding may occur if \a byteCount is not an exact multiple
of the number of bytes per frame.
Each frame has one sample per channel.
\sa framesForDuration()
*/
qint32 AudioFormat::framesForBytes(qint32 byteCount) const
{
int size = bytesPerFrame();
if (size > 0)
return byteCount / size;
return 0;
}
/*!
Returns the number of frames required to represent \a duration microseconds in this format.
Note that some rounding may occur if \a duration is not an exact fraction of the
\l sampleRate().
*/
qint32 AudioFormat::framesForDuration(qint64 duration) const
{
if (!isValid())
return 0;
return qint32((duration * sampleRate()) / kHz);
}
/*!
Return the number of microseconds represented by \a frameCount frames in this format.
*/
qint64 AudioFormat::durationForFrames(qint32 frameCount) const
{
if (!isValid() || frameCount <= 0)
return 0;
return (frameCount * kHz) / sampleRate();
}
int AudioFormat::bytesPerFrame() const
{
if (!isValid())
return 0;
return bytesPerSample() * channels();
}
int AudioFormat::bytesPerSample() const
{
return d->bytes_per_sample;
}
int AudioFormat::bitRate() const
{
return bytesPerSecond() << 3;
}
int AudioFormat::bytesPerSecond() const
{
return channels() * bytesPerSample() * sampleRate();
}
} //namespace QtAV