1 /*
2 * Blackmagic DeckLink output
3 * Copyright (c) 2013-2014 Ramiro Polla
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <DeckLinkAPI.h>
23 #ifdef _WIN32
24 #include <DeckLinkAPI_i.c>
26 #else
27 #include <DeckLinkAPIDispatch.cpp>
29 #endif
30
31 #include <pthread.h>
32 #include <semaphore.h>
33
34 extern "C" {
38 }
39
41
43
45 /* DeckLink SDK interfaces */
49
50 /* DeckLink mode information */
57
58 /* Streams present */
61
62 /* Status */
65
66 /* Options */
70
73
75
77 };
78
79 /* DeckLink callback class declaration */
81 {
82 public:
87
91 virtual BMDPixelFormat STDMETHODCALLTYPE
GetPixelFormat(
void) {
return bmdFormat8BitYUV; }
92 virtual BMDFrameFlags STDMETHODCALLTYPE
GetFlags (
void) {
return bmdVideoOutputFlagDefault; }
94
97
101
104
105 private:
110 };
111
113 {
114 public:
116 {
120
122
124
126 }
131 };
132
133 #ifdef _WIN32
134 static IDeckLinkIterator *CreateDeckLinkIteratorInstance(void)
135 {
136 IDeckLinkIterator *iter;
137
138 if (CoInitialize(NULL) !=
S_OK) {
140 return NULL;
141 }
142
143 if (CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL,
144 IID_IDeckLinkIterator, (
void**) &iter) !=
S_OK) {
146 return NULL;
147 }
148
149 return iter;
150 }
151 #endif
152
153 /* free() is needed for a string returned by the DeckLink SDL. */
154 #undef free
155
156 #ifdef _WIN32
158 {
160 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
162 if (s)
163 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
165 }
166 #define DECKLINK_STR OLECHAR *
167 #define DECKLINK_STRDUP dup_wchar_to_utf8
168 #else
169 #define DECKLINK_STR const char *
170 #define DECKLINK_STRDUP av_strdup
171 #endif
172
174 {
176 HRESULT hr = This->GetDisplayName(&tmpDisplayName);
178 return hr;
180 free((void *) tmpDisplayName);
181 return hr;
182 }
183
186 int tb_num, int tb_den)
187 {
188 BMDDisplayModeSupport support;
189 IDeckLinkDisplayMode *
mode;
190
191 if (tb_num == 1) {
192 tb_num *= 1000;
193 tb_den *= 1000;
194 }
200
201 mode->GetFrameRate(&bmd_tb_num, &bmd_tb_den);
202
203 if (bmd_width == width && bmd_height == height &&
204 bmd_tb_num == tb_num && bmd_tb_den == tb_den) {
205 ctx->
bmd_mode = mode->GetDisplayMode();
210 }
211
212 mode->Release();
213 }
214 if (ctx->
bmd_mode == bmdModeUnknown)
215 return -1;
216 if (ctx->
dlo->DoesSupportVideoMode(ctx->
bmd_mode, bmdFormat8BitYUV,
217 bmdVideoOutputFlagDefault,
218 &support, NULL) !=
S_OK)
219 return -1;
220 if (support == bmdDisplayModeSupported)
221 return 0;
222
223 return -1;
224 }
225
227 {
231
234 return -1;
235 }
236
239 " Only AV_PIX_FMT_UYVY422 is supported.\n");
240 return -1;
241 }
245 " Check available formats with -list_formats 1.\n");
246 return -1;
247 }
248 if (
ctx->dlo->EnableVideoOutput(
ctx->bmd_mode,
249 bmdVideoOutputFlagDefault) !=
S_OK) {
251 return -1;
252 }
253
254 /* Set callback. */
256 ctx->dlo->SetScheduledFrameCompletionCallback(
ctx->callback);
257
258 /* Start video semaphore. */
261 ctx->frames_preroll /= 1000;
262
263 /* Buffer twice as many frames as the preroll. */
264 ctx->frames_buffer =
ctx->frames_preroll * 2;
265 ctx->frames_buffer =
FFMIN(
ctx->frames_buffer, 60);
266 sem_init(&
ctx->semaphore, 0,
ctx->frames_buffer);
267
268 /* The device expects the framerate to be fixed. */
270
272
273 return 0;
274 }
275
277 {
281
284 return -1;
285 }
288 " Only 48kHz is supported.\n");
289 return -1;
290 }
293 " Only stereo and 7.1 are supported.\n");
294 return -1;
295 }
296 if (
ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
297 bmdAudioSampleType16bitInteger,
299 bmdAudioOutputStreamTimestamped) !=
S_OK) {
301 return -1;
302 }
303 if (
ctx->dlo->BeginAudioPreroll() !=
S_OK) {
305 return -1;
306 }
307
308 /* The device expects the sample rate to be fixed. */
311
313
314 return 0;
315 }
316
318 {
321
322 if (
ctx->playback_started) {
323 BMDTimeValue actual;
324 ctx->dlo->StopScheduledPlayback(
ctx->last_pts *
ctx->bmd_tb_num,
325 &actual,
ctx->bmd_tb_den);
326 ctx->dlo->DisableVideoOutput();
328 ctx->dlo->DisableAudioOutput();
329 }
330
335
337 delete ctx->callback;
338
339 sem_destroy(&
ctx->semaphore);
340
342
343 return 0;
344 }
345
347 {
355
356 /* HACK while av_uncoded_frame() isn't implemented */
358
360 if (!tmp)
363 tmp->width =
ctx->bmd_width;
364 tmp->height =
ctx->bmd_height;
369 }
372 tmp->height);
375 if (!avframe) {
378 }
379 /* end HACK */
380
382 (
void *) avframe->
data[0]);
386 }
387
388 /* Always keep at most one second of frames buffered. */
389 sem_wait(&
ctx->semaphore);
390
391 /* Schedule frame for playback. */
392 hr =
ctx->dlo->ScheduleVideoFrame((
struct IDeckLinkVideoFrame *)
frame,
393 pkt->
pts *
ctx->bmd_tb_num,
394 ctx->bmd_tb_num,
ctx->bmd_tb_den);
397 " error %08x.\n", (uint32_t) hr);
400 }
401
402 ctx->dlo->GetBufferedVideoFrameCount(&buffered);
404 if (pkt->
pts > 2 && buffered <= 2)
406 " Video may misbehave!\n");
407
408 /* Preroll video frames. */
409 if (!
ctx->playback_started && pkt->
pts >
ctx->frames_preroll) {
411 if (
ctx->audio &&
ctx->dlo->EndAudioPreroll() !=
S_OK) {
414 }
416 if (
ctx->dlo->StartScheduledPlayback(0,
ctx->bmd_tb_den, 1.0) !=
S_OK) {
419 }
420 ctx->playback_started = 1;
421 }
422
423 return 0;
424 }
425
427 {
430 int sample_count = pkt->
size / (
ctx->channels << 1);
432
433 ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
434 if (pkt->
pts > 1 && !buffered)
436 " Audio will misbehave!\n");
437
438 if (
ctx->dlo->ScheduleAudioSamples(pkt->
data, sample_count, pkt->
pts,
439 bmdAudioSampleRate48kHz, NULL) !=
S_OK) {
442 }
443
444 return 0;
445 }
446
447 extern "C" {
448
450 {
453 IDeckLinkIterator *iter;
454 IDeckLink *dl = NULL;
456
464
465 iter = CreateDeckLinkIteratorInstance();
466 if (!iter) {
469 }
470
471 /* List available devices. */
472 if (
ctx->list_devices) {
474 while (iter->Next(&dl) ==
S_OK) {
475 const char *displayName;
479 dl->Release();
480 }
481 iter->Release();
483 }
484
485 /* Open device. */
486 while (iter->Next(&dl) ==
S_OK) {
487 const char *displayName;
489 if (!strcmp(avctx->
filename, displayName)) {
492 break;
493 }
495 dl->Release();
496 }
497 iter->Release();
501 }
502
503 /* Get output device. */
504 if (
ctx->dl->QueryInterface(IID_IDeckLinkOutput, (
void **) &
ctx->dlo) !=
S_OK) {
509 }
510
511 if (
ctx->dlo->GetDisplayModeIterator(&
ctx->itermode) !=
S_OK) {
515 }
516
517 /* List supported formats. */
518 if (
ctx->list_formats) {
519 IDeckLinkDisplayMode *
mode;
520
523 while (
ctx->itermode->Next(&mode) ==
S_OK) {
524 BMDTimeValue tb_num, tb_den;
525 mode->GetFrameRate(&tb_num, &tb_den);
527 mode->GetWidth(), mode->GetHeight(),
528 (int) tb_den, (int) tb_num);
529 switch (mode->GetFieldDominance()) {
530 case bmdLowerFieldFirst:
532 case bmdUpperFieldFirst:
534 }
536 mode->Release();
537 }
538
539 ctx->itermode->Release();
543 }
544
545 /* Setup streams. */
551 goto error;
554 goto error;
555 } else {
557 goto error;
558 }
559 }
560 ctx->itermode->Release();
561
562 return 0;
563
564 error:
565
568
570 }
571
573 {
577
579
584
586 }
587
588 } /* extern "C" */