1 #ifndef _DECKLINK_OUTPUT_H
2 #define _DECKLINK_OUTPUT_H 1
8 #include <condition_variable>
15 #include "DeckLinkAPI.h"
16 #include "DeckLinkAPITypes.h"
20 #include "print_latency.h"
21 #include "ref_counted_frame.h"
22 #include "ref_counted_gl_sync.h"
30 class ChromaSubsampler;
32 class IDeckLinkOutput;
35 class DeckLinkOutput : public IDeckLinkVideoOutputCallback {
37 DeckLinkOutput(movit::ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index);
39 void set_device(IDeckLink *output);
40 void start_output(uint32_t mode, int64_t base_pts); // Mode comes from get_available_video_modes().
43 void send_frame(GLuint y_tex, GLuint cbcr_tex, const std::vector<RefCountedFrame> &input_frames, int64_t pts, int64_t duration);
44 void send_audio(int64_t pts, const std::vector<float> &samples);
46 // NOTE: The returned timestamp is undefined for preroll.
47 // Otherwise, it is the timestamp of the output frame as it should have been,
48 // even if we're overshooting. E.g. at 50 fps (0.02 spf), assuming the
49 // last frame was at t=0.980:
51 // If we're at t=0.999, we wait until t=1.000 and return that.
52 // If we're at t=1.001, we return t=1.000 immediately (small overshoot).
53 // If we're at t=1.055, we drop two frames and return t=1.040 immediately.
54 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);
56 // Analogous to CaptureInterface. Will only return modes that have the right width/height.
57 std::map<uint32_t, bmusb::VideoMode> get_available_video_modes() const { return video_modes; }
59 // If the given mode is supported, return it. If not, pick some “best” valid mode.
60 uint32_t pick_video_mode(uint32_t mode) const;
63 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
64 ULONG STDMETHODCALLTYPE AddRef() override;
65 ULONG STDMETHODCALLTYPE Release() override;
67 // IDeckLinkVideoOutputCallback.
68 HRESULT ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result) override;
69 HRESULT ScheduledPlaybackHasStopped() override;
72 struct Frame : public IDeckLinkVideoFrame {
77 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) override;
78 ULONG STDMETHODCALLTYPE AddRef() override;
79 ULONG STDMETHODCALLTYPE Release() override;
81 // IDeckLinkVideoFrame.
82 long GetWidth() override;
83 long GetHeight() override;
84 long GetRowBytes() override;
85 BMDPixelFormat GetPixelFormat() override;
86 BMDFrameFlags GetFlags() override;
87 HRESULT GetBytes(/* out */ void **buffer) override;
89 HRESULT GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode) override;
90 HRESULT GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary) override;
93 std::atomic<int> refcount{1};
94 RefCountedGLsync fence; // Needs to be waited on before uyvy_ptr can be read from.
95 std::vector<RefCountedFrame> input_frames; // Cannot be released before we are done rendering (ie., <fence> is asserted).
96 ReceivedTimestamps received_ts;
97 int64_t pts, duration;
98 movit::ResourcePool *resource_pool;
100 // These members are persistently allocated, and reused when the frame object is.
101 GLuint uyvy_tex; // Owned by <resource_pool>.
103 uint8_t *uyvy_ptr; // Persistent mapping into the PBO.
105 // Current Blackmagic drivers (January 2017) have a bug where sending a PBO
106 // pointer to the driver causes a kernel oops. Thus, we do an extra copy into
107 // this pointer before giving the data to the driver. (We don't do a get
108 // directly into this pointer, because e.g. Intel/Mesa hits a slow path when
109 // you do readback into something that's not a PBO.) When Blackmagic fixes
110 // the bug, we should drop this.
111 std::unique_ptr<uint8_t[]> uyvy_ptr_local;
113 friend class DeckLinkOutput;
115 std::unique_ptr<Frame> get_frame();
116 void create_uyvy(GLuint y_tex, GLuint cbcr_tex, GLuint dst_tex);
118 void present_thread_func();
120 std::atomic<int> refcount{1};
122 std::unique_ptr<ChromaSubsampler> chroma_subsampler;
123 std::map<uint32_t, bmusb::VideoMode> video_modes;
125 std::thread present_thread;
126 std::atomic<bool> should_quit{false};
128 std::mutex frame_queue_mutex;
129 std::queue<std::unique_ptr<Frame>> pending_video_frames; // Under <frame_queue_mutex>.
130 std::queue<std::unique_ptr<Frame>> frame_freelist; // Under <frame_queue_mutex>.
131 int num_frames_in_flight = 0; // Number of frames allocated but not on the freelist. Under <frame_queue_mutex>.
132 std::condition_variable frame_queues_changed;
133 bool playback_initiated = false, playback_started = false;
134 int64_t base_pts, frame_duration;
136 movit::ResourcePool *resource_pool;
137 IDeckLinkOutput *output = nullptr;
138 BMDVideoConnection video_connection;
140 unsigned width, height;
143 GLuint uyvy_vbo; // Holds position and texcoord data.
144 GLuint uyvy_program_num; // Owned by <resource_pool>.
145 GLuint uyvy_position_attribute_index, uyvy_texcoord_attribute_index;
148 #endif // !defined(_DECKLINK_OUTPUT_H)