GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/utils.c Lines: 380 548 69.3 %
Date: 2021-04-15 16:04:23 Branches: 288 425 67.8 %

Line Branch Exec 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/avstring.h"
31
#include "libavutil/intreadwrite.h"
32
#include "libavutil/mem_internal.h"
33
#include "libavutil/pixdesc.h"
34
#include "libavutil/imgutils.h"
35
#include "libavutil/pixfmt.h"
36
#include "avcodec.h"
37
#include "codec.h"
38
#include "hwconfig.h"
39
#include "thread.h"
40
#include "internal.h"
41
#include "put_bits.h"
42
#include "raw.h"
43
#include "version.h"
44
#include <stdlib.h>
45
#include <stdarg.h>
46
#include <stdatomic.h>
47
#include <limits.h>
48
#include <float.h>
49
50
137107
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
51
{
52
137107
    uint8_t **p = ptr;
53
137107
    if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
54
        av_freep(p);
55
        *size = 0;
56
        return;
57
    }
58
137107
    if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
59
108580
        memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
60
}
61
62
622
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
63
{
64
622
    uint8_t **p = ptr;
65
622
    if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
66
        av_freep(p);
67
        *size = 0;
68
        return;
69
    }
70
622
    if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
71
507
        memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
72
}
73
74
6338869
int av_codec_is_encoder(const AVCodec *codec)
75
{
76


6338869
    return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
77
}
78
79
11467181
int av_codec_is_decoder(const AVCodec *codec)
80
{
81

11467181
    return codec && (codec->decode || codec->receive_frame);
82
}
83
84
29488
int ff_set_dimensions(AVCodecContext *s, int width, int height)
85
{
86
29488
    int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
87
88
29488
    if (ret < 0)
89
1
        width = height = 0;
90
91
29488
    s->coded_width  = width;
92
29488
    s->coded_height = height;
93
29488
    s->width        = AV_CEIL_RSHIFT(width,  s->lowres);
94
29488
    s->height       = AV_CEIL_RSHIFT(height, s->lowres);
95
96
29488
    return ret;
97
}
98
99
2519
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
100
{
101
2519
    int ret = av_image_check_sar(avctx->width, avctx->height, sar);
102
103
2519
    if (ret < 0) {
104
5
        av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
105
               sar.num, sar.den);
106
5
        avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
107
5
        return ret;
108
    } else {
109
2514
        avctx->sample_aspect_ratio = sar;
110
    }
111
2514
    return 0;
112
}
113
114
20560
int ff_side_data_update_matrix_encoding(AVFrame *frame,
115
                                        enum AVMatrixEncoding matrix_encoding)
116
{
117
    AVFrameSideData *side_data;
118
    enum AVMatrixEncoding *data;
119
120
20560
    side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
121
20560
    if (!side_data)
122
20560
        side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
123
                                           sizeof(enum AVMatrixEncoding));
124
125
20560
    if (!side_data)
126
        return AVERROR(ENOMEM);
127
128
20560
    data  = (enum AVMatrixEncoding*)side_data->data;
129
20560
    *data = matrix_encoding;
130
131
20560
    return 0;
132
}
133
134
8497
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
135
                               int linesize_align[AV_NUM_DATA_POINTERS])
