FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 338 484 69.8%
Functions: 25 26 96.2%
Branches: 256 401 63.8%

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