From 0b547413ea96f4063028a2e11d782b6addf162e4 Mon Sep 17 00:00:00 2001 From: wang-bin Date: Thu, 18 Jun 2015 11:26:08 +0800 Subject: [PATCH] minor optimizatoin. less copy/move --- src/AudioThread.cpp | 55 ++++++++++++++++++--------------------------- src/VideoThread.cpp | 18 +++++---------- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/AudioThread.cpp b/src/AudioThread.cpp index f22140b4f..304af2f4e 100644 --- a/src/AudioThread.cpp +++ b/src/AudioThread.cpp @@ -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 */ @@ -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()); @@ -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)); } @@ -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(); diff --git a/src/VideoThread.cpp b/src/VideoThread.cpp index 1f928dc70..ae88bd922 100644 --- a/src/VideoThread.cpp +++ b/src/VideoThread.cpp @@ -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