FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 263 376 69.9%
Branches: 213 328 64.9%

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 33855 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
36 {
37
2/4
✓ Branch 0 taken 33855 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33855 times.
33855 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 33855 times.
33855 av_assert0(!avpkt->data);
44
45 33855 av_fast_padded_malloc(&avctx->internal->byte_buffer,
46 33855 &avctx->internal->byte_buffer_size, size);
47 33855 avpkt->data = avctx->internal->byte_buffer;
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33855 times.
33855 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 33855 avpkt->size = size;
53
54 33855 return 0;
55 }
56
57 418243 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
58 {
59 int ret;
60
61
2/4
✓ Branch 0 taken 418243 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 418243 times.
418243 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
62 return AVERROR(EINVAL);
63
64
2/4
✓ Branch 0 taken 418243 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 418243 times.
418243 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 418243 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418243 times.
418243 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 418243 avpkt->data = avpkt->buf->data;
75
76 418243 return 0;
77 }
78
79 418243 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 418243 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 418243 times.
418243 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
84 return AVERROR(EINVAL);
85
86
2/4
✓ Branch 0 taken 418243 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 418243 times.
418243 av_assert0(!avpkt->data && !avpkt->buf);
87
88 418243 avpkt->size = size;
89 418243 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418243 times.
418243 if (ret < 0)
91 goto fail;
92
93
2/4
✓ Branch 0 taken 418243 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 418243 times.
418243 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 418243 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
99
100 418243 ret = 0;
101 418243 fail:
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418243 times.
418243 if (ret < 0) {
103 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
104 av_packet_unref(avpkt);
105 }
106
107 418243 return ret;
108 }
109
110 418307 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
111 {
112 418307 uint8_t *data = avpkt->data;
113 int ret;
114
115
2/2
✓ Branch 0 taken 384452 times.
✓ Branch 1 taken 33855 times.
418307 if (avpkt->buf)
116 384452 return 0;
117
118 33855 avpkt->data = NULL;
119 33855 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33855 times.
33855 if (ret < 0)
121 return ret;
122 33855 memcpy(avpkt->data, data, avpkt->size);
123
124 33855 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 806 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 806 times.
806 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 806 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
174 806 avctx->frame_number++;
175 806 return ret;
176 }
177
178 858123 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
179 {
180 858123 AVCodecInternal *avci = avctx->internal;
181
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 858123 times.
858123 if (avci->draining)
183 return AVERROR_EOF;
184
185
2/2
✓ Branch 0 taken 433687 times.
✓ Branch 1 taken 424436 times.
858123 if (!avci->buffer_frame->buf[0])
186 433687 return AVERROR(EAGAIN);
187
188 424436 av_frame_move_ref(frame, avci->buffer_frame);
189
190 424436 return 0;
191 }
192
193 424778 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
194 AVFrame *frame, int *got_packet)
195 {
196 424778 const FFCodec *const codec = ffcodec(avctx->codec);
197 int ret;
198
199 424778 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
200 424778 emms_c();
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424778 times.
424778 av_assert0(ret <= 0);
202
203
3/4
✓ Branch 0 taken 424778 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6455 times.
✓ Branch 3 taken 418323 times.
424778 if (!ret && *got_packet) {
204
2/2
✓ Branch 0 taken 418307 times.
✓ Branch 1 taken 16 times.
418323 if (avpkt->data) {
205 418307 ret = encode_make_refcounted(avctx, avpkt);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418307 times.
418307 if (ret < 0)
207 goto unref;
208 // Date returned by encoders must always be ref-counted
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 418307 times.
418307 av_assert0(avpkt->buf);
210 }
211
212 // set the timestamps for the simple no-delay case
213 // encoders with delay have to set the timestamps themselves
214
2/2
✓ Branch 0 taken 393831 times.
✓ Branch 1 taken 24492 times.
418323 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
215
2/2
✓ Branch 0 taken 378553 times.
✓ Branch 1 taken 15278 times.
393831 if (avpkt->pts == AV_NOPTS_VALUE)
216 378553 avpkt->pts = frame->pts;
217
218
2/2
✓ Branch 0 taken 297142 times.
✓ Branch 1 taken 96689 times.
393831 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
219
2/2
✓ Branch 0 taken 296322 times.
✓ Branch 1 taken 820 times.
297142 if (!avpkt->duration)
220 296322 avpkt->duration = ff_samples_to_time_base(avctx,
221 296322 frame->nb_samples);
222 }
223 }
224
225 // dts equals pts unless there is reordering
226 // there can be no reordering if there is no encoder delay
227
2/2
✓ Branch 0 taken 5600 times.
✓ Branch 1 taken 412723 times.
418323 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
228
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 4835 times.
5600 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
229 413488 avpkt->dts = avpkt->pts;
230 } else {
231 6455 unref:
232 6455 av_packet_unref(avpkt);
233 }
234 #if !FF_API_THREAD_SAFE_CALLBACKS
235 if (frame)
236 av_frame_unref(frame);
237 #endif
238
239 424778 return ret;
240 }
241
242 869145 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
243 {
244 869145 AVCodecInternal *avci = avctx->internal;
245 869145 AVFrame *frame = avci->in_frame;
246 869145 const FFCodec *const codec = ffcodec(avctx->codec);
247 int got_packet;
248 int ret;
249
250
2/2
✓ Branch 0 taken 1707 times.
✓ Branch 1 taken 867438 times.
869145 if (avci->draining_done)
251 1707 return AVERROR_EOF;
252
253
3/4
✓ Branch 0 taken 867438 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 858123 times.
✓ Branch 3 taken 9315 times.
867438 if (!frame->buf[0] && !avci->draining) {
254 858123 av_frame_unref(frame);
255 858123 ret = ff_encode_get_frame(avctx, frame);
256
3/4
✓ Branch 0 taken 433687 times.
✓ Branch 1 taken 424436 times.
✓ Branch 2 taken 433687 times.
✗ Branch 3 not taken.
858123 if (ret < 0 && ret != AVERROR_EOF)
257 433687 return ret;
258 }
259
260
2/2
✓ Branch 0 taken 9315 times.
✓ Branch 1 taken 424436 times.
433751 if (!frame->buf[0]) {
261
2/2
✓ Branch 0 taken 8973 times.
✓ Branch 1 taken 342 times.
9315 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
262
2/2
✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 4503 times.
8973 avci->frame_thread_encoder))
263 4470 return AVERROR_EOF;
264
265 // Flushing is signaled with a NULL frame
266 4845 frame = NULL;
267 }
268
269 429281 got_packet = 0;
270
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 429281 times.
429281 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
272
273
2/2
✓ Branch 0 taken 59347 times.
✓ Branch 1 taken 369934 times.
429281 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
274 /* This will unref frame. */
275 59347 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
276 else {
277 369934 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
278 #if FF_API_THREAD_SAFE_CALLBACKS
279
2/2
✓ Branch 0 taken 369592 times.
✓ Branch 1 taken 342 times.
369934 if (frame)
280 369592 av_frame_unref(frame);
281 #endif
282 }
283
284
4/4
✓ Branch 0 taken 4845 times.
✓ Branch 1 taken 424436 times.
✓ Branch 2 taken 1707 times.
✓ Branch 3 taken 3138 times.
429281 if (avci->draining && !got_packet)
285 1707 avci->draining_done = 1;
286
287 429281 return ret;
288 }
289
290 858187 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
291 {
292 int ret;
293
294
4/4
✓ Branch 0 taken 869161 times.
✓ Branch 1 taken 418307 times.
✓ Branch 2 taken 869145 times.
✓ Branch 3 taken 16 times.
1287468 while (!avpkt->data && !avpkt->side_data) {
295 869145 ret = encode_simple_internal(avctx, avpkt);
296
2/2
✓ Branch 0 taken 439864 times.
✓ Branch 1 taken 429281 times.
869145 if (ret < 0)
297 439864 return ret;
298 }
299
300 418323 return 0;
301 }
302
303 865316 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
304 {
305 865316 AVCodecInternal *avci = avctx->internal;
306 int ret;
307
308
2/2
✓ Branch 0 taken 7129 times.
✓ Branch 1 taken 858187 times.
865316 if (avci->draining_done)
309 7129 return AVERROR_EOF;
310
311
2/4
✓ Branch 0 taken 858187 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 858187 times.
858187 av_assert0(!avpkt->data && !avpkt->side_data);
312
313
2/2
✓ Branch 0 taken 214803 times.
✓ Branch 1 taken 643384 times.
858187 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
314
3/4
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 214399 times.
✓ Branch 2 taken 404 times.
✗ Branch 3 not taken.
214803 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
315 404 avctx->stats_out[0] = '\0';
316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 214803 times.
214803 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
317 return AVERROR(EINVAL);
318 }
319
320
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 858187 times.
858187 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
321 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
322 if (ret < 0)
323 av_packet_unref(avpkt);
324 else
325 // Encoders must always return ref-counted buffers.
326 // Side-data only packets have no data and can be not ref-counted.
327 av_assert0(!avpkt->data || avpkt->buf);
328 } else
329 858187 ret = encode_simple_receive_packet(avctx, avpkt);
330
2/2
✓ Branch 0 taken 418323 times.
✓ Branch 1 taken 439864 times.
858187 if (ret >= 0)
331 418323 avpkt->flags |= avci->intra_only_flag;
332
333
2/2
✓ Branch 0 taken 6177 times.
✓ Branch 1 taken 852010 times.
858187 if (ret == AVERROR_EOF)
334 6177 avci->draining_done = 1;
335
336 858187 return ret;
337 }
338
339 #if CONFIG_LCMS2
340 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
341 {
342 enum AVColorTransferCharacteristic trc = frame->color_trc;
343 enum AVColorPrimaries prim = frame->color_primaries;
344 const FFCodec *const codec = ffcodec(avctx->codec);
345 AVCodecInternal *avci = avctx->internal;
346 cmsHPROFILE profile;
347 int ret;
348
349 /* don't generate ICC profiles if disabled or unsupported */
350 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
351 return 0;
352 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
353 return 0;
354
355 if (trc == AVCOL_TRC_UNSPECIFIED)
356 trc = avctx->color_trc;
357 if (prim == AVCOL_PRI_UNSPECIFIED)
358 prim = avctx->color_primaries;
359 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
360 return 0; /* can't generate ICC profile with missing csp tags */
361
362 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
363 return 0; /* don't overwrite existing ICC profile */
364
365 if (!avci->icc.avctx) {
366 ret = ff_icc_context_init(&avci->icc, avctx);
367 if (ret < 0)
368 return ret;
369 }
370
371 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
372 if (ret < 0)
373 return ret;
374
375 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
376 cmsCloseProfile(profile);
377 return ret;
378 }
379 #else /* !CONFIG_LCMS2 */
380 103355 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
381 {
382 103355 return 0;
383 }
384 #endif
385
386 424436 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
387 {
388 424436 AVCodecInternal *avci = avctx->internal;
389 424436 AVFrame *dst = avci->buffer_frame;
390 int ret;
391
392
2/2
✓ Branch 0 taken 321081 times.
✓ Branch 1 taken 103355 times.
424436 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
393 /* extract audio service type metadata */
394 321081 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
395
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 321081 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
321081 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
396 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
397
398 /* check for valid frame size */
399
2/2
✓ Branch 0 taken 73424 times.
✓ Branch 1 taken 247657 times.
321081 if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
400 /* if we already got an undersized frame, that must have been the last */
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73424 times.
73424 if (avctx->internal->last_audio_frame) {
402 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
403 return AVERROR(EINVAL);
404 }
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73424 times.
73424 if (src->nb_samples > avctx->frame_size) {
406 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
407 return AVERROR(EINVAL);
408 }
409
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 73318 times.
73424 if (src->nb_samples < avctx->frame_size) {
410 106 avctx->internal->last_audio_frame = 1;
411
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 54 times.
106 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
412
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;
413 52 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
414
415
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (out_samples != src->nb_samples) {
416 52 ret = pad_last_frame(avctx, dst, src, out_samples);
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
418 return ret;
419 52 goto finish;
420 }
421 }
422 }
423 }
424 }
425
426 424384 ret = av_frame_ref(dst, src);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424384 times.
424384 if (ret < 0)
428 return ret;
429
430 424384 finish:
431
432 #if FF_API_PKT_DURATION
433 FF_DISABLE_DEPRECATION_WARNINGS
434
4/4
✓ Branch 0 taken 408499 times.
✓ Branch 1 taken 15937 times.
✓ Branch 2 taken 47345 times.
✓ Branch 3 taken 361154 times.
424436 if (dst->pkt_duration && dst->pkt_duration != dst->duration)
435 47345 dst->duration = dst->pkt_duration;
436 FF_ENABLE_DEPRECATION_WARNINGS
437 #endif
438
439
2/2
✓ Branch 0 taken 103355 times.
✓ Branch 1 taken 321081 times.
424436 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
440 103355 ret = encode_generate_icc_profile(avctx, dst);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103355 times.
103355 if (ret < 0)
442 return ret;
443 }
444
445 424436 return 0;
446 }
447
448 433187 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
449 {
450 433187 AVCodecInternal *avci = avctx->internal;
451 int ret;
452
453
2/4
✓ Branch 1 taken 433187 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 433187 times.
433187 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
454 return AVERROR(EINVAL);
455
456
2/2
✓ Branch 0 taken 2574 times.
✓ Branch 1 taken 430613 times.
433187 if (avci->draining)
457 2574 return AVERROR_EOF;
458
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430613 times.
430613 if (avci->buffer_frame->buf[0])
460 return AVERROR(EAGAIN);
461
462
2/2
✓ Branch 0 taken 6177 times.
✓ Branch 1 taken 424436 times.
430613 if (!frame) {
463 6177 avci->draining = 1;
464 } else {
465 424436 ret = encode_send_frame_internal(avctx, frame);
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 424436 times.
424436 if (ret < 0)
467 return ret;
468 }
469
470
2/4
✓ Branch 0 taken 430613 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 430613 times.
✗ Branch 3 not taken.
430613 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
471 430613 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
472
5/6
✓ Branch 0 taken 13806 times.
✓ Branch 1 taken 416807 times.
✓ Branch 2 taken 4555 times.
✓ Branch 3 taken 9251 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4555 times.
430613 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
473 return ret;
474 }
475
476 430613 avctx->frame_number++;
477
478 430613 return 0;
479 }
480
481 851510 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
482 {
483 851510 AVCodecInternal *avci = avctx->internal;
484 int ret;
485
486 851510 av_packet_unref(avpkt);
487
488
2/4
✓ Branch 1 taken 851510 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 851510 times.
851510 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
489 return AVERROR(EINVAL);
490
491
4/4
✓ Branch 0 taken 434719 times.
✓ Branch 1 taken 416791 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 434703 times.
851510 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
492 416807 av_packet_move_ref(avpkt, avci->buffer_pkt);
493 } else {
494 434703 ret = encode_receive_packet_internal(avctx, avpkt);
495
2/2
✓ Branch 0 taken 433187 times.
✓ Branch 1 taken 1516 times.
434703 if (ret < 0)
496 433187 return ret;
497 }
498
499 418323 return 0;
500 }
501
502 17194 static int encode_preinit_video(AVCodecContext *avctx)
503 {
504 17194 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
505 int i;
506
507
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 16444 times.
17194 if (avctx->codec->pix_fmts) {
508
1/2
✓ Branch 0 taken 2182 times.
✗ Branch 1 not taken.
2182 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
509
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 1432 times.
2182 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
510 750 break;
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750 times.
750 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
512 char buf[128];
513 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
514 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
515 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
516 return AVERROR(EINVAL);
517 }
518
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 22 times.
750 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
519
1/2
✓ Branch 0 taken 728 times.
✗ Branch 1 not taken.
728 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
520
2/2
✓ Branch 0 taken 724 times.
✓ Branch 1 taken 4 times.
728 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
521
1/2
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
724 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
522
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 715 times.
724 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
523 35 avctx->color_range = AVCOL_RANGE_JPEG;
524 }
525
526
1/2
✓ Branch 0 taken 17194 times.
✗ Branch 1 not taken.
17194 if ( avctx->bits_per_raw_sample < 0
527
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 17129 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
17194 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
528 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
529 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
530 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
531 }
532
2/4
✓ Branch 0 taken 17194 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17194 times.
17194 if (avctx->width <= 0 || avctx->height <= 0) {
533 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
534 return AVERROR(EINVAL);
535 }
536
537
2/4
✓ Branch 0 taken 17194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17194 times.
✗ Branch 3 not taken.
17194 if (avctx->ticks_per_frame && avctx->time_base.num &&
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17194 times.
17194 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
539 av_log(avctx, AV_LOG_ERROR,
540 "ticks_per_frame %d too large for the timebase %d/%d.",
541 avctx->ticks_per_frame,
542 avctx->time_base.num,
543 avctx->time_base.den);
544 return AVERROR(EINVAL);
545 }
546
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17194 times.
17194 if (avctx->hw_frames_ctx) {
548 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
549 if (frames_ctx->format != avctx->pix_fmt) {
550 av_log(avctx, AV_LOG_ERROR,
551 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
552 return AVERROR(EINVAL);
553 }
554 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
555 avctx->sw_pix_fmt != frames_ctx->sw_format) {
556 av_log(avctx, AV_LOG_ERROR,
557 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
558 "and AVHWFramesContext.sw_format (%s)\n",
559 av_get_pix_fmt_name(avctx->sw_pix_fmt),
560 av_get_pix_fmt_name(frames_ctx->sw_format));
561 return AVERROR(EINVAL);
562 }
563 avctx->sw_pix_fmt = frames_ctx->sw_format;
564 }
565
566 17194 return 0;
567 }
568
569 1195 static int encode_preinit_audio(AVCodecContext *avctx)
570 {
571 int i;
572
573
1/2
✓ Branch 0 taken 1195 times.
✗ Branch 1 not taken.
1195 if (avctx->codec->sample_fmts) {
574
1/2
✓ Branch 0 taken 1211 times.
✗ Branch 1 not taken.
1211 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
575
2/2
✓ Branch 0 taken 1195 times.
✓ Branch 1 taken 16 times.
1211 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
576 1195 break;
577
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
20 if (avctx->ch_layout.nb_channels == 1 &&
578 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
579 4 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
580 avctx->sample_fmt = avctx->codec->sample_fmts[i];
581 break;
582 }
583 }
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1195 times.
1195 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
585 char buf[128];
586 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
587 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
588 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
589 return AVERROR(EINVAL);
590 }
591 }
592
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1134 times.
1195 if (avctx->codec->supported_samplerates) {
593
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
135 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
594
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 74 times.
135 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
595 61 break;
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (avctx->codec->supported_samplerates[i] == 0) {
597 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
598 avctx->sample_rate);
599 return AVERROR(EINVAL);
600 }
601 }
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1195 times.
1195 if (avctx->sample_rate < 0) {
603 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
604 avctx->sample_rate);
605 return AVERROR(EINVAL);
606 }
607
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1117 times.
1195 if (avctx->codec->ch_layouts) {
608
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 for (i = 0; avctx->codec->ch_layouts[i].nb_channels; i++) {
609
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]))
610 78 break;
611 }
612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!avctx->codec->ch_layouts[i].nb_channels) {
613 char buf[512];
614 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
615 if (ret > 0)
616 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
617 return AVERROR(EINVAL);
618 }
619 }
620
621
2/2
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 51 times.
1195 if (!avctx->bits_per_raw_sample)
622 1144 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
623
624 1195 return 0;
625 }
626
627 18425 int ff_encode_preinit(AVCodecContext *avctx)
628 {
629 18425 AVCodecInternal *avci = avctx->internal;
630 18425 int ret = 0;
631
632
2/4
✓ Branch 0 taken 18425 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18425 times.
18425 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
633 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
634 return AVERROR(EINVAL);
635 }
636
637
3/3
✓ Branch 0 taken 17194 times.
✓ Branch 1 taken 1195 times.
✓ Branch 2 taken 36 times.
18425 switch (avctx->codec_type) {
638 17194 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
639 1195 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
640 }
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18425 times.
18425 if (ret < 0)
642 return ret;
643
644
4/4
✓ Branch 0 taken 1231 times.
✓ Branch 1 taken 17194 times.
✓ Branch 2 taken 1195 times.
✓ Branch 3 taken 36 times.
18425 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
645
3/4
✓ Branch 0 taken 18368 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18368 times.
18389 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
646 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
647 }
648
649
2/2
✓ Branch 0 taken 18423 times.
✓ Branch 1 taken 2 times.
18425 if (!avctx->rc_initial_buffer_occupancy)
650 18423 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
651
652
2/2
✓ Branch 0 taken 18087 times.
✓ Branch 1 taken 338 times.
18425 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
653 18087 avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
654
655
2/2
✓ Branch 1 taken 18389 times.
✓ Branch 2 taken 36 times.
18425 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
656 18389 avci->in_frame = av_frame_alloc();
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18389 times.
18389 if (!avci->in_frame)
658 return AVERROR(ENOMEM);
659 }
660
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18425 times.
18425 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
662 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
663 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
664 "from an encoder not supporting it\n");
665 return AVERROR(ENOSYS);
666 }
667
668 avci->recon_frame = av_frame_alloc();
669 if (!avci->recon_frame)
670 return AVERROR(ENOMEM);
671 }
672
673 if (CONFIG_FRAME_THREAD_ENCODER) {
674 18425 ret = ff_frame_thread_encoder_init(avctx);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18425 times.
18425 if (ret < 0)
676 return ret;
677 }
678
679 18425 return 0;
680 }
681
682 9539 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
683 {
684 int ret;
685
686
1/3
✓ Branch 0 taken 9539 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
9539 switch (avctx->codec->type) {
687 9539 case AVMEDIA_TYPE_VIDEO:
688 9539 frame->format = avctx->pix_fmt;
689
3/4
✓ Branch 0 taken 9523 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9523 times.
9539 if (frame->width <= 0 || frame->height <= 0) {
690 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
691 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
692 }
693
694 9539 break;
695 case AVMEDIA_TYPE_AUDIO:
696 frame->sample_rate = avctx->sample_rate;
697 frame->format = avctx->sample_fmt;
698 if (!frame->ch_layout.nb_channels) {
699 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
700 if (ret < 0)
701 return ret;
702 }
703 break;
704 }
705
706 9539 ret = avcodec_default_get_buffer2(avctx, frame, 0);
707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9539 times.
9539 if (ret < 0) {
708 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
709 av_frame_unref(frame);
710 return ret;
711 }
712
713 9539 return 0;
714 }
715
716 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
717 {
718 AVCodecInternal *avci = avctx->internal;
719
720 if (!avci->recon_frame)
721 return AVERROR(EINVAL);
722 if (!avci->recon_frame->buf[0])
723 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
724
725 av_frame_move_ref(frame, avci->recon_frame);
726 return 0;
727 }
728