Coverage Report

Created: 2026-10-28 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/encode.c
Line
Count
Source
1
/*
2
 * generic encoding-related code
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/avassert.h"
22
#include "libavutil/channel_layout.h"
23
#include "libavutil/emms.h"
24
#include "libavutil/frame.h"
25
#include "libavutil/internal.h"
26
#include "libavutil/intreadwrite.h"
27
#include "libavutil/mem.h"
28
#include "libavutil/opt.h"
29
#include "libavutil/pixdesc.h"
30
#include "libavutil/samplefmt.h"
31
32
#include "avcodec.h"
33
#include "avcodec_internal.h"
34
#include "codec_desc.h"
35
#include "codec_internal.h"
36
#include "encode.h"
37
#include "frame_thread_encoder.h"
38
#include "internal.h"
39
40
typedef struct EncodeContext {
41
    AVCodecInternal avci;
42
43
    /**
44
     * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
45
     * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
46
     * This is used to set said flag generically for said encoders.
47
     */
48
    int intra_only_flag;
49
50
    /**
51
     * An audio frame with less than required samples has been submitted (and
52
     * potentially padded with silence). Reject all subsequent frames.
53
     */
54
    int last_audio_frame;
55
} EncodeContext;
56
57
static EncodeContext *encode_ctx(AVCodecInternal *avci)
58
0
{
59
0
    return (EncodeContext*)avci;
60
0
}
61
62
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63
0
{
64
0
    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
65
0
        av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
66
0
               size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
67
0
        return AVERROR(EINVAL);
68
0
    }
69
70
0
    av_assert0(!avpkt->data);
71
72
0
    av_fast_padded_malloc(&avctx->internal->byte_buffer,
73
0
                          &avctx->internal->byte_buffer_size, size);
74
0
    avpkt->data = avctx->internal->byte_buffer;
75
0
    if (!avpkt->data) {
76
0
        av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
77
0
        return AVERROR(ENOMEM);
78
0
    }
79
0
    avpkt->size = size;
80
81
0
    return 0;
82
0
}
83
84
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85
0
{
86
0
    int ret;
87
88
0
    if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89
0
        return AVERROR(EINVAL);
90
91
0
    if (avpkt->data || avpkt->buf) {
92
0
        av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
93
0
        return AVERROR(EINVAL);
94
0
    }
95
96
0
    ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
0
    if (ret < 0) {
98
0
        av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
99
0
        return ret;
100
0
    }
101
0
    avpkt->data = avpkt->buf->data;
102
103
0
    return 0;
104
0
}
105
106
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
107
0
{
108
0
    int ret;
109
110
0
    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111
0
        return AVERROR(EINVAL);
112
113
0
    av_assert0(!avpkt->data && !avpkt->buf);
114
115
0
    avpkt->size = size;
116
0
    ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
0
    if (ret < 0)
118
0
        goto fail;
119
120
0
    if (!avpkt->data || !avpkt->buf) {
121
0
        av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
122
0
        ret = AVERROR(EINVAL);
123
0
        goto fail;
124
0
    }
125
0
    memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127
0
    ret = 0;
128
0
fail:
129
0
    if (ret < 0) {
130
0
        av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131
0
        av_packet_unref(avpkt);
132
0
    }
133
134
0
    return ret;
135
0
}
136
137
static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138
0
{
139
0
    uint8_t *data = avpkt->data;
140
0
    int ret;
141
142
0
    if (avpkt->buf)
143
0
        return 0;
144
145
0
    avpkt->data = NULL;
146
0
    ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
0
    if (ret < 0)
148
0
        return ret;
149
0
    memcpy(avpkt->data, data, avpkt->size);
150
151
0
    return 0;
152
0
}
153
154
/**
155
 * Pad last frame with silence.
156
 */
157
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158
0
{
159
0
    AVFrameSideData *sd;
160
0
    int discard_padding;
161
0
    int ret;
162
163
0
    frame->format         = src->format;
164
0
    frame->nb_samples     = out_samples;
165
0
    ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
166
0
    if (ret < 0)
167
0
        goto fail;
168
0
    ret = av_frame_get_buffer(frame, 0);
169
0
    if (ret < 0)
170
0
        goto fail;
171
172
0
    ret = av_frame_copy_props(frame, src);
173
0
    if (ret < 0)
174
0
        goto fail;
175
176
0
    if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
177
0
                               src->nb_samples, s->ch_layout.nb_channels,
178
0
                               s->sample_fmt)) < 0)
179
0
        goto fail;
180
0
    if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
