Coverage Report

Created: 2026-10-28 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/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/avassert.h"
30
#include "libavutil/channel_layout.h"
31
#include "libavutil/intreadwrite.h"
32
#include "libavutil/mem.h"
33
#include "libavutil/pixdesc.h"
34
#include "libavutil/imgutils.h"
35
#include "libavutil/pixfmt.h"
36
#include "libavutil/timecode_internal.h"
37
#include "avcodec.h"
38
#include "codec.h"
39
#include "codec_desc.h"
40
#include "codec_internal.h"
41
#include "codec_par.h"
42
#include "decode.h"
43
#include "hwconfig.h"
44
#include "libavutil/refstruct.h"
45
#include "thread.h"
46
#include "threadframe.h"
47
#include "internal.h"
48
#include "put_bits.h"
49
#include "startcode.h"
50
#include <stdlib.h>
51
#include <limits.h>
52
53
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
54
0
{
55
0
    uint8_t **p = ptr;
56
0
    if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
57
0
        av_freep(p);
58
0
        *size = 0;
59
0
        return;
60
0
    }
61
0
    av_fast_mallocz(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
62
0
    if (*p)
63
0
        memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
64
0
}
65
66
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
67
0
{
68
0
    uint8_t **p = ptr;
69
0
    if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
70
0
        av_freep(p);
71
0
        *size = 0;
72
0
        return;
73
0
    }
74
0
    av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
75
0
    if (*p)
76
0
        memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
77
0
}
78
79
int av_codec_is_encoder(const AVCodec *avcodec)
80
0
{
81
0
    const FFCodec *const codec = ffcodec(avcodec);
82
0
    return codec && !codec->is_decoder;
83
0
}
84
85
int av_codec_is_decoder(const AVCodec *avcodec)
86
0
{
87
0
    const FFCodec *const codec = ffcodec(avcodec);
88
0
    return codec && codec->is_decoder;
89
0
}
90
91
int ff_set_dimensions(AVCodecContext *s, int width, int height)
92
0
{
93
0
    int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
94
95
0
    if (ret < 0)
96
0
        width = height = 0;
97
98
0
    s->coded_width  = width;
99
0
    s->coded_height = height;
100
0
    s->width        = AV_CEIL_RSHIFT(width,  s->lowres);
101
0
    s->height       = AV_CEIL_RSHIFT(height, s->lowres);
102
103
0
    return ret;
104
0
}
105
106
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
107
0
{
108
0
    int ret = av_image_check_sar(avctx->width, avctx->height, sar);
109
110
0
    if (ret < 0) {
111
0
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
112
0
               sar.num, sar.den);
113
0
        avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
114
0
        return ret;
115
0
    } else {
116
0
        avctx->sample_aspect_ratio = sar;
117
0
    }
118
0
    return 0;
119
0
}
120
121
int ff_side_data_update_matrix_encoding(AVFrame *frame,
122
                                        enum AVMatrixEncoding matrix_encoding)
123
0
{
124
0
    AVFrameSideData *side_data;
125
0
    enum AVMatrixEncoding *data;
126
127
0
    side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
128
0
    if (!side_data)
129
0
        side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
130
0
                                           sizeof(enum AVMatrixEncoding));
131
132
0
    if (!side_data)
133
0
        return AVERROR(ENOMEM);
134
135
0
    data  = (enum AVMatrixEncoding*)side_data->data;
136
0
    *data = matrix_encoding;
137
138
0
    return 0;
139
0
}
140
141
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
142
                               int linesize_align[AV_NUM_DATA_POINTERS])
