Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/libvorbisenc.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include <vorbis/vorbisenc.h>
22
23
#include "libavutil/avassert.h"
24
#include "libavutil/channel_layout.h"
25
#include "libavutil/fifo.h"
26
#include "libavutil/mem.h"
27
#include "libavutil/opt.h"
28
#include "avcodec.h"
29
#include "audio_frame_queue.h"
30
#include "codec_internal.h"
31
#include "encode.h"
32
#include "version.h"
33
#include "vorbis_parser.h"
34
35
36
/* Number of samples the user should send in each call.
37
 * This value is used because it is the LCD of all possible frame sizes, so
38
 * an output packet will always start at the same point as one of the input
39
 * packets.
40
 */
41
0
#define LIBVORBIS_FRAME_SIZE 64
42
43
0
#define BUFFER_SIZE (1024 * 64)
44
45
typedef struct LibvorbisEncContext {
46
    AVClass *av_class;                  /**< class for AVOptions            */
47
    vorbis_info vi;                     /**< vorbis_info used during init   */
48
    vorbis_dsp_state vd;                /**< DSP state used for analysis    */
49
    vorbis_block vb;                    /**< vorbis_block used for analysis */
50
    AVFifo *pkt_fifo;                   /**< output packet buffer           */
51
    int eof;                            /**< end-of-file flag               */
52
    int dsp_initialized;                /**< vd has been initialized        */
53
    vorbis_comment vc;                  /**< VorbisComment info             */
54
    double iblock;                      /**< impulse block bias option      */
55
    AVVorbisParseContext *vp;           /**< parse context to get durations */
56
    AudioFrameQueue afq;                /**< frame queue for timestamps     */
57
} LibvorbisEncContext;
58
59
static const AVOption options[] = {
60
    { "iblock", "Sets the impulse block bias", offsetof(LibvorbisEncContext, iblock), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -15, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
61
    { NULL }
62
};
63
64
static const FFCodecDefault defaults[] = {
65
    { "b",  "0" },
66
    { NULL },
67
};
68
69
static const AVClass vorbis_class = {
70
    .class_name = "libvorbis",
71
    .item_name  = av_default_item_name,
72
    .option     = options,
73
    .version    = LIBAVUTIL_VERSION_INT,
74
};
75
76
static const uint8_t vorbis_encoding_channel_layout_offsets[8][8] = {
77
    { 0 },
78
    { 0, 1 },
79
    { 0, 2, 1 },
80
    { 0, 1, 2, 3 },
81
    { 0, 2, 1, 3, 4 },
82
    { 0, 2, 1, 4, 5, 3 },
83
    { 0, 2, 1, 5, 6, 4, 3 },
84
    { 0, 2, 1, 6, 7, 4, 5, 3 },
85
};
86
87
static int vorbis_error_to_averror(int ov_err)
88
0
{
89
0
    switch (ov_err) {
90
0
    case OV_EFAULT: return AVERROR_BUG;
91
0
    case OV_EINVAL: return AVERROR(EINVAL);
92
0
    case OV_EIMPL:  return AVERROR(EINVAL);
93
0
    default:        return AVERROR_UNKNOWN;
94
0
    }
95
0
}
96
97
static av_cold int libvorbis_setup(vorbis_info *vi, AVCodecContext *avctx)
98
0
{
99
0
    LibvorbisEncContext *s = avctx->priv_data;
100
0
    int channels = avctx->ch_layout.nb_channels;
101
0
    double cfreq;
102
0
    int ret;
103
104
0
    if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
105
        /* variable bitrate
106
         * NOTE: we use the oggenc range of -1 to 10 for global_quality for
107
         *       user convenience, but libvorbis uses -0.1 to 1.0.
108
         */
109
0
        float q = avctx->global_quality / (float)FF_QP2LAMBDA;
110
        /* default to 3 if the user did not set quality or bitrate */
111
0
        if (!(avctx->flags & AV_CODEC_FLAG_QSCALE))
112
0
            q = 3.0;
113
0
        if ((ret = vorbis_encode_setup_vbr(vi, channels,
114
0
                                           avctx->sample_rate,
115
0
                                           q / 10.0)))
116
0
            goto error;
117
0
    } else {
118
0
        int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
119
0
        int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
120
121
        /* average bitrate */
122
0
        if ((ret = vorbis_encode_setup_managed(vi, channels,
123
0
                                               avctx->sample_rate, maxrate,
124
0
                                               avctx->bit_rate, minrate)))
125
0
            goto error;
126
127
        /* variable bitrate by estimate, disable slow rate management */
128
0
        if (minrate == -1 && maxrate == -1)
129
0
            if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
130
0
                goto error; /* should not happen */
131
0
    }
132
133
    /* cutoff frequency */
134
0
    if (avctx->cutoff > 0) {
135
0
        cfreq = avctx->cutoff / 1000.0;
136
0
        if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
137
0
            goto error; /* should not happen */
138
0
    }
139
140
    /* impulse block bias */
141
0
    if (s->iblock) {
142
0
        if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
143
0
            goto error;
144
0
    }
145
146
0
    if ((channels == 3 &&
147
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
148
0
        (channels == 4 &&
149
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) &&
150
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) ||
151
0
        (channels == 5 &&
152
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) &&
153
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) ||
154
0
        (channels == 6 &&
155
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) &&
156
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK)) ||
157
0
        (channels == 7 &&
158
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1)) ||
159
0
        (channels == 8 &&
160
0
         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))) {
161
0
        if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
162
0
            char name[32];
163
0
            av_channel_layout_describe(&avctx->ch_layout, name, sizeof(name));
164
0
            av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
165
0
                                             "output stream will have incorrect "
166
0
                                             "channel layout.\n", name);