181
0
                                      frame->nb_samples - src->nb_samples,
182
0
                                      s->ch_layout.nb_channels, s->sample_fmt)) < 0)
183
0
        goto fail;
184
185
0
    discard_padding = frame->nb_samples - src->nb_samples;
186
0
    av_assert1(discard_padding > 0);
187
0
    sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
188
0
    if (!sd) {
189
0
        ret = AVERROR(ENOMEM);
190
0
        goto fail;
191
0
    }
192
0
    AV_WL32A(sd->data, 0);
193
0
    AV_WL32A(sd->data + 4, discard_padding);
194
0
    AV_WL16A(sd->data + 8, 0);
195
196
0
    return 0;
197
198
0
fail:
199
0
    av_frame_unref(frame);
200
0
    encode_ctx(s->internal)->last_audio_frame = 0;
201
0
    return ret;
202
0
}
203
204
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
205
                            const AVSubtitle *sub)
206
0
{
207
0
    int ret;
208
0
    if (sub->start_display_time) {
209
0
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
210
0
        return -1;
211
0
    }
212
0
    av_assert0(buf_size >= 0);
213
214
0
    ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
215
0
    avctx->frame_num++;
216
0
    return ret;
217
0
}
218
219
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
220
0
{
221
0
    AVCodecInternal *avci = avctx->internal;
222
223
0
    if (avci->draining)
224
0
        return AVERROR_EOF;
225
226
0
    if (!avci->buffer_frame->buf[0])
227
0
        return AVERROR(EAGAIN);
228
229
0
    av_frame_move_ref(frame, avci->buffer_frame);
230
231
0
    return 0;
232
0
}
233
234
static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
235
0
{
236
0
    AVCodecInternal *avci = avctx->internal;
237
0
    EncodeContext     *ec = encode_ctx(avci);
238
239
0
    if (avpkt->pts == AV_NOPTS_VALUE) {
240
0
        avpkt->pts = frame->pts;
241
0
        if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
242
0
            avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
243
0
    }
244
245
0
    if (!avpkt->duration) {
246
0
        if (frame->duration)
247
0
            avpkt->duration = frame->duration;
248
0
        else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
249
0
            avpkt->duration = ff_samples_to_time_base(avctx,
250
0
                                                      frame->nb_samples);
251
0
        }
252
0
        if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
253
0
            AVFrameSideData *frame_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
254
255
0
            if (frame_sd && frame_sd->size >= 10) {
256
0
                int skip_samples    = AV_RL32(frame_sd->data + 0);
257
0
                int discard_padding = AV_RL32(frame_sd->data + 4);
258
259
0
                if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) {
260
0
                    avpkt->duration = av_sat_add64(avpkt->duration, ff_samples_to_time_base(avctx, avctx->initial_padding));
261
0
                    avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, avctx->frame_size));
262
0
                    discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
263
0
                }
264
265
0
                if (skip_samples > 0 || discard_padding > 0) {
266
0
                    uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
267
0
                    if (!packet_sd)
268
0
                         return AVERROR(ENOMEM);
269
0
                    AV_WL32A(packet_sd + 0, skip_samples);
270
0
                    AV_WL32A(packet_sd + 4, discard_padding);
271
0
                    AV_WL8  (packet_sd + 8, AV_RB8(frame_sd->data + 8));
272
0
                    AV_WL8  (packet_sd + 9, AV_RB8(frame_sd->data + 9));
273
0
                }
274
0
            }
275
0
        }
276
0
    }
277
278
0
    return 0;
279
0
}
280
281
int ff_encode_reordered_opaque(AVCodecContext *avctx,
282
                               AVPacket *pkt, const AVFrame *frame)
283
0
{
284
0
    if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
285
0
        int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
286
0
        if (ret < 0)
287
0
            return ret;
288
0
        pkt->opaque = frame->opaque;
289
0
    }
290
291
0
    return 0;
292
0
}
293
294
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
295
                        AVFrame *frame, int *got_packet)
