FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-01-06 05:16:03
Exec Total Coverage
Lines: 349 497 70.2%
Functions: 26 27 96.3%
Branches: 260 409 63.6%

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