Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/oggparseopus.c
Line
Count
Source
1
/*
2
 * Opus parser for Ogg
3
 * Copyright (c) 2012 Nicolas George
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <string.h>
23
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/mem.h"
26
#include "avformat.h"
27
#include "internal.h"
28
#include "oggdec.h"
29
30
struct oggopus_private {
31
    int need_comments;
32
    unsigned pre_skip;
33
    int64_t cur_dts;
34
};
35
36
14.5k
#define OPUS_SEEK_PREROLL_MS 80
37
29.4k
#define OPUS_HEAD_SIZE 19
38
39
static int parse_opus_header(AVFormatContext *avf, AVStream *st, struct ogg_stream *os,
40
                             struct oggopus_private *priv, uint8_t *packet,
41
                             size_t psize)
42
14.7k
{
43
14.7k
    int channels;
44
14.7k
    int ret;
45
46
14.7k
    if (psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
47
90
        return AVERROR_INVALIDDATA;
48
49
14.6k
    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
50
14.6k
    st->codecpar->codec_id   = AV_CODEC_ID_OPUS;
51
52
14.6k
    channels = AV_RL8(packet + 9);
53
14.6k
    if (st->codecpar->ch_layout.nb_channels &&
54
12.9k
        channels != st->codecpar->ch_layout.nb_channels) {
55
28
        av_log(avf, AV_LOG_ERROR, "Channel change is not supported\n");
56
28
        return AVERROR_PATCHWELCOME;
57
28
    }
58
59
14.5k
    st->codecpar->ch_layout.nb_channels = channels;
60
61
14.5k
    priv->pre_skip        = AV_RL16(packet + 10);
62
14.5k
    st->codecpar->initial_padding = priv->pre_skip;
63
14.5k
    os->start_trimming = priv->pre_skip;
64
    /*orig_sample_rate    = AV_RL32(packet + 12);*/
65
    /*gain                = AV_RL16(packet + 16);*/
66
    /*channel_map         = AV_RL8 (packet + 18);*/
67
68
14.5k
    ret = ff_alloc_extradata(st->codecpar, os->psize);
69
14.5k
    if (ret < 0)
70
0
        return ret;
71
72
14.5k
    memcpy(st->codecpar->extradata, packet, os->psize);
73
74
14.5k
    st->codecpar->sample_rate = 48000;
75
14.5k
    st->codecpar->seek_preroll = av_rescale(OPUS_SEEK_PREROLL_MS,
76
14.5k
                                            st->codecpar->sample_rate, 1000);
77
14.5k
    avpriv_set_pts_info(st, 64, 1, 48000);
78
79
14.5k
    priv->need_comments = 1;
80
81
14.5k
    return 1;
82
14.5k
}
83
84
static int opus_header(AVFormatContext *avf, int idx)
85
9.48k
{
86
9.48k
    struct ogg *ogg              = avf->priv_data;
87
9.48k
    struct ogg_stream *os        = &ogg->streams[idx];
88
9.48k
    AVStream *st                 = avf->streams[idx];
89
9.48k
    struct oggopus_private *priv = os->private;
90
9.48k
    uint8_t *packet              = os->buf + os->pstart;
91
92
9.48k
    if (!priv) {
93
1.76k
        priv = os->private = av_mallocz(sizeof(*priv));
94
1.76k
        if (!priv)
95
0
            return AVERROR(ENOMEM);
96
1.76k
    }
97
98
9.48k
    if (os->flags & OGG_FLAG_BOS)
99
5.33k
        return parse_opus_header(avf, st, os, priv, packet, os->psize);
100
101
4.15k
    if (priv->need_comments) {
102
3.09k
        if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
103
30
            return AVERROR_INVALIDDATA;
104
3.06k
        ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8);
105
3.06k
        priv->need_comments--;
106
3.06k
        return 1;
107
3.09k
    }
108
109
1.05k
    return 0;