167
0
        } else {
168
0
            av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
169
0
                                               "will use Vorbis channel layout for "
170
0
                                               "%d channels.\n", channels);
171
0
        }
172
0
    }
173
174
0
    if ((ret = vorbis_encode_setup_init(vi)))
175
0
        goto error;
176
177
0
    return 0;
178
0
error:
179
0
    return vorbis_error_to_averror(ret);
180
0
}
181
182
/* How many bytes are needed for a buffer of length 'l' */
183
static int xiph_len(int l)
184
0
{
185
0
    return 1 + l / 255 + l;
186
0
}
187
188
static av_cold int libvorbis_encode_close(AVCodecContext *avctx)
189
0
{
190
0
    LibvorbisEncContext *s = avctx->priv_data;
191
192
    /* notify vorbisenc this is EOF */
193
0
    if (s->dsp_initialized)
194
0
        vorbis_analysis_wrote(&s->vd, 0);
195
196
0
    vorbis_block_clear(&s->vb);
197
0
    vorbis_dsp_clear(&s->vd);
198
0
    vorbis_info_clear(&s->vi);
199
200
0
    av_fifo_freep2(&s->pkt_fifo);
201
0
    ff_af_queue_close(&s->afq);
202
203
0
    av_vorbis_parse_free(&s->vp);
204
205
0
    return 0;
206
0
}
207
208
static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
209
0
{
210
0
    LibvorbisEncContext *s = avctx->priv_data;
211
0
    ogg_packet header, header_comm, header_code;
212
0
    uint8_t *p;
213
0
    unsigned int offset;
214
0
    int ret;
215
216
0
    vorbis_info_init(&s->vi);
217
0
    if ((ret = libvorbis_setup(&s->vi, avctx))) {
218
0
        av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
219
0
        goto error;
220
0
    }
221
0
    if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
222
0
        av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
223
0
        ret = vorbis_error_to_averror(ret);
224
0
        goto error;
225
0
    }
226
0
    s->dsp_initialized = 1;
227
0
    if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
228
0
        av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
229
0
        ret = vorbis_error_to_averror(ret);
230
0
        goto error;
231
0
    }
232
233
0
    vorbis_comment_init(&s->vc);
234
0
    if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
235
0
        vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
236
237
0
    if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
238
0
                                         &header_code))) {
239
0
        ret = vorbis_error_to_averror(ret);
240
0
        goto error;
241
0
    }
242
243
0
    avctx->extradata_size = 1 + xiph_len(header.bytes)      +
244
0
                                xiph_len(header_comm.bytes) +
245
0
                                header_code.bytes;
246
0
    p = avctx->extradata = av_malloc(avctx->extradata_size +
247
0
                                     AV_INPUT_BUFFER_PADDING_SIZE);
248
0
    if (!p) {
249
0
        ret = AVERROR(ENOMEM);
250
0
        goto error;
251
0
    }
252
0
    p[0]    = 2;
253
0
    offset  = 1;
254
0
    offset += av_xiphlacing(&p[offset], header.bytes);
255
0
    offset += av_xiphlacing(&p[offset], header_comm.bytes);
256
0
    memcpy(&p[offset], header.packet, header.bytes);
257
0
    offset += header.bytes;
258
0
    memcpy(&p[offset], header_comm.packet, header_comm.bytes);
259
0
    offset += header_comm.bytes;
260
0
    memcpy(&p[offset], header_code.packet, header_code.bytes);
261
0
    offset += header_code.bytes;
262
0
    av_assert0(offset == avctx->extradata_size);
263
264
0
    s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
265
0
    if (!s->vp) {
266
0
        av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
267
0
        return ret;
268
0
    }
269
270
0
    vorbis_comment_clear(&s->vc);
