From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 967D845BB0 for ; Fri, 24 Mar 2023 21:07:40 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4FE5368C82A; Fri, 24 Mar 2023 23:07:38 +0200 (EET) Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DA5A768C7E9 for ; Fri, 24 Mar 2023 23:07:31 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id CA2A4E89E7 for ; Fri, 24 Mar 2023 22:07:11 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JdOnn32Ln4lE for ; Fri, 24 Mar 2023 22:07:10 +0100 (CET) Received: from iq (iq [217.27.212.140]) by iq.passwd.hu (Postfix) with ESMTPS id DB2A6E82C0 for ; Fri, 24 Mar 2023 22:07:09 +0100 (CET) Date: Fri, 24 Mar 2023 22:07:09 +0100 (CET) From: Marton Balint To: FFmpeg development discussions and patches In-Reply-To: <20230317150232.17804-2-dheitmueller@ltnglobal.com> Message-ID: <44fc4e6f-6771-2879-129c-59ae407c358c@passwd.hu> References: <20230317150232.17804-1-dheitmueller@ltnglobal.com> <20230317150232.17804-2-dheitmueller@ltnglobal.com> MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH v2 2/2] decklink: Add support for compressed AC-3 output over SDI X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On Fri, 17 Mar 2023, Devin Heitmueller wrote: > Extend the decklink output to include support for compressed AC-3, > encapsulated using the SMPTE ST 377:2015 standard. > > This functionality can be exercised by using the "copy" codec when > the input audio stream is AC-3. For example: > > ./ffmpeg -i ~/foo.ts -codec:a copy -f decklink 'UltraStudio Mini Monitor' > > Note that the default behavior continues to be to do PCM output, > which means without specifying the copy codec a stream containing > AC-3 will be decoded and downmixed to stereo audio before output. > > Thanks to Marton Balint for providing feedback. > > Signed-off-by: Devin Heitmueller > --- > libavdevice/decklink_enc.cpp | 90 ++++++++++++++++++++++++++++++------ > 1 file changed, 75 insertions(+), 15 deletions(-) > > diff --git a/libavdevice/decklink_enc.cpp b/libavdevice/decklink_enc.cpp > index 8d423f6b6e..8d80f00247 100644 > --- a/libavdevice/decklink_enc.cpp > +++ b/libavdevice/decklink_enc.cpp > @@ -32,6 +32,7 @@ extern "C" { > > extern "C" { > #include "libavformat/avformat.h" > +#include "libavcodec/bytestream.h" > #include "libavutil/internal.h" > #include "libavutil/imgutils.h" > #include "avdevice.h" > @@ -243,19 +244,32 @@ static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st) > av_log(avctx, AV_LOG_ERROR, "Only one audio stream is supported!\n"); > return -1; > } > - if (c->sample_rate != 48000) { > - av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!" > - " Only 48kHz is supported.\n"); > - return -1; > - } > - if (c->ch_layout.nb_channels != 2 && c->ch_layout.nb_channels != 8 && c->ch_layout.nb_channels != 16) { > - av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!" > - " Only 2, 8 or 16 channels are supported.\n"); > + > + if (c->codec_id == AV_CODEC_ID_AC3) { > + /* Regardless of the number of channels in the codec, we're only > + using 2 SDI audio channels at 48000Hz */ > + ctx->channels = 2; > + } else if (c->codec_id == AV_CODEC_ID_PCM_S16LE) { > + if (c->sample_rate != 48000) { > + av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate!" > + " Only 48kHz is supported.\n"); > + return -1; > + } > + if (c->ch_layout.nb_channels != 2 && c->ch_layout.nb_channels != 8 && c->ch_layout.nb_channels != 16) { > + av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels!" > + " Only 2, 8 or 16 channels are supported.\n"); > + return -1; > + } > + ctx->channels = c->ch_layout.nb_channels; > + } else { > + av_log(avctx, AV_LOG_ERROR, "Unsupported codec specified!" > + " Only PCM_S16LE and AC-3 are supported.\n"); > return -1; > } > + > if (ctx->dlo->EnableAudioOutput(bmdAudioSampleRate48kHz, > bmdAudioSampleType16bitInteger, > - c->ch_layout.nb_channels, > + ctx->channels, > bmdAudioOutputStreamTimestamped) != S_OK) { > av_log(avctx, AV_LOG_ERROR, "Could not enable audio output!\n"); > return -1; > @@ -266,14 +280,41 @@ static int decklink_setup_audio(AVFormatContext *avctx, AVStream *st) > } > > /* The device expects the sample rate to be fixed. */ > - avpriv_set_pts_info(st, 64, 1, c->sample_rate); > - ctx->channels = c->ch_layout.nb_channels; > + avpriv_set_pts_info(st, 64, 1, 48000); > > ctx->audio = 1; > > return 0; > } > > +static int create_s337_payload(AVPacket *pkt, enum AVCodecID codec_id, uint8_t **outbuf, int *outsize) > +{ > + int payload_size = pkt->size + 8; > + uint16_t bitcount = pkt->size * 8; > + uint8_t *s337_payload; > + PutByteContext pb; > + int i; > + > + if (codec_id != AV_CODEC_ID_AC3) > + return AVERROR(EINVAL); Maybe some sanity check here for pkt->size upper limit to avoid overflows? > + > + /* Encapsulate AC3 syncframe into SMPTE 337 packet */ > + s337_payload = (uint8_t *) av_mallocz(payload_size); Why not simply av_malloc? > + if (s337_payload == NULL) > + return AVERROR(ENOMEM); > + bytestream2_init_writer(&pb, s337_payload, payload_size); > + bytestream2_put_le16u(&pb, 0xf872); /* Sync word 1 */ > + bytestream2_put_le16u(&pb, 0x4e1f); /* Sync word 1 */ > + bytestream2_put_le16u(&pb, 0x0001); /* Burst Info, including data type (1=ac3) */ > + bytestream2_put_le16u(&pb, bitcount); /* Length code */ > + for (i = 0; i < pkt->size; i += 2) for (int i = > + bytestream2_put_le16u(&pb, (pkt->data[i] << 8) | pkt->data[i+1]); > + > + *outsize = payload_size; > + *outbuf = s337_payload; > + return 0; > +} > + > av_cold int ff_decklink_write_trailer(AVFormatContext *avctx) > { > struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; > @@ -531,21 +572,40 @@ static int decklink_write_audio_packet(AVFormatContext *avctx, AVPacket *pkt) > { > struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; > struct decklink_ctx *ctx = (struct decklink_ctx *)cctx->ctx; > - int sample_count = pkt->size / (ctx->channels << 1); > + AVStream *st = avctx->streams[pkt->stream_index]; > + int sample_count; > uint32_t buffered; > + uint8_t *outbuf = NULL; > + int ret = 0; > > ctx->dlo->GetBufferedAudioSampleFrameCount(&buffered); > if (pkt->pts > 1 && !buffered) > av_log(avctx, AV_LOG_WARNING, "There's no buffered audio." > " Audio will misbehave!\n"); > > - if (ctx->dlo->ScheduleAudioSamples(pkt->data, sample_count, pkt->pts, > + if (st->codecpar->codec_id == AV_CODEC_ID_AC3) { > + /* Encapsulate AC3 syncframe into SMPTE 337 packet */ > + int outbuf_size; > + ret = create_s337_payload(pkt, st->codecpar->codec_id, > + &outbuf, &outbuf_size); > + if (ret) if (ret < 0) is preferred > + return ret; > + sample_count = outbuf_size / 4; > + } else { > + sample_count = pkt->size / (ctx->channels << 1); > + outbuf = pkt->data; > + } > + > + if (ctx->dlo->ScheduleAudioSamples(outbuf, sample_count, pkt->pts, > bmdAudioSampleRate48kHz, NULL) != S_OK) { > av_log(avctx, AV_LOG_ERROR, "Could not schedule audio samples.\n"); > - return AVERROR(EIO); > + ret = AVERROR(EIO); > } > > - return 0; > + if (st->codecpar->codec_id == AV_CODEC_ID_AC3) > + av_freep(&outbuf); > + > + return ret; > } > Thanks, Marton _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".