GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/encode.c Lines: 183 242 75.6 %
Date: 2020-09-28 00:47:38 Branches: 152 228 66.7 %

Line Branch Exec 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/attributes.h"
22
#include "libavutil/avassert.h"
23
#include "libavutil/frame.h"
24
#include "libavutil/imgutils.h"
25
#include "libavutil/internal.h"
26
#include "libavutil/samplefmt.h"
27
28
#include "avcodec.h"
29
#include "encode.h"
30
#include "frame_thread_encoder.h"
31
#include "internal.h"
32
33
413534
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
34
{
35

413534
    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
36
        av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
37
               size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
38
        return AVERROR(EINVAL);
39
    }
40
41
413534
    av_assert0(!avpkt->data);
42
43

413534
    if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
44
79992
        av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
45
79992
        avpkt->data = avctx->internal->byte_buffer;
46
79992
        avpkt->size = size;
47
    }
48
49
413534
    if (!avpkt->data) {
50
333542
        int ret = av_new_packet(avpkt, size);
51
333542
        if (ret < 0)
52
            av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
53
333542
        return ret;
54
    }
55
56
79992
    return 0;
57
}
58
59
/**
60
 * Pad last frame with silence.
61
 */
62
48
static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src)
63
{
64
    int ret;
65
66
48
    frame->format         = src->format;
67
48
    frame->channel_layout = src->channel_layout;
68
48
    frame->channels       = src->channels;
69
48
    frame->nb_samples     = s->frame_size;
70
48
    ret = av_frame_get_buffer(frame, 0);
71
48
    if (ret < 0)
72
        goto fail;
73
74
48
    ret = av_frame_copy_props(frame, src);
75
48
    if (ret < 0)
76
        goto fail;
77
78
48
    if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
79
                               src->nb_samples, s->channels, s->sample_fmt)) < 0)
80
        goto fail;
81
48
    if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
82
48
                                      frame->nb_samples - src->nb_samples,
83
                                      s->channels, s->sample_fmt)) < 0)
84
        goto fail;
85
86
48
    return 0;
87
88
fail:
89
    av_frame_unref(frame);
90
    return ret;
91
}
92
93
667
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
94
                            const AVSubtitle *sub)
95
{
96
    int ret;
97
667
    if (sub->start_display_time) {
98
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
99
        return -1;
100
    }
101
102
667
    ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
103
667
    avctx->frame_number++;
104
667
    return ret;
105
}
106
107
851558
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
108
{
109
851558
    AVCodecInternal *avci = avctx->internal;
110
111
851558
    if (avci->draining)
112
        return AVERROR_EOF;
113
114
851558
    if (!avci->buffer_frame->buf[0])
115
431860
        return AVERROR(EAGAIN);
116
117
419698
    av_frame_move_ref(frame, avci->buffer_frame);
118
119
419698
    return 0;
120
}
121
122
857792
static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
123
{
124
857792
    AVCodecInternal   *avci = avctx->internal;
125
857792
    EncodeSimpleContext *es = &avci->es;
126
857792
    AVFrame          *frame = es->in_frame;
127
    int got_packet;
128
    int ret;
129
130
857792
    if (avci->draining_done)
131
186
        return AVERROR_EOF;
132
133

857606
    if (!frame->buf[0] && !avci->draining) {
134
851558
        av_frame_unref(frame);
135
851558
        ret = ff_encode_get_frame(avctx, frame);
136

851558
        if (ret < 0 && ret != AVERROR_EOF)
137
431860
            return ret;
138
    }
139
140
425746
    if (!frame->buf[0]) {
141
6048
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
142

5722
              (avci->frame_thread_encoder && avctx->active_thread_type & FF_THREAD_FRAME)))
143
5601
            return AVERROR_EOF;
144
145
        // Flushing is signaled with a NULL frame
146
447
        frame = NULL;
147
    }
148
149
420145
    got_packet = 0;
150
151
420145
    av_assert0(avctx->codec->encode2);
152
153
420145
    if (CONFIG_FRAME_THREAD_ENCODER &&
154

420145
        avci->frame_thread_encoder && (avctx->active_thread_type & FF_THREAD_FRAME))
155
886
        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
