FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-05-18 19:05:09
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 1068079 static EncodeContext *encode_ctx(AVCodecInternal *avci)
57 {
58 1068079 return (EncodeContext*)avci;
59 }
60
61 29807 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
62 {
63
2/4
✓ Branch 0 taken 29807 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29807 times.
29807 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 29807 times.
29807 av_assert0(!avpkt->data);
70
71 29807 av_fast_padded_malloc(&avctx->internal->byte_buffer,
72 29807 &avctx->internal->byte_buffer_size, size);
73 29807 avpkt->data = avctx->internal->byte_buffer;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29807 times.
29807 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 29807 avpkt->size = size;
79
80 29807 return 0;
81 }
82
83 519383 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
84 {
85 int ret;
86
87
2/4
✓ Branch 0 taken 519383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519383 times.
519383 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88 return AVERROR(EINVAL);
89
90
2/4
✓ Branch 0 taken 519383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519383 times.
519383 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 519383 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519383 times.
519383 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 519383 avpkt->data = avpkt->buf->data;
101
102 519383 return 0;
103 }
104
105 519383 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 519383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519383 times.
519383 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
110 return AVERROR(EINVAL);
111
112
2/4
✓ Branch 0 taken 519383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519383 times.
519383 av_assert0(!avpkt->data && !avpkt->buf);
113
114 519383 avpkt->size = size;
115 519383 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519383 times.
519383 if (ret < 0)
117 goto fail;
118
119
2/4
✓ Branch 0 taken 519383 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519383 times.
519383 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 519383 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
125
126 519383 ret = 0;
127 519383 fail:
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519383 times.
519383 if (ret < 0) {
129 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
130 av_packet_unref(avpkt);
131 }
132
133 519383 return ret;
134 }
135
136 520057 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
137 {
138 520057 uint8_t *data = avpkt->data;
139 int ret;
140
141
2/2
✓ Branch 0 taken 490250 times.
✓ Branch 1 taken 29807 times.
520057 if (avpkt->buf)
142 490250 return 0;
143
144 29807 avpkt->data = NULL;
145 29807 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29807 times.
29807 if (ret < 0)
147 return ret;
148 29807 memcpy(avpkt->data, data, avpkt->size);
149
150 29807 return 0;
151 }
152
153 /**
154 * Pad last frame with silence.
155 */
156 114 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 114 frame->format = src->format;
163 114 frame->nb_samples = out_samples;
164 114 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (ret < 0)
166 goto fail;
167 114 ret = av_frame_get_buffer(frame, 0);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (ret < 0)
169 goto fail;
170
171 114 ret = av_frame_copy_props(frame, src);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (ret < 0)
173 goto fail;
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
176 114 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 114 times.
114 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
180 114 frame->nb_samples - src->nb_samples,
181 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
182 goto fail;
183
184 114 discard_padding = frame->nb_samples - src->nb_samples;
185 av_assert1(discard_padding > 0);
186 114 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (!sd) {
188 ret = AVERROR(ENOMEM);
189 goto fail;
190 }
191 114 AV_WL32A(sd->data, 0);
192 114 AV_WL32A(sd->data + 4, discard_padding);
193 114 AV_WL16A(sd->data + 8, 0);
194
195 114 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 982 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 982 times.
982 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 982 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
213 982 avctx->frame_num++;
214 982 return ret;
215 }
216
217 1061849 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
218 {
219 1061849 AVCodecInternal *avci = avctx->internal;
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1061849 times.
1061849 if (avci->draining)
222 return AVERROR_EOF;
223
224
2/2
✓ Branch 0 taken 535657 times.
✓ Branch 1 taken 526192 times.
1061849 if (!avci->buffer_frame->buf[0])
225 535657 return AVERROR(EAGAIN);
226
227 526192 av_frame_move_ref(frame, avci->buffer_frame);
228
229 526192 return 0;
230 }
231
232 515401 int ff_encode_reordered_opaque(AVCodecContext *avctx,
233 AVPacket *pkt, const AVFrame *frame)
234 {
235
2/2
✓ Branch 0 taken 511856 times.
✓ Branch 1 taken 3545 times.
515401 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
236 511856 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 511856 times.
511856 if (ret < 0)
238 return ret;
239 511856 pkt->opaque = frame->opaque;
240 }
241
242 515401 return 0;
243 }
244
245 526692 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
246 AVFrame *frame, int *got_packet)
247 {
248 526692 const FFCodec *const codec = ffcodec(avctx->codec);
249 int ret;
250
251 526692 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
252 ff_assert1_fpu();
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526692 times.
526692 av_assert0(ret <= 0);
254
255
3/4
✓ Branch 0 taken 526692 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6561 times.
✓ Branch 3 taken 520131 times.
526692 if (!ret && *got_packet) {
256
2/2
✓ Branch 0 taken 520057 times.
✓ Branch 1 taken 74 times.
520131 if (avpkt->data) {
257 520057 ret = encode_make_refcounted(avctx, avpkt);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520057 times.
520057 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 520057 times.
520057 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 28076 times.
✓ Branch 1 taken 492055 times.
✓ Branch 2 taken 27845 times.
✓ Branch 3 taken 231 times.
520131 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
267
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 12227 times.
27845 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
268
2/2
✓ Branch 0 taken 492420 times.
✓ Branch 1 taken 15253 times.
507673 if (avpkt->pts == AV_NOPTS_VALUE)
269 492420 avpkt->pts = frame->pts;
270
271
2/2
✓ Branch 0 taken 503288 times.
✓ Branch 1 taken 4385 times.
507673 if (!avpkt->duration) {
272
2/2
✓ Branch 0 taken 482630 times.
✓ Branch 1 taken 20658 times.
503288 if (frame->duration)
273 482630 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 371692 times.
✓ Branch 1 taken 131596 times.
503288 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
279 371692 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
280
3/4
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 371540 times.
✓ Branch 2 taken 152 times.
✗ Branch 3 not taken.
371692 if (sd && sd->size >= 10) {
281 152 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 152 times.
152 if (!skip_samples) {
283 ret = AVERROR(ENOMEM);
284 goto unref;
285 }
286 152 AV_WL32A(skip_samples + 0, AV_RL32(sd->data + 0));
287 152 AV_WL32A(skip_samples + 4, AV_RL32(sd->data + 4));
288 152 AV_WB8 (skip_samples + 8, AV_RB8 (sd->data + 8));
289 152 AV_WB8 (skip_samples + 9, AV_RB8 (sd->data + 9));
290 }
291 }
292 }
293
294 507673 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507673 times.
507673 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 511664 times.
520131 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 512429 avpkt->dts = avpkt->pts;
305 } else {
306 6561 unref:
307 6561 av_packet_unref(avpkt);
308 }
309
310
2/2
✓ Branch 0 taken 526192 times.
✓ Branch 1 taken 500 times.
526692 if (frame)
311 526192 av_frame_unref(frame);
312
313 526692 return ret;
314 }
315
316 1075518 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
317 {
318 1075518 AVCodecInternal *avci = avctx->internal;
319 1075518 AVFrame *frame = avci->in_frame;
320 1075518 const FFCodec *const codec = ffcodec(avctx->codec);
321 int got_packet;
322 int ret;
323
324
2/2
✓ Branch 0 taken 1951 times.
✓ Branch 1 taken 1073567 times.
1075518 if (avci->draining_done)
325 1951 return AVERROR_EOF;
326
327
3/4
✓ Branch 0 taken 1073567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1061849 times.
✓ Branch 3 taken 11718 times.
1073567 if (!frame->buf[0] && !avci->draining) {
328 1061849 av_frame_unref(frame);
329 1061849 ret = ff_encode_get_frame(avctx, frame);
330
3/4
✓ Branch 0 taken 535657 times.
✓ Branch 1 taken 526192 times.
✓ Branch 2 taken 535657 times.
✗ Branch 3 not taken.
1061849 if (ret < 0 && ret != AVERROR_EOF)
331 535657 return ret;
332 }
333
334
2/2
✓ Branch 0 taken 11718 times.
✓ Branch 1 taken 526192 times.
537910 if (!frame->buf[0]) {
335
2/2
✓ Branch 0 taken 11218 times.
✓ Branch 1 taken 500 times.
11718 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
336
2/2
✓ Branch 0 taken 6301 times.
✓ Branch 1 taken 4917 times.
11218 avci->frame_thread_encoder))
337 6301 return AVERROR_EOF;
338
339 // Flushing is signaled with a NULL frame
340 5417 frame = NULL;
341 }
342
343 531609 got_packet = 0;
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 531609 times.
531609 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
346
347 #if CONFIG_FRAME_THREAD_ENCODER
348
2/2
✓ Branch 0 taken 63302 times.
✓ Branch 1 taken 468307 times.
531609 if (avci->frame_thread_encoder)
349 /* This will unref frame. */
350 63302 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
351 else
352 #endif
353 468307 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
354
355
4/4
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 526192 times.
✓ Branch 2 taken 1951 times.
✓ Branch 3 taken 3466 times.
531609 if (avci->draining && !got_packet)
356 1951 avci->draining_done = 1;
357
358 531609 return ret;
359 }
360
361 1064040 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
362 {
363 int ret;
364
365
4/4
✓ Branch 0 taken 1075592 times.
✓ Branch 1 taken 520057 times.
✓ Branch 2 taken 1075518 times.
✓ Branch 3 taken 74 times.
1595649 while (!avpkt->data && !avpkt->side_data) {
366 1075518 ret = encode_simple_internal(avctx, avpkt);
367
2/2
✓ Branch 0 taken 543909 times.
✓ Branch 1 taken 531609 times.
1075518 if (ret < 0)
368 543909 return ret;
369 }
370
371 520131 return 0;
372 }
373
374 1070450 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
375 {
376 1070450 AVCodecInternal *avci = avctx->internal;
377 int ret;
378
379
2/2
✓ Branch 0 taken 6410 times.
✓ Branch 1 taken 1064040 times.
1070450 if (avci->draining_done)
380 6410 return AVERROR_EOF;
381
382
2/4
✓ Branch 0 taken 1064040 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1064040 times.
1064040 av_assert0(!avpkt->data && !avpkt->side_data);
383
384
2/2
✓ Branch 0 taken 297190 times.
✓ Branch 1 taken 766850 times.
1064040 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
385
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 296382 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
297190 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 1064040 times.
1064040 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 1064040 ret = encode_simple_receive_packet(avctx, avpkt);
399
2/2
✓ Branch 0 taken 520131 times.
✓ Branch 1 taken 543909 times.
1064040 if (ret >= 0)
400 520131 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
401
402
2/2
✓ Branch 0 taken 8252 times.
✓ Branch 1 taken 1055788 times.
1064040 if (ret == AVERROR_EOF)
403 8252 avci->draining_done = 1;
404
405 1064040 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 143503 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
450 {
451 143503 return 0;
452 }
453 #endif
454
455 526192 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
456 {
457 526192 AVCodecInternal *avci = avctx->internal;
458 526192 EncodeContext *ec = encode_ctx(avci);
459 526192 AVFrame *dst = avci->buffer_frame;
460 int ret;
461
462
2/2
✓ Branch 0 taken 382689 times.
✓ Branch 1 taken 143503 times.
526192 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
463 /* extract audio service type metadata */
464 382689 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
465
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 382689 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
382689 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 107469 times.
✓ Branch 1 taken 275220 times.
382689 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 107469 times.
107469 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 107469 times.
107469 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 177 times.
✓ Branch 1 taken 107292 times.
107469 if (src->nb_samples < avctx->frame_size) {
480 177 ec->last_audio_frame = 1;
481
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 57 times.
177 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
482
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 63 times.
120 (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
483
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 111 times.
114 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
484 114 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
485
486
1/2
✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
114 if (out_samples != src->nb_samples) {
487 114 ret = pad_last_frame(avctx, dst, src, out_samples);
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
114 if (ret < 0)
489 return ret;
490 114 goto finish;
491 }
492 }
493 }
494 }
495 }
496
497 526078 ret = av_frame_ref(dst, src);
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526078 times.
526078 if (ret < 0)
499 return ret;
500
501 526078 finish:
502
503
2/2
✓ Branch 0 taken 143503 times.
✓ Branch 1 taken 382689 times.
526192 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
504 143503 ret = encode_generate_icc_profile(avctx, dst);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143503 times.
143503 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 522926 times.
526192 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
514 3266 dst->duration = 0;
515
516 526192 return 0;
517 }
518
519 534444 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
520 {
521 534444 AVCodecInternal *avci = avctx->internal;
522 int ret;
523
524
2/4
✓ Branch 1 taken 534444 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 534444 times.
534444 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 534444 times.
534444 if (avci->draining)
528 return AVERROR_EOF;
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534444 times.
534444 if (avci->buffer_frame->buf[0])
531 return AVERROR(EAGAIN);
532
533
2/2
✓ Branch 0 taken 8252 times.
✓ Branch 1 taken 526192 times.
534444 if (!frame) {
534 8252 avci->draining = 1;
535 } else {
536 526192 ret = encode_send_frame_internal(avctx, frame);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526192 times.
526192 if (ret < 0)
538 return ret;
539 }
540
541
2/4
✓ Branch 0 taken 534444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 534444 times.
✗ Branch 3 not taken.
534444 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
542 534444 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
543
5/6
✓ Branch 0 taken 15937 times.
✓ Branch 1 taken 518507 times.
✓ Branch 2 taken 6410 times.
✓ Branch 3 taken 9527 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6410 times.
534444 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
544 return ret;
545 }
546
547 534444 avctx->frame_num++;
548
549 534444 return 0;
550 }
551
552 1054513 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
553 {
554 1054513 AVCodecInternal *avci = avctx->internal;
555 int ret;
556
557 1054513 av_packet_unref(avpkt);
558
559
2/4
✓ Branch 1 taken 1054513 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1054513 times.
1054513 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
560 return AVERROR(EINVAL);
561
562
4/4
✓ Branch 0 taken 536080 times.
✓ Branch 1 taken 518433 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 536006 times.
1054513 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
563 518507 av_packet_move_ref(avpkt, avci->buffer_pkt);
564 } else {
565 536006 ret = encode_receive_packet_internal(avctx, avpkt);
566
2/2
✓ Branch 0 taken 534382 times.
✓ Branch 1 taken 1624 times.
536006 if (ret < 0)
567 534382 return ret;
568 }
569
570 520131 return 0;
571 }
572
573 20325 static int encode_preinit_video(AVCodecContext *avctx)
574 {
575 20325 const AVCodec *c = avctx->codec;
576 20325 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 20325 times.
20325 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 20325 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 20325 times.
20325 if (ret < 0)
589 return ret;
590
591
2/2
✓ Branch 0 taken 830 times.
✓ Branch 1 taken 19495 times.
20325 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 2474 times.
✓ Branch 1 taken 17851 times.
20325 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
617 const enum AVAlphaMode *alpha_modes;
618 int num_alpha_modes;
619 2474 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 2474 times.
2474 if (ret < 0)
622 return ret;
623
624
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) {
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 20325 times.
✗ Branch 1 not taken.
20325 if ( avctx->bits_per_raw_sample < 0
644
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
20325 || (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 20325 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20325 times.
20325 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 20325 times.
20325 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 20325 return 0;
674 }
675
676 1389 static int encode_preinit_audio(AVCodecContext *avctx)
677 {
678 1389 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 1389 times.
1389 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 1389 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 1389 times.
1389 if (ret < 0)
694 return ret;
695
1/2
✓ Branch 0 taken 1389 times.
✗ Branch 1 not taken.
1389 if (sample_fmts) {
696
1/2
✓ Branch 0 taken 1407 times.
✗ Branch 1 not taken.
1407 for (i = 0; i < num_sample_fmts; i++) {
697
2/2
✓ Branch 0 taken 1389 times.
✓ Branch 1 taken 18 times.
1407 if (avctx->sample_fmt == sample_fmts[i])
698 1389 break;
699
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
22 if (avctx->ch_layout.nb_channels == 1 &&
700 4 av_get_planar_sample_fmt(avctx->sample_fmt) ==
701 4 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 1389 times.
1389 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 1389 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 1389 times.
1389 if (ret < 0)
725 return ret;
726
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1311 times.
1389 if (supported_samplerates) {
727
1/2
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
170 for (i = 0; i < num_samplerates; i++)
728
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 92 times.
170 if (avctx->sample_rate == supported_samplerates[i])
729 78 break;
730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 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 1389 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 1389 times.
1389 if (ret < 0)
745 return ret;
746
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 1296 times.
1389 if (ch_layouts) {
747
1/2
✓ Branch 0 taken 184 times.
✗ Branch 1 not taken.
184 for (i = 0; i < num_ch_layouts; i++) {
748
2/2
✓ Branch 1 taken 93 times.
✓ Branch 2 taken 91 times.
184 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
749 93 break;
750 }
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 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 1334 times.
✓ Branch 1 taken 55 times.
1389 if (!avctx->bits_per_raw_sample)
768 1334 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
769
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 1200 times.
1389 if (!avctx->bits_per_raw_sample)
770 189 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
771
772 1389 return 0;
773 }
774
775 21756 int ff_encode_preinit(AVCodecContext *avctx)
776 {
777 21756 AVCodecInternal *avci = avctx->internal;
778 21756 EncodeContext *ec = encode_ctx(avci);
779 21756 int ret = 0;
780
781
2/4
✓ Branch 0 taken 21756 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21756 times.
21756 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 21756 times.
21756 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 21661 times.
✓ Branch 1 taken 95 times.
21756 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21661 times.
21661 !(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 20325 times.
✓ Branch 1 taken 1389 times.
✓ Branch 2 taken 42 times.
21756 switch (avctx->codec_type) {
799 20325 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
800 1389 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
801 }
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21756 times.
21756 if (ret < 0)
803 return ret;
804
805
4/4
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 20325 times.
✓ Branch 2 taken 1389 times.
✓ Branch 3 taken 42 times.
21756 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
806
3/4
✓ Branch 0 taken 21692 times.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21692 times.
21714 && 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 21754 times.
✓ Branch 1 taken 2 times.
21756 if (!avctx->rc_initial_buffer_occupancy)
811 21754 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
812
813
2/2
✓ Branch 0 taken 21338 times.
✓ Branch 1 taken 418 times.
21756 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
814 21338 ec->intra_only_flag = AV_PKT_FLAG_KEY;
815
816
2/2
✓ Branch 1 taken 21714 times.
✓ Branch 2 taken 42 times.
21756 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
817 21714 avci->in_frame = av_frame_alloc();
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21714 times.
21714 if (!avci->in_frame)
819 return AVERROR(ENOMEM);
820 }
821
822
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21754 times.
21756 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 239316 times.
✓ Branch 1 taken 21756 times.
261072 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
835 239316 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
836 239316 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
837 const AVFrameSideData *sd_frame;
838 AVPacketSideData *sd_packet;
839
840 239316 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 239069 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 247 times.
239563 if (!sd_frame ||
844 247 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
845 type_packet))
846
847 239069 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 21756 ret = ff_frame_thread_encoder_init(avctx);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21756 times.
21756 if (ret < 0)
860 return ret;
861 #endif
862
863 21756 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 21756 AVCodecInternal *ff_encode_internal_alloc(void)
912 {
913 21756 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