[FFmpeg-devel] [PATCH v5 3/7] ogg/opus: implement header packet skip in chained ogg bitstreams.
Romain Beauxis
romain.beauxis at gmail.com
Sat May 10 02:43:23 EEST 2025
---
libavformat/oggdec.c | 4 --
libavformat/oggparseopus.c | 87 ++++++++++++++++--------
tests/ref/fate/ogg-opus-chained-meta.txt | 1 -
3 files changed, 60 insertions(+), 32 deletions(-)
diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index 9baf8040a9..5557eb4a14 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -239,10 +239,6 @@ static int ogg_replace_stream(AVFormatContext *s, uint32_t serial, char *magic,
os->start_trimming = 0;
os->end_trimming = 0;
- /* Chained files have extradata as a new packet */
- if (codec == &ff_opus_codec)
- os->header = -1;
-
return i;
}
diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
index 218e9df581..65b93b4053 100644
--- a/libavformat/oggparseopus.c
+++ b/libavformat/oggparseopus.c
@@ -36,6 +36,51 @@ struct oggopus_private {
#define OPUS_SEEK_PREROLL_MS 80
#define OPUS_HEAD_SIZE 19
+static int parse_opus_header(AVFormatContext *avf, AVStream *st, struct ogg_stream *os,
+ struct oggopus_private *priv, uint8_t *packet,
+ size_t psize)
+{
+ int channels;
+ int ret;
+
+ if (psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
+ return AVERROR_INVALIDDATA;
+
+ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+ st->codecpar->codec_id = AV_CODEC_ID_OPUS;
+
+ channels = AV_RL8(packet + 9);
+ if (st->codecpar->ch_layout.nb_channels &&
+ channels != st->codecpar->ch_layout.nb_channels) {
+ av_log(avf, AV_LOG_ERROR, "Channel change is not supported\n");
+ return AVERROR_PATCHWELCOME;
+ }
+
+ st->codecpar->ch_layout.nb_channels = channels;
+
+ priv->pre_skip = AV_RL16(packet + 10);
+ st->codecpar->initial_padding = priv->pre_skip;
+ os->start_trimming = priv->pre_skip;
+ /*orig_sample_rate = AV_RL32(packet + 12);*/
+ /*gain = AV_RL16(packet + 16);*/
+ /*channel_map = AV_RL8 (packet + 18);*/
+
+ ret = ff_alloc_extradata(st->codecpar, os->psize);
+ if (ret < 0)
+ return ret;
+
+ memcpy(st->codecpar->extradata, packet, os->psize);
+
+ 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);
+
+ priv->need_comments = 1;
+
+ return 1;
+}
+
static int opus_header(AVFormatContext *avf, int idx)
{
struct ogg *ogg = avf->priv_data;
@@ -43,7 +88,6 @@ static int opus_header(AVFormatContext *avf, int idx)
AVStream *st = avf->streams[idx];
struct oggopus_private *priv = os->private;
uint8_t *packet = os->buf + os->pstart;
- int ret;
if (!priv) {
priv = os->private = av_mallocz(sizeof(*priv));
@@ -51,32 +95,8 @@ static int opus_header(AVFormatContext *avf, int idx)
return AVERROR(ENOMEM);
}
- if (os->flags & OGG_FLAG_BOS) {
- if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
- return AVERROR_INVALIDDATA;
- st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
- st->codecpar->codec_id = AV_CODEC_ID_OPUS;
- st->codecpar->ch_layout.nb_channels = AV_RL8(packet + 9);
-
- priv->pre_skip = AV_RL16(packet + 10);
- st->codecpar->initial_padding = priv->pre_skip;
- os->start_trimming = priv->pre_skip;
- /*orig_sample_rate = AV_RL32(packet + 12);*/
- /*gain = AV_RL16(packet + 16);*/
- /*channel_map = AV_RL8 (packet + 18);*/
-
- if ((ret = ff_alloc_extradata(st->codecpar, os->psize)) < 0)
- return ret;
-
- memcpy(st->codecpar->extradata, packet, os->psize);
-
- 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);
- priv->need_comments = 1;
- return 1;
- }
+ if (os->flags & OGG_FLAG_BOS)
+ return parse_opus_header(avf, st, os, priv, packet, os->psize);
if (priv->need_comments) {
if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
@@ -125,6 +145,19 @@ static int opus_packet(AVFormatContext *avf, int idx)
return AVERROR_INVALIDDATA;
}
+ if (os->psize > 8 && !memcmp(packet, "OpusHead", 8)) {
+ ret = parse_opus_header(avf, st, os, priv, packet, os->psize);
+ if (ret < 0)
+ return ret;
+
+ return 1;
+ }
+
+ if (os->psize > 8 && !memcmp(packet, "OpusTags", 8)) {
+ priv->need_comments = 0;
+ return 1;
+ }
+
if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
int seg, d;
int duration;
diff --git a/tests/ref/fate/ogg-opus-chained-meta.txt b/tests/ref/fate/ogg-opus-chained-meta.txt
index fc84b8b703..addc41c1eb 100644
--- a/tests/ref/fate/ogg-opus-chained-meta.txt
+++ b/tests/ref/fate/ogg-opus-chained-meta.txt
@@ -13,7 +13,6 @@ Stream ID: 0, frame PTS: 3528, metadata: N/A
Stream ID: 0, packet PTS: 4488, packet DTS: 4488
Stream ID: 0, frame PTS: 4488, metadata: N/A
Stream ID: 0, packet PTS: -312, packet DTS: -312
-Stream ID: 0, new metadata: encoder=Lavc61.19.100 libopus;Lavc61.19.100 libopus:title=First Stream;Second Stream
Stream ID: 0, frame PTS: -312, metadata: N/A
Stream ID: 0, packet PTS: 648, packet DTS: 648
Stream ID: 0, frame PTS: 648, metadata: N/A
--
2.39.5 (Apple Git-154)
More information about the ffmpeg-devel
mailing list