PR #26163 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26163
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26163.patch

An AMR storage frame-block contains one speech frame per channel. Emit the 
block as soon as the final channel frame is complete, including when it ends 
exactly at the end of the input buffer. Keep buffering when a speech frame is 
partial or a channel frame is still missing.

The new API regression test covers AMR-NB and AMR-WB, including exact, split, 
and combined input, byte consumption, payload, duration, EOF, and 
complete-frame passthrough. It fails before the fix and passes with it.

All 33 targeted AMR FATE tests passed. Packet counts, sizes, payload hashes, 
timestamps, and durations matched before and after the fix across 8,740 packets 
from 25 inputs.



>From 0aece4acf5bf8e67feb7c298dae42b790503e161 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 17 Nov 2026 17:54:43 -0800
Subject: [PATCH] avcodec/amr_parser: emit complete blocks at input boundaries

An AMR storage frame-block contains one speech frame per channel. Emit the 
block as soon as its final channel frame is complete, including when it ends at 
the end of the input buffer. Continue buffering when a speech frame is partial 
or a channel frame is still missing.

Test consumption, payload and duration for AMR-NB and AMR-WB, including split 
and combined input, EOF and complete-frame passthrough.
---
 libavcodec/amr_parser.c         |   3 +-
 tests/api/Makefile              |   1 +
 tests/api/api-amr-parser-test.c | 104 ++++++++++++++++++++++++++++++++
 tests/fate/api.mak              |   5 ++
 4 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 tests/api/api-amr-parser-test.c

diff --git a/libavcodec/amr_parser.c b/libavcodec/amr_parser.c
index 5ee1829c497..804fa30bbe7 100644
--- a/libavcodec/amr_parser.c
+++ b/libavcodec/amr_parser.c
@@ -88,7 +88,8 @@ static int amr_parse(AVCodecParserContext *s1,
             }
 
             offset += next;
