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 <atomic>
23 using std::atomic;
24
25 /* Include internal.h first to avoid conflict between winsock.h (used by
26 * DeckLink headers) and winsock2.h (used by libavformat) in MSVC++ builds */
27 extern "C" {
29 }
30
31 #include <DeckLinkAPI.h>
32
33 extern "C" {
38 }
39
42 #if CONFIG_LIBKLVANC
43 #include "libklvanc/vanc.h"
44 #include "libklvanc/vanc-lines.h"
45 #include "libklvanc/pixels.h"
46 #endif
47
48 /* DeckLink callback class declaration */
50 {
51 public:
59 {
62 else
63 return ((
GetWidth() + 47) / 48) * 128;
64 }
66 {
68 return bmdFormat8BitYUV;
69 else
70 return bmdFormat10BitYUV;
71 }
72 virtual BMDFrameFlags STDMETHODCALLTYPE
GetFlags (
void)
73 {
75 return _avframe->
linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault;
76 else
77 return bmdFrameFlagDefault;
78 }
79
81 {
85 else
87 } else {
89 }
90 return S_OK;
91 }
92
93 virtual HRESULT STDMETHODCALLTYPE
GetTimecode (BMDTimecodeFormat
format, IDeckLinkTimecode **timecode) {
return S_FALSE; }
94 virtual HRESULT STDMETHODCALLTYPE
GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary)
95 {
99 return S_OK;
100 } else {
101 return S_FALSE;
102 }
103 }
104 virtual HRESULT STDMETHODCALLTYPE
SetAncillaryData(IDeckLinkVideoFrameAncillary *ancillary)
105 {
110 return S_OK;
111 }
112 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID iid, LPVOID *ppv) {
return E_NOINTERFACE; }
115 {
122 delete this;
123 }
125 }
126
134
135 private:
137 };
138
140 {
141 public:
143 {
146
149 if (
frame->_avpacket)
151
153 ctx->frames_buffer_available_spots++;
156
157 return S_OK;
158 }
160 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID iid, LPVOID *ppv) {
return E_NOINTERFACE; }
161 virtual ULONG STDMETHODCALLTYPE
AddRef(
void) {
return 1; }
162 virtual ULONG STDMETHODCALLTYPE
Release(
void) {
return 1; }
163 };
164
166 {
170
173 return -1;
174 }
175
179 " Only AV_PIX_FMT_UYVY422 is supported.\n");
180 return -1;
181 }
182 ctx->raw_format = bmdFormat8BitYUV;
185 " Only V210 and wrapped frame with AV_PIX_FMT_UYVY422 are supported.\n");
186 return -1;
187 } else {
188 ctx->raw_format = bmdFormat10BitYUV;
189 }
190
193 return -1;
194 }
198 " Check available formats with -list_formats 1.\n");
199 return -1;
200 }
201 if (
ctx->supports_vanc &&
ctx->dlo->EnableVideoOutput(
ctx->bmd_mode, bmdVideoOutputVANC) != S_OK) {
203 ctx->supports_vanc = 0;
204 }
205 if (!
ctx->supports_vanc &&
ctx->dlo->EnableVideoOutput(
ctx->bmd_mode, bmdVideoOutputFlagDefault) != S_OK) {
207 return -1;
208 }
209
210 /* Set callback. */
212 ctx->dlo->SetScheduledFrameCompletionCallback(
ctx->output_callback);
213
216 ctx->frames_preroll /= 1000;
217
218 /* Buffer twice as many frames as the preroll. */
219 ctx->frames_buffer =
ctx->frames_preroll * 2;
220 ctx->frames_buffer =
FFMIN(
ctx->frames_buffer, 60);
223 ctx->frames_buffer_available_spots =
ctx->frames_buffer;
224
226 avctx->
url,
ctx->frames_preroll,
ctx->frames_buffer);
227
228 /* The device expects the framerate to be fixed. */
230
232
233 return 0;
234 }
235
237 {
241
244 return -1;
245 }
246 if (
c->sample_rate != 48000) {
248 " Only 48kHz is supported.\n");
249 return -1;
250 }
251 if (
c->ch_layout.nb_channels != 2 &&
c->ch_layout.nb_channels != 8 &&
c->ch_layout.nb_channels != 16) {
253 " Only 2, 8 or 16 channels are supported.\n");
254 return -1;
255 }
256 if (
ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
257 bmdAudioSampleType16bitInteger,
258 c->ch_layout.nb_channels,
259 bmdAudioOutputStreamTimestamped) != S_OK) {
261 return -1;
262 }
263 if (
ctx->dlo->BeginAudioPreroll() != S_OK) {
265 return -1;
266 }
267
268 /* The device expects the sample rate to be fixed. */
270 ctx->channels =
c->ch_layout.nb_channels;
271
273
274 return 0;
275 }
276
278 {
281
282 if (
ctx->playback_started) {
283 BMDTimeValue actual;
284 ctx->dlo->StopScheduledPlayback(
ctx->last_pts *
ctx->bmd_tb_num,
285 &actual,
ctx->bmd_tb_den);
286 ctx->dlo->DisableVideoOutput();
288 ctx->dlo->DisableAudioOutput();
289 }
290
292
293 if (
ctx->output_callback)
294 delete ctx->output_callback;
295
298
299 #if CONFIG_LIBKLVANC
300 klvanc_context_destroy(
ctx->vanc_ctx);
301 #endif
302
304
305 return 0;
306 }
307
308 #if CONFIG_LIBKLVANC
310 AVPacket *
pkt,
struct klvanc_line_set_s *vanc_lines)
311 {
312 struct klvanc_packet_eia_708b_s *cdp;
313 uint16_t *cdp_words;
315 uint8_t cc_count;
318
321 return;
322
324
325 ret = klvanc_create_eia708_cdp(&cdp);
327 return;
328
329 ret = klvanc_set_framerate_EIA_708B(cdp,
ctx->bmd_tb_num,
ctx->bmd_tb_den);
332 ctx->bmd_tb_num,
ctx->bmd_tb_den);
333 klvanc_destroy_eia708_cdp(cdp);
334 return;
335 }
336
337 if (cc_count > KLVANC_MAX_CC_COUNT) {
339 cc_count = KLVANC_MAX_CC_COUNT;
340 }
341
342 /* CC data */
343 cdp->header.ccdata_present = 1;
344 cdp->header.caption_service_active = 1;
345 cdp->ccdata.cc_count = cc_count;
346 for (
i = 0;
i < cc_count;
i++) {
347 if (
data [3*
i] & 0x04)
348 cdp->ccdata.cc[
i].cc_valid = 1;
349 cdp->ccdata.cc[
i].cc_type =
data[3*
i] & 0x03;
350 cdp->ccdata.cc[
i].cc_data[0] =
data[3*
i+1];
351 cdp->ccdata.cc[
i].cc_data[1] =
data[3*
i+2];
352 }
353
354 klvanc_finalize_EIA_708B(cdp,
ctx->cdp_sequence_num++);
355 ret = klvanc_convert_EIA_708B_to_words(cdp, &cdp_words, &
len);
356 klvanc_destroy_eia708_cdp(cdp);
359 return;
360 }
361
362 ret = klvanc_line_insert(
ctx->vanc_ctx, vanc_lines, cdp_words,
len, 11, 0);
363 free(cdp_words);
366 return;
367 }
368 }
369
372 {
373 struct klvanc_line_set_s vanc_lines = { 0 };
375
376 if (!
ctx->supports_vanc)
377 return 0;
378
379 construct_cc(avctx,
ctx,
pkt, &vanc_lines);
380
381 IDeckLinkVideoFrameAncillary *vanc;
382 int result =
ctx->dlo->CreateAncillaryData(bmdFormat10BitYUV, &vanc);
386 goto done;
387 }
388
389 /* Now that we've got all the VANC lines in a nice orderly manner, generate the
390 final VANC sections for the Decklink output */
391 for (
i = 0;
i < vanc_lines.num_lines;
i++) {
392 struct klvanc_line_s *
line = vanc_lines.lines[
i];
393 int real_line;
394 void *buf;
395
397 break;
398
399 /* FIXME: include hack for certain Decklink cards which mis-represent
400 line numbers for pSF frames */
401 real_line =
line->line_number;
402
403 result = vanc->GetBufferForVerticalBlankingLine(real_line, &buf);
406 continue;
407 }
408
409 /* Generate the full line taking into account all VANC packets on that line */
410 result = klvanc_generate_vanc_line_v210(
ctx->vanc_ctx,
line, (uint8_t *) buf,
414 continue;
415 }
416 }
417
419 vanc->Release();
423 }
424
425 done:
426 for (
i = 0;
i < vanc_lines.num_lines;
i++)
427 klvanc_line_free(vanc_lines.lines[
i]);
428
430 }
431 #endif
432
434 {
441 uint32_t buffered;
442 HRESULT hr;
443
446 tmp->width !=
ctx->bmd_width ||
447 tmp->height !=
ctx->bmd_height) {
450 }
451
453 if (!avframe) {
456 }
457
459 } else {
461 if (!avpacket) {
464 }
465
467
468 #if CONFIG_LIBKLVANC
469 if (decklink_construct_vanc(avctx,
ctx,
pkt,
frame))
471 #endif
472 }
473
479 }
480
481 /* Always keep at most one second of frames buffered. */
483 while (
ctx->frames_buffer_available_spots == 0) {
485 }
486 ctx->frames_buffer_available_spots--;
488
489 /* Schedule frame for playback. */
490 hr =
ctx->dlo->ScheduleVideoFrame((
class IDeckLinkVideoFrame *)
frame,
492 ctx->bmd_tb_num,
ctx->bmd_tb_den);
493 /* Pass ownership to DeckLink, or release on failure */
495 if (hr != S_OK) {
497 " error %08x.\n", (uint32_t) hr);
499 }
500
501 ctx->dlo->GetBufferedVideoFrameCount(&buffered);
503 if (
pkt->
pts > 2 && buffered <= 2)
505 " Video may misbehave!\n");
506
507 /* Preroll video frames. */
508 if (!
ctx->playback_started &&
pkt->
pts >
ctx->frames_preroll) {
510 if (
ctx->audio &&
ctx->dlo->EndAudioPreroll() != S_OK) {
513 }
515 if (
ctx->dlo->StartScheduledPlayback(0,
ctx->bmd_tb_den, 1.0) != S_OK) {
518 }
519 ctx->playback_started = 1;
520 }
521
522 return 0;
523 }
524
526 {
529 int sample_count =
pkt->
size / (
ctx->channels << 1);
530 uint32_t buffered;
531
532 ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
533 if (
pkt->
pts > 1 && !buffered)
535 " Audio will misbehave!\n");
536
538 bmdAudioSampleRate48kHz,
NULL) != S_OK) {
541 }
542
543 return 0;
544 }
545
546 extern "C" {
547
549 {
552 unsigned int n;
554
565 #if CONFIG_LIBKLVANC
566 if (klvanc_context_create(&
ctx->vanc_ctx) < 0) {
569 }
570 ctx->supports_vanc = 1;
571 #endif
572
573 /* List available devices and exit. */
574 if (
ctx->list_devices) {
577 }
578
582
583 /* Get output device. */
584 if (
ctx->dl->QueryInterface(IID_IDeckLinkOutput, (
void **) &
ctx->dlo) != S_OK) {
589 }
590
591 /* List supported formats. */
592 if (
ctx->list_formats) {
596 }
597
598 /* Setup streams. */
609 } else {
612 }
613 }
614
615 return 0;
616
620 }
621
623 {
627
629
634
636 }
637
639 {
641 }
642
643 } /* extern "C" */