GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/encode.c Lines: 183 241 75.9 %
Date: 2020-07-13 04:22:34 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
425952
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
34
{
35

425952
    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
425952
    av_assert0(!avpkt->data);
42
43

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

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

876383
        if (ret < 0 && ret != AVERROR_EOF)
137
444257
            return ret;
138
    }
139
140
438143
    if (!frame->buf[0]) {
141
6017
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
142

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

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

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

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

426012
        if (frame && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
175
393798
            if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
176
300425
                if (avpkt->pts == AV_NOPTS_VALUE)
177
289205
                    avpkt->pts = frame->pts;
178
300425
                if (!avpkt->duration)
179
299370
                    avpkt->duration = ff_samples_to_time_base(avctx,
180
299370
                                                              frame->nb_samples);
181
            }
182
        }
183
426012
        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
326524
            avpkt->flags |= AV_PKT_FLAG_KEY;
188
326524
            avpkt->dts = avpkt->pts;
189
        }
190
    }
191
192

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

432554
    if (ret < 0 || !got_packet)
197
6542
        av_packet_unref(avpkt);
198
199
432554
    if (frame) {
200
432126
        if (!ret)
201
432126
            avctx->frame_number++;
202
432126
        av_frame_unref(frame);
203
    }
204
205
432554
    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

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

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

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

208498
        if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
238
            avctx->stats_out[0] = '\0';
239
208498
        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
876044
    if (avctx->codec->receive_packet) {
244
        ret = avctx->codec->receive_packet(avctx, avpkt);
245
        if (!ret)
246
            // Encoders must always return ref-counted buffers.
247
            // Side-data only packets have no data and can be not ref-counted.
248
            av_assert0(!avpkt->data || avpkt->buf);
249
    } else
250
876044
        ret = encode_simple_receive_packet(avctx, avpkt);
251
252
876044
    if (ret == AVERROR_EOF)
253
5775
        avci->draining_done = 1;
254
255
876044
    return ret;
256
}
257
258
432126
static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
259
{
260
432126
    AVCodecInternal *avci = avctx->internal;
261
432126
    AVFrame *dst = avci->buffer_frame;
262
    int ret;
263
264
432126
    if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
265
        /* extract audio service type metadata */
266
332638
        AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
267

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

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

437901
    if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
328
437901
        ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
329

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

869688
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
344
        return AVERROR(EINVAL);
345
346

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

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

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

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

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