Commit b1c81f96f1 for ffmpeg
commit b1c81f96f1bf13c1af09b5897aebd843d4f21121
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Sat Nov 21 16:34:05 2026 -0800
avformat/movenc: fix AMR-WB in MOV and 3GP
Write the 0x83ff mode set (3GPP TS 26.244 6.7), the reserved channel
count of 2 in 3GP (table 6.4) and 160/320 samples per packet in MOV.
Require mono and one frame per packet; share the config writer and
frame size table.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Fairy
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavformat/amr.c b/libavformat/amr.c
index e825121af3..a96edfece4 100644
--- a/libavformat/amr.c
+++ b/libavformat/amr.c
@@ -28,6 +28,7 @@ Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.tx
#include "libavutil/attributes_internal.h"
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
+#include "amr.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
@@ -45,13 +46,6 @@ static attribute_nonstring const uint8_t AMRMC_header[12] = "#!AMR_MC1.0\x0a";
static attribute_nonstring const uint8_t AMRWB_header[9] = "#!AMR-WB\x0a";
static attribute_nonstring const uint8_t AMRWBMC_header[15] = "#!AMR-WB_MC1.0\x0a";
-static const uint8_t amrnb_packed_size[16] = {
- 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
-};
-static const uint8_t amrwb_packed_size[16] = {
- 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
-};
-
#if CONFIG_AMR_DEMUXER
static int amr_probe(const AVProbeData *p)
{
@@ -147,7 +141,7 @@ static int amrnb_probe(const AVProbeData *p)
mode = b[i] >> 3 & 0x0F;
if (mode < 9 && (b[i] & 0x4) == 0x4 && (b[i] & 0x03) == 0) {
int last = b[i];
- int size = amrnb_packed_size[mode];
+ int size = ff_amr_frame_size(AV_CODEC_ID_AMR_NB, mode);
while (size--) {
if (b[++i] != last)
break;
@@ -204,7 +198,7 @@ static int amrwb_probe(const AVProbeData *p)
mode = b[i] >> 3 & 0x0F;
if (mode < 10 && (b[i] & 0x4) == 0x4 && (b[i] & 0x03) == 0) {
int last = b[i];
- int size = amrwb_packed_size[mode];
+ int size = ff_amr_frame_size(AV_CODEC_ID_AMR_WB, mode);
while (size--) {
if (b[++i] != last)
break;
diff --git a/libavformat/amr.h b/libavformat/amr.h
new file mode 100644
index 0000000000..b174cf117b
--- /dev/null
+++ b/libavformat/amr.h
@@ -0,0 +1,42 @@
+/*
+ * AMR storage format helpers
+ *
+ * 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
+ */
+
+#ifndef AVFORMAT_AMR_H
+#define AVFORMAT_AMR_H
+
+#include <stdint.h>
+
+#include "libavcodec/codec_id.h"
+
+/**
+ * Size of an RFC 4867 storage format frame, including its header byte.
+ *
+ * @return the size in bytes, or 0 for a reserved frame type
+ */
+static inline int ff_amr_frame_size(enum AVCodecID codec_id, int frame_type)
+{
+ static const uint8_t frame_size[2][16] = {
+ { 13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1 },
+ { 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 0, 0, 0, 0, 1, 1 },
+ };
+ return frame_size[codec_id == AV_CODEC_ID_AMR_WB][frame_type & 0x0F];
+}
+
+#endif /* AVFORMAT_AMR_H */
diff --git a/libavformat/isom.c b/libavformat/isom.c
index 09be63aa19..3544f0dfec 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -21,6 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "amr.h"
#include "avformat.h"
#include "demux.h"
#include "internal.h"
@@ -398,6 +399,46 @@ int ff_mp4_read_dec_config_descr(void *logctx, AVStream *st, AVIOContext *pb)
return 0;
}
+int ff_amr_write_config_atom(AVIOContext *pb,
+ enum AVCodecID codec_id,
+ int quicktime)
+{
+ avio_wb32(pb, FF_AMR_CONFIG_ATOM_SIZE);
+ /* Apple's layout uses samr for either codec. */
+ avio_write(pb, quicktime ? "samr" : "damr", 4);
+ avio_write(pb, "FFMP", 4);
+ avio_w8(pb, 0); /* decoder version */
+ /* All modes and comfort noise frames, per 3GPP TS 26.244, 6.7. */
+ avio_wb16(pb, codec_id == AV_CODEC_ID_AMR_WB ? 0x83FF : 0x81FF);
+ avio_w8(pb, 0); /* mode change period (no restriction) */
+ avio_w8(pb, 1); /* frames per sample */
+ return FF_AMR_CONFIG_ATOM_SIZE;
+}
+
+int ff_amr_check_frame_block(enum AVCodecID codec_id,
+ const uint8_t *buf, int size,
+ int channels)
+{
+ if (channels < 1)
+ return AVERROR_INVALIDDATA;
+
+ for (int ch = 0; ch < channels; ch++) {
+ int frame;
+
+ if (size <= 0)
+ return AVERROR_INVALIDDATA;
+ frame = ff_amr_frame_size(codec_id, *buf >> 3);
+ if (!frame)
+ return AVERROR_INVALIDDATA;
+ if (frame > size)
+ return codec_id == AV_CODEC_ID_AMR_NB && ch == channels - 1
+ ? 0 : AVERROR_INVALIDDATA;
+ buf += frame;
+ size -= frame;
+ }
+ return size ? AVERROR_PATCHWELCOME : 0;
+}
+
typedef struct MovChannelLayout {
int64_t channel_layout;
uint32_t layout_tag;
diff --git a/libavformat/isom.h b/libavformat/isom.h
index e301e2d306..5866d39106 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -48,6 +48,23 @@ extern const AVCodecTag ff_codec_movdata_tags[];
int ff_mov_iso639_to_lang(const char lang[4], int mp4);
int ff_mov_lang_to_iso639(unsigned code, char to[4]);
+#define FF_AMR_CONFIG_ATOM_SIZE 17
+
+int ff_amr_write_config_atom(AVIOContext *pb,
+ enum AVCodecID codec_id,
+ int quicktime);
+
+/**
+ * Check that an AMR packet holds one RFC 4867 frame per channel.
+ * The MOV muxer historically allows a short final AMR-NB frame.
+ *
+ * @return 0 on success, AVERROR_INVALIDDATA for a missing or invalid frame,
+ * or AVERROR_PATCHWELCOME if more data follows the frame block.
+ */
+int ff_amr_check_frame_block(enum AVCodecID codec_id,
+ const uint8_t *buf, int size,
+ int channels);
+
struct AVAESCTR;
/* the QuickTime file format is quite convoluted...
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index aaf5e52046..a23ca51bbf 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -370,16 +370,7 @@ static int mov_write_iacb_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
{
- avio_wb32(pb, 0x11); /* size */
- if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
- else ffio_wfourcc(pb, "damr");
- ffio_wfourcc(pb, "FFMP");
- avio_w8(pb, 0); /* decoder version */
-
- avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
- avio_w8(pb, 0x00); /* Mode change period (no restriction) */
- avio_w8(pb, 0x01); /* Frames per sample */
- return 0x11;
+ return ff_amr_write_config_atom(pb, track->par->codec_id, track->mode == MODE_MOV);
}
struct eac3_info {
@@ -1173,7 +1164,8 @@ static int mov_write_wave_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
mov_write_enda_tag(pb);
} else if (mov_pcm_be_gt16(track->par->codec_id)) {
mov_write_enda_tag_be(pb);
- } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB) {
+ } else if (track->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ track->par->codec_id == AV_CODEC_ID_AMR_WB) {
mov_write_amr_tag(pb, track);
} else if (track->par->codec_id == AV_CODEC_ID_AC3) {
mov_write_ac3_tag(s, pb, track);
@@ -1545,8 +1537,13 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
avio_wb16(pb, 16);
avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
} else { /* reserved for mp4/3gp */
- avio_wb16(pb, track->tag == MKTAG('i', 'a', 'm', 'f') ?
- 0 : track->par->ch_layout.nb_channels);
+ if (track->tag == MKTAG('i', 'a', 'm', 'f'))
+ avio_wb16(pb, 0);
+ else if (track->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ track->par->codec_id == AV_CODEC_ID_AMR_WB)
+ avio_wb16(pb, 2); /* reserved, 3GPP TS 26.244, 6.5 */
+ else
+ avio_wb16(pb, track->par->ch_layout.nb_channels);
if (track->par->codec_id == AV_CODEC_ID_FLAC ||
track->par->codec_id == AV_CODEC_ID_ALAC) {
avio_wb16(pb, track->par->bits_per_raw_sample);
@@ -1577,6 +1574,9 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
if (mov_pcm_le_gt16(track->par->codec_id) ||
mov_pcm_be_gt16(track->par->codec_id))
avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
+ else if (track->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ track->par->codec_id == AV_CODEC_ID_AMR_WB)
+ avio_wb32(pb, track->par->codec_id == AV_CODEC_ID_AMR_WB ? 320 : 160);
else
avio_wb32(pb, track->par->frame_size); /* Samples per packet */
avio_wb32(pb, track->sample_size / track->par->ch_layout.nb_channels); /* Bytes per packet */
@@ -1589,6 +1589,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
track->par->codec_id == AV_CODEC_ID_AC3 ||
track->par->codec_id == AV_CODEC_ID_EAC3 ||
track->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ track->par->codec_id == AV_CODEC_ID_AMR_WB ||
track->par->codec_id == AV_CODEC_ID_ALAC ||
track->par->codec_id == AV_CODEC_ID_ADPCM_MS ||
track->par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
@@ -1603,7 +1604,8 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
else if (track->tag == MKTAG('i','a','m','f'))
ret = mov_write_iacb_tag(mov->fc, pb, track);
#endif
- else if (track->par->codec_id == AV_CODEC_ID_AMR_NB)
+ else if (track->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ track->par->codec_id == AV_CODEC_ID_AMR_WB)
ret = mov_write_amr_tag(pb, track);
else if (track->par->codec_id == AV_CODEC_ID_AC3)
ret = mov_write_ac3_tag(s, pb, track);
@@ -7428,22 +7430,8 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
}
}
- if (par->codec_id == AV_CODEC_ID_AMR_NB) {
- /* We must find out how many AMR blocks there are in one packet */
- static const uint16_t packed_size[16] =
- {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
- int len = 0;
-
- while (len < size && samples_in_chunk < 100) {
- len += packed_size[(pkt->data[len] >> 3) & 0x0F];
- samples_in_chunk++;
- }
- if (samples_in_chunk > 1) {
- av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
- return -1;
- }
- } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
- par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
+ if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
+ par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
/* packets of whole blocks of block_align bytes */
int64_t blocks = size == trk->sample_size ? 1 : size / trk->sample_size;
if (!size || size != blocks * trk->sample_size) {
@@ -8145,6 +8133,16 @@ static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
trk = s->streams[pkt->stream_index]->priv_data;
+ /* Reject unsupported AMR blocks before the packet can change a fragment. */
+ if (pkt->size && (trk->par->codec_id == AV_CODEC_ID_AMR_NB ||
+ trk->par->codec_id == AV_CODEC_ID_AMR_WB)) {
+ int ret = ff_amr_check_frame_block(trk->par->codec_id, pkt->data, pkt->size, 1);
+ if (ret < 0) {
+ av_log(s, AV_LOG_ERROR, "Invalid or unsupported AMR packet\n");
+ return ret;
+ }
+ }
+
#if CONFIG_IAMFENC
if (trk->iamf) {
int ret = mov_build_iamf_packet(s, trk, pkt);
@@ -9124,7 +9122,15 @@ static int mov_init(AVFormatContext *s)
}
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
track->timescale = st->codecpar->sample_rate;
- if (!st->codecpar->frame_size && !av_get_bits_per_sample(st->codecpar->codec_id)) {
+ if (st->codecpar->codec_id == AV_CODEC_ID_AMR_NB ||
+ st->codecpar->codec_id == AV_CODEC_ID_AMR_WB) {
+ if (st->codecpar->ch_layout.nb_channels != 1) {
+ av_log(s, AV_LOG_ERROR, "track %d: AMR requires mono audio\n", i);
+ return AVERROR(EINVAL);
+ }
+ /* One frame per sample, sized by its frame type */
+ track->audio_vbr = 1;
+ } else if (!st->codecpar->frame_size && !av_get_bits_per_sample(st->codecpar->codec_id)) {
av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
track->audio_vbr = 1;
}else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS ||
diff --git a/tests/fate/amrwb.mak b/tests/fate/amrwb.mak
index 3ffb934998..82140e6156 100644
--- a/tests/fate/amrwb.mak
+++ b/tests/fate/amrwb.mak
@@ -44,6 +44,14 @@ fate-amrwb-23k85-2: REF = $(SAMPLES)/amrwb/deus-23k85.pcm
FATE_AMRWB_REMUX-$(call REMUX, AMR, MOV_DEMUXER AMR_PARSER) += fate-amrwb-remux
fate-amrwb-remux: CMD = transcode mov $(TARGET_SAMPLES)/amrwb/seed-23k85.awb amr "-c copy" "-c copy -t 1"
+# Exercise both the MOV wave configuration and the 3GP damr box, including
+# the AMR-WB mode set. The output file checksum covers the configuration.
+FATE_AMRWB_REMUX-$(call REMUX, MOV) += fate-amrwb-mov-remux
+fate-amrwb-mov-remux: CMD = transcode mov $(TARGET_SAMPLES)/amrwb/seed-23k85.awb mov "-c copy" "-c copy -frames:a 2"
+
+FATE_AMRWB_REMUX-$(call REMUX, TGP MOV) += fate-amrwb-3gp-remux
+fate-amrwb-3gp-remux: CMD = transcode mov $(TARGET_SAMPLES)/amrwb/seed-23k85.awb 3gp "-c copy" "-c copy -frames:a 2"
+
$(FATE_AMRWB): CMP = stddev
# Check that SID frames are decoded to comfort noise instead of being dropped,
diff --git a/tests/ref/fate/amrwb-3gp-remux b/tests/ref/fate/amrwb-3gp-remux
new file mode 100644
index 0000000000..2f0ad58586
--- /dev/null
+++ b/tests/ref/fate/amrwb-3gp-remux
@@ -0,0 +1,9 @@
+b08c0b4808df82b6217c12d08899a7c8 *tests/data/fate/amrwb-3gp-remux.3gp
+31843 tests/data/fate/amrwb-3gp-remux.3gp
+#tb 0: 1/16000
+#media_type 0: audio
+#codec_id 0: amr_wb
+#sample_rate 0: 16000
+#channel_layout_name 0: mono
+0, 0, 0, 320, 61, 0xc8891fe9
+0, 320, 320, 320, 61, 0xc2562027
diff --git a/tests/ref/fate/amrwb-mov-remux b/tests/ref/fate/amrwb-mov-remux
new file mode 100644
index 0000000000..ce234fa5bd
--- /dev/null
+++ b/tests/ref/fate/amrwb-mov-remux
@@ -0,0 +1,9 @@
+c327814fbc3cc97c7476c1a984a15caf *tests/data/fate/amrwb-mov-remux.mov
+31923 tests/data/fate/amrwb-mov-remux.mov
+#tb 0: 1/16000
+#media_type 0: audio
+#codec_id 0: amr_wb
+#sample_rate 0: 16000
+#channel_layout_name 0: mono
+0, 0, 0, 320, 61, 0xc8891fe9
+0, 320, 320, 320, 61, 0xc2562027