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