Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/libtheoraenc.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2006 Paul Richards <paul.richards@gmail.com>
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
/**
22
 * @file
23
 * @brief Theora encoder using libtheora.
24
 * @author Paul Richards <paul.richards@gmail.com>
25
 *
26
 * A lot of this is copy / paste from other output codecs in
27
 * libavcodec or pure guesswork (or both).
28
 *
29
 * I have used t_ prefixes on variables which are libtheora types
30
 * and o_ prefixes on variables which are libogg types.
31
 */
32
33
/* FFmpeg includes */
34
#include "libavutil/common.h"
35
#include "libavutil/intreadwrite.h"
36
#include "libavutil/mem.h"
37
#include "libavutil/pixdesc.h"
38
#include "libavutil/log.h"
39
#include "libavutil/base64.h"
40
#include "libavutil/opt.h"
41
#include "avcodec.h"
42
#include "codec_internal.h"
43
#include "encode.h"
44
45
/* libtheora includes */
46
#include <theora/theoraenc.h>
47
48
typedef struct TheoraContext {
49
    AVClass    *av_class;                  /**< class for AVOptions            */
50
    th_enc_ctx *t_state;
51
    uint8_t    *stats;
52
    int         stats_size;
53
    int         stats_offset;
54
    int         uv_hshift;
55
    int         uv_vshift;
56
    unsigned    keyframe_mask;
57
    int         speed_level;
58
} TheoraContext;
59
60
static const AVOption options[] = {
61
    { "speed_level", "Sets the encoding speed level", offsetof(TheoraContext, speed_level), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
62
    { NULL }
63
};
64
65
static const AVClass theora_class = {
66
    .class_name = "libtheora",
67
    .item_name  = av_default_item_name,
68
    .option     = options,
69
    .version    = LIBAVUTIL_VERSION_INT,
70
};
71
72
/** Concatenate an ogg_packet into the extradata. */
73
static int concatenate_packet(unsigned int* offset,
74
                              AVCodecContext* avc_context,
75
                              const ogg_packet* packet)
76
10.0k
{
77
10.0k
    const char* message = NULL;
78
10.0k
    int newsize = avc_context->extradata_size + 2 + packet->bytes;
79
10.0k
    int err = AVERROR_INVALIDDATA;
80
81
10.0k
    if (packet->bytes < 0) {
82
0
        message = "ogg_packet has negative size";
83
10.0k
    } else if (packet->bytes > 0xffff) {
84
0
        message = "ogg_packet is larger than 65535 bytes";
85
10.0k
    } else if (newsize < avc_context->extradata_size) {
86
0
        message = "extradata_size would overflow";
87
10.0k
    } else {
88
10.0k
        if ((err = av_reallocp(&avc_context->extradata, newsize)) < 0) {
89
0
            avc_context->extradata_size = 0;
90
0
            message = "av_realloc failed";
91
0
        }
92
10.0k
    }
93
10.0k
    if (message) {
94
0
        av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
95
0
        return err;
96
0
    }
97
98
10.0k
    avc_context->extradata_size = newsize;
99
10.0k
    AV_WB16(avc_context->extradata + (*offset), packet->bytes);
100
10.0k
    *offset += 2;
101
10.0k
    memcpy(avc_context->extradata + (*offset), packet->packet, packet->bytes);
102
10.0k
    (*offset) += packet->bytes;
103
10.0k
    return 0;
104
10.0k
}
105
106
static int get_stats(AVCodecContext *avctx, int eos)
107
0
{
108
0
#ifdef TH_ENCCTL_2PASS_OUT
109
0
    TheoraContext *h = avctx->priv_data;
110
0
    uint8_t *buf;
111
0
    int bytes;
112
113
0
    bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_OUT, &buf, sizeof(buf));
114
0
    if (bytes < 0) {
115
0
        av_log(avctx, AV_LOG_ERROR, "Error getting first pass stats\n");
116
0
        return AVERROR_EXTERNAL;
117
0
    }
118
0
    if (!eos) {
119
0
        void *tmp = av_fast_realloc(h->stats, &h->stats_size,
120
0
                                   h->stats_offset + bytes);
121
0
        if (!tmp)
122
0
            return AVERROR(ENOMEM);
123
0
        h->stats = tmp;
124
0
        memcpy(h->stats + h->stats_offset, buf, bytes);
125
0
        h->stats_offset += bytes;
126
0
    } else {
127
0
        int b64_size = AV_BASE64_SIZE(h->stats_offset);
128
        // libtheora generates a summary header at the end
129
0
        memcpy(h->stats, buf, bytes);
130
0
        avctx->stats_out = av_malloc(b64_size);
131
0
        if (!avctx->stats_out)
132
0
            return AVERROR(ENOMEM);
133
0
        av_base64_encode(avctx->stats_out, b64_size, h->stats, h->stats_offset);
134
0
    }
135
0
    return 0;
136
#else
137
    av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
138
    return AVERROR(ENOTSUP);
139
#endif
140
0
}
141
142
// libtheora won't read the entire buffer we give it at once, so we have to
143
// repeatedly submit it...
144
static int submit_stats(AVCodecContext *avctx)
145
0
{
146
0
#ifdef TH_ENCCTL_2PASS_IN
147
0
    TheoraContext *h = avctx->priv_data;
148
0
    int bytes;
149
0
    if (!h->stats) {
150
0
        if (!avctx->stats_in) {
151
0
            av_log(avctx, AV_LOG_ERROR, "No statsfile for second pass\n");
152
0
            return AVERROR(EINVAL);
153
0
        }
154
0
        h->stats_size = strlen(avctx->stats_in) * 3/4;
155
0
        h->stats      = av_malloc(h->stats_size);
156
0
        if (!h->stats) {
157
0
            h->stats_size = 0;
158
0
            return AVERROR(ENOMEM);
159
0
        }
160
0
        h->stats_size = av_base64_decode(h->stats, avctx->stats_in, h->stats_size);
161
0
    }
162
0
    while (h->stats_size - h->stats_offset > 0) {
163
0
        bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_IN,
164
0
                              h->stats + h->stats_offset,
165
0
                              h->stats_size - h->stats_offset);
166
0
        if (bytes < 0) {
167
0
            av_log(avctx, AV_LOG_ERROR, "Error submitting stats\n");
168
0
            return AVERROR_EXTERNAL;
169
0
        }
170
0
        if (!bytes)
171
0
            return 0;
172
0
        h->stats_offset += bytes;
173
0
    }
