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