From patchwork Tue Jan 9 01:16:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Devin Heitmueller X-Patchwork-Id: 7222 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp3317729jaa; Mon, 8 Jan 2018 17:18:06 -0800 (PST) X-Google-Smtp-Source: ACJfBouEBEmUgU+UJkO/8Cd/4g3EOqy65Itfv4uFfRH771xui8XCWU/u/W07y9nGOnHf53RK9kiJ X-Received: by 10.28.45.83 with SMTP id t80mr10194650wmt.90.1515460685984; Mon, 08 Jan 2018 17:18:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515460685; cv=none; d=google.com; s=arc-20160816; b=AReT0zr1BoX8jg2NIpKKpeD48E/UnXLQQaqPLMIo32TBfi7j7YGSwBewcbVRP6PHe1 3pxfyC6CHNuofOf9EunluWgz47A1IFlc1e/wL9vfT0aqSZiNwxJI87kuTwMFCLNeMkY6 nonjY2oTqfs8OvTLeAc9KYfs8LfaAmLdgzVpL0wUaCpVhfnhuCjXLkngmeTUkq00e2c2 gUoGa13zDVWdfWV5HrKrBAy19vwMKBo3M/CO0aobkK5zwY7uSQi7bjSQ+RM96DFCwjDw qS3xO0cBydOlRCtxD8hLyLHXj7euAowWrT84OXvIR8CWU1Z8yETWumSP8Qc1GeKq4Ajg nY0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to:arc-authentication-results; bh=MxniU+M1tpPTAsS37xrP0h+XWakq4+++uGmcwcuyG0A=; b=f0DcokJEjE4JNTOdjFEnDpIqiztSiL4TLvtTo4NYWAmf7TjqP5GG8+nQywfcdtOqIp HjqUYytptXfuZH4SMnxMsLFfQ3VZNCf4KycGZCWlqsicqzoxZVE8VDSQb42uDM2U4AjJ PL1fJCHht59UmeZYGGetwJFHB4IkhY/ZiByyMfyfMMrcgz/dPICTh7vEnxO1htamy1LP vmE6vi1XHgu9HIJefn7zfBw1RuqD1lWyca016Ex83teIQF0rq+5ySW6gy3QsdM4bKO6h 3yNR5x2iUfmDfmR97/1u4vwdz86e5TCk+HMSWBplI7rgiL6eTnXkWnia2tNr0nw8H+Sx psEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w15si297958wmf.70.2018.01.08.17.18.05; Mon, 08 Jan 2018 17:18:05 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5072368A08A; Tue, 9 Jan 2018 03:17:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from was-smtp1.livetimenet.net (50-206-97-56-static.hfc.comcastbusiness.net [50.206.97.56]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2C926689F22 for ; Tue, 9 Jan 2018 03:17:26 +0200 (EET) Received: by was-smtp1.livetimenet.net with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.84_2) (envelope-from ) id 1eYiXc-0008Fd-55; Mon, 08 Jan 2018 20:17:25 -0500 From: Devin Heitmueller To: ffmpeg-devel@ffmpeg.org Date: Mon, 8 Jan 2018 20:16:53 -0500 Message-Id: <20180109011658.72370-7-dheitmueller@ltnglobal.com> X-Mailer: git-send-email 2.13.2 In-Reply-To: <20180109011658.72370-1-dheitmueller@ltnglobal.com> References: <20180109011658.72370-1-dheitmueller@ltnglobal.com> MIME-Version: 1.0 X-Spam-Score: -1.9 (-) Subject: [FFmpeg-devel] [PATCH 06/11] decklink: Add support for using libklvanc from within capture module X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: jgreen@ltnglobal.com, Devin Heitmueller Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Make use of libklvanc from within the DeckLink capture module, initially for EIA-708 and AFD. Support for other VANC types will come in subsequent patches. Incorporates feedback from Derek Buitenhuis , James Almer , and Aaron Levinson Signed-off-by: Devin Heitmueller --- libavdevice/decklink_dec.cpp | 136 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp index 6c1ff82..bab3588 100644 --- a/libavdevice/decklink_dec.cpp +++ b/libavdevice/decklink_dec.cpp @@ -3,6 +3,7 @@ * Copyright (c) 2013-2014 Luca Barbato, Deti Fliegl * Copyright (c) 2014 Rafaël Carré * Copyright (c) 2017 Akamai Technologies, Inc. + * Copyright (c) 2017 LTN Global Communications, Inc. * * This file is part of FFmpeg. * @@ -673,10 +674,123 @@ error: return ret; } +#if CONFIG_LIBKLVANC +/* VANC Callbacks */ +struct vanc_cb_ctx { + AVFormatContext *avctx; + AVPacket *pkt; +}; +static int cb_AFD(void *callback_context, struct klvanc_context_s *ctx, + struct klvanc_packet_afd_s *pkt) +{ + struct vanc_cb_ctx *cb_ctx = (struct vanc_cb_ctx *)callback_context; + uint8_t *afd; + + afd = av_packet_new_side_data(cb_ctx->pkt, AV_PKT_DATA_AFD, 1); + if (afd == NULL) { + return AVERROR(ENOMEM); + } + afd[0] = pkt->hdr.payload[0] >> 3; + + return 0; +} + +static int cb_EIA_708B(void *callback_context, struct klvanc_context_s *ctx, + struct klvanc_packet_eia_708b_s *pkt) +{ + struct vanc_cb_ctx *cb_ctx = (struct vanc_cb_ctx *)callback_context; + decklink_cctx *cctx = (struct decklink_cctx *)cb_ctx->avctx->priv_data; + struct decklink_ctx *decklink_ctx = (struct decklink_ctx *)cctx->ctx; + uint16_t expected_cdp; + uint8_t *cc; + + if (!pkt->checksum_valid) + return 0; + + if (!pkt->header.ccdata_present) + return 0; + + expected_cdp = decklink_ctx->cdp_sequence_num + 1; + decklink_ctx->cdp_sequence_num = pkt->header.cdp_hdr_sequence_cntr; + if (pkt->header.cdp_hdr_sequence_cntr != expected_cdp) { + av_log(cb_ctx->avctx, AV_LOG_DEBUG, + "CDP counter inconsistent. Received=0x%04x Expected=%04x\n", + pkt->header.cdp_hdr_sequence_cntr, expected_cdp); + return 0; + } + + cc = av_packet_new_side_data(cb_ctx->pkt, AV_PKT_DATA_A53_CC, pkt->ccdata.cc_count * 3); + if (cc == NULL) + return AVERROR(ENOMEM); + + for (int i = 0; i < pkt->ccdata.cc_count; i++) { + cc[3*i] = 0xf8 | (pkt->ccdata.cc[i].cc_valid ? 0x04 : 0x00) | + (pkt->ccdata.cc[i].cc_type & 0x03); + cc[3*i+1] = pkt->ccdata.cc[i].cc_data[0]; + cc[3*i+2] = pkt->ccdata.cc[i].cc_data[1]; + } + + return 0; +} + +static struct klvanc_callbacks_s callbacks = +{ + cb_AFD, + cb_EIA_708B, + NULL, + NULL, + NULL, + NULL, +}; +/* End: VANC Callbacks */ + +/* Take one line of V210 from VANC, colorspace convert and feed it to the + * VANC parser. We'll expect our VANC message callbacks to happen on this + * same calling thread. + */ +static int klvanc_handle_line(AVFormatContext *avctx, struct klvanc_context_s *vanc_ctx, + unsigned char *buf, unsigned int uiWidth, unsigned int lineNr, + AVPacket *pkt) +{ + /* Convert the vanc line from V210 to CrCB422, then vanc parse it */ + + /* We need two kinds of type pointers into the source vbi buffer */ + /* TODO: What the hell is this, two ptrs? */ + const uint32_t *src = (const uint32_t *)buf; + + /* Convert Blackmagic pixel format to nv20. + * src pointer gets mangled during conversion, hence we need its own + * ptr instead of passing vbiBufferPtr. + * decoded_words should be atleast 2 * uiWidth. + */ + uint16_t decoded_words[16384]; + + /* On output each pixel will be decomposed into three 16-bit words (one for Y, U, V) */ + memset(&decoded_words[0], 0, sizeof(decoded_words)); + uint16_t *p_anc = decoded_words; + if (klvanc_v210_line_to_nv20_c(src, p_anc, sizeof(decoded_words), (uiWidth / 6) * 6) < 0) + return AVERROR(EINVAL); + + if (vanc_ctx) { + struct vanc_cb_ctx cb_ctx = { + .avctx = avctx, + .pkt = pkt + }; + vanc_ctx->callback_context = &cb_ctx; + int ret = klvanc_packet_parse(vanc_ctx, lineNr, decoded_words, sizeof(decoded_words) / (sizeof(uint16_t))); + if (ret < 0) { + return AVERROR(EINVAL); + } + } + return 0; +} +#endif + HRESULT decklink_input_callback::VideoInputFrameArrived( IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame) { decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; + struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx; void *frameBytes; void *audioFrameBytes; BMDTimeValue frameTime; @@ -787,10 +901,17 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( for (i = vanc_line_numbers[idx].vanc_start; i <= vanc_line_numbers[idx].vanc_end; i++) { uint8_t *buf; if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) == S_OK) { +#if CONFIG_LIBKLVANC + int ret = klvanc_handle_line(avctx, ctx->vanc_ctx, + buf, videoFrame->GetWidth(), i, &pkt); + if (ret != 0) + av_log(avctx, AV_LOG_ERROR, "Error parsing VANC for line %d\n", i); +#else uint16_t luma_vanc[MAX_WIDTH_VANC]; extract_luma_from_v210(luma_vanc, buf, videoFrame->GetWidth()); txt_buf = get_metadata(avctx, luma_vanc, videoFrame->GetWidth(), txt_buf, sizeof(txt_buf0) - (txt_buf - txt_buf0), &pkt); +#endif } if (i == vanc_line_numbers[idx].field0_vanc_end) i = vanc_line_numbers[idx].field1_vanc_start - 1; @@ -952,6 +1073,9 @@ av_cold int ff_decklink_read_close(AVFormatContext *avctx) ff_decklink_cleanup(avctx); avpacket_queue_end(&ctx->queue); +#if CONFIG_LIBKLVANC + klvanc_context_destroy(ctx->vanc_ctx); +#endif av_freep(&cctx->ctx); @@ -1195,6 +1319,18 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) avpacket_queue_init (avctx, &ctx->queue); +#if CONFIG_LIBKLVANC + if (klvanc_context_create(&ctx->vanc_ctx) < 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot create VANC library context\n"); + ret = AVERROR(ENOMEM); + goto error; + } else { + ctx->vanc_ctx->verbose = 0; + ctx->vanc_ctx->callbacks = &callbacks; + ctx->vanc_ctx->log_cb = NULL; + } +#endif + if (ctx->dli->StartStreams() != S_OK) { av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n"); ret = AVERROR(EIO);