136
{
137
    int i;
138
8497
    int w_align = 1;
139
8497
    int h_align = 1;
140
8497
    AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
141
142
8497
    if (desc) {
143
8497
        w_align = 1 << desc->log2_chroma_w;
144
8497
        h_align = 1 << desc->log2_chroma_h;
145
    }
146
147


8497
    switch (s->pix_fmt) {
148
7582
    case AV_PIX_FMT_YUV420P:
149
    case AV_PIX_FMT_YUYV422:
150
    case AV_PIX_FMT_YVYU422:
151
    case AV_PIX_FMT_UYVY422:
152
    case AV_PIX_FMT_YUV422P:
153
    case AV_PIX_FMT_YUV440P:
154
    case AV_PIX_FMT_YUV444P:
155
    case AV_PIX_FMT_GBRP:
156
    case AV_PIX_FMT_GBRAP:
157
    case AV_PIX_FMT_GRAY8:
158
    case AV_PIX_FMT_GRAY16BE:
159
    case AV_PIX_FMT_GRAY16LE:
160
    case AV_PIX_FMT_YUVJ420P:
161
    case AV_PIX_FMT_YUVJ422P:
162
    case AV_PIX_FMT_YUVJ440P:
163
    case AV_PIX_FMT_YUVJ444P:
164
    case AV_PIX_FMT_YUVA420P:
165
    case AV_PIX_FMT_YUVA422P:
166
    case AV_PIX_FMT_YUVA444P:
167
    case AV_PIX_FMT_YUV420P9LE:
168
    case AV_PIX_FMT_YUV420P9BE:
169
    case AV_PIX_FMT_YUV420P10LE:
170
    case AV_PIX_FMT_YUV420P10BE:
171
    case AV_PIX_FMT_YUV420P12LE:
172
    case AV_PIX_FMT_YUV420P12BE:
173
    case AV_PIX_FMT_YUV420P14LE:
174
    case AV_PIX_FMT_YUV420P14BE:
175
    case AV_PIX_FMT_YUV420P16LE:
176
    case AV_PIX_FMT_YUV420P16BE:
177
    case AV_PIX_FMT_YUVA420P9LE:
178
    case AV_PIX_FMT_YUVA420P9BE:
179
    case AV_PIX_FMT_YUVA420P10LE:
180
    case AV_PIX_FMT_YUVA420P10BE:
181
    case AV_PIX_FMT_YUVA420P16LE:
182
    case AV_PIX_FMT_YUVA420P16BE:
183
    case AV_PIX_FMT_YUV422P9LE:
184
    case AV_PIX_FMT_YUV422P9BE:
185
    case AV_PIX_FMT_YUV422P10LE:
186
    case AV_PIX_FMT_YUV422P10BE:
187
    case AV_PIX_FMT_YUV422P12LE:
188
    case AV_PIX_FMT_YUV422P12BE:
189
    case AV_PIX_FMT_YUV422P14LE:
190
    case AV_PIX_FMT_YUV422P14BE:
191
    case AV_PIX_FMT_YUV422P16LE:
192
    case AV_PIX_FMT_YUV422P16BE:
193
    case AV_PIX_FMT_YUVA422P9LE:
194
    case AV_PIX_FMT_YUVA422P9BE:
195
    case AV_PIX_FMT_YUVA422P10LE:
196
    case AV_PIX_FMT_YUVA422P10BE:
197
    case AV_PIX_FMT_YUVA422P12LE:
198
    case AV_PIX_FMT_YUVA422P12BE:
199
    case AV_PIX_FMT_YUVA422P16LE:
200
    case AV_PIX_FMT_YUVA422P16BE:
201
    case AV_PIX_FMT_YUV440P10LE:
202
    case AV_PIX_FMT_YUV440P10BE:
203
    case AV_PIX_FMT_YUV440P12LE:
204
    case AV_PIX_FMT_YUV440P12BE:
205
    case AV_PIX_FMT_YUV444P9LE:
206
    case AV_PIX_FMT_YUV444P9BE:
207
    case AV_PIX_FMT_YUV444P10LE:
208
    case AV_PIX_FMT_YUV444P10BE:
209
    case AV_PIX_FMT_YUV444P12LE:
210
    case AV_PIX_FMT_YUV444P12BE:
211
    case AV_PIX_FMT_YUV444P14LE:
212
    case AV_PIX_FMT_YUV444P14BE:
213
    case AV_PIX_FMT_YUV444P16LE:
214
    case AV_PIX_FMT_YUV444P16BE:
215
    case AV_PIX_FMT_YUVA444P9LE:
216
    case AV_PIX_FMT_YUVA444P9BE:
217
    case AV_PIX_FMT_YUVA444P10LE:
218
    case AV_PIX_FMT_YUVA444P10BE:
219
    case AV_PIX_FMT_YUVA444P12LE:
220
    case AV_PIX_FMT_YUVA444P12BE:
221
    case AV_PIX_FMT_YUVA444P16LE:
222
    case AV_PIX_FMT_YUVA444P16BE:
223
    case AV_PIX_FMT_GBRP9LE:
224
    case AV_PIX_FMT_GBRP9BE:
225
    case AV_PIX_FMT_GBRP10LE:
226
    case AV_PIX_FMT_GBRP10BE:
227
    case AV_PIX_FMT_GBRP12LE:
228
    case AV_PIX_FMT_GBRP12BE:
229
    case AV_PIX_FMT_GBRP14LE:
230
    case AV_PIX_FMT_GBRP14BE:
231
    case AV_PIX_FMT_GBRP16LE:
232
    case AV_PIX_FMT_GBRP16BE:
233
    case AV_PIX_FMT_GBRAP12LE:
234
    case AV_PIX_FMT_GBRAP12BE:
235
    case AV_PIX_FMT_GBRAP16LE:
236
    case AV_PIX_FMT_GBRAP16BE:
237
7582
        w_align = 16; //FIXME assume 16 pixel per macroblock
238
7582
        h_align = 16 * 2; // interlaced needs 2 macroblocks height
239
7582
        break;
240
26
    case AV_PIX_FMT_YUV411P:
241
    case AV_PIX_FMT_YUVJ411P:
242
    case AV_PIX_FMT_UYYVYY411:
243
26
        w_align = 32;
244
26
        h_align = 16 * 2;
245
26
        break;
246
21
    case AV_PIX_FMT_YUV410P:
247
21
        if (s->codec_id == AV_CODEC_ID_SVQ1) {
248
10
            w_align = 64;
249
10
            h_align = 64;
250
        }
251
21
        break;
252
34
    case AV_PIX_FMT_RGB555:
253
34
        if (s->codec_id == AV_CODEC_ID_RPZA) {
254
1
            w_align = 4;
255
1
            h_align = 4;
256
        }
257
34
        if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
258
1
            w_align = 8;
259
1
            h_align = 8;
260
        }
261
34
        break;
262
121
    case AV_PIX_FMT_PAL8:
263
    case AV_PIX_FMT_BGR8:
264
    case AV_PIX_FMT_RGB8:
265
121
        if (s->codec_id == AV_CODEC_ID_SMC ||
266
120
            s->codec_id == AV_CODEC_ID_CINEPAK) {
267
2
            w_align = 4;
268
2
            h_align = 4;
269
        }
270
121
        if (s->codec_id == AV_CODEC_ID_JV ||
271
120
            s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
272
2
            w_align = 8;
273
2
            h_align = 8;
274
        }
275
121
        break;
276
73
    case AV_PIX_FMT_BGR24:
277
73
        if ((s->codec_id == AV_CODEC_ID_MSZH) ||
278
72
            (s->codec_id == AV_CODEC_ID_ZLIB)) {
279
6
            w_align = 4;
280
6
            h_align = 4;
281
        }
282
73
        break;
283
195
    case AV_PIX_FMT_RGB24:
284
195
        if (s->codec_id == AV_CODEC_ID_CINEPAK) {
285
6
            w_align = 4;
286
6
            h_align = 4;
287
        }
288
195
        break;
289
445
    default:
290
445
        break;
291
    }
