6#include "playbackengine/qffmpegmediadataholder_p.h"
7#include <QtCore/qloggingcategory.h>
8#include <QtCore/qregularexpression.h>
9#include <QtCore/qspan.h>
13Q_STATIC_LOGGING_CATEGORY(qLcStreamDecoder,
"qt.multimedia.ffmpeg.streamdecoder");
19StreamDecoder::StreamDecoder(
const PlaybackEngineObjectID &id,
const CodecContext &codecContext,
20 TrackPosition absSeekPos)
21 : PlaybackEngineObject(id),
22 m_codecContext(codecContext),
23 m_trackType(MediaDataHolder::trackTypeFromMediaType(codecContext.context()->codec_type)),
24 m_sessionCtx{ absSeekPos }
26 qCDebug(qLcStreamDecoder) <<
"Create stream decoder, trackType" << m_trackType
27 <<
"absSeekPos:" << absSeekPos.get();
28 Q_ASSERT(m_trackType != QPlatformMediaPlayer::NTrackTypes);
33 avcodec_flush_buffers(m_codecContext.context());
38 updateSession(sessionID, [
this, pos, offset]() {
39 m_sessionCtx = { offset.loopStartTimeUs.asDuration() + pos };
40 avcodec_flush_buffers(m_codecContext.context());
46 if (checkSessionID(sourceID.sessionID))
52 if (packet.isValid() && !checkSessionID(packet.sourceID().sessionID)) {
53 qCDebug(qLcStreamDecoder) <<
"Packet session outdated. Source id:" << packet.sourceID()
54 <<
"current id" << id();
59 m_sessionCtx.packets.enqueue(std::move(packet));
65 Packet packet = m_sessionCtx.packets.dequeue();
67 auto decodePacket = [
this](
const Packet &packet) {
68 if (trackType() == QPlatformMediaPlayer::SubtitleStream)
69 decodeSubtitle(packet);
74 if (packet.isValid() && packet.loopOffset().loopIndex != m_sessionCtx.offset.loopIndex) {
77 qCDebug(qLcStreamDecoder) <<
"flush buffers due to new loop:"
78 << packet.loopOffset().loopIndex;
80 avcodec_flush_buffers(m_codecContext.context());
81 m_sessionCtx.offset = packet.loopOffset();
86 setAtEnd(!packet.isValid());
89 emit packetProcessed(
std::move(packet));
116 if (!checkID(frame.sourceID()))
119 --m_sessionCtx.pendingFramesCount;
120 Q_ASSERT(m_sessionCtx.pendingFramesCount >= 0);
127 const qint32 maxCount = maxQueueSize(m_trackType);
129 return !m_sessionCtx.packets.empty() && m_sessionCtx.pendingFramesCount < maxCount
130 && PlaybackEngineObject::canDoNextStep();
135 if (frame.isValid() && frame.absoluteEnd() < m_sessionCtx.absSeekPos)
138 Q_ASSERT(m_sessionCtx.pendingFramesCount >= 0);
139 ++m_sessionCtx.pendingFramesCount;
140 emit requestHandleFrame(frame);
145 auto sendPacketResult = sendAVPacket(packet);
147 if (sendPacketResult == AVERROR(EAGAIN)) {
154 sendPacketResult = sendAVPacket(packet);
156 if (sendPacketResult != AVERROR(EAGAIN))
157 qWarning() <<
"Unexpected FFmpeg behavior";
160 if (sendPacketResult == 0)
161 receiveAVFrames(!packet.isValid());
166 return avcodec_send_packet(m_codecContext.context(), packet.isValid() ? packet.avPacket() :
nullptr);
172 auto avFrame = makeAVFrame();
174 const auto receiveFrameResult = avcodec_receive_frame(m_codecContext.context(), avFrame.get());
176 if (receiveFrameResult == AVERROR_EOF || receiveFrameResult == AVERROR(EAGAIN)) {
177 if (flushPacket && receiveFrameResult == AVERROR(EAGAIN)) {
185 qWarning() <<
"Unexpected FFmpeg behavior: EAGAIN state for avcodec_receive_frame "
186 <<
"at end of the stream";
193 if (receiveFrameResult < 0) {
194 emit error(QMediaPlayer::FormatError, err2str(receiveFrameResult));
200 if (m_trackType == QPlatformMediaPlayer::VideoStream)
201 avFrame = copyFromHwPool(
std::move(avFrame));
203 onFrameFound({ m_sessionCtx.offset, std::move(avFrame), m_codecContext, id() });
209 if (!packet.isValid())
214 memset(&subtitle, 0,
sizeof(subtitle));
218 avcodec_decode_subtitle2(m_codecContext.context(), &subtitle, &gotSubtitle, packet.avPacket());
221 if (res < 0 || !gotSubtitle)
226 TrackPosition start = 0, end = 0;
227 if (subtitle.pts == AV_NOPTS_VALUE) {
228 start = m_codecContext.toTrackPosition(AVStreamPosition(packet.avPacket()->pts));
229 end = start + m_codecContext.toTrackDuration(AVStreamDuration(packet.avPacket()->duration));
231 auto pts = timeStampUs(subtitle.pts, AVRational{ 1, AV_TIME_BASE });
232 start = TrackPosition(*pts + qint64(subtitle.start_display_time) * 1000);
233 end = TrackPosition(*pts + qint64(subtitle.end_display_time) * 1000);
237 qWarning() <<
"Invalid subtitle time";
241 QString text = subtitleTextFromAVSubtitle(subtitle);
243 onFrameFound({ m_sessionCtx.offset, text, start, end - start, id() });
246 onFrameFound({ m_sessionCtx.offset, QString(), end, TrackDuration(0), id() });
252 return QString::fromUtf8(r.text);
254 const char *ass = r.ass;
263 return QString::fromUtf8(ass);
270 for (
const AVSubtitleRect *r : QSpan(subtitle.rects, subtitle.num_rects)) {
271 if (r != subtitle.rects[0])
274 text += extractSubtitleText(*r);
276 static const QRegularExpression lineBreakRe(u"\\\\N|\\\\n|\\r\\n"_s);
277 text.replace(lineBreakRe, u"\n"_s);
278 if (text.endsWith(u'\n'))
287#include "moc_qffmpegstreamdecoder_p.cpp"
bool canDoNextStep() const override
~StreamDecoder() override
void doNextStep() override
void onFrameProcessed(const Frame &frame)
void onFinalPacketReceived(PlaybackEngineObjectID sourceID)
void seek(quint64 sessionID, TrackPosition pos, const LoopOffset &offset)
QPlatformMediaPlayer::TrackType trackType() const
QT_MANGLE_NAMESPACE(QMacScreenCaptureStreamDelegate) QMacScreenCaptureStreamDelegate
QString subtitleTextFromAVSubtitle(const AVSubtitle &subtitle)
static QString extractSubtitleText(const AVSubtitleRect &r)
Combined button and popup list for selecting options.