156
    else {
157
419259
        ret = avctx->codec->encode2(avctx, avpkt, frame, &got_packet);
158

419259
        if (avctx->codec->type == AVMEDIA_TYPE_VIDEO && !ret && got_packet &&
159
98732
            !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
160
92701
            avpkt->pts = avpkt->dts = frame->pts;
161
    }
162
163
420145
    av_assert0(ret <= 0);
164
165
420145
    emms_c();
166
167

420145
    if (!ret && got_packet) {
168
413584
        if (avpkt->data) {
169
413569
            ret = av_packet_make_refcounted(avpkt);
170
413569
            if (ret < 0)
171
                goto end;
172
        }
173
174

413584
        if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
175
381351
            if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
176
287988
                if (avpkt->pts == AV_NOPTS_VALUE)
177
276768
                    avpkt->pts = frame->pts;
178
287988
                if (!avpkt->duration)
179
286933
                    avpkt->duration = ff_samples_to_time_base(avctx,
180
286933
                                                              frame->nb_samples);
181
            }
182
        }
183
413584
        if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
184
            /* NOTE: if we add any audio encoders which output non-keyframe packets,
185
             *       this needs to be moved to the encoders, but for now we can do it
186
             *       here to simplify things */
187
314087
            avpkt->flags |= AV_PKT_FLAG_KEY;
188
314087
            avpkt->dts = avpkt->pts;
189
        }
190
    }
191
192

420145
    if (avci->draining && !got_packet)
193
186
        avci->draining_done = 1;
194
195
419959
end:
196

420145
    if (ret < 0 || !got_packet)
197
6561
        av_packet_unref(avpkt);
198
199
420145
    if (frame) {
200
419698
        if (!ret)
201
419698
            avctx->frame_number++;
202
419698
        av_frame_unref(frame);
203
    }
204
205
420145
    if (got_packet)
206
        // Encoders must always return ref-counted buffers.
207
        // Side-data only packets have no data and can be not ref-counted.
208

413584
        av_assert0(!avpkt->data || avpkt->buf);
209
210
420145
    return ret;
211
}
212
213
851231
static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
214
{
215
    int ret;
216
217

1271376
    while (!avpkt->data && !avpkt->side_data) {
218
857792
        ret = encode_simple_internal(avctx, avpkt);
219
857792
        if (ret < 0)
220
437647
            return ret;
221
    }
222
223
413584
    return 0;
224
}
225
226
856900
static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
227
{
228
856900
    AVCodecInternal *avci = avctx->internal;
229
    int ret;
230
231
856900
    if (avci->draining_done)
232
5669
        return AVERROR_EOF;
233
234

851231
    av_assert0(!avpkt->data && !avpkt->side_data);
235
236
851231
    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
237

208551
        if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
238
            avctx->stats_out[0] = '\0';
239
208551
        if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
240
            return AVERROR(EINVAL);
241
    }
242
243
851231
    if (avctx->codec->receive_packet) {
244
        ret = avctx->codec->receive_packet(avctx, avpkt);
245
        if (ret < 0)
246
            av_packet_unref(avpkt);
247
        else
248
            // Encoders must always return ref-counted buffers.
249
            // Side-data only packets have no data and can be not ref-counted.
250
            av_assert0(!avpkt->data || avpkt->buf);
251
    } else
252
851231
        ret = encode_simple_receive_packet(avctx, avpkt);
253
254
851231
    if (ret == AVERROR_EOF)
255
5787
        avci->draining_done = 1;
256
257
851231
    return ret;
258
}
259
260
419698
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
261
{
262
419698
    AVCodecInternal *avci = avctx->internal;
263
419698
    AVFrame *dst = avci->buffer_frame;
264
    int ret;
265
266
419698
    if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
267
        /* extract audio service type metadata */
268
320201
        AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
269

320201
        if (sd && sd->size >= sizeof(enum AVAudioServiceType))
270
            avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
271
272
        /* check for valid frame size */
273
320201
        if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
274
12224
            if (src->nb_samples > avctx->frame_size) {
275
                av_log(avctx, AV_LOG_ERROR, "more samples than frame size\n");
276
                return AVERROR(EINVAL);
277
            }
278
307977
        } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
279
            /* if we already got an undersized frame, that must have been the last */
280
52749
            if (avctx->internal->last_audio_frame) {
281
                av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
282
                return AVERROR(EINVAL);
283
            }
284
285
52749
            if (src->nb_samples < avctx->frame_size) {
286
48
                ret = pad_last_frame(avctx, dst, src);
287
48
                if (ret < 0)
288
                    return ret;
289
290
48
                avctx->internal->last_audio_frame = 1;
291
52701
            } else if (src->nb_samples > avctx->frame_size) {
292
                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d)\n", src->nb_samples, avctx->frame_size);
293
                return AVERROR(EINVAL);
294
            }
295
        }
296
    }
297
298
419698
    if (!dst->data[0]) {
299
419650
        ret = av_frame_ref(dst, src);
300
419650
        if (ret < 0)
301
             return ret;
302
    }
303
304
419698
    return 0;
