Skip to content

Commit

Permalink
Add mid function to AudioFrame to get an arbitrary portion of the frame
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFelix committed Oct 13, 2017
1 parent 7bf1cbb commit dff0967
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/AudioFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,50 @@ int AudioFrame::channelCount() const
}

AudioFrame AudioFrame::clone() const
{
return mid(0, -1);
}

AudioFrame AudioFrame::mid(int pos, int len) const
{
Q_D(const AudioFrame);

if (d->format.sampleFormatFFmpeg() == AV_SAMPLE_FMT_NONE
|| d->format.channels() <= 0)
|| d->format.channels() <= 0) {
return AudioFrame();
if (d->samples_per_ch <= 0 || bytesPerLine(0) <= 0)
}

if (d->samples_per_ch <= 0 || bytesPerLine(0) <= 0 || len == 0) {
return AudioFrame(format());
QByteArray buf(bytesPerLine()*planeCount(), 0);
}

int bufSize = bytesPerLine();
int posBytes = 0;
if (pos > 0) {
posBytes = pos * d->format.bytesPerSample();
bufSize -= posBytes;
} else {
pos = 0;
}

int lenBytes = len * d->format.bytesPerSample();
if (len > 0 && lenBytes < bufSize) {
bufSize = lenBytes;
} else {
lenBytes = bufSize;
}

QByteArray buf(bufSize * 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;
memcpy(dst, constBits(i) + posBytes, lenBytes);
dst += lenBytes;
}

AudioFrame f(d->format, buf);
f.setSamplesPerChannel(samplesPerChannel());
f.setTimestamp(timestamp());
f.setSamplesPerChannel(bufSize / d->format.bytesPerSample());
f.setTimestamp(d->timestamp + (qreal) d->format.durationForBytes(posBytes) / AudioFormat::kHz);
// meta data?
return f;
}
Expand Down
1 change: 1 addition & 0 deletions src/QtAV/AudioFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Q_AV_EXPORT AudioFrame : public Frame
* then you can use clone().
*/
AudioFrame clone() const;
AudioFrame mid(int pos, int len = -1) const;
AudioFormat format() const;
void setSamplesPerChannel(int samples);
// may change after resampling
Expand Down

0 comments on commit dff0967

Please sign in to comment.