Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/encode.c
Line
Count
Source (jump to first uncovered line)
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/attributes.h"
22
#include "libavutil/avassert.h"
23
#include "libavutil/channel_layout.h"
24
#include "libavutil/emms.h"
25
#include "libavutil/frame.h"
26
#include "libavutil/imgutils.h"
27
#include "libavutil/internal.h"
28
#include "libavutil/mem.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
1.56M
{
59
1.56M
    return (EncodeContext*)avci;
60
1.56M
}
61
62
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63
368k
{
64
368k
    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
368k
    av_assert0(!avpkt->data);
71
72
368k
    av_fast_padded_malloc(&avctx->internal->byte_buffer,
73
368k
                          &avctx->internal->byte_buffer_size, size);
74
368k
    avpkt->data = avctx->internal->byte_buffer;
75
368k
    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
368k
    avpkt->size = size;
80
81
368k
    return 0;
82
368k
}
83
84
int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85
737k
{
86
737k
    int ret;
87
88
737k
    if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89
0
        return AVERROR(EINVAL);
90
91
737k
    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
737k
    ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
737k
    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
737k
    avpkt->data = avpkt->buf->data;
102
103
737k
    return 0;
104
737k
}
105
106
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
107
737k
{
108
737k
    int ret;
109
110
737k
    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111
26
        return AVERROR(EINVAL);
112
113
737k
    av_assert0(!avpkt->data && !avpkt->buf);
114
115
737k
    avpkt->size = size;
116
737k
    ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
737k
    if (ret < 0)
118
0
        goto fail;
119
120
737k
    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
737k
    memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127
737k
    ret = 0;
128
737k
fail:
129
737k
    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
737k
    return ret;
135
737k
}
136
137
static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138
742k
{
139
742k
    uint8_t *data = avpkt->data;
140
742k
    int ret;
141
142
742k
    if (avpkt->buf)
143
374k
        return 0;
144
145
368k
    avpkt->data = NULL;
146
368k
    ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
368k
    if (ret < 0)
148
26
        return ret;
149
368k
    memcpy(avpkt->data, data, avpkt->size);
150
151
368k
    return 0;
152
368k
}
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
    int ret;
160
161
0
    frame->format         = src->format;
162
0
    frame->nb_samples     = out_samples;
163
0
    ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
164
0
    if (ret < 0)
165
0
        goto fail;
166
0
    ret = av_frame_get_buffer(frame, 0);
167
0
    if (ret < 0)
168
0
        goto fail;
169
170
0
    ret = av_frame_copy_props(frame, src);
171
0
    if (ret < 0)
172
0
        goto fail;
173
174
0
    if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
175
0
                               src->nb_samples, s->ch_layout.nb_channels,
176
0
                               s->sample_fmt)) < 0)
177
0
        goto fail;
178
0
    if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
179
0
                                      frame->nb_samples - src->nb_samples,
180
0
                                      s->ch_layout.nb_channels, s->sample_fmt)) < 0)
181
0
        goto fail;
182
183
0
    return 0;
184
185
0
fail:
186
0
    av_frame_unref(frame);
187
0
    encode_ctx(s->internal)->last_audio_frame = 0;
188
0
    return ret;
189
0
}
190
191
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
192
                            const AVSubtitle *sub)