110
4.15k
}
111
112
static int opus_duration(uint8_t *src, int size)
113
105k
{
114
105k
    unsigned nb_frames  = 1;
115
105k
    unsigned toc        = src[0];
116
105k
    unsigned toc_config = toc >> 3;
117
105k
    unsigned toc_count  = toc & 3;
118
105k
    unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
119
105k
                          toc_config < 16 ? 480 << (toc_config & 1) :
120
38.6k
                                            120 << (toc_config & 3);
121
105k
    if (toc_count == 3) {
122
18.9k
        if (size<2)
123
0
            return AVERROR_INVALIDDATA;
124
18.9k
        nb_frames = src[1] & 0x3F;
125
86.2k
    } else if (toc_count) {
126
29.6k
        nb_frames = 2;
127
29.6k
    }
128
129
105k
    return frame_size * nb_frames;
130
105k
}
131
132
static int opus_packet(AVFormatContext *avf, int idx)
133
33.8k
{
134
33.8k
    struct ogg *ogg              = avf->priv_data;
135
33.8k
    struct ogg_stream *os        = &ogg->streams[idx];
136
33.8k
    AVStream *st                 = avf->streams[idx];
137
33.8k
    struct oggopus_private *priv = os->private;
138
33.8k
    uint8_t *packet              = os->buf + os->pstart;
139
33.8k
    int ret;
140
141
33.8k
    if (!os->psize)
142
6
        return AVERROR_INVALIDDATA;
143
33.8k
    if (os->granule > (1LL << 62)) {
144
87
        av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule);
145
87
        return AVERROR_INVALIDDATA;
146
87
    }
147
148
33.7k
     if (os->psize > 8 && !memcmp(packet, "OpusHead", 8)) {
149
9.37k
        ret = parse_opus_header(avf, st, os, priv, packet, os->psize);
150
9.37k
        if (ret < 0)
151
43
            return ret;
152
153
9.33k
        return 1;
154
9.37k
    }
155
156
24.3k
    if (os->psize > 8 && !memcmp(packet, "OpusTags", 8)) {
157
4.90k
        ret = ff_vorbis_update_metadata(avf, st, os->buf + os->pstart + 8,
158
4.90k
                                        os->psize - 8);
159
4.90k
        if (ret < 0)
160
0
            return ret;
161
162
4.90k
        priv->need_comments = 0;
163
4.90k
        return 1;
164
4.90k
    }
165
166
19.4k
    if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
167
12.3k
        int seg, d;
168
12.3k
        int duration;
169
12.3k
        uint8_t *last_pkt  = os->buf + os->pstart;
170
12.3k
        uint8_t *next_pkt  = last_pkt;
171
172
12.3k
        duration = 0;
173
12.3k
        seg = os->segp;
174
12.3k
        d = opus_duration(last_pkt, os->psize);
175
12.3k
        if (d < 0) {
176
0
            os->pflags |= AV_PKT_FLAG_CORRUPT;
177
0
            return 0;
178
0
        }
179
12.3k
        duration += d;
180
12.3k
        last_pkt = next_pkt =  next_pkt + os->psize;
181
87.4k
        for (; seg < os->nsegs; seg++) {
182
75.0k
            next_pkt += os->segments[seg];
183
75.0k
            if (os->segments[seg] < 255 && next_pkt != last_pkt) {
184
73.4k
                d = opus_duration(last_pkt, next_pkt - last_pkt);
185
73.4k
                if (d > 0)
186
69.5k
                    duration += d;
187
73.4k
                last_pkt = next_pkt;
188
73.4k
            }
189
75.0k
        }
190
12.3k
        os->lastpts                 =
191
12.3k
        os->lastdts                 = os->granule - duration;
192
12.3k
    }
193
194
19.4k
    if ((ret = opus_duration(packet, os->psize)) < 0)
195
0
        return ret;
196
197
19.4k
    os->pduration = ret;
198
19.4k
    if (os->lastpts != AV_NOPTS_VALUE) {
199
15.9k
        if (st->start_time == AV_NOPTS_VALUE)
200
331
            st->start_time = os->lastpts;
201
15.9k
        priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
202
15.9k
    }
203
204
19.4k
    priv->cur_dts += os->pduration;
205
19.4k
    if ((os->flags & OGG_FLAG_EOS)) {
206
3.63k
        int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
207
3.63k
        skip = FFMIN(skip, os->pduration);
208
3.63k
        if (skip > 0) {
209
3.00k
            os->pduration = skip < os->pduration ? os->pduration - skip : 1;
210
3.00k
            os->end_trimming = skip;
211
3.00k
            av_log(avf, AV_LOG_DEBUG,
212
3.00k
                   "Last packet was truncated to %d due to end trimming.\n",
213
3.00k
                   os->pduration);
214
3.00k
        }
215
3.63k
    }
216
217
19.4k
    return 0;
218
19.4k
}
219
220
const struct ogg_codec ff_opus_codec = {
221
    .name             = "Opus",
222
    .magic            = "OpusHead",
223
    .magicsize        = 8,
224
    .header           = opus_header,
225
    .packet           = opus_packet,
226
    .nb_header        = 1,
227
};