FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2025-06-01 09:29:47
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 906694 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 906694 return (EncodeContext*)avci;
60 }
61
62 28149 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 28149 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28149 times.
28149 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 28149 times.
28149 av_assert0(!avpkt->data);
71
72 28149 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 28149 &avctx->internal->byte_buffer_size, size);
74 28149 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28149 times.
28149 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 28149 avpkt->size = size;
80
81 28149 return 0;
82 }
83
84 439565 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 439565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439565 times.
439565 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 439565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439565 times.
439565 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 439565 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439565 times.
439565 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 439565 avpkt->data = avpkt->buf->data;
102
103 439565 return 0;
104 }
105
106 439565 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 439565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439565 times.
439565 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 439565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439565 times.
439565 av_assert0(!avpkt->data && !avpkt->buf);
114
115 439565 avpkt->size = size;
116 439565 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439565 times.
439565 if (ret < 0)
118 goto fail;
119
120
2/4
✓ Branch 0 taken 439565 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 439565 times.
439565 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 439565 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 439565 ret = 0;
128 439565 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439565 times.
439565 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 439565 return ret;
135 }
136
137 439639 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 439639 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 411490 times.
✓ Branch 1 taken 28149 times.
439639 if (avpkt->buf)
143 411490 return 0;
144
145 28149 avpkt->data = NULL;
146 28149 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28149 times.
28149 if (ret < 0)
148 return ret;
149 28149 memcpy(avpkt->data, data, avpkt->size);
150
151 28149 return 0;
152 }
153
154 /**
155 * Pad last frame with silence.
156 */
157 54 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158 {
159 int ret;
160
161 54 frame->format = src->format;
162 54 frame->nb_samples = out_samples;
163 54 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
165 goto fail;
166 54 ret = av_frame_get_buffer(frame, 0);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
168 goto fail;
169
170 54 ret = av_frame_copy_props(frame, src);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
172 goto fail;
173
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
175 54 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 54 times.
54 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
179 54 frame->nb_samples - src->nb_samples,
180 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
181 goto fail;
182
183 54 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 901932 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
206 {
207 901932 AVCodecInternal *avci = avctx->internal;
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 901932 times.
901932 if (avci->draining)
210 return AVERROR_EOF;
211
212
2/2
✓ Branch 0 taken 456163 times.
✓ Branch 1 taken 445769 times.
901932 if (!avci->buffer_frame->buf[0])
213 456163 return AVERROR(EAGAIN);
214
215 445769 av_frame_move_ref(frame, avci->buffer_frame);
216
217 445769 return 0;
218 }
219
220 434983 int ff_encode_reordered_opaque(AVCodecContext *avctx,
221 AVPacket *pkt, const AVFrame *frame)
222 {
223
2/2
✓ Branch 0 taken 431444 times.
✓ Branch 1 taken 3539 times.
434983 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
224 431444 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 431444 times.
431444 if (ret < 0)
226 return ret;
227 431444 pkt->opaque = frame->opaque;
228 }
229
230 434983 return 0;
231 }
232
233 446243 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
234 AVFrame *frame, int *got_packet)
235 {
236 446243 const FFCodec *const codec = ffcodec(avctx->codec);
237 int ret;
238
239 446243 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
240 446243 emms_c();
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446243 times.
446243 av_assert0(ret <= 0);
242
243
3/4
✓ Branch 0 taken 446243 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6534 times.
✓ Branch 3 taken 439709 times.
446243 if (!ret && *got_packet) {
244
2/2
✓ Branch 0 taken 439639 times.
✓ Branch 1 taken 70 times.
439709 if (avpkt->data) {
245 439639 ret = encode_make_refcounted(avctx, avpkt);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439639 times.
439639 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 439639 times.
439639 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 413078 times.
✓ Branch 2 taken 26413 times.
✓ Branch 3 taken 218 times.
439709 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 413514 times.
✓ Branch 1 taken 15327 times.
428841 if (avpkt->pts == AV_NOPTS_VALUE)
257 413514 avpkt->pts = frame->pts;
258
259
2/2
✓ Branch 0 taken 423842 times.
✓ Branch 1 taken 4999 times.
428841 if (!avpkt->duration) {
260
2/2
✓ Branch 0 taken 403204 times.
✓ Branch 1 taken 20638 times.
423842 if (frame->duration)
261 403204 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 428841 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428841 times.
428841 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 432828 times.
439709 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 433593 avpkt->dts = avpkt->pts;
279 } else {
280 6534 unref:
281 6534 av_packet_unref(avpkt);
282 }
283
284
2/2
✓ Branch 0 taken 445769 times.
✓ Branch 1 taken 474 times.
446243 if (frame)
285 445769 av_frame_unref(frame);
286
287 446243 return ret;
288 }
289
290 916232 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
291 {
292 916232 AVCodecInternal *avci = avctx->internal;
293 916232 AVFrame *frame = avci->in_frame;
294 916232 const FFCodec *const codec = ffcodec(avctx->codec);
295 int got_packet;
296 int ret;
297
298
2/2
✓ Branch 0 taken 1902 times.
✓ Branch 1 taken 914330 times.
916232 if (avci->draining_done)
299 1902 return AVERROR_EOF;
300
301
3/4
✓ Branch 0 taken 914330 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 901932 times.
✓ Branch 3 taken 12398 times.
914330 if (!frame->buf[0] && !avci->draining) {
302 901932 av_frame_unref(frame);
303 901932 ret = ff_encode_get_frame(avctx, frame);
304
3/4
✓ Branch 0 taken 456163 times.
✓ Branch 1 taken 445769 times.
✓ Branch 2 taken 456163 times.
✗ Branch 3 not taken.
901932 if (ret < 0 && ret != AVERROR_EOF)
305 456163 return ret;
306 }
307
308
2/2
✓ Branch 0 taken 12398 times.
✓ Branch 1 taken 445769 times.
458167 if (!frame->buf[0]) {
309
2/2
✓ Branch 0 taken 11924 times.
✓ Branch 1 taken 474 times.
12398 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
310
2/2
✓ Branch 0 taken 6102 times.
✓ Branch 1 taken 5822 times.
11924 avci->frame_thread_encoder))
311 6102 return AVERROR_EOF;
312
313 // Flushing is signaled with a NULL frame
314 6296 frame = NULL;
315 }
316
317 452065 got_packet = 0;
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 452065 times.
452065 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
320
321
2/2
✓ Branch 0 taken 63473 times.
✓ Branch 1 taken 388592 times.
452065 if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
322 /* This will unref frame. */
323 63473 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
324 else {
325 388592 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
326 }
327
328
4/4
✓ Branch 0 taken 6296 times.
✓ Branch 1 taken 445769 times.
✓ Branch 2 taken 1902 times.
✓ Branch 3 taken 4394 times.
452065 if (avci->draining && !got_packet)
329 1902 avci->draining_done = 1;
330
331 452065 return ret;
332 }
333
334 903876 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
335 {
336 int ret;
337
338
4/4
✓ Branch 0 taken 916302 times.
✓ Branch 1 taken 439639 times.
✓ Branch 2 taken 916232 times.
✓ Branch 3 taken 70 times.
1355941 while (!avpkt->data && !avpkt->side_data) {
339 916232 ret = encode_simple_internal(avctx, avpkt);
340
2/2
✓ Branch 0 taken 464167 times.
✓ Branch 1 taken 452065 times.
916232 if (ret < 0)
341 464167 return ret;
342 }
343
344 439709 return 0;
345 }
346
347 910088 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
348 {
349 910088 AVCodecInternal *avci = avctx->internal;
350 int ret;
351
352
2/2
✓ Branch 0 taken 6212 times.
✓ Branch 1 taken 903876 times.
910088 if (avci->draining_done)
353 6212 return AVERROR_EOF;
354
355
2/4
✓ Branch 0 taken 903876 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 903876 times.
903876 av_assert0(!avpkt->data && !avpkt->side_data);
356
357
2/2
✓ Branch 0 taken 289511 times.
✓ Branch 1 taken 614365 times.
903876 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
358
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 288703 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
289511 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 289511 times.
289511 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 903876 times.
903876 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 903876 ret = encode_simple_receive_packet(avctx, avpkt);
374
2/2
✓ Branch 0 taken 439709 times.
✓ Branch 1 taken 464167 times.
903876 if (ret >= 0)
375 439709 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
376
377
2/2
✓ Branch 0 taken 8004 times.
✓ Branch 1 taken 895872 times.
903876 if (ret == AVERROR_EOF)
378 8004 avci->draining_done = 1;
379
380 903876 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 139300 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
425 {
426 139300 return 0;
427 }
428 #endif
429
430 445769 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
431 {
432 445769 AVCodecInternal *avci = avctx->internal;
433 445769 EncodeContext *ec = encode_ctx(avci);
434 445769 AVFrame *dst = avci->buffer_frame;
435 int ret;
436
437
2/2
✓ Branch 0 taken 306469 times.
✓ Branch 1 taken 139300 times.
445769 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
438 /* extract audio service type metadata */
439 306469 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
440
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 306469 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
306469 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 73603 times.
✓ Branch 1 taken 232866 times.
306469 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 73603 times.
73603 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 73603 times.
73603 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 161 times.
✓ Branch 1 taken 73442 times.
73603 if (src->nb_samples < avctx->frame_size) {
455 161 ec->last_audio_frame = 1;
456
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 107 times.
161 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME)) {
457
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
458 54 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
459
460
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (out_samples != src->nb_samples) {
461 54 ret = pad_last_frame(avctx, dst, src, out_samples);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0)
463 return ret;
464 54 goto finish;
465 }
466 }
467 }
468 }
469 }
470
471 445715 ret = av_frame_ref(dst, src);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445715 times.
445715 if (ret < 0)
473 return ret;
474
475 445715 finish:
476
477
2/2
✓ Branch 0 taken 139300 times.
✓ Branch 1 taken 306469 times.
445769 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
478 139300 ret = encode_generate_icc_profile(avctx, dst);
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139300 times.
139300 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 442509 times.
445769 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
488 3260 dst->duration = 0;
489
490 445769 return 0;
491 }
492
493 453773 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
494 {
495 453773 AVCodecInternal *avci = avctx->internal;
496 int ret;
497
498
2/4
✓ Branch 1 taken 453773 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 453773 times.
453773 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 453773 times.
453773 if (avci->draining)
502 return AVERROR_EOF;
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 453773 times.
453773 if (avci->buffer_frame->buf[0])
505 return AVERROR(EAGAIN);
506
507
2/2
✓ Branch 0 taken 8004 times.
✓ Branch 1 taken 445769 times.
453773 if (!frame) {
508 8004 avci->draining = 1;
509 } else {
510 445769 ret = encode_send_frame_internal(avctx, frame);
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445769 times.
445769 if (ret < 0)
512 return ret;
513 }
514
515
2/4
✓ Branch 0 taken 453773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 453773 times.
✗ Branch 3 not taken.
453773 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
516 453773 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
517
5/6
✓ Branch 0 taken 16666 times.
✓ Branch 1 taken 437107 times.
✓ Branch 2 taken 6212 times.
✓ Branch 3 taken 10454 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6212 times.
453773 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
518 return ret;
519 }
520
521 453773 avctx->frame_num++;
522
523 453773 return 0;
524 }
525
526 893422 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
527 {
528 893422 AVCodecInternal *avci = avctx->internal;
529 int ret;
530
531 893422 av_packet_unref(avpkt);
532
533
2/4
✓ Branch 1 taken 893422 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 893422 times.
893422 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
534 return AVERROR(EINVAL);
535
536
4/4
✓ Branch 0 taken 456385 times.
✓ Branch 1 taken 437037 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 456315 times.
893422 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
537 437107 av_packet_move_ref(avpkt, avci->buffer_pkt);
538 } else {
539 456315 ret = encode_receive_packet_internal(avctx, avpkt);
540
2/2
✓ Branch 0 taken 453713 times.
✓ Branch 1 taken 2602 times.
456315 if (ret < 0)
541 453713 return ret;
542 }
543
544 439709 return 0;
545 }
546
547 19830 static int encode_preinit_video(AVCodecContext *avctx)
548 {
549 19830 const AVCodec *c = avctx->codec;
550 19830 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 19830 times.
19830 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 19830 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 19830 times.
19830 if (ret < 0)
563 return ret;
564
565
2/2
✓ Branch 0 taken 814 times.
✓ Branch 1 taken 19016 times.
19830 if (pix_fmts) {
566
1/2
✓ Branch 0 taken 2678 times.
✗ Branch 1 not taken.
2678 for (i = 0; i < num_pix_fmts; i++)
567
2/2
✓ Branch 0 taken 814 times.
✓ Branch 1 taken 1864 times.
2678 if (avctx->pix_fmt == pix_fmts[i])
568 814 break;
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 814 times.
814 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 793 times.
✓ Branch 1 taken 21 times.
814 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
583
1/2
✓ Branch 0 taken 793 times.
✗ Branch 1 not taken.
793 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
584
2/2
✓ Branch 0 taken 789 times.
✓ Branch 1 taken 4 times.
793 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
585
1/2
✓ Branch 0 taken 789 times.
✗ Branch 1 not taken.
789 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
586
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 781 times.
789 pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
587 33 avctx->color_range = AVCOL_RANGE_JPEG;
588 }
589
590
1/2
✓ Branch 0 taken 19830 times.
✗ Branch 1 not taken.
19830 if ( avctx->bits_per_raw_sample < 0
591
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 19747 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
19830 || (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 19830 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19830 times.
19830 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 19830 times.
19830 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 19830 return 0;
621 }
622
623 1348 static int encode_preinit_audio(AVCodecContext *avctx)
624 {
625 1348 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 1348 times.
1348 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 1348 times.
1348 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 1348 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 1348 times.
1348 if (ret < 0)
646 return ret;
647
1/2
✓ Branch 0 taken 1348 times.
✗ Branch 1 not taken.
1348 if (sample_fmts) {
648
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 for (i = 0; i < num_sample_fmts; i++) {
649
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 17 times.
1365 if (avctx->sample_fmt == sample_fmts[i])
650 1348 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 1348 times.
1348 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 1348 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 1348 times.
1348 if (ret < 0)
677 return ret;
678
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 1287 times.
1348 if (supported_samplerates) {
679
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 for (i = 0; i < num_samplerates; i++)
680
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 71 times.
132 if (avctx->sample_rate == supported_samplerates[i])
681 61 break;
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 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 1348 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 1348 times.
1348 if (ret < 0)
697 return ret;
698
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1269 times.
1348 if (ch_layouts) {
699
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
140 for (i = 0; i < num_ch_layouts; i++) {
700
2/2
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 61 times.
140 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
701 79 break;
702 }
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 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 1295 times.
✓ Branch 1 taken 53 times.
1348 if (!avctx->bits_per_raw_sample)
720 1295 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
721
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1177 times.
1348 if (!avctx->bits_per_raw_sample)
722 171 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
723
724 1348 return 0;
725 }
726
727 21216 int ff_encode_preinit(AVCodecContext *avctx)
728 {
729 21216 AVCodecInternal *avci = avctx->internal;
730 21216 EncodeContext *ec = encode_ctx(avci);
731 21216 int ret = 0;
732
733
2/4
✓ Branch 0 taken 21216 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21216 times.
21216 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 21216 times.
21216 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 21127 times.
✓ Branch 1 taken 89 times.
21216 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21127 times.
21127 !(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 19830 times.
✓ Branch 1 taken 1348 times.
✓ Branch 2 taken 38 times.
21216 switch (avctx->codec_type) {
751 19830 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
752 1348 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
753 }
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21216 times.
21216 if (ret < 0)
755 return ret;
756
757
4/4
✓ Branch 0 taken 1386 times.
✓ Branch 1 taken 19830 times.
✓ Branch 2 taken 1348 times.
✓ Branch 3 taken 38 times.
21216 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
758
3/4
✓ Branch 0 taken 21157 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21157 times.
21178 && 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 21214 times.
✓ Branch 1 taken 2 times.
21216 if (!avctx->rc_initial_buffer_occupancy)
763 21214 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
764
765
2/2
✓ Branch 0 taken 20822 times.
✓ Branch 1 taken 394 times.
21216 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
766 20822 ec->intra_only_flag = AV_PKT_FLAG_KEY;
767
768
2/2
✓ Branch 1 taken 21178 times.
✓ Branch 2 taken 38 times.
21216 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
769 21178 avci->in_frame = av_frame_alloc();
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21178 times.
21178 if (!avci->in_frame)
771 return AVERROR(ENOMEM);
772 }
773
774
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21214 times.
21216 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 190944 times.
✓ Branch 1 taken 21216 times.
212160 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
787 190944 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
788 190944 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
789 const AVFrameSideData *sd_frame;
790 AVPacketSideData *sd_packet;
791
792 190944 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 190712 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 232 times.
191176 if (!sd_frame ||
796 232 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
797 type_packet))
798
799 190712 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 21216 ret = ff_frame_thread_encoder_init(avctx);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21216 times.
21216 if (ret < 0)
812 return ret;
813 }
814
815 21216 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 21216 AVCodecInternal *ff_encode_internal_alloc(void)
876 {
877 21216 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