292
293
8497
    if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
294
2
        w_align = FFMAX(w_align, 8);
295
    }
296
297
8497
    *width  = FFALIGN(*width, w_align);
298
8497
    *height = FFALIGN(*height, h_align);
299

8497
    if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
300

7958
        s->codec_id == AV_CODEC_ID_VP5  || s->codec_id == AV_CODEC_ID_VP6 ||
301

7950
        s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
302
    ) {
303
        // some of the optimized chroma MC reads one line too much
304
        // which is also done in mpeg decoders with lowres > 0
305
551
        *height += 2;
306
307
        // H.264 uses edge emulation for out of frame motion vectors, for this
308
        // it requires a temporary area large enough to hold a 21x21 block,
309
        // increasing witdth ensure that the temporary area is large enough,
310
        // the next rounded up width is 32
311
551
        *width = FFMAX(*width, 32);
312
    }
313
314
42485
    for (i = 0; i < 4; i++)
315
33988
        linesize_align[i] = STRIDE_ALIGN;
316
8497
}
317
318
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
319
{
320
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
321
    int chroma_shift = desc->log2_chroma_w;
322
    int linesize_align[AV_NUM_DATA_POINTERS];
323
    int align;
324
325
    avcodec_align_dimensions2(s, width, height, linesize_align);
326
    align               = FFMAX(linesize_align[0], linesize_align[3]);
327
    linesize_align[1] <<= chroma_shift;
328
    linesize_align[2] <<= chroma_shift;
329
    align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
330
    *width              = FFALIGN(*width, align);
331
}
332
333
14
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
334
{
335

14
    if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
336
        return AVERROR(EINVAL);
337
14
    pos--;
338
339
14
    *xpos = (pos&1) * 128;
340
14
    *ypos = ((pos>>1)^(pos<4)) * 128;
341
342
14
    return 0;
343
}
344
345
10
enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
346
{
347
    int pos, xout, yout;
348
349
10
    for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
350

10
        if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
351
10
            return pos;
352
    }
353
    return AVCHROMA_LOC_UNSPECIFIED;
354
}
355
356
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
357
                             enum AVSampleFormat sample_fmt, const uint8_t *buf,
358
                             int buf_size, int align)
359
{
360
    int ch, planar, needed_size, ret = 0;
361
362
    needed_size = av_samples_get_buffer_size(NULL, nb_channels,
363
                                             frame->nb_samples, sample_fmt,
364
                                             align);
365
    if (buf_size < needed_size)
366
        return AVERROR(EINVAL);
367
368
    planar = av_sample_fmt_is_planar(sample_fmt);
369
    if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
370
        if (!(frame->extended_data = av_mallocz_array(nb_channels,
371
                                                sizeof(*frame->extended_data))))
372
            return AVERROR(ENOMEM);
373
    } else {
374
        frame->extended_data = frame->data;
375
    }
376
377
    if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
378
                                      (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
379
                                      sample_fmt, align)) < 0) {
380
        if (frame->extended_data != frame->data)
381
            av_freep(&frame->extended_data);
382
        return ret;
383
    }
384
    if (frame->extended_data != frame->data) {
385
        for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
386
            frame->data[ch] = frame->extended_data[ch];
387
    }
388
389
    return ret;
390
}
391
392
17
void ff_color_frame(AVFrame *frame, const int c[4])
393
{
394
17
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
395
    int p, y;
396
397
17
    av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
398
399
68
    for (p = 0; p<desc->nb_components; p++) {
400
51
        uint8_t *dst = frame->data[p];
401

51
        int is_chroma = p == 1 || p == 2;
402
51
        int bytes  = is_chroma ? AV_CEIL_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
403
51
        int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
404
51
        if (desc->comp[0].depth >= 9) {
405
            ((uint16_t*)dst)[0] = c[p];
406
            av_memcpy_backptr(dst + 2, 2, bytes - 2);
407
            dst += frame->linesize[p];
408
            for (y = 1; y < height; y++) {
409
                memcpy(dst, frame->data[p], 2*bytes);
410
                dst += frame->linesize[p];
411
            }
412
        } else {
413
25203
            for (y = 0; y < height; y++) {
414
25152
                memset(dst, c[p], bytes);
415
25152
                dst += frame->linesize[p];
416
            }
417
        }
418
    }
419
17
}
420
421
1362
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
422
                                       unsigned int fourcc)