305
}
306
307
425485
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
308
{
309
425485
    AVCodecInternal *avci = avctx->internal;
310
    int ret;
311
312

425485
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
313
        return AVERROR(EINVAL);
314
315
425485
    if (avci->draining)
316
        return AVERROR_EOF;
317
318
425485
    if (avci->buffer_frame->data[0])
319
        return AVERROR(EAGAIN);
320
321
425485
    if (!frame) {
322
5787
        avci->draining = 1;
323
    } else {
324
419698
        ret = encode_send_frame_internal(avctx, frame);
325
419698
        if (ret < 0)
326
            return ret;
327
    }
328
329

425485
    if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
330
425485
        ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
331

425485
        if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
332
            return ret;
333
    }
334
335
425485
    return 0;
336
}
337
338
844856
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
339
{
340
844856
    AVCodecInternal *avci = avctx->internal;
341
    int ret;
342
343
844856
    av_packet_unref(avpkt);
344
345

844856
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
346
        return AVERROR(EINVAL);
347
348

844856
    if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
349
413441
        av_packet_move_ref(avpkt, avci->buffer_pkt);
350
    } else {
351
431415
        ret = encode_receive_packet_internal(avctx, avpkt);
352
431415
        if (ret < 0)
353
431272
            return ret;
354
    }
355
356
413584
    return 0;
357
}
358
359
3260
static int compat_encode(AVCodecContext *avctx, AVPacket *avpkt,
360
                         int *got_packet, const AVFrame *frame)
361
{
362
3260
    AVCodecInternal *avci = avctx->internal;
363
    AVPacket user_pkt;
364
    int ret;
365
366
3260
    *got_packet = 0;
367
368

3260
    if (frame && avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
369
60
        if (frame->format == AV_PIX_FMT_NONE)
370
            av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
371

60
        if (frame->width == 0 || frame->height == 0)
372
            av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
373
    }
374
375
3260
    ret = avcodec_send_frame(avctx, frame);
376
3260
    if (ret == AVERROR_EOF)
377
        ret = 0;
378
3260
    else if (ret == AVERROR(EAGAIN)) {
379
        /* we fully drain all the output in each encode call, so this should not
380
         * ever happen */
381
        return AVERROR_BUG;
382
3260
    } else if (ret < 0)
383
        return ret;
384
385
3260
    av_packet_move_ref(&user_pkt, avpkt);
386
6520
    while (ret >= 0) {
387
6520
        ret = avcodec_receive_packet(avctx, avpkt);
388
6520
        if (ret < 0) {
389

3260
            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
390
3260
                ret = 0;
391
3260
            goto finish;
392
        }
393
394
3260
        if (avpkt != avci->compat_encode_packet) {
395

3260
            if (avpkt->data && user_pkt.data) {
396
                if (user_pkt.size >= avpkt->size) {
397
                    memcpy(user_pkt.data, avpkt->data, avpkt->size);
398
                    av_buffer_unref(&avpkt->buf);
399
                    avpkt->buf  = user_pkt.buf;
400
                    avpkt->data = user_pkt.data;
401
                    av_init_packet(&user_pkt);
402
                } else {
403
                    av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
404
                    av_packet_unref(avpkt);
405
                    ret = AVERROR(EINVAL);
406
                    goto finish;
407
                }
408
            }
409
410
3260
            *got_packet = 1;
411
3260
            avpkt = avci->compat_encode_packet;
412
        } else {
413
            if (!avci->compat_decode_warned) {
414
                av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_encode_* "
415
                       "API cannot return all the packets for this encoder. "
416
                       "Some packets will be dropped. Update your code to the "
417
                       "new encoding API to fix this.\n");
418
                avci->compat_decode_warned = 1;
419
                av_packet_unref(avpkt);
420
            }
421
        }
422
423
3260
        if (avci->draining)
424
            break;
425
    }
426
427
finish:
428
3260
    if (ret < 0)
429
        av_packet_unref(&user_pkt);
430
431
3260
    return ret;
432
}
433
434
3200
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
435
                                              AVPacket *avpkt,
436
                                              const AVFrame *frame,
437
                                              int *got_packet_ptr)
438
{
439
3200
    int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
440
441
3200
    if (ret < 0)
442
        av_packet_unref(avpkt);
443
444
3200
    return ret;
445
}
446
447
60
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
448
                                              AVPacket *avpkt,
449
                                              const AVFrame *frame,
450
                                              int *got_packet_ptr)
451
{
452
60
    int ret = compat_encode(avctx, avpkt, got_packet_ptr, frame);
453
454
60
    if (ret < 0)
455
        av_packet_unref(avpkt);
456
457
60
    return ret;
458
}