296
0
{
297
0
    const FFCodec *const codec = ffcodec(avctx->codec);
298
0
    int ret;
299
300
0
    ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
301
0
    ff_assert1_fpu();
302
0
    av_assert0(ret <= 0);
303
304
0
    if (!ret && *got_packet) {
305
0
        if (avpkt->data) {
306
0
            ret = encode_make_refcounted(avctx, avpkt);
307
0
            if (ret < 0)
308
0
                goto unref;
309
            // Date returned by encoders must always be ref-counted
310
0
            av_assert0(avpkt->buf);
311
0
        }
312
313
        // set the timestamps for the simple no-delay case
314
        // encoders with delay have to set the timestamps themselves
315
0
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
316
0
            (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
317
0
            ret = encode_set_packet_props(avctx, avpkt, frame);
318
0
            if (ret < 0)
319
0
                goto unref;
320
321
0
            ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
322
0
            if (ret < 0)
323
0
                goto unref;
324
0
        }
325
326
        // dts equals pts unless there is reordering
327
        // there can be no reordering if there is no encoder delay
328
0
        if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
329
0
            !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)        ||
330
0
            (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
331
0
            avpkt->dts = avpkt->pts;
332
0
    } else {
333
0
unref:
334
0
        av_packet_unref(avpkt);
335
0
    }
336
337
0
    if (frame)
338
0
        av_frame_unref(frame);
339
340
0
    return ret;
341
0
}
342
343
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
344
0
{
345
0
    AVCodecInternal   *avci = avctx->internal;
346
0
    AVFrame          *frame = avci->in_frame;
347
0
    const FFCodec *const codec = ffcodec(avctx->codec);
348
0
    int got_packet;
349
0
    int ret;
350
351
0
    if (avci->draining_done)
352
0
        return AVERROR_EOF;
353
354
0
    if (!frame->buf[0] && !avci->draining) {
355
0
        av_frame_unref(frame);
356
0
        ret = ff_encode_get_frame(avctx, frame);
357
0
        if (ret < 0 && ret != AVERROR_EOF)
358
0
            return ret;
359
0
    }
360
361
0
    if (!frame->buf[0]) {
362
0
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
363
0
              avci->frame_thread_encoder))
364
0
            return AVERROR_EOF;
365
366
        // Flushing is signaled with a NULL frame
367
0
        frame = NULL;
368
0
    }
369
370
0
    got_packet = 0;
371
372
0
    av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
373
374
#if CONFIG_FRAME_THREAD_ENCODER
375
    if (avci->frame_thread_encoder)
376
        /* This will unref frame. */
377
        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
378
    else
379
#endif
380
0
        ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
381
382
0
    if (avci->draining && !got_packet)
383
0
        avci->draining_done = 1;
384
385
0
    return ret;
386
0
}
387
388
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
389
0
{
390
0
    int ret;
391
392
0
    while (!avpkt->data && !avpkt->side_data) {
393
0
        ret = encode_simple_internal(avctx, avpkt);
394
0
        if (ret < 0)
395
0
            return ret;
396
0
    }
397
398
0
    return 0;
399
0
}
400
401
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
402
0
{
403
0
    AVCodecInternal *avci = avctx->internal;
404
0
    int ret;
405
406
0
    if (avci->draining_done)
407
0
        return AVERROR_EOF;
408
409
0
    av_assert0(!avpkt->data && !avpkt->side_data);
410
411
0
    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
412
0
        if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
413
0
            avctx->stats_out[0] = '\0';
414
0
    }
415
416
0
    if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
417
0
        ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
418
0
        if (ret < 0)
419
0
            av_packet_unref(avpkt);
420
0
        else
421
            // Encoders must always return ref-counted buffers.
422
            // Side-data only packets have no data and can be not ref-counted.
423
0
            av_assert0(!avpkt->data || avpkt->buf);
424
0
    } else
425
0
        ret = encode_simple_receive_packet(avctx, avpkt);
426
0
    if (ret >= 0)
427
0
        avpkt->flags |= encode_ctx(avci)->intra_only_flag;
428
429
0
    if (ret == AVERROR_EOF)
430
0
        avci->draining_done = 1;
431
432
0
    return ret;