271
272
0
    avctx->frame_size = LIBVORBIS_FRAME_SIZE;
273
0
    ff_af_queue_init(avctx, &s->afq);
274
275
0
    s->pkt_fifo = av_fifo_alloc2(BUFFER_SIZE, 1, 0);
276
0
    if (!s->pkt_fifo) {
277
0
        ret = AVERROR(ENOMEM);
278
0
        goto error;
279
0
    }
280
281
0
    return 0;
282
0
error:
283
0
    libvorbis_encode_close(avctx);
284
0
    return ret;
285
0
}
286
287
static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
288
                                  const AVFrame *frame, int *got_packet_ptr)
289
0
{
290
0
    LibvorbisEncContext *s = avctx->priv_data;
291
0
    ogg_packet op;
292
0
    int ret, duration;
293
294
    /* send samples to libvorbis */
295
0
    if (frame) {
296
0
        const int samples = frame->nb_samples;
297
0
        float **buffer;
298
0
        int c, channels = s->vi.channels;
299
300
0
        buffer = vorbis_analysis_buffer(&s->vd, samples);
301
0
        for (c = 0; c < channels; c++) {
302
0
            int co = (channels > 8) ? c :
303
0
                     vorbis_encoding_channel_layout_offsets[channels - 1][c];
304
0
            memcpy(buffer[c], frame->extended_data[co],
305
0
                   samples * sizeof(*buffer[c]));
306
0
        }
307
0
        if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
308
0
            av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
309
0
            return vorbis_error_to_averror(ret);
310
0
        }
311
0
        if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
312
0
            return ret;
313
0
    } else {
314
0
        if (!s->eof && s->afq.frame_alloc)
315
0
            if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
316
0
                av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
317
0
                return vorbis_error_to_averror(ret);
318
0
            }
319
0
        s->eof = 1;
320
0
    }
321
322
    /* retrieve available packets from libvorbis */
323
0
    while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
324
0
        if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
325
0
            break;
326
0
        if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
327
0
            break;
328
329
        /* add any available packets to the output packet buffer */
330
0
        while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
331
0
            if (av_fifo_can_write(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
332
0
                av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
333
0
                return AVERROR_BUG;
334
0
            }
335
0
            av_fifo_write(s->pkt_fifo, &op, sizeof(ogg_packet));
336
0
            av_fifo_write(s->pkt_fifo, op.packet, op.bytes);
337
0
        }
338
0
        if (ret < 0) {
339
0
            av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
340
0
            break;
341
0
        }
342
0
    }
343
0
    if (ret < 0) {
344
0
        av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
345
0
        return vorbis_error_to_averror(ret);
346
0
    }
347
348
    /* Read an available packet if possible */
349
0
    if (av_fifo_read(s->pkt_fifo, &op, sizeof(ogg_packet)) < 0)
350
0
        return 0;
351
352
0
    if ((ret = ff_get_encode_buffer(avctx, avpkt, op.bytes, 0)) < 0)
353
0
        return ret;
354
0
    av_fifo_read(s->pkt_fifo, avpkt->data, op.bytes);
355
356
0
    avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
357
358
0
    duration = av_vorbis_parse_frame(s->vp, avpkt->data, avpkt->size);
359
0
    if (duration > 0) {
360
        /* we do not know encoder delay until we get the first packet from
361
         * libvorbis, so we have to update the AudioFrameQueue counts */
362
0
        if (!avctx->initial_padding && s->afq.frames) {
363
0
            avctx->initial_padding    = duration;
364
0
            av_assert0(!s->afq.remaining_delay);
365
0
            s->afq.frames->duration  += duration;
366
0
            if (s->afq.frames->pts != AV_NOPTS_VALUE)
367
0
                s->afq.frames->pts       -= duration;
368
0
            s->afq.remaining_samples += duration;
369
0
        }
370
0
        ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
371
0
    }
372
373
0
    *got_packet_ptr = 1;
374
0
    return 0;
375
0
}
376
377
const FFCodec ff_libvorbis_encoder = {
378
    .p.name         = "libvorbis",
379
    CODEC_LONG_NAME("libvorbis"),
380
    .p.type         = AVMEDIA_TYPE_AUDIO,
381
    .p.id           = AV_CODEC_ID_VORBIS,
382
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
383
                      AV_CODEC_CAP_SMALL_LAST_FRAME,
384
    .caps_internal  = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
385
    .priv_data_size = sizeof(LibvorbisEncContext),
386
    .init           = libvorbis_encode_init,
387
    FF_CODEC_ENCODE_CB(libvorbis_encode_frame),
388
    .close          = libvorbis_encode_close,
389
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP),
390
    .p.priv_class   = &vorbis_class,
391
    .defaults       = defaults,
392
    .p.wrapper_name = "libvorbis",
393
};