193
0
{
194
0
    int ret;
195
0
    if (sub->start_display_time) {
196
0
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
197
0
        return -1;
198
0
    }
199
200
0
    ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
201
0
    avctx->frame_num++;
202
0
    return ret;
203
0
}
204
205
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
206
1.53M
{
207
1.53M
    AVCodecInternal *avci = avctx->internal;
208
209
1.53M
    if (avci->draining)
210
0
        return AVERROR_EOF;
211
212
1.53M
    if (!avci->buffer_frame->buf[0])
213
782k
        return AVERROR(EAGAIN);
214
215
750k
    av_frame_move_ref(frame, avci->buffer_frame);
216
217
750k
#if FF_API_FRAME_KEY
218
750k
FF_DISABLE_DEPRECATION_WARNINGS
219
750k
    if (frame->key_frame)
220
0
        frame->flags |= AV_FRAME_FLAG_KEY;
221
750k
FF_ENABLE_DEPRECATION_WARNINGS
222
750k
#endif
223
750k
#if FF_API_INTERLACED_FRAME
224
750k
FF_DISABLE_DEPRECATION_WARNINGS
225
750k
    if (frame->interlaced_frame)
226
0
        frame->flags |= AV_FRAME_FLAG_INTERLACED;
227
750k
    if (frame->top_field_first)
228
0
        frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
229
750k
FF_ENABLE_DEPRECATION_WARNINGS
230
750k
#endif
231
232
750k
    return 0;
233
1.53M
}
234
235
int ff_encode_reordered_opaque(AVCodecContext *avctx,
236
                               AVPacket *pkt, const AVFrame *frame)
237
629k
{
238
629k
    if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
239
0
        int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
240
0
        if (ret < 0)
241
0
            return ret;
242
0
        pkt->opaque = frame->opaque;
243
0
    }
244
245
629k
    return 0;
246
629k
}
247
248
int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
249
                        AVFrame *frame, int *got_packet)
250
794k
{
251
794k
    const FFCodec *const codec = ffcodec(avctx->codec);
252
794k
    int ret;
253
254
794k
    ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
255
794k
    emms_c();
256
794k
    av_assert0(ret <= 0);
257
258
794k
    if (!ret && *got_packet) {
259
742k
        if (avpkt->data) {
260
742k
            ret = encode_make_refcounted(avctx, avpkt);
261
742k
            if (ret < 0)
262
26
                goto unref;
263
            // Date returned by encoders must always be ref-counted
264
742k
            av_assert0(avpkt->buf);
265
742k
        }
266
267
        // set the timestamps for the simple no-delay case
268
        // encoders with delay have to set the timestamps themselves
269
742k
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
270
742k
            (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
271
568k
            if (avpkt->pts == AV_NOPTS_VALUE)
272
561k
                avpkt->pts = frame->pts;
273
274
568k
            if (!avpkt->duration) {
275
568k
                if (frame->duration)
276
0
                    avpkt->duration = frame->duration;
277
568k
                else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
278
0
                    avpkt->duration = ff_samples_to_time_base(avctx,
279
0
                                                              frame->nb_samples);
280
0
                }
281
568k
            }
282
283
568k
            ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
284
568k
            if (ret < 0)
285
0
                goto unref;
286
568k
        }
287
288
        // dts equals pts unless there is reordering
289
        // there can be no reordering if there is no encoder delay
290
742k
        if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
291
742k
            !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)        ||
292
742k
            (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
293
740k
            avpkt->dts = avpkt->pts;
294
742k
    } else {
295
52.2k
unref:
296
52.2k
        av_packet_unref(avpkt);
297
52.2k
    }
298
299
794k
    if (frame)
300
750k
        av_frame_unref(frame);
301
302
794k
    return ret;
303
794k
}
304
305
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
306
1.63M
{
307
1.63M
    AVCodecInternal   *avci = avctx->internal;
308
1.63M
    AVFrame          *frame = avci->in_frame;
309
1.63M
    const FFCodec *const codec = ffcodec(avctx->codec);
310
1.63M
    int got_packet;
311
1.63M
    int ret;
312
313
1.63M
    if (avci->draining_done)
314
17.0k
        return AVERROR_EOF;
315
316
1.62M
    if (!frame->buf[0] && !avci->draining) {
317
1.53M
        av_frame_unref(frame);
318
1.53M
        ret = ff_encode_get_frame(avctx, frame);
319
1.53M
        if (ret < 0 && ret != AVERROR_EOF)
320
782k
            return ret;
321
1.53M
    }
322
323
840k
    if (!frame->buf[0]) {
324
89.7k
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
325
89.7k
              avci->frame_thread_encoder))
326
45.7k
            return AVERROR_EOF;
327
328
        // Flushing is signaled with a NULL frame
329
44.0k
        frame = NULL;
330
44.0k
    }
331
332
794k
    got_packet = 0;