433
0
}
434
435
#if CONFIG_LCMS2
436
static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
437
{
438
    enum AVColorTransferCharacteristic trc = frame->color_trc;
439
    enum AVColorPrimaries prim = frame->color_primaries;
440
    const FFCodec *const codec = ffcodec(avctx->codec);
441
    AVCodecInternal *avci = avctx->internal;
442
    cmsHPROFILE profile;
443
    int ret;
444
445
    /* don't generate ICC profiles if disabled or unsupported */
446
    if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
447
        return 0;
448
    if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
449
        return 0;
450
451
    if (trc == AVCOL_TRC_UNSPECIFIED)
452
        trc = avctx->color_trc;
453
    if (prim == AVCOL_PRI_UNSPECIFIED)
454
        prim = avctx->color_primaries;
455
    if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
456
        return 0; /* can't generate ICC profile with missing csp tags */
457
458
    if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
459
        return 0; /* don't overwrite existing ICC profile */
460
461
    if (!avci->icc.avctx) {
462
        ret = ff_icc_context_init(&avci->icc, avctx);
463
        if (ret < 0)
464
            return ret;
465
    }
466
467
    ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
468
    if (ret < 0)
469
        return ret;
470
471
    ret = ff_icc_profile_attach(&avci->icc, profile, frame);
472
    cmsCloseProfile(profile);
473
    return ret;
474
}
475
#else /* !CONFIG_LCMS2 */
476
static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
477
0
{
478
0
    return 0;
479
0
}
480
#endif
481
482
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
483
0
{
484
0
    AVCodecInternal *avci = avctx->internal;
485
0
    EncodeContext     *ec = encode_ctx(avci);
486
0
    AVFrame *dst = avci->buffer_frame;
487
0
    int ret;
488
489
0
    if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
490
        /* extract audio service type metadata */
491
0
        AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
492
0
        if (sd && sd->size >= sizeof(enum AVAudioServiceType))
493
0
            avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
494
495
        /* check for valid frame size */
496
0
        if (avctx->frame_size) {
497
            /* if we already got an undersized frame, that must have been the last */
498
0
            if (ec->last_audio_frame) {
499
0
                av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
500
0
                return AVERROR(EINVAL);
501
0
            }
502
0
            if (src->nb_samples > avctx->frame_size) {
503
0
                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
504
0
                return AVERROR(EINVAL);
505
0
            }
506
0
            if (src->nb_samples < avctx->frame_size) {
507
0
                ec->last_audio_frame = 1;
508
0
                if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
509
0
                    (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
510
0
                    int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
511
0
                    int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
512
513
0
                    if (out_samples != src->nb_samples) {
514
0
                        ret = pad_last_frame(avctx, dst, src, out_samples);
515
0
                        if (ret < 0)
516
0
                            return ret;
517
0
                        goto finish;
518
0
                    }
519
0
                }
520
0
            }
521
0
        }
522
0
    } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
523
0
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
524
525
0
        if (!(desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
526
0
            (src->format != avctx->pix_fmt ||
527
0
             src->width  < avctx->width  || src->height < avctx->height)) {
528
0
            av_log(avctx, AV_LOG_ERROR, "Frame size or pixel format does not match the encoder\n");
529
0
            return AVERROR(EINVAL);
530
0
        }
531
0
    }
532
533
0
    ret = av_frame_ref(dst, src);
534
0
    if (ret < 0)
535
0
        return ret;
536
537
0
finish:
538
539
0
    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
540
0
        ret = encode_generate_icc_profile(avctx, dst);
541
0
        if (ret < 0)
542
0
            return ret;
543
0
    }
544
545
    // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
546
    // since otherwise we cannot be sure that whatever value it has is in the
547
    // right timebase, so we would produce an incorrect value, which is worse
548
    // than none at all
549
0
    if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
550
0
        dst->duration = 0;
551
552
0
    return 0;
553
0
}
554
555
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
556
0
{
557
0
    AVCodecInternal *avci = avctx->internal;
558
0
    int ret;
559
560
0
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
561
0
        return AVERROR(EINVAL);
562
563
0
    if (avci->draining)
564
0
        return AVERROR_EOF;
565
566
0
    if (avci->buffer_frame->buf[0])
567
0
        return AVERROR(EAGAIN);
568
569
0
    if (!frame) {
570
0
        avci->draining = 1;
571
0
    } else {
572
0
        ret = encode_send_frame_internal(avctx, frame);
573
0
        if (ret < 0)
574
0
            return ret;
575
0
    }
576
577
0
    if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
578
0
        ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
579
0
        if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
580
0
            return ret;
581
0
    }
582
583
0
    avctx->frame_num++;
584
585
0
    return 0;
586
0
}
587
588
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
589
0
{
590
0
    AVCodecInternal *avci = avctx->internal;
591
0
    int ret;
592
593
0
    av_packet_unref(avpkt);
594
595
0
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
596
0
        return AVERROR(EINVAL);
597
598
0
    if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
599
0
        av_packet_move_ref(avpkt, avci->buffer_pkt);
600
0
    } else {
601
0
        ret = encode_receive_packet_internal(avctx, avpkt);
602
0
        if (ret < 0)
603
0
            return ret;
604
0
    }
605
606
0
    return 0;
