GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/encode.c Lines: 175 253 69.2 %
Date: 2019-11-20 04:07:19 Branches: 140 224 62.5 %

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 "frame_thread_encoder.h"
30
#include "internal.h"
31
32
403118
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
33
{
34
403118
    if (avpkt->size < 0) {
35
        av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
36
        return AVERROR(EINVAL);
37
    }
38

403118
    if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
39
        av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
40
               size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
41
        return AVERROR(EINVAL);
42
    }
43
44

403118
    if (avctx && 2*min_size < size) { // FIXME The factor needs to be finetuned
45

71172
        av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
46

71172
        if (!avpkt->data || avpkt->size < size) {
47
71172
            av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
48
71172
            avpkt->data = avctx->internal->byte_buffer;
49
71172
            avpkt->size = avctx->internal->byte_buffer_size;
50
        }
51
    }
52
53
403118
    if (avpkt->data) {
54
71172
        AVBufferRef *buf = avpkt->buf;
55
56
71172
        if (avpkt->size < size) {
57
            av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
58
            return AVERROR(EINVAL);
59
        }
60
61
71172
        av_init_packet(avpkt);
62
71172
        avpkt->buf      = buf;
63
71172
        avpkt->size     = size;
64
71172
        return 0;
65
    } else {
66
331946
        int ret = av_new_packet(avpkt, size);
67
331946
        if (ret < 0)
68
            av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
69
331946
        return ret;
70
    }
71
}
72
73
int ff_alloc_packet(AVPacket *avpkt, int size)
74
{
75
    return ff_alloc_packet2(NULL, avpkt, size, 0);
76
}
77
78
/**
79
 * Pad last frame with silence.
80
 */
81
47
static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
82
{
83
47
    AVFrame *frame = NULL;
84
    int ret;
85
86
47
    if (!(frame = av_frame_alloc()))
87
        return AVERROR(ENOMEM);
88
89
47
    frame->format         = src->format;
90
47
    frame->channel_layout = src->channel_layout;
91
47
    frame->channels       = src->channels;
92
47
    frame->nb_samples     = s->frame_size;
93
47
    ret = av_frame_get_buffer(frame, 32);
94
47
    if (ret < 0)
95
        goto fail;
96
97
47
    ret = av_frame_copy_props(frame, src);
98
47
    if (ret < 0)
99
        goto fail;
100
101
47
    if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
102
                               src->nb_samples, s->channels, s->sample_fmt)) < 0)
103
        goto fail;
104
47
    if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
105
47
                                      frame->nb_samples - src->nb_samples,
106
                                      s->channels, s->sample_fmt)) < 0)
107
        goto fail;
108
109
47
    *dst = frame;
110
111
47
    return 0;
112
113
fail:
114
    av_frame_free(&frame);
115
    return ret;
116
}
117
118
310307
int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
119
                                              AVPacket *avpkt,
120
                                              const AVFrame *frame,
121
                                              int *got_packet_ptr)
122
{
123
310307
    AVFrame *extended_frame = NULL;
124
310307
    AVFrame *padded_frame = NULL;
125
    int ret;
126
310307
    AVPacket user_pkt = *avpkt;
127
310307
    int needs_realloc = !user_pkt.data;
128
129
310307
    *got_packet_ptr = 0;
130
131
310307
    if (!avctx->codec->encode2) {
132
        av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
133
        return AVERROR(ENOSYS);
134
    }
135
136

310307
    if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
137
64
        av_packet_unref(avpkt);
138
64
        return 0;
139
    }
140
141
    /* ensure that extended_data is properly set */
142

310243
    if (frame && !frame->extended_data) {
143
        if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
144
            avctx->channels > AV_NUM_DATA_POINTERS) {
145
            av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
146
                                        "with more than %d channels, but extended_data is not set.\n",
147
                   AV_NUM_DATA_POINTERS);
148
            return AVERROR(EINVAL);
149
        }
150
        av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
151
152
        extended_frame = av_frame_alloc();
153
        if (!extended_frame)
154
            return AVERROR(ENOMEM);
155
156
        memcpy(extended_frame, frame, sizeof(AVFrame));
157
        extended_frame->extended_data = extended_frame->data;
158
        frame = extended_frame;
159
    }
160
161
    /* extract audio service type metadata */
162
310243
    if (frame) {
163
310174
        AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
164

310174
        if (sd && sd->size >= sizeof(enum AVAudioServiceType))
165
            avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
166
    }
167
168
    /* check for valid frame size */
169
310243
    if (frame) {
170
310174
        if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
171
11673
            if (frame->nb_samples > avctx->frame_size) {
172
                av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
173
                ret = AVERROR(EINVAL);
174
                goto end;
175
            }
176
298501
        } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
