summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/playbackengine/qffmpegstreamdecoder.cpp
blob: 2d9d63b90f2f6886472aa9054a00605c757f6071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "playbackengine/qffmpegstreamdecoder_p.h"
#include "playbackengine/qffmpegmediadataholder_p.h"
#include <qloggingcategory.h>

QT_BEGIN_NAMESPACE

Q_STATIC_LOGGING_CATEGORY(qLcStreamDecoder, "qt.multimedia.ffmpeg.streamdecoder");

namespace QFFmpeg {

StreamDecoder::StreamDecoder(const Codec &codec, qint64 absSeekPos)
    : m_codec(codec),
      m_absSeekPos(absSeekPos),
      m_trackType(MediaDataHolder::trackTypeFromMediaType(codec.context()->codec_type))
{
    qCDebug(qLcStreamDecoder) << "Create stream decoder, trackType" << m_trackType
                              << "absSeekPos:" << absSeekPos;
    Q_ASSERT(m_trackType != QPlatformMediaPlayer::NTrackTypes);
}

StreamDecoder::~StreamDecoder()
{
    avcodec_flush_buffers(m_codec.context());
}

void StreamDecoder::onFinalPacketReceived()
{
    decode({});
}

void StreamDecoder::setInitialPosition(TimePoint, qint64 trackPos)
{
    m_absSeekPos = trackPos;
}

void StreamDecoder::decode(Packet packet)
{
    m_packets.enqueue(packet);

    scheduleNextStep();
}

void StreamDecoder::doNextStep()
{
    auto packet = m_packets.dequeue();

    auto decodePacket = [this](Packet packet) {
        if (trackType() == QPlatformMediaPlayer::SubtitleStream)
            decodeSubtitle(packet);
        else
            decodeMedia(packet);
    };

    if (packet.isValid() && packet.loopOffset().index != m_offset.index) {
        decodePacket({});

        qCDebug(qLcStreamDecoder) << "flush buffers due to new loop:" << packet.loopOffset().index;

        avcodec_flush_buffers(m_codec.context());
        m_offset = packet.loopOffset();
    }

    decodePacket(packet);

    setAtEnd(!packet.isValid());

    if (packet.isValid())
        emit packetProcessed(packet);

    scheduleNextStep(false);
}

QPlatformMediaPlayer::TrackType StreamDecoder::trackType() const
{
    return m_trackType;
}

qint32 StreamDecoder::maxQueueSize(QPlatformMediaPlayer::TrackType type)
{
    switch (type) {

    case QPlatformMediaPlayer::VideoStream:
        return 3;
    case QPlatformMediaPlayer::AudioStream:
        return 9;
    case QPlatformMediaPlayer::SubtitleStream:
        return 6; /*main packet and closing packet*/
    default:
        Q_UNREACHABLE_RETURN(-1);
    }
}

void StreamDecoder::onFrameProcessed(Frame frame)
{
    if (frame.sourceId() != id())
        return;

    --m_pendingFramesCount;
    Q_ASSERT(m_pendingFramesCount >= 0);

    scheduleNextStep();
}

bool StreamDecoder::canDoNextStep() const
{
    const qint32 maxCount = maxQueueSize(m_trackType);

    return !m_packets.empty() && m_pendingFramesCount < maxCount
            && PlaybackEngineObject::canDoNextStep();
}

void StreamDecoder::onFrameFound(Frame frame)
{
    if (frame.isValid() && frame.absoluteEnd() < m_absSeekPos)
        return;

    Q_ASSERT(m_pendingFramesCount >= 0);
    ++m_pendingFramesCount;
    emit requestHandleFrame(frame);
}

void StreamDecoder::decodeMedia(Packet packet)
{
    auto sendPacketResult = sendAVPacket(packet);

    if (sendPacketResult == AVERROR(EAGAIN)) {
        // Doc says:
        //  AVERROR(EAGAIN): input is not accepted in the current state - user
        //                   must read output with avcodec_receive_frame() (once
        //                   all output is read, the packet should be resent, and
        //                   the call will not fail with EAGAIN).
        receiveAVFrames();
        sendPacketResult = sendAVPacket(packet);

        if (sendPacketResult != AVERROR(EAGAIN))
            qWarning() << "Unexpected FFmpeg behavior";
    }

    if (sendPacketResult == 0)
        receiveAVFrames();
}

int StreamDecoder::sendAVPacket(Packet packet)
{
    return avcodec_send_packet(m_codec.context(), packet.isValid() ? packet.avPacket() : nullptr);
}

void StreamDecoder::receiveAVFrames()
{
    while (true) {
        auto avFrame = makeAVFrame();

        const auto receiveFrameResult = avcodec_receive_frame(m_codec.context(), avFrame.get());

        if (receiveFrameResult == AVERROR_EOF || receiveFrameResult == AVERROR(EAGAIN))
            break;

        if (receiveFrameResult < 0) {
            emit error(QMediaPlayer::FormatError, err2str(receiveFrameResult));
            break;
        }


        // Avoid starvation on FFmpeg decoders with fixed size frame pool
        if (m_trackType == QPlatformMediaPlayer::VideoStream)
            avFrame = copyFromHwPool(std::move(avFrame));

        onFrameFound({ m_offset, std::move(avFrame), m_codec, 0, id() });
    }
}

void StreamDecoder::decodeSubtitle(Packet packet)
{
    if (!packet.isValid())
        return;
    //    qCDebug(qLcDecoder) << "    decoding subtitle" << "has delay:" <<
    //    (codec->codec->capabilities & AV_CODEC_CAP_DELAY);
    AVSubtitle subtitle;
    memset(&subtitle, 0, sizeof(subtitle));
    int gotSubtitle = 0;

    const int res =
            avcodec_decode_subtitle2(m_codec.context(), &subtitle, &gotSubtitle, packet.avPacket());
    //    qCDebug(qLcDecoder) << "       subtitle got:" << res << gotSubtitle << subtitle.format <<
    //    Qt::hex << (quint64)subtitle.pts;
    if (res < 0 || !gotSubtitle)
        return;

    // apparently the timestamps in the AVSubtitle structure are not always filled in
    // if they are missing, use the packets pts and duration values instead
    qint64 start, end;
    if (subtitle.pts == AV_NOPTS_VALUE) {
        start = m_codec.toUs(packet.avPacket()->pts);
        end = start + m_codec.toUs(packet.avPacket()->duration);
    } else {
        auto pts = timeStampUs(subtitle.pts, AVRational{ 1, AV_TIME_BASE });
        start = *pts + qint64(subtitle.start_display_time) * 1000;
        end = *pts + qint64(subtitle.end_display_time) * 1000;
    }

    if (end <= start) {
        qWarning() << "Invalid subtitle time";
        return;
    }
    //        qCDebug(qLcDecoder) << "    got subtitle (" << start << "--" << end << "):";
    QString text;
    for (uint i = 0; i < subtitle.num_rects; ++i) {
        const auto *r = subtitle.rects[i];
        //            qCDebug(qLcDecoder) << "    subtitletext:" << r->text << "/" << r->ass;
        if (i)
            text += QLatin1Char('\n');
        if (r->text)
            text += QString::fromUtf8(r->text);
        else {
            const char *ass = r->ass;
            int nCommas = 0;
            while (*ass) {
                if (nCommas == 8)
                    break;
                if (*ass == ',')
                    ++nCommas;
                ++ass;
            }
            text += QString::fromUtf8(ass);
        }
    }
    text.replace(QLatin1String("\\N"), QLatin1String("\n"));
    text.replace(QLatin1String("\\n"), QLatin1String("\n"));
    text.replace(QLatin1String("\r\n"), QLatin1String("\n"));
    if (text.endsWith(QLatin1Char('\n')))
        text.chop(1);

    onFrameFound({ m_offset, text, start, end - start, id() });

    // TODO: maybe optimize
    onFrameFound({ m_offset, QString(), end, 0, id() });
}
} // namespace QFFmpeg

QT_END_NAMESPACE

#include "moc_qffmpegstreamdecoder_p.cpp"