]> git.sesse.net Git - nageru/blob - decklink_output.h
Release Nageru 1.7.2.
[nageru] / decklink_output.h
1 #ifndef _DECKLINK_OUTPUT_H
2 #define _DECKLINK_OUTPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <movit/image_format.h>
6 #include <stdint.h>
7 #include <atomic>
8 #include <chrono>
9 #include <condition_variable>
10 #include <memory>
11 #include <mutex>
12 #include <queue>
13 #include <thread>
14 #include <vector>
15
16 #include "DeckLinkAPI.h"
17 #include "DeckLinkAPITypes.h"
18 #include "LinuxCOM.h"
19
20 #include "context.h"
21 #include "print_latency.h"
22 #include "quittable_sleeper.h"
23 #include "ref_counted_frame.h"
24 #include "ref_counted_gl_sync.h"
25
26 namespace movit {
27
28 class ResourcePool;
29
30 }  // namespace movit
31
32 class ChromaSubsampler;
33 class IDeckLink;
34 class IDeckLinkOutput;
35 class QSurface;
36
37 class DeckLinkOutput : public IDeckLinkVideoOutputCallback {
38 public:
39         DeckLinkOutput(movit::ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index);
40
41         bool set_device(IDeckLink *output);
42         void start_output(uint32_t mode, int64_t base_pts);  // Mode comes from get_available_video_modes().
43         void end_output();
44
45         void send_frame(GLuint y_tex, GLuint cbcr_tex, movit::YCbCrLumaCoefficients ycbcr_coefficients, const std::vector<RefCountedFrame> &input_frames, int64_t pts, int64_t duration);
46         void send_audio(int64_t pts, const std::vector<float> &samples);
47
48         // NOTE: The returned timestamp is undefined for preroll.
49         // Otherwise, it is the timestamp of the output frame as it should have been,
50         // even if we're overshooting. E.g. at 50 fps (0.02 spf), assuming the
51         // last frame was at t=0.980:
52         //
53         //   If we're at t=0.999, we wait until t=1.000 and return that.
54         //   If we're at t=1.001, we return t=1.000 immediately (small overshoot).
55         //   If we're at t=1.055, we drop two frames and return t=1.040 immediately.
56         void wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll, std::chrono::steady_clock::time_point *frame_timestamp);
57
58         // Analogous to CaptureInterface. Will only return modes that have the right width/height.
59         std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const { return video_modes; }
60
61         // If the given mode is supported, return it. If not, pick some “best” valid mode.
62         uint32_t pick_video_mode(uint32_t mode) const;
63
64         // Desired Y'CbCr coefficients for the current mode. Undefined before start_output().
65         movit::YCbCrLumaCoefficients preferred_ycbcr_coefficients() const;
66
67         // IUnknown.
68         HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
69         ULONG STDMETHODCALLTYPE AddRef() override;
70         ULONG STDMETHODCALLTYPE Release() override;
71
72         // IDeckLinkVideoOutputCallback.
73         HRESULT ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result) override;
74         HRESULT ScheduledPlaybackHasStopped() override;
75
76 private:
77         struct Frame : public IDeckLinkVideoFrame {
78         public:
79                 ~Frame();
80
81                 // IUnknown.
82                 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
83                 ULONG STDMETHODCALLTYPE AddRef() override;
84                 ULONG STDMETHODCALLTYPE Release() override;
85
86                 // IDeckLinkVideoFrame.
87                 long GetWidth() override;
88                 long GetHeight() override;
89                 long GetRowBytes() override;
90                 BMDPixelFormat GetPixelFormat() override;
91                 BMDFrameFlags GetFlags() override;
92                 HRESULT GetBytes(/* out */ void **buffer) override;
93
94                 HRESULT GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode) override;
95                 HRESULT GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary) override;
96
97         private:
98                 std::atomic<int> refcount{1};
99                 RefCountedGLsync fence;  // Needs to be waited on before uyvy_ptr can be read from.
100                 std::vector<RefCountedFrame> input_frames;  // Cannot be released before we are done rendering (ie., <fence> is asserted).
101                 ReceivedTimestamps received_ts;
102                 int64_t pts, duration;
103                 movit::ResourcePool *resource_pool;
104
105                 // These members are persistently allocated, and reused when the frame object is.
106                 GLuint uyvy_tex;  // Owned by <resource_pool>. Can also hold v210 data.
107                 GLuint pbo;
108                 uint8_t *uyvy_ptr;  // Persistent mapping into the PBO.
109
110                 // Current Blackmagic drivers (January 2017) have a bug where sending a PBO
111                 // pointer to the driver causes a kernel oops. Thus, we do an extra copy into
112                 // this pointer before giving the data to the driver. (We don't do a get
113                 // directly into this pointer, because e.g. Intel/Mesa hits a slow path when
114                 // you do readback into something that's not a PBO.) When Blackmagic fixes
115                 // the bug, we should drop this.
116                 std::unique_ptr<uint8_t[]> uyvy_ptr_local;
117
118                 friend class DeckLinkOutput;
119         };
120         std::unique_ptr<Frame> get_frame();
121         void create_uyvy(GLuint y_tex, GLuint cbcr_tex, GLuint dst_tex);
122
123         void present_thread_func();
124
125         std::atomic<int> refcount{1};
126
127         std::unique_ptr<ChromaSubsampler> chroma_subsampler;
128         std::map<uint32_t, bmusb::VideoMode> video_modes;
129
130         std::thread present_thread;
131         QuittableSleeper should_quit;
132
133         std::mutex frame_queue_mutex;
134         std::queue<std::unique_ptr<Frame>> pending_video_frames;  // Under <frame_queue_mutex>.
135         std::queue<std::unique_ptr<Frame>> frame_freelist;  // Under <frame_queue_mutex>.
136         int num_frames_in_flight = 0;  // Number of frames allocated but not on the freelist. Under <frame_queue_mutex>.
137         std::condition_variable frame_queues_changed;
138         bool playback_initiated = false, playback_started = false;
139         int64_t base_pts, frame_duration;
140         BMDDisplayModeFlags current_mode_flags = 0;
141         bool last_frame_had_mode_mismatch = false;
142
143         movit::ResourcePool *resource_pool;
144         IDeckLinkOutput *output = nullptr;
145         BMDVideoConnection video_connection;
146         QSurface *surface;
147         unsigned width, height;
148         unsigned card_index;
149
150         GLuint uyvy_vbo;  // Holds position and texcoord data.
151         GLuint uyvy_program_num;  // Owned by <resource_pool>.
152         GLuint uyvy_position_attribute_index, uyvy_texcoord_attribute_index;
153 };
154
155 #endif  // !defined(_DECKLINK_OUTPUT_H)