143
0
{
144
0
    int i;
145
0
    int w_align = 1;
146
0
    int h_align = 1;
147
0
    AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
148
149
0
    if (desc) {
150
0
        w_align = 1 << desc->log2_chroma_w;
151
0
        h_align = 1 << desc->log2_chroma_h;
152
0
    }
153
154
0
    switch (s->pix_fmt) {
155
0
    case AV_PIX_FMT_YUV420P:
156
0
    case AV_PIX_FMT_YUYV422:
157
0
    case AV_PIX_FMT_YVYU422:
158
0
    case AV_PIX_FMT_UYVY422:
159
0
    case AV_PIX_FMT_YUV422P:
160
0
    case AV_PIX_FMT_YUV440P:
161
0
    case AV_PIX_FMT_YUV444P:
162
0
    case AV_PIX_FMT_GBRP:
163
0
    case AV_PIX_FMT_GBRAP:
164
0
    case AV_PIX_FMT_GRAY8:
165
0
    case AV_PIX_FMT_GRAY16BE:
166
0
    case AV_PIX_FMT_GRAY16LE:
167
0
    case AV_PIX_FMT_YUVJ420P:
168
0
    case AV_PIX_FMT_YUVJ422P:
169
0
    case AV_PIX_FMT_YUVJ440P:
170
0
    case AV_PIX_FMT_YUVJ444P:
171
0
    case AV_PIX_FMT_YUVA420P:
172
0
    case AV_PIX_FMT_YUVA422P:
173
0
    case AV_PIX_FMT_YUVA444P:
174
0
    case AV_PIX_FMT_YUV420P9LE:
175
0
    case AV_PIX_FMT_YUV420P9BE:
176
0
    case AV_PIX_FMT_YUV420P10LE:
177
0
    case AV_PIX_FMT_YUV420P10BE:
178
0
    case AV_PIX_FMT_YUV420P12LE:
179
0
    case AV_PIX_FMT_YUV420P12BE:
180
0
    case AV_PIX_FMT_YUV420P14LE:
181
0
    case AV_PIX_FMT_YUV420P14BE:
182
0
    case AV_PIX_FMT_YUV420P16LE:
183
0
    case AV_PIX_FMT_YUV420P16BE:
184
0
    case AV_PIX_FMT_YUVA420P9LE:
185
0
    case AV_PIX_FMT_YUVA420P9BE:
186
0
    case AV_PIX_FMT_YUVA420P10LE:
187
0
    case AV_PIX_FMT_YUVA420P10BE:
188
0
    case AV_PIX_FMT_YUVA420P16LE:
189
0
    case AV_PIX_FMT_YUVA420P16BE:
190
0
    case AV_PIX_FMT_YUV422P9LE:
191
0
    case AV_PIX_FMT_YUV422P9BE:
192
0
    case AV_PIX_FMT_YUV422P10LE:
193
0
    case AV_PIX_FMT_YUV422P10BE:
194
0
    case AV_PIX_FMT_YUV422P12LE:
195
0
    case AV_PIX_FMT_YUV422P12BE:
196
0
    case AV_PIX_FMT_YUV422P14LE:
197
0
    case AV_PIX_FMT_YUV422P14BE:
198
0
    case AV_PIX_FMT_YUV422P16LE:
199
0
    case AV_PIX_FMT_YUV422P16BE:
200
0
    case AV_PIX_FMT_YUVA422P9LE:
201
0
    case AV_PIX_FMT_YUVA422P9BE:
202
0
    case AV_PIX_FMT_YUVA422P10LE:
203
0
    case AV_PIX_FMT_YUVA422P10BE:
204
0
    case AV_PIX_FMT_YUVA422P12LE:
205
0
    case AV_PIX_FMT_YUVA422P12BE:
206
0
    case AV_PIX_FMT_YUVA422P16LE:
207
0
    case AV_PIX_FMT_YUVA422P16BE:
208
0
    case AV_PIX_FMT_YUV440P10LE:
209
0
    case AV_PIX_FMT_YUV440P10BE:
210
0
    case AV_PIX_FMT_YUV440P12LE:
211
0
    case AV_PIX_FMT_YUV440P12BE:
212
0
    case AV_PIX_FMT_YUV444P9LE:
213
0
    case AV_PIX_FMT_YUV444P9BE:
214
0
    case AV_PIX_FMT_YUV444P10LE:
215
0
    case AV_PIX_FMT_YUV444P10BE:
216
0
    case AV_PIX_FMT_YUV444P12LE:
217
0
    case AV_PIX_FMT_YUV444P12BE:
218
0
    case AV_PIX_FMT_YUV444P14LE:
219
0
    case AV_PIX_FMT_YUV444P14BE:
220
0
    case AV_PIX_FMT_YUV444P16LE:
221
0
    case AV_PIX_FMT_YUV444P16BE:
222
0
    case AV_PIX_FMT_YUVA444P9LE:
223
0
    case AV_PIX_FMT_YUVA444P9BE:
224
0
    case AV_PIX_FMT_YUVA444P10LE:
225
0
    case AV_PIX_FMT_YUVA444P10BE:
226
0
    case AV_PIX_FMT_YUVA444P12LE:
227
0
    case AV_PIX_FMT_YUVA444P12BE:
228
0
    case AV_PIX_FMT_YUVA444P16LE:
229
0
    case AV_PIX_FMT_YUVA444P16BE:
230
0
    case AV_PIX_FMT_GBRP9LE:
231
0
    case AV_PIX_FMT_GBRP9BE:
232
0
    case AV_PIX_FMT_GBRP10LE:
233
0
    case AV_PIX_FMT_GBRP10BE:
234
0
    case AV_PIX_FMT_GBRP12LE:
235
0
    case AV_PIX_FMT_GBRP12BE:
236
0
    case AV_PIX_FMT_GBRP14LE:
237
0
    case AV_PIX_FMT_GBRP14BE:
238
0
    case AV_PIX_FMT_GBRP16LE:
239
0
    case AV_PIX_FMT_GBRP16BE:
240
0
    case AV_PIX_FMT_GBRAP12LE:
241
0
    case AV_PIX_FMT_GBRAP12BE:
242
0
    case AV_PIX_FMT_GBRAP16LE:
243
0
    case AV_PIX_FMT_GBRAP16BE:
244
0
        w_align = 16; //FIXME assume 16 pixel per macroblock
245
0
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
246
0
        if (s->codec_id == AV_CODEC_ID_BINKVIDEO)
247
0
            w_align = 16*2;
248
0
        break;
249
0
    case AV_PIX_FMT_YUV411P:
250
0
    case AV_PIX_FMT_YUVJ411P:
251
0
    case AV_PIX_FMT_UYYVYY411:
252
0
        w_align = 32;
253
0
        h_align = 16 * 2;
254
0
        break;
255
0
    case AV_PIX_FMT_YUV410P:
256
0
        if (s->codec_id == AV_CODEC_ID_SVQ1) {
257
0
            w_align = 64;
258
0
            h_align = 64;
259
0
        } else if (s->codec_id == AV_CODEC_ID_SNOW) {
260
0
            w_align = 16;
261
0
            h_align = 16;
262
0
        }
263
0
        break;
264
0
    case AV_PIX_FMT_RGB555:
265
0
        if (s->codec_id == AV_CODEC_ID_RPZA) {
266
0
            w_align = 4;
267
0
            h_align = 4;
268
0
        }
269
0
        if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
270
0
            w_align = 8;
271
0
            h_align = 8;
272
0
        }
273
0
        break;
274
0
    case AV_PIX_FMT_PAL8:
275
0
    case AV_PIX_FMT_BGR8:
276
0
    case AV_PIX_FMT_RGB8:
277
0
        if (s->codec_id == AV_CODEC_ID_SMC ||
278
0
            s->codec_id == AV_CODEC_ID_CINEPAK) {
279
0
            w_align = 4;
280
0
            h_align = 4;
281
0
        }
282
0
        if (s->codec_id == AV_CODEC_ID_JV ||
283
0
            s->codec_id == AV_CODEC_ID_ARGO ||
284
0
            s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
285
0
            w_align = 8;
286
0
            h_align = 8;
287
0
        }
288
0
        if (s->codec_id == AV_CODEC_ID_MJPEG   ||
289
0
            s->codec_id == AV_CODEC_ID_MJPEGB  ||
290
0
            s->codec_id == AV_CODEC_ID_LJPEG   ||
291
0
            s->codec_id == AV_CODEC_ID_SMVJPEG ||
292
0
            s->codec_id == AV_CODEC_ID_AMV     ||
293
0
            s->codec_id == AV_CODEC_ID_SP5X    ||
294
0
            s->codec_id == AV_CODEC_ID_JPEGLS) {
295
0
            w_align =   8;
296
0
            h_align = 2*8;
297
0
        }
298
0
        break;
299
0
    case AV_PIX_FMT_BGR24:
300
0
        if ((s->codec_id == AV_CODEC_ID_MSZH) ||
301
0
            (s->codec_id == AV_CODEC_ID_ZLIB)) {
302
0
            w_align = 4;
303
0
            h_align = 4;
304
0
        }
305
0
        break;
306
0
    case AV_PIX_FMT_RGB24:
307
0
        if (s->codec_id == AV_CODEC_ID_CINEPAK) {
308
0
            w_align = 4;
309
0
            h_align = 4;
310
0
        }
311
0
        break;
312
0
    case AV_PIX_FMT_BGR0:
313
0
        if (s->codec_id == AV_CODEC_ID_ARGO) {
314
0
            w_align = 8;
315
0
            h_align = 8;
316
0
        }
317
0
        break;
318
0
    case AV_PIX_FMT_BAYER_BGGR8:
319
0
    case AV_PIX_FMT_BAYER_RGGB8:
320
0
    case AV_PIX_FMT_BAYER_GBRG8:
321
0
    case AV_PIX_FMT_BAYER_GRBG8:
322
0
    case AV_PIX_FMT_BAYER_BGGR16LE:
323
0
    case AV_PIX_FMT_BAYER_BGGR16BE:
324
0
    case AV_PIX_FMT_BAYER_RGGB16LE:
325
0
    case AV_PIX_FMT_BAYER_RGGB16BE:
326
0
    case AV_PIX_FMT_BAYER_GBRG16LE:
327
0
    case AV_PIX_FMT_BAYER_GBRG16BE:
328
0
    case AV_PIX_FMT_BAYER_GRBG16LE:
329
0
    case AV_PIX_FMT_BAYER_GRBG16BE:
330
0
        w_align = FFMAX(w_align, 2);
331
0
        h_align = FFMAX(h_align, 2);
332
0
        break;
333
0
    default:
334
0
        break;
335
0
    }