607
0
}
608
609
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
610
0
{
611
0
    const AVCodec *codec = avctx->codec;
612
0
    const FFCodec *codec2 = ffcodec(codec);
613
0
    const AVDictionaryEntry *t = NULL;
614
0
    AVDictionary *copy = NULL;
615
0
    int ret;
616
617
0
    av_assert0(av_codec_is_encoder(codec) && (codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF));
618
619
0
    ret = av_dict_copy(&copy, *dict, 0);
620
0
    if (ret < 0)
621
0
        goto end;
622
623
    // Remove the dictionary entries that would be applied to the private codec context
624
0
    if (codec->priv_class) {
625
0
        while ((t = av_dict_iterate(*dict, t))) {
626
0
            if (av_opt_find(avctx->priv_data, t->key, NULL,
627
0
                            AV_OPT_FLAG_RUNTIME_PARAM, 0))
628
0
                av_dict_set(dict, t->key, NULL, 0);
629
0
        }
630
0
    }
631
632
    // Ditto for global options
633
0
    if (codec2->defaults) {
634
0
        const FFCodecDefault *d = codec2->defaults;
635
0
        while (d->key) {
636
0
            if (d->flags & AV_OPT_FLAG_RUNTIME_PARAM)
637
0
                av_dict_set(dict, d->key, NULL, 0);
638
0
            d++;
639
0
        }
640
0
    }
641
642
    // If any entry remains, then the requested option/s don't exist or are not settable.
643
0
    if (av_dict_count(*dict)) {
644
0
        ret = AVERROR_OPTION_NOT_FOUND;
645
0
        goto end;
646
0
    }
647
648
0
    ret = av_dict_copy(dict, copy, 0);
649
0
    if (ret < 0)
650
0
        goto end;
651
652
    // Do a dry run of applying the options, to ensure the encoder is unchanged in case
653
    // one of them has an invalid value.
654
    // This is done twice, once for avctx and once for the AVCodec, because using the
655
    // search children flag in combination with the fake obj flag will iterate through
656
    // the options from all compiled in codecs if you pass the avctx class.
657
0
    if (codec->priv_class) {
658
0
        ret = av_opt_set_dict2((void *)&codec->priv_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
659
0
        if (ret < 0)
660
0
            goto end;
661
0
    }
662
0
    ret = av_opt_set_dict2((void *)&avctx->av_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
663
0
    if (ret < 0)
664
0
        goto end;
665
666
    // The dictionary should be empty.
667
0
    av_assert0(!av_dict_count(copy));
668
669
0
    ret = av_opt_set_dict2(avctx, dict, AV_OPT_SEARCH_CHILDREN);
670
0
    if (ret < 0)
671
0
        goto end;
672
673
    // The dictionary should be empty.
674
0
    av_assert0(!av_dict_count(*dict));
675
676
0
    ret = 0;
677
0
end:
678
0
    av_dict_free(&copy);
679
680
0
    return ret;
681
0
}
682
683
av_cold int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **dict)
684
0
{
685
0
    const FFCodec *codec = ffcodec(avctx->codec);
686
0
    int ret = AVERROR_BUG;
687
688
0
    if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
689
0
        return AVERROR(EINVAL);
690
691
0
    if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF)) {
692
0
        av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
693
0
        return AVERROR(ENOSYS);
694
0
    }
695
696
0
    if (codec->reconf)
697
0
        ret = codec->reconf(avctx, dict);
698
0
    else
699
0
        ret = ff_encode_reconf_parse_dict(avctx, dict);
700
0
    if (ret < 0)
701
0
        return ret;
702
703
0
    return 0;
704
0
}
705
706
static int encode_preinit_video(AVCodecContext *avctx)
707
0
{
708
0
    const AVCodec *c = avctx->codec;
709
0
    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
710
0
    const enum AVPixelFormat *pix_fmts;
711
0
    int ret, i, num_pix_fmts;
712
713
0
    if (!pixdesc) {
714
0
        av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
715
0
               avctx->pix_fmt);
716
0
        return AVERROR(EINVAL);
717
0
    }
718
719
0
    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
720
0
                                       0, (const void **) &pix_fmts, &num_pix_fmts);
721
0
    if (ret < 0)
722
0
        return ret;
723
724
0
    if (pix_fmts) {
725
0
        for (i = 0; i < num_pix_fmts; i++)
726
0
            if (avctx->pix_fmt == pix_fmts[i])
727
0
                break;
728
0
        if (i == num_pix_fmts) {
729
0
            av_log(avctx, AV_LOG_ERROR,
730
0
                   "Specified pixel format %s is not supported by the %s encoder.\n",
731
0
                   av_get_pix_fmt_name(avctx->pix_fmt), c->name);
732
733
0
            av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
734
0
            for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
735
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n",
736
0
                       av_get_pix_fmt_name(pix_fmts[p]));
737
0
            }
