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