336
337
0
    if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
338
0
        w_align = FFMAX(w_align, 16);
339
0
    }
340
341
0
    *width  = FFALIGN(*width, w_align);
342
0
    *height = FFALIGN(*height, h_align);
343
0
    if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
344
0
        s->codec_id == AV_CODEC_ID_VC1  || s->codec_id == AV_CODEC_ID_WMV3 ||
345
0
        s->codec_id == AV_CODEC_ID_VP5  || s->codec_id == AV_CODEC_ID_VP6 ||
346
0
        s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
347
0
    ) {
348
        // some of the optimized chroma MC reads one line too much
349
        // which is also done in mpeg decoders with lowres > 0
350
0
        *height += 2;
351
352
        // H.264 uses edge emulation for out of frame motion vectors, for this
353
        // it requires a temporary area large enough to hold a 21x21 block,
354
        // increasing width ensure that the temporary area is large enough,
355
        // the next rounded up width is 32
356
0
        *width = FFMAX(*width, 32);
357
0
    }
358
0
    if (s->codec_id == AV_CODEC_ID_SVQ3) {
359
0
        *width = FFMAX(*width, 32);
360
0
    }
361
362
0
    for (i = 0; i < 4; i++)
363
0
        linesize_align[i] = STRIDE_ALIGN;
364
0
}
365
366
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
367
0
{
368
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
369
0
    int chroma_shift = desc->log2_chroma_w;
370
0
    int linesize_align[AV_NUM_DATA_POINTERS];
371
0
    int align;
372
373
0
    avcodec_align_dimensions2(s, width, height, linesize_align);
374
0
    align               = FFMAX(linesize_align[0], linesize_align[3]);
375
0
    linesize_align[1] <<= chroma_shift;
376
0
    linesize_align[2] <<= chroma_shift;
377
0
    align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
378
0
    *width              = FFALIGN(*width, align);
379
0
}
380
381
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
382
                             enum AVSampleFormat sample_fmt, const uint8_t *buf,
383
                             int buf_size, int align)
384
0
{
385
0
    int ch, planar, needed_size, ret = 0;
386
387
0
    needed_size = av_samples_get_buffer_size(NULL, nb_channels,
388
0
                                             frame->nb_samples, sample_fmt,
389
0
                                             align);
390
0
    if (buf_size < needed_size)
391
0
        return AVERROR(EINVAL);
392
393
0
    planar = av_sample_fmt_is_planar(sample_fmt);
394
0
    if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
395
0
        if (!FF_ALLOCZ_TYPED_ARRAY(frame->extended_data, nb_channels))
396
0
            return AVERROR(ENOMEM);
397
0
    } else {
398
0
        frame->extended_data = frame->data;
399
0
    }
