Coverage Report

Created: 2026-09-28 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/bpg/libavcodec/utils.c
Line
Count
Source
1
/*
2
 * utils for libavcodec
3
 * Copyright (c) 2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * utils.
26
 */
27
28
#include "config.h"
29
#include "libavutil/atomic.h"
30
#include "libavutil/attributes.h"
31
#include "libavutil/avassert.h"
32
#include "libavutil/avstring.h"
33
#include "libavutil/bprint.h"
34
#include "libavutil/channel_layout.h"
35
#include "libavutil/crc.h"
36
#include "libavutil/frame.h"
37
#include "libavutil/internal.h"
38
#include "libavutil/mathematics.h"
39
#include "libavutil/pixdesc.h"
40
#include "libavutil/imgutils.h"
41
#include "libavutil/samplefmt.h"
42
#include "libavutil/dict.h"
43
#include "avcodec.h"
44
#include "libavutil/opt.h"
45
#include "thread.h"
46
#include "internal.h"
47
#include "bytestream.h"
48
#include "version.h"
49
#include <stdlib.h>
50
#include <stdarg.h>
51
#include <limits.h>
52
#include <float.h>
53
#if CONFIG_ICONV
54
# include <iconv.h>
55
#endif
56
57
void avcodec_register_all(void)
58
0
{
59
0
}
60
61
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
62
0
{
63
0
    int ret = 0;
64
65
0
    if (codec->priv_data_size > 0) {
66
0
        if (!avctx->priv_data) {
67
0
            avctx->priv_data = av_mallocz(codec->priv_data_size);
68
0
            if (!avctx->priv_data) {
69
0
                ret = AVERROR(ENOMEM);
70
0
                goto end;
71
0
            }
72
0
        }
73
0
    } else {
74
0
        avctx->priv_data = NULL;
75
0
    }
76
0
    avctx->codec = codec;
77
0
    avctx->frame_number = 0;
78
    //    avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
79
80
0
    avctx->thread_count = 1;
81
82
0
    avctx->pts_correction_num_faulty_pts =
83
0
    avctx->pts_correction_num_faulty_dts = 0;
84
0
    avctx->pts_correction_last_pts =
85
0
    avctx->pts_correction_last_dts = INT64_MIN;
86
87
0
    ret = avctx->codec->init(avctx);
88
0
    if (ret < 0) {
89
0
        goto free_and_end;
90
0
    }
91
92
0
    return 0;
93
0
free_and_end:
94
0
    av_freep(&avctx->priv_data);
95
0
    avctx->codec = NULL;
96
0
 end:
97
0
    return ret;
98
0
}
99
100
av_cold int avcodec_close(AVCodecContext *avctx)
101
0
{
102
0
    if (!avctx)
103
0
        return 0;
104
105
0
    if (avctx->codec && avctx->codec->close)
106
0
        avctx->codec->close(avctx);
107
0
    avctx->coded_frame = NULL;
108
109
0
    av_freep(&avctx->priv_data);
110
0
    avctx->codec = NULL;
111
0
    avctx->active_thread_type = 0;
112
113
0
    return 0;
114
0
}
115
116
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
117
0
{
118
0
    int i;
119
120
0
    for (i = 0; i < count; i++) {
121
0
        int r = func(c, (char *)arg + i * size);
122
0
        if (ret)
123
0
            ret[i] = r;
124
0
    }
125
0
    return 0;
126
0
}
127
128
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
129
0
{
130
0
    int i;
131
132
0
    for (i = 0; i < count; i++) {
133
0
        int r = func(c, arg, i, 0);
134
0
        if (ret)
135
0
            ret[i] = r;
136
0
    }
137
0
    return 0;
138
0
}
139
140
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
141
0
{
142
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
143
0
    int i, h, linesize, bpp;
144
145
0
    for (i = 0; i < desc->nb_components; i++) {
146
0
        bpp = (desc->comp[i].depth_minus1 + 8) >> 3;
147
0
        linesize = FFALIGN(frame->width * bpp, 32);
148
0
        if (i == 1 || i == 2)
149
0
            linesize = FF_CEIL_RSHIFT(linesize, desc->log2_chroma_w);
150
151
0
        frame->linesize[i] = linesize;
152
153
0
        h = FFALIGN(frame->height, 32);
154
0
        if (i == 1 || i == 2)
155
0
            h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
156
        
157
0
        frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 32);
158
0
        if (!frame->buf[i])
159
0
            goto fail;
160
161
0
        frame->data[i] = frame->buf[i]->data;
162
0
    }
163
0
    return 0;
164
0
 fail:
165
0
    return -1;