738
739
0
            return AVERROR(EINVAL);
740
0
        }
741
0
        if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
742
0
            pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
743
0
            pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
744
0
            pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
745
0
            pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
746
0
            avctx->color_range = AVCOL_RANGE_JPEG;
747
0
    }
748
749
0
    if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
750
0
        const enum AVAlphaMode *alpha_modes;
751
0
        int num_alpha_modes;
752
0
        ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_ALPHA_MODE,
753
0
                                           0, (const void **) &alpha_modes, &num_alpha_modes);
754
0
        if (ret < 0)
755
0
            return ret;
756
757
0
        if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
758
0
            for (i = 0; i < num_alpha_modes; i++) {
759
0
                if (avctx->alpha_mode == alpha_modes[i])
760
0
                    break;
761
0
            }
762
0
            if (i == num_alpha_modes) {
763
0
                av_log(avctx, AV_LOG_ERROR,
764
0
                       "Specified alpha mode '%s' is not supported by the %s encoder.\n",
765
0
                       av_alpha_mode_name(avctx->alpha_mode), c->name);
766
0
                av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
767
0
                for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
768
0
                    av_log(avctx, AV_LOG_ERROR, "  %s\n",
769
0
                           av_alpha_mode_name(alpha_modes[p]));
770
0
                }
771
0
                return AVERROR(EINVAL);
772
0
            }
773
0
        }
774
0
    }
775
776
0
    if (    avctx->bits_per_raw_sample < 0
777
0
        || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
778
0
        av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
779
0
            avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
780
0
        avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
781
0
    }
782
0
    if (avctx->width <= 0 || avctx->height <= 0) {
783
0
        av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
784
0
        return AVERROR(EINVAL);
785
0
    }
786
787
0
    if (avctx->hw_frames_ctx) {
788
0
        AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
789
0
        if (frames_ctx->format != avctx->pix_fmt) {
790
0
            av_log(avctx, AV_LOG_ERROR,
791
0
                   "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
792
0
            return AVERROR(EINVAL);
793
0
        }
794
0
        if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
795
0
            avctx->sw_pix_fmt != frames_ctx->sw_format) {
796
0
            av_log(avctx, AV_LOG_ERROR,
797
0
                   "Mismatching AVCodecContext.sw_pix_fmt (%s) "
798
0
                   "and AVHWFramesContext.sw_format (%s)\n",
799
0
                   av_get_pix_fmt_name(avctx->sw_pix_fmt),
800
0
                   av_get_pix_fmt_name(frames_ctx->sw_format));
801
0
            return AVERROR(EINVAL);
802
0
        }
803
0
        avctx->sw_pix_fmt = frames_ctx->sw_format;
804
0
    }
805
806
0
    return 0;
807
0
}
808
809
static int encode_preinit_audio(AVCodecContext *avctx)
810
0
{
811
0
    const AVCodec *c = avctx->codec;
812
0
    const enum AVSampleFormat *sample_fmts;
813
0
    const int *supported_samplerates;
814
0
    const AVChannelLayout *ch_layouts;
815
0
    int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
816
817
0
    if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
818
0
        av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
819
0
               avctx->sample_fmt);
820
0
        return AVERROR(EINVAL);
821
0
    }
822
823
0
    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
824
0
                                       0, (const void **) &sample_fmts,
825
0
                                       &num_sample_fmts);
826
0
    if (ret < 0)
827
0
        return ret;
828
0
    if (sample_fmts) {
829
0
        for (i = 0; i < num_sample_fmts; i++) {
830
0
            if (avctx->sample_fmt == sample_fmts[i])
831
0
                break;
832
0
            if (avctx->ch_layout.nb_channels == 1 &&
833
0
                av_get_planar_sample_fmt(avctx->sample_fmt) ==
834
0
                av_get_planar_sample_fmt(sample_fmts[i])) {
835
0
                avctx->sample_fmt = sample_fmts[i];
836
0
                break;
837
0
            }
838
0
        }
839
0
        if (i == num_sample_fmts) {
840
0
            av_log(avctx, AV_LOG_ERROR,
841
0
                   "Specified sample format %s is not supported by the %s encoder\n",
842
0
                   av_get_sample_fmt_name(avctx->sample_fmt), c->name);
843
844
0
            av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
845
0
            for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
846
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n",
847
0
                       av_get_sample_fmt_name(sample_fmts[p]));
848
0
            }
849
850
0
            return AVERROR(EINVAL);