400
401
0
    if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
402
0
                                      (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
403
0
                                      sample_fmt, align)) < 0) {
404
0
        if (frame->extended_data != frame->data)
405
0
            av_freep(&frame->extended_data);
406
0
        return ret;
407
0
    }
408
0
    if (frame->extended_data != frame->data) {
409
0
        for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
410
0
            frame->data[ch] = frame->extended_data[ch];
411
0
    }
412
413
0
    return ret;
414
0
}
415
416
417
0
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
418
0
    return !!(ffcodec(codec)->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
419
0
}
420
421
const char *avcodec_get_name(enum AVCodecID id)
422
0
{
423
0
    const AVCodecDescriptor *cd;
424
425
0
    if (id == AV_CODEC_ID_NONE)
426
0
        return "none";
427
0
    cd = avcodec_descriptor_get(id);
428
0
    if (cd)
429
0
        return cd->name;
430
0
    return "unknown_codec";
431
0
}
432
433
const char *av_get_profile_name(const AVCodec *codec, int profile)
434
0
{
435
0
    const AVProfile *p;
436
0
    if (profile == AV_PROFILE_UNKNOWN || !codec->profiles)
437
0
        return NULL;
438
439
0
    for (p = codec->profiles; p->profile != AV_PROFILE_UNKNOWN; p++)
440
0
        if (p->profile == profile)
441
0
            return p->name;
442
443
0
    return NULL;
444
0
}
445
446
const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
447
0
{
448
0
    const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
449
0
    const AVProfile *p;
450
451
0
    if (profile == AV_PROFILE_UNKNOWN || !desc || !desc->profiles)
452
0
        return NULL;
453
454
0
    for (p = desc->profiles; p->profile != AV_PROFILE_UNKNOWN; p++)
455
0
        if (p->profile == profile)
456
0
            return p->name;
457
458
0
    return NULL;
459
0
}
460
461
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
462
0
{
463
0
    switch (codec_id) {
464
0
    case AV_CODEC_ID_DFPWM:
465
0
        return 1;
466
0
    case AV_CODEC_ID_8SVX_EXP:
467
0
    case AV_CODEC_ID_8SVX_FIB:
468
0
    case AV_CODEC_ID_ADPCM_ARGO:
469
0
    case AV_CODEC_ID_ADPCM_CT:
470
0
    case AV_CODEC_ID_ADPCM_IMA_ALP:
471
0
    case AV_CODEC_ID_ADPCM_IMA_AMV:
472
0
    case AV_CODEC_ID_ADPCM_IMA_APC:
473
0
    case AV_CODEC_ID_ADPCM_IMA_APM:
474
0
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
475
0
    case AV_CODEC_ID_ADPCM_IMA_MAGIX:
476
0
    case AV_CODEC_ID_ADPCM_IMA_OKI:
477
0
    case AV_CODEC_ID_ADPCM_IMA_WS:
478
0
    case AV_CODEC_ID_ADPCM_IMA_SSI:
479
0
    case AV_CODEC_ID_ADPCM_G722:
480
0
    case AV_CODEC_ID_ADPCM_YAMAHA:
481
0
    case AV_CODEC_ID_ADPCM_AICA:
482
0
    case AV_CODEC_ID_ADPCM_RHETOREX:
483
0
        return 4;
484
0
    case AV_CODEC_ID_DSD_LSBF:
485
0
    case AV_CODEC_ID_DSD_MSBF:
486
0
    case AV_CODEC_ID_DSD_LSBF_PLANAR:
487
0
    case AV_CODEC_ID_DSD_MSBF_PLANAR:
488
0
    case AV_CODEC_ID_PCM_ALAW:
489
0
    case AV_CODEC_ID_PCM_MULAW:
490
0
    case AV_CODEC_ID_PCM_VIDC:
491
0
    case AV_CODEC_ID_PCM_S8:
492
0
    case AV_CODEC_ID_PCM_S8_PLANAR:
493
0
    case AV_CODEC_ID_PCM_SGA:
494
0
    case AV_CODEC_ID_PCM_U8:
495
0
    case AV_CODEC_ID_SDX2_DPCM:
496
0
    case AV_CODEC_ID_CBD2_DPCM:
497
0
    case AV_CODEC_ID_DERF_DPCM:
498
0
    case AV_CODEC_ID_WADY_DPCM:
499
0
    case AV_CODEC_ID_ADPCM_CIRCUS:
500
0
        return 8;
501
0
    case AV_CODEC_ID_PCM_S16BE:
502
0
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
503
0
    case AV_CODEC_ID_PCM_S16LE:
504
0
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
505
0
    case AV_CODEC_ID_PCM_U16BE:
506
0
    case AV_CODEC_ID_PCM_U16LE:
507
0
        return 16;
508
0
    case AV_CODEC_ID_PCM_S24DAUD:
509
0
    case AV_CODEC_ID_PCM_S24BE:
510
0
    case AV_CODEC_ID_PCM_S24LE:
511
0
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
512
0
    case AV_CODEC_ID_PCM_U24BE:
513
0
    case AV_CODEC_ID_PCM_U24LE:
514
0
        return 24;
515
0
    case AV_CODEC_ID_PCM_S32BE:
516
0
    case AV_CODEC_ID_PCM_S32LE:
517
0
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
518
0
    case AV_CODEC_ID_PCM_U32BE:
519
0
    case AV_CODEC_ID_PCM_U32LE:
520
0
    case AV_CODEC_ID_PCM_F32BE:
521
0
    case AV_CODEC_ID_PCM_F32LE:
522
0
    case AV_CODEC_ID_PCM_F24LE:
523
0
    case AV_CODEC_ID_PCM_F16LE:
524
0
        return 32;
525
0
    case AV_CODEC_ID_PCM_F64BE:
526
0
    case AV_CODEC_ID_PCM_F64LE:
527
0
    case AV_CODEC_ID_PCM_S64BE:
528
0
    case AV_CODEC_ID_PCM_S64LE:
529
0
        return 64;
530
0
    default:
531
0
        return 0;
532
0
    }
533
0
}
534
535
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
536
0
{
537
0
    static const enum AVCodecID map[][2] = {
538
0
        [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
539
0
        [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
540
0
        [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
541
0
        [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
542
0
        [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
543
0
        [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
544
0
        [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
545
0
        [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
546
0
        [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
547
0
        [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
548
0
        [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
549
0
    };
550
0
    if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
551
0
        return AV_CODEC_ID_NONE;
552
0
    if (be < 0 || be > 1)
553
0
        be = AV_NE(1, 0);
554
0
    return map[fmt][be];
555
0
}
556
557
int av_get_bits_per_sample(enum AVCodecID codec_id)
558
0
{
559
0
    switch (codec_id) {
560
0
    case AV_CODEC_ID_DFPWM:
561
0
        return 1;
562
0
    case AV_CODEC_ID_ADPCM_IMA_CITRIX:
563
0
    case AV_CODEC_ID_ADPCM_SBPRO_2:
564
0
    case AV_CODEC_ID_G728:
565
0
        return 2;
566
0
    case AV_CODEC_ID_ADPCM_SBPRO_3:
567
0
        return 3;
568
0
    case AV_CODEC_ID_ADPCM_SBPRO_4:
569
0
    case AV_CODEC_ID_ADPCM_IMA_WAV:
570
0
    case AV_CODEC_ID_ADPCM_IMA_XBOX:
571
0
    case AV_CODEC_ID_ADPCM_IMA_QT:
572
0
    case AV_CODEC_ID_ADPCM_SWF:
573
0
    case AV_CODEC_ID_ADPCM_MS:
574
0
        return 4;
575
0
    default:
576
0
        return av_get_exact_bits_per_sample(codec_id);
577
0
    }
578
0
}
579
580
static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
581
                                    uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
582
                                    uint8_t * extradata, int frame_size, int frame_bytes)
583
0
{
584
0
    int bps = av_get_exact_bits_per_sample(id);
585
0
    int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
586
587
    /* codecs with an exact constant bits per sample */
588
0
    if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
589
0
        return (frame_bytes * 8LL) / (bps * ch);
590
0
    bps = bits_per_coded_sample;
591
592
    /* codecs with a fixed packet duration */
593
0
    switch (id) {
594
0
    case AV_CODEC_ID_ADPCM_ADX:    return   32;
595
0
    case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
596
0
    case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
597
0
    case AV_CODEC_ID_AMR_NB:
598
0
    case AV_CODEC_ID_EVRC:
599
0
    case AV_CODEC_ID_GSM:
600
0
    case AV_CODEC_ID_QCELP:
601
0
    case AV_CODEC_ID_RA_288:       return  160;
602
0
    case AV_CODEC_ID_AMR_WB:
603
0
    case AV_CODEC_ID_GSM_MS:       return  320;
604
0
    case AV_CODEC_ID_MP1:          return  384;
605
0
    case AV_CODEC_ID_ATRAC1:       return  512;
606
0
    case AV_CODEC_ID_ATRAC9:
607
0
    case AV_CODEC_ID_ATRAC3:
608
0
        if (framecount > INT_MAX/1024)
609
0
            return 0;
610
0
        return 1024 * framecount;
611
0
    case AV_CODEC_ID_ATRAC3P:      return 2048;
612
0
    case AV_CODEC_ID_MP2:
613
0
    case AV_CODEC_ID_MUSEPACK7:    return 1152;
614
0
    case AV_CODEC_ID_AC3:          return 1536;
615
0
    case AV_CODEC_ID_FTR:          return 1024;
616
0
    }
617
618
0
    if (sr > 0) {
619
        /* calc from sample rate */
620
0
        if (id == AV_CODEC_ID_TTA)
621
0
            return 256ll * sr / 245;
622
0
        else if (id == AV_CODEC_ID_DST)
623
0
            return 588ll * sr / 44100;
624
0
        else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
625
0
            if (sr / 22050 > 22)
626
0
                return 0;
627
0
            return (480 << (sr / 22050));
628
0
        }
629
630
0
        if (id == AV_CODEC_ID_MP3)
631
0
            return sr <= 24000 ? 576 : 1152;
632
0
    }
633
634
0
    if (ba > 0) {
635
        /* calc from block_align */
636
0
        if (id == AV_CODEC_ID_SIPR) {
637
0
            switch (ba) {
638
0
            case 20: return 160;
639
0
            case 19: return 144;
640
0
            case 29: return 288;
641
0
            case 37: return 480;
642
0
            }
643
0
        } else if (id == AV_CODEC_ID_ILBC) {
644
0
            switch (ba) {
645
0
            case 38: return 160;
646
0
            case 50: return 240;
647
0
            }
648
0
        }
649
0
    }
650
651
0
    if (frame_bytes > 0) {
652
        /* calc from frame_bytes only */
653
0
        int64_t d = INT64_MIN;
654
0
        switch(id) {
655
0
        case AV_CODEC_ID_TRUESPEECH : d = 240LL * (frame_bytes / 32); break;
656
0
        case AV_CODEC_ID_NELLYMOSER : d = 256LL * (frame_bytes / 64); break;
657
0
        case AV_CODEC_ID_RA_144     : d = 160LL * (frame_bytes / 20); break;
658
0
        case AV_CODEC_ID_APTX       : d =   4LL * (frame_bytes /  4); break;
659
0
        case AV_CODEC_ID_APTX_HD    : d =   4LL * (frame_bytes /  6); break;
660
0
        }
661
0
        if (d > INT64_MIN)
662
0
            return ((int)d == d && d > 0) ? d : 0;
663
664
0
        if (bps > 0) {
665
            /* calc from frame_bytes and bits_per_coded_sample */
666
0
            if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
667
0
                return frame_bytes * 8 / bps;
668
0
        }
669
670
0
        if (ch > 0 && ch < INT_MAX/16) {
671
            /* calc from frame_bytes and channels */
672
0
            switch (id) {
673
0
            case AV_CODEC_ID_FASTAUDIO:
674
0
                return frame_bytes / (40 * ch) * 256;
675
0
            case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
676
0
                return (frame_bytes - 4 * ch) / (128 * ch) * 256;
677
0
            case AV_CODEC_ID_ADPCM_AFC:
678
0
                return frame_bytes / (9 * ch) * 16;
679
0
            case AV_CODEC_ID_ADPCM_N64:
680
0
                frame_bytes /= 9 * ch;
681
0
                if (frame_bytes > INT_MAX / 16)
682
0
                    return 0;
683
0
                return frame_bytes * 16;
684
0
            case AV_CODEC_ID_ADPCM_PSX:
685
0
            case AV_CODEC_ID_ADPCM_DTK:
686
0
                frame_bytes /= 16 * ch;
687
0
                if (frame_bytes > INT_MAX / 28)
688
0
                    return 0;
689
0
                return frame_bytes * 28;
690
0
            case AV_CODEC_ID_ADPCM_PSXC:
691
0
                frame_bytes = (frame_bytes - 1) / ch;
692
0
                if (frame_bytes > INT_MAX / 2)
693
0
                    return 0;
694
0
                return frame_bytes * 2;
695
0
            case AV_CODEC_ID_ADPCM_4XM:
696
0
            case AV_CODEC_ID_ADPCM_IMA_ACORN:
697
0
            case AV_CODEC_ID_ADPCM_IMA_DAT4:
698
0
            case AV_CODEC_ID_ADPCM_IMA_ISS:
699
0
            case AV_CODEC_ID_ADPCM_IMA_PDA:
700
0
                return (frame_bytes - 4 * ch) * 2 / ch;
701
0
            case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
702
0
                return (frame_bytes - 4) * 2 / ch;
703
0
            case AV_CODEC_ID_ADPCM_IMA_AMV:
704
0
                return (frame_bytes - 8) * 2;
705
0
            case AV_CODEC_ID_ADPCM_THP:
706
0
            case AV_CODEC_ID_ADPCM_THP_LE:
707
0
                if (extradata)
708
0
                    return frame_bytes * 14LL / (8 * ch);
709
0
                break;
710
0
            case AV_CODEC_ID_ADPCM_XA:
711
0
                return (frame_bytes / 128) * 224 / ch;
712
0
            case AV_CODEC_ID_INTERPLAY_DPCM:
713
0
                return (frame_bytes - 6 - ch) / ch;
714
0
            case AV_CODEC_ID_ROQ_DPCM:
715
0
                return (frame_bytes - 8) / ch;
716
0
            case AV_CODEC_ID_XAN_DPCM:
717
0
                return (frame_bytes - 2 * ch) / ch;
718
0
            case AV_CODEC_ID_MACE3:
719
0
                return 3 * frame_bytes / ch;
720
0
            case AV_CODEC_ID_MACE6:
721
0
                return 6 * frame_bytes / ch;
722
0
            case AV_CODEC_ID_PCM_LXF:
723
0
                return 2 * (frame_bytes / (5 * ch));
724
0
            case AV_CODEC_ID_IAC:
725
0
            case AV_CODEC_ID_IMC:
726
0
                return 4 * frame_bytes / ch;
727
0
            }
728
729
0
            if (tag) {
730
                /* calc from frame_bytes, channels, and codec_tag */
731
0
                if (id == AV_CODEC_ID_SOL_DPCM) {
732
0
                    if (tag == 3)
733
0
                        return frame_bytes / ch;
734
0
                    else
735
0
                        return frame_bytes * 2 / ch;
736
0
                }
737
0
            }
738
739
0
            if (ba > 0) {
740
                /* calc from frame_bytes, channels, and block_align */
741
0
                int blocks = frame_bytes / ba;
742
0
                int64_t tmp = 0;
743
0
                switch (id) {
744
0
                case AV_CODEC_ID_ADPCM_IMA_XBOX:
745
0
                    if (bps != 4)
746
0
                        return 0;
747
0
                    tmp = blocks * ((ba - 4 * ch) / (bps * ch) * 8);
748
0
                    break;
749
0
                case AV_CODEC_ID_ADPCM_IMA_WAV:
750
0
                    if (bps < 2 || bps > 5)
751
0
                        return 0;
752
0
                    tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8LL);
753
0
                    break;
754
0
                case AV_CODEC_ID_ADPCM_IMA_DK3:
755
0
                    tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch);
756
0
                    break;
757
0
                case AV_CODEC_ID_ADPCM_IMA_DK4:
758
0
                    tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch);
759
0
                    break;
760
0
                case AV_CODEC_ID_ADPCM_IMA_RAD:
761
0
                    tmp = blocks * ((ba - 4LL * ch) * 2 / ch);
762
0
                    break;
763
0
                case AV_CODEC_ID_ADPCM_MS:
764
0
                    tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch);
765
0
                    break;
766
0
                case AV_CODEC_ID_ADPCM_MTAF:
767
0
                    tmp = blocks * (ba - 16LL) * 2 / ch;
768
0
                    break;
769
0
                case AV_CODEC_ID_ADPCM_XMD:
770
0
                    tmp = blocks * 32;
771
0
                    break;
772
0
                case AV_CODEC_ID_ADPCM_IMA_CITRIX:
773
0
                    if (bps != 2 || ch > 2 || ba < 4 * ch || (ch == 2 && ba % 8))
774
0
                        return 0;
775
0
                    tmp = blocks * (1LL + (ba / ch - 4) * 4LL);
776
0
                    break;
777
0
                }
778
0
                if (tmp) {
779
0
                    if (tmp != (int)tmp)
780
0
                        return 0;
781
0
                    return tmp;
782
0
                }
783
0
            }
784
785
0
            if (bps > 0) {
786
                /* calc from frame_bytes, channels, and bits_per_coded_sample */
787
0
                switch (id) {
788
0
                case AV_CODEC_ID_PCM_DVD:
789
0
                    if(bps<4 || frame_bytes<3)
790
0
                        return 0;
791
0
                    return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
792
0
                case AV_CODEC_ID_PCM_BLURAY:
793
0
                    if(bps<4 || frame_bytes<4)
794
0
                        return 0;
795
0
                    return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
796
0
                case AV_CODEC_ID_S302M:
797
0
                    return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
798
0
                }
799
0
            }
800
0
        }
801
0
    }
802
803
    /* Fall back on using frame_size */
804
0
    if (frame_size > 1 && frame_bytes)
805
0
        return frame_size;
806
807
    //For WMA we currently have no other means to calculate duration thus we
808
    //do it here by assuming CBR, which is true for all known cases.
809
0
    if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
810
0
        if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
811
0
            return  (frame_bytes * 8LL * sr) / bitrate;
812
0
    }
813
814
0
    return 0;
815
0
}
816
817
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
818
0
{
819
0
   int channels = avctx->ch_layout.nb_channels;
820
0
   int duration;
821
822
0
    duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
823
0
                                    channels, avctx->block_align,
824
0
                                    avctx->codec_tag, avctx->bits_per_coded_sample,
825
0
                                    avctx->bit_rate, avctx->extradata, avctx->frame_size,
826
0
                                    frame_bytes);
827
0
    return FFMAX(0, duration);
828
0
}
829
830
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
831
0
{
832
0
   int channels = par->ch_layout.nb_channels;
833
0
   int duration;
834
835
0
    duration = get_audio_frame_duration(par->codec_id, par->sample_rate,
836
0
                                    channels, par->block_align,
837
0
                                    par->codec_tag, par->bits_per_coded_sample,
838
0
                                    par->bit_rate, par->extradata, par->frame_size,
839
0
                                    frame_bytes);
840
0
    return FFMAX(0, duration);
841
0
}
842
843
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
844
0
{
845
0
    unsigned int n = 0;
846
847
0
    while (v >= 0xff) {
848
0
        *s++ = 0xff;
849
0
        v -= 0xff;
850
0
        n++;
851
0
    }
852
0
    *s = v;
853
0
    n++;
854
0
    return n;
855
0
}
856
857
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
858
0
{
859
0
    int i;
860
0
    for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
861
0
    return i;
862
0
}
863
864
const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *avcodec, int index)
865
0
{
866
0
    const FFCodec *const codec = ffcodec(avcodec);
867
0
    int i;
868
0
    if (!codec->hw_configs || index < 0)
869
0
        return NULL;
870
0
    for (i = 0; i <= index; i++)
871
0
        if (!codec->hw_configs[i])
872
0
            return NULL;
873
0
    return &codec->hw_configs[index]->public;
874
0
}
875
876
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
877
0
{
878
0
    int ret;
879
880
0
    dst->owner[0] = src->owner[0];
881
0
    dst->owner[1] = src->owner[1];
882
883
0
    ret = av_frame_ref(dst->f, src->f);
884
0
    if (ret < 0)
885
0
        return ret;
886
887
0
    av_assert0(!dst->progress);
888
889
0
    if (src->progress)
890
0
        dst->progress = av_refstruct_ref(src->progress);
891
892
0
    return 0;
893
0
}
894
895
int ff_thread_replace_frame(ThreadFrame *dst, const ThreadFrame *src)
896
0
{
897
0
    int ret;
898
899
0
    dst->owner[0] = src->owner[0];
900
0
    dst->owner[1] = src->owner[1];
901
902
0
    ret = av_frame_replace(dst->f, src->f);
903
0
    if (ret < 0)
904
0
        return ret;
905
906
0
    av_refstruct_replace(&dst->progress, src->progress);
907
908
0
    return 0;
909
0
}
910
911
#if !HAVE_THREADS
912
913
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
914
{
915
    return ff_get_buffer(avctx, f, flags);
916
}
917
918
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
919
{
920
    f->owner[0] = f->owner[1] = avctx;
921
    return ff_get_buffer(avctx, f->f, flags);
922
}
923
924
void ff_thread_release_ext_buffer(ThreadFrame *f)
925
{
926
    f->owner[0] = f->owner[1] = NULL;
927
    if (f->f)
928
        av_frame_unref(f->f);
929
}
930
931
void ff_thread_finish_setup(AVCodecContext *avctx)
932
{
933
}
934
935
void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
936
{
937
}
938
939
void ff_thread_await_progress(const ThreadFrame *f, int progress, int field)
940
{
941
}
942
943
int ff_thread_can_start_frame(AVCodecContext *avctx)
944
{
945
    return 1;
946
}
947
#endif
948
949
const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
950
                                      const uint8_t *end,
951
                                      uint32_t *restrict state)
952
0
{
953
0
    int i;
954
955
0
    av_assert0(p <= end);
956
0
    if (p >= end)
957
0
        return end;
958
959
0
    for (i = 0; i < 3; i++) {
960
0
        uint32_t tmp = *state << 8;
961
0
        *state = tmp + *(p++);
962
0
        if (tmp == 0x100 || p == end)
963
0
            return p;
964
0
    }
965
966
0
    while (p < end) {
967
0
        if      (p[-1] > 1      ) p += 3;
968
0
        else if (p[-2]          ) p += 2;
969
0
        else if (p[-3]|(p[-1]-1)) p++;
970
0
        else {
971
0
            p++;
972
0
            break;
973
0
        }
974
0
    }
975
976
0
    p = FFMIN(p, end) - 4;
977
0
    *state = AV_RB32(p);
978
979
0
    return p + 4;
980
0
}
981
982
AVCPBProperties *av_cpb_properties_alloc(size_t *size)
983
0
{
984
0
    AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
985
0
    if (!props)
986
0
        return NULL;
987
988
0
    if (size)
989
0
        *size = sizeof(*props);
990
991
0
    props->vbv_delay = UINT64_MAX;
992
993
0
    return props;
994
0
}
995
996
static void put_timecode_fields(PutBitContext *pb, AVRational rate, uint32_t tc)
997
0
{
998
0
    unsigned hours, minutes, seconds, frames, drop;
999
1000
0
    ff_timecode_set_smpte(&drop, &hours, &minutes, &seconds, &frames, rate, tc, 0, 0);
1001
1002
0
    put_bits(pb, 5, 0);      // counting_type
1003
0
    put_bits(pb, 1, 1);      // full_timestamp_flag
1004
0
    put_bits(pb, 1, 0);      // discontinuity_flag
1005
0
    put_bits(pb, 1, drop);   // cnt_dropped_flag
1006
0
    put_bits(pb, 9, frames);
1007
0
    put_bits(pb, 6, seconds);
1008
0
    put_bits(pb, 6, minutes);
1009
0
    put_bits(pb, 5, hours);
1010
0
}
1011
1012
int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
1013
                     void **data, size_t *sei_size)
