Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/liblc3dec: support sample format negotiation and planar layout. (PR #20723)
@ 2025-10-20  6:58 cenzhanquan1 via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: cenzhanquan1 via ffmpeg-devel @ 2025-10-20  6:58 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: cenzhanquan1

PR #20723 opened by cenzhanquan1
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20723
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20723.patch

1. Adds support for respecting the requested sample format. Previously,
   the decoder always used AV_SAMPLE_FMT_FLTP. Now it checks if the
   caller requested a specific format via avctx->request_sample_fmt and
   honors that request when supported.

2. Improves planar/interleaved audio buffer handling. The decoding
   logic now properly handles both planar and interleaved sample
   formats by calculating the correct stride and buffer pointers based
   on the actual sample format.

The changes include:
- Added format mapping between AVSampleFormat and lc3_pcm_format
- Implemented format selection logic in initialization.
- Updated buffer pointer calculation for planar/interleaved data.
- Maintained backward compatibility with existing behavior.

Signed-off-by: cenzhanquan1 <cenzhanquan1@xiaomi.com>


>From 34082d39cec5df8220ea81400240defe8da241bb Mon Sep 17 00:00:00 2001
From: cenzhanquan1 <cenzhanquan1@xiaomi.com>
Date: Mon, 20 Oct 2025 14:52:24 +0800
Subject: [PATCH] avcodec/liblc3dec: support sample format negotiation and
 planar layout.

1. Adds support for respecting the requested sample format. Previously,
   the decoder always used AV_SAMPLE_FMT_FLTP. Now it checks if the
   caller requested a specific format via avctx->request_sample_fmt and
   honors that request when supported.

2. Improves planar/interleaved audio buffer handling. The decoding
   logic now properly handles both planar and interleaved sample
   formats by calculating the correct stride and buffer pointers based
   on the actual sample format.

The changes include:
- Added format mapping between AVSampleFormat and lc3_pcm_format
- Implemented format selection logic in initialization.
- Updated buffer pointer calculation for planar/interleaved data.
- Maintained backward compatibility with existing behavior.

Signed-off-by: cenzhanquan1 <cenzhanquan1@xiaomi.com>
---
 libavcodec/liblc3dec.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/libavcodec/liblc3dec.c b/libavcodec/liblc3dec.c
index d250ace38a..023559d67d 100644
--- a/libavcodec/liblc3dec.c
+++ b/libavcodec/liblc3dec.c
@@ -43,6 +43,14 @@ static av_cold int liblc3_decode_init(AVCodecContext *avctx)
     int ep_mode;
     unsigned decoder_size;
 
+    static const struct {
+        enum AVSampleFormat av_format;
+        enum lc3_pcm_format lc3_format;
+    } format_map[] = {
+        { AV_SAMPLE_FMT_FLT,  LC3_PCM_FORMAT_FLOAT },
+        { AV_SAMPLE_FMT_FLTP, LC3_PCM_FORMAT_FLOAT },
+    };
+
     if (avctx->extradata_size < 6)
         return AVERROR_INVALIDDATA;
     if (channels < 0 || channels > DECODER_MAX_CHANNELS) {
@@ -83,6 +91,15 @@ static av_cold int liblc3_decode_init(AVCodecContext *avctx)
     }
 
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
+    if (avctx->request_sample_fmt != AV_SAMPLE_FMT_NONE) {
+        for (int i = 0; i < FF_ARRAY_ELEMS(format_map); i++) {
+            if (format_map[i].av_format == avctx->request_sample_fmt) {
+                avctx->sample_fmt = avctx->request_sample_fmt;
+                break;
+            }
+        }
+    }
+
     avctx->delay = lc3_hr_delay_samples(
         liblc3->hr_mode, liblc3->frame_us, liblc3->srate_hz);
     avctx->internal->skip_samples = avctx->delay;
@@ -106,6 +123,8 @@ static int liblc3_decode(AVCodecContext *avctx, AVFrame *frame,
     int channels = avctx->ch_layout.nb_channels;
     uint8_t *in = avpkt->data;
     int block_bytes, ret;
+    size_t sample_size;
+    int is_planar;
 
     frame->nb_samples = av_rescale(
         liblc3->frame_us, liblc3->srate_hz, 1000*1000);
@@ -113,11 +132,17 @@ static int liblc3_decode(AVCodecContext *avctx, AVFrame *frame,
         return ret;
 
     block_bytes = avpkt->size;
+    is_planar = av_sample_fmt_is_planar(avctx->sample_fmt);
+    sample_size = av_get_bytes_per_sample(avctx->sample_fmt);
+
     for (int ch = 0; ch < channels; ch++) {
         int nbytes = block_bytes / channels + (ch < block_bytes % channels);
+        void *pcm_data = is_planar ? frame->extended_data[ch] :
+                                (uint8_t *)frame->extended_data[0] + ch * sample_size;
+        int stride = is_planar ? 1 : channels;
 
         ret = lc3_decode(liblc3->decoder[ch], in, nbytes,
-                         LC3_PCM_FORMAT_FLOAT, frame->data[ch], 1);
+                         LC3_PCM_FORMAT_FLOAT, pcm_data, stride);
         if (ret < 0)
             return AVERROR_INVALIDDATA;
 
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-10-20  6:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-20  6:58 [FFmpeg-devel] [PATCH] avcodec/liblc3dec: support sample format negotiation and planar layout. (PR #20723) cenzhanquan1 via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git