4#include "playbackengine/qffmpegstreamdecoder_p.h"
5#include "playbackengine/qffmpegmediadataholder_p.h"
6#include <qloggingcategory.h>
14StreamDecoder::StreamDecoder(
const PlaybackEngineObjectID &id,
const CodecContext &codecContext,
15 TrackPosition absSeekPos)
16 : PlaybackEngineObject(id),
17 m_codecContext(codecContext),
18 m_absSeekPos(absSeekPos),
19 m_trackType(MediaDataHolder::trackTypeFromMediaType(codecContext.context()->codec_type))
21 qCDebug(qLcStreamDecoder) <<
"Create stream decoder, trackType" << m_trackType
22 <<
"absSeekPos:" << absSeekPos.get();
23 Q_ASSERT(m_trackType != QPlatformMediaPlayer::NTrackTypes);
28 avcodec_flush_buffers(m_codecContext.context());
33 if (checkSessionID(sourceID.sessionID))
39 if (packet.isValid() && !checkSessionID(packet.sourceID().sessionID)) {
40 qCDebug(qLcStreamDecoder) <<
"Packet session outdated. Source id:" << packet.sourceID()
41 <<
"current id" << id();
46 m_packets.enqueue(std::move(packet));
52 Packet packet = m_packets.dequeue();
54 auto decodePacket = [
this](
const Packet &packet) {
55 if (trackType() == QPlatformMediaPlayer::SubtitleStream)
56 decodeSubtitle(packet);
61 if (packet.isValid() && packet.loopOffset().loopIndex != m_offset.loopIndex) {
64 qCDebug(qLcStreamDecoder) <<
"flush buffers due to new loop:"
65 << packet.loopOffset().loopIndex;
67 avcodec_flush_buffers(m_codecContext.context());
68 m_offset = packet.loopOffset();
73 setAtEnd(!packet.isValid());
76 emit packetProcessed(
std::move(packet));
103 if (!checkID(frame.sourceID()))
106 --m_pendingFramesCount;
107 Q_ASSERT(m_pendingFramesCount >= 0);
114 const qint32 maxCount = maxQueueSize(m_trackType);
116 return !m_packets.empty() && m_pendingFramesCount < maxCount
117 && PlaybackEngineObject::canDoNextStep();
122 if (frame.isValid() && frame.absoluteEnd() < m_absSeekPos)
125 Q_ASSERT(m_pendingFramesCount >= 0);
126 ++m_pendingFramesCount;
127 emit requestHandleFrame(frame);
132 auto sendPacketResult = sendAVPacket(packet);
134 if (sendPacketResult == AVERROR(EAGAIN)) {
141 sendPacketResult = sendAVPacket(packet);
143 if (sendPacketResult != AVERROR(EAGAIN))
144 qWarning() <<
"Unexpected FFmpeg behavior";
147 if (sendPacketResult == 0)
148 receiveAVFrames(!packet.isValid());
153 return avcodec_send_packet(m_codecContext.context(), packet.isValid() ? packet.avPacket() :
nullptr);
159 auto avFrame = makeAVFrame();
161 const auto receiveFrameResult = avcodec_receive_frame(m_codecContext.context(), avFrame.get());
163 if (receiveFrameResult == AVERROR_EOF || receiveFrameResult == AVERROR(EAGAIN)) {
164 if (flushPacket && receiveFrameResult == AVERROR(EAGAIN)) {
172 qWarning() <<
"Unexpected FFmpeg behavior: EAGAIN state for avcodec_receive_frame "
173 <<
"at end of the stream";
180 if (receiveFrameResult < 0) {
181 emit error(QMediaPlayer::FormatError, err2str(receiveFrameResult));
187 if (m_trackType == QPlatformMediaPlayer::VideoStream)
188 avFrame = copyFromHwPool(
std::move(avFrame));
190 onFrameFound({ m_offset, std::move(avFrame), m_codecContext, id() });
196 if (!packet.isValid())
201 memset(&subtitle, 0,
sizeof(subtitle));
205 avcodec_decode_subtitle2(m_codecContext.context(), &subtitle, &gotSubtitle, packet.avPacket());
208 if (res < 0 || !gotSubtitle)
213 TrackPosition start = 0, end = 0;
214 if (subtitle.pts == AV_NOPTS_VALUE) {
215 start = m_codecContext.toTrackPosition(AVStreamPosition(packet.avPacket()->pts));
216 end = start + m_codecContext.toTrackDuration(AVStreamDuration(packet.avPacket()->duration));
218 auto pts = timeStampUs(subtitle.pts, AVRational{ 1, AV_TIME_BASE });
219 start = TrackPosition(*pts + qint64(subtitle.start_display_time) * 1000);
220 end = TrackPosition(*pts + qint64(subtitle.end_display_time) * 1000);
224 qWarning() <<
"Invalid subtitle time";
229 for (uint i = 0; i < subtitle.num_rects; ++i) {
230 const auto *r = subtitle.rects[i];
233 text += QLatin1Char(
'\n');
235 text += QString::fromUtf8(r->text);
237 const char *ass = r->ass;
246 text += QString::fromUtf8(ass);
249 text.replace(QLatin1String(
"\\N"), QLatin1String(
"\n"));
250 text.replace(QLatin1String(
"\\n"), QLatin1String(
"\n"));
251 text.replace(QLatin1String(
"\r\n"), QLatin1String(
"\n"));
252 if (text.endsWith(QLatin1Char(
'\n')))
255 onFrameFound({ m_offset, text, start, end - start, id() });
258 onFrameFound({ m_offset, QString(), end, TrackDuration(0), id() });
264#include "moc_qffmpegstreamdecoder_p.cpp"
bool canDoNextStep() const override
~StreamDecoder() override
void doNextStep() override
void onFrameProcessed(Frame frame)
void onFinalPacketReceived(PlaybackEngineObjectID sourceID)
QPlatformMediaPlayer::TrackType trackType() const
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType
Combined button and popup list for selecting options.
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)