1014
0
{
1015
0
    AVFrameSideData *sd = NULL;
1016
0
    uint8_t *sei_data;
1017
0
    PutBitContext pb;
1018
0
    uint32_t *tc;
1019
0
    int m;
1020
1021
0
    if (frame)
1022
0
        sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
1023
1024
0
    if (!sd) {
1025
0
        *data = NULL;
1026
0
        return 0;
1027
0
    }
1028
0
    tc =  (uint32_t*)sd->data;
1029
0
    m  = tc[0] & 3;
1030
1031
0
    *sei_size = sizeof(uint32_t) * 4;
1032
0
    *data = av_mallocz(*sei_size + prefix_len);
1033
0
    if (!*data)
1034
0
        return AVERROR(ENOMEM);
1035
0
    sei_data = (uint8_t*)*data + prefix_len;
1036
1037
0
    init_put_bits(&pb, sei_data, *sei_size);
1038
0
    put_bits(&pb, 2, m); // num_clock_ts
1039
1040
0
    for (int j = 1; j <= m; j++) {
1041
0
        put_bits(&pb, 1, 1); // clock_timestamp_flag
1042
0
        put_bits(&pb, 1, 1); // units_field_based_flag
1043
0
        put_timecode_fields(&pb, rate, tc[j]);
1044
0
        put_bits(&pb, 5, 0); // time_offset_length
1045
0
    }
1046
0
    flush_put_bits(&pb);
1047
1048
0
    return 0;
1049
0
}
1050
1051
int ff_alloc_timecode_metadata_av1(const AVFrame *frame, AVRational rate,
1052
                                   void **data, size_t *size)