851
0
        }
852
0
    }
853
854
0
    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
855
0
                                       0, (const void **) &supported_samplerates,
856
0
                                       &num_samplerates);
857
0
    if (ret < 0)
858
0
        return ret;
859
0
    if (supported_samplerates) {
860
0
        for (i = 0; i < num_samplerates; i++)
861
0
            if (avctx->sample_rate == supported_samplerates[i])
862
0
                break;
863
0
        if (i == num_samplerates) {
864
0
            av_log(avctx, AV_LOG_ERROR,
865
0
                   "Specified sample rate %d is not supported by the %s encoder\n",
866
0
                   avctx->sample_rate, c->name);
867
868
0
            av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
869
0
            for (int p = 0; supported_samplerates[p]; p++)
870
0
                av_log(avctx, AV_LOG_ERROR, "  %d\n", supported_samplerates[p]);
871
872
0
            return AVERROR(EINVAL);
873
0
        }
874
0
    }
875
0
    ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
876
0
                                       0, (const void **) &ch_layouts, &num_ch_layouts);
877
0
    if (ret < 0)
878
0
        return ret;
879
0
    if (ch_layouts) {
880
0
        for (i = 0; i < num_ch_layouts; i++) {
881
0
            if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
882
0
                break;
883
0
        }
884
0
        if (i == num_ch_layouts) {
885
0
            char buf[512];
886
0
            int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
887
0
            av_log(avctx, AV_LOG_ERROR,
888
0
                   "Specified channel layout '%s' is not supported by the %s encoder\n",
889
0
                   ret > 0 ? buf : "?", c->name);
890
891
0
            av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
892
0
            for (int p = 0; ch_layouts[p].nb_channels; p++) {
893
0
                ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
894
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n", ret > 0 ? buf : "?");
895
0
            }
896
0
            return AVERROR(EINVAL);
897
0
        }
898
0
    }
899
900
0
    if (!avctx->bits_per_raw_sample)
901
0
        avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
902
0
    if (!avctx->bits_per_raw_sample)
903
0
        avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
904
905
0
    return 0;
906
0
}
907
908
int ff_encode_preinit(AVCodecContext *avctx)
909
0
{
910
0
    AVCodecInternal *avci = avctx->internal;
911
0
    EncodeContext     *ec = encode_ctx(avci);
912
0
    int ret = 0;
913
914
0
    if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
915
0
        av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
916
0
        return AVERROR(EINVAL);
917
0
    }
918
919
0
    if (avctx->bit_rate < 0) {
920
0
        av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
921
0
        return AVERROR(EINVAL);
922
0
    }
923
924
0
    if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
925
0
        !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
926
0
        av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
927
0
               "encoder does not support it.\n");
928
0
        return AVERROR(EINVAL);
929
0
    }
930
931
0
    switch (avctx->codec_type) {
932
0
    case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
933
0
    case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
934
0
    }
935
0
    if (ret < 0)
936
0
        return ret;
937
938
0
    if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
939
0
        && avctx->bit_rate>0 && avctx->bit_rate<1000) {
940
0
        av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
941
0
    }
942
943
0
    if (!avctx->rc_initial_buffer_occupancy)
944
0
        avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
945
946
0
    if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
947
0
        ec->intra_only_flag = AV_PKT_FLAG_KEY;
948
949
0
    if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
950
0
        avci->in_frame = av_frame_alloc();
951
0
        if (!avci->in_frame)
952
0
            return AVERROR(ENOMEM);
953
0
    }
954
955
0
    if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
956
0
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
957
0
            av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
958
0
                   "from an encoder not supporting it\n");
959
0
            return AVERROR(ENOSYS);
960
0
        }
961
962
0
        avci->recon_frame = av_frame_alloc();
963
0
        if (!avci->recon_frame)
964
0
            return AVERROR(ENOMEM);
965
0
    }
966
967
0
    for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
968
0
        const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
969
0
        const enum AVFrameSideDataType  type_frame  = ff_sd_global_map[i].frame;
970
0
        const AVFrameSideData *sd_frame;
971
0
        AVPacketSideData      *sd_packet;
972
973
0
        sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
974
0
                                          avctx->nb_decoded_side_data,
975
0
                                          type_frame);
976
0
        if (!sd_frame ||
977
0
            av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
978
0
                                    type_packet))
979
980
0
            continue;
981
982
0
        sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
983
0
                                            type_packet, sd_frame->size, 0);
984
0
        if (!sd_packet)
985
0
            return AVERROR(ENOMEM);
