forked from xueguoliang/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioFrame.cpp
236 lines (212 loc) · 6.62 KB
/
AudioFrame.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
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 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/AudioFrame.h"
#include "QtAV/private/Frame_p.h"
#include "QtAV/AudioResampler.h"
#include "QtAV/private/AVCompat.h"
#include "utils/Logger.h"
namespace QtAV {
namespace{
static const struct RegisterMetaTypes
{
inline RegisterMetaTypes() {
qRegisterMetaType<QtAV::AudioFrame>("QtAV::AudioFrame");
}
} _registerMetaTypes;
}
class AudioFramePrivate : public FramePrivate
{
public:
AudioFramePrivate(const AudioFormat& fmt)
: FramePrivate()
, format(fmt)
, samples_per_ch(0)
, conv(0)
{
if (!format.isValid())
return;
const int nb_planes(format.planeCount());
planes.reserve(nb_planes);
planes.resize(nb_planes);
line_sizes.reserve(nb_planes);
line_sizes.resize(nb_planes);
}
AudioFormat format;
int samples_per_ch;
AudioResampler *conv;
};
/*!
Constructs a shallow copy of \a other. Since AudioFrame is
explicitly shared, these two instances will reflect the same frame.
*/
AudioFrame::AudioFrame(const AudioFrame &other)
: Frame(other)
{
}
AudioFrame::AudioFrame(const AudioFormat &format, const QByteArray& data)
: Frame(new AudioFramePrivate(format))
{
if (data.isEmpty())
return;
Q_D(AudioFrame);
d->format = format;
d->data = data;
if (!d->format.isValid())
return;
if (d->data.isEmpty())
return;
d->samples_per_ch = data.size() / d->format.channels() / d->format.bytesPerSample();
const int nb_planes(d->format.planeCount());
const int bpl(d->data.size()/nb_planes);
for (int i = 0; i < nb_planes; ++i) {
setBytesPerLine(bpl, i);
setBits((uchar*)d->data.constData() + i*bpl, i);
}
//init();
}
/*!
Assigns the contents of \a other to this video frame. Since AudioFrame is
explicitly shared, these two instances will reflect the same frame.
*/
AudioFrame &AudioFrame::operator =(const AudioFrame &other)
{
d_ptr = other.d_ptr;
return *this;
}
AudioFrame::~AudioFrame()
{
}
bool AudioFrame::isValid() const
{
Q_D(const AudioFrame);
return d->samples_per_ch > 0 && d->format.isValid();
}
QByteArray AudioFrame::data()
{
if (!isValid())
return QByteArray();
Q_D(AudioFrame);
if (d->data.isEmpty()) {
AudioFrame a(clone());
d->data = a.data();
}
return d->data;
}
int AudioFrame::channelCount() const
{
Q_D(const AudioFrame);
if (!d->format.isValid())
return 0;
return d->format.channels();
}
AudioFrame AudioFrame::clone() const
{
Q_D(const AudioFrame);
if (d->format.sampleFormatFFmpeg() == AV_SAMPLE_FMT_NONE
|| d->format.channels() <= 0)
return AudioFrame();
if (d->samples_per_ch <= 0 || bytesPerLine(0) <= 0)
return AudioFrame(format());
QByteArray buf(bytesPerLine()*planeCount(), 0);
char *dst = buf.data(); //must before buf is shared, otherwise data will be detached.
for (int i = 0; i < planeCount(); ++i) {
const int plane_size = bytesPerLine(i);
memcpy(dst, constBits(i), plane_size);
dst += plane_size;
}
AudioFrame f(d->format, buf);
f.setSamplesPerChannel(samplesPerChannel());
f.setTimestamp(timestamp());
// meta data?
return f;
}
AudioFormat AudioFrame::format() const
{
return d_func()->format;
}
void AudioFrame::setSamplesPerChannel(int samples)
{
Q_D(AudioFrame);
if (!d->format.isValid()) {
qWarning() << "can not set spc for an invalid format: " << d->format;
return;
}
d->samples_per_ch = samples;
const int nb_planes = d->format.planeCount();
const int bpl(d->line_sizes[0] > 0 ? d->line_sizes[0] : d->samples_per_ch*d->format.bytesPerSample() * (d->format.isPlanar() ? 1 : d->format.channels()));
for (int i = 0; i < nb_planes; ++i) {
setBytesPerLine(bpl, i);
}
if (d->data.isEmpty())
return;
if (!constBits(0)) {
setBits((quint8*)d->data.constData(), 0);
}
for (int i = 1; i < nb_planes; ++i) {
if (!constBits(i)) {
setBits((uchar*)constBits(i-1) + bpl, i);
}
}
}
int AudioFrame::samplesPerChannel() const
{
return d_func()->samples_per_ch;
}
void AudioFrame::setAudioResampler(AudioResampler *conv)
{
d_func()->conv = conv;
}
qint64 AudioFrame::duration() const
{
Q_D(const AudioFrame);
return d->format.durationForBytes(d->data.size());
}
AudioFrame AudioFrame::to(const AudioFormat &fmt) const
{
if (!isValid() || !constBits(0))
return AudioFrame();
//if (fmt == format())
// return clone(); //FIXME: clone a frame from ffmpeg is not enough?
Q_D(const AudioFrame);
// TODO: use a pool
AudioResampler *conv = d->conv;
QScopedPointer<AudioResampler> c;
if (!conv) {
conv = AudioResampler::create(AudioResamplerId_FF);
if (!conv)
conv = AudioResampler::create(AudioResamplerId_Libav);
if (!conv) {
qWarning("no audio resampler is available");
return AudioFrame();
}
c.reset(conv);
}
conv->setInAudioFormat(format());
conv->setOutAudioFormat(fmt);
//conv->prepare(); // already called in setIn/OutFormat
conv->setInSampesPerChannel(samplesPerChannel()); //TODO
if (!conv->convert((const quint8**)d->planes.constData())) {
qWarning() << "AudioFrame::to error: " << format() << "=>" << fmt;
return AudioFrame();
}
AudioFrame f(fmt, conv->outData());
f.setSamplesPerChannel(conv->outSamplesPerChannel());
f.setTimestamp(timestamp());
f.d_ptr->metadata = d->metadata; // need metadata?
return f;
}
} //namespace QtAV