+#if CONFIG_LIBKLVANC
+static void construct_cc(AVFormatContext *avctx, struct decklink_ctx *ctx,
+ AVPacket *pkt, struct klvanc_line_set_s *vanc_lines)
+{
+ struct klvanc_packet_eia_708b_s *cdp;
+ uint16_t *cdp_words;
+ uint16_t len;
+ uint8_t cc_count;
+ buffer_size_t size;
+ int ret, i;
+
+ const uint8_t *data = av_packet_get_side_data(pkt, AV_PKT_DATA_A53_CC, &size);
+ if (!data)
+ return;
+
+ cc_count = size / 3;
+
+ ret = klvanc_create_eia708_cdp(&cdp);
+ if (ret)
+ return;
+
+ ret = klvanc_set_framerate_EIA_708B(cdp, ctx->bmd_tb_num, ctx->bmd_tb_den);
+ if (ret) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid framerate specified: %lld/%lld\n",
+ ctx->bmd_tb_num, ctx->bmd_tb_den);
+ klvanc_destroy_eia708_cdp(cdp);
+ return;
+ }
+
+ if (cc_count > KLVANC_MAX_CC_COUNT) {
+ av_log(avctx, AV_LOG_ERROR, "Illegal cc_count received: %d\n", cc_count);
+ cc_count = KLVANC_MAX_CC_COUNT;
+ }
+
+ /* CC data */
+ cdp->header.ccdata_present = 1;
+ cdp->header.caption_service_active = 1;
+ cdp->ccdata.cc_count = cc_count;
+ for (i = 0; i < cc_count; i++) {
+ if (data [3*i] & 0x04)
+ cdp->ccdata.cc[i].cc_valid = 1;
+ cdp->ccdata.cc[i].cc_type = data[3*i] & 0x03;
+ cdp->ccdata.cc[i].cc_data[0] = data[3*i+1];
+ cdp->ccdata.cc[i].cc_data[1] = data[3*i+2];
+ }
+
+ klvanc_finalize_EIA_708B(cdp, ctx->cdp_sequence_num++);
+ ret = klvanc_convert_EIA_708B_to_words(cdp, &cdp_words, &len);
+ klvanc_destroy_eia708_cdp(cdp);
+ if (ret != 0) {
+ av_log(avctx, AV_LOG_ERROR, "Failed converting 708 packet to words\n");
+ return;
+ }
+
+ ret = klvanc_line_insert(ctx->vanc_ctx, vanc_lines, cdp_words, len, 11, 0);
+ free(cdp_words);
+ if (ret != 0) {
+ av_log(avctx, AV_LOG_ERROR, "VANC line insertion failed\n");
+ return;
+ }
+}
+
+static int decklink_construct_vanc(AVFormatContext *avctx, struct decklink_ctx *ctx,
+ AVPacket *pkt, decklink_frame *frame)
+{
+ struct klvanc_line_set_s vanc_lines = { 0 };
+ int ret = 0, i;
+
+ if (!ctx->supports_vanc)
+ return 0;
+
+ construct_cc(avctx, ctx, pkt, &vanc_lines);
+
+ IDeckLinkVideoFrameAncillary *vanc;
+ int result = ctx->dlo->CreateAncillaryData(bmdFormat10BitYUV, &vanc);
+ if (result != S_OK) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to create vanc\n");
+ ret = AVERROR(EIO);
+ goto done;
+ }
+
+ /* Now that we've got all the VANC lines in a nice orderly manner, generate the
+ final VANC sections for the Decklink output */
+ for (i = 0; i < vanc_lines.num_lines; i++) {
+ struct klvanc_line_s *line = vanc_lines.lines[i];
+ int real_line;
+ void *buf;
+
+ if (!line)
+ break;
+
+ /* FIXME: include hack for certain Decklink cards which mis-represent
+ line numbers for pSF frames */
+ real_line = line->line_number;
+
+ result = vanc->GetBufferForVerticalBlankingLine(real_line, &buf);
+ if (result != S_OK) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get VANC line %d: %d", real_line, result);
+ continue;
+ }
+
+ /* Generate the full line taking into account all VANC packets on that line */
+ result = klvanc_generate_vanc_line_v210(ctx->vanc_ctx, line, (uint8_t *) buf,
+ ctx->bmd_width);
+ if (result) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to generate VANC line\n");
+ continue;
+ }
+ }
+
+ result = frame->SetAncillaryData(vanc);
+ vanc->Release();
+ if (result != S_OK) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to set vanc: %d", result);
+ ret = AVERROR(EIO);
+ }
+
+done:
+ for (i = 0; i < vanc_lines.num_lines; i++)
+ klvanc_line_free(vanc_lines.lines[i]);
+
+ return ret;
+}
+#endif
+