177
            /* if we already got an undersized frame, that must have been the last */
178
44480
            if (avctx->internal->last_audio_frame) {
179
                av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame (avcodec_encode_audio2)\n", avctx->frame_size);
180
                ret = AVERROR(EINVAL);
181
                goto end;
182
            }
183
184
44480
            if (frame->nb_samples < avctx->frame_size) {
185
47
                ret = pad_last_frame(avctx, &padded_frame, frame);
186
47
                if (ret < 0)
187
                    goto end;
188
189
47
                frame = padded_frame;
190
47
                avctx->internal->last_audio_frame = 1;
191
            }
192
193
44480
            if (frame->nb_samples != avctx->frame_size) {
194
                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
195
                ret = AVERROR(EINVAL);
196
                goto end;
197
            }
198
        }
199
    }
200
201
310243
    av_assert0(avctx->codec->encode2);
202
203
310243
    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
204
310243
    if (!ret) {
205
310243
        if (*got_packet_ptr) {
206
304057
            if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
207
277994
                if (avpkt->pts == AV_NOPTS_VALUE)
208
266774
                    avpkt->pts = frame->pts;
209
277994
                if (!avpkt->duration)
210
276939
                    avpkt->duration = ff_samples_to_time_base(avctx,
211
276939
                                                              frame->nb_samples);
212
            }
213
304057
            avpkt->dts = avpkt->pts;
214
        } else {
215
6186
            avpkt->size = 0;
216
        }
217
    }
218

310243
    if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
219
50284
        needs_realloc = 0;
220
50284
        if (user_pkt.data) {
221
            if (user_pkt.size >= avpkt->size) {
222
                memcpy(user_pkt.data, avpkt->data, avpkt->size);
223
            } else {
224
                av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
225
                avpkt->size = user_pkt.size;
226
                ret = -1;
227
            }
228
            avpkt->buf      = user_pkt.buf;
229
            avpkt->data     = user_pkt.data;
230
50284
        } else if (!avpkt->buf) {
231
50284
            ret = av_packet_make_refcounted(avpkt);
232
50284
            if (ret < 0)
233
                goto end;
234
        }
235
    }
236
237
310243
    if (!ret) {
238

310243
        if (needs_realloc && avpkt->data) {
239
253761
            ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
240
253761
            if (ret >= 0)
241
253761
                avpkt->data = avpkt->buf->data;
242
        }
243
310243
        if (frame)
244
310174
            avctx->frame_number++;
245
    }
246
247

310243
    if (ret < 0 || !*got_packet_ptr) {
248
6186
        av_packet_unref(avpkt);
249
6186
        goto end;
250
    }
251
252
    /* NOTE: if we add any audio encoders which output non-keyframe packets,
253
     *       this needs to be moved to the encoders, but for now we can do it
254
     *       here to simplify things */
255
304057
    avpkt->flags |= AV_PKT_FLAG_KEY;
256
257
310243
end:
258
310243
    av_frame_free(&padded_frame);
259
310243
    av_free(extended_frame);
260
261
310243
    return ret;
262
}
263
264
104736
int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
265
                                              AVPacket *avpkt,
266
                                              const AVFrame *frame,
267
                                              int *got_packet_ptr)
268
{
269
    int ret;
270
104736
    AVPacket user_pkt = *avpkt;
271
104736
    int needs_realloc = !user_pkt.data;
272
273
104736
    *got_packet_ptr = 0;
274
275
104736
    if (!avctx->codec->encode2) {
276
        av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
277
        return AVERROR(ENOSYS);
278
    }
279
280
104736
    if(CONFIG_FRAME_THREAD_ENCODER &&
281

104736
       avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
282
863
        return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
283
284

103873
    if ((avctx->flags&AV_CODEC_FLAG_PASS1) && avctx->stats_out)
285
        avctx->stats_out[0] = '\0';
286
287

103873
    if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
288
4436
        av_packet_unref(avpkt);
289
4436
        return 0;
290
    }
291
292
99437
    if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
293
        return AVERROR(EINVAL);
294
295

99437
    if (frame && frame->format == AV_PIX_FMT_NONE)
296
        av_log(avctx, AV_LOG_WARNING, "AVFrame.format is not set\n");
297

99437
    if (frame && (frame->width == 0 || frame->height == 0))
298
        av_log(avctx, AV_LOG_WARNING, "AVFrame.width or height is not set\n");
299
300
99437
    av_assert0(avctx->codec->encode2);
301
302
99437
    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
303
99437
    av_assert0(ret <= 0);
304
305
99437
    emms_c();
306
307

99437
    if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
308
20888
        needs_realloc = 0;
309
20888
        if (user_pkt.data) {
310
            if (user_pkt.size >= avpkt->size) {
311
                memcpy(user_pkt.data, avpkt->data, avpkt->size);
312
            } else {
313
                av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
314
                avpkt->size = user_pkt.size;
315
                ret = -1;
316
            }
317
            avpkt->buf      = user_pkt.buf;
318
            avpkt->data     = user_pkt.data;
319
20888
        } else if (!avpkt->buf) {
320
20888
            ret = av_packet_make_refcounted(avpkt);
321
20888
            if (ret < 0)
322
                return ret;
323
        }
324
    }