174
0
    return 0;
175
#else
176
    av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
177
    return AVERROR(ENOTSUP);
178
#endif
179
0
}
180
181
static av_cold int encode_init(AVCodecContext* avc_context)
182
3.36k
{
183
3.36k
    th_info t_info;
184
3.36k
    th_comment t_comment;
185
3.36k
    ogg_packet o_packet;
186
3.36k
    unsigned int offset;
187
3.36k
    TheoraContext *h = avc_context->priv_data;
188
3.36k
    uint32_t gop_size = avc_context->gop_size;
189
3.36k
    int ret;
190
191
    /* Set up the theora_info struct */
192
3.36k
    th_info_init(&t_info);
193
3.36k
    t_info.frame_width  = FFALIGN(avc_context->width,  16);
194
3.36k
    t_info.frame_height = FFALIGN(avc_context->height, 16);
195
3.36k
    t_info.pic_width    = avc_context->width;
196
3.36k
    t_info.pic_height   = avc_context->height;
197
3.36k
    t_info.pic_x        = 0;
198
3.36k
    t_info.pic_y        = 0;
199
    /* Swap numerator and denominator as time_base in AVCodecContext gives the
200
     * time period between frames, but theora_info needs the framerate.  */
201
3.36k
    t_info.fps_numerator   = avc_context->time_base.den;
202
3.36k
    t_info.fps_denominator = avc_context->time_base.num;
203
3.36k
    if (avc_context->sample_aspect_ratio.num) {
204
0
        t_info.aspect_numerator   = avc_context->sample_aspect_ratio.num;
205
0
        t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
206
3.36k
    } else {
207
3.36k
        t_info.aspect_numerator   = 1;
208
3.36k
        t_info.aspect_denominator = 1;
209
3.36k
    }
210
211
3.36k
    if (avc_context->color_primaries == AVCOL_PRI_BT470M)
212
0
        t_info.colorspace = TH_CS_ITU_REC_470M;
213
3.36k
    else if (avc_context->color_primaries == AVCOL_PRI_BT470BG)
214
0
        t_info.colorspace = TH_CS_ITU_REC_470BG;
215
3.36k
    else
216
3.36k
        t_info.colorspace = TH_CS_UNSPECIFIED;
217
218
3.36k
    if (avc_context->pix_fmt == AV_PIX_FMT_YUV420P)
219
2.82k
        t_info.pixel_fmt = TH_PF_420;
220
538
    else if (avc_context->pix_fmt == AV_PIX_FMT_YUV422P)
221
202
        t_info.pixel_fmt = TH_PF_422;
222
336
    else if (avc_context->pix_fmt == AV_PIX_FMT_YUV444P)
223
336
        t_info.pixel_fmt = TH_PF_444;
224
0
    else {
225
0
        av_log(avc_context, AV_LOG_ERROR, "Unsupported pix_fmt\n");
226
0
        return AVERROR(EINVAL);
227
0
    }
228
3.36k
    ret = av_pix_fmt_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
229
3.36k
    if (ret)
230
0
        return ret;
231
232
3.36k
    if (avc_context->flags & AV_CODEC_FLAG_QSCALE) {
233
        /* Clip global_quality in QP units to the [0 - 10] range
234
           to be consistent with the libvorbis implementation.
235
           Theora accepts a quality parameter which is an int value in
236
           the [0 - 63] range.
237
        */
238
0
        t_info.quality        = av_clipf(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
239
0
        t_info.target_bitrate = 0;
240
3.36k
    } else {
241
3.36k
        t_info.target_bitrate = avc_context->bit_rate;
242
3.36k
        t_info.quality        = 0;
243
3.36k
    }
244
245
    /* Now initialise libtheora */
246
3.36k
    h->t_state = th_encode_alloc(&t_info);
247
3.36k
    if (!h->t_state) {
248
0
        av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
249
0
        return AVERROR_EXTERNAL;
250
0
    }
251
252
3.36k
    h->keyframe_mask = (1U << av_ceil_log2(avc_context->gop_size)) - 1;
253
    /* Clear up theora_info struct */
254
3.36k
    th_info_clear(&t_info);
255
256
3.36k
    if (th_encode_ctl(h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
257
3.36k
                      &gop_size, sizeof(gop_size))) {
258
0
        av_log(avc_context, AV_LOG_ERROR, "Error setting GOP size\n");
259
0
        return AVERROR_EXTERNAL;
260
0
    }
261
262
    // Set encoding speed level
263
3.36k
    if (h->speed_level != -1) {
264
0
        int max_speed_level;
265
0
        int speed_level = h->speed_level;
266
0
        th_encode_ctl(h->t_state, TH_ENCCTL_GET_SPLEVEL_MAX, &max_speed_level, sizeof(max_speed_level));
267
0
        speed_level = FFMIN(speed_level, max_speed_level);
268
0
        th_encode_ctl(h->t_state, TH_ENCCTL_SET_SPLEVEL, &speed_level, sizeof(speed_level));
269
0
    }
270
271
    // need to enable 2 pass (via TH_ENCCTL_2PASS_) before encoding headers
272
3.36k
    if (avc_context->flags & AV_CODEC_FLAG_PASS1) {
273
0
        if ((ret = get_stats(avc_context, 0)) < 0)
274
0
            return ret;
275
3.36k
    } else if (avc_context->flags & AV_CODEC_FLAG_PASS2) {
276
0
        if ((ret = submit_stats(avc_context)) < 0)
277
0
            return ret;
278
0
    }
279
280
    /*
281
        Output first header packet consisting of theora
282
        header, comment, and tables.
283
284
        Each one is prefixed with a 16-bit size, then they
285
        are concatenated together into libavcodec's extradata.
286
    */
287
3.36k
    offset = 0;
288
289
    /* Headers */
290
3.36k
    th_comment_init(&t_comment);
291
292
13.4k
    while (th_encode_flushheader(h->t_state, &t_comment, &o_packet))
293
10.0k
        if ((ret = concatenate_packet(&offset, avc_context, &o_packet)) < 0)
294
0
            return ret;
295
296
3.36k
    th_comment_clear(&t_comment);
297
298
3.36k
    return 0;
299
3.36k
}
300
301
static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt,
302
                        const AVFrame *frame, int *got_packet)
