1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

ogg/opus: implement header packet skip in chained ogg bitstreams.

This commit is contained in:
Romain Beauxis
2025-05-06 09:11:23 -05:00
committed by Lynne
parent 2fb6416dd0
commit 9c5ed57f94
3 changed files with 60 additions and 32 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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