333
334
794k
    av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
335
336
794k
    if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
337
        /* This will unref frame. */
338
0
        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
339
794k
    else {
340
794k
        ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
341
794k
    }
342
343
794k
    if (avci->draining && !got_packet)
344
17.2k
        avci->draining_done = 1;
345
346
794k
    return ret;
347
794k
}
348
349
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
350
1.58M
{
351
1.58M
    int ret;
352
353
2.38M
    while (!avpkt->data && !avpkt->side_data) {
354
1.63M
        ret = encode_simple_internal(avctx, avpkt);
355
1.63M
        if (ret < 0)
356
847k
            return ret;
357
1.63M
    }
358
359
742k
    return 0;
360
1.58M
}
361
362
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
363
1.64M
{
364
1.64M
    AVCodecInternal *avci = avctx->internal;
365
1.64M
    int ret;
366
367
1.64M
    if (avci->draining_done)
368
55.7k
        return AVERROR_EOF;
369
370
1.58M
    av_assert0(!avpkt->data && !avpkt->side_data);
371
372
1.58M
    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
373
1.58M
        if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
374
0
            avctx->stats_out[0] = '\0';
375
1.58M
        if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
376
0
            return AVERROR(EINVAL);
377
1.58M
    }
378
379
1.58M
    if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
380
0
        ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
381
0
        if (ret < 0)
382
0
            av_packet_unref(avpkt);
383
0
        else
384
            // Encoders must always return ref-counted buffers.
385
            // Side-data only packets have no data and can be not ref-counted.
386
0
            av_assert0(!avpkt->data || avpkt->buf);
387
0
    } else
388
1.58M
        ret = encode_simple_receive_packet(avctx, avpkt);
389
1.58M
    if (ret >= 0)
390
742k
        avpkt->flags |= encode_ctx(avci)->intra_only_flag;
391
392
1.58M
    if (ret == AVERROR_EOF)
393
62.7k
        avci->draining_done = 1;
394
395
1.58M
    return ret;
396
1.58M
}
397
398
#if CONFIG_LCMS2
399
static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
400
{
401
    enum AVColorTransferCharacteristic trc = frame->color_trc;
402
    enum AVColorPrimaries prim = frame->color_primaries;
403
    const FFCodec *const codec = ffcodec(avctx->codec);
404
    AVCodecInternal *avci = avctx->internal;
405
    cmsHPROFILE profile;
406
    int ret;
407
408
    /* don't generate ICC profiles if disabled or unsupported */
409
    if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
410
        return 0;
411
    if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
412
        return 0;
413
414
    if (trc == AVCOL_TRC_UNSPECIFIED)
415
        trc = avctx->color_trc;
416
    if (prim == AVCOL_PRI_UNSPECIFIED)
417
        prim = avctx->color_primaries;
418
    if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
419
        return 0; /* can't generate ICC profile with missing csp tags */
420
421
    if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
422
        return 0; /* don't overwrite existing ICC profile */
423
424
    if (!avci->icc.avctx) {
425
        ret = ff_icc_context_init(&avci->icc, avctx);
426
        if (ret < 0)
427
            return ret;
428
    }
429
430
    ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
431
    if (ret < 0)
432
        return ret;
433
434
    ret = ff_icc_profile_attach(&avci->icc, profile, frame);
435
    cmsCloseProfile(profile);
436
    return ret;
437
}
438
#else /* !CONFIG_LCMS2 */
439
static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
440
750k
{
441
750k
    return 0;
442
750k
}
443
#endif
444
445
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
446
750k
{
447
750k
    AVCodecInternal *avci = avctx->internal;
448
750k
    EncodeContext     *ec = encode_ctx(avci);
449
750k
    AVFrame *dst = avci->buffer_frame;
450
750k
    int ret;
451
452
750k
    if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
453
        /* extract audio service type metadata */
454
0
        AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
455
0
        if (sd && sd->size >= sizeof(enum AVAudioServiceType))
456
0
            avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
457
458
        /* check for valid frame size */
459
0
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
460
            /* if we already got an undersized frame, that must have been the last */
461
0
            if (ec->last_audio_frame) {
462
0
                av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
463
0
                return AVERROR(EINVAL);
464
0
            }
465
0
            if (src->nb_samples > avctx->frame_size) {
466
0
                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
467
0
                return AVERROR(EINVAL);
468
0
            }
469
0
            if (src->nb_samples < avctx->frame_size) {
470
0
                ec->last_audio_frame = 1;
471
0
                if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
472
0
                    int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
473
0
                    int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
474
475
0
                    if (out_samples != src->nb_samples) {
476
0
                        ret = pad_last_frame(avctx, dst, src, out_samples);
477
0
                        if (ret < 0)
478
0
                            return ret;
479
0
                        goto finish;
480
0
                    }
481
0
                }
482
0
            }
