FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-06-06 18:10:07
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 1071324 static EncodeContext *encode_ctx(AVCodecInternal *avci)
57 {
58 1071324 return (EncodeContext*)avci;
59 }
60
61 29877 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
62 {
63
2/4
✓ Branch 0 taken 29877 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29877 times.
29877 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 29877 times.
29877 av_assert0(!avpkt->data);
70
71 29877 av_fast_padded_malloc(&avctx->internal->byte_buffer,
72 29877 &avctx->internal->byte_buffer_size, size);
73 29877 avpkt->data = avctx->internal->byte_buffer;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29877 times.
29877 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 29877 avpkt->size = size;
79
80 29877 return 0;
81 }
82
83 520987 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
84 {
85 int ret;
86
87
2/4
✓ Branch 0 taken 520987 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 520987 times.
520987 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 return AVERROR(EINVAL);
89
90
2/4
✓ Branch 0 taken 520987 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 520987 times.
520987 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 520987 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520987 times.
520987 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 520987 avpkt->data = avpkt->buf->data;
101
102 520987 return 0;
103 }
104
105 520987 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 520987 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 520987 times.
520987 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
110 return AVERROR(EINVAL);
111
112
2/4
✓ Branch 0 taken 520987 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 520987 times.
520987 av_assert0(!avpkt->data && !avpkt->buf);
113
114 520987 avpkt->size = size;
115 520987 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520987 times.
520987 if (ret < 0)
117 goto fail;
118
119
2/4
✓ Branch 0 taken 520987 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 520987 times.
520987 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 520987 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
125
126 520987 ret = 0;
127 520987 fail:
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520987 times.
520987 if (ret < 0) {
129 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
130 av_packet_unref(avpkt);
131 }
132
133 520987 return ret;
134 }
135
136 521661 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
137 {
138 521661 uint8_t *data = avpkt->data;
139 int ret;
140
141
2/2
✓ Branch 0 taken 491784 times.
✓ Branch 1 taken 29877 times.
521661 if (avpkt->buf)
142 491784 return 0;
143
144 29877 avpkt->data = NULL;
145 29877 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29877 times.
29877 if (ret < 0)
147 return ret;
148 29877 memcpy(avpkt->data, data, avpkt->size);
149
150 29877 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 1064973 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
218 {
219 1064973 AVCodecInternal *avci = avctx->internal;
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1064973 times.
1064973 if (avci->draining)
222 return AVERROR_EOF;
223
224
2/2
✓ Branch 0 taken 537178 times.
✓ Branch 1 taken 527795 times.
1064973 if (!avci->buffer_frame->buf[0])
225 537178 return AVERROR(EAGAIN);
226
227 527795 av_frame_move_ref(frame, avci->buffer_frame);
228
229 527795 return 0;
230 }
231
232 516958 int ff_encode_reordered_opaque(AVCodecContext *avctx,
233 AVPacket *pkt, const AVFrame *frame)
234 {
235
2/2
✓ Branch 0 taken 513413 times.
✓ Branch 1 taken 3545 times.
516958 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
236 513413 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 513413 times.
513413 if (ret < 0)
238 return ret;
239 513413 pkt->opaque = frame->opaque;
240 }
241
242 516958 return 0;
243 }
244
245 528304 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
246 AVFrame *frame, int *got_packet)
247 {
248 528304 const FFCodec *const codec = ffcodec(avctx->codec);
249 int ret;
250
251 528304 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
252 ff_assert1_fpu();
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528304 times.
528304 av_assert0(ret <= 0);
254
255
3/4
✓ Branch 0 taken 528304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6568 times.
✓ Branch 3 taken 521736 times.
528304 if (!ret && *got_packet) {
256
2/2
✓ Branch 0 taken 521661 times.
✓ Branch 1 taken 75 times.
521736 if (avpkt->data) {
257 521661 ret = encode_make_refcounted(avctx, avpkt);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 521661 times.
521661 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 521661 times.
521661 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 28125 times.
✓ Branch 1 taken 493611 times.
✓ Branch 2 taken 27889 times.
✓ Branch 3 taken 236 times.
521736 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
267
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 12271 times.
27889 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
268
2/2
✓ Branch 0 taken 493953 times.
✓ Branch 1 taken 15276 times.
509229 if (avpkt->pts == AV_NOPTS_VALUE)
269 493953 avpkt->pts = frame->pts;
270
271
2/2
✓ Branch 0 taken 504844 times.
✓ Branch 1 taken 4385 times.
509229 if (!avpkt->duration) {
272
2/2
✓ Branch 0 taken 484186 times.
✓ Branch 1 taken 20658 times.
504844 if (frame->duration)
273 484186 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 373112 times.
✓ Branch 1 taken 131732 times.
504844 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
279 373112 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
280
3/4
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 372959 times.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
373112 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 509229 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 509229 times.
509229 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 8468 times.
✓ Branch 1 taken 513268 times.
521736 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
302
2/2
✓ Branch 0 taken 7703 times.
✓ Branch 1 taken 765 times.
8468 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7703 times.
7703 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
304 514033 avpkt->dts = avpkt->pts;
305 } else {
306 6568 unref:
307 6568 av_packet_unref(avpkt);
308 }
309
310
2/2
✓ Branch 0 taken 527795 times.
✓ Branch 1 taken 509 times.
528304 if (frame)
311 527795 av_frame_unref(frame);
312
313 528304 return ret;
314 }
315
316 1078582 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
317 {
318 1078582 AVCodecInternal *avci = avctx->internal;
319 1078582 AVFrame *frame = avci->in_frame;
320 1078582 const FFCodec *const codec = ffcodec(avctx->codec);
321 int got_packet;
322 int ret;
323
324
2/2
✓ Branch 0 taken 1958 times.
✓ Branch 1 taken 1076624 times.
1078582 if (avci->draining_done)
325 1958 return AVERROR_EOF;
326
327
3/4
✓ Branch 0 taken 1076624 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1064973 times.
✓ Branch 3 taken 11651 times.
1076624 if (!frame->buf[0] && !avci->draining) {
328 1064973 av_frame_unref(frame);
329 1064973 ret = ff_encode_get_frame(avctx, frame);
330
3/4
✓ Branch 0 taken 537178 times.
✓ Branch 1 taken 527795 times.
✓ Branch 2 taken 537178 times.
✗ Branch 3 not taken.
1064973 if (ret < 0 && ret != AVERROR_EOF)
331 537178 return ret;
332 }
333
334
2/2
✓ Branch 0 taken 11651 times.
✓ Branch 1 taken 527795 times.
539446 if (!frame->buf[0]) {
335
2/2
✓ Branch 0 taken 11142 times.
✓ Branch 1 taken 509 times.
11651 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
336
2/2
✓ Branch 0 taken 6307 times.
✓ Branch 1 taken 4835 times.
11142 avci->frame_thread_encoder))
337 6307 return AVERROR_EOF;
338
339 // Flushing is signaled with a NULL frame
340 5344 frame = NULL;
341 }
342
343 533139 got_packet = 0;
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533139 times.
533139 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
346
347 #if CONFIG_FRAME_THREAD_ENCODER
348
2/2
✓ Branch 0 taken 63356 times.
✓ Branch 1 taken 469783 times.
533139 if (avci->frame_thread_encoder)
349 /* This will unref frame. */
350 63356 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
351 else
352 #endif
353 469783 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
354
355
4/4
✓ Branch 0 taken 5344 times.
✓ Branch 1 taken 527795 times.
✓ Branch 2 taken 1958 times.
✓ Branch 3 taken 3386 times.
533139 if (avci->draining && !got_packet)
356 1958 avci->draining_done = 1;
357
358 533139 return ret;
359 }
360
361 1067179 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
362 {
363 int ret;
364
365
4/4
✓ Branch 0 taken 1078657 times.
✓ Branch 1 taken 521661 times.
✓ Branch 2 taken 1078582 times.
✓ Branch 3 taken 75 times.
1600318 while (!avpkt->data && !avpkt->side_data) {
366 1078582 ret = encode_simple_internal(avctx, avpkt);
367
2/2
✓ Branch 0 taken 545443 times.
✓ Branch 1 taken 533139 times.
1078582 if (ret < 0)
368 545443 return ret;
369 }
370
371 521736 return 0;
372 }
373
374 1073602 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
375 {
376 1073602 AVCodecInternal *avci = avctx->internal;
377 int ret;
378
379
2/2
✓ Branch 0 taken 6423 times.
✓ Branch 1 taken 1067179 times.
1073602 if (avci->draining_done)
380 6423 return AVERROR_EOF;
381
382
2/4
✓ Branch 0 taken 1067179 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1067179 times.
1067179 av_assert0(!avpkt->data && !avpkt->side_data);
383
384
2/2
✓ Branch 0 taken 297383 times.
✓ Branch 1 taken 769796 times.
1067179 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
385
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 296575 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
297383 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 1067179 times.
1067179 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 1067179 ret = encode_simple_receive_packet(avctx, avpkt);
399
2/2
✓ Branch 0 taken 521736 times.
✓ Branch 1 taken 545443 times.
1067179 if (ret >= 0)
400 521736 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
401
402
2/2
✓ Branch 0 taken 8265 times.
✓ Branch 1 taken 1058914 times.
1067179 if (ret == AVERROR_EOF)
403 8265 avci->draining_done = 1;
404
405 1067179 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 143640 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
450 {
451 143640 return 0;
452 }
453 #endif
454
455 527795 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
456 {
457 527795 AVCodecInternal *avci = avctx->internal;
458 527795 EncodeContext *ec = encode_ctx(avci);
459 527795 AVFrame *dst = avci->buffer_frame;
460 int ret;
461
462
2/2
✓ Branch 0 taken 384155 times.
✓ Branch 1 taken 143640 times.
527795 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
463 /* extract audio service type metadata */
464 384155 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
465
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 384155 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
384155 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 107688 times.
✓ Branch 1 taken 276467 times.
384155 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 107688 times.
107688 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 107688 times.
107688 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 107508 times.
107688 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 527680 ret = av_frame_ref(dst, src);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527680 times.
527680 if (ret < 0)
499 return ret;
500
501 527680 finish:
502
503
2/2
✓ Branch 0 taken 143640 times.
✓ Branch 1 taken 384155 times.
527795 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
504 143640 ret = encode_generate_icc_profile(avctx, dst);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143640 times.
143640 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 524529 times.
527795 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
514 3266 dst->duration = 0;
515
516 527795 return 0;
517 }
518
519 536060 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
520 {
521 536060 AVCodecInternal *avci = avctx->internal;
522 int ret;
523
524
2/4
✓ Branch 1 taken 536060 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 536060 times.
536060 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 536060 times.
536060 if (avci->draining)
528 return AVERROR_EOF;
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536060 times.
536060 if (avci->buffer_frame->buf[0])
531 return AVERROR(EAGAIN);
532
533
2/2
✓ Branch 0 taken 8265 times.
✓ Branch 1 taken 527795 times.
536060 if (!frame) {
534 8265 avci->draining = 1;
535 } else {
536 527795 ret = encode_send_frame_internal(avctx, frame);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527795 times.
527795 if (ret < 0)
538 return ret;
539 }
540
541
2/4
✓ Branch 0 taken 536060 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 536060 times.
✗ Branch 3 not taken.
536060 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
542 536060 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
543
5/6
✓ Branch 0 taken 15868 times.
✓ Branch 1 taken 520192 times.
✓ Branch 2 taken 6423 times.
✓ Branch 3 taken 9445 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6423 times.
536060 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
544 return ret;
545 }
546
547 536060 avctx->frame_num++;
548
549 536060 return 0;
550 }
551
552 1057734 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
553 {
554 1057734 AVCodecInternal *avci = avctx->internal;
555 int ret;
556
557 1057734 av_packet_unref(avpkt);
558
559
2/4
✓ Branch 1 taken 1057734 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1057734 times.
1057734 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
560 return AVERROR(EINVAL);
561
562
4/4
✓ Branch 0 taken 537617 times.
✓ Branch 1 taken 520117 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 537542 times.
1057734 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
563 520192 av_packet_move_ref(avpkt, avci->buffer_pkt);
564 } else {
565 537542 ret = encode_receive_packet_internal(avctx, avpkt);
566
2/2
✓ Branch 0 taken 535998 times.
✓ Branch 1 taken 1544 times.
537542 if (ret < 0)
567 535998 return ret;
568 }
569
570 521736 return 0;
571 }
572
573 20353 static int encode_preinit_video(AVCodecContext *avctx)
574 {
575 20353 const AVCodec *c = avctx->codec;
576 20353 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 20353 times.
20353 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 20353 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 20353 times.
20353 if (ret < 0)
589 return ret;
590
591
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 19522 times.
20353 if (pix_fmts) {
592
1/2
✓ Branch 0 taken 2717 times.
✗ Branch 1 not taken.
2717 for (i = 0; i < num_pix_fmts; i++)
593
2/2
✓ Branch 0 taken 831 times.
✓ Branch 1 taken 1886 times.
2717 if (avctx->pix_fmt == pix_fmts[i])
594 831 break;
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 831 times.
831 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 810 times.
✓ Branch 1 taken 21 times.
831 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
609
1/2
✓ Branch 0 taken 810 times.
✗ Branch 1 not taken.
810 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
610
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 4 times.
810 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
611
1/2
✓ Branch 0 taken 806 times.
✗ Branch 1 not taken.
806 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
612
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 798 times.
806 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 17852 times.
20353 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 20353 times.
✗ Branch 1 not taken.
20353 if ( avctx->bits_per_raw_sample < 0
644
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20270 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
20353 || (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 20353 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20353 times.
20353 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 20353 times.
20353 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 20353 return 0;
674 }
675
676 1398 static int encode_preinit_audio(AVCodecContext *avctx)
677 {
678 1398 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 1398 times.
1398 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 1398 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 1398 times.
1398 if (ret < 0)
694 return ret;
695
1/2
✓ Branch 0 taken 1398 times.
✗ Branch 1 not taken.
1398 if (sample_fmts) {
696
1/2
✓ Branch 0 taken 1417 times.
✗ Branch 1 not taken.
1417 for (i = 0; i < num_sample_fmts; i++) {
697
2/2
✓ Branch 0 taken 1398 times.
✓ Branch 1 taken 19 times.
1417 if (avctx->sample_fmt == sample_fmts[i])
698 1398 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 1398 times.
1398 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 1398 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 1398 times.
1398 if (ret < 0)
725 return ret;
726
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1318 times.
1398 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 1398 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 1398 times.
1398 if (ret < 0)
745 return ret;
746
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1302 times.
1398 if (ch_layouts) {
747
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 for (i = 0; i < num_ch_layouts; i++) {
748
2/2
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 92 times.
188 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
749 96 break;
750 }
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 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 1343 times.
✓ Branch 1 taken 55 times.
1398 if (!avctx->bits_per_raw_sample)
768 1343 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
769
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 1203 times.
1398 if (!avctx->bits_per_raw_sample)
770 195 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
771
772 1398 return 0;
773 }
774
775 21793 int ff_encode_preinit(AVCodecContext *avctx)
776 {
777 21793 AVCodecInternal *avci = avctx->internal;
778 21793 EncodeContext *ec = encode_ctx(avci);
779 21793 int ret = 0;
780
781
2/4
✓ Branch 0 taken 21793 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21793 times.
21793 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 21793 times.
21793 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 21696 times.
✓ Branch 1 taken 97 times.
21793 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21696 times.
21696 !(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 20353 times.
✓ Branch 1 taken 1398 times.
✓ Branch 2 taken 42 times.
21793 switch (avctx->codec_type) {
799 20353 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
800 1398 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
801 }
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21793 times.
21793 if (ret < 0)
803 return ret;
804
805
4/4
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 20353 times.
✓ Branch 2 taken 1398 times.
✓ Branch 3 taken 42 times.
21793 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
806
3/4
✓ Branch 0 taken 21727 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21727 times.
21751 && 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 21791 times.
✓ Branch 1 taken 2 times.
21793 if (!avctx->rc_initial_buffer_occupancy)
811 21791 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
812
813
2/2
✓ Branch 0 taken 21373 times.
✓ Branch 1 taken 420 times.
21793 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
814 21373 ec->intra_only_flag = AV_PKT_FLAG_KEY;
815
816
2/2
✓ Branch 1 taken 21751 times.
✓ Branch 2 taken 42 times.
21793 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
817 21751 avci->in_frame = av_frame_alloc();
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21751 times.
21751 if (!avci->in_frame)
819 return AVERROR(ENOMEM);
820 }
821
822
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21791 times.
21793 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 239723 times.
✓ Branch 1 taken 21793 times.
261516 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
835 239723 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
836 239723 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
837 const AVFrameSideData *sd_frame;
838 AVPacketSideData *sd_packet;
839
840 239723 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 239476 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 247 times.
239970 if (!sd_frame ||
844 247 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
845 type_packet))
846
847 239476 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 21793 ret = ff_frame_thread_encoder_init(avctx);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21793 times.
21793 if (ret < 0)
860 return ret;
861 #endif
862
863 21793 return 0;
864 }
865
866 12475 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 12475 frame->format = avctx->pix_fmt;
873
3/4
✓ Branch 0 taken 12459 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12459 times.
12475 if (frame->width <= 0 || frame->height <= 0) {
874 16 frame->width = avctx->width;
875 16 frame->height = avctx->height;
876 }
877
878 12475 ret = avcodec_default_get_buffer2(avctx, frame, 0);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12475 times.
12475 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 12475 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 21793 AVCodecInternal *ff_encode_internal_alloc(void)
912 {
913 21793 return av_mallocz(sizeof(EncodeContext));
914 }
915
916 216 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 216 times.
217 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 216 props = av_cpb_properties_alloc(&size);
928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (!props)
929 return NULL;
930
931 216 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 216 times.
216 if (!tmp) {
933 av_freep(&props);
934 return NULL;
935 }
936
937 216 avctx->coded_side_data = tmp;
938 216 avctx->nb_coded_side_data++;
939
940 216 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
941 216 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
942 216 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
943
944 216 return props;
945 }
946
947 12923 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 12923 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
954
1/2
✓ Branch 0 taken 12923 times.
✗ Branch 1 not taken.
12923 if (!side_data) {
955 12923 side_data_size = 4+4+8*error_count;
956 12923 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 12923 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12923 times.
12923 if (!side_data || side_data_size < 4+4+8*error_count)
961 return AVERROR(ENOMEM);
962
963 12923 AV_WL32(side_data, quality);
964 12923 side_data[4] = pict_type;
965 12923 side_data[5] = error_count;
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12923 times.
12923 for (int i = 0; i < error_count; ++i)
967 AV_WL64(side_data+8 + 8*i , error[i]);
968
969 12923 return 0;
970 }
971
972 1624 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
973 {
974 1624 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
975 1624 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 4872 times.
✓ Branch 1 taken 1624 times.
6496 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
978 4872 uint16_t *matrix = matrices[m];
979
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4872 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4872 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 1624 return 0;
989 }
990