]> git.sesse.net Git - nageru/blob - decklink_output.cpp
Make for clean shutdown of the output cards.
[nageru] / decklink_output.cpp
1 #include <movit/effect_util.h>
2 #include <movit/util.h>
3 #include <movit/resource_pool.h>  // Must be above the Xlib includes.
4
5 #include <epoxy/egl.h>
6
7 #include "chroma_subsampler.h"
8 #include "decklink_output.h"
9 #include "decklink_util.h"
10 #include "flags.h"
11 #include "print_latency.h"
12 #include "resource_pool.h"
13 #include "timebase.h"
14
15 using namespace movit;
16 using namespace std;
17 using namespace std::chrono;
18
19 DeckLinkOutput::DeckLinkOutput(ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index)
20         : resource_pool(resource_pool), surface(surface), width(width), height(height), card_index(card_index)
21 {
22         chroma_subsampler.reset(new ChromaSubsampler(resource_pool));
23 }
24
25 void DeckLinkOutput::set_device(IDeckLink *decklink)
26 {
27         if (decklink->QueryInterface(IID_IDeckLinkOutput, (void**)&output) != S_OK) {
28                 fprintf(stderr, "Card %u has no outputs\n", card_index);
29                 exit(1);
30         }
31
32         IDeckLinkDisplayModeIterator *mode_it;
33         if (output->GetDisplayModeIterator(&mode_it) != S_OK) {
34                 fprintf(stderr, "Failed to enumerate output display modes for card %u\n", card_index);
35                 exit(1);
36         }
37
38         video_modes.clear();
39
40         for (const auto &it : summarize_video_modes(mode_it, card_index)) {
41                 if (it.second.width != width || it.second.height != height) {
42                         continue;
43                 }
44
45                 // We could support interlaced modes, but let's stay out of it for now,
46                 // since we don't have interlaced stream output.
47                 if (it.second.interlaced) {
48                         continue;
49                 }
50
51                 video_modes.insert(it);
52         }
53
54         mode_it->Release();
55
56         // HDMI or SDI generally mean “both HDMI and SDI at the same time” on DeckLink cards
57         // that support both; pick_default_video_connection() will generally pick one of those
58         // if they exist. We're not very likely to need analog outputs, so we don't need a way
59         // to change beyond that.
60         video_connection = pick_default_video_connection(decklink, BMDDeckLinkVideoOutputConnections, card_index);
61 }
62
63 void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts)
64 {
65         assert(output);
66         assert(!playback_initiated);
67
68         should_quit = false;
69         playback_initiated = true;
70         playback_started = false;
71         this->base_pts = base_pts;
72
73         IDeckLinkConfiguration *config = nullptr;
74         if (output->QueryInterface(IID_IDeckLinkConfiguration, (void**)&config) != S_OK) {
75                 fprintf(stderr, "Failed to get configuration interface for output card\n");
76                 exit(1);
77         }
78         if (config->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true) != S_OK) {
79                 fprintf(stderr, "Failed to set low latency output\n");
80                 exit(1);
81         }
82         if (config->SetInt(bmdDeckLinkConfigVideoOutputConnection, video_connection) != S_OK) {
83                 fprintf(stderr, "Failed to set video output connection for card %u\n", card_index);
84                 exit(1);
85         }
86         if (config->SetFlag(bmdDeckLinkConfigUse1080pNotPsF, true) != S_OK) {
87                 fprintf(stderr, "Failed to set PsF flag for card\n");
88                 exit(1);
89         }
90
91         BMDDisplayModeSupport support;
92         IDeckLinkDisplayMode *display_mode;
93         if (output->DoesSupportVideoMode(mode, bmdFormat8BitYUV, bmdVideoOutputFlagDefault,
94                                          &support, &display_mode) != S_OK) {
95                 fprintf(stderr, "Couldn't ask for format support\n");
96                 exit(1);
97         }
98
99         if (support == bmdDisplayModeNotSupported) {
100                 fprintf(stderr, "Requested display mode not supported\n");
101                 exit(1);
102         }
103
104         BMDDisplayModeFlags flags = display_mode->GetFlags();
105         if ((flags & bmdDisplayModeColorspaceRec601) && global_flags.ycbcr_rec709_coefficients) {
106                 fprintf(stderr, "WARNING: Chosen output mode expects Rec. 601 Y'CbCr coefficients.\n");
107                 fprintf(stderr, "         Consider --output-ycbcr-coefficients=rec601 (or =auto).\n");
108         } else if ((flags & bmdDisplayModeColorspaceRec709) && !global_flags.ycbcr_rec709_coefficients) {
109                 fprintf(stderr, "WARNING: Chosen output mode expects Rec. 709 Y'CbCr coefficients.\n");
110                 fprintf(stderr, "         Consider --output-ycbcr-coefficients=rec709 (or =auto).\n");
111         }
112
113         BMDTimeValue time_value;
114         BMDTimeScale time_scale;
115         if (display_mode->GetFrameRate(&time_value, &time_scale) != S_OK) {
116                 fprintf(stderr, "Couldn't get frame rate\n");
117                 exit(1);
118         }
119
120         frame_duration = time_value * TIMEBASE / time_scale;
121
122         display_mode->Release();
123
124         HRESULT result = output->EnableVideoOutput(mode, bmdVideoOutputFlagDefault);
125         if (result != S_OK) {
126                 fprintf(stderr, "Couldn't enable output with error 0x%x\n", result);
127                 exit(1);
128         }
129         if (output->SetScheduledFrameCompletionCallback(this) != S_OK) {
130                 fprintf(stderr, "Couldn't set callback\n");
131                 exit(1);
132         }
133         assert(OUTPUT_FREQUENCY == 48000);
134         if (output->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2, bmdAudioOutputStreamTimestamped) != S_OK) {
135                 fprintf(stderr, "Couldn't enable audio output\n");
136                 exit(1);
137         }
138         if (output->BeginAudioPreroll() != S_OK) {
139                 fprintf(stderr, "Couldn't begin audio preroll\n");
140                 exit(1);
141         }
142
143         present_thread = thread([this]{
144                 QOpenGLContext *context = create_context(this->surface);
145                 eglBindAPI(EGL_OPENGL_API);
146                 if (!make_current(context, this->surface)) {
147                         printf("display=%p surface=%p context=%p curr=%p err=%d\n", eglGetCurrentDisplay(), this->surface, context, eglGetCurrentContext(),
148                                 eglGetError());
149                         exit(1);
150                 }
151                 present_thread_func();
152                 delete_context(context);
153         });
154 }
155
156 void DeckLinkOutput::end_output()
157 {
158         if (!playback_initiated) {
159                 return;
160         }
161
162         should_quit = true;
163         frame_queues_changed.notify_all();
164         present_thread.join();
165         playback_initiated = false;
166
167         output->StopScheduledPlayback(0, nullptr, 0);
168         output->DisableVideoOutput();
169         output->DisableAudioOutput();
170
171         // Wait until all frames are accounted for, and free them.
172         {
173                 unique_lock<mutex> lock(frame_queue_mutex);
174                 while (!(frame_freelist.empty() && num_frames_in_flight == 0)) {
175                         frame_queues_changed.wait(lock, [this]{ return !frame_freelist.empty(); });
176                         frame_freelist.pop();
177                 }
178         }
179 }
180
181 void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, const vector<RefCountedFrame> &input_frames, int64_t pts, int64_t duration)
182 {
183         unique_ptr<Frame> frame = move(get_frame());
184         chroma_subsampler->create_uyvy(y_tex, cbcr_tex, width, height, frame->uyvy_tex);
185
186         // Download the UYVY texture to the PBO.
187         glPixelStorei(GL_PACK_ROW_LENGTH, 0);
188         check_error();
189
190         glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo);
191         check_error();
192
193         glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex);
194         check_error();
195         glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
196         check_error();
197
198         glBindTexture(GL_TEXTURE_2D, 0);
199         check_error();
200         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
201         check_error();
202
203         glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
204         check_error();
205
206         frame->fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
207         check_error();
208         glFlush();  // Make the DeckLink thread see the fence as soon as possible.
209         check_error();
210
211         frame->input_frames = input_frames;
212         frame->received_ts = find_received_timestamp(input_frames);
213         frame->pts = pts;
214         frame->duration = duration;
215
216         {
217                 unique_lock<mutex> lock(frame_queue_mutex);
218                 pending_video_frames.push(move(frame));
219         }
220         frame_queues_changed.notify_all();
221 }
222
223 void DeckLinkOutput::send_audio(int64_t pts, const std::vector<float> &samples)
224 {
225         unique_ptr<int32_t[]> int_samples(new int32_t[samples.size()]);
226         for (size_t i = 0; i < samples.size(); ++i) {
227                 int_samples[i] = lrintf(samples[i] * 2147483648.0f);
228         }
229
230         uint32_t frames_written;
231         HRESULT result = output->ScheduleAudioSamples(int_samples.get(), samples.size() / 2,
232                 pts, TIMEBASE, &frames_written);
233         if (result != S_OK) {
234                 fprintf(stderr, "ScheduleAudioSamples(pts=%ld) failed (result=0x%08x)\n", pts, result);
235         } else {
236                 if (frames_written != samples.size() / 2) {
237                         fprintf(stderr, "ScheduleAudioSamples() returned short write (%u/%ld)\n", frames_written, samples.size() / 2);
238                 }
239         }
240 }
241
242 void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration)
243 {
244         *dropped_frames = 0;
245         *frame_duration = this->frame_duration;
246
247         const BMDTimeValue buffer = lrint(*frame_duration * global_flags.output_buffer_frames);
248         const BMDTimeValue max_overshoot = lrint(*frame_duration * global_flags.output_slop_frames);
249         BMDTimeValue target_time = pts - buffer;
250
251         // While prerolling, we send out frames as quickly as we can.
252         if (target_time < base_pts) {
253                 return;
254         }
255
256         if (!playback_started) {
257                 if (output->EndAudioPreroll() != S_OK) {
258                         fprintf(stderr, "Could not end audio preroll\n");
259                         exit(1);  // TODO
260                 }
261                 if (output->StartScheduledPlayback(base_pts, TIMEBASE, 1.0) != S_OK) {
262                         fprintf(stderr, "Could not start playback\n");
263                         exit(1);  // TODO
264                 }
265                 playback_started = true;
266         }
267
268         BMDTimeValue stream_frame_time;
269         double playback_speed;
270         output->GetScheduledStreamTime(TIMEBASE, &stream_frame_time, &playback_speed);
271
272         // If we're ahead of time, wait for the frame to (approximately) start.
273         if (stream_frame_time < target_time) {
274                 steady_clock::time_point t = steady_clock::now() +
275                         nanoseconds((target_time - stream_frame_time) * 1000000000 / TIMEBASE);
276                 this_thread::sleep_until(t);
277                 return;
278         }
279
280         // If we overshot the previous frame by just a little,
281         // fire off one immediately.
282         if (stream_frame_time < target_time + max_overshoot) {
283                 fprintf(stderr, "Warning: Frame was %ld ms late (but not skipping it due to --output-slop-frames).\n",
284                         lrint((stream_frame_time - target_time) * 1000.0 / TIMEBASE));
285                 return;
286         }
287
288         // Oops, we missed by more than one frame. Return immediately,
289         // but drop so that we catch up.
290         *dropped_frames = (stream_frame_time - target_time + *frame_duration - 1) / *frame_duration;
291         fprintf(stderr, "Dropped %d output frames; skipping.\n", *dropped_frames);
292 }
293
294 HRESULT DeckLinkOutput::ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result)
295 {
296         Frame *frame = static_cast<Frame *>(completedFrame);
297         switch (result) {
298         case bmdOutputFrameCompleted:
299                 break;
300         case bmdOutputFrameDisplayedLate:
301                 fprintf(stderr, "Output frame displayed late (pts=%ld)\n", frame->pts);
302                 fprintf(stderr, "Consider increasing --output-buffer-frames if this persists.\n");
303                 break;
304         case bmdOutputFrameDropped:
305                 fprintf(stderr, "Output frame was dropped (pts=%ld)\n", frame->pts);
306                 fprintf(stderr, "Consider increasing --output-buffer-frames if this persists.\n");
307                 break;
308         case bmdOutputFrameFlushed:
309                 fprintf(stderr, "Output frame was flushed (pts=%ld)\n", frame->pts);
310                 break;
311         default:
312                 fprintf(stderr, "Output frame completed with unknown status %d\n", result);
313                 break;
314         }
315
316         static int hei = 0;
317         print_latency("DeckLink output latency (frame received → output on HDMI):", frame->received_ts, false, &hei);
318
319         {
320                 lock_guard<mutex> lock(frame_queue_mutex);
321                 frame_freelist.push(unique_ptr<Frame>(frame));
322                 --num_frames_in_flight;
323         }
324
325         return S_OK;
326 }
327
328 HRESULT DeckLinkOutput::ScheduledPlaybackHasStopped()
329 {
330         printf("playback stopped!\n");
331         return S_OK;
332 }
333
334 unique_ptr<DeckLinkOutput::Frame> DeckLinkOutput::get_frame()
335 {
336         lock_guard<mutex> lock(frame_queue_mutex);
337
338         if (!frame_freelist.empty()) {
339                 unique_ptr<Frame> frame = move(frame_freelist.front());
340                 frame_freelist.pop();
341                 return frame;
342         }
343
344         unique_ptr<Frame> frame(new Frame);
345
346         frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGBA8, width / 2, height);
347
348         glGenBuffers(1, &frame->pbo);
349         check_error();
350         glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo);
351         check_error();
352         glBufferStorage(GL_PIXEL_PACK_BUFFER, width * height * 2, NULL, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
353         check_error();
354         frame->uyvy_ptr = (uint8_t *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, width * height * 2, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
355         check_error();
356         frame->uyvy_ptr_local.reset(new uint8_t[width * height * 2]);
357         frame->resource_pool = resource_pool;
358
359         return frame;
360 }
361
362 void DeckLinkOutput::present_thread_func()
363 {
364         for ( ;; ) {
365                 unique_ptr<Frame> frame;
366                 {
367                         unique_lock<mutex> lock(frame_queue_mutex);
368                         frame_queues_changed.wait(lock, [this]{
369                                 return should_quit || !pending_video_frames.empty();
370                         });
371                         if (should_quit) {
372                                 return;
373                         }
374                         frame = move(pending_video_frames.front());
375                         pending_video_frames.pop();
376                         ++num_frames_in_flight;
377                 }
378
379                 glWaitSync(frame->fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
380                 check_error();
381                 frame->fence.reset();
382
383                 memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, width * height * 2);
384
385                 // Release any input frames we needed to render this frame.
386                 frame->input_frames.clear();
387
388                 BMDTimeValue pts = frame->pts;
389                 BMDTimeValue duration = frame->duration;
390                 HRESULT res = output->ScheduleVideoFrame(frame.get(), pts, duration, TIMEBASE);
391                 if (res == S_OK) {
392                         frame.release();  // Owned by the driver now.
393                 } else {
394                         fprintf(stderr, "Could not schedule video frame! (error=0x%08x)\n", res);
395
396                         lock_guard<mutex> lock(frame_queue_mutex);
397                         frame_freelist.push(move(frame));
398                         --num_frames_in_flight;
399                 }
400         }
401 }
402
403 HRESULT STDMETHODCALLTYPE DeckLinkOutput::QueryInterface(REFIID, LPVOID *)
404 {
405         return E_NOINTERFACE;
406 }
407
408 ULONG STDMETHODCALLTYPE DeckLinkOutput::AddRef()
409 {
410         return refcount.fetch_add(1) + 1;
411 }
412
413 ULONG STDMETHODCALLTYPE DeckLinkOutput::Release()
414 {
415         int new_ref = refcount.fetch_sub(1) - 1;
416         if (new_ref == 0)
417                 delete this;
418         return new_ref;
419 }
420
421 DeckLinkOutput::Frame::~Frame()
422 {
423         glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
424         check_error();
425         glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
426         check_error();
427         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
428         check_error();
429         glDeleteBuffers(1, &pbo);
430         check_error();
431         resource_pool->release_2d_texture(uyvy_tex);
432         check_error();
433 }
434
435 HRESULT STDMETHODCALLTYPE DeckLinkOutput::Frame::QueryInterface(REFIID, LPVOID *)
436 {
437         return E_NOINTERFACE;
438 }
439
440 ULONG STDMETHODCALLTYPE DeckLinkOutput::Frame::AddRef()
441 {
442         return refcount.fetch_add(1) + 1;
443 }
444
445 ULONG STDMETHODCALLTYPE DeckLinkOutput::Frame::Release()
446 {
447         int new_ref = refcount.fetch_sub(1) - 1;
448         if (new_ref == 0)
449                 delete this;
450         return new_ref;
451 }
452
453 long DeckLinkOutput::Frame::GetWidth()
454 {
455         return global_flags.width;
456 }
457
458 long DeckLinkOutput::Frame::GetHeight()
459 {
460         return global_flags.height;
461 }
462
463 long DeckLinkOutput::Frame::GetRowBytes()
464 {
465         return global_flags.width * 2;
466 }
467
468 BMDPixelFormat DeckLinkOutput::Frame::GetPixelFormat()
469 {
470         return bmdFormat8BitYUV;
471 }
472
473 BMDFrameFlags DeckLinkOutput::Frame::GetFlags()
474 {
475         return bmdFrameFlagDefault;
476 }
477
478 HRESULT DeckLinkOutput::Frame::GetBytes(/* out */ void **buffer)
479 {
480         *buffer = uyvy_ptr_local.get();
481         return S_OK;
482 }
483
484 HRESULT DeckLinkOutput::Frame::GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode)
485 {
486         fprintf(stderr, "STUB: GetTimecode()\n");
487         return E_NOTIMPL;
488 }
489
490 HRESULT DeckLinkOutput::Frame::GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary)
491 {
492         fprintf(stderr, "STUB: GetAncillaryData()\n");
493         return E_NOTIMPL;
494 }