1 #ifndef _DECKLINK_OUTPUT_H
2 #define _DECKLINK_OUTPUT_H 1
5 #include <movit/image_format.h>
9 #include <condition_variable>
17 #include "DeckLinkAPI.h"
18 #include "DeckLinkAPITypes.h"
21 #include "shared/context.h"
22 #include "print_latency.h"
23 #include "quittable_sleeper.h"
24 #include "ref_counted_frame.h"
25 #include "shared/ref_counted_gl_sync.h"
33 class ChromaSubsampler;
35 class IDeckLinkOutput;
38 class DeckLinkOutput : public IDeckLinkVideoOutputCallback {
40 DeckLinkOutput(movit::ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index);
42 bool set_device(IDeckLink *output);
43 void start_output(uint32_t mode, int64_t base_pts); // Mode comes from get_available_video_modes().
46 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);
47 void send_audio(int64_t pts, const std::vector<float> &samples);
49 // NOTE: The returned timestamp is undefined for preroll.
50 // Otherwise, it is the timestamp of the output frame as it should have been,
51 // even if we're overshooting. E.g. at 50 fps (0.02 spf), assuming the
52 // last frame was at t=0.980:
54 // If we're at t=0.999, we wait until t=1.000 and return that.
55 // If we're at t=1.001, we return t=1.000 immediately (small overshoot).
56 // If we're at t=1.055, we drop two frames and return t=1.040 immediately.
57 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);
59 // Analogous to CaptureInterface. Will only return modes that have the right width/height.
60 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const { return video_modes; }
62 // If the given mode is supported, return it. If not, pick some “best” valid mode.
63 uint32_t pick_video_mode(uint32_t mode) const;
65 // Desired Y'CbCr coefficients for the current mode. Undefined before start_output().
66 movit::YCbCrLumaCoefficients preferred_ycbcr_coefficients() const;
69 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
70 ULONG STDMETHODCALLTYPE AddRef() override;
71 ULONG STDMETHODCALLTYPE Release() override;
73 // IDeckLinkVideoOutputCallback.
74 HRESULT ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result) override;
75 HRESULT ScheduledPlaybackHasStopped() override;
78 struct Frame : public IDeckLinkVideoFrame {
83 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
84 ULONG STDMETHODCALLTYPE AddRef() override;
85 ULONG STDMETHODCALLTYPE Release() override;
87 // IDeckLinkVideoFrame.
88 long GetWidth() override;
89 long GetHeight() override;
90 long GetRowBytes() override;
91 BMDPixelFormat GetPixelFormat() override;
92 BMDFrameFlags GetFlags() override;
93 HRESULT GetBytes(/* out */ void **buffer) override;
95 HRESULT GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode) override;
96 HRESULT GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary) override;
99 std::atomic<int> refcount{1};
100 RefCountedGLsync fence; // Needs to be waited on before uyvy_ptr can be read from.
101 std::vector<RefCountedFrame> input_frames; // Cannot be released before we are done rendering (ie., <fence> is asserted).
102 ReceivedTimestamps received_ts;
103 int64_t pts, duration;
104 movit::ResourcePool *resource_pool;
106 // These members are persistently allocated, and reused when the frame object is.
107 GLuint uyvy_tex; // Owned by <resource_pool>. Can also hold v210 data.
109 uint8_t *uyvy_ptr; // Persistent mapping into the PBO.
111 // Current Blackmagic drivers (January 2017) have a bug where sending a PBO
112 // pointer to the driver causes a kernel oops. Thus, we do an extra copy into
113 // this pointer before giving the data to the driver. (We don't do a get
114 // directly into this pointer, because e.g. Intel/Mesa hits a slow path when
115 // you do readback into something that's not a PBO.) When Blackmagic fixes
116 // the bug, we should drop this.
117 std::unique_ptr<uint8_t[]> uyvy_ptr_local;
119 friend class DeckLinkOutput;
121 std::unique_ptr<Frame> get_frame();
122 void create_uyvy(GLuint y_tex, GLuint cbcr_tex, GLuint dst_tex);
124 void present_thread_func();
126 std::atomic<int> refcount{1};
128 std::unique_ptr<ChromaSubsampler> chroma_subsampler;
129 std::map<uint32_t, bmusb::VideoMode> video_modes;
131 std::thread present_thread;
132 QuittableSleeper should_quit;
134 std::mutex frame_queue_mutex;
135 std::queue<std::unique_ptr<Frame>> pending_video_frames; // Under <frame_queue_mutex>.
136 std::queue<std::unique_ptr<Frame>> frame_freelist; // Under <frame_queue_mutex>.
137 std::deque<Frame *> scheduled_frames; // Owned by the driver, so no unique_ptr. Under <frame_queue_mutex>.
139 std::condition_variable frame_queues_changed;
140 bool playback_initiated = false, playback_started = false;
141 int64_t base_pts, frame_duration;
142 BMDDisplayModeFlags current_mode_flags = 0;
143 bool last_frame_had_mode_mismatch = false;
145 movit::ResourcePool *resource_pool;
146 IDeckLinkInput *input = nullptr;
147 IDeckLinkOutput *output = nullptr;
148 BMDVideoConnection video_connection;
150 unsigned width, height;
153 GLuint uyvy_vbo; // Holds position and texcoord data.
154 GLuint uyvy_program_num; // Owned by <resource_pool>.
155 GLuint uyvy_position_attribute_index, uyvy_texcoord_attribute_index;
158 #endif // !defined(_DECKLINK_OUTPUT_H)