FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-05-27 12:09:14
Exec Total Coverage
Lines: 364 516 70.5%
Functions: 26 27 96.3%
Branches: 269 421 63.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/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 1068559 static EncodeContext *encode_ctx(AVCodecInternal *avci)
57 {
58 1068559 return (EncodeContext*)avci;
59 }
60
61 29876 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
62 {
63
2/4
✓ Branch 0 taken 29876 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29876 times.
29876 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 29876 times.
29876 av_assert0(!avpkt->data);
70
71 29876 av_fast_padded_malloc(&avctx->internal->byte_buffer,
72 29876 &avctx->internal->byte_buffer_size, size);
73 29876 avpkt->data = avctx->internal->byte_buffer;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29876 times.
29876 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 29876 avpkt->size = size;
79
80 29876 return 0;
81 }
82
83 519607 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
84 {
85 int ret;
86
87
2/4
✓ Branch 0 taken 519607 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519607 times.
519607 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 return AVERROR(EINVAL);
89
90
2/4
✓ Branch 0 taken 519607 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519607 times.
519607 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 519607 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519607 times.
519607 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 519607 avpkt->data = avpkt->buf->data;
101
102 519607 return 0;
103 }
104
105 519607 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 519607 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519607 times.
519607 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
110 return AVERROR(EINVAL);
111
112
2/4
✓ Branch 0 taken 519607 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519607 times.
519607 av_assert0(!avpkt->data && !avpkt->buf);
113
114 519607 avpkt->size = size;
115 519607 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519607 times.
519607 if (ret < 0)
117 goto fail;
118
119
2/4
✓ Branch 0 taken 519607 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519607 times.
519607 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 519607 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
125
126 519607 ret = 0;
127 519607 fail:
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519607 times.
519607 if (ret < 0) {
129 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
130 av_packet_unref(avpkt);
131 }
132
133 519607 return ret;
134 }
135
136 520281 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
137 {
138 520281 uint8_t *data = avpkt->data;
139 int ret;
140
141
2/2
✓ Branch 0 taken 490405 times.
✓ Branch 1 taken 29876 times.
520281 if (avpkt->buf)
142 490405 return 0;
143
144 29876 avpkt->data = NULL;
145 29876 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29876 times.
29876 if (ret < 0)
147 return ret;
148 29876 memcpy(avpkt->data, data, avpkt->size);
149
150 29876 return 0;
151 }
152
153 /**
154 * Pad last frame with silence.
155 */
156 115 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
157 {
158 AVFrameSideData *sd;
159 int discard_padding;
160 int ret;
161
162 115 frame->format = src->format;
163 115 frame->nb_samples = out_samples;
164 115 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (ret < 0)
166 goto fail;
167 115 ret = av_frame_get_buffer(frame, 0);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (ret < 0)
169 goto fail;
170
171 115 ret = av_frame_copy_props(frame, src);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (ret < 0)
173 goto fail;
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
176 115 src->nb_samples, s->ch_layout.nb_channels,
177 s->sample_fmt)) < 0)
178 goto fail;
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
180 115 frame->nb_samples - src->nb_samples,
181 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
182 goto fail;
183
184 115 discard_padding = frame->nb_samples - src->nb_samples;
185 av_assert1(discard_padding > 0);
186 115 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (!sd) {
188 ret = AVERROR(ENOMEM);
189 goto fail;
190 }
191 115 AV_WL32A(sd->data, 0);
192 115 AV_WL32A(sd->data + 4, discard_padding);
193 115 AV_WL16A(sd->data + 8, 0);
194
195 115 return 0;
196
197 fail:
198 av_frame_unref(frame);
199 encode_ctx(s->internal)->last_audio_frame = 0;
200 return ret;
201 }
202
203 980 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
204 const AVSubtitle *sub)
205 {
206 int ret;
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980 times.
980 if (sub->start_display_time) {
208 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
209 return -1;
210 }
211
212 980 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
213 980 avctx->frame_num++;
214 980 return ret;
215 }
216
217 1062199 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
218 {
219 1062199 AVCodecInternal *avci = avctx->internal;
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1062199 times.
1062199 if (avci->draining)
222 return AVERROR_EOF;
223
224
2/2
✓ Branch 0 taken 535784 times.
✓ Branch 1 taken 526415 times.
1062199 if (!avci->buffer_frame->buf[0])
225 535784 return AVERROR(EAGAIN);
226
227 526415 av_frame_move_ref(frame, avci->buffer_frame);
228
229 526415 return 0;
230 }
231
232 515578 int ff_encode_reordered_opaque(AVCodecContext *avctx,
233 AVPacket *pkt, const AVFrame *frame)
234 {
235
2/2
✓ Branch 0 taken 512033 times.
✓ Branch 1 taken 3545 times.
515578 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
236 512033 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 512033 times.
512033 if (ret < 0)
238 return ret;
239 512033 pkt->opaque = frame->opaque;
240 }
241
242 515578 return 0;
243 }
244
245 526923 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
246 AVFrame *frame, int *got_packet)
247 {
248 526923 const FFCodec *const codec = ffcodec(avctx->codec);
249 int ret;
250
251 526923 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
252 ff_assert1_fpu();
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526923 times.
526923 av_assert0(ret <= 0);
254
255
3/4
✓ Branch 0 taken 526923 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6567 times.
✓ Branch 3 taken 520356 times.
526923 if (!ret && *got_packet) {
256
2/2
✓ Branch 0 taken 520281 times.
✓ Branch 1 taken 75 times.
520356 if (avpkt->data) {
257 520281 ret = encode_make_refcounted(avctx, avpkt);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520281 times.
520281 if (ret < 0)
259 goto unref;
260 // Date returned by encoders must always be ref-counted
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520281 times.
520281 av_assert0(avpkt->buf);
262 }
263
264 // set the timestamps for the simple no-delay case
265 // encoders with delay have to set the timestamps themselves
266
4/4
✓ Branch 0 taken 28124 times.
✓ Branch 1 taken 492232 times.
✓ Branch 2 taken 27888 times.
✓ Branch 3 taken 236 times.
520356 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
267
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 12270 times.
27888 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
268
2/2
✓ Branch 0 taken 492556 times.
✓ Branch 1 taken 15294 times.
507850 if (avpkt->pts == AV_NOPTS_VALUE)
269 492556 avpkt->pts = frame->pts;
270
271
2/2
✓ Branch 0 taken 503465 times.
✓ Branch 1 taken 4385 times.
507850 if (!avpkt->duration) {
272
2/2
✓ Branch 0 taken 482807 times.
✓ Branch 1 taken 20658 times.
503465 if (frame->duration)
273 482807 avpkt->duration = frame->duration;
274
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17458 times.
20658 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
275 3200 avpkt->duration = ff_samples_to_time_base(avctx,
276 3200 frame->nb_samples);
277 }
278
2/2
✓ Branch 0 taken 371733 times.
✓ Branch 1 taken 131732 times.
503465 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
279 371733 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
280
3/4
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 371580 times.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
371733 if (sd && sd->size >= 10) {
281 153 uint8_t *skip_samples = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
153 if (!skip_samples) {
283 ret = AVERROR(ENOMEM);
284 goto unref;
285 }
286 153 AV_WL32A(skip_samples + 0, AV_RL32(sd->data + 0));
287 153 AV_WL32A(skip_samples + 4, AV_RL32(sd->data + 4));
288 153 AV_WB8 (skip_samples + 8, AV_RB8 (sd->data + 8));
289 153 AV_WB8 (skip_samples + 9, AV_RB8 (sd->data + 9));
290 }
291 }
292 }
293
294 507850 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507850 times.
507850 if (ret < 0)
296 goto unref;
297 }
298
299 // dts equals pts unless there is reordering
300 // there can be no reordering if there is no encoder delay
301
2/2
✓ Branch 0 taken 8467 times.
✓ Branch 1 taken 511889 times.
520356 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
302
2/2
✓ Branch 0 taken 7702 times.
✓ Branch 1 taken 765 times.
8467 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7702 times.
7702 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
304 512654 avpkt->dts = avpkt->pts;
305 } else {
306 6567 unref:
307 6567 av_packet_unref(avpkt);
308 }
309
310
2/2
✓ Branch 0 taken 526415 times.
✓ Branch 1 taken 508 times.
526923 if (frame)
311 526415 av_frame_unref(frame);
312
313 526923 return ret;
314 }
315
316 1075788 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
317 {
318 1075788 AVCodecInternal *avci = avctx->internal;
319 1075788 AVFrame *frame = avci->in_frame;
320 1075788 const FFCodec *const codec = ffcodec(avctx->codec);
321 int got_packet;
322 int ret;
323
324
2/2
✓ Branch 0 taken 1957 times.
✓ Branch 1 taken 1073831 times.
1075788 if (avci->draining_done)
325 1957 return AVERROR_EOF;
326
327
3/4
✓ Branch 0 taken 1073831 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1062199 times.
✓ Branch 3 taken 11632 times.
1073831 if (!frame->buf[0] && !avci->draining) {
328 1062199 av_frame_unref(frame);
329 1062199 ret = ff_encode_get_frame(avctx, frame);
330
3/4
✓ Branch 0 taken 535784 times.
✓ Branch 1 taken 526415 times.
✓ Branch 2 taken 535784 times.
✗ Branch 3 not taken.
1062199 if (ret < 0 && ret != AVERROR_EOF)
331 535784 return ret;
332 }
333
334
2/2
✓ Branch 0 taken 11632 times.
✓ Branch 1 taken 526415 times.
538047 if (!frame->buf[0]) {
335
2/2
✓ Branch 0 taken 11124 times.
✓ Branch 1 taken 508 times.
11632 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
336
2/2
✓ Branch 0 taken 6303 times.
✓ Branch 1 taken 4821 times.
11124 avci->frame_thread_encoder))
337 6303 return AVERROR_EOF;
338
339 // Flushing is signaled with a NULL frame
340 5329 frame = NULL;
341 }
342
343 531744 got_packet = 0;
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 531744 times.
531744 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
346
347 #if CONFIG_FRAME_THREAD_ENCODER
348
2/2
✓ Branch 0 taken 63342 times.
✓ Branch 1 taken 468402 times.
531744 if (avci->frame_thread_encoder)
349 /* This will unref frame. */
350 63342 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
351 else
352 #endif
353 468402 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
354
355
4/4
✓ Branch 0 taken 5329 times.
✓ Branch 1 taken 526415 times.
✓ Branch 2 taken 1957 times.
✓ Branch 3 taken 3372 times.
531744 if (avci->draining && !got_packet)
356 1957 avci->draining_done = 1;
357
358 531744 return ret;
359 }
360
361 1064400 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
362 {
363 int ret;
364
365
4/4
✓ Branch 0 taken 1075863 times.
✓ Branch 1 taken 520281 times.
✓ Branch 2 taken 1075788 times.
✓ Branch 3 taken 75 times.
1596144 while (!avpkt->data && !avpkt->side_data) {
366 1075788 ret = encode_simple_internal(avctx, avpkt);
367
2/2
✓ Branch 0 taken 544044 times.
✓ Branch 1 taken 531744 times.
1075788 if (ret < 0)
368 544044 return ret;
369 }
370
371 520356 return 0;
372 }
373
374 1070813 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
375 {
376 1070813 AVCodecInternal *avci = avctx->internal;
377 int ret;
378
379
2/2
✓ Branch 0 taken 6413 times.
✓ Branch 1 taken 1064400 times.
1070813 if (avci->draining_done)
380 6413 return AVERROR_EOF;
381
382
2/4
✓ Branch 0 taken 1064400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1064400 times.
1064400 av_assert0(!avpkt->data && !avpkt->side_data);
383
384
2/2
✓ Branch 0 taken 297366 times.
✓ Branch 1 taken 767034 times.
1064400 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
385
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 296558 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
297366 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
386 808 avctx->stats_out[0] = '\0';
387 }
388
389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1064400 times.
1064400 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
390 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
391 if (ret < 0)
392 av_packet_unref(avpkt);
393 else
394 // Encoders must always return ref-counted buffers.
395 // Side-data only packets have no data and can be not ref-counted.
396 av_assert0(!avpkt->data || avpkt->buf);
397 } else
398 1064400 ret = encode_simple_receive_packet(avctx, avpkt);
399
2/2
✓ Branch 0 taken 520356 times.
✓ Branch 1 taken 544044 times.
1064400 if (ret >= 0)
400 520356 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
401
402
2/2
✓ Branch 0 taken 8260 times.
✓ Branch 1 taken 1056140 times.
1064400 if (ret == AVERROR_EOF)
403 8260 avci->draining_done = 1;
404
405 1064400 return ret;
406 }
407
408 #if CONFIG_LCMS2
409 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
410 {
411 enum AVColorTransferCharacteristic trc = frame->color_trc;
412 enum AVColorPrimaries prim = frame->color_primaries;
413 const FFCodec *const codec = ffcodec(avctx->codec);
414 AVCodecInternal *avci = avctx->internal;
415 cmsHPROFILE profile;
416 int ret;
417
418 /* don't generate ICC profiles if disabled or unsupported */
419 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
420 return 0;
421 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
422 return 0;
423
424 if (trc == AVCOL_TRC_UNSPECIFIED)
425 trc = avctx->color_trc;
426 if (prim == AVCOL_PRI_UNSPECIFIED)
427 prim = avctx->color_primaries;
428 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
429 return 0; /* can't generate ICC profile with missing csp tags */
430
431 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
432 return 0; /* don't overwrite existing ICC profile */
433
434 if (!avci->icc.avctx) {
435 ret = ff_icc_context_init(&avci->icc, avctx);
436 if (ret < 0)
437 return ret;
438 }
439
440 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
441 if (ret < 0)
442 return ret;
443
444 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
445 cmsCloseProfile(profile);
446 return ret;
447 }
448 #else /* !CONFIG_LCMS2 */
449 143639 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
450 {
451 143639 return 0;
452 }
453 #endif
454
455 526415 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
456 {
457 526415 AVCodecInternal *avci = avctx->internal;
458 526415 EncodeContext *ec = encode_ctx(avci);
459 526415 AVFrame *dst = avci->buffer_frame;
460 int ret;
461
462
2/2
✓ Branch 0 taken 382776 times.
✓ Branch 1 taken 143639 times.
526415 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
463 /* extract audio service type metadata */
464 382776 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
465
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 382776 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
382776 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
466 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
467
468 /* check for valid frame size */
469
2/2
✓ Branch 0 taken 107556 times.
✓ Branch 1 taken 275220 times.
382776 if (avctx->frame_size) {
470 /* if we already got an undersized frame, that must have been the last */
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107556 times.
107556 if (ec->last_audio_frame) {
472 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
473 return AVERROR(EINVAL);
474 }
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107556 times.
107556 if (src->nb_samples > avctx->frame_size) {
476 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
477 return AVERROR(EINVAL);
478 }
479
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 107376 times.
107556 if (src->nb_samples < avctx->frame_size) {
480 180 ec->last_audio_frame = 1;
481
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 58 times.
180 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
482
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 65 times.
122 (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
483
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 112 times.
115 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
484 115 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
485
486
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 if (out_samples != src->nb_samples) {
487 115 ret = pad_last_frame(avctx, dst, src, out_samples);
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
115 if (ret < 0)
489 return ret;
490 115 goto finish;
491 }
492 }
493 }
494 }
495 }
496
497 526300 ret = av_frame_ref(dst, src);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526300 times.
526300 if (ret < 0)
499 return ret;
500
501 526300 finish:
502
503
2/2
✓ Branch 0 taken 143639 times.
✓ Branch 1 taken 382776 times.
526415 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
504 143639 ret = encode_generate_icc_profile(avctx, dst);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143639 times.
143639 if (ret < 0)
506 return ret;
507 }
508
509 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
510 // since otherwise we cannot be sure that whatever value it has is in the
511 // right timebase, so we would produce an incorrect value, which is worse
512 // than none at all
513
2/2
✓ Branch 0 taken 3266 times.
✓ Branch 1 taken 523149 times.
526415 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
514 3266 dst->duration = 0;
515
516 526415 return 0;
517 }
518
519 534675 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
520 {
521 534675 AVCodecInternal *avci = avctx->internal;
522 int ret;
523
524
2/4
✓ Branch 1 taken 534675 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 534675 times.
534675 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
525 return AVERROR(EINVAL);
526
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534675 times.
534675 if (avci->draining)
528 return AVERROR_EOF;
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534675 times.
534675 if (avci->buffer_frame->buf[0])
531 return AVERROR(EAGAIN);
532
533
2/2
✓ Branch 0 taken 8260 times.
✓ Branch 1 taken 526415 times.
534675 if (!frame) {
534 8260 avci->draining = 1;
535 } else {
536 526415 ret = encode_send_frame_internal(avctx, frame);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526415 times.
526415 if (ret < 0)
538 return ret;
539 }
540
541
2/4
✓ Branch 0 taken 534675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 534675 times.
✗ Branch 3 not taken.
534675 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
542 534675 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
543
5/6
✓ Branch 0 taken 15844 times.
✓ Branch 1 taken 518831 times.
✓ Branch 2 taken 6413 times.
✓ Branch 3 taken 9431 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6413 times.
534675 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
544 return ret;
545 }
546
547 534675 avctx->frame_num++;
548
549 534675 return 0;
550 }
551
552 1054969 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
553 {
554 1054969 AVCodecInternal *avci = avctx->internal;
555 int ret;
556
557 1054969 av_packet_unref(avpkt);
558
559
2/4
✓ Branch 1 taken 1054969 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1054969 times.
1054969 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
560 return AVERROR(EINVAL);
561
562
4/4
✓ Branch 0 taken 536213 times.
✓ Branch 1 taken 518756 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 536138 times.
1054969 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
563 518831 av_packet_move_ref(avpkt, avci->buffer_pkt);
564 } else {
565 536138 ret = encode_receive_packet_internal(avctx, avpkt);
566
2/2
✓ Branch 0 taken 534613 times.
✓ Branch 1 taken 1525 times.
536138 if (ret < 0)
567 534613 return ret;
568 }
569
570 520356 return 0;
571 }
572
573 20352 static int encode_preinit_video(AVCodecContext *avctx)
574 {
575 20352 const AVCodec *c = avctx->codec;
576 20352 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
577 const enum AVPixelFormat *pix_fmts;
578 int ret, i, num_pix_fmts;
579
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20352 times.
20352 if (!pixdesc) {
581 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
582 avctx->pix_fmt);
583 return AVERROR(EINVAL);
584 }
585
586 20352 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
587 0, (const void **) &pix_fmts, &num_pix_fmts);
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20352 times.
20352 if (ret < 0)
589 return ret;
590
591
2/2
✓ Branch 0 taken 830 times.
✓ Branch 1 taken 19522 times.
20352 if (pix_fmts) {
592
1/2
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
2716 for (i = 0; i < num_pix_fmts; i++)
593
2/2
✓ Branch 0 taken 830 times.
✓ Branch 1 taken 1886 times.
2716 if (avctx->pix_fmt == pix_fmts[i])
594 830 break;
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 830 times.
830 if (i == num_pix_fmts) {
596 av_log(avctx, AV_LOG_ERROR,
597 "Specified pixel format %s is not supported by the %s encoder.\n",
598 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
599
600 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
601 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
602 av_log(avctx, AV_LOG_ERROR, " %s\n",
603 av_get_pix_fmt_name(pix_fmts[p]));
604 }
605
606 return AVERROR(EINVAL);
607 }
608
2/2
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 21 times.
830 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
609
1/2
✓ Branch 0 taken 809 times.
✗ Branch 1 not taken.
809 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
610
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 4 times.
809 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
611
1/2
✓ Branch 0 taken 805 times.
✗ Branch 1 not taken.
805 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
612
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 797 times.
805 pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
613 33 avctx->color_range = AVCOL_RANGE_JPEG;
614 }
615
616
2/2
✓ Branch 0 taken 2501 times.
✓ Branch 1 taken 17851 times.
20352 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
617 const enum AVAlphaMode *alpha_modes;
618 int num_alpha_modes;
619 2501 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_ALPHA_MODE,
620 0, (const void **) &alpha_modes, &num_alpha_modes);
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2501 times.
2501 if (ret < 0)
622 return ret;
623
624
4/4
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 1977 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 520 times.
2501 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
625
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (i = 0; i < num_alpha_modes; i++) {
626
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->alpha_mode == alpha_modes[i])
627 4 break;
628 }
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (i == num_alpha_modes) {
630 av_log(avctx, AV_LOG_ERROR,
631 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
632 av_alpha_mode_name(avctx->alpha_mode), c->name);
633 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
634 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
635 av_log(avctx, AV_LOG_ERROR, " %s\n",
636 av_alpha_mode_name(alpha_modes[p]));
637 }
638 return AVERROR(EINVAL);
639 }
640 }
641 }
642
643
1/2
✓ Branch 0 taken 20352 times.
✗ Branch 1 not taken.
20352 if ( avctx->bits_per_raw_sample < 0
644
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20269 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
20352 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
645 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
646 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
647 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
648 }
649
2/4
✓ Branch 0 taken 20352 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20352 times.
20352 if (avctx->width <= 0 || avctx->height <= 0) {
650 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
651 return AVERROR(EINVAL);
652 }
653
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20352 times.
20352 if (avctx->hw_frames_ctx) {
655 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
656 if (frames_ctx->format != avctx->pix_fmt) {
657 av_log(avctx, AV_LOG_ERROR,
658 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
659 return AVERROR(EINVAL);
660 }
661 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
662 avctx->sw_pix_fmt != frames_ctx->sw_format) {
663 av_log(avctx, AV_LOG_ERROR,
664 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
665 "and AVHWFramesContext.sw_format (%s)\n",
666 av_get_pix_fmt_name(avctx->sw_pix_fmt),
667 av_get_pix_fmt_name(frames_ctx->sw_format));
668 return AVERROR(EINVAL);
669 }
670 avctx->sw_pix_fmt = frames_ctx->sw_format;
671 }
672
673 20352 return 0;
674 }
675
676 1394 static int encode_preinit_audio(AVCodecContext *avctx)
677 {
678 1394 const AVCodec *c = avctx->codec;
679 const enum AVSampleFormat *sample_fmts;
680 const int *supported_samplerates;
681 const AVChannelLayout *ch_layouts;
682 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
683
684
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1394 times.
1394 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
685 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
686 avctx->sample_fmt);
687 return AVERROR(EINVAL);
688 }
689
690 1394 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
691 0, (const void **) &sample_fmts,
692 &num_sample_fmts);
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
1394 if (ret < 0)
694 return ret;
695
1/2
✓ Branch 0 taken 1394 times.
✗ Branch 1 not taken.
1394 if (sample_fmts) {
696
1/2
✓ Branch 0 taken 1413 times.
✗ Branch 1 not taken.
1413 for (i = 0; i < num_sample_fmts; i++) {
697
2/2
✓ Branch 0 taken 1394 times.
✓ Branch 1 taken 19 times.
1413 if (avctx->sample_fmt == sample_fmts[i])
698 1394 break;
699
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
24 if (avctx->ch_layout.nb_channels == 1 &&
700 5 av_get_planar_sample_fmt(avctx->sample_fmt) ==
701 5 av_get_planar_sample_fmt(sample_fmts[i])) {
702 avctx->sample_fmt = sample_fmts[i];
703 break;
704 }
705 }
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
1394 if (i == num_sample_fmts) {
707 av_log(avctx, AV_LOG_ERROR,
708 "Specified sample format %s is not supported by the %s encoder\n",
709 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
710
711 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
712 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
713 av_log(avctx, AV_LOG_ERROR, " %s\n",
714 av_get_sample_fmt_name(sample_fmts[p]));
715 }
716
717 return AVERROR(EINVAL);
718 }
719 }
720
721 1394 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
722 0, (const void **) &supported_samplerates,
723 &num_samplerates);
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
1394 if (ret < 0)
725 return ret;
726
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1314 times.
1394 if (supported_samplerates) {
727
1/2
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
176 for (i = 0; i < num_samplerates; i++)
728
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 96 times.
176 if (avctx->sample_rate == supported_samplerates[i])
729 80 break;
730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (i == num_samplerates) {
731 av_log(avctx, AV_LOG_ERROR,
732 "Specified sample rate %d is not supported by the %s encoder\n",
733 avctx->sample_rate, c->name);
734
735 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
736 for (int p = 0; supported_samplerates[p]; p++)
737 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
738
739 return AVERROR(EINVAL);
740 }
741 }
742 1394 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
743 0, (const void **) &ch_layouts, &num_ch_layouts);
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
1394 if (ret < 0)
745 return ret;
746
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1300 times.
1394 if (ch_layouts) {
747
1/2
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
186 for (i = 0; i < num_ch_layouts; i++) {
748
2/2
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 92 times.
186 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
749 94 break;
750 }
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (i == num_ch_layouts) {
752 char buf[512];
753 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
754 av_log(avctx, AV_LOG_ERROR,
755 "Specified channel layout '%s' is not supported by the %s encoder\n",
756 ret > 0 ? buf : "?", c->name);
757
758 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
759 for (int p = 0; ch_layouts[p].nb_channels; p++) {
760 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
761 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
762 }
763 return AVERROR(EINVAL);
764 }
765 }
766
767
2/2
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 55 times.
1394 if (!avctx->bits_per_raw_sample)
768 1339 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
769
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 1201 times.
1394 if (!avctx->bits_per_raw_sample)
770 193 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
771
772 1394 return 0;
773 }
774
775 21788 int ff_encode_preinit(AVCodecContext *avctx)
776 {
777 21788 AVCodecInternal *avci = avctx->internal;
778 21788 EncodeContext *ec = encode_ctx(avci);
779 21788 int ret = 0;
780
781
2/4
✓ Branch 0 taken 21788 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21788 times.
21788 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
782 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
783 return AVERROR(EINVAL);
784 }
785
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21788 times.
21788 if (avctx->bit_rate < 0) {
787 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
788 return AVERROR(EINVAL);
789 }
790
791
2/2
✓ Branch 0 taken 21691 times.
✓ Branch 1 taken 97 times.
21788 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21691 times.
21691 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
793 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
794 "encoder does not support it.\n");
795 return AVERROR(EINVAL);
796 }
797
798
3/3
✓ Branch 0 taken 20352 times.
✓ Branch 1 taken 1394 times.
✓ Branch 2 taken 42 times.
21788 switch (avctx->codec_type) {
799 20352 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
800 1394 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
801 }
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21788 times.
21788 if (ret < 0)
803 return ret;
804
805
4/4
✓ Branch 0 taken 1436 times.
✓ Branch 1 taken 20352 times.
✓ Branch 2 taken 1394 times.
✓ Branch 3 taken 42 times.
21788 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
806
3/4
✓ Branch 0 taken 21722 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21722 times.
21746 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
807 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
808 }
809
810
2/2
✓ Branch 0 taken 21786 times.
✓ Branch 1 taken 2 times.
21788 if (!avctx->rc_initial_buffer_occupancy)
811 21786 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
812
813
2/2
✓ Branch 0 taken 21369 times.
✓ Branch 1 taken 419 times.
21788 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
814 21369 ec->intra_only_flag = AV_PKT_FLAG_KEY;
815
816
2/2
✓ Branch 1 taken 21746 times.
✓ Branch 2 taken 42 times.
21788 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
817 21746 avci->in_frame = av_frame_alloc();
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21746 times.
21746 if (!avci->in_frame)
819 return AVERROR(ENOMEM);
820 }
821
822
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21786 times.
21788 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
823
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
824 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
825 "from an encoder not supporting it\n");
826 return AVERROR(ENOSYS);
827 }
828
829 2 avci->recon_frame = av_frame_alloc();
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!avci->recon_frame)
831 return AVERROR(ENOMEM);
832 }
833
834
2/2
✓ Branch 0 taken 239668 times.
✓ Branch 1 taken 21788 times.
261456 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
835 239668 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
836 239668 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
837 const AVFrameSideData *sd_frame;
838 AVPacketSideData *sd_packet;
839
840 239668 sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
841 avctx->nb_decoded_side_data,
842 type_frame);
843
3/4
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 239421 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 247 times.
239915 if (!sd_frame ||
844 247 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
845 type_packet))
846
847 239421 continue;
848
849 247 sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
850 247 type_packet, sd_frame->size, 0);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
247 if (!sd_packet)
852 return AVERROR(ENOMEM);
853
854 247 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
855 }
856
857 #if CONFIG_FRAME_THREAD_ENCODER
858 21788 ret = ff_frame_thread_encoder_init(avctx);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21788 times.
21788 if (ret < 0)
860 return ret;
861 #endif
862
863 21788 return 0;
864 }
865
866 12474 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
867 {
868 int ret;
869
870 av_assert1(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
871
872 12474 frame->format = avctx->pix_fmt;
873
3/4
✓ Branch 0 taken 12458 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12458 times.
12474 if (frame->width <= 0 || frame->height <= 0) {
874 16 frame->width = avctx->width;
875 16 frame->height = avctx->height;
876 }
877
878 12474 ret = avcodec_default_get_buffer2(avctx, frame, 0);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12474 times.
12474 if (ret < 0) {
880 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
881 av_frame_unref(frame);
882 return ret;
883 }
884
885 12474 return 0;
886 }
887
888 62 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
889 {
890 62 AVCodecInternal *avci = avctx->internal;
891
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!avci->recon_frame)
893 return AVERROR(EINVAL);
894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!avci->recon_frame->buf[0])
895 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
896
897 62 av_frame_move_ref(frame, avci->recon_frame);
898 62 return 0;
899 }
900
901 void ff_encode_flush_buffers(AVCodecContext *avctx)
902 {
903 AVCodecInternal *avci = avctx->internal;
904
905 if (avci->in_frame)
906 av_frame_unref(avci->in_frame);
907 if (avci->recon_frame)
908 av_frame_unref(avci->recon_frame);
909 }
910
911 21788 AVCodecInternal *ff_encode_internal_alloc(void)
912 {
913 21788 return av_mallocz(sizeof(EncodeContext));
914 }
915
916 215 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
917 {
918 AVPacketSideData *tmp;
919 AVCPBProperties *props;
920 size_t size;
921 int i;
922
923
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 215 times.
216 for (i = 0; i < avctx->nb_coded_side_data; i++)
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
925 return (AVCPBProperties *)avctx->coded_side_data[i].data;
926
927 215 props = av_cpb_properties_alloc(&size);
928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (!props)
929 return NULL;
930
931 215 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 if (!tmp) {
933 av_freep(&props);
934 return NULL;
935 }
936
937 215 avctx->coded_side_data = tmp;
938 215 avctx->nb_coded_side_data++;
939
940 215 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
941 215 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
942 215 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
943
944 215 return props;
945 }
946
947 12922 int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[],
948 int error_count, enum AVPictureType pict_type)
949 {
950 uint8_t *side_data;
951 size_t side_data_size;
952
953 12922 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
954
1/2
✓ Branch 0 taken 12922 times.
✗ Branch 1 not taken.
12922 if (!side_data) {
955 12922 side_data_size = 4+4+8*error_count;
956 12922 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
957 side_data_size);
958 }
959
960
2/4
✓ Branch 0 taken 12922 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12922 times.
12922 if (!side_data || side_data_size < 4+4+8*error_count)
961 return AVERROR(ENOMEM);
962
963 12922 AV_WL32(side_data, quality);
964 12922 side_data[4] = pict_type;
965 12922 side_data[5] = error_count;
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12922 times.
12922 for (int i = 0; i < error_count; ++i)
967 AV_WL64(side_data+8 + 8*i , error[i]);
968
969 12922 return 0;
970 }
971
972 1623 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
973 {
974 1623 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
975 1623 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
976 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
977
2/2
✓ Branch 0 taken 4869 times.
✓ Branch 1 taken 1623 times.
6492 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
978 4869 uint16_t *matrix = matrices[m];
979
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4869 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4869 if (matrix && (types & (1U << m))) {
980 for (int i = 0; i < 64; i++) {
981 if (matrix[i] < min || matrix[i] > max) {
982 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);
983 return AVERROR(EINVAL);
984 }
985 }
986 }
987 }
988 1623 return 0;
989 }
990