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