summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/playbackengine/qffmpegframe_p.h
blob: bccb13ebe649176a8590dcfe66706389259453b3 (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
// 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

#ifndef QFFMPEGFRAME_P_H
#define QFFMPEGFRAME_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qffmpeg_p.h"
#include "playbackengine/qffmpegcodec_p.h"
#include "QtCore/qsharedpointer.h"
#include "qpointer.h"
#include "qobject.h"

#include <optional>

QT_BEGIN_NAMESPACE

namespace QFFmpeg {

struct Frame
{
    struct Data
    {
        Data(AVFrameUPtr f, const Codec &codec, qint64, const QObject *source)
            : codec(codec), frame(std::move(f)), source(source)
        {
            Q_ASSERT(frame);
            if (frame->pts != AV_NOPTS_VALUE)
                pts = codec.toUs(frame->pts);
            else
                pts = codec.toUs(frame->best_effort_timestamp);
            const auto &avgFrameRate = codec.stream()->avg_frame_rate;
            duration = avgFrameRate.num
                    ? (1000000 * avgFrameRate.den + avgFrameRate.num / 2) / avgFrameRate.num
                    : 0;
        }
        Data(const QString &text, qint64 pts, qint64 duration, const QObject *source)
            : text(text), pts(pts), duration(duration), source(source)
        {
        }

        QAtomicInt ref;
        std::optional<Codec> codec;
        AVFrameUPtr frame;
        QString text;
        qint64 pts = -1;
        qint64 duration = -1;
        QPointer<const QObject> source;
    };
    Frame() = default;

    Frame(AVFrameUPtr f, const Codec &codec, qint64 pts, const QObject *source = nullptr)
        : d(new Data(std::move(f), codec, pts, source))
    {
    }
    Frame(const QString &text, qint64 pts, qint64 duration, const QObject *source = nullptr)
        : d(new Data(text, pts, duration, source))
    {
    }
    bool isValid() const { return !!d; }

    AVFrame *avFrame() const { return data().frame.get(); }
    AVFrameUPtr takeAVFrame() { return std::move(data().frame); }
    const Codec *codec() const { return data().codec ? &data().codec.value() : nullptr; }
    qint64 pts() const { return data().pts; }
    qint64 duration() const { return data().duration; }
    qint64 end() const { return data().pts + data().duration; }
    QString text() const { return data().text; }
    const QObject *source() const { return data().source; };

private:
    Data &data() const
    {
        Q_ASSERT(d);
        return *d;
    }

private:
    QExplicitlySharedDataPointer<Data> d;
};

} // namespace QFFmpeg

QT_END_NAMESPACE

Q_DECLARE_METATYPE(QFFmpeg::Frame);

#endif // QFFMPEGFRAME_P_H