FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 380 586 64.8%
Functions: 27 30 90.0%
Branches: 286 487 58.7%

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/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/samplefmt.h"
31
32 #include "avcodec.h"
33 #include "avcodec_internal.h"
34 #include "codec_desc.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37 #include "frame_thread_encoder.h"
38 #include "internal.h"
39
40 typedef struct EncodeContext {
41 AVCodecInternal avci;
42
43 /**
44 * This is set to AV_PKT_FLAG_KEY for encoders that encode intra-only
45 * formats (i.e. whose codec descriptor has AV_CODEC_PROP_INTRA_ONLY set).
46 * This is used to set said flag generically for said encoders.
47 */
48 int intra_only_flag;
49
50 /**
51 * An audio frame with less than required samples has been submitted (and
52 * potentially padded with silence). Reject all subsequent frames.
53 */
54 int last_audio_frame;
55 } EncodeContext;
56
57 1599489 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 1599489 return (EncodeContext*)avci;
60 }
61
62 30045 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 30045 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30045 times.
30045 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
65 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
66 size, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
67 return AVERROR(EINVAL);
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30045 times.
30045 av_assert0(!avpkt->data);
71
72 30045 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 30045 &avctx->internal->byte_buffer_size, size);
74 30045 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30045 times.
30045 if (!avpkt->data) {
76 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
77 return AVERROR(ENOMEM);
78 }
79 30045 avpkt->size = size;
80
81 30045 return 0;
82 }
83
84 527734 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 527734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 527734 times.
527734 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 527734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 527734 times.
527734 if (avpkt->data || avpkt->buf) {
92 av_log(avctx, AV_LOG_ERROR, "avpkt->{data,buf} != NULL in avcodec_default_get_encode_buffer()\n");
93 return AVERROR(EINVAL);
94 }
95
96 527734 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527734 times.
527734 if (ret < 0) {
98 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", avpkt->size);
99 return ret;
100 }
101 527734 avpkt->data = avpkt->buf->data;
102
103 527734 return 0;
104 }
105
106 527734 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
107 {
108 int ret;
109
110
2/4
✓ Branch 0 taken 527734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 527734 times.
527734 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 527734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 527734 times.
527734 av_assert0(!avpkt->data && !avpkt->buf);
114
115 527734 avpkt->size = size;
116 527734 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527734 times.
527734 if (ret < 0)
118 goto fail;
119
120
2/4
✓ Branch 0 taken 527734 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 527734 times.
527734 if (!avpkt->data || !avpkt->buf) {
121 av_log(avctx, AV_LOG_ERROR, "No buffer returned by get_encode_buffer()\n");
122 ret = AVERROR(EINVAL);
123 goto fail;
124 }
125 527734 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 527734 ret = 0;
128 527734 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 527734 times.
527734 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 527734 return ret;
135 }
136
137 528408 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 528408 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 498363 times.
✓ Branch 1 taken 30045 times.
528408 if (avpkt->buf)
143 498363 return 0;
144
145 30045 avpkt->data = NULL;
146 30045 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30045 times.
30045 if (ret < 0)
148 return ret;
149 30045 memcpy(avpkt->data, data, avpkt->size);
150
151 30045 return 0;
152 }
153
154 /**
155 * Pad last frame with silence.
156 */
157 110 static int pad_last_frame(AVCodecContext *s, AVFrame *frame, const AVFrame *src, int out_samples)
158 {
159 AVFrameSideData *sd;
160 int discard_padding;
161 int ret;
162
163 110 frame->format = src->format;
164 110 frame->nb_samples = out_samples;
165 110 ret = av_channel_layout_copy(&frame->ch_layout, &s->ch_layout);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
167 goto fail;
168 110 ret = av_frame_get_buffer(frame, 0);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
170 goto fail;
171
172 110 ret = av_frame_copy_props(frame, src);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
174 goto fail;
175
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
177 110 src->nb_samples, s->ch_layout.nb_channels,
178 s->sample_fmt)) < 0)
179 goto fail;
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
181 110 frame->nb_samples - src->nb_samples,
182 s->ch_layout.nb_channels, s->sample_fmt)) < 0)
183 goto fail;
184
185 110 discard_padding = frame->nb_samples - src->nb_samples;
186 av_assert1(discard_padding > 0);
187 110 sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (!sd) {
189 ret = AVERROR(ENOMEM);
190 goto fail;
191 }
192 110 AV_WL32A(sd->data, 0);
193 110 AV_WL32A(sd->data + 4, discard_padding);
194 110 AV_WL16A(sd->data + 8, 0);
195
196 110 return 0;
197
198 fail:
199 av_frame_unref(frame);
200 encode_ctx(s->internal)->last_audio_frame = 0;
201 return ret;
202 }
203
204 980 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
205 const AVSubtitle *sub)
206 {
207 int ret;
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980 times.
980 if (sub->start_display_time) {
209 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
210 return -1;
211 }
212
213 980 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
214 980 avctx->frame_num++;
215 980 return ret;
216 }
217
218 1078491 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
219 {
220 1078491 AVCodecInternal *avci = avctx->internal;
221
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1078491 times.
1078491 if (avci->draining)
223 return AVERROR_EOF;
224
225
2/2
✓ Branch 0 taken 543958 times.
✓ Branch 1 taken 534533 times.
1078491 if (!avci->buffer_frame->buf[0])
226 543958 return AVERROR(EAGAIN);
227
228 534533 av_frame_move_ref(frame, avci->buffer_frame);
229
230 534533 return 0;
231 }
232
233 514504 static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
234 {
235 514504 AVCodecInternal *avci = avctx->internal;
236 514504 EncodeContext *ec = encode_ctx(avci);
237
238
2/2
✓ Branch 0 taken 500228 times.
✓ Branch 1 taken 14276 times.
514504 if (avpkt->pts == AV_NOPTS_VALUE) {
239 500228 avpkt->pts = frame->pts;
240
4/4
✓ Branch 0 taken 368432 times.
✓ Branch 1 taken 131796 times.
✓ Branch 2 taken 365232 times.
✓ Branch 3 taken 3200 times.
500228 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
241 365232 avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
242 }
243
244
2/2
✓ Branch 0 taken 510119 times.
✓ Branch 1 taken 4385 times.
514504 if (!avpkt->duration) {
245
2/2
✓ Branch 0 taken 489465 times.
✓ Branch 1 taken 20654 times.
510119 if (frame->duration)
246 489465 avpkt->duration = frame->duration;
247
2/2
✓ Branch 0 taken 3200 times.
✓ Branch 1 taken 17454 times.
20654 else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
248 3200 avpkt->duration = ff_samples_to_time_base(avctx,
249 3200 frame->nb_samples);
250 }
251
2/2
✓ Branch 0 taken 378319 times.
✓ Branch 1 taken 131800 times.
510119 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
252 378319 AVFrameSideData *frame_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
253
254
3/4
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 378171 times.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
378319 if (frame_sd && frame_sd->size >= 10) {
255 148 int skip_samples = AV_RL32(frame_sd->data + 0);
256 148 int discard_padding = AV_RL32(frame_sd->data + 4);
257
258
4/6
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
148 if (discard_padding > 0 && avctx->frame_size && ec->last_audio_frame) {
259 106 avpkt->duration = av_sat_add64(avpkt->duration, ff_samples_to_time_base(avctx, avctx->initial_padding));
260
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 89 times.
106 avpkt->duration = FFMIN(avpkt->duration, ff_samples_to_time_base(avctx, avctx->frame_size));
261 106 discard_padding = avctx->frame_size - ff_samples_from_time_base(avctx, avpkt->duration);
262 }
263
264
4/4
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 89 times.
✓ Branch 3 taken 17 times.
148 if (skip_samples > 0 || discard_padding > 0) {
265 131 uint8_t *packet_sd = av_packet_new_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (!packet_sd)
267 return AVERROR(ENOMEM);
268 131 AV_WL32A(packet_sd + 0, skip_samples);
269 131 AV_WL32A(packet_sd + 4, discard_padding);
270 131 AV_WL8 (packet_sd + 8, AV_RB8(frame_sd->data + 8));
271 131 AV_WL8 (packet_sd + 9, AV_RB8(frame_sd->data + 9));
272 }
273 }
274 }
275 }
276
277 514504 return 0;
278 }
279
280 522402 int ff_encode_reordered_opaque(AVCodecContext *avctx,
281 AVPacket *pkt, const AVFrame *frame)
282 {
283
2/2
✓ Branch 0 taken 518849 times.
✓ Branch 1 taken 3553 times.
522402 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
284 518849 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518849 times.
518849 if (ret < 0)
286 return ret;
287 518849 pkt->opaque = frame->opaque;
288 }
289
290 522402 return 0;
291 }
292
293 535107 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
294 AVFrame *frame, int *got_packet)
295 {
296 535107 const FFCodec *const codec = ffcodec(avctx->codec);
297 int ret;
298
299 535107 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
300 ff_assert1_fpu();
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 535107 times.
535107 av_assert0(ret <= 0);
302
303
3/4
✓ Branch 0 taken 535107 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6622 times.
✓ Branch 3 taken 528485 times.
535107 if (!ret && *got_packet) {
304
2/2
✓ Branch 0 taken 528408 times.
✓ Branch 1 taken 77 times.
528485 if (avpkt->data) {
305 528408 ret = encode_make_refcounted(avctx, avpkt);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528408 times.
528408 if (ret < 0)
307 goto unref;
308 // Date returned by encoders must always be ref-counted
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528408 times.
528408 av_assert0(avpkt->buf);
310 }
311
312 // set the timestamps for the simple no-delay case
313 // encoders with delay have to set the timestamps themselves
314
4/4
✓ Branch 0 taken 29609 times.
✓ Branch 1 taken 498876 times.
✓ Branch 2 taken 29341 times.
✓ Branch 3 taken 268 times.
528485 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
315
2/2
✓ Branch 0 taken 15628 times.
✓ Branch 1 taken 13713 times.
29341 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
316 514504 ret = encode_set_packet_props(avctx, avpkt, frame);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514504 times.
514504 if (ret < 0)
318 goto unref;
319
320 514504 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514504 times.
514504 if (ret < 0)
322 goto unref;
323 }
324
325 // dts equals pts unless there is reordering
326 // there can be no reordering if there is no encoder delay
327
2/2
✓ Branch 0 taken 8637 times.
✓ Branch 1 taken 519848 times.
528485 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
328
2/2
✓ Branch 0 taken 7872 times.
✓ Branch 1 taken 765 times.
8637 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7872 times.
7872 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
330 520613 avpkt->dts = avpkt->pts;
331 } else {
332 6622 unref:
333 6622 av_packet_unref(avpkt);
334 }
335
336
2/2
✓ Branch 0 taken 534533 times.
✓ Branch 1 taken 574 times.
535107 if (frame)
337 534533 av_frame_unref(frame);
338
339 535107 return ret;
340 }
341
342 1092273 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
343 {
344 1092273 AVCodecInternal *avci = avctx->internal;
345 1092273 AVFrame *frame = avci->in_frame;
346 1092273 const FFCodec *const codec = ffcodec(avctx->codec);
347 int got_packet;
348 int ret;
349
350
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 1090269 times.
1092273 if (avci->draining_done)
351 2004 return AVERROR_EOF;
352
353
3/4
✓ Branch 0 taken 1090269 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1078491 times.
✓ Branch 3 taken 11778 times.
1090269 if (!frame->buf[0] && !avci->draining) {
354 1078491 av_frame_unref(frame);
355 1078491 ret = ff_encode_get_frame(avctx, frame);
356
3/4
✓ Branch 0 taken 543958 times.
✓ Branch 1 taken 534533 times.
✓ Branch 2 taken 543958 times.
✗ Branch 3 not taken.
1078491 if (ret < 0 && ret != AVERROR_EOF)
357 543958 return ret;
358 }
359
360
2/2
✓ Branch 0 taken 11778 times.
✓ Branch 1 taken 534533 times.
546311 if (!frame->buf[0]) {
361
2/2
✓ Branch 0 taken 11204 times.
✓ Branch 1 taken 574 times.
11778 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
362
2/2
✓ Branch 0 taken 6327 times.
✓ Branch 1 taken 4877 times.
11204 avci->frame_thread_encoder))
363 6327 return AVERROR_EOF;
364
365 // Flushing is signaled with a NULL frame
366 5451 frame = NULL;
367 }
368
369 539984 got_packet = 0;
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539984 times.
539984 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
372
373 #if CONFIG_FRAME_THREAD_ENCODER
374
2/2
✓ Branch 0 taken 63500 times.
✓ Branch 1 taken 476484 times.
539984 if (avci->frame_thread_encoder)
375 /* This will unref frame. */
376 63500 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
377 else
378 #endif
379 476484 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
380
381
4/4
✓ Branch 0 taken 5451 times.
✓ Branch 1 taken 534533 times.
✓ Branch 2 taken 2004 times.
✓ Branch 3 taken 3447 times.
539984 if (avci->draining && !got_packet)
382 2004 avci->draining_done = 1;
383
384 539984 return ret;
385 }
386
387 1080774 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
388 {
389 int ret;
390
391
4/4
✓ Branch 0 taken 1092350 times.
✓ Branch 1 taken 528408 times.
✓ Branch 2 taken 1092273 times.
✓ Branch 3 taken 77 times.
1620758 while (!avpkt->data && !avpkt->side_data) {
392 1092273 ret = encode_simple_internal(avctx, avpkt);
393
2/2
✓ Branch 0 taken 552289 times.
✓ Branch 1 taken 539984 times.
1092273 if (ret < 0)
394 552289 return ret;
395 }
396
397 528485 return 0;
398 }
399
400 1087223 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
401 {
402 1087223 AVCodecInternal *avci = avctx->internal;
403 int ret;
404
405
2/2
✓ Branch 0 taken 6449 times.
✓ Branch 1 taken 1080774 times.
1087223 if (avci->draining_done)
406 6449 return AVERROR_EOF;
407
408
2/4
✓ Branch 0 taken 1080774 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1080774 times.
1080774 av_assert0(!avpkt->data && !avpkt->side_data);
409
410
2/2
✓ Branch 0 taken 297923 times.
✓ Branch 1 taken 782851 times.
1080774 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
411
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 297115 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
297923 if ((avctx->flags & AV_CODEC_FLAG_PASS1) && avctx->stats_out)
412 808 avctx->stats_out[0] = '\0';
413 }
414
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1080774 times.
1080774 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_RECEIVE_PACKET) {
416 ret = ffcodec(avctx->codec)->cb.receive_packet(avctx, avpkt);
417 if (ret < 0)
418 av_packet_unref(avpkt);
419 else
420 // Encoders must always return ref-counted buffers.
421 // Side-data only packets have no data and can be not ref-counted.
422 av_assert0(!avpkt->data || avpkt->buf);
423 } else
424 1080774 ret = encode_simple_receive_packet(avctx, avpkt);
425
2/2
✓ Branch 0 taken 528485 times.
✓ Branch 1 taken 552289 times.
1080774 if (ret >= 0)
426 528485 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
427
428
2/2
✓ Branch 0 taken 8331 times.
✓ Branch 1 taken 1072443 times.
1080774 if (ret == AVERROR_EOF)
429 8331 avci->draining_done = 1;
430
431 1080774 return ret;
432 }
433
434 #if CONFIG_LCMS2
435 static int encode_generate_icc_profile(AVCodecContext *avctx, AVFrame *frame)
436 {
437 enum AVColorTransferCharacteristic trc = frame->color_trc;
438 enum AVColorPrimaries prim = frame->color_primaries;
439 const FFCodec *const codec = ffcodec(avctx->codec);
440 AVCodecInternal *avci = avctx->internal;
441 cmsHPROFILE profile;
442 int ret;
443
444 /* don't generate ICC profiles if disabled or unsupported */
445 if (!(avctx->flags2 & AV_CODEC_FLAG2_ICC_PROFILES))
446 return 0;
447 if (!(codec->caps_internal & FF_CODEC_CAP_ICC_PROFILES))
448 return 0;
449
450 if (trc == AVCOL_TRC_UNSPECIFIED)
451 trc = avctx->color_trc;
452 if (prim == AVCOL_PRI_UNSPECIFIED)
453 prim = avctx->color_primaries;
454 if (trc == AVCOL_TRC_UNSPECIFIED || prim == AVCOL_PRI_UNSPECIFIED)
455 return 0; /* can't generate ICC profile with missing csp tags */
456
457 if (av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE))
458 return 0; /* don't overwrite existing ICC profile */
459
460 if (!avci->icc.avctx) {
461 ret = ff_icc_context_init(&avci->icc, avctx);
462 if (ret < 0)
463 return ret;
464 }
465
466 ret = ff_icc_profile_generate(&avci->icc, prim, trc, &profile);
467 if (ret < 0)
468 return ret;
469
470 ret = ff_icc_profile_attach(&avci->icc, profile, frame);
471 cmsCloseProfile(profile);
472 return ret;
473 }
474 #else /* !CONFIG_LCMS2 */
475 143877 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
476 {
477 143877 return 0;
478 }
479 #endif
480
481 534533 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
482 {
483 534533 AVCodecInternal *avci = avctx->internal;
484 534533 EncodeContext *ec = encode_ctx(avci);
485 534533 AVFrame *dst = avci->buffer_frame;
486 int ret;
487
488
2/2
✓ Branch 0 taken 390656 times.
✓ Branch 1 taken 143877 times.
534533 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
489 /* extract audio service type metadata */
490 390656 AVFrameSideData *sd = av_frame_get_side_data(src, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
491
3/4
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 390110 times.
✓ Branch 2 taken 546 times.
✗ Branch 3 not taken.
390656 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
492 546 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
493
494 /* check for valid frame size */
495
2/2
✓ Branch 0 taken 108515 times.
✓ Branch 1 taken 282141 times.
390656 if (avctx->frame_size) {
496 /* if we already got an undersized frame, that must have been the last */
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108515 times.
108515 if (ec->last_audio_frame) {
498 av_log(avctx, AV_LOG_ERROR, "frame_size (%d) was not respected for a non-last frame\n", avctx->frame_size);
499 return AVERROR(EINVAL);
500 }
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108515 times.
108515 if (src->nb_samples > avctx->frame_size) {
502 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) > frame_size (%d)\n", src->nb_samples, avctx->frame_size);
503 return AVERROR(EINVAL);
504 }
505
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 108326 times.
108515 if (src->nb_samples < avctx->frame_size) {
506 189 ec->last_audio_frame = 1;
507
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 52 times.
189 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
508
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 79 times.
137 (avctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)) {
509
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 107 times.
110 int pad_samples = avci->pad_samples ? avci->pad_samples : avctx->frame_size;
510 110 int out_samples = (src->nb_samples + pad_samples - 1) / pad_samples * pad_samples;
511
512
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 if (out_samples != src->nb_samples) {
513 110 ret = pad_last_frame(avctx, dst, src, out_samples);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0)
515 return ret;
516 110 goto finish;
517 }
518 }
519 }
520 }
521 }
522
523 534423 ret = av_frame_ref(dst, src);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534423 times.
534423 if (ret < 0)
525 return ret;
526
527 534423 finish:
528
529
2/2
✓ Branch 0 taken 143877 times.
✓ Branch 1 taken 390656 times.
534533 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
530 143877 ret = encode_generate_icc_profile(avctx, dst);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143877 times.
143877 if (ret < 0)
532 return ret;
533 }
534
535 // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
536 // since otherwise we cannot be sure that whatever value it has is in the
537 // right timebase, so we would produce an incorrect value, which is worse
538 // than none at all
539
2/2
✓ Branch 0 taken 3274 times.
✓ Branch 1 taken 531259 times.
534533 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
540 3274 dst->duration = 0;
541
542 534533 return 0;
543 }
544
545 542864 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
546 {
547 542864 AVCodecInternal *avci = avctx->internal;
548 int ret;
549
550
2/4
✓ Branch 1 taken 542864 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 542864 times.
542864 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
551 return AVERROR(EINVAL);
552
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542864 times.
542864 if (avci->draining)
554 return AVERROR_EOF;
555
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542864 times.
542864 if (avci->buffer_frame->buf[0])
557 return AVERROR(EAGAIN);
558
559
2/2
✓ Branch 0 taken 8331 times.
✓ Branch 1 taken 534533 times.
542864 if (!frame) {
560 8331 avci->draining = 1;
561 } else {
562 534533 ret = encode_send_frame_internal(avctx, frame);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534533 times.
534533 if (ret < 0)
564 return ret;
565 }
566
567
2/4
✓ Branch 0 taken 542864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 542864 times.
✗ Branch 3 not taken.
542864 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
568 542864 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
569
5/6
✓ Branch 0 taken 15944 times.
✓ Branch 1 taken 526920 times.
✓ Branch 2 taken 6449 times.
✓ Branch 3 taken 9495 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6449 times.
542864 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
570 return ret;
571 }
572
573 542864 avctx->frame_num++;
574
575 542864 return 0;
576 }
577
578 1071279 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
579 {
580 1071279 AVCodecInternal *avci = avctx->internal;
581 int ret;
582
583 1071279 av_packet_unref(avpkt);
584
585
2/4
✓ Branch 1 taken 1071279 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1071279 times.
1071279 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
586 return AVERROR(EINVAL);
587
588
4/4
✓ Branch 0 taken 544436 times.
✓ Branch 1 taken 526843 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 544359 times.
1071279 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
589 526920 av_packet_move_ref(avpkt, avci->buffer_pkt);
590 } else {
591 544359 ret = encode_receive_packet_internal(avctx, avpkt);
592
2/2
✓ Branch 0 taken 542794 times.
✓ Branch 1 taken 1565 times.
544359 if (ret < 0)
593 542794 return ret;
594 }
595
596 528485 return 0;
597 }
598
599 av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
600 {
601 const AVCodec *codec = avctx->codec;
602 const FFCodec *codec2 = ffcodec(codec);
603 const AVDictionaryEntry *t = NULL;
604 AVDictionary *copy = NULL;
605 int ret;
606
607 av_assert0(av_codec_is_encoder(codec) && (codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF));
608
609 ret = av_dict_copy(&copy, *dict, 0);
610 if (ret < 0)
611 goto end;
612
613 // Remove the dictionary entries that would be applied to the private codec context
614 if (codec->priv_class) {
615 while ((t = av_dict_iterate(*dict, t))) {
616 if (av_opt_find(avctx->priv_data, t->key, NULL,
617 AV_OPT_FLAG_RUNTIME_PARAM, 0))
618 av_dict_set(dict, t->key, NULL, 0);
619 }
620 }
621
622 // Ditto for global options
623 if (codec2->defaults) {
624 const FFCodecDefault *d = codec2->defaults;
625 while (d->key) {
626 if (d->flags & AV_OPT_FLAG_RUNTIME_PARAM)
627 av_dict_set(dict, d->key, NULL, 0);
628 d++;
629 }
630 }
631
632 // If any entry remains, then the requested option/s don't exist or are not settable.
633 if (av_dict_count(*dict)) {
634 ret = AVERROR_OPTION_NOT_FOUND;
635 goto end;
636 }
637
638 ret = av_dict_copy(dict, copy, 0);
639 if (ret < 0)
640 goto end;
641
642 // Do a dry run of applying the options, to ensure the encoder is unchanged in case
643 // one of them has an invalid value.
644 // This is done twice, once for avctx and once for the AVCodec, because using the
645 // search children flag in combination with the fake obj flag will iterate through
646 // the options from all compiled in codecs if you pass the avctx class.
647 if (codec->priv_class) {
648 ret = av_opt_set_dict2((void *)&codec->priv_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
649 if (ret < 0)
650 goto end;
651 }
652 ret = av_opt_set_dict2((void *)&avctx->av_class, &copy, AV_OPT_SEARCH_FAKE_OBJ);
653 if (ret < 0)
654 goto end;
655
656 // The dictionary should be empty.
657 av_assert0(!av_dict_count(copy));
658
659 ret = av_opt_set_dict2(avctx, dict, AV_OPT_SEARCH_CHILDREN);
660 if (ret < 0)
661 goto end;
662
663 // The dictionary should be empty.
664 av_assert0(!av_dict_count(*dict));
665
666 ret = 0;
667 end:
668 av_dict_free(&copy);
669
670 return ret;
671 }
672
673 av_cold int avcodec_encode_reconfigure(AVCodecContext *avctx, AVDictionary **dict)
674 {
675 const FFCodec *codec = ffcodec(avctx->codec);
676 int ret = AVERROR_BUG;
677
678 if (!dict || !*dict || !avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
679 return AVERROR(EINVAL);
680
681 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF)) {
682 av_log(avctx, AV_LOG_ERROR, "This encoder does not support reconfiguration\n");
683 return AVERROR(ENOSYS);
684 }
685
686 if (codec->reconf)
687 ret = codec->reconf(avctx, dict);
688 else
689 ret = ff_encode_reconf_parse_dict(avctx, dict);
690 if (ret < 0)
691 return ret;
692
693 return 0;
694 }
695
696 20488 static int encode_preinit_video(AVCodecContext *avctx)
697 {
698 20488 const AVCodec *c = avctx->codec;
699 20488 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
700 const enum AVPixelFormat *pix_fmts;
701 int ret, i, num_pix_fmts;
702
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20488 times.
20488 if (!pixdesc) {
704 av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
705 avctx->pix_fmt);
706 return AVERROR(EINVAL);
707 }
708
709 20488 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_PIX_FORMAT,
710 0, (const void **) &pix_fmts, &num_pix_fmts);
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20488 times.
20488 if (ret < 0)
712 return ret;
713
714
2/2
✓ Branch 0 taken 848 times.
✓ Branch 1 taken 19640 times.
20488 if (pix_fmts) {
715
1/2
✓ Branch 0 taken 2738 times.
✗ Branch 1 not taken.
2738 for (i = 0; i < num_pix_fmts; i++)
716
2/2
✓ Branch 0 taken 848 times.
✓ Branch 1 taken 1890 times.
2738 if (avctx->pix_fmt == pix_fmts[i])
717 848 break;
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
848 if (i == num_pix_fmts) {
719 av_log(avctx, AV_LOG_ERROR,
720 "Specified pixel format %s is not supported by the %s encoder.\n",
721 av_get_pix_fmt_name(avctx->pix_fmt), c->name);
722
723 av_log(avctx, AV_LOG_ERROR, "Supported pixel formats:\n");
724 for (int p = 0; pix_fmts[p] != AV_PIX_FMT_NONE; p++) {
725 av_log(avctx, AV_LOG_ERROR, " %s\n",
726 av_get_pix_fmt_name(pix_fmts[p]));
727 }
728
729 return AVERROR(EINVAL);
730 }
731
2/2
✓ Branch 0 taken 827 times.
✓ Branch 1 taken 21 times.
848 if (pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
732
1/2
✓ Branch 0 taken 827 times.
✗ Branch 1 not taken.
827 pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
733
2/2
✓ Branch 0 taken 823 times.
✓ Branch 1 taken 4 times.
827 pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
734
1/2
✓ Branch 0 taken 823 times.
✗ Branch 1 not taken.
823 pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
735
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 815 times.
823 pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
736 33 avctx->color_range = AVCOL_RANGE_JPEG;
737 }
738
739
2/2
✓ Branch 0 taken 2555 times.
✓ Branch 1 taken 17933 times.
20488 if (pixdesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
740 const enum AVAlphaMode *alpha_modes;
741 int num_alpha_modes;
742 2555 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_ALPHA_MODE,
743 0, (const void **) &alpha_modes, &num_alpha_modes);
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2555 times.
2555 if (ret < 0)
745 return ret;
746
747
4/4
✓ Branch 0 taken 524 times.
✓ Branch 1 taken 2031 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 520 times.
2555 if (avctx->alpha_mode != AVALPHA_MODE_UNSPECIFIED && alpha_modes) {
748
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (i = 0; i < num_alpha_modes; i++) {
749
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->alpha_mode == alpha_modes[i])
750 4 break;
751 }
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (i == num_alpha_modes) {
753 av_log(avctx, AV_LOG_ERROR,
754 "Specified alpha mode '%s' is not supported by the %s encoder.\n",
755 av_alpha_mode_name(avctx->alpha_mode), c->name);
756 av_log(avctx, AV_LOG_ERROR, "Supported alpha modes:\n");
757 for (int p = 0; alpha_modes[p] != AVALPHA_MODE_UNSPECIFIED; p++) {
758 av_log(avctx, AV_LOG_ERROR, " %s\n",
759 av_alpha_mode_name(alpha_modes[p]));
760 }
761 return AVERROR(EINVAL);
762 }
763 }
764 }
765
766
1/2
✓ Branch 0 taken 20488 times.
✗ Branch 1 not taken.
20488 if ( avctx->bits_per_raw_sample < 0
767
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20405 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
20488 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
768 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
769 avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
770 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
771 }
772
2/4
✓ Branch 0 taken 20488 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20488 times.
20488 if (avctx->width <= 0 || avctx->height <= 0) {
773 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
774 return AVERROR(EINVAL);
775 }
776
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20488 times.
20488 if (avctx->hw_frames_ctx) {
778 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
779 if (frames_ctx->format != avctx->pix_fmt) {
780 av_log(avctx, AV_LOG_ERROR,
781 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
782 return AVERROR(EINVAL);
783 }
784 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
785 avctx->sw_pix_fmt != frames_ctx->sw_format) {
786 av_log(avctx, AV_LOG_ERROR,
787 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
788 "and AVHWFramesContext.sw_format (%s)\n",
789 av_get_pix_fmt_name(avctx->sw_pix_fmt),
790 av_get_pix_fmt_name(frames_ctx->sw_format));
791 return AVERROR(EINVAL);
792 }
793 avctx->sw_pix_fmt = frames_ctx->sw_format;
794 }
795
796 20488 return 0;
797 }
798
799 1437 static int encode_preinit_audio(AVCodecContext *avctx)
800 {
801 1437 const AVCodec *c = avctx->codec;
802 const enum AVSampleFormat *sample_fmts;
803 const int *supported_samplerates;
804 const AVChannelLayout *ch_layouts;
805 int ret, i, num_sample_fmts, num_samplerates, num_ch_layouts;
806
807
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1437 times.
1437 if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
808 av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
809 avctx->sample_fmt);
810 return AVERROR(EINVAL);
811 }
812
813 1437 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_FORMAT,
814 0, (const void **) &sample_fmts,
815 &num_sample_fmts);
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
1437 if (ret < 0)
817 return ret;
818
1/2
✓ Branch 0 taken 1437 times.
✗ Branch 1 not taken.
1437 if (sample_fmts) {
819
1/2
✓ Branch 0 taken 1456 times.
✗ Branch 1 not taken.
1456 for (i = 0; i < num_sample_fmts; i++) {
820
2/2
✓ Branch 0 taken 1437 times.
✓ Branch 1 taken 19 times.
1456 if (avctx->sample_fmt == sample_fmts[i])
821 1437 break;
822
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 &&
823 5 av_get_planar_sample_fmt(avctx->sample_fmt) ==
824 5 av_get_planar_sample_fmt(sample_fmts[i])) {
825 avctx->sample_fmt = sample_fmts[i];
826 break;
827 }
828 }
829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
1437 if (i == num_sample_fmts) {
830 av_log(avctx, AV_LOG_ERROR,
831 "Specified sample format %s is not supported by the %s encoder\n",
832 av_get_sample_fmt_name(avctx->sample_fmt), c->name);
833
834 av_log(avctx, AV_LOG_ERROR, "Supported sample formats:\n");
835 for (int p = 0; sample_fmts[p] != AV_SAMPLE_FMT_NONE; p++) {
836 av_log(avctx, AV_LOG_ERROR, " %s\n",
837 av_get_sample_fmt_name(sample_fmts[p]));
838 }
839
840 return AVERROR(EINVAL);
841 }
842 }
843
844 1437 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_SAMPLE_RATE,
845 0, (const void **) &supported_samplerates,
846 &num_samplerates);
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
1437 if (ret < 0)
848 return ret;
849
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 1350 times.
1437 if (supported_samplerates) {
850
1/2
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
204 for (i = 0; i < num_samplerates; i++)
851
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 117 times.
204 if (avctx->sample_rate == supported_samplerates[i])
852 87 break;
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (i == num_samplerates) {
854 av_log(avctx, AV_LOG_ERROR,
855 "Specified sample rate %d is not supported by the %s encoder\n",
856 avctx->sample_rate, c->name);
857
858 av_log(avctx, AV_LOG_ERROR, "Supported sample rates:\n");
859 for (int p = 0; supported_samplerates[p]; p++)
860 av_log(avctx, AV_LOG_ERROR, " %d\n", supported_samplerates[p]);
861
862 return AVERROR(EINVAL);
863 }
864 }
865 1437 ret = avcodec_get_supported_config(avctx, NULL, AV_CODEC_CONFIG_CHANNEL_LAYOUT,
866 0, (const void **) &ch_layouts, &num_ch_layouts);
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1437 times.
1437 if (ret < 0)
868 return ret;
869
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 1335 times.
1437 if (ch_layouts) {
870
1/2
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
214 for (i = 0; i < num_ch_layouts; i++) {
871
2/2
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 112 times.
214 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
872 102 break;
873 }
874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102 times.
102 if (i == num_ch_layouts) {
875 char buf[512];
876 int ret = av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
877 av_log(avctx, AV_LOG_ERROR,
878 "Specified channel layout '%s' is not supported by the %s encoder\n",
879 ret > 0 ? buf : "?", c->name);
880
881 av_log(avctx, AV_LOG_ERROR, "Supported channel layouts:\n");
882 for (int p = 0; ch_layouts[p].nb_channels; p++) {
883 ret = av_channel_layout_describe(&ch_layouts[p], buf, sizeof(buf));
884 av_log(avctx, AV_LOG_ERROR, " %s\n", ret > 0 ? buf : "?");
885 }
886 return AVERROR(EINVAL);
887 }
888 }
889
890
2/2
✓ Branch 0 taken 1381 times.
✓ Branch 1 taken 56 times.
1437 if (!avctx->bits_per_raw_sample)
891 1381 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
892
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 1229 times.
1437 if (!avctx->bits_per_raw_sample)
893 208 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
894
895 1437 return 0;
896 }
897
898 21967 int ff_encode_preinit(AVCodecContext *avctx)
899 {
900 21967 AVCodecInternal *avci = avctx->internal;
901 21967 EncodeContext *ec = encode_ctx(avci);
902 21967 int ret = 0;
903
904
2/4
✓ Branch 0 taken 21967 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21967 times.
21967 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
905 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
906 return AVERROR(EINVAL);
907 }
908
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21967 times.
21967 if (avctx->bit_rate < 0) {
910 av_log(avctx, AV_LOG_ERROR, "The encoder bitrate is negative.\n");
911 return AVERROR(EINVAL);
912 }
913
914
2/2
✓ Branch 0 taken 21861 times.
✓ Branch 1 taken 106 times.
21967 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21861 times.
21861 !(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)) {
916 av_log(avctx, AV_LOG_ERROR, "The copy_opaque flag is set, but the "
917 "encoder does not support it.\n");
918 return AVERROR(EINVAL);
919 }
920
921
3/3
✓ Branch 0 taken 20488 times.
✓ Branch 1 taken 1437 times.
✓ Branch 2 taken 42 times.
21967 switch (avctx->codec_type) {
922 20488 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
923 1437 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
924 }
925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21967 times.
21967 if (ret < 0)
926 return ret;
927
928
4/4
✓ Branch 0 taken 1479 times.
✓ Branch 1 taken 20488 times.
✓ Branch 2 taken 1437 times.
✓ Branch 3 taken 42 times.
21967 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
929
3/4
✓ Branch 0 taken 21896 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21896 times.
21925 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
930 av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
931 }
932
933
2/2
✓ Branch 0 taken 21965 times.
✓ Branch 1 taken 2 times.
21967 if (!avctx->rc_initial_buffer_occupancy)
934 21965 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
935
936
2/2
✓ Branch 0 taken 21525 times.
✓ Branch 1 taken 442 times.
21967 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
937 21525 ec->intra_only_flag = AV_PKT_FLAG_KEY;
938
939
2/2
✓ Branch 1 taken 21925 times.
✓ Branch 2 taken 42 times.
21967 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
940 21925 avci->in_frame = av_frame_alloc();
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21925 times.
21925 if (!avci->in_frame)
942 return AVERROR(ENOMEM);
943 }
944
945
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 21963 times.
21967 if ((avctx->flags & AV_CODEC_FLAG_RECON_FRAME)) {
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!(avctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECON_FRAME)) {
947 av_log(avctx, AV_LOG_ERROR, "Reconstructed frame output requested "
948 "from an encoder not supporting it\n");
949 return AVERROR(ENOSYS);
950 }
951
952 4 avci->recon_frame = av_frame_alloc();
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!avci->recon_frame)
954 return AVERROR(ENOMEM);
955 }
956
957
2/2
✓ Branch 0 taken 241637 times.
✓ Branch 1 taken 21967 times.
263604 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
958 241637 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
959 241637 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
960 const AVFrameSideData *sd_frame;
961 AVPacketSideData *sd_packet;
962
963 241637 sd_frame = av_frame_side_data_get(avctx->decoded_side_data,
964 avctx->nb_decoded_side_data,
965 type_frame);
966
3/4
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 241364 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
241910 if (!sd_frame ||
967 273 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
968 type_packet))
969
970 241364 continue;
971
972 273 sd_packet = av_packet_side_data_new(&avctx->coded_side_data, &avctx->nb_coded_side_data,
973 273 type_packet, sd_frame->size, 0);
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
273 if (!sd_packet)
975 return AVERROR(ENOMEM);
976
977 273 memcpy(sd_packet->data, sd_frame->data, sd_frame->size);
978 }
979
980 #if CONFIG_FRAME_THREAD_ENCODER
981 21967 ret = ff_frame_thread_encoder_init(avctx);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21967 times.
21967 if (ret < 0)
983 return ret;
984 #endif
985
986 21967 return 0;
987 }
988
989 12654 int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
990 {
991 int ret;
992
993 av_assert1(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
994
995 12654 frame->format = avctx->pix_fmt;
996
3/4
✓ Branch 0 taken 12638 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12638 times.
12654 if (frame->width <= 0 || frame->height <= 0) {
997 16 frame->width = avctx->width;
998 16 frame->height = avctx->height;
999 }
1000
1001 12654 ret = avcodec_default_get_buffer2(avctx, frame, 0);
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12654 times.
12654 if (ret < 0) {
1003 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1004 av_frame_unref(frame);
1005 return ret;
1006 }
1007
1008 12654 return 0;
1009 }
1010
1011 70 int ff_encode_receive_frame(AVCodecContext *avctx, AVFrame *frame)
1012 {
1013 70 AVCodecInternal *avci = avctx->internal;
1014
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (!avci->recon_frame)
1016 return AVERROR(EINVAL);
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (!avci->recon_frame->buf[0])
1018 return avci->draining_done ? AVERROR_EOF : AVERROR(EAGAIN);
1019
1020 70 av_frame_move_ref(frame, avci->recon_frame);
1021 70 return 0;
1022 }
1023
1024 void ff_encode_flush_buffers(AVCodecContext *avctx)
1025 {
1026 AVCodecInternal *avci = avctx->internal;
1027
1028 if (avci->in_frame)
1029 av_frame_unref(avci->in_frame);
1030 if (avci->recon_frame)
1031 av_frame_unref(avci->recon_frame);
1032 }
1033
1034 21967 AVCodecInternal *ff_encode_internal_alloc(void)
1035 {
1036 21967 return av_mallocz(sizeof(EncodeContext));
1037 }
1038
1039 230 AVCPBProperties *ff_encode_add_cpb_side_data(AVCodecContext *avctx)
1040 {
1041 AVPacketSideData *tmp;
1042 AVCPBProperties *props;
1043 size_t size;
1044 int i;
1045
1046
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 230 times.
251 for (i = 0; i < avctx->nb_coded_side_data; i++)
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
1048 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1049
1050 230 props = av_cpb_properties_alloc(&size);
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (!props)
1052 return NULL;
1053
1054 230 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (!tmp) {
1056 av_freep(&props);
1057 return NULL;
1058 }
1059
1060 230 avctx->coded_side_data = tmp;
1061 230 avctx->nb_coded_side_data++;
1062
1063 230 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1064 230 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1065 230 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1066
1067 230 return props;
1068 }
1069
1070 13100 int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[],
1071 int error_count, enum AVPictureType pict_type)
1072 {
1073 uint8_t *side_data;
1074 size_t side_data_size;
1075
1076 13100 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1077
1/2
✓ Branch 0 taken 13100 times.
✗ Branch 1 not taken.
13100 if (!side_data) {
1078 13100 side_data_size = 4+4+8*error_count;
1079 13100 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
1080 side_data_size);
1081 }
1082
1083
2/4
✓ Branch 0 taken 13100 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13100 times.
13100 if (!side_data || side_data_size < 4+4+8*error_count)
1084 return AVERROR(ENOMEM);
1085
1086 13100 AV_WL32(side_data, quality);
1087 13100 side_data[4] = pict_type;
1088 13100 side_data[5] = error_count;
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13100 times.
13100 for (int i = 0; i < error_count; ++i)
1090 AV_WL64(side_data+8 + 8*i , error[i]);
1091
1092 13100 return 0;
1093 }
1094
1095 1638 int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
1096 {
1097 1638 uint16_t *matrices[] = {avctx->intra_matrix, avctx->inter_matrix, avctx->chroma_intra_matrix};
1098 1638 const char *names[] = {"Intra", "Inter", "Chroma Intra"};
1099 static_assert(FF_ARRAY_ELEMS(matrices) == FF_ARRAY_ELEMS(names), "matrix count mismatch");
1100
2/2
✓ Branch 0 taken 4914 times.
✓ Branch 1 taken 1638 times.
6552 for (int m = 0; m < FF_ARRAY_ELEMS(matrices); m++) {
1101 4914 uint16_t *matrix = matrices[m];
1102
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4914 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4914 if (matrix && (types & (1U << m))) {
1103 for (int i = 0; i < 64; i++) {
1104 if (matrix[i] < min || matrix[i] > max) {
1105 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);
1106 return AVERROR(EINVAL);
1107 }
1108 }
1109 }
1110 }
1111 1638 return 0;
1112 }
1113