-            if (offset >= buf_size) {
+            if (offset > buf_size ||
+                (offset == buf_size && ch + 1 < avctx->ch_layout.nb_channels)) 
{
                 s->remaining = offset - buf_size;
                 next = END_NOT_FOUND;
                 break;
diff --git a/tests/api/Makefile b/tests/api/Makefile
index c411e82e5f1..1c7a3502736 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -1,4 +1,5 @@
 APITESTPROGS-$(call ALLYES, AC3_ENCODER AC3_DECODER EAC3_DECODER 
AC3_FIXED_DECODER) += api-ac3-flush
+APITESTPROGS-$(CONFIG_AMR_PARSER) += api-amr-parser
 APITESTPROGS-$(call ALLYES, LIBASTCENC_ENCODER LIBASTCENC_DECODER) += 
api-astc-profile
 APITESTPROGS-$(call ENCDEC, FLAC, FLAC) += api-flac
 APITESTPROGS-$(call DEMDEC, H264, H264) += api-h264
diff --git a/tests/api/api-amr-parser-test.c b/tests/api/api-amr-parser-test.c
new file mode 100644
index 00000000000..4389f162700
--- /dev/null
+++ b/tests/api/api-amr-parser-test.c
@@ -0,0 +1,104 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+
+static int check_parse(AVCodecParserContext *parser, AVCodecContext *ctx,
+                       const uint8_t *input, int input_size, int consumed,
+                       const uint8_t *expected, int expected_size, int 
duration)
+{
+    uint8_t padded[2 * 2 * 18 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
+    uint8_t *output;
+    int output_size;
+    int ret;
+
+    if (input_size)
+        memcpy(padded, input, input_size);
+    ret = av_parser_parse2(parser, ctx, &output, &output_size,
+                           input_size ? padded : NULL, input_size,
+                           AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
+    if (ret != consumed || output_size != expected_size ||
+        (expected_size && (memcmp(output, expected, expected_size) ||
+                           parser->duration != duration))) {
+        fprintf(stderr, "%s (%d channels), input %d: consumed %d/%d, output 
%d/%d, duration %d/%d\n",
+                avcodec_get_name(ctx->codec_id), ctx->ch_layout.nb_channels,
+                input_size, ret, consumed, output_size, expected_size,
+                parser->duration, duration);
+        return 1;
+    }
+    return 0;
+}
+
+static int test_parser(enum AVCodecID codec, int channels, int frame_size,
+                       int duration)
+{
+    AVCodecParserContext *parser = av_parser_init(codec);
+    AVCodecContext *ctx = avcodec_alloc_context3(NULL);
+    uint8_t blocks[2 * 2 * 18] = { 0 };
+    const int block_size = channels * frame_size;
+    int ret = 1;
+
+    if (!parser || !ctx)
+        goto end;
+    ctx->codec_id = codec;
+    av_channel_layout_default(&ctx->ch_layout, channels);
+
+    /* Mode 0 headers, with distinct payloads in each speech frame. */
+    for (int i = 0; i < 2 * channels; i++)
+        blocks[i * frame_size + 1] = i + 1;
+
+    ret = check_parse(parser, ctx, blocks, block_size, block_size,
+                      blocks, block_size, duration);
+    ret |= check_parse(parser, ctx, blocks, block_size - 1, block_size - 1,
+                       NULL, 0, duration);
+    ret |= check_parse(parser, ctx, blocks + block_size - 1, 1, 1,
+                       blocks, block_size, duration);
+    if (channels > 1) {
+        ret |= check_parse(parser, ctx, blocks, frame_size, frame_size,
+                           NULL, 0, duration);
+        ret |= check_parse(parser, ctx, blocks + frame_size, frame_size, 
frame_size,
+                           blocks, block_size, duration);
+    }
+    ret |= check_parse(parser, ctx, blocks, 2 * block_size, block_size,
+                       blocks, block_size, duration);
+    ret |= check_parse(parser, ctx, blocks + block_size, block_size, 
block_size,
+                       blocks + block_size, block_size, duration);
+    ret |= check_parse(parser, ctx, NULL, 0, 0, NULL, 0, duration);
+
+    parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
+    ret |= check_parse(parser, ctx, blocks, 2 * block_size, 2 * block_size,
+                       blocks, 2 * block_size, duration);
+    ret |= check_parse(parser, ctx, NULL, 0, 0, NULL, 0, duration);
+
+end:
+    av_parser_close(parser);
+    avcodec_free_context(&ctx);
+    return ret;
+}
+
+int main(void)
+{
+    int ret = test_parser(AV_CODEC_ID_AMR_NB, 1, 13, 160);
+    ret |= test_parser(AV_CODEC_ID_AMR_NB, 2, 13, 160);
+    ret |= test_parser(AV_CODEC_ID_AMR_WB, 1, 18, 320);
+    ret |= test_parser(AV_CODEC_ID_AMR_WB, 2, 18, 320);
+    return ret;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1659c7979c0..f992abec5b3 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -3,6 +3,11 @@ fate-api-ac3-flush: $(APITESTSDIR)/api-ac3-flush-test$(EXESUF)
 fate-api-ac3-flush: CMD = run $(APITESTSDIR)/api-ac3-flush-test$(EXESUF)
 fate-api-ac3-flush: CMP = null
 
+FATE_API_LIBAVCODEC-$(CONFIG_AMR_PARSER) += fate-api-amr-parser
+fate-api-amr-parser: $(APITESTSDIR)/api-amr-parser-test$(EXESUF)
+fate-api-amr-parser: CMD = run $(APITESTSDIR)/api-amr-parser-test$(EXESUF)
+fate-api-amr-parser: CMP = null
+
 FATE_API_LIBAVCODEC-$(call ALLYES, LIBASTCENC_ENCODER LIBASTCENC_DECODER) += 
fate-api-astc-profile
 fate-api-astc-profile: $(APITESTSDIR)/api-astc-profile-test$(EXESUF)
 fate-api-astc-profile: CMD = run $(APITESTSDIR)/api-astc-profile-test$(EXESUF)
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to