FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 335 484 69.2%
Functions: 25 26 96.2%
Branches: 249 396 62.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/emms.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/samplefmt.h"
31
32 #include "avcodec.h"
33 #include "avcodec_internal.h"
34 #include "codec_desc.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37 #include "frame_thread_encoder.h"
38 #include "internal.h"
39
40 typedef struct EncodeContext {
41 AVCodecInternal avci;
42
43 /**
44 * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
45 * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
46 * This is used to set said flag generically for said encoders.
47 */
48 int intra_only_flag;
49
50 /**
51 * An audio frame with less than required samples has been submitted (and
52 * potentially padded with silence). Reject all subsequent frames.
53 */
54 int last_audio_frame;
55 } EncodeContext;
56
57 905488 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 905488 return (EncodeContext*)avci;
60 }
61
62 28147 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 28147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28147 times.
28147 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
65 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
66 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
67 return AVERROR(EINVAL);
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28147 times.
28147 av_assert0(!avpkt->data);
71
72 28147 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 28147 &avctx->internal->byte_buffer_size, size);
74 28147 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28147 times.
28147 if (!avpkt->data) {
76 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
77 return AVERROR(ENOMEM);
78 }
79 28147 avpkt->size = size;
80
81 28147 return 0;
82 }
83
84 438985 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 438985 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438985 times.
438985 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 438985 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438985 times.
438985 if (avpkt->data || avpkt->buf) {
92 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
93 return AVERROR(EINVAL);
94 }
95
96 438985 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 438985 times.
438985 if (ret < 0) {
98 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
99 return ret;
100 }
101 438985 avpkt->data = avpkt->buf->data;
102
103 438985 return 0;
104 }
105
106 438985 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
107 {
108 int ret;
109
110
2/4
✓ Branch 0 taken 438985 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438985 times.
438985 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 438985 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438985 times.
438985 av_assert0(!avpkt->data && !avpkt->buf);
114
115 438985 avpkt->size = size;
116 438985 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 438985 times.
438985 if (ret < 0)
118 goto fail;
119
120
2/4
✓ Branch 0 taken 438985 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 438985 times.
438985 if (!avpkt->data || !avpkt->buf) {
121 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
122 ret = AVERROR(EINVAL);
123 goto fail;
124 }
125 438985 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 438985 ret = 0;
128 438985 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 438985 times.
438985 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 438985 return ret;
135 }
136
137 439059 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 439059 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 410912 times.
✓ Branch 1 taken 28147 times.
439059 if (avpkt->buf)
143 410912 return 0;
144
145 28147 avpkt->data = NULL;
146 28147 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28147 times.
28147 if (ret < 0)
148 return ret;
149 28147 memcpy(avpkt->data, data, avpkt->size);
150
151 28147 return 0;
152 }
153
154 /**
155 * Pad last frame with silence.
156 */
157 53 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158 {
159 int ret;
160
161 53 frame->format = src->format;
162 53 frame->nb_samples = out_samples;
163 53 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ret < 0)
165 goto fail;
166 53 ret = av_frame_get_buffer(frame, 0);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ret < 0)
168 goto fail;
169
170 53 ret = av_frame_copy_props(frame, src);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ret < 0)
172 goto fail;
173
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
175 53 src->nb_samples, s->ch_layout.nb_channels,
176 s->sample_fmt)) < 0)
177 goto fail;
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
179 53 frame->nb_samples - src->nb_samples,
180 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
181 goto fail;
182
183 53 return 0;
184
185 fail:
186 av_frame_unref(frame);
187 encode_ctx(s->internal)->last_audio_frame = 0;
188 return ret;
189 }
190
191 822 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
192 const AVSubtitle *sub)
193 {
194 int ret;
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (sub->start_display_time) {
196 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
197 return -1;
198 }
199
200 822 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
201 822 avctx->frame_num++;
202 822 return ret;
203 }
204
205 900747 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
206 {
207 900747 AVCodecInternal *avci = avctx->internal;
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 900747 times.
900747 if (avci->draining)
210 return AVERROR_EOF;
211
212
2/2
✓ Branch 0 taken 455558 times.
✓ Branch 1 taken 445189 times.
900747 if (!avci->buffer_frame->buf[0])
213 455558 return AVERROR(EAGAIN);
214
215 445189 av_frame_move_ref(frame, avci->buffer_frame);
216
217 445189 return 0;
218 }
219
220 434403 int ff_encode_reordered_opaque(AVCodecContext *avctx,
221 AVPacket *pkt, const AVFrame *frame)
222 {
223
2/2
✓ Branch 0 taken 430864 times.
✓ Branch 1 taken 3539 times.
434403 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
224 430864 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 430864 times.
430864 if (ret < 0)
226 return ret;
227 430864 pkt->opaque = frame->opaque;
228 }
229
230 434403 return 0;
231 }
232
233 445663 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
234 AVFrame *frame, int *got_packet)
235 {
236 445663 const FFCodec *const codec = ffcodec(avctx->codec);
237 int ret;
238
239 445663 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
240 445663 emms_c();
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445663 times.
445663 av_assert0(ret <= 0);
242
243
3/4
✓ Branch 0 taken 445663 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6534 times.
✓ Branch 3 taken 439129 times.
445663 if (!ret && *got_packet) {
244
2/2
✓ Branch 0 taken 439059 times.
✓ Branch 1 taken 70 times.
439129 if (avpkt->data) {
245 439059 ret = encode_make_refcounted(avctx, avpkt);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439059 times.
439059 if (ret < 0)
247 goto unref;
248 // Date returned by encoders must always be ref-counted
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439059 times.
439059 av_assert0(avpkt->buf);
250 }
251
252 // set the timestamps for the simple no-delay case
253 // encoders with delay have to set the timestamps themselves
254
4/4
✓ Branch 0 taken 26631 times.
✓ Branch 1 taken 412498 times.
✓ Branch 2 taken 26413 times.
✓ Branch 3 taken 218 times.
439129 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
255
2/2
✓ Branch 0 taken 15763 times.
✓ Branch 1 taken 10650 times.
26413 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
256
2/2
✓ Branch 0 taken 412954 times.
✓ Branch 1 taken 15307 times.
428261 if (avpkt->pts == AV_NOPTS_VALUE)
257 412954 avpkt->pts = frame->pts;
258
259
2/2
✓ Branch 0 taken 423262 times.
✓ Branch 1 taken 4999 times.
428261 if (!avpkt->duration) {
260
2/2
✓ Branch 0 taken 402624 times.
✓ Branch 1 taken 20638 times.
423262 if (frame->duration)
261 402624 avpkt->duration = frame->duration;
262
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17438 times.
20638 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
263 3200 avpkt->duration = ff_samples_to_time_base(avctx,
264 3200 frame->nb_samples);
265 }
266 }
267
268 428261 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428261 times.
428261 if (ret < 0)
270 goto unref;
271 }
272
273 // dts equals pts unless there is reordering
274 // there can be no reordering if there is no encoder delay
275
2/2
✓ Branch 0 taken 6881 times.
✓ Branch 1 taken 432248 times.
439129 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
276
2/2
✓ Branch 0 taken 6116 times.
✓ Branch 1 taken 765 times.
6881 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6116 times.
6116 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
278 433013 avpkt->dts = avpkt->pts;
279 } else {
280 6534 unref:
281 6534 av_packet_unref(avpkt);
282 }
283
284
2/2
✓ Branch 0 taken 445189 times.
✓ Branch 1 taken 474 times.
445663 if (frame)
285 445189 av_frame_unref(frame);
286
287 445663 return ret;
288 }
289
290 914997 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
291 {
292 914997 AVCodecInternal *avci = avctx->internal;
293 914997 AVFrame *frame = avci->in_frame;
294 914997 const FFCodec *const codec = ffcodec(avctx->codec);
295 int got_packet;
296 int ret;
297
298
2/2
✓ Branch 0 taken 1899 times.
✓ Branch 1 taken 913098 times.
914997 if (avci->draining_done)
299 1899 return AVERROR_EOF;
300
301
3/4
✓ Branch 0 taken 913098 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 900747 times.
✓ Branch 3 taken 12351 times.
913098 if (!frame->buf[0] && !avci->draining) {
302 900747 av_frame_unref(frame);
303 900747 ret = ff_encode_get_frame(avctx, frame);
304
3/4
✓ Branch 0 taken 455558 times.
✓ Branch 1 taken 445189 times.
✓ Branch 2 taken 455558 times.
✗ Branch 3 not taken.
900747 if (ret < 0 && ret != AVERROR_EOF)
305 455558 return ret;
306 }
307
308
2/2
✓ Branch 0 taken 12351 times.
✓ Branch 1 taken 445189 times.
457540 if (!frame->buf[0]) {
309
2/2
✓ Branch 0 taken 11877 times.
✓ Branch 1 taken 474 times.
12351 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
310
2/2
✓ Branch 0 taken 6083 times.
✓ Branch 1 taken 5794 times.
11877 avci->frame_thread_encoder))
311 6083 return AVERROR_EOF;
312
313 // Flushing is signaled with a NULL frame
314 6268 frame = NULL;
315 }
316
317 451457 got_packet = 0;
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 451457 times.
451457 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
320
321
2/2
✓ Branch 0 taken 63441 times.
✓ Branch 1 taken 388016 times.
451457 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
322 /* This will unref frame. */
323 63441 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
324 else {
325 388016 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
326 }
327
328
4/4
✓ Branch 0 taken 6268 times.
✓ Branch 1 taken 445189 times.
✓ Branch 2 taken 1899 times.
✓ Branch 3 taken 4369 times.
451457 if (avci->draining && !got_packet)
329 1899 avci->draining_done = 1;
330
331 451457 return ret;
332 }
333
334 902669 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
335 {
336 int ret;
337
338
4/4
✓ Branch 0 taken 915067 times.
✓ Branch 1 taken 439059 times.
✓ Branch 2 taken 914997 times.
✓ Branch 3 taken 70 times.
1354126 while (!avpkt->data && !avpkt->side_data) {
339 914997 ret = encode_simple_internal(avctx, avpkt);
340
2/2
✓ Branch 0 taken 463540 times.
✓ Branch 1 taken 451457 times.
914997 if (ret < 0)
341 463540 return ret;
342 }
343
344 439129 return 0;
345 }
346
347 908861 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
348 {
349 908861 AVCodecInternal *avci = avctx->internal;
350 int ret;
351
352
2/2
✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 902669 times.
908861 if (avci->draining_done)
353 6192 return AVERROR_EOF;
354
355
2/4
✓ Branch 0 taken 902669 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 902669 times.
902669 av_assert0(!avpkt->data && !avpkt->side_data);
356
357
2/2
✓ Branch 0 taken 288520 times.
✓ Branch 1 taken 614149 times.
902669 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
358
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 287712 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
288520 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
359 808 avctx->stats_out[0] = '\0';
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 288520 times.
288520 if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
361 return AVERROR(EINVAL);
362 }
363
364
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 902669 times.
902669 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
365 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
366 if (ret < 0)
367 av_packet_unref(avpkt);
368 else
369 // Encoders must always return ref-counted buffers.
370 // Side-data only packets have no data and can be not ref-counted.
371 av_assert0(!avpkt->data || avpkt->buf);
372 } else
373 902669 ret = encode_simple_receive_packet(avctx, avpkt);
374
2/2
✓ Branch 0 taken 439129 times.
✓ Branch 1 taken 463540 times.
902669 if (ret >= 0)
375 439129 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
376
377
2/2
✓ Branch 0 taken 7982 times.
✓ Branch 1 taken 894687 times.
902669 if (ret == AVERROR_EOF)
378 7982 avci->draining_done = 1;
379
380 902669 return ret;
381 }
382
383 #if CONFIG_LCMS2
384 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
385 {
386 enum AVColorTransferCharacteristic trc = frame->color_trc;
387 enum AVColorPrimaries prim = frame->color_primaries;
388 const FFCodec *const codec = ffcodec(avctx->codec);
389 AVCodecInternal *avci = avctx->internal;
390 cmsHPROFILE profile;
391 int ret;
392
393 /* don't generate ICC profiles if disabled or unsupported */
394 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
395 return 0;
396 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
397 return 0;
398
399 if (trc == AVCOL_TRC_UNSPECIFIED)
400 trc = avctx->color_trc;
401 if (prim == AVCOL_PRI_UNSPECIFIED)
402 prim = avctx->color_primaries;
403 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
404 return 0; /* can't generate ICC profile with missing csp tags */
405
406 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
407 return 0; /* don't overwrite existing ICC profile */
408
409 if (!avci->icc.avctx) {
410 ret = ff_icc_context_init(&avci->icc, avctx);
411 if (ret < 0)
412 return ret;
413 }
414
415 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
416 if (ret < 0)
417 return ret;
418
419 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
420 cmsCloseProfile(profile);
421 return ret;
422 }
423 #else /* !CONFIG_LCMS2 */
424 138827 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
425 {
426 138827 return 0;
427 }
428 #endif
429
430 445189 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
431 {
432 445189 AVCodecInternal *avci = avctx->internal;
433 445189 EncodeContext *ec = encode_ctx(avci);
434 445189 AVFrame *dst = avci->buffer_frame;
435 int ret;
436
437
2/2
✓ Branch 0 taken 306362 times.
✓ Branch 1 taken 138827 times.
445189 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
438 /* extract audio service type metadata */
439 306362 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
440
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 306362 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
306362 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
441 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
442
443 /* check for valid frame size */
444
2/2
✓ Branch 0 taken 73583 times.
✓ Branch 1 taken 232779 times.
306362 if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
445 /* if we already got an undersized frame, that must have been the last */
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73583 times.
73583 if (ec->last_audio_frame) {
447 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
448 return AVERROR(EINVAL);
449 }
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73583 times.
73583 if (src->nb_samples > avctx->frame_size) {
451 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
452 return AVERROR(EINVAL);
453 }
454
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 73423 times.
73583 if (src->nb_samples < avctx->frame_size) {
455 160 ec->last_audio_frame = 1;
456
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 107 times.
160 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
457
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 51 times.
53 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
458 53 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
459
460
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 if (out_samples != src->nb_samples) {
461 53 ret = pad_last_frame(avctx, dst, src, out_samples);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (ret < 0)
463 return ret;
464 53 goto finish;
465 }
466 }
467 }
468 }
469 }
470
471 445136 ret = av_frame_ref(dst, src);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445136 times.
445136 if (ret < 0)
473 return ret;
474
475 445136 finish:
476
477
2/2
✓ Branch 0 taken 138827 times.
✓ Branch 1 taken 306362 times.
445189 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
478 138827 ret = encode_generate_icc_profile(avctx, dst);
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138827 times.
138827 if (ret < 0)
480 return ret;
481 }
482
483 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
484 // since otherwise we cannot be sure that whatever value it has is in the
485 // right timebase, so we would produce an incorrect value, which is worse
486 // than none at all
487
2/2
✓ Branch 0 taken 3260 times.
✓ Branch 1 taken 441929 times.
445189 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
488 3260 dst->duration = 0;
489
490 445189 return 0;
491 }
492
493 453171 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
494 {
495 453171 AVCodecInternal *avci = avctx->internal;
496 int ret;
497
498
2/4
✓ Branch 1 taken 453171 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 453171 times.
453171 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
499 return AVERROR(EINVAL);
500
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 453171 times.
453171 if (avci->draining)
502 return AVERROR_EOF;
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 453171 times.
453171 if (avci->buffer_frame->buf[0])
505 return AVERROR(EAGAIN);
506
507
2/2
✓ Branch 0 taken 7982 times.
✓ Branch 1 taken 445189 times.
453171 if (!frame) {
508 7982 avci->draining = 1;
509 } else {
510 445189 ret = encode_send_frame_internal(avctx, frame);
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445189 times.
445189 if (ret < 0)
512 return ret;
513 }
514
515
2/4
✓ Branch 0 taken 453171 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 453171 times.
✗ Branch 3 not taken.
453171 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
516 453171 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
517
5/6
✓ Branch 0 taken 16621 times.
✓ Branch 1 taken 436550 times.
✓ Branch 2 taken 6192 times.
✓ Branch 3 taken 10429 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6192 times.
453171 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
518 return ret;
519 }
520
521 453171 avctx->frame_num++;
522
523 453171 return 0;
524 }
525
526 892240 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
527 {
528 892240 AVCodecInternal *avci = avctx->internal;
529 int ret;
530
531 892240 av_packet_unref(avpkt);
532
533
2/4
✓ Branch 1 taken 892240 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 892240 times.
892240 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
534 return AVERROR(EINVAL);
535
536
4/4
✓ Branch 0 taken 455760 times.
✓ Branch 1 taken 436480 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 455690 times.
892240 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
537 436550 av_packet_move_ref(avpkt, avci->buffer_pkt);
538 } else {
539 455690 ret = encode_receive_packet_internal(avctx, avpkt);
540
2/2
✓ Branch 0 taken 453111 times.
✓ Branch 1 taken 2579 times.
455690 if (ret < 0)
541 453111 return ret;
542 }
543
544 439129 return 0;
545 }
546
547 19786 static int encode_preinit_video(AVCodecContext *avctx)
548 {
549 19786 const AVCodec *c = avctx->codec;
550 19786 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
551 const enum AVPixelFormat *pix_fmts;
552 int ret, i, num_pix_fmts;
553
554
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19786 times.
19786 if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
555 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
556 avctx->pix_fmt);
557 return AVERROR(EINVAL);
558 }
559
560 19786 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
561 0, (const void **) &pix_fmts, &num_pix_fmts);
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19786 times.
19786 if (ret < 0)
563 return ret;
564
565
2/2
✓ Branch 0 taken 804 times.
✓ Branch 1 taken 18982 times.
19786 if (pix_fmts) {
566
1/2
✓ Branch 0 taken 2665 times.
✗ Branch 1 not taken.
2665 for (i = 0; i < num_pix_fmts; i++)
567
2/2
✓ Branch 0 taken 804 times.
✓ Branch 1 taken 1861 times.
2665 if (avctx->pix_fmt == pix_fmts[i])
568 804 break;
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 804 times.
804 if (i == num_pix_fmts) {
570 av_log(avctx, AV_LOG_ERROR,
571 "Specified pixel format %s is not supported by the %s encoder.\n",
572 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
573
574 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
575 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
576 av_log(avctx, AV_LOG_ERROR, " %s\n",
577 av_get_pix_fmt_name(pix_fmts[p]));
578 }
579
580 return AVERROR(EINVAL);
581 }
582
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 21 times.
804 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
583
1/2
✓ Branch 0 taken 783 times.
✗ Branch 1 not taken.
783 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
584
2/2
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 4 times.
783 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
585
1/2
✓ Branch 0 taken 779 times.
✗ Branch 1 not taken.
779 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
586
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 771 times.
779 pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
587 33 avctx->color_range = AVCOL_RANGE_JPEG;
588 }
589
590
1/2
✓ Branch 0 taken 19786 times.
✗ Branch 1 not taken.
19786 if ( avctx->bits_per_raw_sample < 0
591
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 19703 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
19786 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
592 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
593 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
594 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
595 }
596
2/4
✓ Branch 0 taken 19786 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19786 times.
19786 if (avctx->width <= 0 || avctx->height <= 0) {
597 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
598 return AVERROR(EINVAL);
599 }
600
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19786 times.
19786 if (avctx->hw_frames_ctx) {
602 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
603 if (frames_ctx->format != avctx->pix_fmt) {
604 av_log(avctx, AV_LOG_ERROR,
605 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
606 return AVERROR(EINVAL);
607 }
608 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
609 avctx->sw_pix_fmt != frames_ctx->sw_format) {
610 av_log(avctx, AV_LOG_ERROR,
611 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
612 "and AVHWFramesContext.sw_format (%s)\n",
613 av_get_pix_fmt_name(avctx->sw_pix_fmt),
614 av_get_pix_fmt_name(frames_ctx->sw_format));
615 return AVERROR(EINVAL);
616 }
617 avctx->sw_pix_fmt = frames_ctx->sw_format;
618 }
619
620 19786 return 0;
621 }
622
623 1346 static int encode_preinit_audio(AVCodecContext *avctx)
624 {
625 1346 const AVCodec *c = avctx->codec;
626 const enum AVSampleFormat *sample_fmts;
627 const int *supported_samplerates;
628 const AVChannelLayout *ch_layouts;
629 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
630
631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1346 times.
1346 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
632 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
633 avctx->sample_fmt);
634 return AVERROR(EINVAL);
635 }
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (avctx->sample_rate <= 0) {
637 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample rate: %d\n",
638 avctx->sample_rate);
639 return AVERROR(EINVAL);
640 }
641
642 1346 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
643 0, (const void **) &sample_fmts,
644 &num_sample_fmts);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (ret < 0)
646 return ret;
647
1/2
✓ Branch 0 taken 1346 times.
✗ Branch 1 not taken.
1346 if (sample_fmts) {
648
1/2
✓ Branch 0 taken 1363 times.
✗ Branch 1 not taken.
1363 for (i = 0; i < num_sample_fmts; i++) {
649
2/2
✓ Branch 0 taken 1346 times.
✓ Branch 1 taken 17 times.
1363 if (avctx->sample_fmt == sample_fmts[i])
650 1346 break;
651
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 &&
652 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
653 4 av_get_planar_sample_fmt(sample_fmts[i])) {
654 avctx->sample_fmt = sample_fmts[i];
655 break;
656 }
657 }
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (i == num_sample_fmts) {
659 av_log(avctx, AV_LOG_ERROR,
660 "Specified sample format %s is not supported by the %s encoder\n",
661 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
662
663 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
664 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
665 av_log(avctx, AV_LOG_ERROR, " %s\n",
666 av_get_sample_fmt_name(sample_fmts[p]));
667 }
668
669 return AVERROR(EINVAL);
670 }
671 }
672
673 1346 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
674 0, (const void **) &supported_samplerates,
675 &num_samplerates);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (ret < 0)
677 return ret;
678
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1286 times.
1346 if (supported_samplerates) {
679
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 for (i = 0; i < num_samplerates; i++)
680
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 70 times.
130 if (avctx->sample_rate == supported_samplerates[i])
681 60 break;
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (i == num_samplerates) {
683 av_log(avctx, AV_LOG_ERROR,
684 "Specified sample rate %d is not supported by the %s encoder\n",
685 avctx->sample_rate, c->name);
686
687 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
688 for (int p = 0; supported_samplerates[p]; p++)
689 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
690
691 return AVERROR(EINVAL);
692 }
693 }
694 1346 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
695 0, (const void **) &ch_layouts, &num_ch_layouts);
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1346 times.
1346 if (ret < 0)
697 return ret;
698
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1268 times.
1346 if (ch_layouts) {
699
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 for (i = 0; i < num_ch_layouts; i++) {
700
2/2
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 47 times.
125 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
701 78 break;
702 }
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (i == num_ch_layouts) {
704 char buf[512];
705 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
706 av_log(avctx, AV_LOG_ERROR,
707 "Specified channel layout '%s' is not supported by the %s encoder\n",
708 ret > 0 ? buf : "?", c->name);
709
710 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
711 for (int p = 0; ch_layouts[p].nb_channels; p++) {
712 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
713 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
714 }
715 return AVERROR(EINVAL);
716 }
717 }
718
719
2/2
✓ Branch 0 taken 1293 times.
✓ Branch 1 taken 53 times.
1346 if (!avctx->bits_per_raw_sample)
720 1293 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
721
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 1176 times.
1346 if (!avctx->bits_per_raw_sample)
722 170 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
723
724 1346 return 0;
725 }
726
727 21170 int ff_encode_preinit(AVCodecContext *avctx)
728 {
729 21170 AVCodecInternal *avci = avctx->internal;
730 21170 EncodeContext *ec = encode_ctx(avci);
731 21170 int ret = 0;
732
733
2/4
✓ Branch 0 taken 21170 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21170 times.
21170 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
734 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
735 return AVERROR(EINVAL);
736 }
737
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21170 times.
21170 if (avctx->bit_rate < 0) {
739 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
740 return AVERROR(EINVAL);
741 }
742
743
2/2
✓ Branch 0 taken 21081 times.
✓ Branch 1 taken 89 times.
21170 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21081 times.
21081 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
745 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
746 "encoder does not support it.\n");
747 return AVERROR(EINVAL);
748 }
749
750
3/3
✓ Branch 0 taken 19786 times.
✓ Branch 1 taken 1346 times.
✓ Branch 2 taken 38 times.
21170 switch (avctx->codec_type) {
751 19786 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
752 1346 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
753 }
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21170 times.
21170 if (ret < 0)
755 return ret;
756
757
4/4
✓ Branch 0 taken 1384 times.
✓ Branch 1 taken 19786 times.
✓ Branch 2 taken 1346 times.
✓ Branch 3 taken 38 times.
21170 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
758
3/4
✓ Branch 0 taken 21111 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21111 times.
21132 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
759 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
760 }
761
762
2/2
✓ Branch 0 taken 21168 times.
✓ Branch 1 taken 2 times.
21170 if (!avctx->rc_initial_buffer_occupancy)
763 21168 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
764
765
2/2
✓ Branch 0 taken 20785 times.
✓ Branch 1 taken 385 times.
21170 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
766 20785 ec->intra_only_flag = AV_PKT_FLAG_KEY;
767
768
2/2
✓ Branch 1 taken 21132 times.
✓ Branch 2 taken 38 times.
21170 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
769 21132 avci->in_frame = av_frame_alloc();
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21132 times.
21132 if (!avci->in_frame)
771 return AVERROR(ENOMEM);
772 }
773
774
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21168 times.
21170 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
776 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
777 "from an encoder not supporting it\n");
778 return AVERROR(ENOSYS);
779 }
780
781 2 avci->recon_frame = av_frame_alloc();
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avci->recon_frame)
783 return AVERROR(ENOMEM);
784 }
785
786
2/2
✓ Branch 0 taken 190530 times.
✓ Branch 1 taken 21170 times.
211700 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
787 190530 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
788 190530 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
789 const AVFrameSideData *sd_frame;
790 AVPacketSideData *sd_packet;
791
792 190530 sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
793 avctx->nb_decoded_side_data,
794 type_frame);
795
3/4
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 190298 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 232 times.
190762 if (!sd_frame ||
796 232 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
797 type_packet))
798
799 190298 continue;
800
801 232 sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
802 232 type_packet, sd_frame->size, 0);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 if (!sd_packet)
804 return AVERROR(ENOMEM);
805
806 232 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
807 }
808
809 if (CONFIG_FRAME_THREAD_ENCODER) {
810 21170 ret = ff_frame_thread_encoder_init(avctx);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21170 times.
21170 if (ret < 0)
812 return ret;
813 }
814
815 21170 return 0;
816 }
817
818 10882 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
819 {
820 int ret;
821
822
1/3
✓ Branch 0 taken 10882 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
10882 switch (avctx->codec->type) {
823 10882 case AVMEDIA_TYPE_VIDEO:
824 10882 frame->format = avctx->pix_fmt;
825
3/4
✓ Branch 0 taken 10866 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10866 times.
10882 if (frame->width <= 0 || frame->height <= 0) {
826 16 frame->width = FFMAX(avctx->width, avctx->coded_width);
827 16 frame->height = FFMAX(avctx->height, avctx->coded_height);
828 }
829
830 10882 break;
831 case AVMEDIA_TYPE_AUDIO:
832 frame->sample_rate = avctx->sample_rate;
833 frame->format = avctx->sample_fmt;
834 if (!frame->ch_layout.nb_channels) {
835 ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
836 if (ret < 0)
837 return ret;
838 }
839 break;
840 }
841
842 10882 ret = avcodec_default_get_buffer2(avctx, frame, 0);
843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10882 times.
10882 if (ret < 0) {
844 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
845 av_frame_unref(frame);
846 return ret;
847 }
848
849 10882 return 0;
850 }
851
852 60 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
853 {
854 60 AVCodecInternal *avci = avctx->internal;
855
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame)
857 return AVERROR(EINVAL);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!avci->recon_frame->buf[0])
859 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
860
861 60 av_frame_move_ref(frame, avci->recon_frame);
862 60 return 0;
863 }
864
865 void ff_encode_flush_buffers(AVCodecContext *avctx)
866 {
867 AVCodecInternal *avci = avctx->internal;
868
869 if (avci->in_frame)
870 av_frame_unref(avci->in_frame);
871 if (avci->recon_frame)
872 av_frame_unref(avci->recon_frame);
873 }
874
875 21170 AVCodecInternal *ff_encode_internal_alloc(void)
876 {
877 21170 return av_mallocz(sizeof(EncodeContext));
878 }
879
880 204 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
881 {
882 AVPacketSideData *tmp;
883 AVCPBProperties *props;
884 size_t size;
885 int i;
886
887
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 204 times.
205 for (i = 0; i < avctx->nb_coded_side_data; i++)
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
889 return (AVCPBProperties *)avctx->coded_side_data[i].data;
890
891 204 props = av_cpb_properties_alloc(&size);
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (!props)
893 return NULL;
894
895 204 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (!tmp) {
897 av_freep(&props);
898 return NULL;
899 }
900
901 204 avctx->coded_side_data = tmp;
902 204 avctx->nb_coded_side_data++;
903
904 204 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
905 204 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
906 204 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
907
908 204 return props;
909 }
910
911 1612 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
912 {
913 1612 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
914 1612 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
915 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
916
2/2
✓ Branch 0 taken 4836 times.
✓ Branch 1 taken 1612 times.
6448 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
917 4836 uint16_t *matrix = matrices[m];
918
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4836 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4836 if (matrix && (types & (1U << m))) {
919 for (int i = 0; i < 64; i++) {
920 if (matrix[i] < min || matrix[i] > max) {
921 av_log(avctx, AV_LOG_ERROR, "%s matrix[%d] is %d which is out of the allowed range [%"PRIu16"-%"PRIu16"].\n", names[m], i, matrix[i], min, max);
922 return AVERROR(EINVAL);
923 }
924 }
925 }
926 }
927 1612 return 0;
928 }
929