Skip to content

Commit

Permalink
minor optimizatoin. less copy/move
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Jun 18, 2015
1 parent fa76399 commit 0b54741
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
55 changes: 22 additions & 33 deletions src/AudioThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ void AudioThread::run()
}
}
}

/* lock here to ensure decoder and ao can complete current work before they are changed
* current packet maybe not supported by new decoder
*/
Expand Down Expand Up @@ -219,29 +218,31 @@ void AudioThread::run()
d.last_pts = d.clock->value(); //not pkt.pts! the delay is updated!
continue;
}
// reduce here to ensure to decode the rest data in the next loop
pkt.data = QByteArray::fromRawData(pkt.data.constData() + pkt.data.size() - dec->undecodedSize(), dec->undecodedSize());
#if USE_AUDIO_FRAME
AudioFrame frame(dec->frame());
if (frame) {
if (d.render_pts0 >= 0.0) { // seeking
if (frame.timestamp() < d.render_pts0) {
qDebug("skip audio rendering: %f-%f", frame.timestamp(), d.render_pts0);
d.clock->updateValue(frame.timestamp());
pkt = Packet();
continue;
}
d.render_pts0 = -1.0;
Q_EMIT seekFinished(qint64(frame.timestamp()*1000.0));
}
if (has_ao) {
applyFilters(frame);
frame.setAudioResampler(dec->resampler()); //!!!
// FIXME: resample is required for audio frames from ffmpeg
//if (ao->audioFormat() != frame.format()) {
frame = frame.to(ao->audioFormat());
//}
if (!frame)
continue;
if (frame.timestamp() <= 0)
frame.setTimestamp(pkt.pts); // pkt.pts is wrong. >= real timestamp
if (d.render_pts0 >= 0.0) { // seeking
if (frame.timestamp() < d.render_pts0) {
qDebug("skip audio rendering: %f-%f", frame.timestamp(), d.render_pts0);
d.clock->updateValue(frame.timestamp());
continue;
}
} // no continue if frame is invalid. decoder may need more data to get a frame

d.render_pts0 = -1.0;
Q_EMIT seekFinished(qint64(frame.timestamp()*1000.0));
}
if (has_ao) {
applyFilters(frame);
frame.setAudioResampler(dec->resampler()); //!!!
// FIXME: resample ONCE is required for audio frames from ffmpeg
//if (ao->audioFormat() != frame.format()) {
frame = frame.to(ao->audioFormat());
//}
}
QByteArray decoded(frame.data());
#else
QByteArray decoded(dec->data());
Expand Down Expand Up @@ -274,11 +275,6 @@ void AudioThread::run()
* the advantage is if no audio device, the play speed is ok too
* So is portaudio blocking the thread when playing?
*/
static bool sWarn_no_ao = true; //FIXME: no warning when replay. warn only once
if (sWarn_no_ao) {
qDebug("Audio output not available! msleep(%lu)", (unsigned long)((qreal)chunk/(qreal)byte_rate * 1000));
sWarn_no_ao = false;
}
//TODO: avoid acummulative error. External clock?
msleep((unsigned long)(chunk_delay * 1000.0));
}
Expand All @@ -287,13 +283,6 @@ void AudioThread::run()
}
if (has_ao)
emit frameDelivered();
int undecoded = dec->undecodedSize();
if (undecoded > 0) {
pkt.data.remove(0, pkt.data.size() - undecoded);
} else {
pkt = Packet();
}

d.last_pts = d.clock->value(); //not pkt.pts! the delay is updated!
}
d.packets.clear();
Expand Down
18 changes: 5 additions & 13 deletions src/VideoThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,26 +445,18 @@ void VideoThread::run()
if (dec_opt != dec_opt_old)
dec->setOptions(*dec_opt);
if (!dec->decode(pkt)) {
qWarning("Decode video failed. undecoded: %d", dec->undecodedSize());
pkt = Packet();
continue;
} else {
int undecoded = dec->undecodedSize();
if (undecoded > 0) {
qDebug("undecoded size: %d", undecoded);
const int remove = pkt.data.size() - undecoded;
if (remove > 0)
pkt.data.remove(0, pkt.data.size() - undecoded);
} else {
pkt = Packet();
}
}
// reduce here to ensure to decode the rest data in the next loop
pkt.data = QByteArray::fromRawData(pkt.data.constData() + pkt.data.size() - dec->undecodedSize(), dec->undecodedSize());
VideoFrame frame = dec->frame();
if (!frame.isValid()) {
pkt = Packet(); //mark invalid to take next
qWarning() << "invalid video frame from decoder";
qWarning("invalid video frame from decoder. undecoded data size: %d", pkt.data.size());
continue;
}
if (frame.timestamp() == 0)
if (frame.timestamp() <= 0)
frame.setTimestamp(pkt.pts); // pkt.pts is wrong. >= real timestamp
const qreal pts = frame.timestamp();
// seek finished because we can ensure no packet before seek decoded when render_pts0 is set
Expand Down

0 comments on commit 0b54741

Please sign in to comment.