166
0
}
167
168
int avcodec_get_context_defaults3(AVCodecContext *s, const AVCodec *codec)
169
0
{
170
0
    memset(s, 0, sizeof(AVCodecContext));
171
172
0
    s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
173
0
    if (codec)
174
0
        s->codec_id = codec->id;
175
176
0
    s->time_base           = (AVRational){0,1};
177
0
    s->framerate           = (AVRational){ 0, 1 };
178
0
    s->pkt_timebase        = (AVRational){ 0, 1 };
179
0
    s->get_buffer2         = avcodec_default_get_buffer2;
180
    //    s->get_format          = avcodec_default_get_format;
181
0
    s->execute             = avcodec_default_execute;
182
0
    s->execute2            = avcodec_default_execute2;
183
0
    s->sample_aspect_ratio = (AVRational){0,1};
184
0
    s->pix_fmt             = AV_PIX_FMT_NONE;
185
0
    s->sample_fmt          = AV_SAMPLE_FMT_NONE;
186
187
0
    s->reordered_opaque    = AV_NOPTS_VALUE;
188
0
    if(codec && codec->priv_data_size){
189
0
        if(!s->priv_data){
190
0
            s->priv_data= av_mallocz(codec->priv_data_size);
191
0
            if (!s->priv_data) {
192
0
                return AVERROR(ENOMEM);
193
0
            }
194
0
        }
195
0
    }
196
0
    return 0;
197
0
}
198
199
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
200
0
{
201
0
    return 0;
202
0
}
203
204
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)
205
0
{
206
0
    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
207
208
0
    if (!avctx)
209
0
        return NULL;
210
211
0
    if(avcodec_get_context_defaults3(avctx, codec) < 0){
212
0
        av_free(avctx);
213
0
        return NULL;
214
0
    }
215
216
0
    return avctx;
217
0
}
218
219
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
220
                                              int *got_picture_ptr,
221
                                              const AVPacket *avpkt)
222
0
{
223
0
    int ret;
224
    // copy to ensure we do not change avpkt
225
0
    AVPacket tmp = *avpkt;
226
227
0
    if (!avctx->codec)
228
0
        return AVERROR(EINVAL);
229
0
    if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
230
0
        av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
231
0
        return AVERROR(EINVAL);
232
0
    }
233
234
0
    *got_picture_ptr = 0;
235
0
    if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
236
0
        return AVERROR(EINVAL);
237
238
0
    av_frame_unref(picture);
239
240
0
    if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
241
242
0
        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
243
0
                                   &tmp);
244
245
0
        if (*got_picture_ptr) {
246
0
            avctx->frame_number++;
247
0
        } else {
248
0
            av_frame_unref(picture);
249
0
        }
250
0
    } else {
251
0
        ret = 0;
252
0
    }
253
254
    /* many decoders assign whole AVFrames, thus overwriting extended_data;
255
     * make sure it's set correctly */
256
    //    av_assert0(!picture->extended_data || picture->extended_data == picture->data);
257
258
0
    return ret;
259
0
}
260
261
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
262
0
{
263
0
    if ((int)w>0 && (int)h>0 && (w+128) < (INT_MAX/8) / (h + 128))
264
0
        return 0;
265
0
    else
266
0
        return AVERROR(EINVAL);
267
0
}
268
269
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
270
0
{
271
0
    return 0;
272
0
}
273
274
static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
275
0
{
276
0
    int override_dimensions = 1;
277
0
    int ret;
278
279
0
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
280
0
        if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
281
0
            av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
282
0
            return AVERROR(EINVAL);
283
0
        }
284
0
    }
285
0
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
286
0
        if (frame->width <= 0 || frame->height <= 0) {
287
0
            frame->width  = FFMAX(avctx->width,  FF_CEIL_RSHIFT(avctx->coded_width,  avctx->lowres));
288
0
            frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
289
0
            override_dimensions = 0;
290
0
        }
291
0
        frame->format              = avctx->pix_fmt;
292
0
    }
293
0
    ret = avctx->get_buffer2(avctx, frame, flags);
294
295
0
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
296
0
        frame->width  = avctx->width;
297
0
        frame->height = avctx->height;
298
0
    }
299
300
0
    return ret;
301
0
}
302
303
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
304
0
{
305
0
    int ret = get_buffer_internal(avctx, frame, flags);
306
0
    if (ret < 0)
307
0
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
308
0
    return ret;
309
0
}
310
311
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
312
0
{
313
0
    f->owner = avctx;
314
0
    return ff_get_buffer(avctx, f->f, flags);
315
0
}
316
317
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
318
0
{
319
0
    if (f->f)
320
0
        av_frame_unref(f->f);
321
0
}
322
323
void ff_thread_finish_setup(AVCodecContext *avctx)
324
0
{
325
0
}
326
327
void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
328
0
{
329
0
}
330
331
void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
332
0
{
333
0
}
334
335
int ff_thread_can_start_frame(AVCodecContext *avctx)
336
0
{
337
0
    return 1;
338
0
}
339
340
int ff_alloc_entries(AVCodecContext *avctx, int count)
341
0
{
342
0
    return 0;
343
0
}
344
345
void ff_reset_entries(AVCodecContext *avctx)
346
0
{
347
0
}
348
349
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
350
0
{
351
0
}
352
353
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
354
0
{
355
0
}
356
357
void av_init_packet(AVPacket *pkt)
358
0
{
359
0
    pkt->pts                  = AV_NOPTS_VALUE;
360
0
    pkt->dts                  = AV_NOPTS_VALUE;
361
0
    pkt->pos                  = -1;
362
0
    pkt->duration             = 0;
363
0
    pkt->convergence_duration = 0;
364
0
    pkt->flags                = 0;
365
0
    pkt->stream_index         = 0;
366
0
    pkt->buf                  = NULL;
367
    pkt->side_data            = NULL;
368
0
    pkt->side_data_elems      = 0;
369
0
}