303
46.8k
{
304
46.8k
    th_ycbcr_buffer t_yuv_buffer;
305
46.8k
    TheoraContext *h = avc_context->priv_data;
306
46.8k
    ogg_packet o_packet;
307
46.8k
    int result, i, ret;
308
309
    // EOS, finish and get 1st pass stats if applicable
310
46.8k
    if (!frame) {
311
3.36k
        th_encode_packetout(h->t_state, 1, &o_packet);
312
3.36k
        if (avc_context->flags & AV_CODEC_FLAG_PASS1)
313
0
            if ((ret = get_stats(avc_context, 1)) < 0)
314
0
                return ret;
315
3.36k
        return 0;
316
3.36k
    }
317
318
    /* Copy planes to the theora yuv_buffer */
319
173k
    for (i = 0; i < 3; i++) {
320
130k
        t_yuv_buffer[i].width  = FFALIGN(avc_context->width,  16) >> (i && h->uv_hshift);
321
130k
        t_yuv_buffer[i].height = FFALIGN(avc_context->height, 16) >> (i && h->uv_vshift);
322
130k
        t_yuv_buffer[i].stride = frame->linesize[i];
323
130k
        t_yuv_buffer[i].data   = frame->data[i];
324
130k
    }
325
326
43.4k
    if (avc_context->flags & AV_CODEC_FLAG_PASS2)
327
0
        if ((ret = submit_stats(avc_context)) < 0)
328
0
            return ret;
329
330
    /* Now call into theora_encode_YUVin */
331
43.4k
    result = th_encode_ycbcr_in(h->t_state, t_yuv_buffer);
332
43.4k
    if (result) {
333
0
        const char* message;
334
0
        switch (result) {
335
0
        case -1:
336
0
            message = "differing frame sizes";
337
0
            break;
338
0
        case TH_EINVAL:
339
0
            message = "encoder is not ready or is finished";
340
0
            break;
341
0
        default:
342
0
            message = "unknown reason";
343
0
            break;
344
0
        }
345
0
        av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
346
0
        return AVERROR_EXTERNAL;
347
0
    }
348
349
43.4k
    if (avc_context->flags & AV_CODEC_FLAG_PASS1)
350
0
        if ((ret = get_stats(avc_context, 0)) < 0)
351
0
            return ret;
352
353
    /* Pick up returned ogg_packet */
354
43.4k
    result = th_encode_packetout(h->t_state, 0, &o_packet);
355
43.4k
    switch (result) {
356
0
    case 0:
357
        /* No packet is ready */
358
0
        return 0;
359
43.4k
    case 1:
360
        /* Success, we have a packet */
361
43.4k
        break;
362
0
    default:
363
0
        av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
364
0
        return AVERROR_EXTERNAL;
365
43.4k
    }
366
367
    /* Copy ogg_packet content out to buffer */
368
43.4k
    if ((ret = ff_get_encode_buffer(avc_context, pkt, o_packet.bytes, 0)) < 0)
369
0
        return ret;
370
43.4k
    memcpy(pkt->data, o_packet.packet, o_packet.bytes);
371
372
    // HACK: assumes no encoder delay, this is true until libtheora becomes
373
    // multithreaded (which will be disabled unless explicitly requested)
374
43.4k
    pkt->pts = frame->pts;
375
43.4k
    pkt->duration = frame->duration;
376
377
43.4k
    ret = ff_encode_reordered_opaque(avc_context, pkt, frame);
378
43.4k
    if (ret < 0)
379
0
        return ret;
380
381
43.4k
    if (!(o_packet.granulepos & h->keyframe_mask))
382
22.1k
        pkt->flags |= AV_PKT_FLAG_KEY;
383
43.4k
    *got_packet = 1;
384
385
43.4k
    return 0;
386
43.4k
}
387
388
static av_cold int encode_close(AVCodecContext* avc_context)
389
3.36k
{
390
3.36k
    TheoraContext *h = avc_context->priv_data;
391
392
3.36k
    th_encode_free(h->t_state);
393
3.36k
    av_freep(&h->stats);
394
3.36k
    av_freep(&avc_context->stats_out);
395
3.36k
    avc_context->extradata_size = 0;
396
397
3.36k
    return 0;
398
3.36k
}
399
400
/** AVCodec struct exposed to libavcodec */
401
const FFCodec ff_libtheora_encoder = {
402
    .p.name         = "libtheora",
403
    CODEC_LONG_NAME("libtheora Theora"),
404
    .p.type         = AVMEDIA_TYPE_VIDEO,
405
    .p.id           = AV_CODEC_ID_THEORA,
406
    .p.capabilities = AV_CODEC_CAP_DR1 |
407
                      /* for statsfile summary */
408
                      AV_CODEC_CAP_DELAY |
409
                      AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
410
    .caps_internal  = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
411
    .priv_data_size = sizeof(TheoraContext),
412
    .init           = encode_init,
413
    .close          = encode_close,
414
    FF_CODEC_ENCODE_CB(encode_frame),
415
    CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P),
416
    .p.priv_class   = &theora_class,
417
    .color_ranges   = AVCOL_RANGE_MPEG,
418
    .p.wrapper_name = "libtheora",
419
};