Coverage Report

Created: 2026-08-25 06:40

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