483
0
        }
484
0
    }
485
486
750k
    ret = av_frame_ref(dst, src);
487
750k
    if (ret < 0)
488
0
        return ret;
489
490
750k
finish:
491
492
750k
    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
493
750k
        ret = encode_generate_icc_profile(avctx, dst);
494
750k
        if (ret < 0)
495
0
            return ret;
496
750k
    }
497
498
    // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
499
    // since otherwise we cannot be sure that whatever value it has is in the
500
    // right timebase, so we would produce an incorrect value, which is worse
501
    // than none at all
502
750k
    if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
503
750k
        dst->duration = 0;
504
505
750k
    return 0;
506
750k
}
507
508
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
509
813k
{
510
813k
    AVCodecInternal *avci = avctx->internal;
511
813k
    int ret;
512
513
813k
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
514
0
        return AVERROR(EINVAL);
515
516
813k
    if (avci->draining)
517
0
        return AVERROR_EOF;
518
519
813k
    if (avci->buffer_frame->buf[0])
520
0
        return AVERROR(EAGAIN);
521
522
813k
    if (!frame) {
523
62.9k
        avci->draining = 1;
524
750k
    } else {
525
750k
        ret = encode_send_frame_internal(avctx, frame);
526
750k
        if (ret < 0)
527
0
            return ret;
528
750k
    }
529
530
813k
    if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
531
813k
        ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
532
813k
        if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
533
1.69k
            return ret;
534
813k
    }
535
536
811k
    avctx->frame_num++;
537
538
811k
    return 0;
539
813k
}
540
541
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
542
1.55M
{
543
1.55M
    AVCodecInternal *avci = avctx->internal;
544
1.55M
    int ret;
545
546
1.55M
    av_packet_unref(avpkt);
547
548
1.55M
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
549
0
        return AVERROR(EINVAL);
550
551
1.55M
    if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
552
722k
        av_packet_move_ref(avpkt, avci->buffer_pkt);
553
831k
    } else {
554
831k
        ret = encode_receive_packet_internal(avctx, avpkt);
555
831k
        if (ret < 0)
556
811k
            return ret;
557
831k
    }
558
559
742k
    return 0;
560
1.55M
}
561
562
static int encode_preinit_video(AVCodecContext *avctx)
563
66.7k
{
564
66.7k
    const AVCodec *c = avctx->codec;
565
66.7k
    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
566
66.7k
    int i;
567
568
66.7k
    if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
569
265
        av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
570
265
               avctx->pix_fmt);
571
265
        return AVERROR(EINVAL);
572
265
    }
