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" {
40 }
41
44 #if CONFIG_LIBKLVANC
45 #include "libklvanc/vanc.h"
46 #include "libklvanc/vanc-lines.h"
47 #include "libklvanc/pixels.h"
48 #endif
49
50 /* DeckLink callback class declaration */
52 {
53 public:
61 {
64 else
65 return ((
GetWidth() + 47) / 48) * 128;
66 }
68 {
70 return bmdFormat8BitYUV;
71 else
72 return bmdFormat10BitYUV;
73 }
74 virtual BMDFrameFlags STDMETHODCALLTYPE
GetFlags (
void)
75 {
77 return _avframe->
linesize[0] < 0 ? bmdFrameFlagFlipVertical : bmdFrameFlagDefault;
78 else
79 return bmdFrameFlagDefault;
80 }
81
83 {
87 else
89 } else {
91 }
92 return S_OK;
93 }
94
95 virtual HRESULT STDMETHODCALLTYPE
GetTimecode (BMDTimecodeFormat
format, IDeckLinkTimecode **timecode) {
return S_FALSE; }
96 virtual HRESULT STDMETHODCALLTYPE
GetAncillaryData(IDeckLinkVideoFrameAncillary **ancillary)
97 {
101 return S_OK;
102 } else {
103 return S_FALSE;
104 }
105 }
106 virtual HRESULT STDMETHODCALLTYPE
SetAncillaryData(IDeckLinkVideoFrameAncillary *ancillary)
107 {
112 return S_OK;
113 }
114 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID iid, LPVOID *ppv) {
return E_NOINTERFACE; }
117 {
124 delete this;
125 }
127 }
128
136
137 private:
139 };
140
142 {
143 public:
145 {
148
151 if (
frame->_avpacket)
153
155 ctx->frames_buffer_available_spots++;
158
159 return S_OK;
160 }
162 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID iid, LPVOID *ppv) {
return E_NOINTERFACE; }
163 virtual ULONG STDMETHODCALLTYPE
AddRef(
void) {
return 1; }
164 virtual ULONG STDMETHODCALLTYPE
Release(
void) {
return 1; }
165 };
166
168 {
172
175 return -1;
176 }
177
181 " Only AV_PIX_FMT_UYVY422 is supported.\n");
182 return -1;
183 }
184 ctx->raw_format = bmdFormat8BitYUV;
187 " Only V210 and wrapped frame with AV_PIX_FMT_UYVY422 are supported.\n");
188 return -1;
189 } else {
190 ctx->raw_format = bmdFormat10BitYUV;
191 }
192
195 return -1;
196 }
200 " Check available formats with -list_formats 1.\n");
201 return -1;
202 }
203 if (
ctx->supports_vanc &&
ctx->dlo->EnableVideoOutput(
ctx->bmd_mode, bmdVideoOutputVANC) != S_OK) {
205 ctx->supports_vanc = 0;
206 }
207 if (!
ctx->supports_vanc &&
ctx->dlo->EnableVideoOutput(
ctx->bmd_mode, bmdVideoOutputFlagDefault) != S_OK) {
209 return -1;
210 }
211
212 /* Set callback. */
214 ctx->dlo->SetScheduledFrameCompletionCallback(
ctx->output_callback);
215
218 ctx->frames_preroll /= 1000;
219
220 /* Buffer twice as many frames as the preroll. */
221 ctx->frames_buffer =
ctx->frames_preroll * 2;
222 ctx->frames_buffer =
FFMIN(
ctx->frames_buffer, 60);
225 ctx->frames_buffer_available_spots =
ctx->frames_buffer;
226
228 avctx->
url,
ctx->frames_preroll,
ctx->frames_buffer);
229
230 /* The device expects the framerate to be fixed. */
232
234
235 return 0;
236 }
237
239 {
243
246 return -1;
247 }
248
250 /* Regardless of the number of channels in the codec, we're only
251 using 2 SDI audio channels at 48000Hz */
254 if (
c->sample_rate != 48000) {
256 " Only 48kHz is supported.\n");
257 return -1;
258 }
259 if (
c->ch_layout.nb_channels != 2 &&
c->ch_layout.nb_channels != 8 &&
c->ch_layout.nb_channels != 16) {
261 " Only 2, 8 or 16 channels are supported.\n");
262 return -1;
263 }
264 ctx->channels =
c->ch_layout.nb_channels;
265 } else {
267 " Only PCM_S16LE and AC-3 are supported.\n");
268 return -1;
269 }
270
271 if (
ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz,
272 bmdAudioSampleType16bitInteger,
274 bmdAudioOutputStreamTimestamped) != S_OK) {
276 return -1;
277 }
278 if (
ctx->dlo->BeginAudioPreroll() != S_OK) {
280 return -1;
281 }
282
283 /* The device expects the sample rate to be fixed. */
285
287
288 return 0;
289 }
290
291 /* Wrap the AC-3 packet into an S337 payload that is in S16LE format which can be easily
292 injected into the PCM stream. Note: despite the function name, only AC-3 is implemented */
294 {
295 /* Note: if the packet size is not divisible by four, we need to make the actual
296 payload larger to ensure it ends on an two channel S16LE boundary */
298 uint16_t bitcount =
pkt->
size * 8;
299 uint8_t *s337_payload;
301
302 /* Sanity check: According to SMPTE ST 340:2015 Sec 4.1, the AC-3 sync frame will
303 exactly match the 1536 samples of baseband (PCM) audio that it represents. */
306
307 /* Encapsulate AC3 syncframe into SMPTE 337 packet */
308 s337_payload = (uint8_t *)
av_malloc(payload_size);
309 if (s337_payload ==
NULL)
312 bytestream2_put_le16u(&pb, 0xf872); /* Sync word 1 */
313 bytestream2_put_le16u(&pb, 0x4e1f); /* Sync word 1 */
314 bytestream2_put_le16u(&pb, 0x0001); /* Burst Info, including data type (1=ac3) */
315 bytestream2_put_le16u(&pb, bitcount); /* Length code */
318
319 /* Ensure final payload is aligned on 4-byte boundary */
323 bytestream2_put_le16u(&pb, 0);
324
325 *outsize = payload_size;
326 *outbuf = s337_payload;
327 return 0;
328 }
329
331 {
333
335 #if CONFIG_LIBKLVANC
337 /* No special setup required */
339 break;
340 #endif
341 default:
343 break;
344 }
345
347 }
348
350 {
352
354 #if CONFIG_LIBKLVANC
356 /* No specific setup required */
358 break;
359 #endif
360 default:
362 break;
363 }
364
366 }
367
369 {
372
373 if (
ctx->playback_started) {
374 BMDTimeValue actual;
375 ctx->dlo->StopScheduledPlayback(
ctx->last_pts *
ctx->bmd_tb_num,
376 &actual,
ctx->bmd_tb_den);
377 ctx->dlo->DisableVideoOutput();
379 ctx->dlo->DisableAudioOutput();
380 }
381
383
384 if (
ctx->output_callback)
385 delete ctx->output_callback;
386
389
390 #if CONFIG_LIBKLVANC
391 klvanc_context_destroy(
ctx->vanc_ctx);
392 #endif
394
397
398 return 0;
399 }
400
401 #if CONFIG_LIBKLVANC
403 AVPacket *
pkt,
struct klvanc_line_set_s *vanc_lines)
404 {
405 struct klvanc_packet_eia_708b_s *cdp;
406 uint16_t *cdp_words;
408 uint8_t cc_count;
411
414 return;
415
417
418 ret = klvanc_create_eia708_cdp(&cdp);
420 return;
421
422 ret = klvanc_set_framerate_EIA_708B(cdp,
ctx->bmd_tb_num,
ctx->bmd_tb_den);
425 ctx->bmd_tb_num,
ctx->bmd_tb_den);
426 klvanc_destroy_eia708_cdp(cdp);
427 return;
428 }
429
430 if (cc_count > KLVANC_MAX_CC_COUNT) {
432 cc_count = KLVANC_MAX_CC_COUNT;
433 }
434
435 /* CC data */
436 cdp->header.ccdata_present = 1;
437 cdp->header.caption_service_active = 1;
438 cdp->ccdata.cc_count = cc_count;
439 for (
i = 0;
i < cc_count;
i++) {
440 if (
data [3*
i] & 0x04)
441 cdp->ccdata.cc[
i].cc_valid = 1;
442 cdp->ccdata.cc[
i].cc_type =
data[3*
i] & 0x03;
443 cdp->ccdata.cc[
i].cc_data[0] =
data[3*
i+1];
444 cdp->ccdata.cc[
i].cc_data[1] =
data[3*
i+2];
445 }
446
447 klvanc_finalize_EIA_708B(cdp,
ctx->cdp_sequence_num++);
448 ret = klvanc_convert_EIA_708B_to_words(cdp, &cdp_words, &
len);
449 klvanc_destroy_eia708_cdp(cdp);
452 return;
453 }
454
455 ret = klvanc_line_insert(
ctx->vanc_ctx, vanc_lines, cdp_words,
len, 11, 0);
456 free(cdp_words);
459 return;
460 }
461 }
462
463 /* See SMPTE ST 2016-3:2009 */
465 AVPacket *
pkt,
struct klvanc_line_set_s *vanc_lines,
467 {
468 struct klvanc_packet_afd_s *afd =
NULL;
469 uint16_t *afd_words =
NULL;
472 int f1_line = 12, f2_line = 0,
ret;
473
476 return;
477
478 ret = klvanc_create_AFD(&afd);
480 return;
481
482 ret = klvanc_set_AFD_val(afd,
data[0]);
486 klvanc_destroy_AFD(afd);
487 return;
488 }
489
490 /* Compute the AR flag based on the DAR (see ST 2016-1:2009 Sec 9.1). Note, we treat
491 anything below 1.4 as 4:3 (as opposed to the standard 1.33), because there are lots
492 of streams in the field that aren't *exactly* 4:3 but a tiny bit larger after doing
493 the math... */
496 afd->aspectRatio = ASPECT_16x9;
497 else
498 afd->aspectRatio = ASPECT_4x3;
499
500 ret = klvanc_convert_AFD_to_words(afd, &afd_words, &
len);
504 }
505
506 ret = klvanc_line_insert(
ctx->vanc_ctx, vanc_lines, afd_words,
len, f1_line, 0);
510 }
511
512 /* For interlaced video, insert into both fields. Switching lines for field 2
513 derived from SMPTE RP 168:2009, Sec 6, Table 2. */
514 switch (
ctx->bmd_mode) {
515 case bmdModeNTSC:
516 case bmdModeNTSC2398:
517 f2_line = 273 - 10 + f1_line;
518 break;
519 case bmdModePAL:
520 f2_line = 319 - 6 + f1_line;
521 break;
522 case bmdModeHD1080i50:
523 case bmdModeHD1080i5994:
524 case bmdModeHD1080i6000:
525 f2_line = 569 - 7 + f1_line;
526 break;
527 default:
528 f2_line = 0;
529 break;
530 }
531
532 if (f2_line > 0) {
533 ret = klvanc_line_insert(
ctx->vanc_ctx, vanc_lines, afd_words,
len, f2_line, 0);
537 }
538 }
539
541 if (afd)
542 klvanc_destroy_AFD(afd);
543 if (afd_words)
544 free(afd_words);
545 }
546
547 /* Parse any EIA-608 subtitles sitting on the queue, and write packet side data
548 that will later be handled by construct_cc... */
550 {
552 uint8_t *cc_data;
553
555 return;
556
558 if (cc_data)
560 }
561
565 {
566 struct klvanc_line_set_s vanc_lines = { 0 };
568
569 if (!
ctx->supports_vanc)
570 return 0;
571
572 parse_608subs(avctx,
ctx,
pkt);
573 construct_cc(avctx,
ctx,
pkt, &vanc_lines);
574 construct_afd(avctx,
ctx,
pkt, &vanc_lines, st);
575
576 /* See if there any pending data packets to process */
581
583 if (
pts >
ctx->last_pts) {
584 /* We haven't gotten to the video frame we are supposed to inject
585 the oldest VANC packet into yet, so leave it on the queue... */
586 break;
587 }
588
590 if (vanc_pkt.
pts + 1 <
ctx->last_pts) {
593 continue;
594 }
595
598 struct klvanc_smpte2038_anc_data_packet_s *pkt_2038 =
NULL;
599
600 klvanc_smpte2038_parse_pes_payload(vanc_pkt.
data, vanc_pkt.
size, &pkt_2038);
601 if (pkt_2038 ==
NULL) {
604 continue;
605 }
606 for (
int i = 0;
i < pkt_2038->lineCount;
i++) {
607 struct klvanc_smpte2038_anc_data_line_s *l = &pkt_2038->lines[
i];
608 uint16_t *vancWords =
NULL;
609 uint16_t vancWordCount;
610
611 if (klvanc_smpte2038_convert_line_to_words(l, &vancWords,
612 &vancWordCount) < 0)
613 break;
614
615 ret = klvanc_line_insert(
ctx->vanc_ctx, &vanc_lines, vancWords,
616 vancWordCount, l->line_number, 0);
617 free(vancWords);
620 break;
621 }
622 }
623 klvanc_smpte2038_anc_data_packet_free(pkt_2038);
624 }
626 }
627
628 IDeckLinkVideoFrameAncillary *vanc;
629 int result =
ctx->dlo->CreateAncillaryData(bmdFormat10BitYUV, &vanc);
633 goto done;
634 }
635
636 /* Now that we've got all the VANC lines in a nice orderly manner, generate the
637 final VANC sections for the Decklink output */
638 for (
i = 0;
i < vanc_lines.num_lines;
i++) {
639 struct klvanc_line_s *
line = vanc_lines.lines[
i];
640 int real_line;
641 void *buf;
642
644 break;
645
646 /* FIXME: include hack for certain Decklink cards which mis-represent
647 line numbers for pSF frames */
648 real_line =
line->line_number;
649
650 result = vanc->GetBufferForVerticalBlankingLine(real_line, &buf);
653 continue;
654 }
655
656 /* Generate the full line taking into account all VANC packets on that line */
657 result = klvanc_generate_vanc_line_v210(
ctx->vanc_ctx,
line, (uint8_t *) buf,
661 continue;
662 }
663 }
664
666 vanc->Release();
670 }
671
672 done:
673 for (
i = 0;
i < vanc_lines.num_lines;
i++)
674 klvanc_line_free(vanc_lines.lines[
i]);
675
677 }
678 #endif
679
681 {
688 uint32_t buffered;
689 HRESULT hr;
690
692
695 tmp->width !=
ctx->bmd_width ||
696 tmp->height !=
ctx->bmd_height) {
699 }
700
702 if (!avframe) {
705 }
706
708 } else {
710 if (!avpacket) {
713 }
714
716
717 #if CONFIG_LIBKLVANC
718 if (decklink_construct_vanc(avctx,
ctx,
pkt,
frame, st))
720 #endif
721 }
722
728 }
729
730 /* Always keep at most one second of frames buffered. */
732 while (
ctx->frames_buffer_available_spots == 0) {
734 }
735 ctx->frames_buffer_available_spots--;
737
740
741 /* Schedule frame for playback. */
742 hr =
ctx->dlo->ScheduleVideoFrame((
class IDeckLinkVideoFrame *)
frame,
744 ctx->bmd_tb_num,
ctx->bmd_tb_den);
745 /* Pass ownership to DeckLink, or release on failure */
747 if (hr != S_OK) {
749 " error %08x.\n", (uint32_t) hr);
751 }
752
753 ctx->dlo->GetBufferedVideoFrameCount(&buffered);
755 if (
pkt->
pts > 2 && buffered <= 2)
757 " Video may misbehave!\n");
758
759 /* Preroll video frames. */
760 if (!
ctx->playback_started &&
pkt->
pts > (
ctx->first_pts +
ctx->frames_preroll)) {
762 if (
ctx->audio &&
ctx->dlo->EndAudioPreroll() != S_OK) {
765 }
767 if (
ctx->dlo->StartScheduledPlayback(
ctx->first_pts *
ctx->bmd_tb_num,
ctx->bmd_tb_den, 1.0) != S_OK) {
770 }
771 ctx->playback_started = 1;
772 }
773
774 return 0;
775 }
776
778 {
782 int sample_count;
783 uint32_t buffered;
784 uint8_t *outbuf =
NULL;
786
787 ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered);
788 if (
pkt->
pts > 1 && !buffered)
790 " Audio will misbehave!\n");
791
793 /* Encapsulate AC3 syncframe into SMPTE 337 packet */
794 int outbuf_size;
798 sample_count = outbuf_size / 4;
799 } else {
800 sample_count =
pkt->
size / (
ctx->channels << 1);
802 }
803
804 if (
ctx->dlo->ScheduleAudioSamples(outbuf, sample_count,
pkt->
pts,
805 bmdAudioSampleRate48kHz,
NULL) != S_OK) {
808 }
809
812
814 }
815
817 {
820
822
823 return 0;
824 }
825
827 {
830
833 }
834
835 return 0;
836 }
837
838 extern "C" {
839
841 {
844 unsigned int n;
846
858 #if CONFIG_LIBKLVANC
859 if (klvanc_context_create(&
ctx->vanc_ctx) < 0) {
862 }
863 ctx->supports_vanc = 1;
864 #endif
865
866 /* List available devices and exit. */
867 if (
ctx->list_devices) {
870 }
871
875
876 /* Get output device. */
877 if (
ctx->dl->QueryInterface(IID_IDeckLinkOutput, (
void **) &
ctx->dlo) != S_OK) {
882 }
883
884 /* List supported formats. */
885 if (
ctx->list_formats) {
889 }
890
891 /* Setup streams. */
908 } else {
911 }
912 }
913
914 /* Reconfigure the data/subtitle stream clocks to match the video */
918
922 }
924
929 }
930
931 return 0;
932
936 }
937
939 {
941
950
952 }
953
955 {
957 }
958
959 } /* extern "C" */