Commit 113a13c527 for ffmpeg
commit 113a13c5279cb78f16e1353b4228bbdd18855bdf
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Thu Nov 12 15:03:11 2026 -0600
avcodec/opus/tab: add OPUS_TIMESTAMP_RATE
Name the 48 kHz clock of Opus timestamps and use it in place of literal
48000s.
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index 9782dd9cdc..a5818fab69 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -30,6 +30,7 @@
#include "codec_internal.h"
#include "encode.h"
#include "libopus.h"
+#include "opus/tab.h"
#include "audio_frame_queue.h"
#include "vorbis_data.h"
@@ -98,7 +99,8 @@ static void libopus_write_header(AVCodecContext *avctx, int stream_count,
bytestream_put_buffer(&p, "OpusHead", 8);
bytestream_put_byte(&p, 1); /* Version */
bytestream_put_byte(&p, channels);
- bytestream_put_le16(&p, avctx->initial_padding * 48000 / avctx->sample_rate); /* Lookahead samples at 48kHz */
+ bytestream_put_le16(&p, avctx->initial_padding * OPUS_TIMESTAMP_RATE /
+ avctx->sample_rate); /* Lookahead */
bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
@@ -275,7 +277,7 @@ static av_cold int libopus_encode_init(AVCodecContext *avctx)
int coupled_stream_count, header_size, frame_size;
int mapping_family;
- frame_size = opus->opts.frame_duration * 48000 / 1000;
+ frame_size = opus->opts.frame_duration * OPUS_TIMESTAMP_RATE / 1000;
switch (frame_size) {
case 120:
case 240:
@@ -298,7 +300,8 @@ static av_cold int libopus_encode_init(AVCodecContext *avctx)
case 5760:
#endif
opus->opts.packet_size =
- avctx->frame_size = frame_size * avctx->sample_rate / 48000;
+ avctx->frame_size = frame_size * avctx->sample_rate /
+ OPUS_TIMESTAMP_RATE;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
diff --git a/libavcodec/opus/tab.h b/libavcodec/opus/tab.h
index a6fcf62535..1513b48a11 100644
--- a/libavcodec/opus/tab.h
+++ b/libavcodec/opus/tab.h
@@ -28,13 +28,19 @@
#include "libavutil/attributes_internal.h"
#include "libavutil/error.h"
+/**
+ * The clock of Opus: packet lengths, pre-skip and trimming count samples at
+ * 48 kHz whatever the sample rate of the stream.
+ */
+#define OPUS_TIMESTAMP_RATE 48000
+
FF_VISIBILITY_PUSH_HIDDEN
extern const uint16_t ff_opus_frame_duration[32];
/**
- * @return the duration of an Opus packet in samples at 48 kHz, read from
- * its TOC, or AVERROR_INVALIDDATA if the packet is too short or
- * longer than 120 ms
+ * @return the duration of an Opus packet in samples at OPUS_TIMESTAMP_RATE,
+ * read from its TOC, or AVERROR_INVALIDDATA if the packet is too
+ * short or longer than 120 ms
*/
static inline int ff_opus_packet_samples(const uint8_t *buf, int buf_size)
{
@@ -57,7 +63,8 @@ static inline int ff_opus_packet_samples(const uint8_t *buf, int buf_size)
}
samples = frame_count * ff_opus_frame_duration[buf[0] >> 3];
// A packet holds at most 120 ms.
- return samples > 120 * 48 ? AVERROR_INVALIDDATA : samples;
+ return samples > 120 * OPUS_TIMESTAMP_RATE / 1000 ? AVERROR_INVALIDDATA
+ : samples;
}
extern const uint8_t ff_celt_band_end[];
diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c
index b7e7d1c56f..0634bdb4c1 100644
--- a/libavformat/cafenc.c
+++ b/libavformat/cafenc.c
@@ -29,6 +29,7 @@
#include "libavutil/intfloat.h"
#include "libavutil/dict.h"
#include "libavutil/mem.h"
+#include "libavcodec/opus/tab.h"
#define FRAME_SIZE_OFFSET 40
@@ -94,7 +95,7 @@ static uint32_t samples_per_packet(const AVCodecParameters *par) {
case AV_CODEC_ID_MP1:
return 384;
case AV_CODEC_ID_OPUS:
- return frame_size * 48000 / sample_rate;
+ return frame_size * OPUS_TIMESTAMP_RATE / sample_rate;
case AV_CODEC_ID_MP2:
case AV_CODEC_ID_MP3:
return 1152;
@@ -169,7 +170,7 @@ static int caf_write_header(AVFormatContext *s)
}
if (par->codec_id == AV_CODEC_ID_OPUS)
- sample_rate = 48000;
+ sample_rate = OPUS_TIMESTAMP_RATE;
ffio_wfourcc(pb, "caff"); //< mFileType
avio_wb16(pb, 1); //< mFileVersion
diff --git a/libavformat/iamf_writer.c b/libavformat/iamf_writer.c
index 98d09bfe01..ffee4479d2 100644
--- a/libavformat/iamf_writer.c
+++ b/libavformat/iamf_writer.c
@@ -25,6 +25,7 @@
#include "libavutil/iamf.h"
#include "libavutil/mem.h"
#include "libavcodec/get_bits.h"
+#include "libavcodec/opus/tab.h"
#include "libavcodec/put_bits.h"
#include "avformat.h"
#include "avio_internal.h"
@@ -123,7 +124,9 @@ static int fill_codec_config(IAMFContext *iamf, const AVStreamGroup *stg,
switch (codec_config->codec_id) {
case AV_CODEC_ID_OPUS:
codec_config->sample_rate = 48000;
- codec_config->nb_samples = av_rescale(st->codecpar->frame_size, 48000, st->codecpar->sample_rate);
+ codec_config->nb_samples = av_rescale(st->codecpar->frame_size,
+ OPUS_TIMESTAMP_RATE,
+ st->codecpar->sample_rate);
break;
default:
codec_config->sample_rate = st->codecpar->sample_rate;
@@ -1292,9 +1295,10 @@ int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
// IAMF's num_samples_to_trim_at_start is the same as Opus's pre-skip.
if (!skip_samples)
skip_samples = pkt->dts < 0
- ? av_rescale(-pkt->dts, 48000, pkt->time_base.den)
+ ? av_rescale(-pkt->dts, OPUS_TIMESTAMP_RATE, pkt->time_base.den)
: 0;
- discard_padding = av_rescale(discard_padding, 48000, pkt->time_base.den);
+ discard_padding = av_rescale(discard_padding, OPUS_TIMESTAMP_RATE,
+ pkt->time_base.den);
}
ret = avio_open_dyn_buf(&dyn_bc);
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 25526d079d..d9a40f8822 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -2876,8 +2876,9 @@ static int mkv_write_block(void *logctx, MatroskaMuxContext *mkv,
if (ret >= 0) {
/* If the packet's duration is inconsistent with the coded duration,
* add an explicit duration element. */
- uint64_t parsed_duration = av_rescale_q(ret, (AVRational){1, 48000},
- st->time_base);
+ uint64_t parsed_duration =
+ av_rescale_q(ret, (AVRational){1, OPUS_TIMESTAMP_RATE},
+ st->time_base);
if (parsed_duration != duration)
ebml_writer_add_uint(&writer, MATROSKA_ID_BLOCKDURATION, duration);
}
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 41c2f1f409..3fe0304f4d 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -71,6 +71,7 @@
#include "riff.h"
#include "isom.h"
#include "libavcodec/get_bits.h"
+#include "libavcodec/opus/tab.h"
#include "id3v1.h"
#include "mov_chan.h"
#include "replaygain.h"
@@ -9201,9 +9202,9 @@ static int mov_read_dops(MOVContext *c, AVIOContext *pb, MOVAtom atom)
AV_WL16A(st->codecpar->extradata + 16, AV_RB16A(st->codecpar->extradata + 16));
st->codecpar->initial_padding = pre_skip;
- st->codecpar->seek_preroll = av_rescale_q(OPUS_SEEK_PREROLL_MS,
- (AVRational){1, 1000},
- (AVRational){1, 48000});
+ st->codecpar->seek_preroll =
+ av_rescale_q(OPUS_SEEK_PREROLL_MS, (AVRational){1, 1000},
+ (AVRational){1, OPUS_TIMESTAMP_RATE});
return 0;
}
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 25df5e5f46..e51fd53458 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -47,6 +47,7 @@
#include "libavcodec/get_bits.h"
#include "libavcodec/internal.h"
+#include "libavcodec/opus/tab.h"
#include "libavcodec/put_bits.h"
#include "libavcodec/vc1_common.h"
#include "libavcodec/raw.h"
@@ -1551,7 +1552,7 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
if (track->tag == MKTAG('i','a','m','f'))
avio_wb16(pb, 0); /* samplerate must be 0 for IAMF */
else if (track->par->codec_id == AV_CODEC_ID_OPUS)
- avio_wb16(pb, 48000);
+ avio_wb16(pb, OPUS_TIMESTAMP_RATE);
else if (track->par->codec_id == AV_CODEC_ID_TRUEHD)
avio_wb32(pb, track->par->sample_rate);
else
@@ -3453,7 +3454,8 @@ static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
int duration = get_cluster_duration(track, i);
#if CONFIG_IAMFENC
if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
- duration = av_rescale(duration, 48000, track->par->sample_rate);
+ duration = av_rescale(duration, OPUS_TIMESTAMP_RATE,
+ track->par->sample_rate);
#endif
if (i && duration == stts_entries[entries].duration) {
stts_entries[entries].count++; /* compress */
@@ -3509,7 +3511,7 @@ static int mov_preroll_write_stbl_atoms(AVIOContext *pb, MOVTrack *track)
const int OPUS_SEEK_PREROLL_MS = 80;
int roll_samples = av_rescale_q(OPUS_SEEK_PREROLL_MS,
(AVRational){1, 1000},
- (AVRational){1, 48000});
+ (AVRational){1, OPUS_TIMESTAMP_RATE});
if (!track->entry)
return 0;
@@ -4446,7 +4448,8 @@ static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov,
#if CONFIG_IAMFENC
if (track->iamf && track->par->codec_id == AV_CODEC_ID_OPUS)
- start_ct = av_rescale(start_ct, 48000, track->par->sample_rate);
+ start_ct = av_rescale(start_ct, OPUS_TIMESTAMP_RATE,
+ track->par->sample_rate);
#endif
/* Note, this delay is calculated from the pts of the first sample,
* ensuring that we don't reduce the duration for cases with
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 6682abfac4..582a9a3e60 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -33,6 +33,7 @@
#include "libavcodec/defs.h"
#include "libavcodec/h264.h"
#include "libavcodec/hevc/hevc.h"
+#include "libavcodec/opus/tab.h"
#include "libavcodec/vvc.h"
#include "libavcodec/startcode.h"
@@ -1341,7 +1342,9 @@ static int mpegts_init(AVFormatContext *s)
return ret;
}
if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
- ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate;
+ ts_st->opus_pending_trim_start = st->codecpar->initial_padding *
+ OPUS_TIMESTAMP_RATE /
+ st->codecpar->sample_rate;
}
}
@@ -2221,7 +2224,8 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
&side_data_size);
if (side_data && side_data_size >= 10) {
- trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
+ trim_end = AV_RL32(side_data + 4) * OPUS_TIMESTAMP_RATE /
+ st->codecpar->sample_rate;
}
if (pkt->size > INT_MAX - 7 - pkt->size / 255)
diff --git a/libavformat/ogg/mapping/opus.c b/libavformat/ogg/mapping/opus.c
index c9fca87993..714c4709b6 100644
--- a/libavformat/ogg/mapping/opus.c
+++ b/libavformat/ogg/mapping/opus.c
@@ -23,6 +23,7 @@
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
+#include "libavcodec/opus/tab.h"
#include "libavformat/avformat.h"
#include "libavformat/internal.h"
#include "libavformat/ogg/mapping.h"
@@ -75,7 +76,7 @@ static int parse_opus_header(AVFormatContext *avf, AVStream *st, struct ogg_stre
st->codecpar->sample_rate = 48000;
st->codecpar->seek_preroll = av_rescale(OPUS_SEEK_PREROLL_MS,
st->codecpar->sample_rate, 1000);
- avpriv_set_pts_info(st, 64, 1, 48000);
+ avpriv_set_pts_info(st, 64, 1, OPUS_TIMESTAMP_RATE);
priv->need_comments = 1;
diff --git a/libavformat/ogg/muxer.c b/libavformat/ogg/muxer.c
index 409478e5c5..69b9a679b9 100644
--- a/libavformat/ogg/muxer.c
+++ b/libavformat/ogg/muxer.c
@@ -31,6 +31,7 @@
#include "libavcodec/xiph.h"
#include "libavcodec/bytestream.h"
#include "libavcodec/flac.h"
+#include "libavcodec/opus/tab.h"
#include "libavformat/avformat.h"
#include "libavformat/avio_internal.h"
#include "libavformat/internal.h"
@@ -534,8 +535,7 @@ static int ogg_init(AVFormatContext *s)
if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
if (st->codecpar->codec_id == AV_CODEC_ID_OPUS)
- /* Opus requires a fixed 48kHz clock */
- avpriv_set_pts_info(st, 64, 1, 48000);
+ avpriv_set_pts_info(st, 64, 1, OPUS_TIMESTAMP_RATE);
else
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
}
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index f462f7826f..0e634c070d 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -28,6 +28,7 @@
#include "libavutil/mem.h"
#include "libavutil/random_seed.h"
#include "libavutil/opt.h"
+#include "libavcodec/opus/tab.h"
#include "rtpenc.h"
@@ -300,7 +301,7 @@ static int rtp_write_header(AVFormatContext *s1)
/* The opus RTP RFC says that all opus streams should use 48000 Hz
* as clock rate, since all opus sample rates can be expressed in
* this clock rate, and sample rate changes on the fly are supported. */
- avpriv_set_pts_info(st, 32, 1, 48000);
+ avpriv_set_pts_info(st, 32, 1, OPUS_TIMESTAMP_RATE);
break;
case AV_CODEC_ID_ILBC:
if (st->codecpar->block_align != 38 && st->codecpar->block_align != 50) {