summaryrefslogtreecommitdiffstats
path: root/src/sources/soundsourceffmpeg.h
blob: a36e49c05ce51a5be53c87d9b8a89658223ac1e4 (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
#pragma once

extern "C" {

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>

} // extern "C"

#include "sources/readaheadframebuffer.h"
#include "sources/soundsourceprovider.h"

namespace mixxx {

class SoundSourceFFmpeg : public SoundSource {
  public:
    explicit SoundSourceFFmpeg(const QUrl& url);
    ~SoundSourceFFmpeg() override;

    void close() override;

  protected:
    ReadableSampleFrames readSampleFramesClamped(
            const WritableSampleFrames& sampleFrames) override;

  private:
    OpenResult tryOpen(
            OpenMode mode,
            const OpenParams& params) override;

    bool initResampling(
            audio::ChannelCount* pResampledChannelCount,
            audio::SampleRate* pResampledSampleRate);
    const CSAMPLE* resampleDecodedAVFrame();

    // Seek to the requested start index (if needed) or return false
    // upon seek errors.
    bool adjustCurrentPosition(
            SINT startIndex);

    bool consumeNextAVPacket(
            AVPacket** ppavNextPacket);

    // Takes ownership of an input format context and ensures that
    // the corresponding AVFormatContext is closed, either explicitly
    // or implicitly by the destructor. The wrapper can only be
    // moved, copying is disabled.
    class InputAVFormatContextPtr final {
      public:
        explicit InputAVFormatContextPtr(AVFormatContext* pavInputFormatContext = nullptr)
                : m_pavInputFormatContext(pavInputFormatContext) {
        }
        InputAVFormatContextPtr(const InputAVFormatContextPtr&) = delete;
        InputAVFormatContextPtr(InputAVFormatContextPtr&& that)
                : m_pavInputFormatContext(that.m_pavInputFormatContext) {
            that.m_pavInputFormatContext = nullptr;
        }
        ~InputAVFormatContextPtr() {
            close();
        }

        void take(AVFormatContext** ppavInputFormatContext);

        void close();

        friend void swap(InputAVFormatContextPtr& lhs, InputAVFormatContextPtr& rhs) {
            std::swap(lhs.m_pavInputFormatContext, rhs.m_pavInputFormatContext);
        }

        InputAVFormatContextPtr& operator=(const InputAVFormatContextPtr&) = delete;
        InputAVFormatContextPtr& operator=(InputAVFormatContextPtr&& that) {
            swap(*this, that);
            return *this;
        }

        AVFormatContext* operator->() {
            return m_pavInputFormatContext;
        }
        operator AVFormatContext*() {
            return m_pavInputFormatContext;
        }

      private:
        AVFormatContext* m_pavInputFormatContext;
    };
    InputAVFormatContextPtr m_pavInputFormatContext;

    AVStream* m_pavStream;

    // Takes ownership of an opened (audio) codec context and ensures that
    // the corresponding AVCodecContext is closed, either explicitly or
    // implicitly by the destructor. The wrapper can only be moved,
    // copying is disabled.
    class AVCodecContextPtr final {
      public:
        static AVCodecContextPtr alloc(const AVCodec* codec);

        explicit AVCodecContextPtr(AVCodecContext* pavCodecContext = nullptr)
                : m_pavCodecContext(pavCodecContext) {
        }
        AVCodecContextPtr(const AVCodecContextPtr&) = delete;
        AVCodecContextPtr(AVCodecContextPtr&& that)
                : m_pavCodecContext(that.m_pavCodecContext) {
            that.m_pavCodecContext = nullptr;
        }
        ~AVCodecContextPtr() {
            close();
        }

        void take(AVCodecContext** ppavCodecContext);
        void close();

        friend void swap(AVCodecContextPtr& lhs, AVCodecContextPtr& rhs) {
            std::swap(lhs.m_pavCodecContext, rhs.m_pavCodecContext);
        }

        AVCodecContextPtr& operator=(const AVCodecContextPtr&) = delete;
        AVCodecContextPtr& operator=(AVCodecContextPtr&& that) {
            swap(*this, that);
            return *this;
        }

        AVCodecContext* operator->() {
            return m_pavCodecContext;
        }
        operator AVCodecContext*() {
            return m_pavCodecContext;
        }

      private:
        AVCodecContext* m_pavCodecContext;
    };
    AVCodecContextPtr m_pavCodecContext;

    // Resampler
    class SwrContextPtr final {
      public:
        explicit SwrContextPtr(SwrContext* m_pSwrContext = nullptr)
                : m_pSwrContext(m_pSwrContext) {
        }
        SwrContextPtr(const SwrContextPtr&) = delete;
        SwrContextPtr(SwrContextPtr&& that)
                : m_pSwrContext(that.m_pSwrContext) {
            that.m_pSwrContext = nullptr;
        }
        ~SwrContextPtr() {
            close();
        }

        void take(SwrContext** ppSwrContext);

        void close();

        friend void swap(SwrContextPtr& lhs, SwrContextPtr& rhs) {
            std::swap(lhs.m_pSwrContext, rhs.m_pSwrContext);
        }

        SwrContextPtr& operator=(const SwrContextPtr&) = delete;
        SwrContextPtr& operator=(SwrContextPtr&& that) {
            swap(*this, that);
            return *this;
        }

        SwrContext* operator->() {
            return m_pSwrContext;
        }
        operator SwrContext*() {
            return m_pSwrContext;
        }

      private:
        SwrContext* m_pSwrContext;
    };
    SwrContextPtr m_pSwrContext;

    uint64_t m_avStreamChannelLayout;
    uint64_t m_avResampledChannelLayout;

    AVPacket* m_pavPacket;

    AVFrame* m_pavDecodedFrame;
    AVFrame* m_pavResampledFrame;

    FrameCount m_seekPrerollFrameCount;

    ReadAheadFrameBuffer m_frameBuffer;
};

class SoundSourceProviderFFmpeg : public SoundSourceProvider {
  public:
    static const QString kDisplayName;

    SoundSourceProviderFFmpeg();

    QString getDisplayName() const override {
        return kDisplayName;
    }

    QStringList getSupportedFileExtensions() const override;

    SoundSourceProviderPriority getPriorityHint(
            const QString& supportedFileExtension) const override;

    SoundSourcePointer newSoundSource(const QUrl& url) override {
        return newSoundSourceFromUrl<SoundSourceFFmpeg>(url);
    }
};

} // namespace mixxx