Commit d311f7e608 for ffmpeg
commit d311f7e608ba4905ea639ad140e88eba4f501ee3
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Tue Nov 17 17:54:43 2026 -0800
avcodec/amr_parser: emit complete blocks at input boundaries
Emit a frame-block as soon as its last channel frame is complete,
and count the whole block in the bitrate average.
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/libavcodec/amr_parser.c b/libavcodec/amr_parser.c
index 5ee1829c49..58029c70db 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;
@@ -101,19 +102,18 @@ static int amr_parse(AVCodecParserContext *s1,
if (s->remaining < 0)
next = offset;
- if (next != END_NOT_FOUND) {
- if (s->cumulated_size < UINT64_MAX - next) {
- s->cumulated_size += next;
- /* Both AMR formats have 50 frames per second */
- avctx->bit_rate = s->cumulated_size / ++s->block_count * 8 * 50;
- }
- }
-
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
+
+ if (next != END_NOT_FOUND &&
+ s->cumulated_size <= UINT64_MAX - buf_size) {
+ s->cumulated_size += buf_size;
+ /* Both AMR formats have 50 frame-blocks per second. */
+ avctx->bit_rate = s->cumulated_size / ++s->block_count * 8 * 50;
+ }
}
s1->duration = avctx->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
diff --git a/tests/api/Makefile b/tests/api/Makefile
index c411e82e5f..1c7a350273 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 0000000000..0c42c041cc
--- /dev/null
+++ b/tests/api/api-amr-parser-test.c
@@ -0,0 +1,103 @@
+/*
+ * 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 <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+
+static void parse(AVCodecParserContext *parser, AVCodecContext *ctx,
+ const char *name, const uint8_t *input, int input_size)
+{
+ 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);
+ printf("%s: input %d, consumed %d, output %d", name, input_size, ret, output_size);
+ if (output_size) {
+ printf(", duration %d, bitrate %" PRId64 ", ",
+ parser->duration, ctx->bit_rate);
+ for (int i = 0; i < output_size; i++)
+ printf("%02x", output[i]);
+ }
+ printf("\n");
+}
+
+static int test_parser(enum AVCodecID codec, int channels, int frame_size)
+{
+ 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;
+
+ printf("%s, channels %d\n", avcodec_get_name(codec), channels);
+ parse(parser, ctx, "exact", blocks, block_size);
+ parse(parser, ctx, "split start", blocks, block_size - 1);
+ parse(parser, ctx, "split end", blocks + block_size - 1, 1);
+ if (channels > 1) {
+ parse(parser, ctx, "first channel", blocks, frame_size);
+ parse(parser, ctx, "last channel", blocks + frame_size, frame_size);
+ }
+ parse(parser, ctx, "combined", blocks, 2 * block_size);
+ parse(parser, ctx, "remainder", blocks + block_size, block_size);
+ if (channels == 1)
+ parse(parser, ctx, "truncated block", blocks, block_size - 1);
+ parse(parser, ctx, "eof", NULL, 0);
+
+ /* New input after EOF needs a new parser. */
+ av_parser_close(parser);
+ parser = av_parser_init(codec);
+ if (!parser)
+ goto end;
+ parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
+ ctx->bit_rate = 0;
+ parse(parser, ctx, "complete frames", blocks, 2 * block_size);
+ parse(parser, ctx, "complete frames eof", NULL, 0);
+ ret = 0;
+
+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);
+ ret |= test_parser(AV_CODEC_ID_AMR_NB, 2, 13);
+ ret |= test_parser(AV_CODEC_ID_AMR_WB, 1, 18);
+ ret |= test_parser(AV_CODEC_ID_AMR_WB, 2, 18);
+ return ret;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1f3c1b43c7..7b5e00f331 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -3,6 +3,10 @@ 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_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)
diff --git a/tests/ref/fate/api-amr-parser b/tests/ref/fate/api-amr-parser
new file mode 100644
index 0000000000..da7748dab1
--- /dev/null
+++ b/tests/ref/fate/api-amr-parser
@@ -0,0 +1,42 @@
+amr_nb, channels 1
+exact: input 13, consumed 13, output 13, duration 160, bitrate 5200, 00010000000000000000000000
+split start: input 12, consumed 12, output 0
+split end: input 1, consumed 1, output 13, duration 160, bitrate 5200, 00010000000000000000000000
+combined: input 26, consumed 13, output 13, duration 160, bitrate 5200, 00010000000000000000000000
+remainder: input 13, consumed 13, output 13, duration 160, bitrate 5200, 00020000000000000000000000
+truncated block: input 12, consumed 12, output 0
+eof: input 0, consumed 0, output 12, duration 160, bitrate 5200, 000100000000000000000000
+complete frames: input 26, consumed 26, output 26, duration 160, bitrate 0, 0001000000000000000000000000020000000000000000000000
+complete frames eof: input 0, consumed 0, output 0
+amr_nb, channels 2
+exact: input 26, consumed 26, output 26, duration 160, bitrate 10400, 0001000000000000000000000000020000000000000000000000
+split start: input 25, consumed 25, output 0
+split end: input 1, consumed 1, output 26, duration 160, bitrate 10400, 0001000000000000000000000000020000000000000000000000
+first channel: input 13, consumed 13, output 0
+last channel: input 13, consumed 13, output 26, duration 160, bitrate 10400, 0001000000000000000000000000020000000000000000000000
+combined: input 52, consumed 26, output 26, duration 160, bitrate 10400, 0001000000000000000000000000020000000000000000000000
+remainder: input 26, consumed 26, output 26, duration 160, bitrate 10400, 0003000000000000000000000000040000000000000000000000
+eof: input 0, consumed 0, output 0
+complete frames: input 52, consumed 52, output 52, duration 160, bitrate 0, 00010000000000000000000000000200000000000000000000000003000000000000000000000000040000000000000000000000
+complete frames eof: input 0, consumed 0, output 0
+amr_wb, channels 1
+exact: input 18, consumed 18, output 18, duration 320, bitrate 7200, 000100000000000000000000000000000000
+split start: input 17, consumed 17, output 0
+split end: input 1, consumed 1, output 18, duration 320, bitrate 7200, 000100000000000000000000000000000000
+combined: input 36, consumed 18, output 18, duration 320, bitrate 7200, 000100000000000000000000000000000000
+remainder: input 18, consumed 18, output 18, duration 320, bitrate 7200, 000200000000000000000000000000000000
+truncated block: input 17, consumed 17, output 0
+eof: input 0, consumed 0, output 17, duration 320, bitrate 7200, 0001000000000000000000000000000000
+complete frames: input 36, consumed 36, output 36, duration 320, bitrate 0, 000100000000000000000000000000000000000200000000000000000000000000000000
+complete frames eof: input 0, consumed 0, output 0
+amr_wb, channels 2
+exact: input 36, consumed 36, output 36, duration 320, bitrate 14400, 000100000000000000000000000000000000000200000000000000000000000000000000
+split start: input 35, consumed 35, output 0
+split end: input 1, consumed 1, output 36, duration 320, bitrate 14400, 000100000000000000000000000000000000000200000000000000000000000000000000
+first channel: input 18, consumed 18, output 0
+last channel: input 18, consumed 18, output 36, duration 320, bitrate 14400, 000100000000000000000000000000000000000200000000000000000000000000000000
+combined: input 72, consumed 36, output 36, duration 320, bitrate 14400, 000100000000000000000000000000000000000200000000000000000000000000000000
+remainder: input 36, consumed 36, output 36, duration 320, bitrate 14400, 000300000000000000000000000000000000000400000000000000000000000000000000
+eof: input 0, consumed 0, output 0
+complete frames: input 72, consumed 72, output 72, duration 320, bitrate 0, 000100000000000000000000000000000000000200000000000000000000000000000000000300000000000000000000000000000000000400000000000000000000000000000000
+complete frames eof: input 0, consumed 0, output 0