media_decoder.h 3.47 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2013-2019 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Guillaume Roguez's avatar
Guillaume Roguez committed
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
19
 */
20
#pragma once
21 22 23

#include "config.h"

24 25
#include "rational.h"

Adrien Béraud's avatar
Adrien Béraud committed
26
#ifdef ENABLE_VIDEO
27 28
#include "video/video_base.h"
#include "video/video_scaler.h"
Adrien Béraud's avatar
Adrien Béraud committed
29
#endif // ENABLE_VIDEO
30

31 32
#include "audio/audiobuffer.h"

33
#include "media_device.h"
Philippe Gorley's avatar
Philippe Gorley committed
34
#include "media_stream.h"
35
#include "rational.h"
36 37
#include "noncopyable.h"

38
#include <map>
39
#include <string>
40
#include <memory>
41
#include <chrono>
42

43
extern "C" {
44 45 46 47 48
struct AVCodecContext;
struct AVStream;
struct AVDictionary;
struct AVFormatContext;
struct AVCodec;
Philippe Gorley's avatar
Philippe Gorley committed
49
enum AVMediaType;
50
}
51

52
namespace DRing {
Adrien Béraud's avatar
Adrien Béraud committed
53
class AudioFrame;
54 55
}

Adrien Béraud's avatar
Adrien Béraud committed
56
namespace jami {
Guillaume Roguez's avatar
Guillaume Roguez committed
57

58
using AudioFrame = DRing::AudioFrame;
59
struct AudioFormat;
60 61 62
class RingBuffer;
class Resampler;
class MediaIOHandle;
63

Philippe Gorley's avatar
Philippe Gorley committed
64
#ifdef RING_ACCEL
65 66 67
namespace video {
class HardwareAccel;
}
Philippe Gorley's avatar
Philippe Gorley committed
68 69
#endif

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
class MediaDecoder {
    public:
        enum class Status {
            Success,
            FrameFinished,
            EOFError,
            ReadError,
            DecodeError,
            RestartRequired
        };

        MediaDecoder();
        ~MediaDecoder();

        void emulateRate() { emulateRate_ = true; }
        void setInterruptCallback(int (*cb)(void*), void *opaque);
        int openInput(const DeviceParams&);

        void setIOContext(MediaIOHandle *ioctx);
#ifdef ENABLE_VIDEO
        int setupFromVideoData();
        Status decode(VideoFrame&);
        Status flush(VideoFrame&);
#endif // ENABLE_VIDEO

        int setupFromAudioData();
        Status decode(AudioFrame&);

        int getWidth() const;
        int getHeight() const;
        std::string getDecoderName() const;
Gasuleg's avatar
Gasuleg committed
101

102 103 104 105 106 107 108
        rational<double> getFps() const;
        AVPixelFormat getPixelFormat() const;

        void setOptions(const std::map<std::string, std::string>& options);
#ifdef RING_ACCEL
        void enableAccel(bool enableAccel);
#endif
109

110
        MediaStream getStream(std::string name = "") const;
111

112 113
    private:
        NON_COPYABLE(MediaDecoder);
114

115
        rational<unsigned> getTimeBase() const;
116

117 118 119 120 121 122 123 124
        AVCodec *inputDecoder_ = nullptr;
        AVCodecContext *decoderCtx_ = nullptr;
        AVFormatContext *inputCtx_ = nullptr;
        AVStream *avStream_ = nullptr;
        int streamIndex_ = -1;
        bool emulateRate_ = false;
        int64_t startTime_;
        int64_t lastTimestamp_{0};
125

126
        DeviceParams inputParams_;
127

128 129 130
        int correctPixFmt(int input_pix_fmt);
        int setupStream(AVMediaType mediaType);
        int selectStream(AVMediaType type);
131

132
        bool fallback_ = false;
133

134
#ifdef RING_ACCEL
135 136 137
        bool enableAccel_ = true;
        std::unique_ptr<video::HardwareAccel> accel_;
        unsigned short accelFailures_ = 0;
Philippe Gorley's avatar
Philippe Gorley committed
138 139
#endif

140 141
    protected:
        AVDictionary *options_ = nullptr;
142
};
Guillaume Roguez's avatar
Guillaume Roguez committed
143

Adrien Béraud's avatar
Adrien Béraud committed
144
} // namespace jami