[FFmpeg-devel] [PATCH 2/5] avcodec/liblc3: Add encoding/decoding support of LC3 audio codec
Stefano Sabatini
stefasab at gmail.com
Wed Mar 27 13:49:29 EET 2024
On date Tuesday 2024-03-26 23:07:54 +0000, ffmpeg-devel Mailing List wrote:
> The LC3 audio codec is the default codec of Bluetooth LE audio.
> This is a wrapper over the liblc3 library (https://github.com/google/liblc3).
>
> Signed-off-by: Antoine Soulier <asoulier at google.com>
> ---
> libavcodec/Makefile | 2 +
> libavcodec/allcodecs.c | 2 +
> libavcodec/codec_desc.c | 7 ++
> libavcodec/codec_id.h | 1 +
> libavcodec/liblc3dec.c | 138 +++++++++++++++++++++++++++++
> libavcodec/liblc3enc.c | 192 ++++++++++++++++++++++++++++++++++++++++
> 6 files changed, 342 insertions(+)
> create mode 100644 libavcodec/liblc3dec.c
> create mode 100644 libavcodec/liblc3enc.c
[...]
> +static av_cold int liblc3_encode_init(AVCodecContext *avctx)
> +{
> + LibLC3EncContext *liblc3 = avctx->priv_data;
> + bool hr_mode = liblc3->opts.hr_mode;
> + int frame_us = liblc3->opts.frame_duration * 1000;
> + int srate_hz = avctx->sample_rate;
> + int channels = avctx->ch_layout.nb_channels;
> + int effective_bit_rate;
> + unsigned encoder_size;
> +
> + if (frame_us != 2500 && frame_us != 5000 &&
> + frame_us != 7500 && frame_us != 10000 ) {
> + av_log(avctx, AV_LOG_ERROR,
> + "Unsupported frame duration %.1f ms\n", frame_us / 1e3f);
> + return AVERROR(EINVAL);
> + }
> +
> + hr_mode |= srate_hz > 48000;
> + hr_mode &= srate_hz >= 48000;
> +
> + if (frame_us == 7500 && hr_mode) {
> + av_log(avctx, AV_LOG_ERROR,
> + "High-resolution mode not supported with 7.5 ms frames\n");
> + return AVERROR(EINVAL);
> + }
> +
> + av_log(avctx, AV_LOG_INFO, "Encoding %.1f ms frames\n", frame_us / 1e3f);
readability: use 1000f since it's more explicit
> + if (hr_mode)
> + av_log(avctx, AV_LOG_INFO, "High-resolution mode enabled\n");
> +
> + liblc3->block_bytes = lc3_hr_frame_block_bytes(
> + hr_mode, frame_us, srate_hz, channels, avctx->bit_rate);
> +
> + effective_bit_rate = lc3_hr_resolve_bitrate(
> + hr_mode, frame_us, srate_hz, liblc3->block_bytes);
> +
> + if (avctx->bit_rate != effective_bit_rate)
> + av_log(avctx, AV_LOG_WARNING,
> + "Bitrate changed to %d bps\n", effective_bit_rate);
> + avctx->bit_rate = effective_bit_rate;
> +
> + encoder_size = lc3_hr_encoder_size(hr_mode, frame_us, srate_hz);
> + if (!encoder_size)
> + return AVERROR(EINVAL);
> +
> + liblc3->encoder_mem = av_malloc_array(channels, encoder_size);
> + if (!liblc3->encoder_mem)
> + return AVERROR(ENOMEM);
> +
> + for (int ch = 0; ch < channels; ch++) {
> + liblc3->encoder[ch] = lc3_hr_setup_encoder(
> + hr_mode, frame_us, srate_hz, 0,
> + (char *)liblc3->encoder_mem + ch * encoder_size);
> + if (!liblc3->encoder[ch])
> + return AVERROR(EINVAL);
do you need to reset/free the encoder in case of failure? If this is
the case you need to unset/free here or in the close function
[...]
> +static int liblc3_encode(AVCodecContext *avctx, AVPacket *avpkt,
> + const AVFrame *av_frame, int *got_packet_ptr)
> +{
> + LibLC3EncContext *liblc3 = avctx->priv_data;
> + int block_bytes = liblc3->block_bytes;
> + int channels = avctx->ch_layout.nb_channels;
> + uint8_t *data_ptr;
> + int ret;
> +
> + if ((ret = ff_alloc_packet(avctx, avpkt, block_bytes)) < 0)
> + return ret;
> +
> + data_ptr = avpkt->data;
> + for (int ch = 0; ch < channels; ch++) {
> + int frame_bytes = block_bytes / channels
> + + (ch < block_bytes % channels);
> +
> + lc3_encode(liblc3->encoder[ch],
> + LC3_PCM_FORMAT_FLOAT, av_frame->data[ch], 1,
> + frame_bytes, data_ptr);
can this fail?
[...]
More information about the ffmpeg-devel
mailing list