FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/encode.c
Date: 2026-08-23 18:45:53
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 1604878 static EncodeContext *encode_ctx(AVCodecInternal *avci)
58 {
59 1604878 return (EncodeContext*)avci;
60 }
61
62 30109 int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
63 {
64
2/4
✓ Branch 0 taken 30109 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30109 times.
30109 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 30109 times.
30109 av_assert0(!avpkt->data);
71
72 30109 av_fast_padded_malloc(&avctx->internal->byte_buffer,
73 30109 &avctx->internal->byte_buffer_size, size);
74 30109 avpkt->data = avctx->internal->byte_buffer;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30109 times.
30109 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 30109 avpkt->size = size;
80
81 30109 return 0;
82 }
83
84 529556 int avcodec_default_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int flags)
85 {
86 int ret;
87
88
2/4
✓ Branch 0 taken 529556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529556 times.
529556 if (avpkt->size < 0 || avpkt->size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
89 return AVERROR(EINVAL);
90
91
2/4
✓ Branch 0 taken 529556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529556 times.
529556 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 529556 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529556 times.
529556 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 529556 avpkt->data = avpkt->buf->data;
102
103 529556 return 0;
104 }
105
106 529556 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 529556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529556 times.
529556 if (size < 0 || size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
111 return AVERROR(EINVAL);
112
113
2/4
✓ Branch 0 taken 529556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529556 times.
529556 av_assert0(!avpkt->data && !avpkt->buf);
114
115 529556 avpkt->size = size;
116 529556 ret = avctx->get_encode_buffer(avctx, avpkt, flags);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529556 times.
529556 if (ret < 0)
118 goto fail;
119
120
2/4
✓ Branch 0 taken 529556 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529556 times.
529556 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 529556 memset(avpkt->data + avpkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
126
127 529556 ret = 0;
128 529556 fail:
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529556 times.
529556 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "get_encode_buffer() failed\n");
131 av_packet_unref(avpkt);
132 }
133
134 529556 return ret;
135 }
136
137 530230 static int encode_make_refcounted(AVCodecContext *avctx, AVPacket *avpkt)
138 {
139 530230 uint8_t *data = avpkt->data;
140 int ret;
141
142
2/2
✓ Branch 0 taken 500121 times.
✓ Branch 1 taken 30109 times.
530230 if (avpkt->buf)
143 500121 return 0;
144
145 30109 avpkt->data = NULL;
146 30109 ret = ff_get_encode_buffer(avctx, avpkt, avpkt->size, 0);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30109 times.
30109 if (ret < 0)
148 return ret;
149 30109 memcpy(avpkt->data, data, avpkt->size);
150
151 30109 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 982 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 982 times.
982 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 982 ret = ffcodec(avctx->codec)->cb.encode_sub(avctx, buf, buf_size, sub);
214 982 avctx->frame_num++;
215 982 return ret;
216 }
217
218 1082205 int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
219 {
220 1082205 AVCodecInternal *avci = avctx->internal;
221
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1082205 times.
1082205 if (avci->draining)
223 return AVERROR_EOF;
224
225
2/2
✓ Branch 0 taken 545856 times.
✓ Branch 1 taken 536349 times.
1082205 if (!avci->buffer_frame->buf[0])
226 545856 return AVERROR(EAGAIN);
227
228 536349 av_frame_move_ref(frame, avci->buffer_frame);
229
230 536349 return 0;
231 }
232
233 516228 static int encode_set_packet_props(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame)
234 {
235 516228 AVCodecInternal *avci = avctx->internal;
236 516228 EncodeContext *ec = encode_ctx(avci);
237
238
2/2
✓ Branch 0 taken 501952 times.
✓ Branch 1 taken 14276 times.
516228 if (avpkt->pts == AV_NOPTS_VALUE) {
239 501952 avpkt->pts = frame->pts;
240
4/4
✓ Branch 0 taken 370155 times.
✓ Branch 1 taken 131797 times.
✓ Branch 2 taken 366955 times.
✓ Branch 3 taken 3200 times.
501952 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO && avpkt->pts != AV_NOPTS_VALUE)
241 366955 avpkt->pts -= ff_samples_to_time_base(avctx, avctx->initial_padding);
242 }
243
244
2/2
✓ Branch 0 taken 511843 times.
✓ Branch 1 taken 4385 times.
516228 if (!avpkt->duration) {
245
2/2
✓ Branch 0 taken 491189 times.
✓ Branch 1 taken 20654 times.
511843 if (frame->duration)
246 491189 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 380042 times.
✓ Branch 1 taken 131801 times.
511843 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
252 380042 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 379894 times.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
380042 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 516228 return 0;
278 }
279
280 524127 int ff_encode_reordered_opaque(AVCodecContext *avctx,
281 AVPacket *pkt, const AVFrame *frame)
282 {
283
2/2
✓ Branch 0 taken 520574 times.
✓ Branch 1 taken 3553 times.
524127 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
284 520574 int ret = av_buffer_replace(&pkt->opaque_ref, frame->opaque_ref);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520574 times.
520574 if (ret < 0)
286 return ret;
287 520574 pkt->opaque = frame->opaque;
288 }
289
290 524127 return 0;
291 }
292
293 536944 int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
294 AVFrame *frame, int *got_packet)
295 {
296 536944 const FFCodec *const codec = ffcodec(avctx->codec);
297 int ret;
298
299 536944 ret = codec->cb.encode(avctx, avpkt, frame, got_packet);
300 ff_assert1_fpu();
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536944 times.
536944 av_assert0(ret <= 0);
302
303
3/4
✓ Branch 0 taken 536944 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6637 times.
✓ Branch 3 taken 530307 times.
536944 if (!ret && *got_packet) {
304
2/2
✓ Branch 0 taken 530230 times.
✓ Branch 1 taken 77 times.
530307 if (avpkt->data) {
305 530230 ret = encode_make_refcounted(avctx, avpkt);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 530230 times.
530230 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 530230 times.
530230 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 29707 times.
✓ Branch 1 taken 500600 times.
✓ Branch 2 taken 29426 times.
✓ Branch 3 taken 281 times.
530307 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
315
2/2
✓ Branch 0 taken 15628 times.
✓ Branch 1 taken 13798 times.
29426 (frame && (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))) {
316 516228 ret = encode_set_packet_props(avctx, avpkt, frame);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 516228 times.
516228 if (ret < 0)
318 goto unref;
319
320 516228 ret = ff_encode_reordered_opaque(avctx, avpkt, frame);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 516228 times.
516228 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 8638 times.
✓ Branch 1 taken 521669 times.
530307 if (!(avctx->codec_descriptor->props & AV_CODEC_PROP_REORDER) ||
328
2/2
✓ Branch 0 taken 7873 times.
✓ Branch 1 taken 765 times.
8638 !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) ||
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7873 times.
7873 (codec->caps_internal & FF_CODEC_CAP_EOF_FLUSH))
330 522434 avpkt->dts = avpkt->pts;
331 } else {
332 6637 unref:
333 6637 av_packet_unref(avpkt);
334 }
335
336
2/2
✓ Branch 0 taken 536349 times.
✓ Branch 1 taken 595 times.
536944 if (frame)
337 536349 av_frame_unref(frame);
338
339 536944 return ret;
340 }
341
342 1096103 static int encode_simple_internal(AVCodecContext *avctx, AVPacket *avpkt)
343 {
344 1096103 AVCodecInternal *avci = avctx->internal;
345 1096103 AVFrame *frame = avci->in_frame;
346 1096103 const FFCodec *const codec = ffcodec(avctx->codec);
347 int got_packet;
348 int ret;
349
350
2/2
✓ Branch 0 taken 2013 times.
✓ Branch 1 taken 1094090 times.
1096103 if (avci->draining_done)
351 2013 return AVERROR_EOF;
352
353
3/4
✓ Branch 0 taken 1094090 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1082205 times.
✓ Branch 3 taken 11885 times.
1094090 if (!frame->buf[0] && !avci->draining) {
354 1082205 av_frame_unref(frame);
355 1082205 ret = ff_encode_get_frame(avctx, frame);
356
3/4
✓ Branch 0 taken 545856 times.
✓ Branch 1 taken 536349 times.
✓ Branch 2 taken 545856 times.
✗ Branch 3 not taken.
1082205 if (ret < 0 && ret != AVERROR_EOF)
357 545856 return ret;
358 }
359
360
2/2
✓ Branch 0 taken 11885 times.
✓ Branch 1 taken 536349 times.
548234 if (!frame->buf[0]) {
361
2/2
✓ Branch 0 taken 11290 times.
✓ Branch 1 taken 595 times.
11885 if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
362
2/2
✓ Branch 0 taken 6337 times.
✓ Branch 1 taken 4953 times.
11290 avci->frame_thread_encoder))
363 6337 return AVERROR_EOF;
364
365 // Flushing is signaled with a NULL frame
366 5548 frame = NULL;
367 }
368
369 541897 got_packet = 0;
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 541897 times.
541897 av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
372
373 #if CONFIG_FRAME_THREAD_ENCODER
374
2/2
✓ Branch 0 taken 63577 times.
✓ Branch 1 taken 478320 times.
541897 if (avci->frame_thread_encoder)
375 /* This will unref frame. */
376 63577 ret = ff_thread_video_encode_frame(avctx, avpkt, frame, &got_packet);
377 else
378 #endif
379 478320 ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
380
381
4/4
✓ Branch 0 taken 5548 times.
✓ Branch 1 taken 536349 times.
✓ Branch 2 taken 2013 times.
✓ Branch 3 taken 3535 times.
541897 if (avci->draining && !got_packet)
382 2013 avci->draining_done = 1;
383
384 541897 return ret;
385 }
386
387 1084513 static int encode_simple_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
388 {
389 int ret;
390
391
4/4
✓ Branch 0 taken 1096180 times.
✓ Branch 1 taken 530230 times.
✓ Branch 2 taken 1096103 times.
✓ Branch 3 taken 77 times.
1626410 while (!avpkt->data && !avpkt->side_data) {
392 1096103 ret = encode_simple_internal(avctx, avpkt);
393
2/2
✓ Branch 0 taken 554206 times.
✓ Branch 1 taken 541897 times.
1096103 if (ret < 0)
394 554206 return ret;
395 }
396
397 530307 return 0;
398 }
399
400 1090970 static int encode_receive_packet_internal(AVCodecContext *avctx, AVPacket *avpkt)
401 {
402 1090970 AVCodecInternal *avci = avctx->internal;
403 int ret;
404
405
2/2
✓ Branch 0 taken 6457 times.
✓ Branch 1 taken 1084513 times.
1090970 if (avci->draining_done)
406 6457 return AVERROR_EOF;
407
408
2/4
✓ Branch 0 taken 1084513 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1084513 times.
1084513 av_assert0(!avpkt->data && !avpkt->side_data);
409
410
2/2
✓ Branch 0 taken 298003 times.
✓ Branch 1 taken 786510 times.
1084513 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
411
3/4
✓ Branch 0 taken 808 times.
✓ Branch 1 taken 297195 times.
✓ Branch 2 taken 808 times.
✗ Branch 3 not taken.
298003 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 1084513 times.
1084513 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 1084513 ret = encode_simple_receive_packet(avctx, avpkt);
425
2/2
✓ Branch 0 taken 530307 times.
✓ Branch 1 taken 554206 times.
1084513 if (ret >= 0)
426 530307 avpkt->flags |= encode_ctx(avci)->intra_only_flag;
427
428
2/2
✓ Branch 0 taken 8350 times.
✓ Branch 1 taken 1076163 times.
1084513 if (ret == AVERROR_EOF)
429 8350 avci->draining_done = 1;
430
431 1084513 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 143879 static int encode_generate_icc_profile(av_unused AVCodecContext *c, av_unused AVFrame *f)
476 {
477 143879 return 0;
478 }
479 #endif
480
481 536349 static int encode_send_frame_internal(AVCodecContext *avctx, const AVFrame *src)
482 {
483 536349 AVCodecInternal *avci = avctx->internal;
484 536349 EncodeContext *ec = encode_ctx(avci);
485 536349 AVFrame *dst = avci->buffer_frame;
486 int ret;
487
488
2/2
✓ Branch 0 taken 392470 times.
✓ Branch 1 taken 143879 times.
536349 if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
489 /* extract audio service type metadata */
490 392470 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 391924 times.
✓ Branch 2 taken 546 times.
✗ Branch 3 not taken.
392470 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 108606 times.
✓ Branch 1 taken 283864 times.
392470 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 108606 times.
108606 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 108606 times.
108606 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 191 times.
✓ Branch 1 taken 108415 times.
108606 if (src->nb_samples < avctx->frame_size) {
506 191 ec->last_audio_frame = 1;
507
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 52 times.
191 if (!(avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) ||
508
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 81 times.
139 (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 536239 ret = av_frame_ref(dst, src);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536239 times.
536239 if (ret < 0)
525 return ret;
526
527 536239 finish:
528
529
2/2
✓ Branch 0 taken 143879 times.
✓ Branch 1 taken 392470 times.
536349 if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
530 143879 ret = encode_generate_icc_profile(avctx, dst);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143879 times.
143879 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 533075 times.
536349 if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
540 3274 dst->duration = 0;
541
542 536349 return 0;
543 }
544
545 544699 int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
546 {
547 544699 AVCodecInternal *avci = avctx->internal;
548 int ret;
549
550
2/4
✓ Branch 1 taken 544699 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 544699 times.
544699 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 544699 times.
544699 if (avci->draining)
554 return AVERROR_EOF;
555
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 544699 times.
544699 if (avci->buffer_frame->buf[0])
557 return AVERROR(EAGAIN);
558
559
2/2
✓ Branch 0 taken 8350 times.
✓ Branch 1 taken 536349 times.
544699 if (!frame) {
560 8350 avci->draining = 1;
561 } else {
562 536349 ret = encode_send_frame_internal(avctx, frame);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 536349 times.
536349 if (ret < 0)
564 return ret;
565 }
566
567
2/4
✓ Branch 0 taken 544699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 544699 times.
✗ Branch 3 not taken.
544699 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data) {
568 544699 ret = encode_receive_packet_internal(avctx, avci->buffer_pkt);
569
5/6
✓ Branch 0 taken 16034 times.
✓ Branch 1 taken 528665 times.
✓ Branch 2 taken 6457 times.
✓ Branch 3 taken 9577 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6457 times.
544699 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
570 return ret;
571 }
572
573 544699 avctx->frame_num++;
574
575 544699 return 0;
576 }
577
578 1074936 int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
579 {
580 1074936 AVCodecInternal *avci = avctx->internal;
581 int ret;
582
583 1074936 av_packet_unref(avpkt);
584
585
2/4
✓ Branch 1 taken 1074936 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1074936 times.
1074936 if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
586 return AVERROR(EINVAL);
587
588
4/4
✓ Branch 0 taken 546348 times.
✓ Branch 1 taken 528588 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 546271 times.
1074936 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data) {
589 528665 av_packet_move_ref(avpkt, avci->buffer_pkt);
590 } else {
591 546271 ret = encode_receive_packet_internal(avctx, avpkt);
592
2/2
✓ Branch 0 taken 544629 times.
✓ Branch 1 taken 1642 times.
546271 if (ret < 0)
593 544629 return ret;
594 }
595
596 530307 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 20497 static int encode_preinit_video(AVCodecContext *avctx)
697 {
698 20497 const AVCodec *c = avctx->codec;
699 20497 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 20497 times.
20497 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 20497 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 20497 times.
20497 if (ret < 0)
712 return ret;
713
714
2/2
✓ Branch 0 taken 848 times.
✓ Branch 1 taken 19649 times.
20497 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 17942 times.
20497 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 20497 times.
✗ Branch 1 not taken.
20497 if ( avctx->bits_per_raw_sample < 0
767
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 20414 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 83 times.
20497 || (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 20497 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20497 times.
20497 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 20497 times.
20497 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 20497 return 0;
797 }
798
799 1455 static int encode_preinit_audio(AVCodecContext *avctx)
800 {
801 1455 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 1455 times.
1455 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 1455 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 1455 times.
1455 if (ret < 0)
817 return ret;
818
1/2
✓ Branch 0 taken 1455 times.
✗ Branch 1 not taken.
1455 if (sample_fmts) {
819
1/2
✓ Branch 0 taken 1474 times.
✗ Branch 1 not taken.
1474 for (i = 0; i < num_sample_fmts; i++) {
820
2/2
✓ Branch 0 taken 1455 times.
✓ Branch 1 taken 19 times.
1474 if (avctx->sample_fmt == sample_fmts[i])
821 1455 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 1455 times.
1455 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 1455 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 1455 times.
1455 if (ret < 0)
848 return ret;
849
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 1360 times.
1455 if (supported_samplerates) {
850
1/2
✓ Branch 0 taken 241 times.
✗ Branch 1 not taken.
241 for (i = 0; i < num_samplerates; i++)
851
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 146 times.
241 if (avctx->sample_rate == supported_samplerates[i])
852 95 break;
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 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 1455 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 1455 times.
1455 if (ret < 0)
868 return ret;
869
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 1352 times.
1455 if (ch_layouts) {
870
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 for (i = 0; i < num_ch_layouts; i++) {
871
2/2
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 113 times.
216 if (!av_channel_layout_compare(&avctx->ch_layout, &ch_layouts[i]))
872 103 break;
873 }
874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 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 1399 times.
✓ Branch 1 taken 56 times.
1455 if (!avctx->bits_per_raw_sample)
891 1399 avctx->bits_per_raw_sample = av_get_exact_bits_per_sample(avctx->codec_id);
892
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1239 times.
1455 if (!avctx->bits_per_raw_sample)
893 216 avctx->bits_per_raw_sample = 8 * av_get_bytes_per_sample(avctx->sample_fmt);
894
895 1455 return 0;
896 }
897
898 21994 int ff_encode_preinit(AVCodecContext *avctx)
899 {
900 21994 AVCodecInternal *avci = avctx->internal;
901 21994 EncodeContext *ec = encode_ctx(avci);
902 21994 int ret = 0;
903
904
2/4
✓ Branch 0 taken 21994 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21994 times.
21994 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 21994 times.
21994 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 21881 times.
✓ Branch 1 taken 113 times.
21994 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE &&
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21881 times.
21881 !(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 20497 times.
✓ Branch 1 taken 1455 times.
✓ Branch 2 taken 42 times.
21994 switch (avctx->codec_type) {
922 20497 case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
923 1455 case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
924 }
925
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21994 times.
21994 if (ret < 0)
926 return ret;
927
928
4/4
✓ Branch 0 taken 1497 times.
✓ Branch 1 taken 20497 times.
✓ Branch 2 taken 1455 times.
✓ Branch 3 taken 42 times.
21994 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
929
3/4
✓ Branch 0 taken 21916 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21916 times.
21952 && 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 21992 times.
✓ Branch 1 taken 2 times.
21994 if (!avctx->rc_initial_buffer_occupancy)
934 21992 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
935
936
2/2
✓ Branch 0 taken 21545 times.
✓ Branch 1 taken 449 times.
21994 if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
937 21545 ec->intra_only_flag = AV_PKT_FLAG_KEY;
938
939
2/2
✓ Branch 1 taken 21952 times.
✓ Branch 2 taken 42 times.
21994 if (ffcodec(avctx->codec)->cb_type == FF_CODEC_CB_TYPE_ENCODE) {
940 21952 avci->in_frame = av_frame_alloc();
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21952 times.
21952 if (!avci->in_frame)
942 return AVERROR(ENOMEM);
943 }
944
945
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 21990 times.
21994 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 241934 times.
✓ Branch 1 taken 21994 times.
263928 for (int i = 0; ff_sd_global_map[i].packet < AV_PKT_DATA_NB; i++) {
958 241934 const enum AVPacketSideDataType type_packet = ff_sd_global_map[i].packet;
959 241934 const enum AVFrameSideDataType type_frame = ff_sd_global_map[i].frame;
960 const AVFrameSideData *sd_frame;
961 AVPacketSideData *sd_packet;
962
963 241934 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 241661 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
242207 if (!sd_frame ||
967 273 av_packet_side_data_get(avctx->coded_side_data, avctx->nb_coded_side_data,
968 type_packet))
969
970 241661 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 21994 ret = ff_frame_thread_encoder_init(avctx);
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21994 times.
21994 if (ret < 0)
983 return ret;
984 #endif
985
986 21994 return 0;
987 }
988
989 12655 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 12655 frame->format = avctx->pix_fmt;
996
3/4
✓ Branch 0 taken 12639 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12639 times.
12655 if (frame->width <= 0 || frame->height <= 0) {
997 16 frame->width = avctx->width;
998 16 frame->height = avctx->height;
999 }
1000
1001 12655 ret = avcodec_default_get_buffer2(avctx, frame, 0);
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12655 times.
12655 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 12655 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 21994 AVCodecInternal *ff_encode_internal_alloc(void)
1035 {
1036 21994 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 13101 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 13101 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
1077
1/2
✓ Branch 0 taken 13101 times.
✗ Branch 1 not taken.
13101 if (!side_data) {
1078 13101 side_data_size = 4+4+8*error_count;
1079 13101 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 13101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13101 times.
13101 if (!side_data || side_data_size < 4+4+8*error_count)
1084 return AVERROR(ENOMEM);
1085
1086 13101 AV_WL32(side_data, quality);
1087 13101 side_data[4] = pict_type;
1088 13101 side_data[5] = error_count;
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13101 times.
13101 for (int i = 0; i < error_count; ++i)
1090 AV_WL64(side_data+8 + 8*i , error[i]);
1091
1092 13101 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