]> git.sesse.net Git - nageru/blob - decklink_output.cpp
Pre-serialize only the labels for metrics; the ordering constraints did not really...
[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 #include <pthread.h>
5
6 #include <epoxy/egl.h>
7
8 #include "chroma_subsampler.h"
9 #include "decklink_output.h"
10 #include "decklink_util.h"
11 #include "flags.h"
12 #include "print_latency.h"
13 #include "resource_pool.h"
14 #include "timebase.h"
15 #include "v210_converter.h"
16
17 using namespace movit;
18 using namespace std;
19 using namespace std::chrono;
20
21 namespace {
22
23 // This class can be deleted during regular use, so make all the metrics static.
24 bool metrics_inited = false;
25 LatencyHistogram latency_histogram;
26 atomic<int64_t> metric_decklink_output_width_pixels{-1};
27 atomic<int64_t> metric_decklink_output_height_pixels{-1};
28 atomic<int64_t> metric_decklink_output_frame_rate_den{-1};
29 atomic<int64_t> metric_decklink_output_frame_rate_nom{-1};
30 atomic<int64_t> metric_decklink_output_inflight_frames{0};
31 atomic<int64_t> metric_decklink_output_color_mismatch_frames{0};
32
33 atomic<int64_t> metric_decklink_output_scheduled_frames_dropped{0};
34 atomic<int64_t> metric_decklink_output_scheduled_frames_late{0};
35 atomic<int64_t> metric_decklink_output_scheduled_frames_normal{0};
36 atomic<int64_t> metric_decklink_output_scheduled_frames_preroll{0};
37
38 atomic<int64_t> metric_decklink_output_completed_frames_completed{0};
39 atomic<int64_t> metric_decklink_output_completed_frames_dropped{0};
40 atomic<int64_t> metric_decklink_output_completed_frames_flushed{0};
41 atomic<int64_t> metric_decklink_output_completed_frames_late{0};
42 atomic<int64_t> metric_decklink_output_completed_frames_unknown{0};
43
44 atomic<int64_t> metric_decklink_output_scheduled_samples{0};
45
46 }  // namespace
47
48 DeckLinkOutput::DeckLinkOutput(ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index)
49         : resource_pool(resource_pool), surface(surface), width(width), height(height), card_index(card_index)
50 {
51         chroma_subsampler.reset(new ChromaSubsampler(resource_pool));
52
53         if (!metrics_inited) {
54                 latency_histogram.init("decklink_output");
55                 global_metrics.add("decklink_output_width_pixels", &metric_decklink_output_width_pixels, Metrics::TYPE_GAUGE);
56                 global_metrics.add("decklink_output_height_pixels", &metric_decklink_output_height_pixels, Metrics::TYPE_GAUGE);
57                 global_metrics.add("decklink_output_frame_rate_den", &metric_decklink_output_frame_rate_den, Metrics::TYPE_GAUGE);
58                 global_metrics.add("decklink_output_frame_rate_nom", &metric_decklink_output_frame_rate_nom, Metrics::TYPE_GAUGE);
59                 global_metrics.add("decklink_output_inflight_frames", &metric_decklink_output_inflight_frames, Metrics::TYPE_GAUGE);
60                 global_metrics.add("decklink_output_color_mismatch_frames", &metric_decklink_output_color_mismatch_frames);
61
62                 global_metrics.add("decklink_output_scheduled_frames", {{ "status", "dropped" }}, &metric_decklink_output_scheduled_frames_dropped);
63                 global_metrics.add("decklink_output_scheduled_frames", {{ "status", "late" }}, &metric_decklink_output_scheduled_frames_late);
64                 global_metrics.add("decklink_output_scheduled_frames", {{ "status", "normal" }}, &metric_decklink_output_scheduled_frames_normal);
65                 global_metrics.add("decklink_output_scheduled_frames", {{ "status", "preroll" }}, &metric_decklink_output_scheduled_frames_preroll);
66
67                 global_metrics.add("decklink_output_completed_frames", {{ "status", "completed" }}, &metric_decklink_output_completed_frames_completed);
68                 global_metrics.add("decklink_output_completed_frames", {{ "status", "dropped" }}, &metric_decklink_output_completed_frames_dropped);
69                 global_metrics.add("decklink_output_completed_frames", {{ "status", "flushed" }}, &metric_decklink_output_completed_frames_flushed);
70                 global_metrics.add("decklink_output_completed_frames", {{ "status", "late" }}, &metric_decklink_output_completed_frames_late);
71                 global_metrics.add("decklink_output_completed_frames", {{ "status", "unknown" }}, &metric_decklink_output_completed_frames_unknown);
72
73                 global_metrics.add("decklink_output_scheduled_samples", &metric_decklink_output_scheduled_samples);
74
75                 metrics_inited = true;
76         }
77 }
78
79 void DeckLinkOutput::set_device(IDeckLink *decklink)
80 {
81         if (decklink->QueryInterface(IID_IDeckLinkOutput, (void**)&output) != S_OK) {
82                 fprintf(stderr, "Card %u has no outputs\n", card_index);
83                 exit(1);
84         }
85
86         IDeckLinkDisplayModeIterator *mode_it;
87         if (output->GetDisplayModeIterator(&mode_it) != S_OK) {
88                 fprintf(stderr, "Failed to enumerate output display modes for card %u\n", card_index);
89                 exit(1);
90         }
91
92         video_modes.clear();
93
94         for (const auto &it : summarize_video_modes(mode_it, card_index)) {
95                 if (it.second.width != width || it.second.height != height) {
96                         continue;
97                 }
98
99                 // We could support interlaced modes, but let's stay out of it for now,
100                 // since we don't have interlaced stream output.
101                 if (it.second.interlaced) {
102                         continue;
103                 }
104
105                 video_modes.insert(it);
106         }
107
108         mode_it->Release();
109
110         // HDMI or SDI generally mean “both HDMI and SDI at the same time” on DeckLink cards
111         // that support both; pick_default_video_connection() will generally pick one of those
112         // if they exist. We're not very likely to need analog outputs, so we don't need a way
113         // to change beyond that.
114         video_connection = pick_default_video_connection(decklink, BMDDeckLinkVideoOutputConnections, card_index);
115 }
116
117 void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts)
118 {
119         assert(output);
120         assert(!playback_initiated);
121
122         if (video_modes.empty()) {
123                 fprintf(stderr, "ERROR: No matching output modes for %dx%d found\n", width, height);
124                 exit(1);
125         }
126
127         should_quit.unquit();
128         playback_initiated = true;
129         playback_started = false;
130         this->base_pts = base_pts;
131
132         IDeckLinkConfiguration *config = nullptr;
133         if (output->QueryInterface(IID_IDeckLinkConfiguration, (void**)&config) != S_OK) {
134                 fprintf(stderr, "Failed to get configuration interface for output card\n");
135                 exit(1);
136         }
137         if (config->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true) != S_OK) {
138                 fprintf(stderr, "Failed to set low latency output\n");
139                 exit(1);
140         }
141         if (config->SetInt(bmdDeckLinkConfigVideoOutputConnection, video_connection) != S_OK) {
142                 fprintf(stderr, "Failed to set video output connection for card %u\n", card_index);
143                 exit(1);
144         }
145         if (config->SetFlag(bmdDeckLinkConfigUse1080pNotPsF, true) != S_OK) {
146                 fprintf(stderr, "Failed to set PsF flag for card\n");
147                 exit(1);
148         }
149         if (config->SetFlag(bmdDeckLinkConfigSMPTELevelAOutput, true) != S_OK) {
150                 // This affects at least some no-name SDI->HDMI converters.
151                 // Warn, but don't die.
152                 fprintf(stderr, "WARNING: Failed to enable SMTPE Level A; resolutions like 1080p60 might have issues.\n");
153         }
154
155         BMDDisplayModeSupport support;
156         IDeckLinkDisplayMode *display_mode;
157         BMDPixelFormat pixel_format = global_flags.ten_bit_output ? bmdFormat10BitYUV : bmdFormat8BitYUV;
158         if (output->DoesSupportVideoMode(mode, pixel_format, bmdVideoOutputFlagDefault,
159                                          &support, &display_mode) != S_OK) {
160                 fprintf(stderr, "Couldn't ask for format support\n");
161                 exit(1);
162         }
163
164         if (support == bmdDisplayModeNotSupported) {
165                 fprintf(stderr, "Requested display mode not supported\n");
166                 exit(1);
167         }
168
169         current_mode_flags = display_mode->GetFlags();
170
171         BMDTimeValue time_value;
172         BMDTimeScale time_scale;
173         if (display_mode->GetFrameRate(&time_value, &time_scale) != S_OK) {
174                 fprintf(stderr, "Couldn't get frame rate\n");
175                 exit(1);
176         }
177
178         metric_decklink_output_width_pixels = width;
179         metric_decklink_output_height_pixels = height;
180         metric_decklink_output_frame_rate_nom = time_value;
181         metric_decklink_output_frame_rate_den = time_scale;
182
183         frame_duration = time_value * TIMEBASE / time_scale;
184
185         display_mode->Release();
186
187         HRESULT result = output->EnableVideoOutput(mode, bmdVideoOutputFlagDefault);
188         if (result != S_OK) {
189                 fprintf(stderr, "Couldn't enable output with error 0x%x\n", result);
190                 exit(1);
191         }
192         if (output->SetScheduledFrameCompletionCallback(this) != S_OK) {
193                 fprintf(stderr, "Couldn't set callback\n");
194                 exit(1);
195         }
196         assert(OUTPUT_FREQUENCY == 48000);
197         if (output->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2, bmdAudioOutputStreamTimestamped) != S_OK) {
198                 fprintf(stderr, "Couldn't enable audio output\n");
199                 exit(1);
200         }
201         if (output->BeginAudioPreroll() != S_OK) {
202                 fprintf(stderr, "Couldn't begin audio preroll\n");
203                 exit(1);
204         }
205
206         present_thread = thread([this]{
207                 QOpenGLContext *context = create_context(this->surface);
208                 eglBindAPI(EGL_OPENGL_API);
209                 if (!make_current(context, this->surface)) {
210                         printf("display=%p surface=%p context=%p curr=%p err=%d\n", eglGetCurrentDisplay(), this->surface, context, eglGetCurrentContext(),
211                                 eglGetError());
212                         exit(1);
213                 }
214                 present_thread_func();
215                 delete_context(context);
216         });
217 }
218
219 void DeckLinkOutput::end_output()
220 {
221         if (!playback_initiated) {
222                 return;
223         }
224
225         should_quit.quit();
226         frame_queues_changed.notify_all();
227         present_thread.join();
228         playback_initiated = false;
229
230         output->StopScheduledPlayback(0, nullptr, 0);
231         output->DisableVideoOutput();
232         output->DisableAudioOutput();
233
234         // Wait until all frames are accounted for, and free them.
235         {
236                 unique_lock<mutex> lock(frame_queue_mutex);
237                 while (!(frame_freelist.empty() && num_frames_in_flight == 0)) {
238                         frame_queues_changed.wait(lock, [this]{ return !frame_freelist.empty(); });
239                         frame_freelist.pop();
240                 }
241         }
242 }
243
244 void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, YCbCrLumaCoefficients output_ycbcr_coefficients, const vector<RefCountedFrame> &input_frames, int64_t pts, int64_t duration)
245 {
246         assert(!should_quit.should_quit());
247
248         if ((current_mode_flags & bmdDisplayModeColorspaceRec601) && output_ycbcr_coefficients == YCBCR_REC_709) {
249                 if (!last_frame_had_mode_mismatch) {
250                         fprintf(stderr, "WARNING: Chosen output mode expects Rec. 601 Y'CbCr coefficients.\n");
251                         fprintf(stderr, "         Consider --output-ycbcr-coefficients=rec601 (or =auto).\n");
252                 }
253                 last_frame_had_mode_mismatch = true;
254                 ++metric_decklink_output_color_mismatch_frames;
255         } else if ((current_mode_flags & bmdDisplayModeColorspaceRec709) && output_ycbcr_coefficients == YCBCR_REC_601) {
256                 if (!last_frame_had_mode_mismatch) {
257                         fprintf(stderr, "WARNING: Chosen output mode expects Rec. 709 Y'CbCr coefficients.\n");
258                         fprintf(stderr, "         Consider --output-ycbcr-coefficients=rec709 (or =auto).\n");
259                 }
260                 last_frame_had_mode_mismatch = true;
261                 ++metric_decklink_output_color_mismatch_frames;
262         } else {
263                 last_frame_had_mode_mismatch = false;
264         }
265
266         unique_ptr<Frame> frame = move(get_frame());
267         if (global_flags.ten_bit_output) {
268                 chroma_subsampler->create_v210(y_tex, cbcr_tex, width, height, frame->uyvy_tex);
269         } else {
270                 chroma_subsampler->create_uyvy(y_tex, cbcr_tex, width, height, frame->uyvy_tex);
271         }
272
273         // Download the UYVY texture to the PBO.
274         glPixelStorei(GL_PACK_ROW_LENGTH, 0);
275         check_error();
276
277         glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo);
278         check_error();
279
280         if (global_flags.ten_bit_output) {
281                 glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex);
282                 check_error();
283                 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, BUFFER_OFFSET(0));
284                 check_error();
285         } else {
286                 glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex);
287                 check_error();
288                 glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
289                 check_error();
290         }
291
292         glBindTexture(GL_TEXTURE_2D, 0);
293         check_error();
294         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
295         check_error();
296
297         glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
298         check_error();
299
300         frame->fence = RefCountedGLsync(GL_SYNC_GPU_COMMANDS_COMPLETE, /*flags=*/0);
301         check_error();
302         glFlush();  // Make the DeckLink thread see the fence as soon as possible.
303         check_error();
304
305         frame->input_frames = input_frames;
306         frame->received_ts = find_received_timestamp(input_frames);
307         frame->pts = pts;
308         frame->duration = duration;
309
310         {
311                 unique_lock<mutex> lock(frame_queue_mutex);
312                 pending_video_frames.push(move(frame));
313         }
314         frame_queues_changed.notify_all();
315 }
316
317 void DeckLinkOutput::send_audio(int64_t pts, const std::vector<float> &samples)
318 {
319         unique_ptr<int32_t[]> int_samples(new int32_t[samples.size()]);
320         for (size_t i = 0; i < samples.size(); ++i) {
321                 int_samples[i] = lrintf(samples[i] * 2147483648.0f);
322         }
323
324         uint32_t frames_written;
325         HRESULT result = output->ScheduleAudioSamples(int_samples.get(), samples.size() / 2,
326                 pts, TIMEBASE, &frames_written);
327         if (result != S_OK) {
328                 fprintf(stderr, "ScheduleAudioSamples(pts=%ld) failed (result=0x%08x)\n", pts, result);
329         } else {
330                 if (frames_written != samples.size() / 2) {
331                         fprintf(stderr, "ScheduleAudioSamples() returned short write (%u/%ld)\n", frames_written, samples.size() / 2);
332                 }
333         }
334         metric_decklink_output_scheduled_samples += samples.size() / 2;
335 }
336
337 void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *frame_duration, bool *is_preroll, steady_clock::time_point *frame_timestamp)
338 {
339         assert(!should_quit.should_quit());
340
341         *dropped_frames = 0;
342         *frame_duration = this->frame_duration;
343
344         const BMDTimeValue buffer = lrint(*frame_duration * global_flags.output_buffer_frames);
345         const BMDTimeValue max_overshoot = lrint(*frame_duration * global_flags.output_slop_frames);
346         BMDTimeValue target_time = pts - buffer;
347
348         // While prerolling, we send out frames as quickly as we can.
349         if (target_time < base_pts) {
350                 *is_preroll = true;
351                 ++metric_decklink_output_scheduled_frames_preroll;
352                 return;
353         }
354
355         *is_preroll = !playback_started;
356
357         if (!playback_started) {
358                 if (output->EndAudioPreroll() != S_OK) {
359                         fprintf(stderr, "Could not end audio preroll\n");
360                         exit(1);  // TODO
361                 }
362                 if (output->StartScheduledPlayback(base_pts, TIMEBASE, 1.0) != S_OK) {
363                         fprintf(stderr, "Could not start playback\n");
364                         exit(1);  // TODO
365                 }
366                 playback_started = true;
367         }
368
369         BMDTimeValue stream_frame_time;
370         double playback_speed;
371         output->GetScheduledStreamTime(TIMEBASE, &stream_frame_time, &playback_speed);
372
373         *frame_timestamp = steady_clock::now() +
374                 nanoseconds((target_time - stream_frame_time) * 1000000000 / TIMEBASE);
375
376         // If we're ahead of time, wait for the frame to (approximately) start.
377         if (stream_frame_time < target_time) {
378                 should_quit.sleep_until(*frame_timestamp);
379                 ++metric_decklink_output_scheduled_frames_normal;
380                 return;
381         }
382
383         // If we overshot the previous frame by just a little,
384         // fire off one immediately.
385         if (stream_frame_time < target_time + max_overshoot) {
386                 fprintf(stderr, "Warning: Frame was %ld ms late (but not skipping it due to --output-slop-frames).\n",
387                         lrint((stream_frame_time - target_time) * 1000.0 / TIMEBASE));
388                 ++metric_decklink_output_scheduled_frames_late;
389                 return;
390         }
391
392         // Oops, we missed by more than one frame. Return immediately,
393         // but drop so that we catch up.
394         *dropped_frames = (stream_frame_time - target_time + *frame_duration - 1) / *frame_duration;
395         const int64_t ns_per_frame = this->frame_duration * 1000000000 / TIMEBASE;
396         *frame_timestamp += nanoseconds(*dropped_frames * ns_per_frame);
397         fprintf(stderr, "Dropped %d output frames; skipping.\n", *dropped_frames);
398         metric_decklink_output_scheduled_frames_dropped += *dropped_frames;
399         ++metric_decklink_output_scheduled_frames_normal;
400 }
401
402 uint32_t DeckLinkOutput::pick_video_mode(uint32_t mode) const
403 {
404         if (video_modes.count(mode)) {
405                 return mode;
406         }
407
408         // Prioritize 59.94 > 60 > 29.97. If none of those are found, then pick the highest one.
409         for (const pair<int, int> &desired : vector<pair<int, int>>{ { 60000, 1001 }, { 60, 0 }, { 30000, 1001 } }) {
410                 for (const auto &it : video_modes) {
411                         if (it.second.frame_rate_num * desired.second == desired.first * it.second.frame_rate_den) {
412                                 return it.first;
413                         }
414                 }
415         }
416
417         uint32_t best_mode = 0;
418         double best_fps = 0.0;
419         for (const auto &it : video_modes) {
420                 double fps = double(it.second.frame_rate_num) / it.second.frame_rate_den;
421                 if (fps > best_fps) {
422                         best_mode = it.first;
423                         best_fps = fps;
424                 }
425         }
426         return best_mode;
427 }
428
429 YCbCrLumaCoefficients DeckLinkOutput::preferred_ycbcr_coefficients() const
430 {
431         if (current_mode_flags & bmdDisplayModeColorspaceRec601) {
432                 return YCBCR_REC_601;
433         } else {
434                 // Don't bother checking bmdDisplayModeColorspaceRec709;
435                 // if none is set, 709 is a good default anyway.
436                 return YCBCR_REC_709;
437         }
438 }
439
440 HRESULT DeckLinkOutput::ScheduledFrameCompleted(/* in */ IDeckLinkVideoFrame *completedFrame, /* in */ BMDOutputFrameCompletionResult result)
441 {
442         Frame *frame = static_cast<Frame *>(completedFrame);
443         switch (result) {
444         case bmdOutputFrameCompleted:
445                 ++metric_decklink_output_completed_frames_completed;
446                 break;
447         case bmdOutputFrameDisplayedLate:
448                 fprintf(stderr, "Output frame displayed late (pts=%ld)\n", frame->pts);
449                 fprintf(stderr, "Consider increasing --output-buffer-frames if this persists.\n");
450                 ++metric_decklink_output_completed_frames_late;
451                 break;
452         case bmdOutputFrameDropped:
453                 fprintf(stderr, "Output frame was dropped (pts=%ld)\n", frame->pts);
454                 fprintf(stderr, "Consider increasing --output-buffer-frames if this persists.\n");
455                 ++metric_decklink_output_completed_frames_dropped;
456                 break;
457         case bmdOutputFrameFlushed:
458                 fprintf(stderr, "Output frame was flushed (pts=%ld)\n", frame->pts);
459                 ++metric_decklink_output_completed_frames_flushed;
460                 break;
461         default:
462                 fprintf(stderr, "Output frame completed with unknown status %d\n", result);
463                 ++metric_decklink_output_completed_frames_unknown;
464                 break;
465         }
466
467         static int frameno = 0;
468         print_latency("DeckLink output latency (frame received → output on HDMI):", frame->received_ts, false, &frameno, &latency_histogram);
469
470         {
471                 lock_guard<mutex> lock(frame_queue_mutex);
472                 frame_freelist.push(unique_ptr<Frame>(frame));
473                 --num_frames_in_flight;
474                 --metric_decklink_output_inflight_frames;
475         }
476
477         return S_OK;
478 }
479
480 HRESULT DeckLinkOutput::ScheduledPlaybackHasStopped()
481 {
482         printf("playback stopped!\n");
483         return S_OK;
484 }
485
486 unique_ptr<DeckLinkOutput::Frame> DeckLinkOutput::get_frame()
487 {
488         lock_guard<mutex> lock(frame_queue_mutex);
489
490         if (!frame_freelist.empty()) {
491                 unique_ptr<Frame> frame = move(frame_freelist.front());
492                 frame_freelist.pop();
493                 return frame;
494         }
495
496         unique_ptr<Frame> frame(new Frame);
497
498         size_t stride;
499         if (global_flags.ten_bit_output) {
500                 stride = v210Converter::get_v210_stride(width);
501                 GLint v210_width = stride / sizeof(uint32_t);
502                 frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGB10_A2, v210_width, height);
503
504                 // We need valid texture state, or NVIDIA won't allow us to write to the texture.
505                 glBindTexture(GL_TEXTURE_2D, frame->uyvy_tex);
506                 check_error();
507                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
508                 check_error();
509         } else {
510                 stride = width * 2;
511                 frame->uyvy_tex = resource_pool->create_2d_texture(GL_RGBA8, width / 2, height);
512         }
513
514         glGenBuffers(1, &frame->pbo);
515         check_error();
516         glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo);
517         check_error();
518         glBufferStorage(GL_PIXEL_PACK_BUFFER, stride * height, NULL, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
519         check_error();
520         frame->uyvy_ptr = (uint8_t *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, stride * height, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
521         check_error();
522         frame->uyvy_ptr_local.reset(new uint8_t[stride * height]);
523         frame->resource_pool = resource_pool;
524
525         return frame;
526 }
527
528 void DeckLinkOutput::present_thread_func()
529 {
530         pthread_setname_np(pthread_self(), "DeckLinkOutput");
531         for ( ;; ) {
532                 unique_ptr<Frame> frame;
533                 {
534                         unique_lock<mutex> lock(frame_queue_mutex);
535                         frame_queues_changed.wait(lock, [this]{
536                                 return should_quit.should_quit() || !pending_video_frames.empty();
537                         });
538                         if (should_quit.should_quit()) {
539                                 return;
540                         }
541                         frame = move(pending_video_frames.front());
542                         pending_video_frames.pop();
543                         ++num_frames_in_flight;
544                         ++metric_decklink_output_inflight_frames;
545                 }
546
547                 glClientWaitSync(frame->fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
548                 check_error();
549                 frame->fence.reset();
550
551                 if (global_flags.ten_bit_output) {
552                         memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, v210Converter::get_v210_stride(width) * height);
553                 } else {
554                         memcpy(frame->uyvy_ptr_local.get(), frame->uyvy_ptr, width * height * 2);
555                 }
556
557                 // Release any input frames we needed to render this frame.
558                 frame->input_frames.clear();
559
560                 BMDTimeValue pts = frame->pts;
561                 BMDTimeValue duration = frame->duration;
562                 HRESULT res = output->ScheduleVideoFrame(frame.get(), pts, duration, TIMEBASE);
563                 if (res == S_OK) {
564                         frame.release();  // Owned by the driver now.
565                 } else {
566                         fprintf(stderr, "Could not schedule video frame! (error=0x%08x)\n", res);
567
568                         lock_guard<mutex> lock(frame_queue_mutex);
569                         frame_freelist.push(move(frame));
570                         --num_frames_in_flight;
571                         --metric_decklink_output_inflight_frames;
572                 }
573         }
574 }
575
576 HRESULT STDMETHODCALLTYPE DeckLinkOutput::QueryInterface(REFIID, LPVOID *)
577 {
578         return E_NOINTERFACE;
579 }
580
581 ULONG STDMETHODCALLTYPE DeckLinkOutput::AddRef()
582 {
583         return refcount.fetch_add(1) + 1;
584 }
585
586 ULONG STDMETHODCALLTYPE DeckLinkOutput::Release()
587 {
588         int new_ref = refcount.fetch_sub(1) - 1;
589         if (new_ref == 0)
590                 delete this;
591         return new_ref;
592 }
593
594 DeckLinkOutput::Frame::~Frame()
595 {
596         glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
597         check_error();
598         glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
599         check_error();
600         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
601         check_error();
602         glDeleteBuffers(1, &pbo);
603         check_error();
604         resource_pool->release_2d_texture(uyvy_tex);
605         check_error();
606 }
607
608 HRESULT STDMETHODCALLTYPE DeckLinkOutput::Frame::QueryInterface(REFIID, LPVOID *)
609 {
610         return E_NOINTERFACE;
611 }
612
613 ULONG STDMETHODCALLTYPE DeckLinkOutput::Frame::AddRef()
614 {
615         return refcount.fetch_add(1) + 1;
616 }
617
618 ULONG STDMETHODCALLTYPE DeckLinkOutput::Frame::Release()
619 {
620         int new_ref = refcount.fetch_sub(1) - 1;
621         if (new_ref == 0)
622                 delete this;
623         return new_ref;
624 }
625
626 long DeckLinkOutput::Frame::GetWidth()
627 {
628         return global_flags.width;
629 }
630
631 long DeckLinkOutput::Frame::GetHeight()
632 {
633         return global_flags.height;
634 }
635
636 long DeckLinkOutput::Frame::GetRowBytes()
637 {
638         if (global_flags.ten_bit_output) {
639                 return v210Converter::get_v210_stride(global_flags.width);
640         } else {
641                 return global_flags.width * 2;
642         }
643 }
644
645 BMDPixelFormat DeckLinkOutput::Frame::GetPixelFormat()
646 {
647         if (global_flags.ten_bit_output) {
648                 return bmdFormat10BitYUV;
649         } else {
650                 return bmdFormat8BitYUV;
651         }
652 }
653
654 BMDFrameFlags DeckLinkOutput::Frame::GetFlags()
655 {
656         return bmdFrameFlagDefault;
657 }
658
659 HRESULT DeckLinkOutput::Frame::GetBytes(/* out */ void **buffer)
660 {
661         *buffer = uyvy_ptr_local.get();
662         return S_OK;
663 }
664
665 HRESULT DeckLinkOutput::Frame::GetTimecode(/* in */ BMDTimecodeFormat format, /* out */ IDeckLinkTimecode **timecode)
666 {
667         fprintf(stderr, "STUB: GetTimecode()\n");
668         return E_NOTIMPL;
669 }
670
671 HRESULT DeckLinkOutput::Frame::GetAncillaryData(/* out */ IDeckLinkVideoFrameAncillary **ancillary)
672 {
673         fprintf(stderr, "STUB: GetAncillaryData()\n");
674         return E_NOTIMPL;
675 }