573
574
66.4k
    if (c->pix_fmts) {
575
184k
        for (i = 0; c->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
576
184k
            if (avctx->pix_fmt == c->pix_fmts[i])
577
61.9k
                break;
578
61.9k
        if (c->pix_fmts[i] == AV_PIX_FMT_NONE) {
579
0
            av_log(avctx, AV_LOG_ERROR,
580
0
                   "Specified pixel format %s is not supported by the %s encoder.\n",
581
0
                   av_get_pix_fmt_name(avctx->pix_fmt), c->name);
582
583
0
            av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
584
0
            for (int p = 0; c->pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
585
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n",
586
0
                       av_get_pix_fmt_name(c->pix_fmts[p]));
587
0
            }
588
589
0
            return AVERROR(EINVAL);
590
0
        }
591
61.9k
        if (c->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
592
61.9k
            c->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
593
61.9k
            c->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
594
61.9k
            c->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
595
61.9k
            c->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
596
2.08k
            avctx->color_range = AVCOL_RANGE_JPEG;
597
61.9k
    }
598
599
66.4k
    if (    avctx->bits_per_raw_sample < 0
600
66.4k
        || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
601
0
        av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
602
0
            avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
603
0
        avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
604
0
    }
605
66.4k
    if (avctx->width <= 0 || avctx->height <= 0) {
606
0
        av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
607
0
        return AVERROR(EINVAL);
608
0
    }
609
610
66.4k
#if FF_API_TICKS_PER_FRAME
611
66.4k
FF_DISABLE_DEPRECATION_WARNINGS
612
66.4k
    if (avctx->ticks_per_frame && avctx->time_base.num &&
613
66.4k
        avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
614
0
        av_log(avctx, AV_LOG_ERROR,
615
0
               "ticks_per_frame %d too large for the timebase %d/%d.",
616
0
               avctx->ticks_per_frame,
617
0
               avctx->time_base.num,
618
0
               avctx->time_base.den);
619
0
        return AVERROR(EINVAL);
620
0
    }
621
66.4k
FF_ENABLE_DEPRECATION_WARNINGS
622
66.4k
#endif
623
624
66.4k
    if (avctx->hw_frames_ctx) {
625
0
        AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
626
0
        if (frames_ctx->format != avctx->pix_fmt) {
627
0
            av_log(avctx, AV_LOG_ERROR,
628
0
                   "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
629
0
            return AVERROR(EINVAL);
630
0
        }
631
0
        if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
632
0
            avctx->sw_pix_fmt != frames_ctx->sw_format) {
633
0
            av_log(avctx, AV_LOG_ERROR,
634
0
                   "Mismatching AVCodecContext.sw_pix_fmt (%s) "
635
0
                   "and AVHWFramesContext.sw_format (%s)\n",
636
0
                   av_get_pix_fmt_name(avctx->sw_pix_fmt),
637
0
                   av_get_pix_fmt_name(frames_ctx->sw_format));
638
0
            return AVERROR(EINVAL);
639
0
        }
640
0
        avctx->sw_pix_fmt = frames_ctx->sw_format;
641
0
    }
642
643
66.4k
    return 0;
644
66.4k
}
645
646
static int encode_preinit_audio(AVCodecContext *avctx)
647
0
{
648
0
    const AVCodec *c = avctx->codec;
649
0
    int i;
650
651
0
    if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
652
0
        av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
653
0
               avctx->sample_fmt);
654
0
        return AVERROR(EINVAL);
655
0
    }
656
0
    if (avctx->sample_rate <= 0) {
657
0
        av_log(avctx, AV_LOG_ERROR, "Invalid audio sample rate: %d\n",
658
0
               avctx->sample_rate);
659
0
        return AVERROR(EINVAL);
660
0
    }
661
662
0
    if (c->sample_fmts) {
663
0
        for (i = 0; c->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
664
0
            if (avctx->sample_fmt == c->sample_fmts[i])
665
0
                break;
666
0
            if (avctx->ch_layout.nb_channels == 1 &&
667
0
                av_get_planar_sample_fmt(avctx->sample_fmt) ==
668
0
                av_get_planar_sample_fmt(c->sample_fmts[i])) {
669
0
                avctx->sample_fmt = c->sample_fmts[i];
670
0
                break;
671
0
            }
672
0
        }
673
0
        if (c->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
674
0
            av_log(avctx, AV_LOG_ERROR,
675
0
                   "Specified sample format %s is not supported by the %s encoder\n",
676
0
                   av_get_sample_fmt_name(avctx->sample_fmt), c->name);
677
678
0
            av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
679
0
            for (int p = 0; c->sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
680
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n",
681
0
                       av_get_sample_fmt_name(c->sample_fmts[p]));
682
0
            }
683
684
0
            return AVERROR(EINVAL);
685
0
        }