1053
0
{
1054
0
    AVFrameSideData *sd = NULL;
1055
0
    PutBitContext pb;
1056
0
    uint32_t *tc;
1057
1058
0
    if (frame)
1059
0
        sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
1060
1061
0
    *data = NULL;
1062
0
    if (!sd)
1063
0
        return 0;
1064
0
    tc = (uint32_t*)sd->data;
1065
0
    if (!(tc[0] & 3)) // num_clock_ts
1066
0
        return 0;
1067
1068
0
    *size = 5;
1069
0
    *data = av_mallocz(*size);
1070
0
    if (!*data)
1071
0
        return AVERROR(ENOMEM);
1072
1073
0
    init_put_bits(&pb, *data, *size);
1074
0
    put_timecode_fields(&pb, rate, tc[1]);
1075
0
    put_bits(&pb, 5, 1); // time_offset_length
1076
0
    put_bits(&pb, 1, 0); // time_offset_value
1077
0
    flush_put_bits(&pb);
1078
1079
0
    return 0;
1080
0
}
1081
1082
int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
1083
0
{
1084
0
    AVRational framerate = avctx->framerate;
1085
0
    int bits_per_coded_sample = avctx->bits_per_coded_sample;
1086
0
    int64_t bitrate;
1087
1088
0
    if (!(framerate.num && framerate.den))
1089
0
        framerate = av_inv_q(avctx->time_base);
1090
0
    if (!(framerate.num && framerate.den))
1091
0
        return 0;
1092
1093
0
    if (!bits_per_coded_sample) {
1094
0
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1095
0
        bits_per_coded_sample = av_get_bits_per_pixel(desc);
1096
0
    }
1097
0
    bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
1098
0
              framerate.num / framerate.den;
1099
1100
0
    return bitrate;
1101
0
}