986
987
0
        memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
988
0
    }
989
990
#if CONFIG_FRAME_THREAD_ENCODER
991
    ret = ff_frame_thread_encoder_init(avctx);
992
    if (ret < 0)
993
        return ret;
994
#endif
995
996
0
    return 0;
997
0
}
998
999
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
1000
0
{
1001
0
    int ret;
1002
1003
0
    av_assert1(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1004
1005
0
    frame->format = avctx->pix_fmt;
1006
0
    if (frame->width <= 0 || frame->height <= 0) {
1007
0
        frame->width  = avctx->width;
1008
0
        frame->height = avctx->height;
1009
0
    }
1010
1011
0
    ret = avcodec_default_get_buffer2(avctx, frame, 0);
1012
0
    if (ret < 0) {
1013
0
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1014
0
        av_frame_unref(frame);
1015
0
        return ret;
1016
0
    }
1017
1018
0
    return 0;
1019
0
}
1020
1021
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1022
0
{
1023
0
    AVCodecInternal *avci = avctx->internal;
1024
1025
0
    if (!avci->recon_frame)
1026
0
        return AVERROR(EINVAL);
1027
0
    if (!avci->recon_frame->buf[0])
1028
0
        return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1029
1030
0
    av_frame_move_ref(frame, avci->recon_frame);
1031
0
    return 0;
1032
0
}
1033
1034
void ff_encode_flush_buffers(AVCodecContext *avctx)
1035
0
{
1036
0
    AVCodecInternal *avci = avctx->internal;
1037
1038
0
    if (avci->in_frame)
1039
0
        av_frame_unref(avci->in_frame);
1040
0
    if (avci->recon_frame)
1041
0
        av_frame_unref(avci->recon_frame);
1042
0
}
1043
1044
AVCodecInternal *ff_encode_internal_alloc(void)
1045
0
{
1046
0
    return av_mallocz(sizeof(EncodeContext));
1047
0
}
1048
1049
AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
1050
0
{
1051
0
    AVPacketSideData *tmp;
1052
0
    AVCPBProperties  *props;
1053
0
    size_t size;
1054
0
    int i;
1055
1056
0
    for (i = 0; i < avctx->nb_coded_side_data; i++)
1057
0
        if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
1058
0
            return (AVCPBProperties *)avctx->coded_side_data[i].data;
1059
1060
0
    props = av_cpb_properties_alloc(&size);
1061
0
    if (!props)
1062
0
        return NULL;
1063
1064
0
    tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1065
0
    if (!tmp) {
1066
0
        av_freep(&props);
1067
0
        return NULL;
1068
0
    }
1069
1070
0
    avctx->coded_side_data = tmp;
1071
0
    avctx->nb_coded_side_data++;
1072
1073
0
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1074
0
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1075
0
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1076
1077
0
    return props;
1078
0
}
1079
1080
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[],
1081
                                  int error_count, enum AVPictureType pict_type)
1082
0
{
1083
0
    uint8_t *side_data;
1084
0
    size_t side_data_size;
1085
1086
0
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1087
0
    if (!side_data) {
1088
0
        side_data_size = 4+4+8*error_count;
1089
0
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
1090
0
                                            side_data_size);
1091
0
    }
1092
1093
0
    if (!side_data || side_data_size < 4+4+8*error_count)
1094
0
        return AVERROR(ENOMEM);
1095
1096
0
    AV_WL32(side_data, quality);
1097
0
    side_data[4] = pict_type;
1098
0
    side_data[5] = error_count;
1099
0
    for (int i = 0; i < error_count; ++i)
1100
0
        AV_WL64(side_data+8 + 8*i , error[i]);
1101
1102
0
    return 0;
1103
0
}
1104
1105
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1106
0
{
1107
0
    uint16_t  *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1108
0
    const char   *names[] = {"Intra", "Inter", "Chroma Intra"};
1109
0
    static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1110
0
    for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1111
0
        uint16_t *matrix = matrices[m];
1112
0
        if (matrix && (types & (1U << m))) {
1113
0
            for (int i = 0; i < 64; i++) {
1114
0
                if (matrix[i] < min || matrix[i] > max) {
1115
0
                    av_log(avctx, AV_LOG_ERROR, "%s matrix[%d] is %d which is out of the allowed range [%"PRIu16"-%"PRIu16"].\n", names[m], i, matrix[i], min, max);
1116
0
                    return AVERROR(EINVAL);
1117
0
                }
1118
0
            }
1119
0
        }
1120
0
    }
1121
0
    return 0;
1122
0
}