686
0
    }
687
0
    if (c->supported_samplerates) {
688
0
        for (i = 0; c->supported_samplerates[i] != 0; i++)
689
0
            if (avctx->sample_rate == c->supported_samplerates[i])
690
0
                break;
691
0
        if (c->supported_samplerates[i] == 0) {
692
0
            av_log(avctx, AV_LOG_ERROR,
693
0
                   "Specified sample rate %d is not supported by the %s encoder\n",
694
0
                   avctx->sample_rate, c->name);
695
696
0
            av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
697
0
            for (int p = 0; c->supported_samplerates[p]; p++)
698
0
                av_log(avctx, AV_LOG_ERROR, "  %d\n", c->supported_samplerates[p]);
699
700
0
            return AVERROR(EINVAL);
701
0
        }
702
0
    }
703
0
    if (c->ch_layouts) {
704
0
        for (i = 0; c->ch_layouts[i].nb_channels; i++) {
705
0
            if (!av_channel_layout_compare(&avctx->ch_layout, &c->ch_layouts[i]))
706
0
                break;
707
0
        }
708
0
        if (!c->ch_layouts[i].nb_channels) {
709
0
            char buf[512];
710
0
            int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
711
0
            av_log(avctx, AV_LOG_ERROR,
712
0
                   "Specified channel layout '%s' is not supported by the %s encoder\n",
713
0
                   ret > 0 ? buf : "?", c->name);
714
715
0
            av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
716
0
            for (int p = 0; c->ch_layouts[p].nb_channels; p++) {
717
0
                ret = av_channel_layout_describe(&c->ch_layouts[p], buf, sizeof(buf));
718
0
                av_log(avctx, AV_LOG_ERROR, "  %s\n", ret > 0 ? buf : "?");
719
0
            }
720
0
            return AVERROR(EINVAL);
721
0
        }
722
0
    }
723
724
0
    if (!avctx->bits_per_raw_sample)
725
0
        avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
726
0
    if (!avctx->bits_per_raw_sample)
727
0
        avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
728
729
0
    return 0;
730
0
}
731
732
int ff_encode_preinit(AVCodecContext *avctx)
733
68.8k
{
734
68.8k
    AVCodecInternal *avci = avctx->internal;
735
68.8k
    EncodeContext     *ec = encode_ctx(avci);
736
68.8k
    int ret = 0;
737
738
68.8k
    if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
739
2.15k
        av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
740
2.15k
        return AVERROR(EINVAL);
741
2.15k
    }
742
743
66.7k
    if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
744
66.7k
        !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
745
0
        av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
746
0
               "encoder does not support it.\n");
747
0
        return AVERROR(EINVAL);
748
0
    }
749
750
66.7k
    switch (avctx->codec_type) {
751
66.7k
    case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
752
0
    case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
753
66.7k
    }
754
66.7k
    if (ret < 0)
755
265
        return ret;
756
757
66.4k
    if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
758
66.4k
        && avctx->bit_rate>0 && avctx->bit_rate<1000) {
759
1.76k
        av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
760
1.76k
    }
761
762
66.4k
    if (!avctx->rc_initial_buffer_occupancy)
763
66.4k
        avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
764
765
66.4k
    if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
766
24.5k
        ec->intra_only_flag = AV_PKT_FLAG_KEY;
767
768
66.4k
    if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
769
65.7k
        avci->in_frame = av_frame_alloc();
770
65.7k
        if (!avci->in_frame)
771
0
            return AVERROR(ENOMEM);
772
65.7k
    }
773
774
66.4k
    if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
775
0
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
776
0
            av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
777
0
                   "from an encoder not supporting it\n");
778
0
            return AVERROR(ENOSYS);
779
0
        }
780
781
0
        avci->recon_frame = av_frame_alloc();
782
0
        if (!avci->recon_frame)
783
0
            return AVERROR(ENOMEM);
784
0
    }
785
786
664k
    for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
787
598k
        const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
788
598k
        const enum AVFrameSideDataType  type_frame  = ff_sd_global_map[i].frame;
789
598k
        const AVFrameSideData *sd_frame;