325
326
99437
    if (!ret) {
327
99437
        if (!*got_packet_ptr)
328
319
            avpkt->size = 0;
329
99118
        else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
330
93087
            avpkt->pts = avpkt->dts = frame->pts;
331
332

99437
        if (needs_realloc && avpkt->data) {
333
78230
            ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
334
78230
            if (ret >= 0)
335
78230
                avpkt->data = avpkt->buf->data;
336
        }
337
338
99437
        if (frame)
339
99118
            avctx->frame_number++;
340
    }
341
342

99437
    if (ret < 0 || !*got_packet_ptr)
343
319
        av_packet_unref(avpkt);
344
345
99437
    return ret;
346
}
347
348
595
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
349
                            const AVSubtitle *sub)
350
{
351
    int ret;
352
595
    if (sub->start_display_time) {
353
        av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
354
        return -1;
355
    }
356
357
595
    ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
358
595
    avctx->frame_number++;
359
595
    return ret;
360
}
361
362
411018
static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
363
{
364
    int ret;
365
411018
    *got_packet = 0;
366
367
411018
    av_packet_unref(avctx->internal->buffer_pkt);
368
411018
    avctx->internal->buffer_pkt_valid = 0;
369
370
411018
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
371
103911
        ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
372
                                    frame, got_packet);
373
307107
    } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
374
307107
        ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
375
                                    frame, got_packet);
376
    } else {
377
        ret = AVERROR(EINVAL);
378
    }
379
380

411018
    if (ret >= 0 && *got_packet) {
381
        // Encoders must always return ref-counted buffers.
382
        // Side-data only packets have no data and can be not ref-counted.
383

399915
        av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
384
399915
        avctx->internal->buffer_pkt_valid = 1;
385
399915
        ret = 0;
386
    } else {
387
11103
        av_packet_unref(avctx->internal->buffer_pkt);
388
    }
389
390
411018
    return ret;
391
}
392
393
410715
int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
394
{
395

410715
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
396
        return AVERROR(EINVAL);
397
398
410715
    if (avctx->internal->draining)
399
        return AVERROR_EOF;
400
401
410715
    if (!frame) {
402
4683
        avctx->internal->draining = 1;
403
404
4683
        if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
405
4518
            return 0;
406
    }
407
408
406197
    if (avctx->codec->send_frame)
409
        return avctx->codec->send_frame(avctx, frame);
410
411
    // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
412
    // 1. if the AVFrame is not refcounted, the copying will be much more
413
    //    expensive than copying the packet data
414
    // 2. assume few users use non-refcounted AVPackets, so usually no copy is
415
    //    needed
416
417
406197
    if (avctx->internal->buffer_pkt_valid)
418
        return AVERROR(EAGAIN);
419
420
406197
    return do_encode(avctx, frame, &(int){0});
421
}
422
423
815313
int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
424
{
425
815313
    av_packet_unref(avpkt);
426
427

815313
    if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
428
        return AVERROR(EINVAL);
429
430
815313
    if (avctx->codec->receive_packet) {
431
        int ret;
432
        if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
433
            return AVERROR_EOF;
434
        ret = avctx->codec->receive_packet(avctx, avpkt);
435
        if (!ret)
436
            // Encoders must always return ref-counted buffers.
437
            // Side-data only packets have no data and can be not ref-counted.
438
            av_assert0(!avpkt->data || avpkt->buf);
439
        return ret;
440
    }
441
442
    // Emulation via old API.
443
444
815313
    if (!avctx->internal->buffer_pkt_valid) {
445
        int got_packet;
446
        int ret;
447
415536
        if (!avctx->internal->draining)
448
415398
            return AVERROR(EAGAIN);
449
4821
        ret = do_encode(avctx, NULL, &got_packet);
450
4821
        if (ret < 0)
451
            return ret;
452

4821
        if (ret >= 0 && !got_packet)
453
4683
            return AVERROR_EOF;
454
    }
455
456
399915
    av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
457
399915
    avctx->internal->buffer_pkt_valid = 0;
458
399915
    return 0;
459
}