FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2023-03-31 03:41:15
Exec Total Coverage
Lines: 290 399 72.7%
Functions: 21 21 100.0%
Branches: 232 348 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/channel_layout.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/samplefmt.h"
28
29 #include "avcodec.h"
30 #include "codec_internal.h"
31 #include "encode.h"
32 #include "frame_thread_encoder.h"
33 #include "internal.h"
34
35 34125 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
36 {
37
2/4
✓ Branch 0 taken 34125 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 34125 times.
34125 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
38 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
39 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
40 return AVERROR(EINVAL);
41 }
42
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34125 times.
34125 av_assert0(!avpkt->data);
44
45 34125 av_fast_padded_malloc(&avctx->internal->byte_buffer,
46 34125 &avctx->internal->byte_buffer_size, size);
47 34125 avpkt->data = avctx->internal->byte_buffer;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34125 times.
34125 if (!avpkt->data) {
49 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
50 return AVERROR(ENOMEM);
51 }
52 34125 avpkt->size = size;
53
54 34125 return 0;
55 }
56
57 419676 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
58 {
59 int ret;
60
61
2/4
✓ Branch 0 taken 419676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419676 times.
419676 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
62 return AVERROR(EINVAL);
63
64
2/4
✓ Branch 0 taken 419676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419676 times.
419676 if (avpkt->data || avpkt->buf) {
65 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
66 return AVERROR(EINVAL);
67 }
68
69 419676 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419676 times.
419676 if (ret < 0) {
71 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
72 return ret;
73 }
74 419676 avpkt->data = avpkt->buf->data;
75
76 419676 return 0;
77 }
78
79 419676 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
80 {
81 int ret;
82
83
2/4
✓ Branch 0 taken 419676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419676 times.
419676 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
84 return AVERROR(EINVAL);
85
86
2/4
✓ Branch 0 taken 419676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419676 times.
419676 av_assert0(!avpkt->data && !avpkt->buf);
87
88 419676 avpkt->size = size;
89 419676 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419676 times.
419676 if (ret < 0)
91 goto fail;
92
93
2/4
✓ Branch 0 taken 419676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 419676 times.
419676 if (!avpkt->data || !avpkt->buf) {
94 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
95 ret = AVERROR(EINVAL);
96 goto fail;
97 }
98 419676 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
99
100 419676 ret = 0;
101 419676 fail:
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419676 times.
419676 if (ret < 0) {
103 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
104 av_packet_unref(avpkt);
105 }
106
107 419676 return ret;
108 }
109
110 419740 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
111 {
112 419740 uint8_t *data = avpkt->data;
113 int ret;
114
115
2/2
✓ Branch 0 taken 385615 times.
✓ Branch 1 taken 34125 times.
419740 if (avpkt->buf)
116 385615 return 0;
117
118 34125 avpkt->data = NULL;
119 34125 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34125 times.
34125 if (ret < 0)
121 return ret;
122 34125 memcpy(avpkt->data, data, avpkt->size);
123
124 34125 return 0;
125 }
126
127 /**
128 * Pad last frame with silence.
129 */
130 52 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
131 {
132 int ret;
133
134 52 frame->format = src->format;
135 52 frame->nb_samples = out_samples;
136 52 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
138 goto fail;
139 52 ret = av_frame_get_buffer(frame, 0);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
141 goto fail;
142
143 52 ret = av_frame_copy_props(frame, src);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
145 goto fail;
146
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
148 52 src->nb_samples, s->ch_layout.nb_channels,
149 s->sample_fmt)) < 0)
150 goto fail;
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
152 52 frame->nb_samples - src->nb_samples,
153 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
154 goto fail;
155
156 52 return 0;
157
158 fail:
159 av_frame_unref(frame);
160 s->internal->last_audio_frame = 0;
161 return ret;
162 }
163
164 828 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
165 const AVSubtitle *sub)
166 {
167 int ret;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 828 times.
828 if (sub->start_display_time) {
169 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
170 return -1;
171 }
172
173 828 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
174 828 avctx->frame_num++;
175 #if FF_API_AVCTX_FRAME_NUMBER
176 FF_DISABLE_DEPRECATION_WARNINGS
177 828 avctx->frame_number = avctx->frame_num;
178 FF_ENABLE_DEPRECATION_WARNINGS
179 #endif
180 828 return ret;
181 }
182
183 861063 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
184 {
185 861063 AVCodecInternal *avci = avctx->internal;
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 861063 times.
861063 if (avci->draining)
188 return AVERROR_EOF;
189
190
2/2
✓ Branch 0 taken 435194 times.
✓ Branch 1 taken 425869 times.
861063 if (!avci->buffer_frame->buf[0])
191 435194 return AVERROR(EAGAIN);
192
193 425869 av_frame_move_ref(frame, avci->buffer_frame);
194
195 425869 return 0;
196 }
197
198 414673 int ff_encode_reordered_opaque(AVCodecContext *avctx,
199 AVPacket *pkt, const AVFrame *frame)
200 {
201 #if FF_API_REORDERED_OPAQUE
202 FF_DISABLE_DEPRECATION_WARNINGS
203 414673 avctx->reordered_opaque = frame->reordered_opaque;
204 FF_ENABLE_DEPRECATION_WARNINGS
205 #endif
206
207
2/2
✓ Branch 0 taken 411335 times.
✓ Branch 1 taken 3338 times.
414673 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
208 411335 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411335 times.
411335 if (ret < 0)
210 return ret;
211 411335 pkt->opaque = frame->opaque;
212 }
213
214 414673 return 0;
215 }
216
217 426216 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
218 AVFrame *frame, int *got_packet)
219 {
220 426216 const FFCodec *const codec = ffcodec(avctx->codec);
221 int ret;
222
223 426216 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
224 426216 emms_c();
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 426216 times.
426216 av_assert0(ret <= 0);
226
227
3/4
✓ Branch 0 taken 426216 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6459 times.
✓ Branch 3 taken 419757 times.
426216 if (!ret && *got_packet) {
228
2/2
✓ Branch 0 taken 419740 times.
✓ Branch 1 taken 17 times.
419757 if (avpkt->data) {
229 419740 ret = encode_make_refcounted(avctx, avpkt);
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419740 times.
419740 if (ret < 0)
231 goto unref;
232 // Date returned by encoders must always be ref-counted
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419740 times.
419740 av_assert0(avpkt->buf);
234 }
235
236 // set the timestamps for the simple no-delay case
237 // encoders with delay have to set the timestamps themselves
238
4/4
✓ Branch 0 taken 24663 times.
✓ Branch 1 taken 395094 times.
✓ Branch 2 taken 24500 times.
✓ Branch 3 taken 163 times.
419757 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
239
2/2
✓ Branch 0 taken 14583 times.
✓ Branch 1 taken 9917 times.
24500 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
240
2/2
✓ Branch 0 taken 394399 times.
✓ Branch 1 taken 15278 times.
409677 if (avpkt->pts == AV_NOPTS_VALUE)
241 394399 avpkt->pts = frame->pts;
242
243
2/2
✓ Branch 0 taken 408857 times.
✓ Branch 1 taken 820 times.
409677 if (!avpkt->duration) {
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408857 times.
408857 if (frame->duration)
245 avpkt->duration = frame->duration;
246
2/2
✓ Branch 0 taken 309937 times.
✓ Branch 1 taken 98920 times.
408857 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
247 309937 avpkt->duration = ff_samples_to_time_base(avctx,
248 309937 frame->nb_samples);
249 }
250 }
251
252 409677 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409677 times.
409677 if (ret < 0)
254 goto unref;
255 }
256
257 // dts equals pts unless there is reordering
258 // there can be no reordering if there is no encoder delay
259
2/2
✓ Branch 0 taken 5735 times.
✓ Branch 1 taken 414022 times.
419757 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
260
2/2
✓ Branch 0 taken 4970 times.
✓ Branch 1 taken 765 times.
5735 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4970 times.
4970 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
262 414787 avpkt->dts = avpkt->pts;
263 } else {
264 6459 unref:
265 6459 av_packet_unref(avpkt);
266 }
267
268
2/2
✓ Branch 0 taken 425869 times.
✓ Branch 1 taken 347 times.
426216 if (frame)
269 425869 av_frame_unref(frame);
270
271 426216 return ret;
272 }
273
274 872261 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
275 {
276 872261 AVCodecInternal *avci = avctx->internal;
277 872261 AVFrame *frame = avci->in_frame;
278 872261 const FFCodec *const codec = ffcodec(avctx->codec);
279 int got_packet;
280 int ret;
281
282
2/2
✓ Branch 0 taken 1720 times.
✓ Branch 1 taken 870541 times.
872261 if (avci->draining_done)
283 1720 return AVERROR_EOF;
284
285
3/4
✓ Branch 0 taken 870541 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 861063 times.
✓ Branch 3 taken 9478 times.
870541 if (!frame->buf[0] && !avci->draining) {
286 861063 av_frame_unref(frame);
287 861063 ret = ff_encode_get_frame(avctx, frame);
288
3/4
✓ Branch 0 taken 435194 times.
✓ Branch 1 taken 425869 times.
✓ Branch 2 taken 435194 times.
✗ Branch 3 not taken.
861063 if (ret < 0 && ret != AVERROR_EOF)
289 435194 return ret;
290 }
291
292
2/2
✓ Branch 0 taken 9478 times.
✓ Branch 1 taken 425869 times.
435347 if (!frame->buf[0]) {
293
2/2
✓ Branch 0 taken 9131 times.
✓ Branch 1 taken 347 times.
9478 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
294
2/2
✓ Branch 0 taken 4485 times.
✓ Branch 1 taken 4646 times.
9131 avci->frame_thread_encoder))
295 4485 return AVERROR_EOF;
296
297 // Flushing is signaled with a NULL frame
298 4993 frame = NULL;
299 }
300
301 430862 got_packet = 0;
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430862 times.
430862 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
304
305
2/2
✓ Branch 0 taken 59729 times.
✓ Branch 1 taken 371133 times.
430862 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
306 /* This will unref frame. */
307 59729 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
308 else {
309 371133 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
310 }
311
312
4/4
✓ Branch 0 taken 4993 times.
✓ Branch 1 taken 425869 times.
✓ Branch 2 taken 1720 times.
✓ Branch 3 taken 3273 times.
430862 if (avci->draining && !got_packet)
313 1720 avci->draining_done = 1;
314
315 430862 return ret;
316 }
317
318 861156 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
319 {
320 int ret;
321
322
4/4
✓ Branch 0 taken 872278 times.
✓ Branch 1 taken 419740 times.
✓ Branch 2 taken 872261 times.
✓ Branch 3 taken 17 times.
1292018 while (!avpkt->data && !avpkt->side_data) {
323 872261 ret = encode_simple_internal(avctx, avpkt);
324
2/2
✓ Branch 0 taken 441399 times.
✓ Branch 1 taken 430862 times.
872261 if (ret < 0)
325 441399 return ret;
326 }
327
328 419757 return 0;
329 }
330
331 868307 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
332 {
333 868307 AVCodecInternal *avci = avctx->internal;
334 int ret;
335
336
2/2
✓ Branch 0 taken 7151 times.
✓ Branch 1 taken 861156 times.
868307 if (avci->draining_done)
337 7151 return AVERROR_EOF;
338
339
2/4
✓ Branch 0 taken 861156 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 861156 times.
861156 av_assert0(!avpkt->data && !avpkt->side_data);
340
341
2/2
✓ Branch 0 taken 216017 times.
✓ Branch 1 taken 645139 times.
861156 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
342
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 215613 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
216017 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
343 404 avctx->stats_out[0] = '\0';
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216017 times.
216017 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
345 return AVERROR(EINVAL);
346 }
347
348
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 861156 times.
861156 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
349 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
350 if (ret < 0)
351 av_packet_unref(avpkt);
352 else
353 // Encoders must always return ref-counted buffers.
354 // Side-data only packets have no data and can be not ref-counted.
355 av_assert0(!avpkt->data || avpkt->buf);
356 } else
357 861156 ret = encode_simple_receive_packet(avctx, avpkt);
358
2/2
✓ Branch 0 taken 419757 times.
✓ Branch 1 taken 441399 times.
861156 if (ret >= 0)
359 419757 avpkt->flags |= avci->intra_only_flag;
360
361
2/2
✓ Branch 0 taken 6205 times.
✓ Branch 1 taken 854951 times.
861156 if (ret == AVERROR_EOF)
362 6205 avci->draining_done = 1;
363
364 861156 return ret;
365 }
366
367 #if CONFIG_LCMS2
368 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
369 {
370 enum AVColorTransferCharacteristic trc = frame->color_trc;
371 enum AVColorPrimaries prim = frame->color_primaries;
372 const FFCodec *const codec = ffcodec(avctx->codec);
373 AVCodecInternal *avci = avctx->internal;
374 cmsHPROFILE profile;
375 int ret;
376
377 /* don't generate ICC profiles if disabled or unsupported */
378 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
379 return 0;
380 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
381 return 0;
382
383 if (trc == AVCOL_TRC_UNSPECIFIED)
384 trc = avctx->color_trc;
385 if (prim == AVCOL_PRI_UNSPECIFIED)
386 prim = avctx->color_primaries;
387 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
388 return 0; /* can't generate ICC profile with missing csp tags */
389
390 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
391 return 0; /* don't overwrite existing ICC profile */
392
393 if (!avci->icc.avctx) {
394 ret = ff_icc_context_init(&avci->icc, avctx);
395 if (ret < 0)
396 return ret;
397 }
398
399 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
400 if (ret < 0)
401 return ret;
402
403 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
404 cmsCloseProfile(profile);
405 return ret;
406 }
407 #else /* !CONFIG_LCMS2 */
408 103916 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
409 {
410 103916 return 0;
411 }
412 #endif
413
414 425869 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
415 {
416 425869 AVCodecInternal *avci = avctx->internal;
417 425869 AVFrame *dst = avci->buffer_frame;
418 int ret;
419
420
2/2
✓ Branch 0 taken 321953 times.
✓ Branch 1 taken 103916 times.
425869 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
421 /* extract audio service type metadata */
422 321953 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
423
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 321953 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
321953 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
424 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
425
426 /* check for valid frame size */
427
2/2
✓ Branch 0 taken 73434 times.
✓ Branch 1 taken 248519 times.
321953 if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
428 /* if we already got an undersized frame, that must have been the last */
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73434 times.
73434 if (avctx->internal->last_audio_frame) {
430 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
431 return AVERROR(EINVAL);
432 }
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73434 times.
73434 if (src->nb_samples > avctx->frame_size) {
434 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
435 return AVERROR(EINVAL);
436 }
437
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 73327 times.
73434 if (src->nb_samples < avctx->frame_size) {
438 107 avctx->internal->last_audio_frame = 1;
439
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 55 times.
107 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
440
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 50 times.
52 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
441 52 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
442
443
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (out_samples != src->nb_samples) {
444 52 ret = pad_last_frame(avctx, dst, src, out_samples);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
446 return ret;
447 52 goto finish;
448 }
449 }
450 }
451 }
452 }
453
454 425817 ret = av_frame_ref(dst, src);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425817 times.
425817 if (ret < 0)
456 return ret;
457
458 425817 finish:
459
460 #if FF_API_PKT_DURATION
461 FF_DISABLE_DEPRECATION_WARNINGS
462
4/4
✓ Branch 0 taken 410040 times.
✓ Branch 1 taken 15829 times.
✓ Branch 2 taken 47589 times.
✓ Branch 3 taken 362451 times.
425869 if (dst->pkt_duration && dst->pkt_duration != dst->duration)
463 47589 dst->duration = dst->pkt_duration;
464 FF_ENABLE_DEPRECATION_WARNINGS
465 #endif
466
467
2/2
✓ Branch 0 taken 103916 times.
✓ Branch 1 taken 321953 times.
425869 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
468 103916 ret = encode_generate_icc_profile(avctx, dst);
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103916 times.
103916 if (ret < 0)
470 return ret;
471 }
472
473 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
474 // since otherwise we cannot be sure that whatever value it has is in the
475 // right timebase, so we would produce an incorrect value, which is worse
476 // than none at all
477
1/2
✓ Branch 0 taken 425869 times.
✗ Branch 1 not taken.
425869 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
478 425869 dst->duration = 0;
479
480 425869 return 0;
481 }
482
483 434655 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
484 {
485 434655 AVCodecInternal *avci = avctx->internal;
486 int ret;
487
488
2/4
✓ Branch 1 taken 434655 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 434655 times.
434655 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
489 return AVERROR(EINVAL);
490
491
2/2
✓ Branch 0 taken 2581 times.
✓ Branch 1 taken 432074 times.
434655 if (avci->draining)
492 2581 return AVERROR_EOF;
493
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432074 times.
432074 if (avci->buffer_frame->buf[0])
495 return AVERROR(EAGAIN);
496
497
2/2
✓ Branch 0 taken 6205 times.
✓ Branch 1 taken 425869 times.
432074 if (!frame) {
498 6205 avci->draining = 1;
499 } else {
500 425869 ret = encode_send_frame_internal(avctx, frame);
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425869 times.
425869 if (ret < 0)
502 return ret;
503 }
504
505
2/4
✓ Branch 0 taken 432074 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 432074 times.
✗ Branch 3 not taken.
432074 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
506 432074 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
507
5/6
✓ Branch 0 taken 13955 times.
✓ Branch 1 taken 418119 times.
✓ Branch 2 taken 4570 times.
✓ Branch 3 taken 9385 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4570 times.
432074 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
508 return ret;
509 }
510
511 432074 avctx->frame_num++;
512 #if FF_API_AVCTX_FRAME_NUMBER
513 FF_DISABLE_DEPRECATION_WARNINGS
514 432074 avctx->frame_number = avctx->frame_num;
515 FF_ENABLE_DEPRECATION_WARNINGS
516 #endif
517
518 432074 return 0;
519 }
520
521 854352 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
522 {
523 854352 AVCodecInternal *avci = avctx->internal;
524 int ret;
525
526 854352 av_packet_unref(avpkt);
527
528
2/4
✓ Branch 1 taken 854352 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 854352 times.
854352 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
529 return AVERROR(EINVAL);
530
531
4/4
✓ Branch 0 taken 436250 times.
✓ Branch 1 taken 418102 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 436233 times.
854352 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
532 418119 av_packet_move_ref(avpkt, avci->buffer_pkt);
533 } else {
534 436233 ret = encode_receive_packet_internal(avctx, avpkt);
535
2/2
✓ Branch 0 taken 434595 times.
✓ Branch 1 taken 1638 times.
436233 if (ret < 0)
536 434595 return ret;
537 }
538
539 419757 return 0;
540 }
541
542 17294 static int encode_preinit_video(AVCodecContext *avctx)
543 {
544 17294 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
545 int i;
546
547
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 16536 times.
17294 if (avctx->codec->pix_fmts) {
548
1/2
✓ Branch 0 taken 2196 times.
✗ Branch 1 not taken.
2196 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
549
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 1438 times.
2196 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
550 758 break;
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
552 char buf[128];
553 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
554 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
555 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
556 return AVERROR(EINVAL);
557 }
558
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 22 times.
758 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
559
1/2
✓ Branch 0 taken 736 times.
✗ Branch 1 not taken.
736 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
560
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 4 times.
736 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
561
1/2
✓ Branch 0 taken 732 times.
✗ Branch 1 not taken.
732 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
562
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 723 times.
732 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
563 35 avctx->color_range = AVCOL_RANGE_JPEG;
564 }
565
566
1/2
✓ Branch 0 taken 17294 times.
✗ Branch 1 not taken.
17294 if ( avctx->bits_per_raw_sample < 0
567
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 17229 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
17294 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
568 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
569 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
570 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
571 }
572
2/4
✓ Branch 0 taken 17294 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17294 times.
17294 if (avctx->width <= 0 || avctx->height <= 0) {
573 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
574 return AVERROR(EINVAL);
575 }
576
577
2/4
✓ Branch 0 taken 17294 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17294 times.
✗ Branch 3 not taken.
17294 if (avctx->ticks_per_frame && avctx->time_base.num &&
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17294 times.
17294 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
579 av_log(avctx, AV_LOG_ERROR,
580 "ticks_per_frame %d too large for the timebase %d/%d.",
581 avctx->ticks_per_frame,
582 avctx->time_base.num,
583 avctx->time_base.den);
584 return AVERROR(EINVAL);
585 }
586
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17294 times.
17294 if (avctx->hw_frames_ctx) {
588 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
589 if (frames_ctx->format != avctx->pix_fmt) {
590 av_log(avctx, AV_LOG_ERROR,
591 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
592 return AVERROR(EINVAL);
593 }
594 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
595 avctx->sw_pix_fmt != frames_ctx->sw_format) {
596 av_log(avctx, AV_LOG_ERROR,
597 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
598 "and AVHWFramesContext.sw_format (%s)\n",
599 av_get_pix_fmt_name(avctx->sw_pix_fmt),
600 av_get_pix_fmt_name(frames_ctx->sw_format));
601 return AVERROR(EINVAL);
602 }
603 avctx->sw_pix_fmt = frames_ctx->sw_format;
604 }
605
606 17294 return 0;
607 }
608
609 1205 static int encode_preinit_audio(AVCodecContext *avctx)
610 {
611 int i;
612
613
1/2
✓ Branch 0 taken 1205 times.
✗ Branch 1 not taken.
1205 if (avctx->codec->sample_fmts) {
614
1/2
✓ Branch 0 taken 1222 times.
✗ Branch 1 not taken.
1222 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
615
2/2
✓ Branch 0 taken 1205 times.
✓ Branch 1 taken 17 times.
1222 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
616 1205 break;
617
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
21 if (avctx->ch_layout.nb_channels == 1 &&
618 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
619 4 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
620 avctx->sample_fmt = avctx->codec->sample_fmts[i];
621 break;
622 }
623 }
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1205 times.
1205 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
625 char buf[128];
626 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
627 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
628 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
629 return AVERROR(EINVAL);
630 }
631 }
632
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1144 times.
1205 if (avctx->codec->supported_samplerates) {
633
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
135 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
634
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 74 times.
135 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
635 61 break;
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (avctx->codec->supported_samplerates[i] == 0) {
637 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
638 avctx->sample_rate);
639 return AVERROR(EINVAL);
640 }
641 }
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1205 times.
1205 if (avctx->sample_rate < 0) {
643 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
644 avctx->sample_rate);
645 return AVERROR(EINVAL);
646 }
647
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1127 times.
1205 if (avctx->codec->ch_layouts) {
648
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) {
649
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 47 times.
125 if (!av_channel_layout_compare(&avctx->ch_layout, &avctx->codec->ch_layouts[i]))
650 78 break;
651 }
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!avctx->codec->ch_layouts[i].nb_channels) {
653 char buf[512];
654 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
655 if (ret > 0)
656 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
657 return AVERROR(EINVAL);
658 }
659 }
660
661
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 53 times.
1205 if (!avctx->bits_per_raw_sample)
662 1152 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
663
664 1205 return 0;
665 }
666
667 18537 int ff_encode_preinit(AVCodecContext *avctx)
668 {
669 18537 AVCodecInternal *avci = avctx->internal;
670 18537 int ret = 0;
671
672
2/4
✓ Branch 0 taken 18537 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18537 times.
18537 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
673 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
674 return AVERROR(EINVAL);
675 }
676
677
2/2
✓ Branch 0 taken 18460 times.
✓ Branch 1 taken 77 times.
18537 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18460 times.
18460 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
679 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
680 "encoder does not support it.\n");
681 return AVERROR(EINVAL);
682 }
683
684
3/3
✓ Branch 0 taken 17294 times.
✓ Branch 1 taken 1205 times.
✓ Branch 2 taken 38 times.
18537 switch (avctx->codec_type) {
685 17294 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
686 1205 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
687 }
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18537 times.
18537 if (ret < 0)
689 return ret;
690
691
4/4
✓ Branch 0 taken 1243 times.
✓ Branch 1 taken 17294 times.
✓ Branch 2 taken 1205 times.
✓ Branch 3 taken 38 times.
18537 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
692
3/4
✓ Branch 0 taken 18478 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18478 times.
18499 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
693 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
694 }
695
696
2/2
✓ Branch 0 taken 18535 times.
✓ Branch 1 taken 2 times.
18537 if (!avctx->rc_initial_buffer_occupancy)
697 18535 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
698
699
2/2
✓ Branch 0 taken 18193 times.
✓ Branch 1 taken 344 times.
18537 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
700 18193 avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
701
702
2/2
✓ Branch 1 taken 18499 times.
✓ Branch 2 taken 38 times.
18537 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
703 18499 avci->in_frame = av_frame_alloc();
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18499 times.
18499 if (!avci->in_frame)
705 return AVERROR(ENOMEM);
706 }
707
708
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18535 times.
18537 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
710 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
711 "from an encoder not supporting it\n");
712 return AVERROR(ENOSYS);
713 }
714
715 2 avci->recon_frame = av_frame_alloc();
716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avci->recon_frame)
717 return AVERROR(ENOMEM);
718 }
719
720 if (CONFIG_FRAME_THREAD_ENCODER) {
721 18537 ret = ff_frame_thread_encoder_init(avctx);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18537 times.
18537 if (ret < 0)
723 return ret;
724 }
725
726 18537 return 0;
727 }
728
729 9736 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
730 {
731 int ret;
732
733
1/3
✓ Branch 0 taken 9736 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
9736 switch (avctx->codec->type) {
734 9736 case AVMEDIA_TYPE_VIDEO:
735 9736 frame->format = avctx->pix_fmt;
736
3/4
✓ Branch 0 taken 9720 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9720 times.
9736 if (frame->width <= 0 || frame->height <= 0) {
737 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
738 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
739 }
740
741 9736 break;
742 case AVMEDIA_TYPE_AUDIO:
743 frame->sample_rate = avctx->sample_rate;
744 frame->format = avctx->sample_fmt;
745 if (!frame->ch_layout.nb_channels) {
746 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
747 if (ret < 0)
748 return ret;
749 }
750 break;
751 }
752
753 9736 ret = avcodec_default_get_buffer2(avctx, frame, 0);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9736 times.
9736 if (ret < 0) {
755 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
756 av_frame_unref(frame);
757 return ret;
758 }
759
760 9736 return 0;
761 }
762
763 60 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
764 {
765 60 AVCodecInternal *avci = avctx->internal;
766
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame)
768 return AVERROR(EINVAL);
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame->buf[0])
770 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
771
772 60 av_frame_move_ref(frame, avci->recon_frame);
773 60 return 0;
774 }
775