423
{
424
12588
    while (tags->pix_fmt >= 0) {
425
12390
        if (tags->fourcc == fourcc)
426
1164
            return tags->pix_fmt;
427
11226
        tags++;
428
    }
429
198
    return AV_PIX_FMT_NONE;
430
}
431
432
#if FF_API_CODEC_GET_SET
433
MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
434
MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
435
MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
436
MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
437
MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
438
439
unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
440
{
441
    return codec->properties;
442
}
443
444
int av_codec_get_max_lowres(const AVCodec *codec)
445
{
446
    return codec->max_lowres;
447
}
448
#endif
449
450
165279
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
451
165279
    return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
452
}
453
454
16378
const char *avcodec_get_name(enum AVCodecID id)
455
{
456
    const AVCodecDescriptor *cd;
457
    const AVCodec *codec;
458
459
16378
    if (id == AV_CODEC_ID_NONE)
460
156
        return "none";
461
16222
    cd = avcodec_descriptor_get(id);
462
16222
    if (cd)
463
16220
        return cd->name;
464
2
    av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
465
2
    codec = avcodec_find_decoder(id);
466
2
    if (codec)
467
        return codec->name;
468
2
    codec = avcodec_find_encoder(id);
469
2
    if (codec)
470
        return codec->name;
471
2
    return "unknown_codec";
472
}
473
474
#if FF_API_TAG_STRING
475
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
476
{
477
    int i, len, ret = 0;
478
479
#define TAG_PRINT(x)                                              \
480
    (((x) >= '0' && (x) <= '9') ||                                \
481
     ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
482
     ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
483
484
    for (i = 0; i < 4; i++) {
485
        len = snprintf(buf, buf_size,
486
                       TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
487
        buf        += len;
488
        buf_size    = buf_size > len ? buf_size - len : 0;
489
        ret        += len;
490
        codec_tag >>= 8;
491
    }
492
    return ret;
493
}
494
#endif
495
496
const char *av_get_profile_name(const AVCodec *codec, int profile)
497
{
498
    const AVProfile *p;
499
    if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
500
        return NULL;
501
502
    for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
503
        if (p->profile == profile)
504
            return p->name;
505
506
    return NULL;
507
}
508
509
12749
const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
510
{
511
12749
    const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
512
    const AVProfile *p;
513
514

12749
    if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
515
11150
        return NULL;
516
517
4340
    for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
518
4330
        if (p->profile == profile)
519
1589
            return p->name;
520
521
10
    return NULL;
522
}
523
524
809163
int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
525
{
526

809163
    switch (codec_id) {
527
10922
    case AV_CODEC_ID_8SVX_EXP:
528
    case AV_CODEC_ID_8SVX_FIB:
529
    case AV_CODEC_ID_ADPCM_ARGO:
530
    case AV_CODEC_ID_ADPCM_CT:
531
    case AV_CODEC_ID_ADPCM_IMA_ALP:
532
    case AV_CODEC_ID_ADPCM_IMA_AMV:
533
    case AV_CODEC_ID_ADPCM_IMA_APC:
534
    case AV_CODEC_ID_ADPCM_IMA_APM:
535
    case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
536
    case AV_CODEC_ID_ADPCM_IMA_OKI:
537
    case AV_CODEC_ID_ADPCM_IMA_WS:
538
    case AV_CODEC_ID_ADPCM_IMA_SSI:
539
    case AV_CODEC_ID_ADPCM_G722:
540
    case AV_CODEC_ID_ADPCM_YAMAHA:
541
    case AV_CODEC_ID_ADPCM_AICA:
542
10922
        return 4;
543
11702
    case AV_CODEC_ID_DSD_LSBF:
544
    case AV_CODEC_ID_DSD_MSBF:
545
    case AV_CODEC_ID_DSD_LSBF_PLANAR:
546
    case AV_CODEC_ID_DSD_MSBF_PLANAR:
547
    case AV_CODEC_ID_PCM_ALAW:
548
    case AV_CODEC_ID_PCM_MULAW:
549
    case AV_CODEC_ID_PCM_VIDC:
550
    case AV_CODEC_ID_PCM_S8:
551
    case AV_CODEC_ID_PCM_S8_PLANAR:
552
    case AV_CODEC_ID_PCM_SGA:
553
    case AV_CODEC_ID_PCM_U8:
554
    case AV_CODEC_ID_SDX2_DPCM:
555
    case AV_CODEC_ID_DERF_DPCM:
556
11702
        return 8;
557
543441
    case AV_CODEC_ID_PCM_S16BE:
558
    case AV_CODEC_ID_PCM_S16BE_PLANAR:
559
    case AV_CODEC_ID_PCM_S16LE:
560
    case AV_CODEC_ID_PCM_S16LE_PLANAR:
561
    case AV_CODEC_ID_PCM_U16BE:
562
    case AV_CODEC_ID_PCM_U16LE:
563
543441
        return 16;
564
45098
    case AV_CODEC_ID_PCM_S24DAUD:
565
    case AV_CODEC_ID_PCM_S24BE:
566
    case AV_CODEC_ID_PCM_S24LE:
567
    case AV_CODEC_ID_PCM_S24LE_PLANAR:
568
    case AV_CODEC_ID_PCM_U24BE:
569
    case AV_CODEC_ID_PCM_U24LE:
570
45098
        return 24;
571
28022
    case AV_CODEC_ID_PCM_S32BE:
572
    case AV_CODEC_ID_PCM_S32LE:
573
    case AV_CODEC_ID_PCM_S32LE_PLANAR:
574
    case AV_CODEC_ID_PCM_U32BE:
575
    case AV_CODEC_ID_PCM_U32LE:
576
    case AV_CODEC_ID_PCM_F32BE:
577
    case AV_CODEC_ID_PCM_F32LE:
578
    case AV_CODEC_ID_PCM_F24LE:
579
    case AV_CODEC_ID_PCM_F16LE:
580
28022
        return 32;
581
21300
    case AV_CODEC_ID_PCM_F64BE:
582
    case AV_CODEC_ID_PCM_F64LE:
583
    case AV_CODEC_ID_PCM_S64BE:
584
    case AV_CODEC_ID_PCM_S64LE:
585
21300
        return 64;
586
148678
    default:
587
148678
        return 0;
588
    }
589
}
590
591
16
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
592
{
593
    static const enum AVCodecID map[][2] = {
594
        [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
595
        [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
596
        [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
597
        [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
598
        [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
599
        [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
600
        [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
601
        [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
602
        [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
603
        [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
604
        [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
605
    };
606

16
    if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
607
        return AV_CODEC_ID_NONE;
608

16
    if (be < 0 || be > 1)
609
16
        be = AV_NE(1, 0);
610
16
    return map[fmt][be];
611
}
612
613
323836
int av_get_bits_per_sample(enum AVCodecID codec_id)
614
{
615

323836
    switch (codec_id) {
616
3
    case AV_CODEC_ID_ADPCM_SBPRO_2:
617
3
        return 2;
618
3
    case AV_CODEC_ID_ADPCM_SBPRO_3:
619
3
        return 3;
620
64
    case AV_CODEC_ID_ADPCM_SBPRO_4:
621
    case AV_CODEC_ID_ADPCM_IMA_WAV:
622
    case AV_CODEC_ID_ADPCM_IMA_QT:
623
    case AV_CODEC_ID_ADPCM_SWF:
624
    case AV_CODEC_ID_ADPCM_MS:
625
64
        return 4;
626
323766
    default:
627
323766
        return av_get_exact_bits_per_sample(codec_id);
628
    }
629
}
630
631
479521
static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
632
                                    uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
633
                                    uint8_t * extradata, int frame_size, int frame_bytes)
634
{
635
479521
    int bps = av_get_exact_bits_per_sample(id);
636

479521
    int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
637
638
    /* codecs with an exact constant bits per sample */
639


479521
    if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
640
337962
        return (frame_bytes * 8LL) / (bps * ch);
641
141559
    bps = bits_per_coded_sample;
642
643
    /* codecs with a fixed packet duration */
644



141559
    switch (id) {
645
16542
    case AV_CODEC_ID_ADPCM_ADX:    return   32;
646
8274
    case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
647
600
    case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
648
4428
    case AV_CODEC_ID_AMR_NB:
649
    case AV_CODEC_ID_EVRC:
650
    case AV_CODEC_ID_GSM:
651
    case AV_CODEC_ID_QCELP:
652
4428
    case AV_CODEC_ID_RA_288:       return  160;
653
    case AV_CODEC_ID_AMR_WB:
654
    case AV_CODEC_ID_GSM_MS:       return  320;
655
    case AV_CODEC_ID_MP1:          return  384;
656
4319
    case AV_CODEC_ID_ATRAC1:       return  512;
657
4114
    case AV_CODEC_ID_ATRAC9:
658
    case AV_CODEC_ID_ATRAC3:
659
4114
        if (framecount > INT_MAX/1024)
660
            return 0;
661
4114
        return 1024 * framecount;
662
813
    case AV_CODEC_ID_ATRAC3P:      return 2048;
663
18455
    case AV_CODEC_ID_MP2:
664
18455
    case AV_CODEC_ID_MUSEPACK7:    return 1152;
665
1636
    case AV_CODEC_ID_AC3:          return 1536;
666
    }
667
668
82378
    if (sr > 0) {
669
        /* calc from sample rate */
670
79665
        if (id == AV_CODEC_ID_TTA)
671
9
            return 256 * sr / 245;
672
79656
        else if (id == AV_CODEC_ID_DST)
673
            return 588 * sr / 44100;
674
79656
        else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
675
66
            if (sr / 22050 > 22)
676
                return 0;
677
66
            return (480 << (sr / 22050));
678
        }
679
680
79590
        if (id == AV_CODEC_ID_MP3)
681
5185
            return sr <= 24000 ? 576 : 1152;
682
    }
683
684
77118
    if (ba > 0) {
685
        /* calc from block_align */
686
22263
        if (id == AV_CODEC_ID_SIPR) {
687

7249
            switch (ba) {
688
3250
            case 20: return 160;
689
1983
            case 19: return 144;
690
1680
            case 29: return 288;
691
336
            case 37: return 480;
692
            }
693
15014
        } else if (id == AV_CODEC_ID_ILBC) {
694
            switch (ba) {
695
            case 38: return 160;
696
            case 50: return 240;
697
            }
698
        }
699
    }
700
701
69869
    if (frame_bytes > 0) {
702
        /* calc from frame_bytes only */
703
67191
        if (id == AV_CODEC_ID_TRUESPEECH)
704
12
            return 240 * (frame_bytes / 32);
705
67179
        if (id == AV_CODEC_ID_NELLYMOSER)
706
2074
            return 256 * (frame_bytes / 64);
707
65105
        if (id == AV_CODEC_ID_RA_144)
708
1057
            return 160 * (frame_bytes / 20);
709
710
64048
        if (bps > 0) {
711
            /* calc from frame_bytes and bits_per_coded_sample */
712

18061
            if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
713
106
                return frame_bytes * 8 / bps;
714
        }
715
716

63942
        if (ch > 0 && ch < INT_MAX/16) {
717
            /* calc from frame_bytes and channels */
718




63841
            switch (id) {
719
            case AV_CODEC_ID_FASTAUDIO:
720
                return frame_bytes / (40 * ch) * 256;
721
            case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
722
                return (frame_bytes - 4 * ch) / (128 * ch) * 256;
723
48
            case AV_CODEC_ID_ADPCM_AFC:
724
48
                return frame_bytes / (9 * ch) * 16;
725
33
            case AV_CODEC_ID_ADPCM_PSX:
726
            case AV_CODEC_ID_ADPCM_DTK:
727
33
                frame_bytes /= 16 * ch;
728
33
                if (frame_bytes > INT_MAX / 28)
729
                    return 0;
730
33
                return frame_bytes * 28;
731
452
            case AV_CODEC_ID_ADPCM_4XM:
732
            case AV_CODEC_ID_ADPCM_IMA_DAT4:
733
            case AV_CODEC_ID_ADPCM_IMA_ISS:
734
452
                return (frame_bytes - 4 * ch) * 2 / ch;
735
1951
            case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
736
1951
                return (frame_bytes - 4) * 2 / ch;
737
            case AV_CODEC_ID_ADPCM_IMA_AMV:
738
                return (frame_bytes - 8) * 2;
739
3580
            case AV_CODEC_ID_ADPCM_THP:
740
            case AV_CODEC_ID_ADPCM_THP_LE:
741
3580
                if (extradata)
742
3530
                    return frame_bytes * 14 / (8 * ch);
743
50
                break;
744
222
            case AV_CODEC_ID_ADPCM_XA:
745
222
                return (frame_bytes / 128) * 224 / ch;
746
327
            case AV_CODEC_ID_INTERPLAY_DPCM:
747
327
                return (frame_bytes - 6 - ch) / ch;
748
680
            case AV_CODEC_ID_ROQ_DPCM:
749
680
                return (frame_bytes - 8) / ch;
750
112
            case AV_CODEC_ID_XAN_DPCM:
751
112
                return (frame_bytes - 2 * ch) / ch;
752
            case AV_CODEC_ID_MACE3:
753
                return 3 * frame_bytes / ch;
754
138
            case AV_CODEC_ID_MACE6:
755
138
                return 6 * frame_bytes / ch;
756
            case AV_CODEC_ID_PCM_LXF:
757
                return 2 * (frame_bytes / (5 * ch));
758
1311
            case AV_CODEC_ID_IAC:
759
            case AV_CODEC_ID_IMC:
760
1311
                return 4 * frame_bytes / ch;
761
            }
762
763
55037
            if (tag) {
764
                /* calc from frame_bytes, channels, and codec_tag */
765
20198
                if (id == AV_CODEC_ID_SOL_DPCM) {
766
74
                    if (tag == 3)
767
74
                        return frame_bytes / ch;
768
                    else
769
                        return frame_bytes * 2 / ch;
770
                }
771
            }
772
773
54963
            if (ba > 0) {
774
                /* calc from frame_bytes, channels, and block_align */
775
11885
                int blocks = frame_bytes / ba;
776

11885
                switch (id) {
777
1007
                case AV_CODEC_ID_ADPCM_IMA_WAV:
778

1007
                    if (bps < 2 || bps > 5)
779
                        return 0;
780
1007
                    return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
781
669
                case AV_CODEC_ID_ADPCM_IMA_DK3:
782
669
                    return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
783
649
                case AV_CODEC_ID_ADPCM_IMA_DK4:
784
649
                    return blocks * (1 + (ba - 4 * ch) * 2 / ch);
785
1001
                case AV_CODEC_ID_ADPCM_IMA_RAD:
786
1001
                    return blocks * ((ba - 4 * ch) * 2 / ch);
787
890
                case AV_CODEC_ID_ADPCM_MS:
788
890
                    return blocks * (2 + (ba - 7 * ch) * 2LL / ch);
789
                case AV_CODEC_ID_ADPCM_MTAF:
790
                    return blocks * (ba - 16) * 2 / ch;
791
                }
792
            }
793
794
50747
            if (bps > 0) {
795
                /* calc from frame_bytes, channels, and bits_per_coded_sample */
796

9942
                switch (id) {
797
742
                case AV_CODEC_ID_PCM_DVD:
798

742
                    if(bps<4 || frame_bytes<3)
799
                        return 0;
800
742
                    return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
801
                case AV_CODEC_ID_PCM_BLURAY:
802
                    if(bps<4 || frame_bytes<4)
803
                        return 0;
804
                    return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
805
                case AV_CODEC_ID_S302M:
806
                    return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
807
                }
808
            }
809
        }
810
    }
811
812
    /* Fall back on using frame_size */
813

52784
    if (frame_size > 1 && frame_bytes)
814
25229
        return frame_size;
815
816
    //For WMA we currently have no other means to calculate duration thus we
817
    //do it here by assuming CBR, which is true for all known cases.
818


27555
    if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
819

7019
        if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
820
928
            return  (frame_bytes * 8LL * sr) / bitrate;
821
    }
822
823
26627
    return 0;
824
}
825
826
55763
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
827
{
828
55763
    return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
829
                                    avctx->channels, avctx->block_align,
830
                                    avctx->codec_tag, avctx->bits_per_coded_sample,
831
                                    avctx->bit_rate, avctx->extradata, avctx->frame_size,
832
                                    frame_bytes);
833
}
834
835
423758
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
836
{
837
423758
    return get_audio_frame_duration(par->codec_id, par->sample_rate,
838
                                    par->channels, par->block_align,
839
                                    par->codec_tag, par->bits_per_coded_sample,
840
                                    par->bit_rate, par->extradata, par->frame_size,
841
                                    frame_bytes);
842
}
843
844
#if !HAVE_THREADS
845
int ff_thread_init(AVCodecContext *s)
846
{
847
    return -1;
848
}
849
850
#endif
851
852
54
unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
853
{
854
54
    unsigned int n = 0;
855
856
54
    while (v >= 0xff) {
857
        *s++ = 0xff;
858
        v -= 0xff;
859
        n++;
860
    }
861
54
    *s = v;
862
54
    n++;
863
54
    return n;
864
}
865
866
476
int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
867
{
868
    int i;
869

1924
    for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
870
476
    return i;
871
}
872
873
13260
const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
874
{
875
    int i;
876

13260
    if (!codec->hw_configs || index < 0)
877
10814
        return NULL;
878
6432
    for (i = 0; i <= index; i++)
879
4824
        if (!codec->hw_configs[i])
880
838
            return NULL;
881
1608
    return &codec->hw_configs[index]->public;
882
}
883
884
#if FF_API_USER_VISIBLE_AVHWACCEL
885
AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
886
{
887
    return NULL;
888
}
889
890
void av_register_hwaccel(AVHWAccel *hwaccel)
891
{
892
}
893
#endif
894
895
630985
unsigned int avpriv_toupper4(unsigned int x)
896
{
897
630985
    return av_toupper(x & 0xFF) +
898
630985
          (av_toupper((x >>  8) & 0xFF) << 8)  +
899
1261970
          (av_toupper((x >> 16) & 0xFF) << 16) +
900
630985
((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
901
}
902
903
137745
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
904
{
905
    int ret;
906
907
137745
    dst->owner[0] = src->owner[0];
908
137745
    dst->owner[1] = src->owner[1];
909
910
137745
    ret = av_frame_ref(dst->f, src->f);
911
137745
    if (ret < 0)
912
6
        return ret;
913
914
137739
    av_assert0(!dst->progress);
915
916
137739
    if (src->progress &&
917
333
        !(dst->progress = av_buffer_ref(src->progress))) {
918
        ff_thread_release_buffer(dst->owner[0], dst);
919
        return AVERROR(ENOMEM);
920
    }
921
922
137739
    return 0;
923
}
924
925
#if !HAVE_THREADS
926
927
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
928
{
929
    return ff_get_format(avctx, fmt);
930
}
931
932
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
933
{
934
    f->owner[0] = f->owner[1] = avctx;
935
    return ff_get_buffer(avctx, f->f, flags);
936
}
937
938
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
939
{
940
    if (f->f)
941
        av_frame_unref(f->f);
942
}
943
944
void ff_thread_finish_setup(AVCodecContext *avctx)
945
{
946
}
947
948
void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
949
{
950
}
951
952
void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
953
{
954
}
955
956
int ff_thread_can_start_frame(AVCodecContext *avctx)
957
{
958
    return 1;
959
}
960
961
int ff_alloc_entries(AVCodecContext *avctx, int count)
962
{
963
    return 0;
964
}
965
966
void ff_reset_entries(AVCodecContext *avctx)
967
{
968
}
969
970
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
971
{
972
}
973
974
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
975
{
976
}
977
978
#endif
979
980
570117
const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
981
                                      const uint8_t *end,
982
                                      uint32_t *av_restrict state)
983
{
984
    int i;
985
986
570117
    av_assert0(p <= end);
987
570117
    if (p >= end)
988
555
        return end;
989
990
2269157
    for (i = 0; i < 3; i++) {
991
1703302
        uint32_t tmp = *state << 8;
992
1703302
        *state = tmp + *(p++);
993

1703302
        if (tmp == 0x100 || p == end)
994
3707
            return p;
995
    }
996
997
162535066
    while (p < end) {
998
162477899
        if      (p[-1] > 1      ) p += 3;
999
21512651
        else if (p[-2]          ) p += 2;
1000
13587356
        else if (p[-3]|(p[-1]-1)) p++;
1001
        else {
1002
508688
            p++;
1003
508688
            break;
1004
        }
1005
    }
1006
1007
565855
    p = FFMIN(p, end) - 4;
1008
565855
    *state = AV_RB32(p);
1009
1010
565855
    return p + 4;
1011
}
1012
1013
421
AVCPBProperties *av_cpb_properties_alloc(size_t *size)
1014
{
1015
421
    AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
1016
421
    if (!props)
1017
        return NULL;
1018
1019
421
    if (size)
1020
421
        *size = sizeof(*props);
1021
1022
421
    props->vbv_delay = UINT64_MAX;
1023
1024
421
    return props;
1025
}
1026
1027
834
AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
1028
{
1029
    AVPacketSideData *tmp;
1030
    AVCPBProperties  *props;
1031
    size_t size;
1032
    int i;
1033
1034
834
    for (i = 0; i < avctx->nb_coded_side_data; i++)
1035
438
        if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
1036
438
            return (AVCPBProperties *)avctx->coded_side_data[i].data;
1037
1038
396
    props = av_cpb_properties_alloc(&size);
1039
396
    if (!props)
1040
        return NULL;
1041
1042
396
    tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1043
396
    if (!tmp) {
1044
        av_freep(&props);
1045
        return NULL;
1046
    }
1047
1048
396
    avctx->coded_side_data = tmp;
1049
396
    avctx->nb_coded_side_data++;
1050
1051
396
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1052
396
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1053
396
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1054
1055
396
    return props;
1056
}
1057
1058
static unsigned bcd2uint(uint8_t bcd)
1059
{
1060
    unsigned low  = bcd & 0xf;
1061
    unsigned high = bcd >> 4;
1062
    if (low > 9 || high > 9)
1063
        return 0;
1064
    return low + 10*high;
1065
}
1066
1067
int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
1068
                     void **data, size_t *sei_size)
1069
{
1070
    AVFrameSideData *sd = NULL;
1071
    uint8_t *sei_data;
1072
    PutBitContext pb;
1073
    uint32_t *tc;
1074
    int m;
1075
1076
    if (frame)
1077
        sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
1078
1079
    if (!sd) {
1080
        *data = NULL;
1081
        return 0;
1082
    }
1083
    tc =  (uint32_t*)sd->data;
1084
    m  = tc[0] & 3;
1085
1086
    *sei_size = sizeof(uint32_t) * 4;
1087
    *data = av_mallocz(*sei_size + prefix_len);
1088
    if (!*data)
1089
        return AVERROR(ENOMEM);
1090
    sei_data = (uint8_t*)*data + prefix_len;
1091
1092
    init_put_bits(&pb, sei_data, *sei_size);
1093
    put_bits(&pb, 2, m); // num_clock_ts
1094
1095
    for (int j = 1; j <= m; j++) {
1096
        uint32_t tcsmpte = tc[j];
1097
        unsigned hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
1098
        unsigned mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
1099
        unsigned ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
1100
        unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
1101
        unsigned drop = tcsmpte & 1<<30 && !0;  // 1-bit drop if not arbitrary bit
1102
1103
        /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
1104
        if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
1105
            unsigned pc;
1106
            ff *= 2;
1107
            if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
1108
                pc = !!(tcsmpte & 1 << 7);
1109
            else
1110
                pc = !!(tcsmpte & 1 << 23);
1111
            ff = (ff + pc) & 0x7f;
1112
        }
1113
1114
        put_bits(&pb, 1, 1); // clock_timestamp_flag
1115
        put_bits(&pb, 1, 1); // units_field_based_flag
1116
        put_bits(&pb, 5, 0); // counting_type
1117
        put_bits(&pb, 1, 1); // full_timestamp_flag
1118
        put_bits(&pb, 1, 0); // discontinuity_flag
1119
        put_bits(&pb, 1, drop);
1120
        put_bits(&pb, 9, ff);
1121
        put_bits(&pb, 6, ss);
1122
        put_bits(&pb, 6, mm);
1123
        put_bits(&pb, 5, hh);
1124
        put_bits(&pb, 5, 0);
1125
    }
1126
    flush_put_bits(&pb);
1127
1128
    return 0;
1129
}
1130
1131
4135
int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
1132
{
1133
4135
    AVRational framerate = avctx->framerate;
1134
4135
    int bits_per_coded_sample = avctx->bits_per_coded_sample;
1135
    int64_t bitrate;
1136
1137

4135
    if (!(framerate.num && framerate.den))
1138
1
        framerate = av_inv_q(avctx->time_base);
1139

4135
    if (!(framerate.num && framerate.den))
1140
        return 0;
1141
1142
4135
    if (!bits_per_coded_sample) {
1143
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1144
        bits_per_coded_sample = av_get_bits_per_pixel(desc);
1145
    }
1146
4135
    bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
1147
4135
              framerate.num / framerate.den;
1148
1149
4135
    return bitrate;
1150
}
1151
1152
2400
int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
1153
                                const int * array_valid_values, int default_value)
1154
{
1155
2400
    int i = 0, ref_val;
1156
1157
    while (1) {
1158
6400
        ref_val = array_valid_values[i];
1159
6400
        if (ref_val == INT_MAX)
1160
            break;
1161
6400
        if (val == ref_val)
1162
2400
            return val;
1163
4000
        i++;
1164
    }
1165
    /* val is not a valid value */
1166
    av_log(ctx, AV_LOG_DEBUG,
1167
           "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
1168
    return default_value;
1169
}