790
598k
        AVPacketSideData      *sd_packet;
791
792
598k
        sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
793
598k
                                          avctx->nb_decoded_side_data,
794
598k
                                          type_frame);
795
598k
        if (!sd_frame ||
796
598k
            av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
797
0
                                    type_packet))
798
799
598k
            continue;
800
801
0
        sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
802
0
                                            type_packet, sd_frame->size, 0);
803
0
        if (!sd_packet)
804
0
            return AVERROR(ENOMEM);
805
806
0
        memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
807
0
    }
808
809
66.4k
    if (CONFIG_FRAME_THREAD_ENCODER) {
810
66.4k
        ret = ff_frame_thread_encoder_init(avctx);
811
66.4k
        if (ret < 0)
812
0
            return ret;
813
66.4k
    }
814
815
66.4k
    return 0;
816
66.4k
}
817
818
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
819
59.1k
{
820
59.1k
    int ret;
821
822
59.1k
    switch (avctx->codec->type) {
823
59.1k
    case AVMEDIA_TYPE_VIDEO:
824
59.1k
        frame->format = avctx->pix_fmt;
825
59.1k
        if (frame->width <= 0 || frame->height <= 0) {
826
4.69k
            frame->width  = FFMAX(avctx->width,  avctx->coded_width);
827
4.69k
            frame->height = FFMAX(avctx->height, avctx->coded_height);
828
4.69k
        }
829
830
59.1k
        break;
831
0
    case AVMEDIA_TYPE_AUDIO:
832
0
        frame->sample_rate = avctx->sample_rate;
833
0
        frame->format      = avctx->sample_fmt;
834
0
        if (!frame->ch_layout.nb_channels) {
835
0
            ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
836
0
            if (ret < 0)
837
0
                return ret;
838
0
        }
839
0
        break;
840
59.1k
    }
841
842
59.1k
    ret = avcodec_default_get_buffer2(avctx, frame, 0);
843
59.1k
    if (ret < 0) {
844
0
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
845
0
        av_frame_unref(frame);
846
0
        return ret;
847
0
    }
848
849
59.1k
    return 0;
850
59.1k
}
851
852
int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
853
0
{
854
0
    AVCodecInternal *avci = avctx->internal;
855
856
0
    if (!avci->recon_frame)
857
0
        return AVERROR(EINVAL);
858
0
    if (!avci->recon_frame->buf[0])
859
0
        return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
860
861
0
    av_frame_move_ref(frame, avci->recon_frame);
862
0
    return 0;
863
0
}
864
865
void ff_encode_flush_buffers(AVCodecContext *avctx)
866
0
{
867
0
    AVCodecInternal *avci = avctx->internal;
868
869
0
    if (avci->in_frame)
870
0
        av_frame_unref(avci->in_frame);
871
0
    if (avci->recon_frame)
872
0
        av_frame_unref(avci->recon_frame);
873
0
}
874
875
AVCodecInternal *ff_encode_internal_alloc(void)
876
73.8k
{
877
73.8k
    return av_mallocz(sizeof(EncodeContext));
878
73.8k
}
879
880
AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
881
16.7k
{
882
16.7k
    AVPacketSideData *tmp;
883
16.7k
    AVCPBProperties  *props;
884
16.7k
    size_t size;
885
16.7k
    int i;
886
887
16.7k
    for (i = 0; i < avctx->nb_coded_side_data; i++)
888
0
        if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
889
0
            return (AVCPBProperties *)avctx->coded_side_data[i].data;
890
891
16.7k
    props = av_cpb_properties_alloc(&size);
892
16.7k
    if (!props)
893
0
        return NULL;
894
895
16.7k
    tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
896
16.7k
    if (!tmp) {
897
0
        av_freep(&props);
898
0
        return NULL;
899
0
    }
900
901
16.7k
    avctx->coded_side_data = tmp;
902
16.7k
    avctx->nb_coded_side_data++;
903
904
16.7k
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
905
16.7k
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
906
16.7k
    avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
907
908
16.7k
    return props;
909
16.7k
}